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 CameraComponent to an entity
const entity = new Entity()
entity.addComponent('camera', {
nearClip: 1,
farClip: 100,
fov: 55
})
// Get the CameraComponent on an entity
const cameraComponent = entity.camera
// Update a property on a camera component
entity.camera.nearClip = 2
entity: Entity
The Entity that this Component is attached to.
Component.entity
system: ComponentSystem
The ComponentSystem used to create this Component.
Component.system
get aperture(): number
Gets the camera aperture in f-stops.
number
set aperture(value: number): void
Sets the camera aperture in f-stops. Default is 16. Higher value means less exposure.
number
void
get aspectRatio(): number
Gets the aspect ratio (width divided by height) of the camera.
number
set aspectRatio(value: number): void
Sets 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.
number
void
get aspectRatioMode(): number
Gets the aspect ratio mode of the camera.
number
set aspectRatioMode(value: number): void
Sets the aspect ratio mode of the camera. Can be:
Defaults to ASPECT_AUTO.
number
void
get calculateProjection(): CalculateMatrixCallback
Gets the custom function to calculate the camera projection matrix manually.
CalculateMatrixCallback
set calculateProjection(value: CalculateMatrixCallback): void
Sets the custom function 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:
Left and right are only used in stereo rendering.
CalculateMatrixCallback
void
get calculateTransform(): CalculateMatrixCallback
Gets the custom function to calculate the camera transformation matrix manually.
CalculateMatrixCallback
set calculateTransform(value: CalculateMatrixCallback): void
Sets the custom function to calculate the camera transformation matrix manually. Can be used for complex effects like reflections. Function is called using component's scope. Arguments:
Left and right are only used in stereo rendering.
CalculateMatrixCallback
void
get clearColor(): Color
Gets the camera component's clear color.
set clearColor(value: Color): void
Sets the camera component's clear color. Defaults to [0.75, 0.75, 0.75, 1].
void
get clearColorBuffer(): boolean
Gets whether the camera will automatically clear the color buffer before rendering.
boolean
set clearColorBuffer(value: boolean): void
Sets whether the camera will automatically clear the color buffer before rendering. Defaults to true.
boolean
void
get clearDepthBuffer(): boolean
Gets whether the camera will automatically clear the depth buffer before rendering.
boolean
set clearDepthBuffer(value: boolean): void
Sets whether the camera will automatically clear the depth buffer before rendering. Defaults to true.
boolean
void
get clearStencilBuffer(): boolean
Gets whether the camera will automatically clear the stencil buffer before rendering.
boolean
set clearStencilBuffer(value: boolean): void
Sets whether the camera will automatically clear the stencil buffer before rendering. Defaults to true.
boolean
void
get cullFaces(): boolean
Gets whether the camera will cull triangle faces.
boolean
set cullFaces(value: boolean): void
Sets whether the camera will cull triangle faces. If true, the camera will take
material.cull into account. Otherwise both front and back faces will be rendered. Defaults
to true.
boolean
void
get disablePostEffectsLayer(): number
Gets the layer id of the layer on which the post-processing of the camera stops being applied to.
number
set disablePostEffectsLayer(layer: number): void
Sets the layer id of the layer on which the post-processing of the camera stops being applied
to. Defaults to LAYERID_UI, which causes post-processing to not be applied to UI
layer and any following layers for the camera. Set to undefined for post-processing to be
applied to all layers of the camera.
number
void
get enabled(): boolean
Gets the enabled state of the component.
boolean
set enabled(arg: boolean): void
Sets the enabled state of the component.
boolean
void
Component.enabled
get farClip(): number
Gets the distance from the camera after which no rendering will take place.
number
set farClip(value: number): void
Sets the distance from the camera after which no rendering will take place. Defaults to 1000.
number
void
get flipFaces(): boolean
Gets whether the camera will flip the face direction of triangles.
boolean
set flipFaces(value: boolean): void
Sets whether the camera will flip the face direction of triangles. If set to true, the camera will invert front and back faces. Can be useful for reflection rendering. Defaults to false.
boolean
void
get fog(): null | FogParams
Gets a FogParams that defines fog parameters, or null if those are not set.
null | FogParams
set fog(value: null | FogParams): void
Sets the fog parameters. If this is not null, the camera will use these fog parameters instead of those specified on the Scene#fog.
null | FogParams
void
get fov(): number
Gets the field of view of the camera in degrees.
number
set fov(value: number): void
Sets the field of view of the camera in degrees. Usually this is the Y-axis field of view, see CameraComponent#horizontalFov. Used for PROJECTION_PERSPECTIVE cameras only. Defaults to 45.
number
void
get frustum(): Frustum
Gets the camera's frustum shape.
get frustumCulling(): boolean
Gets whether frustum culling is enabled.
boolean
set frustumCulling(value: boolean): void
Sets whether frustum culling is enabled. This controls the culling of mesh instances against the camera frustum, i.e. if objects outside of the camera's frustum should be omitted from rendering. If false, all mesh instances in the scene are rendered by the camera, regardless of visibility. Defaults to false.
boolean
void
get gammaCorrection(): number
Gets the gamma correction used when rendering the scene.
number
set gammaCorrection(value: number): void
Sets the gamma correction to apply when rendering the scene. Can be:
Defaults to GAMMA_SRGB.
number
void
get horizontalFov(): boolean
Gets whether the camera's field of view (fov) is horizontal or vertical.
boolean
set horizontalFov(value: boolean): void
Sets whether the camera's field of view (fov) is horizontal or vertical. Defaults to
false (meaning it is vertical by default).
boolean
void
get jitter(): number
Gets the jitter intensity applied in the projection matrix.
number
set jitter(value: number): void
Sets the jitter intensity applied in the projection matrix. Used for jittered sampling by TAA.
A value of 1 represents a jitter in the range of [-1, 1] of a pixel. Smaller values result
in a crisper yet more aliased outcome, whereas increased values produce a smoother but blurred
result. Defaults to 0, representing no jitter.
number
void
get layers(): number[]
Gets the array of layer IDs (Layer#id) to which this camera belongs.
number
set layers(newValue: number[]): void
Sets the array of layer IDs (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.
Defaults to [LAYERID_WORLD, LAYERID_DEPTH, LAYERID_SKYBOX, LAYERID_UI, LAYERID_IMMEDIATE].
number
void
get nearClip(): number
Gets the distance from the camera before which no rendering will take place.
number
set nearClip(value: number): void
Sets the distance from the camera before which no rendering will take place. Defaults to 0.1.
number
void
get orthoHeight(): number
Gets the half-height of the orthographic view window (in the Y-axis).
number
set orthoHeight(value: number): void
Sets the half-height of the orthographic view window (in the Y-axis). Used for PROJECTION_ORTHOGRAPHIC cameras only. Defaults to 10.
number
void
get postEffects(): PostEffectQueue
Gets the post effects queue for this camera. Use this to add or remove post effects from the camera.
get priority(): number
Gets the priority to control the render order of this camera.
number
set priority(newValue: number): void
Sets the priority to control the render order of this camera. Cameras with a smaller priority value are rendered first. Defaults to 0.
number
void
get projection(): number
Gets the type of projection used to render the camera.
number
set projection(value: number): void
Sets the type of projection used to render the camera. Can be:
Defaults to PROJECTION_PERSPECTIVE.
number
void
get projectionMatrix(): Mat4
Gets the camera's projection matrix.
get rect(): Vec4
Gets the rendering rectangle for the camera.
set rect(value: Vec4): void
Sets the rendering rectangle for the camera. This controls where on the screen the camera
will render in normalized screen coordinates. Defaults to [0, 0, 1, 1].
void
get renderTarget(): RenderTarget
Gets the render target to which rendering of the camera is performed.
RenderTarget
set renderTarget(value: RenderTarget): void
Sets the render target to which rendering of the camera is performed. If not set, it will render simply to the screen.
RenderTarget
void
get scissorRect(): Vec4
Gets the scissor rectangle for the camera.
set scissorRect(value: Vec4): void
Sets the scissor rectangle for the camera. This clips all pixels which are not in the
rectangle. The order of the values is [x, y, width, height]. Defaults to [0, 0, 1, 1].
void
get sensitivity(): number
Gets the camera sensitivity in ISO.
number
set sensitivity(value: number): void
Sets the camera sensitivity in ISO. Defaults to 1000. Higher value means more exposure.
number
void
get shutter(): number
Gets the camera shutter speed in seconds.
number
set shutter(value: number): void
Sets the camera shutter speed in seconds. Defaults to 1/1000s. Longer shutter means more exposure.
number
void
get toneMapping(): number
Gets the tonemapping transform applied to the rendered color buffer.
number
set toneMapping(value: number): void
Sets the tonemapping transform to apply to the rendered color buffer. Can be:
Defaults to TONEMAP_LINEAR.
number
void
get viewMatrix(): Mat4
Gets the camera's view matrix.
calculateAspectRatio(rt?: null | RenderTarget): number
Calculates aspect ratio value for a given render target.
Optional render target. If unspecified, the backbuffer is used.
null | RenderTarget
number
The aspect ratio of the render target (or backbuffer).
endXr(callback?: XrErrorCallback): void
Attempt to end XR session of this camera.
XrErrorCallback
Optional callback function called once session is ended. The callback has one argument Error - it is null if successfully ended XR session.
void
// On an entity with a camera component
this.entity.camera.endXr(function (err) {
// not anymore in XR
})
fire(
name: string,
arg1?: any,
arg2?: any,
arg3?: any,
arg4?: any,
arg5?: any,
arg6?: any,
arg7?: any,
arg8?: any): EventHandler
Fire an event, all additional arguments are passed on to the event listener.
string
Name of event to fire.
any
First argument that is passed to the event handler.
any
Second argument that is passed to the event handler.
any
Third argument that is passed to the event handler.
any
Fourth argument that is passed to the event handler.
any
Fifth argument that is passed to the event handler.
any
Sixth argument that is passed to the event handler.
any
Seventh argument that is passed to the event handler.
any
Eighth argument that is passed to the event handler.
Self for chaining.
obj.fire('test', 'This is the message')
Component.fire
getShaderPass(): undefined | string
Shader pass name.
undefined | string
The name of the shader pass, or undefined if no shader pass is set.
hasEvent(name: string): boolean
Test if there are any handlers bound to an event name.
string
The name of the event to test.
boolean
True if the object has handlers bound to the specified event name.
obj.on('test', () => {}) // bind an event to 'test'
obj.hasEvent('test') // returns true
obj.hasEvent('hello') // returns false
Component.hasEvent
off(
name?: string,
callback?: HandleEventCallback,
scope?: any): EventHandler
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.
string
Name of the event to unbind.
HandleEventCallback
Function to be unbound.
any
Scope that was used as the this when the event is fired.
Self for chaining.
const handler = () => {}
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 handler functions, called 'test' with scope this
Component.off
on(
name: string,
callback: HandleEventCallback,
scope?: any): EventHandle
Attach an event handler to an event.
string
Name of the event to bind the callback to.
HandleEventCallback
Function that is called when event is fired. Note the callback is limited to 8 arguments.
any = ...
Object to use as 'this' when the event is fired, defaults to current this.
Can be used for removing event in the future.
obj.on('test', (a, b) => {
console.log(a + b)
})
obj.fire('test', 1, 2) // prints 3 to the console
const evt = obj.on('test', (a, b) => {
console.log(a + b)
})
// some time later
evt.off()
Component.on
once(
name: string,
callback: HandleEventCallback,
scope?: any): EventHandle
Attach an event handler to an event. This handler will be removed after being fired once.
string
Name of the event to bind the callback to.
HandleEventCallback
Function that is called when event is fired. Note the callback is limited to 8 arguments.
any = ...
Object to use as 'this' when the event is fired, defaults to current this.
obj.once('test', (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
Component.once
requestSceneColorMap(enabled: boolean): void
Request the scene to generate a texture containing the scene color map. Note that this call is accumulative, and for each enable request, a disable request need to be called. Note that this setting is ignored when the CameraComponent#renderPasses is used.
boolean
True to request the generation, false to disable it.
void
requestSceneDepthMap(enabled: boolean): void
Request the scene to generate a texture containing the scene depth map. Note that this call is accumulative, and for each enable request, a disable request need to be called. Note that this setting is ignored when the CameraComponent#renderPasses is used.
boolean
True to request the generation, false to disable it.
void
screenToWorld(
screenx: number,
screeny: number,
cameraz: number,
worldCoord?: Vec3): Vec3
Convert a point from 2D screen space to 3D world space.
number
X coordinate on Mirror Engine' canvas element. Should be in the range
0 to canvas.offsetWidth of the application's canvas element.
number
Y coordinate on Mirror Engine' canvas element. Should be in the range
0 to canvas.offsetHeight of the application's canvas element.
number
The distance from the camera in world space to create the new point.
3D vector to receive world coordinate result.
The world space coordinate.
// Get the start and end points of a 3D ray fired from a screen click position
const start = entity.camera.screenToWorld(
clickX,
clickY,
entity.camera.nearClip
)
const 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')
})
setShaderPass(name: string): number
Sets the name of the shader pass the camera will use when rendering.
In addition to existing names (see the parameter description), a new name can be specified, which creates a new shader pass with the given name. The name provided can only use alphanumeric characters and underscores. When a shader is compiled for the new pass, a define is added to the shader. For example, if the name is 'custom_rendering', the define 'CUSTOM_RENDERING_PASS' is added to the shader, allowing the shader code to conditionally execute code only when that shader pass is active.
Another instance where this approach may prove useful is when a camera needs to render a more cost-effective version of shaders, such as when creating a reflection texture. To accomplish this, a callback on the material that triggers during shader compilation can be used. This callback can modify the shader generation options specifically for this shader pass.
const shaderPassId = camera.setShaderPass('custom_rendering')
material.onUpdateShader = function (options) {
if (options.pass === shaderPassId) {
options.litOptions.normalMapEnabled = false
options.litOptions.useSpecular = false
}
return options
}
string
The name of the shader pass. Defaults to undefined, which is equivalent to SHADERPASS_FORWARD. Can be:
number
The id of the shader pass.
startXr(
type: string,
spaceType: string,
options?: {
anchors: boolean;
callback: XrErrorCallback;
depthSensing: {
dataFormatPreference: string;
usagePreference: string;
};
imageTracking: boolean;
optionalFeatures: string[];
planeDetection: boolean;
}): void
Attempt to start XR session with this camera.
string
The type of session. Can be one of the following:
string
Reference space type. Can be one of the following:
Object with options for XR session initialization.
boolean
Optional boolean to attempt to enable XrAnchors.
XrErrorCallback
Optional callback function called once the session is started. The callback has one argument Error - it is null if the XR session started successfully.
{
dataFormatPreference: string;
usagePreference: string;
}
Optional object with parameters to attempt to enable depth sensing.
string
Optional data format preference for depth sensing. Can be 'luminance-alpha' or 'float32' (XRDEPTHSENSINGFORMAT_*), defaults to 'luminance-alpha'. Most preferred and supported will be chosen by the underlying depth sensing system.
string
Optional usage preference for depth sensing, can be 'cpu-optimized' or 'gpu-optimized' (XRDEPTHSENSINGUSAGE_*), defaults to 'cpu-optimized'. Most preferred and supported will be chosen by the underlying depth sensing system.
boolean
Set to true to attempt to enable XrImageTracking.
string
Optional features for XRSession start. It is used for getting access to additional WebXR spec extensions.
boolean
Set to true to attempt to enable XrPlaneDetection.
void
// On an entity with a camera component
this.entity.camera.startXr(XRTYPE_VR, XRSPACE_LOCAL, {
callback: function (err) {
if (err) {
// failed to start XR session
} else {
// in XR
}
}
})
worldToScreen(worldCoord: Vec3, screenCoord?: Vec3): Vec3
Convert a point from 3D world space to 2D screen space.
The world space coordinate.
3D vector to receive screen coordinate result.
The screen space coordinate.