How-to guide·3D viewerJavaScript

Navigation modes

Start the default, walk, game or drone mode, define a custom mode with its own key bindings, and know which forwardMode values the viewer actually implements.

A navigation mode decides what mouse drags, the keyboard and pointer lock do with the camera. This guide switches modes and defines a custom one.

Before you start#

You have a working viewer (Embed the viewer). The names below are the values of NavigationMode in the type definitions; the actions and bindings are listed on Default key bindings.

The four presets#

Mode Pointer lock Forward moves Left and right keys Notes
default no level with the ground strafe Orbit, pan, zoom and section on the mouse, Alt + left drag to look around, WSAD, arrows, Q and Z on the keyboard.
walk no level turn the camera With the walkable-surfaces data the server generates, walk physics adds collision, gravity, stair climbing, jumps and Page Up / Page Down storey jumps (Walk through a building).
game yes level strafe Starting the mode requests pointer lock; mouse movement looks around. A right- or middle-click in the view, or Escape, returns to default.
drone yes along the camera direction strafe Like game, with movement speed ×3.

On touch-capable devices, walk, game and drone show two virtual joysticks at the bottom of the viewer. The left joystick moves forward, backward and sideways; the right joystick looks around. Push farther from the center to move or turn faster, and use both thumbs simultaneously to steer while moving. In drone mode, forward and backward follow the camera direction, so looking up or down lets you climb or descend. The drone joystick moves at 20 times the configured drone speed (×60 game speed with the default settings), with ×0.5 look joystick sensitivity. Releasing a joystick stops its input. Touch game and drone navigation do not request pointer lock. The controls disappear when switching to the default preset.

Steps#

  1. Pick the initial mode in the settings, or switch at runtime:

    JavaScript#
    const viewer = new Viewer3d(objectStateManager, undefined, {navigationMode: "walk"});
    // later
    viewer.startDroneNavigation(true);          // true = request browser fullscreen first
    viewer.startNavigationMode("default");
    console.log(viewer.getNavigationMode());    // "default"

    startDefaultNavigation, startWalkNavigation, startGameNavigation(fullscreen) and startDroneNavigation(fullscreen) are shorthands for startNavigationMode. Every start method returns false when nothing changed. Pointer lock is only requested from a user gesture, so call the game and drone starters from a click handler.

  2. Define a custom mode under navigationModes and name it in navigationMode. A custom mode extends a preset (type is an alias) and overrides what it lists; keyBindings replaces the bindings of the actions it names, and an empty array switches an action off for that mode. Names are matched case-insensitively.

    JavaScript#
    const viewer = new Viewer3d(objectStateManager, undefined, {
    	navigationMode: "inspection",
    	navigationModes: {
    		inspection: {
    			extends: "game",
    			pointerLock: false,
    			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"}],
    				colorObjectTransparent: []   // frees the D key
    			}
    		}
    	}
    });

    A KeyBinding uses KeyboardEvent.code values (KeyW, ArrowUp, PageDown) or a mouse button (0 left, 1 middle, 2 right), plus shiftKey, ctrlKey, altKey and, for movement actions, a speed multiplier. Without a Shift binding of its own, a movement key held with Shift runs at five times its speed.

  3. Change the built-in default the same way, by declaring navigationModes.default. Runtime overrides work too: viewer.startNavigationMode("drone", {pointerLock: false, navigationSpeed: 2}) changes the preset for this session without registering a name.

The mode settings#

Setting Meaning
extends / type The preset to start from; default when absent.
pointerLock Request pointer lock when the mode starts and look around with raw mouse movement. While the pointer is locked, a right- or middle-click (or Escape) returns to default; viewer.usesPointerLock() and viewer.isPointerLocked() report the state.
lookAround Allow drag look-around for a binding mapped to lookAround.
keyboardNavigation Enable keyboard movement; switched on automatically when movement bindings are given.
turnWithKeyboard Left and right yaw the camera instead of strafing (the walk preset).
forwardMode "horizontal" keeps forward and backward level; "camera" follows the view direction.
navigationSpeed Multiplier for free keyboard movement (drone uses 3).
keyBindings Per-action overrides, see step 2.
collision, walkThroughWalls, gravity, characterHeightMm, eyeHeightMm, characterRadiusMm, climbHeightMm Walk physics; only with the walkable-surfaces data (viewer.walkPhysicsAvailable()). Defaults and meaning on how-to/viewer3d/walk-mode#1-start-walking.
Warning

The type definitions allow a third forwardMode, "orbit", but the viewer only implements "horizontal" and "camera": with any other value free keyboard movement is switched off altogether (unless turnWithKeyboard is set). Treat "orbit" as not available.

The right mouse button is not a binding. Set the rightMouseClick(event, qid) callback in the viewer settings to open a context menu (reference/viewer3d#rightmouseclick).

Check#

viewer.getNavigationMode() returns the name you started, and the keys you bound move the camera.