首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何指定相对于ARImageAnchor的子节点位置?

如何指定相对于ARImageAnchor的子节点位置?
EN

Stack Overflow用户
提问于 2022-02-23 17:47:10
回答 1查看 263关注 0票数 1

我有一个RealityKit应用程序正在做一些基本的AR图像跟踪。它检测到一个矩形的图像,我期待着在图像的每个角落放置一些球形点。我知道我可以使用ModelEntity自己创建球体,但我还没有弄清楚如何从参考图像中指定这些球相对于已建立的ARImageAnchor的位置。

我想我只需要与SceneKit的addChildNode(SCNNode)函数相对应,它使用SCNVector3Make()来指定一个职位。我只是无法找到一种方法来建立一个相对位置,并将一个子节点分配给这些ARImageAnchor函数之外的SceneKit。在RealityKit中是否有内置的东西可以实现这一点,或者是否有一种方法可以使用SceneKit来放置角点,同时仍然使用我目前在RealityKit中的设置来进行AR参考图像跟踪?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-26 11:43:31

尝试以下方法:

代码语言:javascript
复制
import ARKit
import RealityKit
 
class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    var anchorEntity = AnchorEntity()
    let model_01 = ModelEntity(mesh: .generateSphere(radius: 0.03))
    let model_02 = ModelEntity(mesh: .generateSphere(radius: 0.03))
    let model_03 = ModelEntity(mesh: .generateSphere(radius: 0.03))
    let model_04 = ModelEntity(mesh: .generateSphere(radius: 0.03))
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)            
        arView.session.delegate = self
        
        guard let reference = ARReferenceImage.referenceImages(
                                                inGroupNamed: "AR Resources",
                                                      bundle: nil)
        else { return }
        
        let config = ARImageTrackingConfiguration()
        config.trackingImages = reference
        arView.session.run(config)
        
        self.anchorEntity.addChild(model_01)
        self.anchorEntity.addChild(model_02)
        self.anchorEntity.addChild(model_03)
        self.anchorEntity.addChild(model_04)    
        arView.scene.anchors.append(self.anchorEntity)
    }
}

然后实现session(_:didUpdate:)方法:

代码语言:javascript
复制
extension ViewController: ARSessionDelegate {
    
    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        
        guard let imageAnchor = anchors.first as? ARImageAnchor
        else { return }
        
        let width = Float(imageAnchor.referenceImage.physicalSize.width)
        let height = Float(imageAnchor.referenceImage.physicalSize.height)
        let x = imageAnchor.transform.columns.3.x
        let y = imageAnchor.transform.columns.3.y
        let z = imageAnchor.transform.columns.3.z
        
        let lowerLeft = SIMD3<Float>(x - width/2, y - height/2, z)
        let lowerRight = SIMD3<Float>(x + width/2, y - height/2, z)
        let upperRight = SIMD3<Float>(x + width/2, y + height/2, z)
        let upperLeft = SIMD3<Float>(x - width/2, y + height/2, z)
        
        self.model_01.position = lowerLeft
        self.model_02.position = lowerRight
        self.model_03.position = upperRight
        self.model_04.position = upperLeft
        
        self.anchorEntity = AnchorEntity(anchor: imageAnchor)
    }
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71241779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档