我目前正在编写一个 AR 应用程序,首先要放置一个 SCNNode(表)。首先,应用程序搜索水平面,每当找到表面时,就会显示该对象,但尚未放置。虽然没有放置它,但我希望该对象始终面向相机,但我很难找到相机的当前位置,也无法每帧更新该对象。
当前,对象面向世界坐标。因此,当我启动应用程序并找到水平面时 - 该对象出现并面向世界坐标的起点(无论我启动应用程序的位置)
任何人都可以帮助我,如何从相机位置获取矢量并使对象每帧更新其方向?
var tableNode : SCNNode! // Node of the actual table
var trackingPosition = SCNVector3Make(0.0, 0.0, 0.0)
let trans = SCNMatrix4(hitTest.worldTransform)
self.trackingPosition = SCNVector3Make(trans.m41, trans.m42, trans.m43)
self.tableNode.position = self.trackingPosition
最佳答案
您尝试实现的功能称为广告牌。要实现此效果,您需要使用场景的 .pointOfView
作为目标来设置 SCNLookAtConstraint
。
您可以删除
self.trackingPosition = SCNVector3Make(trans.m41, trans.m42, trans.m43)
self.tableNode.position = self.trackingPosition
并使用:
// set constraint to force the node to always face camera
let constraint = SCNLookAtConstraint(target:sceneView.pointOfView)
constraint.isGimbalLockEnabled = true
self.tableNode.constraints = [constraint] // constraint can cause node appear flipped over X axis
关于swift - 如何让SCNNode面向当前相机位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56612591/