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.
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:
You can also mount the camera component to an Entity in the script with the following code:
// 创建实体
const entity = root.createChild("cameraEntity");
// 创建相机组件
const camera = entity.addComponent(Camera);
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.
You can also get the camera component and set the corresponding properties through the script.
// 从挂载相机的节点上获取相机组件
const camera = entity.getComponent(Camera);
// 设置相机类型
camera.isOrthographic = true;
// 设置相机的近平面
camera.nearClipPlane = 0.1;
// 设置相机的远平面
camera.farClipPlane = 100;
// 设置相机的 FOV(角度制)
camera.fieldOfView = 45;
// 设置相机在画布上的渲染区域(归一化)
camera.viewport = new Vector4(0, 0, 1, 1);
// 设置相机的渲染优先级(值越小,渲染优先级越高)
camera.priority = 0;
// 设置相机是否开启视锥体裁剪
camera.enableFrustumCulling = true;
// 设置相机渲染前的清除标记
camera.clearFlags = CameraClearFlags.All;
// 开启后处理
camera.enablePostProcess = true;
// 开启 HDR
camera.enableHDR = true;
The functionality corresponding to each property is as follows:
Type | Property | Description |
---|---|---|
General | isOrthographic | Determines whether to use perspective projection or orthographic projection by setting isOrthographic. Set to false for perspective effect, default is false . |
nearClipPlane | Near clipping plane. Objects closer to the camera than this value will not be rendered properly. | |
farClipPlane | Far clipping plane. Objects farther from the camera than this value will not be rendered properly. | |
viewport | Viewport, determines the range of content rendered to the target device. Modifying this value can determine the final rendering position in the rendering target. | |
priority | Rendering priority, used to determine the order in which cameras render their content in the case of multiple cameras. | |
enableFrustumCulling | Whether to enable frustum culling. When enabled, objects outside the rendering range will be culled. Default is true . | |
clearFlags | Flags to clear the canvas buffer before rendering this camera. By setting these flags, you can selectively retain the results of the previous camera rendering. | |
cullingMask | Culling mask, used to selectively render rendering components in the scene. | |
aspectRatio | Aspect ratio of the rendering target, generally automatically calculated based on the canvas size, but can also be manually changed (not recommended). | |
renderTarget | Rendering target, determines which target the content is rendered to. | |
pixelViewport | The 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 Projection | fieldOfView | Field of view, default is 45 degrees (0, 180). |
Orthographic Projection | orthographicSize | In orthographic mode, half the distance from the top to the bottom of the camera's view. |
Rendering Related | depthTextureMode | Depth 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. |
opaqueTextureEnabled | Whether to enable opaque texture. Default is off. If enabled, the camera_OpaqueTexture opaque texture can be used in the shader of the transparent queue. | |
opaqueTextureDownsampling | When opaque texture is enabled, downsampling can be set according to clarity and performance requirements. | |
msaaSamples | Number of samples for multi-sample anti-aliasing, effective only when the standalone canvas is enabled, such as enableHDR , enablePostProcess , opaqueTextureEnabled . | |
enableHDR | Whether 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. | |
enablePostProcess | Whether to enable post-processing. For post-processing configuration, see Post-Processing Tutorial. | |
postProcessMask | Post-processing mask, which determines the effective post-processing components. For post-processing configuration, Post-Processing Tutorial. |
The camera component can selectively render the rendering components in the scene by setting the cullingMask
.
The camera component can render the result to different targets by setting the renderTarget
.
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.
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;
Type | Property | Description |
---|---|---|
Rendering | resetProjectionMatrix | Reset the custom projection matrix and revert to automatic mode. |
resetAspectRatio | Reset the custom rendering aspect ratio and revert to automatic mode. | |
render | Manual rendering. | |
setReplacementShader | Set a global rendering replacement shader. | |
resetReplacementShader | Clear the global rendering replacement shader. | |
Space Transformation | worldToViewportPoint | Convert a point from world space to viewport space. |
viewportToWorldPoint | Convert a point from viewport space to world space. | |
viewportPointToRay | Generate a world space ray from a point in viewport space. | |
screenToViewportPoint | Convert a point from screen space to viewport space. | |
viewportToScreenPoint | Convert a point from viewport space to screen space. | |
worldToScreenPoint | Convert a point from world space to screen space. | |
screenToWorldPoint | Convert a point from screen space to world space. | |
screenPointToRay | Generate a world space ray from a point in screen space. |
With the ability of setReplacementShader
to globally replace shaders, you can observe specific rendering effects:
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.
The camera component additionally includes two lifecycle callbacks, onBeginRender and onEndRender. Their sequence can be referenced in the script lifecycle sequence diagram.