Привязка нескольких сцен в RealityKit

При загрузке нескольких сцен (из композитора реальности) в arView сцены не привязаны к одному и тому же пространству.

В этом примере scene1 загружается при запуске приложения. После нажатия кнопки в сцену добавляется сцена 2. В обеих сценах модели помещаются в начало координат и, как ожидается, будут перекрываться, а в вид добавляется сцена 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?


person Arihant Parsoya    schedule 12.05.2020    source источник
comment
Что произойдет, если вместо этого вы сделаете arView.scene.anchors.append(scene2)?   -  person iicaptain    schedule 06.06.2020


Ответы (1)


Используйте следующий подход:

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)
person Andy Fedoroff    schedule 08.06.2020