Provides functionality for a 3D camera.
This object calculates view, projection and combined transforms for a right-handed coordinate system. It can also calculate frustum planes that can be used for visibility queries.
Required scripts
The Camera and CameraController objects require:
/*{{ javascript("jslib/camera.js") }}*/
Summary
Creates and returns a camera object with default values.
Syntax
var mathDevice = TurbulenzEngine.createMathDevice({});
var camera = Camera.create(mathDevice);
Summary
Sets the camera matrix to look at the specified 3D point in world space from another one.
Syntax
camera.lookAt(target, up, origin);
camera.updateViewMatrix();
camera.updateViewProjectionMatrix();
Summary
Updates the camera viewMatrix. Should be called anytime the camera matrix is updated.
Syntax
camera.matrix = mathDevice.m43FromAxisRotation(up, angle);
camera.updateViewMatrix();
Summary
Updates the camera projectionMatrix. Should be called any time the frustum parameters are changed.
Syntax
var aspectRatio = (deviceWidth / deviceHeight);
if (aspectRatio !== camera.aspectRatio)
{
camera.aspectRatio = aspectRatio;
camera.updateProjectionMatrix();
}
Summary
Updates the camera viewProjectionMatrix. Should be called any time the camera matrix or the frustum parameters are changed.
Syntax
camera.matrix = mathDevice.m43FromAxisRotation(up, angle);
camera.updateViewMatrix();
var aspectRatio = (deviceWidth / deviceHeight);
if (aspectRatio !== camera.aspectRatio)
{
camera.aspectRatio = aspectRatio;
camera.updateProjectionMatrix();
}
camera.updateViewProjectionMatrix();
Summary
Updates the camera frustum planes. Required if the camera frustum planes are used after the camera matrix or the frustum parameters have been changed.
Syntax
camera.updateFrustumPlanes();
var frustumPlanes = camera.frustumPlanes;
var isVisible = mathDevice.isInsidePlanesAABB(extents, frustumPlanes);
Summary
Returns an array with the camera frustum planes. This function is used by updateFrustumPlanes to build the camera frustum planes.
Syntax
camera.matrix = mathDevice.m43FromAxisRotation(up, angle);
camera.updateViewMatrix();
camera.updateViewProjectionMatrix();
var frustum1 = camera.extractFrustumPlanes();
camera.lookAt(target, up, origin);
camera.updateViewMatrix();
camera.updateViewProjectionMatrix();
var frustum2 = camera.extractFrustumPlanes();
Returns an array of 6 Plane objects.
Summary
Returns an array with the 8 points in world units that define the camera frustum. Useful for calculating a bounding volume of the camera frustum.
Syntax
camera.updateViewProjectionMatrix();
var frustumPoints = camera.getFrustumPoints();
var sphere = calculateBoundingSphere(frustumPoints);
Returns a JavaScript array of 8 points. Each point is represented as JavaScript array of 3 components.
Summary
Returns an array with the 4 points in world units of the far plane of the camera frustum. Useful for calculating simple intersections of the camera frustum with other volumes.
Syntax
camera.updateViewProjectionMatrix();
var farPlanePoints = camera.getFrustumFarPoints();
Returns a JavaScript array of 4 points. Each point is represented as JavaScript array of 3 components.
Summary
The version number of the Camera implementation.
Syntax
var cameraVersionNumber = camera.version;
Summary
Shear offsets of the camera frustum. Default to zero.
Syntax
camera.viewOffsetX = 0.1;
Summary
Reciprocals to set the field of view of the camera frustum. Default to 1.0.
Syntax
var horizontalFOV = 120;
var degreesToradians = (Math.PI / 180);
camera.recipViewWindowX = 1.0 / Math.tan((horizontalFOV * 0.5) * degreesToradians);
Summary
Sets the far plane of the camera frustum at infinity when true, it effectively disables clipping by that plane. Defaults to false.
Syntax
camera.infinite = true;
Summary
Sets the camera frustum as an orthographic projection. Defaults to false.
Syntax
camera.parallel = true;
Summary
Sets the aspect ratio of the camera frustum. Defaults to 4.0 / 3.0.
Syntax
camera.aspectRatio = 16.0 / 9.0;
Summary
Sets the distance in world units from the camera position to the near plane of the camera frustum. Setting the near plane to a value lower than 1 can severely degrade the precision of the Z-buffer. Defaults to 1.0.
Syntax
camera.nearPlane = 4.0;
Summary
Sets the distance in world units from the camera position to the far plane of the camera frustum. Defaults to 1000.0.
Syntax
camera.farPlane = 3000.0;
Summary
Sets the Matrix43 object that defines the position and orientation of the camera in world space. Defaults to the identity matrix.
Syntax
camera.matrix = mathDevice.m43FromAxisRotation(up, angle);
Summary
The Matrix43 that defines the transform from world space to the camera space. It is effectively the inverse of the camera matrix. Not recommended to be updated by hand, it is better to call the method updateViewMatrix when the camera matrix is changed.
Syntax
var viewMatrix = camera.viewMatrix;
Summary
The Matrix44 that defines the projection from camera space into screen space. Not recommended to be updated by hand, it is better to call the method updateProjectionMatrix when the frustum parameters are changed.
Syntax
var projectionMatrix = camera.projectionMatrix;
Summary
The Matrix44 that defines the transform from world space to the projected screen space. Not recommended to be updated by hand, it is better to call the method updateViewProjectionMatrix when the camera matrix or the frustum parameters are changed.
Syntax
var viewProjectionMatrix = camera.viewProjectionMatrix;
Summary
The array of planes that defines the camera frustum. Not recommended to be updated by hand, it is better to call the method updateFrustumPlanes when the camera matrix or the frustum parameters are changed.
Syntax
var frustumPlanes = camera.frustumPlanes;
var isVisible = mathDevice.isInsidePlanesAABB(extents, frustumPlanes);
Provides functionality for a fly camera controlled by mouse and keyboard. Click on the engine window to enable input capture.
The arrow keys move the camera forwards, backwards, to the left and to the right. The keys W, S, A and D work as the arrow keys. The keys E and Q work as up and down keys. The key enter switches between fullscreen and windowed mode. The mouse movements rotate the camera and rolling the mouse is a quick forward movement. Press the escape key to release the input devices.
Summary
Creates and returns a camera object with default values.
Syntax
var cameraController = CameraController.create(graphicsDevice, inputDevice, camera);
Summary
Updates the camera according to the input events.
Syntax
inputDevice.update();
cameraController.update();
Summary
The version number of the CameraController implementation.
Syntax
var cameraVersionNumber = cameraController.version;
Summary
The maximum movement speed for each frame. Defaults to 1.0.
Syntax
cameraController.maxSpeed = (deltaTime * maxSceneSpeed);
Summary
The rotation speed factor. Defaults to 2.0.
Syntax
cameraController.rotateSpeed = 1.0;
Summary
The mouse rotation speed factor. Defaults to 0.1.
Syntax
cameraController.mouseRotateFactor = 1.0;