在将SCNNode添加到ARSCNView场景后,我正在尝试获得锚。我的理解是,应该自动创建锚,但我似乎无法检索它。
下面是我怎么加的。节点保存在一个名为testNode的变量中。
let node = SCNNode()
node.geometry = SCNBox(width: 0.5, height: 0.1, length: 0.3, chamferRadius: 0)
node.geometry?.firstMaterial?.diffuse.contents = UIColor.green
sceneView.scene.rootNode.addChildNode(node)
testNode = node以下是我试图找回它的方法。总是印不出来。
if let testNode = testNode {
print(sceneView.anchor(for: testNode))
}它不是创造了锚吗?如果是这样的话:还有其他方法可以用来检索吗?
发布于 2018-03-27 12:12:39
如果您查看一下Apple Docs,就会发现:
要跟踪真实或虚拟对象相对于摄像机的位置和方向,请创建定位对象并使用add (锚:)方法将它们添加到AR会话中。
因此,我认为由于您没有使用PlaneDetection,因此需要手动创建一个ARAnchor (如果需要):
每当您放置虚拟对象时,始终将表示其位置和方向的ARAnchor添加到ARSession中。移动虚拟对象后,移除旧位置的锚,并在新位置创建新锚。添加一个锚告诉ARKit,一个位置很重要,可以提高该区域的世界跟踪质量,并帮助虚拟对象相对于真实的表面保持位置。
您可以在下面的线程What's the difference between using ARAnchor to insert a node and directly insert a node?中阅读更多有关这方面的内容。
总之,为了让您开始工作,我首先创建了一个名为SCNNode的currentNode:
var currentNode: SCNNode?然后使用UITapGestureRecognizer,我在touchLocation上手动创建了一个ARAnchor。
@objc func handleTap(_ gesture: UITapGestureRecognizer){
//1. Get The Current Touch Location
let currentTouchLocation = gesture.location(in: self.augmentedRealityView)
//2. If We Have Hit A Feature Point Get The Result
if let hitTest = augmentedRealityView.hitTest(currentTouchLocation, types: [.featurePoint]).last {
//2. Create An Anchore At The World Transform
let anchor = ARAnchor(transform: hitTest.worldTransform)
//3. Add It To The Scene
augmentedRealitySession.add(anchor: anchor)
}
}添加了锚后,我使用ARSCNViewDelegate回调来创建currentNode,如下所示:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if currentNode == nil{
currentNode = SCNNode()
let nodeGeometry = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
nodeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
currentNode?.geometry = nodeGeometry
currentNode?.position = SCNVector3(anchor.transform.columns.3.x, anchor.transform.columns.3.y, anchor.transform.columns.3.z)
node.addChildNode(currentNode!)
}
}为了测试它是否有效,例如能够记录相应的ARAnchor,我更改了tapGesture方法,以便在最后包含以下内容:
if let anchorHitTest = augmentedRealityView.hitTest(currentTouchLocation, options: nil).first,{
print(augmentedRealityView.anchor(for: anchorHitTest.node))
}在我的ConsoleLog打印中:
Optional(<ARAnchor: 0x1c0535680 identifier="23CFF447-68E9-451D-A64D-17C972EB5F4B" transform=<translation=(-0.006610 -0.095542 -0.357221) rotation=(-0.00° 0.00° 0.00°)>>)希望能帮上忙..。
https://stackoverflow.com/questions/49508106
复制相似问题