The SpriteKit scene coordinate system doesn't map to texture coordinates in the same way an image does. Pixel coordinates in an image have the y-axis increasing downward; in a SpriteKit scene, the y-axis goes up.
This is a lot more apparent if you just map that SpriteKit scene onto an SCNPlane
that's the only thing in a SceneKit scene — you'll see that the image is flipped.
Left: Image as loaded from bundle, Right: Image filling an SKScene
mapped to a plane (with annotations)
To fix this, you'll need a two-step coordinate transformation:
SKScene
您可以内做这一点SKScene
,或者在SCNMaterialProperty
该SpriteKit内容到SceneKit对象纹理映射。我赞成这种SCNMaterialProperty
方法-为其contentsTransform
属性设置适当的矩阵:
let translate = SCNMatrix4MakeTranslation(0, 1, 0)
let yFlippedTranslate = SCNMatrix4Scale(translate, 1, -1, 1)
material.diffuse.contentsTransform = yFlippedTranslate
请注意,如果您还尝试使用材质的纹理坐标(通过SCNHitTestResult
)将触摸/单击事件映射到SpriteKit场景中,则需要在此处执行类似的转换:
let texcoord = result.textureCoordinatesWithMappingChannel(0)
sprite.position.x = texcoord.x * skScene.size.width
sprite.position.y = (1 - texcoord.y) * skScene.size.height
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句