Camera
A Camera defines viewing and projection transforms for its Scene.
Overview
- One Camera per Scene
- Controls viewing and projection transforms
- Has methods to pan, zoom and orbit (or first-person rotation)
- Dynamically configurable World-space "up" direction
- Switchable between perspective, frustum and orthographic projections
- Switchable gimbal lock
- Can be "flown" to look at targets using a CameraFlightAnimation
- Can be animated along a path using a CameraPathAnimation
- Can follow a target using a CameraFollowAnimation
Examples
- Perspective projection
- Orthographic projection
- Frustum projection
- Camera with world Z-axis as "up"
- Camera with world Y-axis as "up"
- Automatically following a Mesh with a Camera
- Animating a Camera along a path
- Architectural fly-through
Usage
- Getting the Camera
- Moving around
- Projection
- Configuring World up direction
- Gimbal locking
- Stereo rendering
Getting the Camera
There is exactly one Camera per Scene:
var camera = myScene.camera;
Moving around
Get and set the Camera's absolute position at any time via its eye, look and up properties:
camera.eye = [-10,0,0];
camera.look = [-10,0,0];
camera.up = [0,1,0];
Get the view matrix:
var viewMatrix = camera.viewMatrix;
var viewNormalMatrix = camera.normalMatrix;
Listen for view matrix updates:
camera.on("matrix", function(matrix) { ... });
Orbiting the look position:
camera.orbitYaw(20.0);
camera.orbitPitch(10.0);
First-person rotation, rotates look and up about eye:
camera.yaw(5.0);
camera.pitch(-10.0);
Panning along the Camera's local axis (ie. left/right, up/down, forward/backward):
camera.pan([-20, 0, 10]);
Zoom to vary distance between eye and look:
camera.zoom(-5); // Move five units closer
Get the current distance between eye and look:
var distance = camera.eyeLookDist;
Projection
For each projection type, the Camera has a Component to manage that projection's configuration. You can hot-switch the Camera between those projection types, while updating the properties of each projection component at any time.
camera.perspective.near = 0.4;
camera.perspective.fov = 45;
//...
camera.ortho.near = 0.8;
camera.ortho.far = 1000;
//...
camera.frustum.left = -1.0;
camera.frustum.right = 1.0;
camera.frustum.far = 1000.0;
//...
camera.customProjection.matrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
camera.projection = "perspective"; // Switch to perspective
camera.projection = "frustum"; // Switch to frustum
camera.projection = "ortho"; // Switch to ortho
camera.projection = "customProjection"; // Switch to custom
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 direction that we consider to be "up" in the World-space coordinate system.
Set the +Y axis as World "up" (convention in some modeling software):
camera.worldAxis = [
1, 0, 0, // Right
0, 1, 0, // Up
0, 0,-1 // Forward
];
Set the +Z axis as World "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
See: https://en.wikipedia.org/wiki/Gimbal_lock
Stereo rendering
TODO: Describe stereo techniques and the deviceMatrix property
Constructor
Camera
-
[owner]
-
[cfg]
Parameters:
-
[owner]
Component optionalOwner component. When destroyed, the owner will destroy this component as well. Creates this component within the default Scene when omitted.
-
[cfg]
optionalConfigs
-
[id]
String optional -
[meta]
String:Object optionalOptional map of user-defined metadata to attach to this Camera.
-
Index
Methods
Properties
Methods
create
-
[cfg]
Convenience method for creating a Component within this Component's Scene.
The method is given a component configuration, like so:
var material = myComponent.create({
type: "xeogl.PhongMaterial",
diffuse: [1,0,0],
specular: [1,1,0]
}, "myMaterial");
Parameters:
-
[cfg]
optionalConfiguration for the component instance.
Returns:
destroy
()
Destroys this component.
Fires a destroyed event on this Component.
Automatically disassociates this component from other components, causing them to fall back on any defaults that this component overrode on them.
TODO: describe effect with respect to #create
error
-
message
Logs an error for this component to the JavaScript console.
The console message will have this format: [ERROR] [<component type> =<component id>: <message>
Also fires the message as an error event on the parent Scene.
Parameters:
-
message
StringThe message to log
fire
-
event
-
value
-
[forget=false]
Fires an event on this component.
Notifies existing subscribers to the event, optionally retains the event to give to any subsequent notifications on the event as they are made.
Parameters:
-
event
StringThe event type name
-
value
ObjectThe event parameters
-
[forget=false]
Boolean optionalWhen true, does not retain for subsequent subscribers
hasSubs
-
event
Returns true if there are any subscribers to the given event on this component.
Parameters:
-
event
StringThe event
Returns:
True if there are any subscribers to the given event on this component.
isType
-
type
Tests if this component is of the given type, or is a subclass of the given type.
The type may be given as either a string or a component constructor.
This method works by walking up the inheritance type chain, which this component provides in property Component/superTypes:property, returning true as soon as one of the type strings in the chain matches the given type, of false if none match.
Examples:
var myRotate = new xeogl.Rotate({ ... });
myRotate.isType(xeogl.Component); // Returns true for all xeogl components
myRotate.isType("xeogl.Component"); // Returns true for all xeogl components
myRotate.isType(xeogl.Rotate); // Returns true
myRotate.isType(xeogl.Transform); // Returns true
myRotate.isType("xeogl.Transform"); // Returns true
myRotate.isType(xeogl.Mesh); // Returns false, because xeogl.Rotate does not (even indirectly) extend xeogl.Mesh
Parameters:
-
type
String | FunctionComponent type to compare with, eg "xeogl.PhongMaterial", or a xeogl component constructor.
Returns:
True if this component is of given type or is subclass of the given type.
log
-
message
Logs a console debugging message for this component.
The console message will have this format: [LOG] [<component type> <component id>: <message>
Parameters:
-
message
StringThe message to log
off
-
subId
Cancels an event subscription that was previously made with Component#on() or Component#once().
Parameters:
-
subId
StringPublication subId
on
-
event
-
callback
-
[scope=this]
Subscribes to an event on this component.
The callback is be called with this component as scope.
Parameters:
-
event
StringThe event
-
callback
FunctionCalled fired on the event
-
[scope=this]
Object optionalScope for the callback
Returns:
Handle to the subscription, which may be used to unsubscribe with {@link #off}.
once
-
event
-
callback
-
[scope=this]
Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is subIdd.
This is equivalent to calling Component#on(), and then calling Component#off() inside the callback function.
Parameters:
-
event
StringData event to listen to
-
callback
Function(data)Called when fresh data is available at the event
-
[scope=this]
Object optionalScope for the callback
orbitPitch
-
angle
Parameters:
-
angle
NumberAngle of rotation in degrees
orbitYaw
-
angle
Parameters:
-
angle
NumberAngle of rotation in degrees
pan
-
pan
Pans the camera along the camera's local X, Y and Z axis.
Parameters:
-
pan
ObjectThe pan vector
pitch
-
angle
Parameters:
-
angle
NumberAngle of rotation in degrees
warn
-
message
Logs a warning for this component to the JavaScript console.
The console message will have this format: [WARN] [<component type> =<component id>: <message>
Parameters:
-
message
StringThe message to log
yaw
-
angle
Parameters:
-
angle
NumberAngle of rotation in degrees
Properties
constrainPitch
Boolean
Whether to prevent camera from being pitched upside down.
The camera is upside down when the angle between up and worldUp is less than one degree.
Fires a constrainPitch event on change.
Default: false
customProjection
CustomProjection
final
A custom projection transform, given as a 4x4 matrix.
This is used while projection equals "customProjection".
destroyed
Boolean
True as soon as this Component has been destroyed
deviceMatrix
Float32Array
Sets an optional matrix to premultiply into this Camera's matrix matrix.
This is intended to be used for stereo rendering with WebVR etc.
eyeLookDist
Number
final
Distance from "look" to "eye".
frustum
Frustum
final
The frustum projection transform for this Camera.
This is used while projection equals "frustum".
gimbalLock
Boolean
Whether to lock yaw rotation to pivot about the World-space "up" axis.
Fires a gimbalLock event on change.
Default: true
look
Float32Array
Position of this Camera's point-of-interest.
Fires a look event on change.
Default: [0,0,0]
matrix
Float32Array
deprecated
final
The Camera's viewing transformation matrix.
Fires a Camera/matrix:event event on change.
model
Model
final
The Model which contains this Component, if any.
Will be null if this Component is not in a Model.
normalMatrix
Float32Array
deprecated
final
The Camera's viewing normal transformation matrix.
Fires a Camera/matrix:event event on change.
ortho
Ortho
final
The orthographic projection transform for this Camera.
This is used while projection equals "ortho".
perspective
Perspective
final
The perspective projection transform for this Camera.
This is used while projection equals "perspective".
project
Transform
final
The active projection transform for this Camera.
projection
String
The active projection type.
Accepted values are "perspective", "ortho", "frustum" and "customProjection".
Default: "perspective"
projMatrix
Float32Array
final
Camera's projection transformation projMatrix.
Fires a Camera/projMatrix:event event on change.
type
String
final
JavaScript class name for this Component.
For example: "xeogl.AmbientLight", "xeogl.MetallicMaterial" etc.
viewMatrix
Float32Array
final
The Camera's viewing transformation matrix.
Fires a Camera/matrix:event event on change.
viewNormalMatrix
Float32Array
final
The Camera's viewing normal transformation matrix.
Fires a Camera/matrix:event event on change.
worldAxis
Float32Array
Indicates the up, right and forward axis of the World coordinate system.
Has format: [rightX, rightY, rightZ, upX, upY, upZ, forwardX, forwardY, forwardZ]
Default: [1, 0, 0, 0, 1, 0, 0, 0, 1]
worldForward
Float32Array
final
Direction of World-space "forwards".
Default: [0,0,-1]
worldRight
Float32Array
final
Direction of World-space "right".
Default: [1,0,0]
worldUp
Float32Array
final
Direction of World-space "up".
Default: [0,1,0]
Events
constrainPitch
Fired whenever this Camera's constrainPitch property changes.
Event Payload:
-
value
ObjectThe property's new value
destroyed
Fired when this Component is destroyed.
deviceMatrix
Fired whenever this CustomProjection's matrix property changes.
Event Payload:
-
value
ObjectThe property's new value
eye
Fired whenever this Camera's eye property changes.
Event Payload:
-
value
ObjectThe property's new value
gimbalLock
Fired whenever this Camera's gimbalLock property changes.
Event Payload:
-
value
ObjectThe property's new value
look
Fired whenever this Camera's look property changes.
Event Payload:
-
value
ObjectThe property's new value