2

When I launch my app, I do see my scene loaded but I'm not able to use the translate, rotate or scale gestures to manipulate the scene.

Would like some help to make this work?

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load scene from .rcproject
        let scene = try! Project.loadScene()

        // Add scene to the AR view's scene
        arView.scene.addAnchor(scene)

        // Add collision detection to the scene
        if let entity = scene.children.first as? Entity & HasCollision {
            // Generate collision shape
            entity.generateCollisionShapes(recursive: true)

            // Create an anchor and add the entity as a child of the anchor
            let anchor = AnchorEntity(world: [0, 0, 0])
            anchor.addChild(entity)

            // Add the anchor to the scene
            arView.scene.addAnchor(anchor)

            // Install rotation, scaling, and movement gestures for the entity
            arView.installGestures([.rotation, .scale, .translation], 
                                    for: entity)
        }
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

1 Answers1

0

RealityKit's gestures mustn't be applied to the anchor, they must be applied to the model. So, all you need to do is to find a model in the scene's hierarchy. Pay attention that the Reality Composer scene already has an anchor, so you don't need a second one.

Try this code with Reality Composer's regular Box scene:

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    typealias GstReady = Entity & HasCollision
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let scn = try! Experience.loadBox()
        arView.scene.anchors.append(scn)
        
        print(scn)
        
        if let entity = scn.findEntity(named: "simpBld_root") as? GstReady {
            entity.generateCollisionShapes(recursive: false)
            arView.installGestures([.all], for: entity)
        }
    }
}

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220