Camera

View source

Camera component that maps view-space coordinates into clip space and defines the visible depth range for an entity.

Add Camera to an entity with a transform and either a RenderLists2D or RenderLists3D component. The default PerspectiveProjection uses a field of view of Math.PI / 2 and an aspect ratio of 1; choose an OrthographicProjection for fixed view bounds, as the 2D camera prefab does.

Examples

import { World } from '@wimaengine/ecs'
import { createCamera3D } from '@wimaengine/render-core'

const world = new World()
const cameraEntity = world.spawn(createCamera3D(0, 0, 5))

Constructor


constructor(projection?: Projection, near?: number, far?: number)

View source

Creates a camera configuration with the supplied projection and clipping distances.

Defaults to a PerspectiveProjection with fov = Math.PI / 2, aspect = 1, near = 0.1, and far = 1000.

Parameters

NameDescriptionDefault
projection?

Projection used to build the camera matrix.

...
near?

Near clipping distance.

0.1
far?

Far clipping distance.

1000

Properties


Methods


viewToScreen2D(position: Vector2): Vector2

View source

Projects a 2D position from camera view space into screen-space NDC.

Returns undefined when this camera uses perspective projection.

Parameters

NameDescriptionDefault
position——

viewToScreen3D(position: Vector3): Vector3

View source

Projects a 3D position from camera view space into screen-space NDC.

The returned vector stores screen-space X and Y in x and y, with normalized device-coordinate depth in z.

Parameters

NameDescriptionDefault
position——

screenToView2D(point: Vector2): Vector2

View source

Converts screen-space NDC into a 2D position in camera view space.

Returns undefined when this camera uses perspective projection.

Parameters

NameDescriptionDefault
point——

screenToView3D(point: Vector2): object

View source

Converts screen-space NDC into a ray in camera view space.

Orthographic rays start on the near clipping plane. Perspective rays start at the camera origin.

Parameters

NameDescriptionDefault
point——

worldToScreen2D(position: Vector2, cameraTransform: Affine2): Vector2

View source

Projects a world-space 2D position into screen-space NDC.

Returns undefined when this camera uses perspective projection because a perspective camera does not define a single 2D world plane.

Parameters

NameDescriptionDefault
position——
cameraTransform——

worldToScreen3D(position: Vector3, cameraTransform: Affine3): Vector3

View source

Projects a world-space 3D position into screen-space NDC.

The returned vector stores screen-space X and Y in x and y, with normalized device-coordinate depth in z. The camera transform is inverted with Affine3 before applying the active projection.

Parameters

NameDescriptionDefault
position——
cameraTransform——

screenToWorld2D(point: Vector2, cameraTransform: Affine2): Vector2

View source

Converts screen-space NDC into a world-space 2D position.

Returns undefined when this camera uses perspective projection.

Parameters

NameDescriptionDefault
point——
cameraTransform——

screenToWorld3D(point: Vector2, cameraTransform: Affine3): object

View source

Converts screen-space NDC into a world-space ray.

Orthographic rays start on the near clipping plane. Perspective rays start at the camera position. The returned ray is transformed by the supplied camera affine.

Parameters

NameDescriptionDefault
point——
cameraTransform——

staticcopy(source: Camera, target?: Camera): Camera

View source

Copies a camera into a target camera.

The near and far distances are copied directly. Built-in PerspectiveProjection and OrthographicProjection values are recreated with their current settings, so the copied camera does not share those projection objects with its source. For another Projection implementation, no projection-specific copy is performed: the default target retains the source projection object, while an explicitly supplied target retains its own projection object.

Parameters

NameDescriptionDefault
source——
target?—...

staticclone(target: Camera): Camera

View source

Creates a copied camera with the same clipping distances and projection settings as the supplied camera.

This calls Camera.copy without an explicit target. For the built-in projection classes, the returned camera owns a new projection object. Custom Projection implementations retain their source projection object.

Parameters

NameDescriptionDefault
target——

staticdeserialize(value: CameraSerial, out?: Camera): Camera

View source

Reconstructs a Camera from CameraSerial data.

A nested type of perspective or orthographic selects the matching built-in projection deserializer. Any other nested projection type replaces the output projection with a default PerspectiveProjection. The method expects value.projection to exist and copies near and far as provided; it does not validate or fill in missing clipping values.

When out is supplied, that camera object is updated and returned, but its projection is replaced with a newly deserialized or fallback projection.

Parameters

NameDescriptionDefault
value——
out?—...

projectionMatrix(): Matrix4

View source

Builds a Matrix4 projection matrix for the current clipping distances.

This calls Projection.asProjectionMatrix with the current near and far values each time, so the result reflects the active projection and clipping distances. The camera does not cache or mutate a matrix.