首页 > 解决方案 > SpriteKit 纹理图集导致图像伪影和空白区域

问题描述

我有一个 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)

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

谢谢!

标签: swiftsprite-kit

解决方案


It looks like I've solved the problem, though I'm not sure why my solution works.

Instead of having a top-level .atlas folder, I've put a .spriteatlas folder inside Assets.xcassets.

For whatever reason, this results in the correct textures being shown without any nasty artifacts or transparent areas.


推荐阅读