quemeful
quemeful

Reputation: 9848

Rotate SCNText Node around center of itself (Swift - Scenekit)

I am trying to rotate some 3D text in place. How do I rotate it around the center of itself, not the left edge? I've tried setting the text alignment to center and that didn't help either.

let titleText = SCNText(string: "My Title", extrusionDepth: 0.8)  
let titleNode = SCNNode(geometry: titleText)

titleNode.position = SCNVector3(x: 0, y: 0, z: 0)
titleNode.runAction(SCNAction.rotateByX(0, y: 5, z: 0, duration: 2.5))
scene.rootNode.addChildNode(titleNode)

Upvotes: 2

Views: 2976

Answers (2)

Tony
Tony

Reputation: 1611

Setup pivot of the SCNText is a bit tricky (look here for more details)

Bellow is the code to setup pivot and rotate the text:

let geoText = SCNText(string: "Hello", extrusionDepth: 1.0)
geoText.font = UIFont (name: "Arial", size: 8)
geoText.firstMaterial!.diffuse.contents = UIColor.red
let textNode = SCNNode(geometry: geoText)

let (minVec, maxVec) = textNode.boundingBox
textNode.pivot = SCNMatrix4MakeTranslation((maxVec.x - minVec.x) / 2 + minVec.x, (maxVec.y - minVec.y) / 2 + minVec.y, 0)
scnView.scene?.rootNode.addChildNode(textNode)

let loop = SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 5, z: 0, duration: 2.5))
textNode.runAction(loop)

Upvotes: 1

mnuages
mnuages

Reputation: 13462

you can set the node's pivot property to half its width and height (see getBoundingBoxMin:max:)

Upvotes: 2

Related Questions