SpriteKit 纹理 "scaling" 各不相同
SpriteKit texture "scaling" different from one to another
完全相同的纹理,不同的结果
这里 SKSpriteNode
和 SKShapeNode
被赋予相同的纹理,来自纹理图集。
SKSpriteNode
在右边,做对了。
SKShapeNode
在左边,做错了
拜托,我做错了什么?
这是代码...
override func didMove(to view: SKView) {
let myTextureAtlas = SKTextureAtlas(named: "demoArt")
let arrayOfFileNames = myTextureAtlas.textureNames
let myTexture = myTextureAtlas.textureNamed(arrayOfFileNames.last!)
let circleShape = SKShapeNode(circleOfRadius: 180)
circleShape.fillColor = SKColor.white
circleShape.fillTexture = myTexture
circleShape.position = CGPoint(x: 256, y: 384)
addChild(circleShape)
let circleSprite = SKSpriteNode(color: SKColor.white, size: CGSize(width: 320, height: 320))
circleSprite.texture = myTexture
circleSprite.position = (CGPoint(x: 768, y: 384))
addChild(circleSprite)
print(myTexture)
print(myTexture)
的结果是正确的:
<SKTexture>
'seven.png' (512 x 512)
如果您想对此进行测试,这里是进入 "atlas"...
的图像文件
我实现了你的代码并使用了一组不同的纹理(字母而不是数字),我得到了完全相同的行为。我怀疑 SKShapeNode 实现其 fillTexture 的方式与 SKSpriteNode 不同。
在我下面的代码中,我制作了四个节点。上面两个圆圈是 SKShapeNodes,下面两个是 SKSpriteNodes。
- 左上SKShapeNode 使用
SKTexture(imageNamed: "a.png")
- 右上SKShapeNode 使用
myTexture
- 左下SKSpriteNode使用
myTexture
- 右下SKSpriteNode使用
SKTexture(imageNamed: "a.png")
要解决您的问题,不要使用纹理图集,只需使用单独的 SKTexture,例如我示例中左上角的圆圈。
这是我的 GameScene.swift 文件:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let myTextureAtlas = SKTextureAtlas(named: "8bitfont")
let arrayOfFileNames = myTextureAtlas.textureNames
print(arrayOfFileNames)
let myTexture = myTextureAtlas.textureNamed("a.png")
// Top left circle
let circleShape1 = SKShapeNode(circleOfRadius: 160)
circleShape1.fillColor = SKColor.white
circleShape1.fillTexture = SKTexture(imageNamed: "a.png")
circleShape1.position = CGPoint(x: -180, y: 180)
addChild(circleShape1)
// Top right circle
let circleShape2 = SKShapeNode(circleOfRadius: 160)
circleShape2.fillColor = SKColor.white
circleShape2.fillTexture = myTexture as SKTexture
circleShape2.position = CGPoint(x: 180, y: 180)
addChild(circleShape2)
// Bottom left circle
let circleSprite1 = SKSpriteNode(color: SKColor.white, size: CGSize(width: 320, height: 320))
circleSprite1.texture = myTexture
circleSprite1.position = (CGPoint(x: -180, y: -180))
addChild(circleSprite1)
// Bottom right circle
let circleSprite2 = SKSpriteNode(color: SKColor.white, size: CGSize(width: 320, height: 320))
circleSprite2.texture = SKTexture(imageNamed: "a.png")
circleSprite2.position = (CGPoint(x: 180, y: -180))
addChild(circleSprite2)
print(myTexture)
}
}
这是我得到的结果:
这是 github 上的完整项目:
https://github.com/starkindustries/SKShapeNodeVsSKSpriteNode
这不是错误(原样需要),SKShapeNode
默认缩放填充纹理,而不是重复图案,并且您无法直接控制此缩放。
如 API 参考资料所述:
Shape nodes are useful for content that cannot be easily decomposed
into simple textured sprites. Shape nodes are also very useful for
building and displaying debugging information on top of your game
content. However, the SKSpriteNode class offers higher performance
than this class, so use shape nodes sparingly.
在这些规定之外使用 SKShapeNode
会不必要地降低性能并且会产生视觉缺陷。
完全相同的纹理,不同的结果
这里 SKSpriteNode
和 SKShapeNode
被赋予相同的纹理,来自纹理图集。
SKSpriteNode
在右边,做对了。
SKShapeNode
在左边,做错了
拜托,我做错了什么?
这是代码...
override func didMove(to view: SKView) {
let myTextureAtlas = SKTextureAtlas(named: "demoArt")
let arrayOfFileNames = myTextureAtlas.textureNames
let myTexture = myTextureAtlas.textureNamed(arrayOfFileNames.last!)
let circleShape = SKShapeNode(circleOfRadius: 180)
circleShape.fillColor = SKColor.white
circleShape.fillTexture = myTexture
circleShape.position = CGPoint(x: 256, y: 384)
addChild(circleShape)
let circleSprite = SKSpriteNode(color: SKColor.white, size: CGSize(width: 320, height: 320))
circleSprite.texture = myTexture
circleSprite.position = (CGPoint(x: 768, y: 384))
addChild(circleSprite)
print(myTexture)
print(myTexture)
的结果是正确的:
<SKTexture>
'seven.png' (512 x 512)
如果您想对此进行测试,这里是进入 "atlas"...
的图像文件我实现了你的代码并使用了一组不同的纹理(字母而不是数字),我得到了完全相同的行为。我怀疑 SKShapeNode 实现其 fillTexture 的方式与 SKSpriteNode 不同。
在我下面的代码中,我制作了四个节点。上面两个圆圈是 SKShapeNodes,下面两个是 SKSpriteNodes。
- 左上SKShapeNode 使用
SKTexture(imageNamed: "a.png")
- 右上SKShapeNode 使用
myTexture
- 左下SKSpriteNode使用
myTexture
- 右下SKSpriteNode使用
SKTexture(imageNamed: "a.png")
要解决您的问题,不要使用纹理图集,只需使用单独的 SKTexture,例如我示例中左上角的圆圈。
这是我的 GameScene.swift 文件:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let myTextureAtlas = SKTextureAtlas(named: "8bitfont")
let arrayOfFileNames = myTextureAtlas.textureNames
print(arrayOfFileNames)
let myTexture = myTextureAtlas.textureNamed("a.png")
// Top left circle
let circleShape1 = SKShapeNode(circleOfRadius: 160)
circleShape1.fillColor = SKColor.white
circleShape1.fillTexture = SKTexture(imageNamed: "a.png")
circleShape1.position = CGPoint(x: -180, y: 180)
addChild(circleShape1)
// Top right circle
let circleShape2 = SKShapeNode(circleOfRadius: 160)
circleShape2.fillColor = SKColor.white
circleShape2.fillTexture = myTexture as SKTexture
circleShape2.position = CGPoint(x: 180, y: 180)
addChild(circleShape2)
// Bottom left circle
let circleSprite1 = SKSpriteNode(color: SKColor.white, size: CGSize(width: 320, height: 320))
circleSprite1.texture = myTexture
circleSprite1.position = (CGPoint(x: -180, y: -180))
addChild(circleSprite1)
// Bottom right circle
let circleSprite2 = SKSpriteNode(color: SKColor.white, size: CGSize(width: 320, height: 320))
circleSprite2.texture = SKTexture(imageNamed: "a.png")
circleSprite2.position = (CGPoint(x: 180, y: -180))
addChild(circleSprite2)
print(myTexture)
}
}
这是我得到的结果:
这是 github 上的完整项目:
https://github.com/starkindustries/SKShapeNodeVsSKSpriteNode
这不是错误(原样需要),SKShapeNode
默认缩放填充纹理,而不是重复图案,并且您无法直接控制此缩放。
如 API 参考资料所述:
Shape nodes are useful for content that cannot be easily decomposed into simple textured sprites. Shape nodes are also very useful for building and displaying debugging information on top of your game content. However, the SKSpriteNode class offers higher performance than this class, so use shape nodes sparingly.
在这些规定之外使用 SKShapeNode
会不必要地降低性能并且会产生视觉缺陷。