虽然将多个场景(从Reality Composer中加载)到arView中,但这些场景并未锚定在同一空间中。
在此示例中,场景1是在应用启动时加载的。按下按钮后,将场景2添加到场景中。在两个场景中,模型都放置在原点,并且预期与场景2重叠,从而将模型添加到视图中。但是,当将场景1和场景2添加到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?
使用以下方法:
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)