ARKit billboarding effect with SceneKit

a 夏天 提交于 2019-12-06 07:28:11

You were almost there!

Just modify the text's node's pivot to rotate it by 180 degrees (Obj-C).

node.pivot = SCNMatrix4MakeRotation(M_PI, 0, 1, 0);

You should check out SCNBillboardConstraint

Updated Code

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

// Update object's pivot to its center
// https://stackoverflow.com/questions/44828764/arkit-placing-an-scntext-at-a-particular-point-in-front-of-the-camera
let (min, max) = textGeometry.boundingBox
let dx = min.x + 0.5 * (max.x - min.x)
let dy = min.y + 0.5 * (max.y - min.y)
let dz = min.z + 0.5 * (max.z - min.z)
textNode.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)

textNode.scale = SCNVector3(0.01, 0.01, 0.01)

let plane = SCNPlane(width: 0.2, height: 0.2)
let blueMaterial = SCNMaterial()
blueMaterial.diffuse.contents = UIColor.blue
plane.firstMaterial = blueMaterial
let parentNode = SCNNode(geometry: plane) // this node will hold our text node

let yFreeConstraint = SCNBillboardConstraint()
yFreeConstraint.freeAxes = .Y // optionally
parentNode.constraints = [yFreeConstraint] // apply the constraint to the parent node

parentNode.position = SCNVector3(0, 0, -0.5)
parentNode.addChildNode(textNode)

sceneView.scene.rootNode.addChildNode(parentNode) // add our text holder to the scene

It seems that applying billboard constraint directly to the text node resets its position and scale, so the text node gets huge and positioned at 0,0,0 relative to the camera. Don't know why :( But applying the constraint to the parent node works fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!