Skip to content

Gestures

Max Cobb edited this page Dec 21, 2020 · 5 revisions

Turn

RealityKit offers a 2 finger rotate gesture, but it can be a little awkward to use two fingers on a phone while looking at something in AR. HasPivotPoint adds a way to rotate something in any axis you like with just one finger.

Tap

Let RealityUI easily take care of the raycasting to find taps in your RealityKit scenes with HasClick.

/// Example class that uses the HasClick protocol
class ClickyEntity: Entity, HasClick, HasModel {
  // Required property from HasClick
  var tapAction: ((HasClick, SIMD3<Float>?) -> Void)?

  init(model: ModelComponent, tapAction: ((HasClick, SIMD3<Float>?) -> Void)) {
    self.tapAction = tapAction
    super.init()
    self.model = model
    self.generateCollisionShapes(recursive: false)
  }

  required convenience init() {
     self.init()
  }
}

Adding to your RealityKit scene:

// IMPORTANT: Required to enable RealityUI tap gesture
// Only needs to be called once per ARView.
RealityUI.enableGestures(.tap, on: arView)

let testAnchor = AnchorEntity(world: [0, 0, -1])

let clickySphere = ClickyEntity(
  model: ModelComponent(mesh: .generateBox(size: 0.2), materials: [SimpleMaterial(color: .red, isMetallic: false)])
) { (clickedObj, atPosition) in
    // In this example we're just assigning the colour of the clickable
    // entity model to a green SimpleMaterial.
    (clickedObj as? HasModel)?.model?.materials = [
        SimpleMaterial(color: .green, isMetallic: false)
    ]
}

testAnchor.addChild(clickySphere)
arView.scene.addAnchor(testAnchor)
Clone this wiki locally