Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

swift - SpriteKit texture atlas leads to image artifacts and blank areas

I have a SpriteKit game in which there's a shape created via SKShapeNode(splinePoints:count:).

The shape has a fillTexture, loaded from a texture atlas via textureNamed(_:).

In my texture atlas there are 5 images -- 1 of which is used for the shape.

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. Also, the texture works correctly if it's the only image in the atlas. It's when I add additional images that the problem occurs.

Here's the code that results in the correct texture:

            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)

Expected results:

The shape, which has a purple gradient image, should look like this (please ignore the dotted white line):

enter image description here

Actual results:

Instead, the shape looks like this, with strange blank areas and little artifacts from the other images located in the atlas:

enter image description here

Here's the version of code that uses the atlas:

            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)

Why is this problem happening, and how do I resolve it?

Thank you!

question from:https://stackoverflow.com/questions/66053157/spritekit-texture-atlas-leads-to-image-artifacts-and-blank-areas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...