pc.CameraComponent
Extends: pc.Component
The Camera Component enables an Entity to render the scene. A scene requires at least one enabled camera component to be rendered. Note that multiple camera components can be enabled simultaneously (for split-screen or offscreen rendering, for example).
// Add a pc.CameraComponent to an entity
var entity = new pc.Entity();
entity.addComponent('camera', {
nearClip: 1,
farClip: 100,
fov: 55
});
// Get the pc.CameraComponent on an entity
var cameraComponent = entity.camera;
// Update a property on a camera component
entity.camera.nearClip = 2;
Summary
Properties
| aspectRatio | The aspect ratio (width divided by height) of the camera. |
| aspectRatioMode | The aspect ratio mode of the camera. |
| calculateProjection | Custom function you can provide to calculate the camera projection matrix manually. |
| calculateTransform | Custom function you can provide to calculate the camera transformation matrix manually. |
| clearColor | The color used to clear the canvas to before the camera starts to render. |
| clearColorBuffer | If true the camera will clear the color buffer to the color set in clearColor. |
| clearDepthBuffer | If true the camera will clear the depth buffer. |
| clearStencilBuffer | If true the camera will clear the stencil buffer. |
| cullFaces | If true the camera will take material. |
| farClip | The distance from the camera after which no rendering will take place. |
| flipFaces | If true the camera will invert front and back faces. |
| fov | The field of view of the camera in degrees. |
| frustum | Queries the camera's frustum shape.[read only] |
| frustumCulling | Controls the culling of mesh instances against the camera frustum, i. |
| horizontalFov | Set which axis to use for the Field of View calculation. |
| layers | An array of layer IDs (pc.Layer#id) to which this camera should belong. |
| nearClip | The distance from the camera before which no rendering will take place. |
| node | Queries the camera's GraphNode.[read only] |
| orthoHeight | The half-height of the orthographic view window (in the Y-axis). |
| postEffects | The post effects queue for this camera. |
| priority | Controls the order in which cameras are rendered. |
| projection | The type of projection used to render the camera. |
| projectionMatrix | Queries the camera's projection matrix.[read only] |
| rect | Controls where on the screen the camera will be rendered in normalized screen coordinates. |
| scissorRect | Clips all pixels which are not in the rectangle. |
| viewMatrix | Queries the camera's view matrix.[read only] |
Methods
| calculateAspectRatio | Calculates aspect ratio value for a given render target. |
| endXr | Attempt to end XR session of this camera |
| screenToWorld | Convert a point from 2D screen space to 3D world space. |
| startXr | Attempt to start XR session with this camera |
| worldToScreen | Convert a point from 3D world space to 2D screen space. |
Inherited
Properties
| system | The ComponentSystem used to create this Component. |
| entity | The Entity that this Component is attached to. |
| enabled | Enables or disables the component. |
Methods
| fire | Fire an event, all additional arguments are passed on to the event listener. |
| hasEvent | Test if there are any handlers bound to an event name. |
| off | Detach an event handler from an event. |
| on | Attach an event handler to an event. |
| once | Attach an event handler to an event. |
Details
Constructor
CameraComponent(system, entity)
Create a new Camera Component.
// Add a pc.CameraComponent to an entity
var entity = new pc.Entity();
entity.addComponent('camera', {
nearClip: 1,
farClip: 100,
fov: 55
});
// Get the pc.CameraComponent on an entity
var cameraComponent = entity.camera;
// Update a property on a camera component
entity.camera.nearClip = 2;
Parameters
| system | pc.CameraComponentSystem | The ComponentSystem that created this Component. |
| entity | pc.Entity | The Entity that this Component is attached to. |
Properties
The aspect ratio (width divided by height) of the camera. If aspectRatioMode is ASPECT_AUTO, then this value will be automatically calculated every frame, and you can only read it. If it's ASPECT_MANUAL, you can set the value.
The aspect ratio mode of the camera. Can be pc.ASPECT_AUTO (default) or pc.ASPECT_MANUAL. ASPECT_AUTO will always be current render target's width divided by height. ASPECT_MANUAL will use the aspectRatio value instead.
Custom function you can provide to calculate the camera projection matrix manually. Can be used for complex effects like doing oblique projection. Function is called using component's scope. Arguments:
- {pc.Mat4} transformMatrix: output of the function
- {number} view: Type of view. Can be pc.VIEW_CENTER, pc.VIEW_LEFT or pc.VIEW_RIGHT. Left and right are only used in stereo rendering.
Custom function you can provide to calculate the camera transformation matrix manually. Can be used for complex effects like reflections. Function is called using component's scope. Arguments:
- {pc.Mat4} transformMatrix: output of the function.
- {number} view: Type of view. Can be pc.VIEW_CENTER, pc.VIEW_LEFT or pc.VIEW_RIGHT. Left and right are only used in stereo rendering.
If true the camera will clear the color buffer to the color set in clearColor.
If true the camera will take material.cull into account. Otherwise both front and back faces will be rendered.
If true the camera will invert front and back faces. Can be useful for reflection rendering.
The field of view of the camera in degrees. Usually this is the Y-axis field of view, see pc.CameraComponent#horizontalFov. Used for pc.PROJECTION_PERSPECTIVE cameras only. Defaults to 45.
Controls the culling of mesh instances against the camera frustum, i.e. if objects outside of camera should be omitted from rendering. If true, culling is enabled. If false, all mesh instances in the scene are rendered by the camera, regardless of visibility. Defaults to false.
Set which axis to use for the Field of View calculation. Defaults to false (use Y-axis).
An array of layer IDs (pc.Layer#id) to which this camera should belong. Don't push/pop/splice or modify this array, if you want to change it - set a new one instead.
Queries the camera's GraphNode. Can be used to get position and rotation.
[read only]The half-height of the orthographic view window (in the Y-axis). Used for pc.PROJECTION_ORTHOGRAPHIC cameras only. Defaults to 10.
The post effects queue for this camera. Use this to add or remove post effects from the camera.
Controls the order in which cameras are rendered. Cameras with smaller values for priority are rendered first.
The type of projection used to render the camera. Can be:
- pc.PROJECTION_PERSPECTIVE: A perspective projection. The camera frustum resembles a truncated pyramid.
- pc.PROJECTION_ORTHOGRAPHIC: An orthographic projection. The camera frustum is a cuboid.
Defaults to pc.PROJECTION_PERSPECTIVE.
Controls where on the screen the camera will be rendered in normalized screen coordinates.
Clips all pixels which are not in the rectangle. The order of the values is [x, y, width, height].
Methods
calculateAspectRatio([rt])
Calculates aspect ratio value for a given render target.
Parameters
| rt | pc.RenderTarget | Optional render target. If unspecified, the backbuffer is assumed. |
Returns
numberThe aspect ratio of the render target (or backbuffer).
endXr([callback])
Attempt to end XR session of this camera
// On an entity with a camera component
this.entity.camera.endXr(function (err) {
// not anymore in XR
});
Parameters
| callback | pc.callbacks.XrError | Optional callback function called once session is ended. The callback has one argument Error - it is null if successfully ended XR session. |
screenToWorld(screenx, screeny, cameraz, [worldCoord])
Convert a point from 2D screen space to 3D world space.
// Get the start and end points of a 3D ray fired from a screen click position
var start = entity.camera.screenToWorld(clickX, clickY, entity.camera.nearClip);
var end = entity.camera.screenToWorld(clickX, clickY, entity.camera.farClip);
// Use the ray coordinates to perform a raycast
app.systems.rigidbody.raycastFirst(start, end, function (result) {
console.log("Entity " + result.entity.name + " was selected");
});
Parameters
| screenx | number | X coordinate on PlayCanvas' canvas element. |
| screeny | number | Y coordinate on PlayCanvas' canvas element. |
| cameraz | number | The distance from the camera in world space to create the new point. |
| worldCoord | pc.Vec3 | 3D vector to receive world coordinate result. |
Returns
pc.Vec3The world space coordinate.
startXr(type, spaceType, [callback])
Attempt to start XR session with this camera
// On an entity with a camera component
this.entity.camera.startXr(pc.XRTYPE_VR, pc.XRSPACE_LOCAL, function (err) {
if (err) {
// failed to start XR session
} else {
// in XR
}
});
Parameters
| type | string | The type of session. Can be one of the following:
|
| spaceType | string | reference space type. Can be one of the following:
|
| callback | pc.callbacks.XrError | Optional callback function called once the session is started. The callback has one argument Error - it is null if the XR session started successfully. |
worldToScreen(worldCoord, [screenCoord])
Convert a point from 3D world space to 2D screen space.
Parameters
| worldCoord | pc.Vec3 | The world space coordinate. |
| screenCoord | pc.Vec3 | 3D vector to receive screen coordinate result. |
Returns
pc.Vec3The screen space coordinate.
Inherited
Properties
Methods
fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])
Fire an event, all additional arguments are passed on to the event listener.
obj.fire('test', 'This is the message');
Parameters
| name | object | Name of event to fire. |
| arg1 | * | First argument that is passed to the event handler. |
| arg2 | * | Second argument that is passed to the event handler. |
| arg3 | * | Third argument that is passed to the event handler. |
| arg4 | * | Fourth argument that is passed to the event handler. |
| arg5 | * | Fifth argument that is passed to the event handler. |
| arg6 | * | Sixth argument that is passed to the event handler. |
| arg7 | * | Seventh argument that is passed to the event handler. |
| arg8 | * | Eighth argument that is passed to the event handler. |
Returns
pc.EventHandlerSelf for chaining.
hasEvent(name)
Test if there are any handlers bound to an event name.
obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false
Parameters
| name | string | The name of the event to test. |
Returns
booleanTrue if the object has handlers bound to the specified event name.
off([name], [callback], [scope])
Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.
var handler = function () {
};
obj.on('test', handler);
obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all hander functions, called 'test' with scope this
Parameters
| name | string | Name of the event to unbind. |
| callback | pc.callbacks.HandleEvent | Function to be unbound. |
| scope | object | Scope that was used as the this when the event is fired. |
Returns
pc.EventHandlerSelf for chaining.
on(name, callback, [scope])
Attach an event handler to an event.
obj.on('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
Parameters
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf for chaining.
once(name, callback, [scope])
Attach an event handler to an event. This handler will be removed after being fired once.
obj.once('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
obj.fire('test', 1, 2); // not going to get handled
Parameters
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf for chaining.