【问题标题】:SpriteKit texture atlas leads to image artifacts and blank areasSpriteKit 纹理图集导致图像伪影和空白区域
【发布时间】:2021-02-04 20:12:21
【问题描述】:

我有一个 SpriteKit 游戏,其中有一个通过 SKShapeNode(splinePoints:count:) 创建的形状。

形状有一个fillTexture,通过textureNamed(_:)从纹理图集加载。

在我的纹理图集中有 5 张图片——其中 1 张用于形状。

如果我使用常规的 .xcassets 文件夹而不是图集,则形状的纹理正确。所以这绝对是地图集的问题。此外,如果纹理是图集中唯一的图像,则纹理可以正常工作。当我添加其他图像时,就会出现问题。

这是导致正确纹理的代码:

            var splinePoints = mainData.getSplinePointsForGround()
            let ground = SKShapeNode(splinePoints: &splinePoints, count: splinePoints.count)
            let texture = SKTexture(imageNamed: "groundTexture")
            
            ground.fillTexture = texture
            
            ground.lineWidth = 1
            ground.fillColor = UIColor(Color.purple)
            ground.strokeColor = UIColor(Color.clear)
            
            addChild(ground)

预期结果:

具有紫色渐变图像的形状应如下所示(请忽略虚线):

实际结果:

相反,形状看起来像这样,带有奇怪的空白区域和来自地图集中其他图像的少量伪影:

这是使用图集的代码版本:

            let textureAtlas = SKTextureAtlas(named: "assets")
            var splinePoints = mainData.getSplinePointsForGround()
            let ground = SKShapeNode(splinePoints: &splinePoints, count: splinePoints.count)
            let texture = textureAtlas.textureNamed("groundTexture2.png")
            
            ground.fillTexture = texture
            
            ground.lineWidth = 1
            ground.fillColor = UIColor(Color.purple)
            ground.strokeColor = UIColor(Color.clear)
            
            addChild(ground)

为什么会出现这个问题,我该如何解决?

谢谢!

【问题讨论】:

  • 你能包括它成功的方式吗? If I use the regular .xcassets folder instead of an atlas, the shape is textured correctly. So it's definitely an issue with the atlas.
  • @impression7vx 我编辑了问题以包含使用 .xcassets 正常工作的代码。谢谢。
  • 以“正确”的方式进行操作有什么问题?
  • @impression7vx 据我了解,使用纹理图集是一种性能增强,可以减少渲染场景所需的绘制调用次数。所以我想用它。

标签: swift sprite-kit


【解决方案1】:

看起来我已经解决了这个问题,但我不确定为什么我的解决方案有效。

我没有使用顶级.atlas 文件夹,而是将.spriteatlas 文件夹在里面 Assets.xcassets

无论出于何种原因,这都会导致显示正确的纹理,而不会出现任何令人讨厌的伪影或透明区域。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多