swift 在RealityKit中锚定多个场景

k7fdbhmy  于 2023-03-16  发布在  Swift
关注(0)|答案(1)|浏览(164)

将多个场景(来自Reality Composer)加载到arView中时,这些场景未锚定在同一空间中。
在本例中,scene1在应用启动时加载,按下按钮后,scene2被添加到场景中,在两个场景中,模型都被放置在原点,并期望与scene2重叠被添加到视图中,但是scene1和scene2在添加到arView中时的位置不同。

import UIKit  
import RealityKit  

class ViewController: UIViewController {  

    @IBOutlet var arView: ARView!  
    @IBOutlet weak var button: UIButton!  

    var scene1: Experience.Scene1!  
    var scene2: Experience.Scene2!  

    override func viewDidLoad() {  
        super.viewDidLoad()  

        // Load the "Box" scene from the "Experience" Reality File  
        scene1 = try! Experience.loadScene1()  
        scene2 = try! Experience.loadScene2()  

        // Add the box anchor to the scene  
        arView.scene.addAnchor(scene1)  
    }  

    @IBAction func buttonPressed(_ sender: Any) {  
        arView.scene.addAnchor(scene2)  
    }  

}

注意:同时添加两个场景时不会发生此问题。
如何确保两个场景都锚定在同一个ARAnchor上?

krcsximq

krcsximq1#

使用以下策略,以便跟踪两个型号的单个锚:

let scene01 = try! Cube.loadCube()
let scene02 = try! Ball.loadSphere()

let cubeEntity = scene01.steelCube!.children[0] as! ModelEntity
let ballEntity = scene02.glassBall!.children[0] as! ModelEntity

// var cubeComp: ModelComponent = cubeEntity.components[ModelComponent].self!
// var ballComp: ModelComponent = ballEntity.components[ModelComponent].self!

let anchor = AnchorEntity(plane: .horizontal)
anchor.addChild(cubeEntity)
anchor.addChild(ballEntity)

// scene01.steelCube!.components.set(cubeComp)
// scene02.glassBall!.components.set(ballComp)

arView.scene.anchors.append(anchor)

相关问题