Reputation: 240
I am trying to have an SCNNode with plane geometry, which presents 2d text inside it. I'm doing that through setting SKLabelNode as geometry's material (took that from here)
Here's my code:
let label = SKLabelNode(text: "Test Text 1")
label.fontColor = .white
label.fontName = UIFont.systemFont(ofSize: 32).fontName
label.fontSize = 32
let backgroundRectShape = SKShapeNode(rect: CGRect(origin: .zero, size: label.frame.size))
backgroundRectShape.fillColor = .orange
backgroundRectShape.addChild(label)
label.position = CGPoint(x: backgroundRectShape.frame.width / 2, y: 0)
let skScene = SKScene(size: label.frame.size)
skScene.addChild(backgroundRectShape)
backgroundRectShape.position = .zero
let plane = SCNPlane(width: label.frame.width / 10, height: label.frame.height / 10)
plane.firstMaterial?.diffuse.contents = skScene
let node = SCNNode(geometry: plane)
scene.rootNode.addChildNode(node)
Everything seems fine, but the SKLabelNode appears upside-down:
The SCNNode is surely properly aligned and not rotated, because when I set an image as material content, everything is ok:
I just can't understand why it is happening. The easiest solution would be setting SKLabelNode
's yScale
to -1, but I don't want to do that without understanding the reason why it appears upside down by default.
Upvotes: 9
Views: 940
Reputation: 6255
plane.firstMaterial?.diffuse.contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0)
Upvotes: 6
Reputation: 58563
It is seemingly a bug or one of the many limitations of SpriteKit
/SceneKit
framework.
I suppose SCNMaterial's content erroneously appears upside-down due to the origin of iOS coordinate system.
But instead of scaling the SCNNode itself you should better mirror a texture along Y-axis
using contentsTransform
instance property of the SCNMaterial
. Like so:
material.diffuse.contentsTransform = SCNMatrix4MakeScale(1, -1, 1)
Hope this helps.
Upvotes: 1