2

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)
quemeful
  • 9,542
  • 4
  • 60
  • 69

2 Answers2

2

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

mnuages
  • 13,049
  • 2
  • 23
  • 40
1

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)
pacification
  • 5,838
  • 4
  • 29
  • 51
Tony
  • 1,551
  • 20
  • 21