Camera

Camera Component

The camera component can project a 3D scene onto a 2D screen. Based on the camera component, we can customize various rendering effects.

First, you need to mount the camera component onto an activated Entity in the scene. Editor projects usually come with a camera component by default, but you can also add one manually.

image-20231009114711623

After adding it, you can view the camera properties in the inspector, and the camera preview in the lower left corner allows you to conveniently see the camera effect during the actual project runtime:

image-20240718211520816

You can also mount the camera component to an Entity in the script with the following code:

// Create an entity
const entity = root.createChild("cameraEntity");
// Create camera
const camera = entity.addComponent(Camera);

Properties

By modifying the properties of the camera component, you can customize the rendering effects. Below are the property settings exposed by the camera component in the Inspector Panel.

image-20240718211645854

You can also get the camera component and set the corresponding properties through the script.

// Get the camera component from the node where the camera is mounted
const camera = entity.getComponent(Camera);
 
// Enable post-processing
camera.enablePostProcess = true;
// Enable HDR
camera.enableHDR = true;
// Enable FXAA
camera.antiAliasing = AntiAliasing.FXAA;
// Preserve the alpha channel in the output
camera.isAlphaOutputRequired = true;

The functionality corresponding to each property is as follows:

TypePropertyDescription
GeneralisOrthographicDetermines whether to use perspective projection or orthographic projection by setting isOrthographic. Set to false for perspective effect, default is false.
nearClipPlaneNear clipping plane. Objects closer to the camera than this value will not be rendered properly.
farClipPlaneFar clipping plane. Objects farther from the camera than this value will not be rendered properly.
viewportViewport, determines the range of content rendered to the target device. Modifying this value can determine the final rendering position in the rendering target.
priorityRendering priority, used to determine the order in which cameras render their content in the case of multiple cameras.
enableFrustumCullingWhether to enable frustum culling. When enabled, objects outside the rendering range will be culled. Default is true.
clearFlagsFlags to clear the canvas buffer before rendering this camera. By setting these flags, you can selectively retain the results of the previous camera rendering.
cullingMaskCulling mask, used to selectively render rendering components in the scene.
aspectRatioAspect ratio of the rendering target, generally automatically calculated based on the canvas size, but can also be manually changed (not recommended).
renderTargetRendering target, determines which target the content is rendered to.
pixelViewportThe camera's viewport on the screen (in pixel coordinates). If the rendering target is the canvas and the viewport is the entire canvas, the top-left corner is (0, 0) and the bottom-right corner is (canvas.width, canvas.height).
Perspective ProjectionfieldOfViewField of view, default is 45 degrees (0, 180).
Orthographic ProjectionorthographicSizeIn orthographic mode, half the distance from the top to the bottom of the camera's view.
Rendering RelateddepthTextureModeDepth texture mode, default is DepthTextureMode.None. If enabled, the camera_DepthTexture depth texture can be used in the shader. For details, refer to Camera Texture.
opaqueTextureEnabledWhether to enable opaque texture. Default is off. If enabled, the camera_OpaqueTexture opaque texture can be used in the shader of the transparent queue.
opaqueTextureDownsamplingWhen opaque texture is enabled, downsampling can be set according to clarity and performance requirements.
antiAliasingSelect the anti-aliasing method, the default is not turned on, see the details:antiAliasing
msaaSamplesMultiple anti-aliasing sample number, default 4 times, see detailsantiAliasing
enableHDRWhether to enable HDR rendering, allowing the shader's output color to be stored using floating-point numbers, providing a wider range of values for post-processing and other scenarios.
enablePostProcessWhether to enable post-processing. For post-processing configuration, see Post-Processing Tutorial.
postProcessMaskPost-processing mask, which determines the effective post-processing components. For post-processing configuration, Post-Processing Tutorial.
isAlphaOutputRequiredDetermines whether to preserve the alpha channel in the output. When set to true, the alpha channel is always preserved, when set to false, the engine automatically decides whether to preserve it.

Culling Mask

The camera component can selectively render the rendering components in the scene by setting the cullingMask.

Render Target

The camera component can render the result to different targets by setting the renderTarget.

Frustum Culling

The enableFrustumCulling property is enabled by default because, in a 3D world, "things that are not visible do not need to be rendered" is a very natural logic and is the most basic performance optimization. Disabling frustum culling means turning off this optimization. If you want to keep this optimization but always render a specific node, you can set the bounding box of the node's renderer to be infinite.

Methods

The camera component provides various methods (mainly related to rendering and space transformation) to facilitate developers in achieving the desired customization capabilities. Before that, you need to learn how to get the camera component. If you know which node the camera component is mounted on, you can directly get it through getComponent or getComponentsIncludeChildren:

// 从挂载相机的节点上获取相机组件
const camera = entity.getComponent(Camera);
// 从挂载相机节点的父节点上获取相机组件(不推荐)
const cameras = entity.getComponentsIncludeChildren(Camera, []);

If you are not sure which node the camera component is mounted on, you can also get all the camera components in the scene through a more hacky way:

// Get all camera components in this scene (not recommended)
const cameras = scene._componentsManager._activeCameras;
TypePropertyDescription
RenderingresetProjectionMatrixReset the custom projection matrix and revert to automatic mode.
resetAspectRatioReset the custom rendering aspect ratio and revert to automatic mode.
renderManual rendering.
setReplacementShaderSet a global rendering replacement shader.
resetReplacementShaderClear the global rendering replacement shader.
Space TransformationworldToViewportPointConvert a point from world space to viewport space.
viewportToWorldPointConvert a point from viewport space to world space.
viewportPointToRayGenerate a world space ray from a point in viewport space.
screenToViewportPointConvert a point from screen space to viewport space.
viewportToScreenPointConvert a point from viewport space to screen space.
worldToScreenPointConvert a point from world space to screen space.
screenToWorldPointConvert a point from screen space to world space.
screenPointToRayGenerate a world space ray from a point in screen space.

Shader Replacement

With the ability of setReplacementShader to globally replace shaders, you can observe specific rendering effects.

Space Transformation

It should be noted that the Z of the point passed into methods like screenToWorldPoint and viewportToWorldPoint represents the distance from the returned point to the camera.

onBeginRender and onEndRender

The camera component additionally includes two lifecycle callbacks, onBeginRender and onEndRender. Their sequence can be referenced in the script lifecycle sequence diagram.

Was this page helpful?