You can make 3D models and RealityViews respond to user interaction using Gestures.
To begin, add the CollisionComponent to your entity. This component "gives an entity the ability to collide with other entities".
model.components.set(CollisionComponent(
shapes: [.generateBox(width: 0.2, height: 1, depth: 0.2)],
mode: .colliding, // colliding, default, trigger
filter: .default
))
The CollisionComponent can have one of the following shapes:
- Sphere (.generateSphere)
- Capsule (.generateCapsule)
- Box (.generateBox)
- Convex Shape (.generateConvex)
Next, add a gesture to the RealityView. The following gestures are available:
- TapGesture: Gesture that recognizes one or more taps
- SpatialTapGesture: A gesture that recognizes one or more taps and reports their location.
- LongPressGesture: A gesture that succeeds when the user performs a long press.
- SpatialEventGesture
- DragGesture: A dragging motion that invokes an action as the drag-event sequence changes.
- MagnifyGesture: A gesture that recognizes a magnification motion and tracks the amount of magnification.
- RotateGesture: A gesture that recognizes a rotation motion and tracks the angle of the rotation.
- RotateGesture3D: A gesture that recognizes 3D rotation motion and tracks the angle and axis of the rotation.
- SequenceGesture: A gesture that’s a sequence of two gestures.
RealityView{ content in
let model = ModelEntity(
mesh: .generateCylinder(height: 1, radius: 0.1),
materials: [
SimpleMaterial(color: .red, isMetallic: true),
])
model.components.set(InputTargetComponent())
model.components.set(CollisionComponent(
shapes: [.generateBox(width: 0.2, height: 1, depth: 0.2)],
mode: .colliding,
filter: .default
))
content.add(model)
} update: { content in
if let model = content.entities.first {
model.transform.scale = scale ? [1.2, 1.2, 1.2]: [ 1.0, 1.0, 1.0]
}
}
.gesture( LongPressGesture().targetedToAnyEntity().onEnded { _ in
scale.toggle()
})