-
Notifications
You must be signed in to change notification settings - Fork 36
Gestures
Add new gestures to your RealityKit entities without worrying about adding your own gesture recognisers.
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. HasTurnTouch adds a way to rotate something in any axis you like with just one finger.
The entity with the touch needs to use the HasTurnTouch protocol, then simply set the turnAxis to be the angle to rotate around and set the collision component (used for starting the initial collision).
For this gesture you need to have also run RealityUI.enableGestures(.longTouch, on: arView)
at least once on your ARView.
Example Entity class:
class KeyModel: Entity, HasTurnTouch, HasModel {
init(with model: ModelComponent) {
self.turnAxis = [0, 0, -1]
self.collision = CollisionComponent(shapes: [.generateBox(size: [1, 1, 0.2])])
self.model = model
}
}
Adding the class that uses HasTurnTouch:
// IMPORTANT: Required to enable RealityUI long touch gestures
// Only needs to be called once per ARView.
RealityUI.enableGestures(.longTouch, on: arView)
let testAnchor = AnchorEntity(world: [0, 0, -1])
testAnchor.addChild(KeyModel(with: keyModel))
arView.scene.addAnchor(testAnchor)
Let RealityUI easily take care of the raycasting to find where any taps on your ARView end up in your RealityKit scenes with HasClick.
For this gesture you need to have also run RealityUI.enableGestures(.tap, on: arView)
at least once on your ARView.
/// 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)