swift - 使用ARKit anchor 放置RealityKit场景

标签 swift swiftui augmented-reality arkit realitykit

我目前正在努力将 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/

相关文章:

swift - 在 Swift 中使用默认参数获取对实例变量的引用

macos - 如何在 SwiftUI 中为文本预留空间

image - SwiftUI - 在列表/表单中显示图像,单元格周围没有边框

javascript - 通过移动设备相机检测特定图像

javascript - 增强现实库支持 React Native?

ios - 如何确定何时从 Swift 中的一组图像下载了所有图像?

ios - 使用 tableview 设置 uisearchdisplaycontroller 时出现问题 - Swift

swiftui - 如何使用 JSON 在 SwiftUI 中对列表进行排序

swift - ARKit Stereo – 是否可以同时运行两个 ARSCNView?

swift - UIPanGestureRecognizer translationInView 与 locationInView