将多个场景(来自 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 上?
使用以下策略,以便跟踪两个模型的单个锚点:
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)