API Docs for: 0.7.0

Camera

Extends Component
Module: camera
Parent Module: xeogl

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

Usage

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.matrix;
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.projection = "perspective"; // Switch to perspective
camera.projection = "frustum"; // Switch to frustum
camera.projection = "ortho"; // Switch to ortho

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

Constructor

Camera

(
  • [scene]
  • [cfg]
)

Parameters:

  • [scene] Scene optional

    Parent Scene, creates this Camera within the default Scene when omitted.

  • [cfg] optional

    Configs

    • [id] String optional

      Optional ID, unique among all components in the parent Scene, generated automatically when omitted. You only need to supply an ID if you need to be able to find the Camera by ID within its parent Scene later.

    • [meta] String:Object optional

      Optional map of user-defined metadata to attach to this Camera.

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] optional

    Configuration 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 String

    The 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 String

    The event type name

  • value Object

    The event parameters

  • [forget=false] Boolean optional

    When true, does not retain for subsequent subscribers

hasSubs

(
  • event
)
Boolean

Returns true if there are any subscribers to the given event on this component.

Parameters:

  • event String

    The event

Returns:

Boolean:

True if there are any subscribers to the given event on this component.

isType

(
  • type
)
Boolean

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 superTypes, 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.Entity); // Returns false, because xeogl.Rotate does not (even indirectly) extend xeogl.Entity

Parameters:

  • type String | Function

    Component type to compare with, eg "xeogl.PhongMaterial", or a xeogl component constructor.

Returns:

Boolean:

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>

Also fires the message as a log event on the parent Scene.

Parameters:

  • message String

    The message to log

off

(
  • handle
)

Cancels an event subscription that was previously made with on or once.

Parameters:

  • handle String

    Publication handle

on

(
  • event
  • callback
  • [scope=this]
)
String

Subscribes to an event on this component.

The callback is be called with this component as scope.

Parameters:

  • event String

    The event

  • callback Function

    Called fired on the event

  • [scope=this] Object optional

    Scope for the callback

Returns:

String:

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 handled.

This is equivalent to calling on, and then calling off inside the callback function.

Parameters:

  • event String

    Data event to listen to

  • callback Function(data)

    Called when fresh data is available at the event

  • [scope=this] Object optional

    Scope for the callback

orbitPitch

(
  • angle
)

Rotates eye about look around the right axis (orthogonal to up and "look").

Parameters:

  • angle Number

    Angle of rotation in degrees

orbitYaw

(
  • angle
)

Rotates eye about look, around the up vector

Parameters:

  • angle Number

    Angle of rotation in degrees

pan

(
  • pan
)

Pans the camera along the camera's local X, Y and Z axis.

Parameters:

  • pan Object

    The pan vector

pitch

(
  • angle
)

Rotates look about eye, around the right axis (orthogonal to up and "look").

Parameters:

  • angle Number

    Angle 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>

Also fires the message as a warn event on the parent Scene.

Parameters:

  • message String

    The message to log

yaw

(
  • angle
)

Rotates look about eye, around the up vector.

Parameters:

  • angle Number

    Angle of rotation in degrees

zoom

(
  • delta
)

Increments/decrements zoom factor, ie. distance between eye and look.

Parameters:

  • delta Object

Properties

destroyed

Boolean

True as soon as this Component has been destroyed

eye

Float32Array

Position of this Camera's eye.

Fires an eye event on change.

Default: [0,0,10]

eyeLookDist

Number

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

id

String final

Unique ID for this Component within its parent Scene.

isDefault

Boolean

Indicates whether this is one of the Scene's built-in Components.

look

Float32Array

Position of this Camera's point-of-interest.

Fires a look event on change.

Default: [0,0,0]

matrix

Float32Array

The Camera's viewing transformation matrix.

Fires a Camera/matrix:event event on change.

metadata

Object

Arbitrary, user-defined metadata on this component.

normalMatrix

Float32Array

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" and "frustum".

Default: "perspective"

projMatrix

Float32Array

Camera's projection transformation projMatrix.

Fires a Camera/projMatrix:event event on change.

scene

Scene final

The parent Scene that contains this Component.

superTypes

Array of String final

An array of strings that indicates the chain of super-types within this component's inheritance hierarchy.

For example, if this component is a Rotate, which extends Transform, which in turn extends Component, then this property will have the value:

["xeogl.Component", "xeogl.Transform"]
                    

Note that the chain is ordered downwards in the hierarchy, ie. from super-class down towards sub-class.

type

String final

JavaScript class name for this Component.

For example: "xeogl.AmbientLight", "xeogl.ColorTarget", "xeogl.Lights" etc.

up

Float32Array

Direction of this Camera's up vector.

Fires an up event on change.

Default: [0,1,0]

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

destroyed

Fired when this Component is destroyed.

eye

Fired whenever this Camera's eye property changes.

Event Payload:

  • value Object

    The property's new value

gimbalLock

Fired whenever this Camera's gimbalLock property changes.

Event Payload:

  • value Object

    The property's new value

look

Fired whenever this Camera's look property changes.

Event Payload:

  • value Object

    The property's new value

up

Fired whenever this Camera's up property changes.

Event Payload:

  • value Object

    The property's new value