gpt4 book ai didi

swift - SCNNode 的 ARAnchor

转载 作者:搜寻专家 更新时间:2023-10-31 22:09:53 25 4
gpt4 key购买 nike

在将 SCNNode 添加到 ARSCNView 的场景后,我试图获取 anchor 。我的理解是应该自动创建 anchor ,但我似乎无法检索它。

下面是我添加它的方法。该节点保存在一个名为 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

这是我尝试检索它的方法。它总是打印 nil。

if let testNode = testNode {
print(sceneView.anchor(for: testNode))
}

它不创建 anchor 吗?如果是这样:我可以使用另一种方法来检索它吗?

最佳答案

如果您查看 Apple Docs,它指出:

To track the positions and orientations of real or virtual objects relative to the camera, create anchor objects and use the add(anchor:) method to add them to your AR session.

因此,我认为由于您没有使用 PlaneDetection,如果需要,您需要手动创建一个 ARAnchor:

Whenever you place a virtual object, always add an ARAnchor representing its position and orientation to the ARSession. After moving a virtual object, remove the anchor at the old position and create a new anchor at the new position. Adding an anchor tells ARKit that a position is important, improving world tracking quality in that area and helping virtual objects appear to stay in place relative to real-world surfaces.

您可以在以下主题中阅读更多相关信息 What's the difference between using ARAnchor to insert a node and directly insert a node?

无论如何,为了让您开始,我首先创建了一个名为 currentNode 的 SCNNode:

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)


}

}

添加 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°)>>)

希望对你有帮助

关于swift - SCNNode 的 ARAnchor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49508106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com