Gestures & Input
OCCTSwiftViewport ships with a platform-neutral input architecture built around three layers:
GestureConfiguration: declares what each gesture or modifier+drag means (orbit, pan, zoom, select, …)ViewportInputEvent: the portable event type that carries deltas and modifier state, decoupled from AppKit/UIKitViewportController.dispatch(_:): the single interpretation entry point that applies the configuration and drives the camera
Native platform gesture recognisers (iOS drag/pinch/rotate; macOS mouse/scroll) are already wired in by MetalViewportView. You only need this page if you want to change the mapping, intercept events for inspection, or feed synthetic input (tests, XR, Catalyst, scripting).
Gesture Presets
GestureConfiguration is a Sendable value type. Assign a preset to ViewportConfiguration.gestureConfiguration before (or after) creating the controller.
.default: Shapr3D style (the out-of-the-box preset)
| Input | Action |
|---|---|
| Single-finger drag / unmodified mouse drag | Orbit |
| Two-finger drag | Pan |
| Pinch / scroll wheel | Zoom |
| Shift+drag (macOS) | Pan |
| Option+drag (macOS) | Zoom |
| Command+drag (macOS) | Select |
| Double-tap / double-click | Nothing (opt in; see Resetting the view) |
.blender: Blender style
Unmodified mouse drag selects; orbit is behind Option, pan behind Shift, zoom behind Command.
| Input | Action |
|---|---|
| Unmodified mouse drag | Select |
| Shift+drag | Pan |
| Option+drag | Orbit |
| Command+drag | Zoom |
Touch gestures (iOS/visionOS) are unchanged from .default.
.fusion360: Fusion 360 style
| Input | Action |
|---|---|
| Unmodified mouse drag | Select |
| Shift+drag | Orbit |
| Option+drag | Pan |
| Command+drag | Zoom |
.visionOS: spatial (indirect pinch) input
Identical iOS touch mapping with dampingFactor raised to 0.15 so inertia settles more predictably with indirect input. Tune further on real hardware.
Applying a Preset
import OCCTSwiftViewport
// 1. Build a ViewportConfiguration with the desired gesture preset.
var config = ViewportConfiguration()
config.gestureConfiguration = .blender
// 2. Pass it to the controller.
let controller = ViewportController(configuration: config)
ViewportController.configuration is a let, so the mapping is fixed once the controller exists. To offer a different preset, create a new controller with the new configuration.
Fine-Tuning Sensitivity and Orbit Direction
All sensitivity values are properties on GestureConfiguration. Start from a preset and mutate:
var gestures = GestureConfiguration.default
gestures.orbitSensitivity = 0.008 // radians per point (default 0.005)
gestures.panSensitivity = 1 // pan speed × the finger's; 1 = 1:1 (default 1)
gestures.zoomSensitivity = 1.2 // pinch / drag-zoom exponent (default 1.0)
gestures.scrollZoomSensitivity = 0.4 // scroll-wheel zoom (default 0.25)
gestures.enableInertia = true // momentum after release (default true)
gestures.dampingFactor = 0.1 // 0 = frictionless, 1 = instant stop (default 0.1)
gestures.momentumScale = 0.06 // momentum as a fraction of release speed (default 0.06)
gestures.dragRotation = .orbitCamera // move the camera, not the model (default .rotateObject)
gestures.invertOrbitHorizontal = false // flip one axis on top of dragRotation (default false)
gestures.invertOrbitVertical = false // (default false)
// Every gesture can be remapped; `.none` disables it.
gestures.twoFingerDrag = .orbit // two fingers orbit instead of pan
gestures.scrollWheel = .none // ignore the scroll wheel
gestures.doubleTap = .focusOnPoint // re-centre on the tapped surface (default .none)
gestures.doubleClick = .focusOnPoint
var config = ViewportConfiguration()
config.gestureConfiguration = gestures
let controller = ViewportController(configuration: config)
To change sensitivity on a live controller, set the matching property on controller.cameraController (for example orbitSensitivity). The action mappings are read from the configuration and cannot change after init.
Orbit direction
By default a drag turns the model as if the finger were holding it (dragRotation = .rotateObject): drag right and the model’s near side moves right, drag up and it moves up. This applies to the iOS one-finger drag, the macOS mouse drag, the navigation cube and the release momentum, in every RotationStyle. Every preset uses it, matching Blender’s and Fusion 360’s orbit. Set .orbitCamera to move the camera around the pivot instead, which turns the model against the drag on both axes. invertOrbitHorizontal / invertOrbitVertical flip one axis on top of either.
Earlier releases mixed the two: a horizontal drag orbited the camera while a vertical drag turned the model. If your app set invertOrbitHorizontal = true to work around that, remove it. See Drag direction.
In .arcball the convention also sets which way a drag outside the sphere rolls the view, and in .firstPerson, which turns the camera in place, .rotateObject drags the scene with the finger while .orbitCamera is mouse-look (drag right, look right). See Rotation styles.
Momentum
After a fast release the view coasts. momentumScale sets how much of the release speed it coasts with: 1 carries on at the speed the finger was moving, and the default 0.06 gives the short, gentle coast the viewport has always had. dampingFactor sets how quickly it slows. A new drag stops the coast.
gestures.momentumScale = 0.3 // a longer, livelier flick
Custom input sources and the arcball
If you drive the viewport with dispatch(_:) from your own gestures, tell it where each drag starts so the arcball can roll:
controller.setDragOrigin(offsetFromCenter: SIMD2(Float(start.x - size.width / 2),
Float(start.y - size.height / 2)))
controller.dispatch(.dragChanged(delta: delta, modifiers: []))
Without it a drag is taken to start at the centre of the view, where the arcball only tumbles. A two-finger drag bound to .orbit takes its start, the centroid of the touches, from setTwoFingerDragOrigin(offsetFromCenter:) in the same way.
If your view is not a MetalViewportView, also give the controller its size, so pan stays 1:1 with the finger:
controller.updateViewportSize(size) // sets lastAspectRatio and the camera's viewportHeight
Pan speed
Pan is 1:1 by default: the point under the finger stays under it (in perspective, the point at the pivot’s depth; in orthographic, any point). panSensitivity multiplies that, so 2 pans twice as far as the finger moves. Before issue #200 it was world units per point scaled by the camera distance, default 0.005, which panned about five times as far as the finger; a configuration that still sets 0.005 now pans 200 times slower than 1:1, so change it to 1.
Resetting the view
No gesture resets the camera unless you opt in. Pick the trigger that suits your app’s UX (a key combination, a double-click with optional modifiers, or a double-tap) and list it in resetViewTriggers:
var gestures = GestureConfiguration.default
gestures.resetViewTriggers = [
.keyCombination(key: "h", modifiers: [.command, .shift]), // ⇧⌘H (macOS)
.doubleClick(modifiers: [.option]), // ⌥ double-click (macOS)
.doubleTap, // iOS / visionOS
]
var config = ViewportConfiguration()
config.gestureConfiguration = gestures
let controller = ViewportController(configuration: config)
The reset goes to ViewportConfiguration.initialCameraState. The action is the public controller.reset(animated:), so you can also bind it to a menu command:
Button("Reset View") { controller.reset() }
.keyboardShortcut("h", modifiers: [.command, .shift])
None of the presets ships a reset binding. Blender’s Home key and Fusion 360’s Home view frame or restore a saved view rather than returning to a fixed start pose, so no binding is a faithful match.
GestureAction Values
GestureAction is a CaseIterable String-backed enum. The full set:
| Case | Effect |
|---|---|
.orbit | Rotate camera around pivot |
.pan | Translate camera parallel to view plane |
.zoom | Dolly in/out |
.select | Initiates a pick (observational; picking runs on its own renderer path) |
.focusOnPoint | Recentres the pivot under the cursor |
.resetView | Resets to initialCameraState (prefer resetViewTriggers) |
.none | No-op |
ViewportModifierKeys
ViewportModifierKeys is a platform-neutral OptionSet used to encode which keyboard modifiers are active. The four flags:
ViewportModifierKeys.shift // rawValue 1
ViewportModifierKeys.control // rawValue 2
ViewportModifierKeys.option // rawValue 4
ViewportModifierKeys.command // rawValue 8
Bridge from platform types at the point where you translate native input:
// macOS: inside an NSResponder / NSEvent handler
let modifiers = ViewportModifierKeys(event.modifierFlags) // NSEvent.ModifierFlags
// iOS / Catalyst: from a UIKeyModifierFlags (e.g. a hardware-keyboard gesture)
let modifiers = ViewportModifierKeys(command.modifierFlags) // UIKeyModifierFlags
Resolve a drag action against the active modifiers:
let action = controller.configuration.gestureConfiguration.dragAction(for: modifiers)
// Priority: command → shift → option → unmodified
The Portable Event Model: ViewportInputEvent
ViewportInputEvent is a Sendable, Equatable enum. Every native gesture recogniser converts to one of these before calling dispatch(_:). The full case list:
| Case | Payload | Typical source |
|---|---|---|
.dragChanged(delta:modifiers:) | point delta + modifiers | Mouse drag / single-finger |
.dragEnded(velocity:modifiers:) | release velocity + modifiers | Drag release (inertia) |
.twoFingerPanChanged(translation:) | incremental translation | Two-finger drag (twoFingerDrag) |
.twoFingerPanEnded(velocity:) | release velocity | Two-finger release |
.pinchChanged(scale:) | incremental scale ratio (1.0 = no change) | Pinch gesture |
.pinchAtChanged(scale:centerNDC:aspectRatio:) | scale + pinch centre in NDC | Pinch-to-zoom at cursor |
.pinchEnded | (none) | Pinch release |
.rotateChanged(radians:) | incremental angle | Two-finger rotation |
.rotateEnded | (none) | Rotation release |
.scroll(delta:cursorNDC:aspectRatio:) | scroll amount + cursor NDC | Scroll wheel |
.tap(ndc:count:) | NDC position + tap count | Tap / click |
NDC coordinates are in the range −1…+1 on each axis. A double-tap (count >= 2) does nothing by default. It resets the view if resetViewTriggers lists a matching .doubleTap / .doubleClick(modifiers:), and otherwise runs doubleTap (doubleClick on macOS), for example .focusOnPoint to move the pivot onto the surface under the tap. Single taps are observational via dispatch (picking runs on a separate renderer path).
ViewportController.dispatch(_:)
dispatch(_:) is the single entry point for all input. The built-in gesture recognisers call it automatically. You call it directly for synthetic or external input:
// Synthesise an orbit drag (e.g. from a game controller thumbstick)
let delta = SIMD2<Float>(thumbstick.x * 3.0, thumbstick.y * 3.0)
controller.dispatch(.dragChanged(delta: delta, modifiers: []))
controller.dispatch(.dragEnded(velocity: .zero, modifiers: []))
// Synthesise a scroll-wheel zoom toward view centre (NDC 0,0)
controller.dispatch(.scroll(delta: -0.5, cursorNDC: .zero, aspectRatio: 1.0))
// Synthesise a pinch zoom toward a specific screen point
let pinchCentre = SIMD2<Float>(0.3, -0.1) // NDC
controller.dispatch(.pinchAtChanged(scale: 1.05, centerNDC: pinchCentre, aspectRatio: aspectRatio))
controller.dispatch(.pinchEnded)
The dispatch implementation resolves the GestureAction (using GestureConfiguration.dragAction(for:) on macOS, singleFingerDrag on iOS), applies sign conventions, and calls the underlying camera methods. All interpretation lives in dispatch; the platform translation layer only produces deltas.
Observing the Event Stream: onInputEvent
ViewportController.onInputEvent is an optional ((ViewportInputEvent) -> Void) callback fired on every event before interpretation. It does not affect camera behaviour; use it for debugging, HUD inspectors, or logging:
controller.onInputEvent = { event in
switch event {
case let .dragChanged(delta, modifiers):
let action = controller.configuration.gestureConfiguration.dragAction(for: modifiers)
print("drag \(delta) → \(action)")
case let .scroll(delta, cursor, _):
print("scroll \(delta) at NDC \(cursor)")
default:
break
}
}
Remove the observer by setting it to nil:
controller.onInputEvent = nil
Building a Custom Mapping
For a mapping not covered by the presets, construct GestureConfiguration directly. Example, Maya-style (Alt+LMB orbit, Alt+MMB pan, Alt+RMB zoom; bare click selects):
var maya = GestureConfiguration(
mouseDrag: .select,
shiftDrag: .pan,
optionDrag: .orbit, // Alt+drag → orbit
commandDrag: .none
)
// Alt is the `option` modifier; there is no middle-mouse button on a trackpad,
// so put pan behind Shift as a practical fallback.
maya.orbitSensitivity = 0.006
maya.enableInertia = false // Maya-style: no momentum
controller.configuration.gestureConfiguration = maya
Feeding a Non-Apple Input Source
For visionOS immersive space, external game-controller APIs, or test harnesses that have no gesture recognisers at all, translate your native input into ViewportInputEvent and call dispatch(_:) directly. No AppKit or UIKit types are required:
// Example: map a visionOS SpatialEventGesture update to a drag event
func handleSpatialUpdate(translation: SIMD3<Float>) {
let delta = SIMD2<Float>(translation.x, translation.y)
controller.dispatch(.dragChanged(delta: delta * 50, modifiers: []))
}
func handleSpatialEnd(velocity: SIMD3<Float>) {
let v = SIMD2<Float>(velocity.x, velocity.y)
controller.dispatch(.dragEnded(velocity: v, modifiers: []))
}
Because ViewportInputEvent is Sendable and Equatable, events can be constructed, stored, replayed, or diffed in tests without any UI framework.