gpt4 book ai didi

swift - ARKit 对象不是从保存的文件创建的

转载 作者:行者123 更新时间:2023-11-30 10:46:11 25 4
gpt4 key购买 nike

我正在从事一个使用 ARKit 的项目。我需要保存一个物体位置 例如,在我的家里,我在房间里设置了一把椅子/物体中心,几个小时后回到房间,我希望在 ARKit 中看到那个地方的椅子/物体.

我放置了椅子/物体并将其保存在文件中,文件保存成功。但是当我检索保存的文件并重新加载对象时。但该对象在 ARSession 中不可见

    let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal]

let options: ARSession.RunOptions = [.resetTracking, .removeExistingAnchors]
if let worldMap = worldMap {
configuration.initialWorldMap = worldMap
print("Found saved world map.")
self.showAlert("Found saved world map.", "")
} else {
print("Move camera around to map your surrounding space.")
}
sceneView.session.run(configuration, options: options)
sceneView.delegate = self

最佳答案

放置椅子时,您需要在场景中添加一个 anchor :

  let anchor = ARAnchor(name: "chair", transform: transform)
session.add(anchor: anchor)

然后,您实现 ARSCNViewDelegate 并将实际模型添加到 anchor 。

func session(_ session: ARSession, didAdd node: SCNNode, for anchor: ARAnchor) {
guard anchor.name == "chair" else { return }
node.addChildNode(chairNode)
}

那么发生了什么?当您保存 ARWorldMap 时,它包含所有 anchor ,但不包含 SceneKit 数据。这就是为什么我们需要首先添加最近保存的 anchor 并在委托(delegate)中添加几何图形。当您手动添加 anchor 时以及当您使用新的初始世界地图运行 session 后系统添加 anchor 时,都会调用委托(delegate)。

来自documentation :

The same ARSCNView delegate method renderer(_:didAdd:for:) fires both when you directly add an anchor to the session and when the session restores anchors from a world map. To determine which saved anchor represents the virtual object, this app uses the ARAnchor name property.

关于swift - ARKit 对象不是从保存的文件创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727456/

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