Class GamepadCameraControls

GamepadCameraControls extends the CameraControls class to provide camera control using a gamepad. It listens for gamepad connection and disconnection events and processes gamepad input to control the camera's rotation, movement, zoom, and elevation.

Hierarchy

  • CameraControls
    • GamepadCameraControls

Implements

Methods

Other

Properties

Statics

Methods

cancel: (() => void)

Force cancel user dragging.

lockPointer: (() => void)

Still an experimental feature. This could change at any time.

unlockPointer: (() => void)

Still an experimental feature. This could change at any time.

  • Adds the specified event listener. Applicable event types (which is K) are:

    Event name Timing
    'controlstart' When the user starts to control the camera via mouse / touches. ¹
    'control' When the user controls the camera (dragging).
    'controlend' When the user ends to control the camera. ¹
    'transitionstart' When any kind of transition starts, either user control or using a method with enableTransition = true
    'update' When the camera position is updated.
    'wake' When the camera starts moving.
    'rest' When the camera movement is below .restThreshold ².
    'sleep' When the camera end moving.
    1. mouseButtons.wheel (Mouse wheel control) does not emit 'controlstart' and 'controlend'. mouseButtons.wheel uses scroll-event internally, and scroll-event happens intermittently. That means "start" and "end" cannot be detected.
    2. Due to damping, sleep will usually fire a few seconds after the camera appears to have stopped moving. If you want to do something (e.g. enable UI, perform another transition) at the point when the camera has stopped, you probably want the rest event. This can be fine tuned using the .restThreshold parameter. See the Rest and Sleep Example.

    e.g.

    cameraControl.addEventListener( 'controlstart', myCallbackFunction );
    

    Type Parameters

    • K extends keyof CameraControlsEventMap

    Parameters

    • type: K

      event name

    • listener: ((event: CameraControlsEventMap[K]) => any)

      handler function

        • (event): any
        • Parameters

          • event: CameraControlsEventMap[K]

          Returns any

    Returns void

  • Apply current camera-up direction to the camera. The orbit system will be re-initialized with the current position.

    Returns void

  • Attach all internal event handlers to enable drag control.

    Parameters

    • domElement: HTMLElement

    Returns void

  • Fire an event type.

    Parameters

    • event: DispatcherEvent

      DispatcherEvent

    Returns void

  • Dolly in/out camera position.

    Parameters

    • distance: number

      Distance of dollyIn. Negative number for dollyOut.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately.

    Returns Promise<void>

  • Dolly in, but does not change the distance between the target and the camera, and moves the target position instead. Specify a negative value for dolly out.

    Parameters

    • distance: number

      Distance of dolly.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately.

    Returns Promise<void>

  • Dolly in/out camera position to given distance.

    Parameters

    • distance: number

      Distance of dolly.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately.

    Returns Promise<void>

  • Move up / down.

    Parameters

    • height: number

      Amount to move up / down. Negative value to move down

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Fit the viewport to the box or the bounding box of the object, using the nearest axis. paddings are in unit. set cover: true to fill enter screen. e.g.

    cameraControls.fitToBox( myMesh );
    

    Parameters

    • box3OrObject: Object3D<Object3DEventMap> | Box3

      Axis aligned bounding box to fit the view.

    • enableTransition: boolean

      Whether to move smoothly or immediately.

    • Optionaloptions: Partial<FitToOptions>

      | <object> { cover: boolean, paddingTop: number, paddingLeft: number, paddingBottom: number, paddingRight: number }

    Returns Promise<void[]>

    Transition end promise

  • Fit the viewport to the sphere or the bounding sphere of the object.

    Parameters

    • sphereOrMesh: Object3D<Object3DEventMap> | Sphere
    • enableTransition: boolean

    Returns Promise<void[]>

  • Move forward / backward.

    Parameters

    • distance: number

      Amount to move forward / backward. Negative value to move backward

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Reproduce the control state with JSON. enableTransition is where anim or not in a boolean.

    Parameters

    • json: string
    • OptionalenableTransition: boolean

    Returns void

  • Calculate the distance to fit the box.

    Parameters

    • width: number

      box width

    • height: number

      box height

    • depth: number

      box depth

    • Optionalcover: boolean

    Returns number

    distance

  • Calculate the distance to fit the sphere.

    Parameters

    • radius: number

      sphere radius

    Returns number

    distance

  • Returns the focal offset, which is how much the camera appears to be translated in screen parallel coordinates.

    Parameters

    • out: Vector3

      The receiving Vector3 instance to copy the result

    • OptionalreceiveEndValue: boolean

      Whether receive the transition end coords or current. default is true

    Returns Vector3

  • Returns the camera position.

    Parameters

    • out: Vector3

      The receiving Vector3 instance to copy the result

    • OptionalreceiveEndValue: boolean

      Whether receive the transition end coords or current. default is true

    Returns Vector3

  • Returns the spherical coordinates of the orbit.

    Parameters

    • out: Spherical

      The receiving Spherical instance to copy the result

    • OptionalreceiveEndValue: boolean

      Whether receive the transition end coords or current. default is true

    Returns Spherical

  • Returns the orbit center position, where the camera looking at.

    Parameters

    • out: Vector3

      The receiving Vector3 instance to copy the result

    • OptionalreceiveEndValue: boolean

      Whether receive the transition end coords or current. default is true

    Returns Vector3

  • Presence of the specified event listener.

    Parameters

    • type: string

      event name

    • listener: Listener

      handler function

    Returns boolean

  • Similar to setLookAt, but it interpolates between two states.

    Parameters

    • positionAX: number
    • positionAY: number
    • positionAZ: number
    • targetAX: number
    • targetAY: number
    • targetAZ: number
    • positionBX: number
    • positionBY: number
    • positionBZ: number
    • targetBX: number
    • targetBY: number
    • targetBZ: number
    • t: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

  • Look in the given point direction.

    Parameters

    • x: number

      point x.

    • y: number

      point y.

    • z: number

      point z.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately.

    Returns Promise<void>

    Transition end promise

  • Move target position to given point.

    Parameters

    • x: number

      x coord to move center position

    • y: number

      y coord to move center position

    • z: number

      z coord to move center position

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Normalize camera azimuth angle rotation between 0 and 360 degrees.

    Returns void

  • Parameters

    • x: number
    • y: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

    pan() has been renamed to truck()

  • Removes all event listeners

    Parameters

    • Optionaltype: string

      event name

    Returns void

  • Removes the specified event listener e.g.

    cameraControl.addEventListener( 'controlstart', myCallbackFunction );
    

    Type Parameters

    • K extends keyof CameraControlsEventMap

    Parameters

    • type: K

      event name

    • listener: ((event: CameraControlsEventMap[K]) => any)

      handler function

        • (event): any
        • Parameters

          • event: CameraControlsEventMap[K]

          Returns any

    Returns void

  • Reset all rotation and position to defaults.

    Parameters

    • OptionalenableTransition: boolean

    Returns Promise<void[]>

  • Rotate azimuthal angle(horizontal) and polar angle(vertical). Every value is added to the current value.

    Parameters

    • azimuthAngle: number

      Azimuth rotate angle. In radian.

    • polarAngle: number

      Polar rotate angle. In radian.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Rotate azimuthal angle(horizontal) to the given angle and keep the same polar angle(vertical) target.

    e.g.

    cameraControls.rotateAzimuthTo( 30 * THREE.MathUtils.DEG2RAD, true );
    

    Parameters

    • azimuthAngle: number

      Azimuth rotate angle. In radian.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Rotate polar angle(vertical) to the given angle and keep the same azimuthal angle(horizontal) target.

    e.g.

    cameraControls.rotatePolarTo( 30 * THREE.MathUtils.DEG2RAD, true );
    

    Parameters

    • polarAngle: number

      Polar rotate angle. In radian.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Rotate azimuthal angle(horizontal) and polar angle(vertical) to the given angle. Camera view will rotate over the orbit pivot absolutely:

    azimuthAngle


    \
    90º -----+----- -90º
    \
    180º
    direction angle
    front
    left 90º (Math.PI / 2)
    right -90º (- Math.PI / 2)
    back 180º (Math.PI)

    polarAngle

        180º
    |
    90º
    |
    direction angle
    top/sky 180º (Math.PI)
    horizontal from view 90º (Math.PI / 2)
    bottom/floor

    Parameters

    • azimuthAngle: number

      Azimuth rotate angle to. In radian.

    • polarAngle: number

      Polar rotate angle to. In radian.

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Set current camera position as the default position.

    Returns void

  • Set the boundary box that encloses the target of the camera. box3 is in THREE.Box3

    Parameters

    • Optionalbox3: Box3

    Returns void

  • Set focal offset using the screen parallel coordinates. z doesn't affect in Orthographic as with Dolly.

    Parameters

    • x: number
    • y: number
    • z: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

  • Look at the target from the position.

    Parameters

    • positionX: number
    • positionY: number
    • positionZ: number
    • targetX: number
    • targetY: number
    • targetZ: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

  • Set orbit point without moving the camera. SHOULD NOT RUN DURING ANIMATIONS. setOrbitPoint() will immediately fix the positions.

    Parameters

    • targetX: number
    • targetY: number
    • targetZ: number

    Returns void

  • Set angle and distance by given position. An alias of setLookAt(), without target change. Thus keep gazing at the current target

    Parameters

    • positionX: number
    • positionY: number
    • positionZ: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

  • Set the target position where gaze at. An alias of setLookAt(), without position change. Thus keep the same position.

    Parameters

    • targetX: number
    • targetY: number
    • targetZ: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

  • Set (or unset) the current viewport. Set this when you want to use renderer viewport and .dollyToCursor feature at the same time.

    Parameters

    • viewportOrX: null | number | Vector4
    • y: number
    • width: number
    • height: number

    Returns void

  • Move sideways

    Parameters

    • distance: number

      Amount to move sideways. Negative value to move left

    • enableTransition: boolean = false

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Get all state in JSON string

    Returns string

  • Truck and pedestal camera using current azimuthal angle

    Parameters

    • x: number

      Horizontal translate amount

    • y: number

      Vertical translate amount

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Update camera position and directions. This should be called in your tick loop every time, and returns true if re-rendering is needed.

    Parameters

    • delta: number

    Returns boolean

    updated

  • Sync camera-up direction. When camera-up vector is changed, .updateCameraUp() must be called.

    Returns void

  • Zoom in/out camera. The value is added to camera zoom. Limits set with .minZoom and .maxZoom

    Parameters

    • zoomStep: number

      zoom scale

    • OptionalenableTransition: boolean

      Whether to move smoothly or immediately

    Returns Promise<void>

  • Zoom in/out camera to given scale. The value overwrites camera zoom. Limits set with .minZoom and .maxZoom

    Parameters

    • zoom: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

Other

_activePointers: PointerInput[]
_boundary: Box3
_boundaryEnclosesCamera: boolean
_camera: PerspectiveCamera | OrthographicCamera
_cameraUp0: Vector3
_changedDolly: number
_changedZoom: number
_dollyControlCoord: Vector2
_dollyInternal: ((delta: number, x: number, y: number) => void)
_domElement?: HTMLElement
_dragNeedsUpdate: boolean
_elementRect: DOMRect
_enabled: boolean
_focalOffset: Vector3
_focalOffset0: Vector3
_focalOffsetEnd: Vector3
_focalOffsetVelocity: Vector3
_hasRested: boolean
_interactiveArea: DOMRect
_isDragging: boolean
_isUserControllingDolly: boolean
_isUserControllingOffset: boolean
_isUserControllingRotate: boolean
_isUserControllingTruck: boolean
_isUserControllingZoom: boolean
_lastDistance: number
_lastDollyDirection: DOLLY_DIRECTION
_lastZoom: number
_lockedPointer: null | PointerInput
_nearPlaneCorners: [Vector3, Vector3, Vector3, Vector3]
_needsUpdate: boolean
_phiVelocity: Ref
_position0: Vector3
_radiusVelocity: Ref
_rotateInternal: ((deltaX: number, deltaY: number) => void)
_spherical: Spherical
_sphericalEnd: Spherical
_state: number
_target: Vector3
_target0: Vector3
_targetEnd: Vector3
_targetVelocity: Vector3
_thetaVelocity: Ref
_truckInternal: ((deltaX: number, deltaY: number, dragToOffset: boolean) => void)
_updatedLastTime: boolean
_viewport: null | Vector4
_yAxisUpSpace: Quaternion
_yAxisUpSpaceInverse: Quaternion
_zoom: number
_zoom0: number
_zoomEnd: number
_zoomInternal: ((delta: number, x: number, y: number) => void)
_zoomVelocity: Ref
active: boolean
azimuthAngle: number
boundaryEnclosesCamera: boolean
camera: PerspectiveCamera | OrthographicCamera
currentAction: number
dampingFactor: number
distance: number
draggingDampingFactor: number
enabled: boolean
events: {
    a: EventDispatcher;
    b: EventDispatcher;
    back: EventDispatcher;
    down: EventDispatcher;
    left: EventDispatcher;
    leftBumper: EventDispatcher;
    leftStick: EventDispatcher;
    leftTrigger: EventDispatcher;
    right: EventDispatcher;
    rightBumper: EventDispatcher;
    rightStick: EventDispatcher;
    rightTrigger: EventDispatcher;
    start: EventDispatcher;
    up: EventDispatcher;
    x: EventDispatcher;
    y: EventDispatcher;
} = Events

The events associated with the gamepad controls.

interactiveArea: DOMRect | {
    height: number;
    width: number;
    x: number;
    y: number;
}
polarAngle: number

The parameters for the gamepad controls.

ACTION: Readonly<{
    DOLLY: 8;
    NONE: 0;
    OFFSET: 4;
    ROTATE: 1;
    TOUCH_DOLLY: 256;
    TOUCH_DOLLY_OFFSET: 2048;
    TOUCH_DOLLY_ROTATE: 4096;
    TOUCH_DOLLY_TRUCK: 1024;
    TOUCH_OFFSET: 128;
    TOUCH_ROTATE: 32;
    TOUCH_TRUCK: 64;
    TOUCH_ZOOM: 512;
    TOUCH_ZOOM_OFFSET: 16384;
    TOUCH_ZOOM_ROTATE: 32768;
    TOUCH_ZOOM_TRUCK: 8192;
    TRUCK: 2;
    ZOOM: 16;
}>
  • Parameters

    • _domElement: HTMLElement

    Returns void

  • Returns number

  • Parameters

    • resolveImmediately: boolean

    Returns Promise<void>

  • Parameters

    • pointer: PointerInput

    Returns void

  • Parameters

    • distance: number
    • OptionalenableTransition: boolean

    Returns Promise<void>

  • Parameters

    • position: Vector3
    • offset: Vector3
    • friction: number

    Returns Vector3

  • Parameters

    • pointerId: number

    Returns undefined | PointerInput

  • Parameters

    • mouseButton: MOUSE_BUTTON

    Returns undefined | PointerInput

  • Parameters

    • out: Vector3

    Returns Vector3

  • Get its client rect and package into given DOMRect .

    Parameters

    • target: DOMRect

    Returns undefined | DOMRect

  • Parameters

    • out: Vector3

    Returns Vector3

  • Returns void

  • Returns void

  • Detach all internal event handlers to disable drag control.

    Returns void

  • Retrieves a list of connected gamepads.

    Returns readonly Gamepad[]

    An array of connected gamepads.

  • stop all transitions.

    Returns void

  • Parameters

    • object3d: Object3D<Object3DEventMap>
    • Optionalout: Sphere

    Returns Sphere

Properties

azimuthRotateSpeed: number

Speed of azimuth (horizontal) rotation.

boundaryFriction: number

Friction ratio of the boundary.

colliderMeshes: Object3D<Object3DEventMap>[]

An array of Meshes to collide with camera. Be aware colliderMeshes may decrease performance. The collision test uses 4 raycasters from the camera since the near plane has 4 corners.

dollyDragInverted: boolean

true to invert direction when dollying or zooming via drag

dollySpeed: number

Speed of mouse-wheel dollying.

dollyToCursor: boolean

true to enable Dolly-in to the mouse cursor coords.

draggingSmoothTime: number

the smoothTime while dragging

dragToOffset: boolean
infinityDolly: boolean

true to enable Infinity Dolly for wheel and pinch. Use this with minDistance and maxDistance If the Dolly distance is less (or over) than the minDistance (or maxDistance), infinityDolly will keep the distance and pushes the target position instead.

maxAzimuthAngle: number

Maximum horizontal angle in radians. The angle has to be greater than .minAzimuthAngle. The default value is Infinity.

e.g.

cameraControls.maxAzimuthAngle = Infinity;
maxDistance: number

Maximum distance for dolly. The value must be higher than minDistance. Default is Infinity. PerspectiveCamera only.

maxPolarAngle: number

Maximum vertical angle in radians. The angle has to be between .maxPolarAngle and Math.PI inclusive. The default value is Math.PI.

e.g.

cameraControls.maxPolarAngle = Math.PI;
maxSpeed: number

Max transition speed in unit-per-seconds

maxZoom: number

Maximum camera zoom.

minAzimuthAngle: number

Minimum horizontal angle in radians. The angle has to be less than .maxAzimuthAngle. The default value is - Infinity.

e.g.

cameraControls.minAzimuthAngle = - Infinity;
minDistance: number

Minimum distance for dolly. The value must be higher than 0. Default is Number.EPSILON. PerspectiveCamera only.

minPolarAngle: number

Minimum vertical angle in radians. The angle has to be between 0 and .maxPolarAngle inclusive. The default value is 0.

e.g.

cameraControls.maxPolarAngle = 0;
minZoom: number

Minimum camera zoom.

mouseButtons: MouseButtons

User's mouse input config.

button to assign behavior
mouseButtons.left CameraControls.ACTION.ROTATE* | CameraControls.ACTION.TRUCK | CameraControls.ACTION.OFFSET | CameraControls.ACTION.DOLLY | CameraControls.ACTION.ZOOM | CameraControls.ACTION.NONE
mouseButtons.right CameraControls.ACTION.ROTATE | CameraControls.ACTION.TRUCK* | CameraControls.ACTION.OFFSET | CameraControls.ACTION.DOLLY | CameraControls.ACTION.ZOOM | CameraControls.ACTION.NONE
mouseButtons.wheel ¹ CameraControls.ACTION.ROTATE | CameraControls.ACTION.TRUCK | CameraControls.ACTION.OFFSET | CameraControls.ACTION.DOLLY | CameraControls.ACTION.ZOOM | CameraControls.ACTION.NONE
mouseButtons.middle ² CameraControls.ACTION.ROTATE | CameraControls.ACTION.TRUCK | CameraControls.ACTION.OFFSET | CameraControls.ACTION.DOLLY* | CameraControls.ACTION.ZOOM | CameraControls.ACTION.NONE
  1. Mouse wheel event for scroll "up/down" on mac "up/down/left/right"
  2. Mouse click on wheel event "button"
  • * is the default.
  • The default of mouseButtons.wheel is:
    • DOLLY for Perspective camera.
    • ZOOM for Orthographic camera, and can't set DOLLY.
polarRotateSpeed: number

Speed of polar (vertical) rotation.

restThreshold: number

Controls how soon the rest event fires as the camera slows.

smoothTime: number

Approximate time in seconds to reach the target. A smaller value will reach the target faster.

touches: Touches

User's touch input config.

fingers to assign behavior
touches.one CameraControls.ACTION.TOUCH_ROTATE* | CameraControls.ACTION.TOUCH_TRUCK | CameraControls.ACTION.TOUCH_OFFSET | CameraControls.ACTION.DOLLY
touches.two ACTION.TOUCH_DOLLY_TRUCK | ACTION.TOUCH_DOLLY_OFFSET | ACTION.TOUCH_DOLLY_ROTATE | ACTION.TOUCH_ZOOM_TRUCK | ACTION.TOUCH_ZOOM_OFFSET | ACTION.TOUCH_ZOOM_ROTATE | ACTION.TOUCH_DOLLY | ACTION.TOUCH_ZOOM | CameraControls.ACTION.TOUCH_ROTATE | CameraControls.ACTION.TOUCH_TRUCK | CameraControls.ACTION.TOUCH_OFFSET | CameraControls.ACTION.NONE
touches.three ACTION.TOUCH_DOLLY_TRUCK | ACTION.TOUCH_DOLLY_OFFSET | ACTION.TOUCH_DOLLY_ROTATE | ACTION.TOUCH_ZOOM_TRUCK | ACTION.TOUCH_ZOOM_OFFSET | ACTION.TOUCH_ZOOM_ROTATE | CameraControls.ACTION.TOUCH_ROTATE | CameraControls.ACTION.TOUCH_TRUCK | CameraControls.ACTION.TOUCH_OFFSET | CameraControls.ACTION.NONE
  • * is the default.
  • The default of touches.two and touches.three is:
    • TOUCH_DOLLY_TRUCK for Perspective camera.
    • TOUCH_ZOOM_TRUCK for Orthographic camera, and can't set TOUCH_DOLLY_TRUCK and TOUCH_DOLLY.
truckSpeed: number

Speed of drag for truck and pedestal.

verticalDragToForward: boolean

The same as .screenSpacePanning in three.js's OrbitControls.

Statics

  • Injects THREE as the dependency. You can then proceed to use CameraControls.

    e.g

    CameraControls.install( { THREE: THREE } );
    

    Note: If you do not wish to use enter three.js to reduce file size(tree-shaking for example), make a subset to install.

    import {
    Vector2,
    Vector3,
    Vector4,
    Quaternion,
    Matrix4,
    Spherical,
    Box3,
    Sphere,
    Raycaster,
    MathUtils,
    } from 'three';

    const subsetOfTHREE = {
    Vector2 : Vector2,
    Vector3 : Vector3,
    Vector4 : Vector4,
    Quaternion: Quaternion,
    Matrix4 : Matrix4,
    Spherical : Spherical,
    Box3 : Box3,
    Sphere : Sphere,
    Raycaster : Raycaster,
    };

    CameraControls.install( { THREE: subsetOfTHREE } );

    Parameters

    • libs: {
          THREE: THREESubset;
      }
      • THREE: THREESubset

    Returns void