<aside> 💡

This is a new feature currently in beta in 2.0.0-b435

</aside>

Viewer3d supports several camera navigation modes. A mode controls how mouse drags, pointer lock and keyboard movement are interpreted by the viewer.

Set the initial mode when creating the viewer with navigationMode. The value can be one of the built-in presets or the name of a custom mode declared in navigationModes. After the viewer has been created, switch modes with the helper methods startDefaultNavigation, startWalkNavigation, startGameNavigation, startDroneNavigation, or with startNavigationMode(mode, settings, fullscreen) for either a built-in or custom mode.

Built-in presets

Preset Description
default Standard model navigation. Mouse bindings provide orbit, pan, zoom, section-plane dragging and Alt + left-button look-around. Keyboard navigation bindings are available for moving the camera.
walk Walk-style navigation. Forward and backward movement stays horizontal, vertical movement uses the world up axis, and left/right keyboard bindings turn the camera instead of strafing. Alt + left-button look-around is available.
game First-person game-style navigation. Starting the mode requests pointer lock; mouse movement controls look-around, and WASD/arrow-style bindings move horizontally.
drone Free-flight navigation. Like game, it uses pointer lock and mouse look-around, but forward/backward movement follows the camera direction instead of staying horizontal. It also uses a higher default navigation speed.

startGameNavigation and startDroneNavigation accept a fullscreen argument. When it is true, Viewer3d requests browser fullscreen before starting the mode.

Custom modes

Custom modes are configured through navigationModes in the Viewer3d settings. Each custom mode can extend one of the presets with extends or type, then override behavior such as pointer lock, keyboard navigation, movement direction, speed and key bindings. Viewer3d shortcut bindings are configured on navigation modes only; the old top-level keyBindings and disableDefaultKeyBindings settings are no longer available.

The most common settings are:

Setting Description
extends / type Built-in preset to use as the base: default, walk, game or drone. Defaults to default.
pointerLock Request pointer lock and use raw mouse movement for look-around when the mode starts.
lookAround Enable look-around for bindings mapped to the lookAround action.
keyboardNavigation Enable keyboard movement actions. This is also enabled automatically when movement bindings are supplied.
turnWithKeyboard Make left/right navigation bindings yaw the camera instead of strafing. This is used by walk.
forwardMode horizontal keeps forward/backward movement level; camera moves along the camera direction.
navigationSpeed Mode-level speed multiplier for free keyboard navigation.
keyBindings Mode-specific bindings for all Viewer3d shortcut actions. This includes camera actions such as orbit, pan, zoom, section, lookAround and movement actions, plus viewer actions such as hideObject, colorObjectOpaque, colorObjectTransparent, autoRotate, focus, vertexSnappingOff and vertexSnappingOn. Set an action to an empty array to disable its default bindings for that mode.

Key bindings use DOM KeyboardEvent.code values such as KeyW, ArrowUp or PageDown. Mouse bindings use DOM button numbers: 0 for the main/left button, 1 for the middle button and 2 for the secondary/right button. A binding can also include shiftKey, ctrlKey, altKey and an optional speed multiplier for navigation movement.

Context-menu behavior is not a key binding. If an application needs to handle right-click/context-menu selection, configure the top-level rightMouseClick callback in the Viewer3d settings.

{
  "navigationMode": "inspection",
  "navigationModes": {
    "inspection": {
      "extends": "game",
      "pointerLock": false,
      "forwardMode": "horizontal",
      "navigationSpeed": 1.5,
      "keyBindings": {
        "lookAround": [
          { "button": 0, "altKey": true }
        ],
        "navigationForward": [
          { "keyCode": "KeyW" },
          { "keyCode": "ArrowUp" },
          { "keyCode": "KeyW", "shiftKey": true, "speed": 5 }
        ],
        "navigationBackward": [
          { "keyCode": "KeyS" },
          { "keyCode": "ArrowDown" }
        ],
        "navigationLeft": [
          { "keyCode": "KeyA" }
        ],
        "navigationRight": [
          { "keyCode": "KeyD" }
        ],
        "navigationUp": [
          { "keyCode": "PageUp" }
        ],
        "navigationDown": [
          { "keyCode": "PageDown" }
        ]
      }
    }
  }
}

To customize the built-in default mode for the initial viewer, override navigationModes.default:

const viewer3d = new Viewer3d(objectStateManager, undefined, {
  navigationModes: {
    default: {
      keyBindings: {
        orbit: [{ button: 1 }],
        pan: [{ button: 0 }],
        zoom: [{ button: 2 }],
        autoRotate: [],
        hideObject: []
      }
    }
  },
  rightMouseClick: (event, qid) => {
    // Open an application context menu.
  }
});

Runtime overrides can also be supplied when starting a mode. This is useful when a caller wants to temporarily change a preset without registering a named custom mode:

viewer3d.startNavigationMode("drone", {
  pointerLock: false,
  navigationSpeed: 2,
  keyBindings: {
    navigationForward: [{ keyCode: "KeyW" }],
    navigationBackward: [{ keyCode: "KeyS" }],
    navigationUp: [{ keyCode: "KeyE" }],
    navigationDown: [{ keyCode: "KeyQ" }]
  }
});

Use getNavigationMode() to read the active mode name. Custom mode names are matched case-insensitively.