Adding gestures to ALL Model Entities in one Reality Composer scene, not just one?
Asked Answered
T

1

1

So I asked a different question before, and arrived at a partial solution. I was able to add gestures to ONE of the two model entities on one scene, but not the other. Because when you print the scene, the two models have the same root name, but only the first one gets the gestures.

enter image description here

See the two cubes here, I was able to drag the blue cube to the front, but nothing happens when I try to drag the green cube

blue and green cubes

import UIKit
import RealityKit
import SwiftUI

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    //private var myEntity: Entity?
    typealias GstReady = Entity & HasCollision
    //typealias ModelPack = ModelEntity & HasCollision


 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let boxAnchor = try! Experience.loadBox()
        arView.scene.anchors.append(boxAnchor)
        
        let cubesAnchor = try! Experience.loadCubes()
        arView.scene.anchors.append(cubesAnchor)
        
        
        if let entity = cubesAnchor.findEntity(named: "simpBld_root") as? GstReady {
            entity.generateCollisionShapes(recursive: false)
                  arView.installGestures([.all], for: entity)
              }

        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
            //self.arView.scene.anchors.removeAll()
            let coneAnchor = try! Experience.loadCone()
            self.arView.scene.anchors.append(coneAnchor)
            
            print(cubesAnchor)
            
        }
        
        func handleTapOnEntity(_ entity: Entity?){
            guard let entity = entity else { return }
            
            //self.myEntity = entity
            //self.myEntity?.isEnabled = false
            
            
            
        }
        
        
        
        
    }
    
    
    
    
}

See related question here

Theologue answered 30/4, 2023 at 0:13 Comment(1)
Found the partial answer, and now I can move both objects. However, the entire point of this exercise is to find the solution to then apply to a larger set of objects ( 20+ objects) in one scene at the same time. Meaning if I drag from any one of the object that are connected to each other, all the objects move at the same time. I believe the answer, in this case, is to create a parent entity in RealityKit and then make all objects, children of this one larger object. I will try this tomorrow!!!Theologue
T
0

Found an answer, not the simple workaround I was looking for, but a solution nonetheless. I wanted to find a way to add gestures to all the model Entities within a given scene, at once, but as far as I was able to try, it is not possible.

The group solution doesn't exist quite yet.

So I had to ungroup the objects, name each one separately and then declare each one separately.

Loading the scene

let cubesAnchor = try! Experience.loadCubes() arView.scene.anchors.append(cubesAnchor)

Generating collision shapes for the scene

    cubesAnchor.generateCollisionShapes(recursive: true)

Adding Entity & HasCollision to each object in the scene

        let greenCubesGest = cubesAnchor.greenCube as? Entity & HasCollision
        arView.installGestures(for: greenCubesGest!)
        let blueCubesGest = cubesAnchor.blueCube as? Entity & HasCollision
        arView.installGestures(for: blueCubesGest!)

The full code is below.

import UIKit
import RealityKit
import SwiftUI

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!


 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let boxAnchor = try! Experience.loadBox()
        arView.scene.anchors.append(boxAnchor)
        
        let cubesAnchor = try! Experience.loadCubes()
        arView.scene.anchors.append(cubesAnchor)
        cubesAnchor.generateCollisionShapes(recursive: true)
        let greenCubesGest = cubesAnchor.greenCube as? Entity & HasCollision
        arView.installGestures(for: greenCubesGest!)
        let blueCubesGest = cubesAnchor.blueCube as? Entity & HasCollision
        arView.installGestures(for: blueCubesGest!)
        
        /**
         cubesAnchor.generateCollisionShapes(recursive: true)
         let allCubes= cubesAnchor.group as? Entity & HasCollision
         arView.installGestures(for: allCubes!)
         */
        /**
        
       
        if let entity = cubesAnchor.findEntity(named: "simpBld_root") as? GstReady {
            entity.generateCollisionShapes(recursive: false)
                  arView.installGestures([.all], for: entity)
              }

         */
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
            //self.arView.scene.anchors.removeAll()
            let coneAnchor = try! Experience.loadCone()
            self.arView.scene.anchors.append(coneAnchor)
            
            print(cubesAnchor)
            
        }
        
        func handleTapOnEntity(_ entity: Entity?){
            guard let entity = entity else { return }
            
            //self.myEntity = entity
            //self.myEntity?.isEnabled = false
            
            
            
        }
        
        
        
        
    }
    
    
    
    
}
Theologue answered 30/4, 2023 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.