1

将多个场景(来自现实作曲家)加载到 arView 中时,场景并未锚定在同一空间中。

在此示例中,scene1 在应用程序启动时加载。按下按钮后,scene2 被添加到场景中。在这两个场景中,模型都放置在原点并预计与场景2 重叠添加到视图中。但是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 上?

4

1 回答 1

0

使用以下方法:

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

let cubeEntity: Entity = scene01.steelCube!.children[0]
let ballEntity: Entity = scene02.glassBall!.children[0]

// var cubeComponent: ModelComponent = cubeEntity.components[ModelComponent].self!
// var ballComponent: ModelComponent = ballEntity.components[ModelComponent].self!

let anchor = AnchorEntity()
anchor.addChild(cubeEntity)
anchor.addChild(ballEntity)

// scene01.steelCube!.components.set(cubeComponent)
// scene02.glassBall!.components.set(ballComponent)
arView.scene.anchors.append(anchor)
于 2020-06-08T09:24:51.473 回答