Camera
Extends:
Manages viewing and projection transforms for its Scene.
- One Camera per Scene
- Scene is located at Viewer#scene and Camera is located at Scene#camera
- Controls viewing and projection transforms
- Has methods to pan, zoom and orbit (or first-person rotation)
- Dynamically configurable World-space axis
- Has Perspective, Ortho and Frustum and CustomProjection, which you can dynamically switch it between
- Switchable gimbal lock
- Can be "flown" to look at targets using a CameraFlightAnimation
- Can be animated along a path using a CameraPathAnimation
Getting the Camera
There is exactly one Camera per Scene:
import {Viewer} from "xeokit-sdk.es.js";
var camera = viewer.scene.camera;
Setting the Camera Position
Get and set the Camera's absolute position via Camera#eye, Camera#look and Camera#up:
camera.eye = [-10,0,0];
camera.look = [-10,0,0];
camera.up = [0,1,0];
Camera View and Projection Matrices
The Camera's view matrix transforms coordinates from World-space to View-space.
Getting the view matrix:
var viewMatrix = camera.viewMatrix;
var viewNormalMatrix = camera.normalMatrix;
The Camera's view normal matrix transforms normal vectors from World-space to View-space.
Getting the view normal matrix:
var viewNormalMatrix = camera.normalMatrix;
The Camera fires a "viewMatrix"
event whenever the Camera#viewMatrix and Camera#viewNormalMatrix updates.
Listen for view matrix updates:
camera.on("viewMatrix", function(matrix) { ... });
Rotating the Camera
Orbiting the Camera#look position:
camera.orbitYaw(20.0);
camera.orbitPitch(10.0);
First-person rotation, rotates Camera#look and Camera#up about Camera#eye:
camera.yaw(5.0);
camera.pitch(-10.0);
Panning the Camera
Panning along the Camera's local axis (ie. left/right, up/down, forward/backward):
camera.pan([-20, 0, 10]);
Zooming the Camera
Zoom to vary distance between Camera#eye and Camera#look:
camera.zoom(-5); // Move five units closer
Get the current distance between Camera#eye and Camera#look:
var distance = camera.eyeLookDist;
Projection
The Camera has a Component to manage each projection type, which are: Perspective, Ortho and Frustum and CustomProjection.
You can configure those components at any time, regardless of which is currently active:
The Camera has a Perspective to manage perspective
// Set some properties on Perspective
camera.perspective.near = 0.4;
camera.perspective.fov = 45;
// Set some properties on Ortho
camera.ortho.near = 0.8;
camera.ortho.far = 1000;
// Set some properties on Frustum
camera.frustum.left = -1.0;
camera.frustum.right = 1.0;
camera.frustum.far = 1000.0;
// Set the matrix property on CustomProjection
camera.customProjection.matrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
// Switch between the projection types
camera.projection = "perspective"; // Switch to perspective
camera.projection = "frustum"; // Switch to frustum
camera.projection = "ortho"; // Switch to ortho
camera.projection = "customProjection"; // Switch to custom
Camera provides the projection matrix for the currently active projection in Camera#projMatrix.
Get the projection matrix:
var projMatrix = camera.projMatrix;
Listen for projection matrix updates:
camera.on("projMatrix", function(matrix) { ... });
Configuring World up direction
We can dynamically configure the directions of the World-space coordinate system.
Setting the +Y axis as World "up", +X as right and -Z as forwards (convention in some modeling software):
camera.worldAxis = [
1, 0, 0, // Right
0, 1, 0, // Up
0, 0,-1 // Forward
];
Setting the +Z axis as World "up", +X as right and -Y as "up" (convention in most CAD and BIM viewers):
camera.worldAxis = [
1, 0, 0, // Right
0, 0, 1, // Up
0,-1, 0 // Forward
];
The Camera has read-only convenience properties that provide each axis individually:
var worldRight = camera.worldRight;
var worldForward = camera.worldForward;
var worldUp = camera.worldUp;
Gimbal locking
By default, the Camera locks yaw rotation to pivot about the World-space "up" axis. We can dynamically lock and unlock that at any time:
camera.gimbalLock = false; // Yaw rotation now happens about Camera's local Y-axis
camera.gimbalLock = true; // Yaw rotation now happens about World's "up" axis
Member Summary
Public Members | ||
public set |
constrainPitch(value: Boolean) Sets whether to prevent camera from being pitched upside down. |
|
public get |
Gets the Camera's custom projection. This is used while Camera#projection equals "customProjection". |
|
public set |
deviceMatrix(matrix: Number[]) Sets an optional matrix to premultiply into Camera#matrix matrix. |
|
public get |
deviceMatrix: Number[]: * Gets an optional matrix to premultiply into Camera#matrix matrix. |
|
public set |
Sets the position of the Camera's eye. Default value is |
|
public get |
Gets the position of the Camera's eye. Default vale is |
|
public get |
eyeLookDist: Number: * Gets whether to prevent camera from being pitched upside down. |
|
public get |
Gets the Camera's frustum projection. The Camera uses this while Camera#projection equals |
|
public set |
Sets whether to lock yaw rotation to pivot about the World-space "up" axis. |
|
public get |
gimbalLock: Boolean: * Gets whether to lock yaw rotation to pivot about the World-space "up" axis. |
|
public get |
inverseViewMatrix: Number[]: * Gets the inverse of the Camera's viewing transform matrix. This has the same value as Camera#normalMatrix. |
|
public set |
Sets the position of this Camera's point-of-interest. Default value is |
|
public get |
Gets the position of this Camera's point-of-interest. Default value is |
|
public get |
Gets the Camera's viewing transformation matrix. Fires a Camera#matrix:event event on change. |
|
public get |
normalMatrix: Number[]: * The Camera's viewing normal transformation matrix. Fires a Camera#matrix:event event on change. |
|
public get |
Gets the Camera's orthographic projection. The Camera uses this while Camera#projection equals |
|
public get |
Gets the Camera's perspective projection. The Camera uses this while Camera#projection equals |
|
public get |
projMatrix: Number[]: * Gets the Camera's projection transformation projMatrix. Fires a Camera#projMatrix:event event on change. |
|
public get |
project: Perspective | Ortho | Frustum | CustomProjection: * Gets the currently active projection for this Camera. |
|
public set |
projection(value: String) Sets the active projection type. |
|
public get |
projection: String: * Gets the active projection type. |
|
public set |
Sets the direction of this Camera's Camera#up vector. |
|
public get |
Gets the direction of this Camera's Camera#up vector. |
|
public get |
viewMatrix: Number[]: * Gets the Camera's viewing transformation matrix. Fires a Camera#matrix:event event on change. |
|
public get |
viewNormalMatrix: Number[]: * The Camera's viewing normal transformation matrix. Fires a Camera#matrix:event event on change. |
|
public set |
Sets the up, right and forward axis of the World coordinate system. |
|
public get |
Gets the up, right and forward axis of the World coordinate system. |
|
public get |
worldForward: Number[]: * Gets the direction of World-space "forwards". |
|
public get |
worldRight: Number[]: * Gets the direction of World-space "right". |
|
public get |
Gets the direction of World-space "up". |
|
public get |
Gets if the World-space X-axis is "up". |
|
public get |
Gets if the World-space Y-axis is "up". |
|
public get |
Gets if the World-space Z-axis is "up". |
Method Summary
Public Methods | ||
public |
destroy() Destroys this Camera. |
|
public |
orbitPitch(angleInc: Number) Rotates Camera#eye about Camera#look around the right axis (orthogonal to Camera#up and "look"). |
|
public |
Rotates Camera#eye about Camera#look, around the Camera#up vector |
|
public |
pan(pan: *) Pans the Camera along its local X, Y and Z axis. |
|
public |
Rotates Camera#look about Camera#eye, around the right axis (orthogonal to Camera#up and "look"). |
|
public |
projectWorldPos(worldPos: [number, number, number]): [number, number] Get the 2D canvas position of a 3D world position. |
|
public |
Rotates Camera#look about Camera#eye, around the Camera#up vector. |
|
public |
Increments/decrements the Camera's zoom factor, which is the distance between Camera#eye and Camera#look. |
Inherited Summary
From class Component | ||
public get |
The Component that owns the lifecycle of this Component, if any. |
|
public |
True as soon as this Component has been destroyed |
|
public |
ID of this Component, unique within the Scene. |
|
public |
meta: * Arbitrary, user-defined metadata on this component. |
|
public |
The parent Scene that contains this Component. |
|
public |
The viewer that contains this Scene. |
|
public |
clear() Destroys all Components that are owned by this. |
|
public |
destroy() Destroys this component. |
|
public |
Logs an error for this component to the JavaScript console. |
|
public |
Fires an event on this component. |
|
public |
Returns true if there are any subscribers to the given event on this component. |
|
public |
Tests if this component is of the given type, or is a subclass of the given type. |
|
public |
Logs a console debugging message for this component. |
|
public |
Cancels an event subscription that was previously made with Component#on or Component#once. |
|
public |
Subscribes to an event on this component. |
|
public |
Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is subIdd. |
|
public |
scheduleTask(task: *) Schedule a task to perform on the next browser interval |
|
public |
Logs a warning for this component to the JavaScript console. |
Public Members
public set constrainPitch(value: Boolean) source
Sets whether to prevent camera from being pitched upside down.
The camera is upside down when the angle between Camera#up and Camera#worldUp is less than one degree.
Fires a Camera#constrainPitch:event event on change.
Default value is false
.
public get customProjection: CustomProjection: * source
Gets the Camera's custom projection.
This is used while Camera#projection equals "customProjection".
public set deviceMatrix(matrix: Number[]) source
Sets an optional matrix to premultiply into Camera#matrix matrix.
This is intended to be used for stereo rendering with WebVR etc.
public get deviceMatrix: Number[]: * source
Gets an optional matrix to premultiply into Camera#matrix matrix.
public set eye: Number[] source
Sets the position of the Camera's eye.
Default value is [0,0,10]
.
Emit:
* |
"eye" event on change, with the value of this property. |
public get eyeLookDist: Number: * source
Gets whether to prevent camera from being pitched upside down.
The camera is upside down when the angle between Camera#up and Camera#worldUp is less than one degree.
Default value is false
.
public get frustum: Frustum: * source
Gets the Camera's frustum projection.
The Camera uses this while Camera#projection equals frustum
.
public set gimbalLock source
Sets whether to lock yaw rotation to pivot about the World-space "up" axis.
Fires a Camera#gimbalLock:event event on change.
public get gimbalLock: Boolean: * source
Gets whether to lock yaw rotation to pivot about the World-space "up" axis.
public get inverseViewMatrix: Number[]: * source
Gets the inverse of the Camera's viewing transform matrix.
This has the same value as Camera#normalMatrix.
public set look(look: Number[]) source
Sets the position of this Camera's point-of-interest.
Default value is [0,0,0]
.
Emit:
* |
"look" event on change, with the value of this property. |
public get look: Number[]: * source
Gets the position of this Camera's point-of-interest.
Default value is [0,0,0]
.
public get matrix: Number[]: * source
Gets the Camera's viewing transformation matrix.
Fires a Camera#matrix:event event on change.
public get normalMatrix: Number[]: * source
The Camera's viewing normal transformation matrix.
Fires a Camera#matrix:event event on change.
public get ortho: Ortho: * source
Gets the Camera's orthographic projection.
The Camera uses this while Camera#projection equals ortho
.
public get perspective: Perspective: * source
Gets the Camera's perspective projection.
The Camera uses this while Camera#projection equals perspective
.
public get projMatrix: Number[]: * source
Gets the Camera's projection transformation projMatrix.
Fires a Camera#projMatrix:event event on change.
public get project: Perspective | Ortho | Frustum | CustomProjection: * source
Gets the currently active projection for this Camera.
The currently active project is selected with Camera#projection.
public set projection(value: String) source
Sets the active projection type.
Accepted values are "perspective"
, "ortho"
, "frustum"
and "customProjection"
.
Default value is "perspective"
.
public get projection: String: * source
Gets the active projection type.
Possible values are "perspective"
, "ortho"
, "frustum"
and "customProjection"
.
Default value is "perspective"
.
public set up(up: Number[]) source
Sets the direction of this Camera's Camera#up vector.
Emit:
* |
"up" event on change, with the value of this property. |
public get viewMatrix: Number[]: * source
Gets the Camera's viewing transformation matrix.
Fires a Camera#matrix:event event on change.
public get viewNormalMatrix: Number[]: * source
The Camera's viewing normal transformation matrix.
Fires a Camera#matrix:event event on change.
public set worldAxis(axis: Number[]) source
Sets the up, right and forward axis of the World coordinate system.
Has format: [rightX, rightY, rightZ, upX, upY, upZ, forwardX, forwardY, forwardZ]
Default axis is [1, 0, 0, 0, 1, 0, 0, 0, 1]
public get worldAxis: Number[]: * source
Gets the up, right and forward axis of the World coordinate system.
Has format: [rightX, rightY, rightZ, upX, upY, upZ, forwardX, forwardY, forwardZ]
Default axis is [1, 0, 0, 0, 1, 0, 0, 0, 1]
public get worldForward: Number[]: * source
Gets the direction of World-space "forwards".
This is set by Camera#worldAxis.
Default value is [0,0,1]
.
public get worldRight: Number[]: * source
Gets the direction of World-space "right".
This is set by Camera#worldAxis.
Default value is [1,0,0]
.
Public Methods
public orbitPitch(angleInc: Number) source
Rotates Camera#eye about Camera#look around the right axis (orthogonal to Camera#up and "look").
Params:
Name | Type | Attribute | Description |
angleInc | Number | Angle of rotation in degrees |
public orbitYaw(angleInc: Number) source
Rotates Camera#eye about Camera#look, around the Camera#up vector
Params:
Name | Type | Attribute | Description |
angleInc | Number | Angle of rotation in degrees |
public pan(pan: *) source
Pans the Camera along its local X, Y and Z axis.
Params:
Name | Type | Attribute | Description |
pan | * | The pan vector |
public pitch(angleInc: Number) source
Rotates Camera#look about Camera#eye, around the right axis (orthogonal to Camera#up and "look").
Params:
Name | Type | Attribute | Description |
angleInc | Number | Angle of rotation in degrees |
public projectWorldPos(worldPos: [number, number, number]): [number, number] source
Get the 2D canvas position of a 3D world position.
Params:
Name | Type | Attribute | Description |
worldPos | [number, number, number] |
Return:
[number, number] | the canvas position |
public yaw(angleInc: Number) source
Rotates Camera#look about Camera#eye, around the Camera#up vector.
Params:
Name | Type | Attribute | Description |
angleInc | Number | Angle of rotation in degrees |
public zoom(delta: Number) source
Increments/decrements the Camera's zoom factor, which is the distance between Camera#eye and Camera#look.
Params:
Name | Type | Attribute | Description |
delta | Number | Zoom factor increment. |