gpt4 book ai didi

swift - 使用 ARKit anchor 放置 RealityKit 场景

转载 作者:行者123 更新时间:2023-12-04 08:25:08 35 4
gpt4 key购买 nike

我目前正在努力将 RealityKit 场景添加到 ARKit anchor 。正如我之前的问题,无法将 RealityKit 场景直接添加到 ARKit anchor 节点。
但是如何使用 ARKit Anchor 来放置我的场景?
例如,我想在房间中找到一个物体,根据我找到的物体,我想使用不同的场景并根据识别的物体位置将它们 anchor 定在房间中。
谢谢您的帮助。
当前尝试:

func makeUIView(context: Context) -> ARView {

let arView = ARView(frame: .zero)

// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()

// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)

let configuration = ARWorldTrackingConfiguration()
guard let referenceObjects = ARReferenceObject.referenceObjects(
inGroupNamed: "AR Objects", bundle: nil) else {
fatalError("Missing expected asset catalog resources.")
}

configuration.detectionObjects = referenceObjects
arView.session.run(configuration) // not possible at arview, but i need arview for the .rcproject :-/

return arView

}
编辑:添加代码

最佳答案

ARKit 没有节点。 SceneKit 可以。
但是,如果您需要使用 ARKit 的 anchor 将 RealityKit 的模型放入 AR 场景中,就这么简单:

import ARKit
import RealityKit

class ViewController: UIViewController {

@IBOutlet var arView: ARView!
let boxScene = try! Experience.loadBox()

override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self

let entity = boxScene.steelBox

var transform = simd_float4x4(diagonal: [1,1,1,1])
transform.columns.3.z = -3.0 // three meters away

let arkitAnchor = ARAnchor(name: "ARKit anchor",
transform: transform)

let anchorEntity = AnchorEntity(anchor: arkitAnchor)
anchorEntity.name = "RealityKit anchor"

arView.session.add(anchor: arkitAnchor)
anchor.addChild(entity)
arView.scene.anchors.append(anchorEntity)
}
}
由于 AnchorEntity 便利初始值设定项,这成为可能:
convenience init(anchor: ARAnchor)

您也可以使用 session(_:didAdd:)session(_:didUpdate:)实例方法:
extension ViewController: ARSessionDelegate {

func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {

guard let objectAnchor = anchors.first as? ARObjectAnchor
else { return }

let anchor = AnchorEntity(anchor: objectAnchor)
let entity = boxScene.steelBox
anchor.addChild(entity)
arView.session.add(anchor: objectAnchor)
arView.scene.anchors.append(anchor)
}
}

关于swift - 使用 ARKit anchor 放置 RealityKit 场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65304656/

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