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.
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
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
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
}
}
}