Swift 如何在ARKit中实现基于距离的动态缩放
我有一个简单的SCNNode,它被放置在真实世界的位置,节点对应于一个具有已知坐标的地标。就用户而言,我希望SCNNode具有相同的大小,尽管节点位置和用户/ARcamera之间的距离发生变化。这是到目前为止我的代码,它是一种基于距离计算刻度的非常粗糙的方法Swift 如何在ARKit中实现基于距离的动态缩放,swift,scenekit,arkit,Swift,Scenekit,Arkit,我有一个简单的SCNNode,它被放置在真实世界的位置,节点对应于一个具有已知坐标的地标。就用户而言,我希望SCNNode具有相同的大小,尽管节点位置和用户/ARcamera之间的距离发生变化。这是到目前为止我的代码,它是一种基于距离计算刻度的非常粗糙的方法 func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) { self.update
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
self.updateScaleOfNodes(node: nodeAdded, fromPointOfView: self.sceneView.pointOfView!)
}
func updateScaleOfNodes(node: SCNNode, fromPointOfView pointofView: SCNNode){
//Find the 3D objects position
let nodePosition = SCNVector3ToGLKVector3(node.worldPosition)
//Find the cameras current position
let currentCameraPosition = SCNVector3ToGLKVector3(pointofView.worldPosition)
//Find the distance between camera and 3D object
let distanceBetweenCameraAndNode = GLKVector3Distance(nodePosition, currentCameraPosition)
self.locationAcc.isHidden = false
let distance_test = "Distance between camera and node:" + String(distanceBetweenCameraAndNode)
self.locationAcc.text = distance_test
//180 metres
//NOTE: Do not calculate delta distance between node and current position because that focuses on the size of each step rather than how far away your current position is from the node
//Begin animation
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.3
//Multiply distance by scaling factor
//Each metre you walk away the node scales down by 1cm
let scale:Float = 1/1000 * distanceBetweenCameraAndNode
//Calculate the scale difference (moving closer will make node larger)
let scaleDiff:Float = 1 - scale
//Assign the scale to 3D object
node.scale = SCNVector3(scaleDiff * Float(scale_W), scaleDiff * Float(scale_H), scaleDiff * Float(scale_l))
SCNTransaction.commit()
}
当我测试这段代码时,我发现一些对象往往在其他对象无法正常工作的情况下工作。是否有更好的解决方案可以应用于所有不同维度的对象?我已经研究了一个使用视野的解决方案,但我不确定它是否比这个更好。如有任何建议,将不胜感激
提前感谢您找到解决方案了吗,我也被困在这个问题上