【问题标题】:How do you use SKTextureFilteringNearest with SKTextureAtlas你如何使用 SKTextureFilteringNearest 和 SKTextureAtlas
【发布时间】:2013-11-15 01:48:43
【问题描述】:

我似乎在使用 SKTextureAtlas 和最近邻过滤纹理时遇到问题。当我使用没有 SKTextureAtlas 的最近邻过滤时,它工作正常,但是当我使用 SKTextureAtlas 时,一切都变成了线性过滤。

没有 SKTextureAtlas 的代码和结果:

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

应该产生最近邻过滤和做

使用 SKTextureAtlas 的代码和结果:

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

应该产生最近邻过滤&

有什么建议吗?

【问题讨论】:

  • 试试 spriteNodeWithTexture: 不指定大小,有区别吗?对我来说,最近的带有 atlas 纹理的过滤器有效,但我还没有尝试在 init 上更改 sprite rect。通过 xScale/yScale 创建精灵后,您仍然可以缩放精灵。
  • 好吧,我试过了,我得到了同样的结果:(
  • 我无法按照您的方式工作,但不是在 init 中设置大小,之后我将大小设置为 [node setSize:]; 并且它有效
  • 我正在使用 64x64 纹理,并且我有一个名为 GrassNode 的对象,它继承自 SKSpriteNode,我最初的大小和比例代码失败了。现在,当我在场景中声明 GrassNode 并使用 [grass setSize:CGSizeMake(1,1)];它仍然失败。你能告诉我你的布局,也许我能弄清楚。
  • 如果发现我的问题没有真正的解决方案,我可能会升级我的纹理,以便它们在更高的分辨率下保持清晰。

标签: ios objective-c sprite-kit


【解决方案1】:

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

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

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

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

祝你好运。

【讨论】:

  • 这不是同一个纹理图集的两个不同实例。它实际上是两个物理上不同的文件夹(pixelArt.atlas 和 normalTextures.atlas)。当我加载它们时,我在它们上设置了过滤模式,它会影响所有带有纹理的精灵。
【解决方案2】:

这确实是一个奇怪的问题。在我看来,缺少一个 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;
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      相关资源
      最近更新 更多