我目前正在努力将 RealityKit 场景添加到 ARKit anchor 。正如我之前的问题,不可能直接将 RealityKit 场景添加到 ARKit anchor 节点。
但是我如何使用 ARKit 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/