如何替换RealityKit中的模型义?

发布于 01-17 13:18 字数 2370 浏览 2 评论 0原文

我想通过单击按钮将 ModelEntity 从 fvBoatAnchor.fvBoatObject 切换到 fvBridgeAnchor.fvBridgeObject,但我不确定如何做到这一点,同时在更改后还保留手势和碰撞以及图像跟踪器实体。

这是我的代码

import UIKit
import RealityKit
import ARKit

class fvVessel: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
    @IBOutlet weak var imageView: UIImageView!
    
    let fvBoatAnchor = try! FV.loadFvBoatScene()
    let fvBridgeAnchor = try! FV.loadFvBridgeScene()
    
            var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
    
        override func viewDidLoad() {
                super.viewDidLoad()
                    let fvBoat = fvBoatAnchor.fvBoatObject as? Entity & HasCollision
                    arView.installGestures(for: fvBoat!)
                    
                    fvBoatAnchor.generateCollisionShapes(recursive: true)
                        arView.scene.addAnchor(fvBoatAnchor)
                        arView.session.delegate = self
                }
           
        func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
            anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                let anchorEntity = AnchorEntity()
                let modelEntity = fvBoatAnchor.fvBoatObject!
                anchorEntity.addChild(modelEntity)
                arView.scene.addAnchor(anchorEntity)
                anchorEntity.transform.matrix = $0.transform
                imageAnchorToEntity[$0] = anchorEntity
                
                imageView.isHidden = true
                
            }
            
        }
    
//    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
//        anchors.compactMap { $0 as? ARImageAnchor }.forEach {
//            let anchorEntity = imageAnchorToEntity[$0]
//            anchorEntity?.transform.matrix = $0.transform
//        }
//    }

    func installGestures(on object:ModelEntity){
        
        object.generateCollisionShapes(recursive: true)
        arView.installGestures(.all, for: object)
    }
    
     func leaveScene() {

        arView?.session.pause()
        arView?.session.delegate = nil
        arView?.scene.anchors.removeAll()
        arView?.removeFromSuperview()
        arView?.window?.resignKey()
        arView = nil
     }

    @IBAction func leaveScene(_ sender: UIButton) {
        leaveScene()
    }
}

I want to switch the ModelEntity from the fvBoatAnchor.fvBoatObject to the fvBridgeAnchor.fvBridgeObject with the click of a button, but I'm not sure how I do it, while also keeping the gestures and collisions as well as the image tracker after changing the entity.

Here is my code

import UIKit
import RealityKit
import ARKit

class fvVessel: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
    @IBOutlet weak var imageView: UIImageView!
    
    let fvBoatAnchor = try! FV.loadFvBoatScene()
    let fvBridgeAnchor = try! FV.loadFvBridgeScene()
    
            var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
    
        override func viewDidLoad() {
                super.viewDidLoad()
                    let fvBoat = fvBoatAnchor.fvBoatObject as? Entity & HasCollision
                    arView.installGestures(for: fvBoat!)
                    
                    fvBoatAnchor.generateCollisionShapes(recursive: true)
                        arView.scene.addAnchor(fvBoatAnchor)
                        arView.session.delegate = self
                }
           
        func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
            anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                let anchorEntity = AnchorEntity()
                let modelEntity = fvBoatAnchor.fvBoatObject!
                anchorEntity.addChild(modelEntity)
                arView.scene.addAnchor(anchorEntity)
                anchorEntity.transform.matrix = $0.transform
                imageAnchorToEntity[$0] = anchorEntity
                
                imageView.isHidden = true
                
            }
            
        }
    
//    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
//        anchors.compactMap { $0 as? ARImageAnchor }.forEach {
//            let anchorEntity = imageAnchorToEntity[$0]
//            anchorEntity?.transform.matrix = $0.transform
//        }
//    }

    func installGestures(on object:ModelEntity){
        
        object.generateCollisionShapes(recursive: true)
        arView.installGestures(.all, for: object)
    }
    
     func leaveScene() {

        arView?.session.pause()
        arView?.session.delegate = nil
        arView?.scene.anchors.removeAll()
        arView?.removeFromSuperview()
        arView?.window?.resignKey()
        arView = nil
     }

    @IBAction func leaveScene(_ sender: UIButton) {
        leaveScene()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文