9

I seem to be having a problem using SKTextureAtlas and nearest neighbor filtering for textures. When I used the nearest neighbor filtering without SKTextureAtlas it works fine, but everything is just changed to linear filtering when I use an SKTextureAtlas.

Code and Result Without SKTextureAtlas:

SKTexture* texture = [SKTexture textureWithImageNamed:@"grass"];
texture.filteringMode = SKTextureFilteringNearest;
SKSpriteNode* node = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(512,512)];

Should Produce Nearest Neighbor Filtering & Does enter image description here

Code and Result With SKTextureAtlas:

SKTextureAtlas* atlas = [SKTextureAtlas atlasNamed:@"myAtlas"];
SKTexture* texture = [atlas textureNamed:@"grass"];
texture.filteringMode = SKTextureFilteringNearest;
SKSpriteNode* node = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(512,512)];

Should Produce Nearest Neighbor Filtering & DOES NOT enter image description here

Any suggestions?

4

3 回答 3

4

我一直在努力解决完全相同的问题。

似乎当您为来自 SKTextureAtlas 的 SKTexture 设置过滤模式时,它会为来自该图集的所有内容设置过滤模式。

我最终通过制作两个 SKTextureAtlas(AtlasLinear 和 AtlasNearest)来解决它。一个用于我的正常艺术品,另一个用于像素艺术。这是一种魅力。

但是,对于非常小的图集,我有时会遇到奇怪的小像素错误。如果这为您弹出,它实际上有助于将一些大的白色块 png 添加到您的像素艺术图集中。

祝你好运。

于 2014-01-17T10:06:25.470 回答
3

这确实是一个奇怪的问题。在我看来,缺少一个 API:[SKTextureAtlas atlasNamed:atlasName withFilteringMode:filteringMode]

暂时代替这样的API,我使用下面的方法:

-(SKTextureAtlas *) textureAtlasWithName:(NSString *)atlasName filteringMode:(SKTextureFilteringMode)filteringMode {

    SKTextureAtlas * result = [SKTextureAtlas atlasNamed:atlasName];

    NSArray * textureNames = [result textureNames];

    if ([textureNames count] > 0) {
        SKTexture * aTexture = [result textureNamed:[textureNames lastObject]];
        [aTexture setFilteringMode:filteringMode];

    } else {
        NSLog(@"WARNING: couldn't find any textures in the atlas %@; filtering mode set likely failed.", result);
    }

    return result;
}
于 2014-05-24T20:25:34.953 回答
1

我设法在我的程序中解决了这个问题。我发现如果纹理图集被实例化多次,即使来自图集的所有纹理加载都设置为 SKTextureFilteringNearest,纹理也会使用线性过滤进行渲染。我最终做的是通过单例提供我的图集,并确保所有纹理加载时都将过滤设置为 SKTextureFilteringNearest,并且效果很好。

于 2014-01-19T01:44:44.780 回答