PerspectiveProjection

View source

ExtendsProjection

Represents the perspective variant of the Projection contract used by Camera.

This class identifies its variant with ProjectionType.Perspective.

Examples

import { Camera, PerspectiveProjection } from '@wimaengine/render-core'

const camera = new Camera(new PerspectiveProjection(Math.PI / 3, 16 / 9))
const matrix = camera.projectionMatrix()

Constructor


constructor(fov?: number, aspect?: number)

View source

Creates a perspective projection value.

The constructor stores fov and aspect as supplied. It does not validate, clamp, or normalize either value.

Parameters

NameDescriptionDefault
fov?

Full vertical field-of-view angle in radians.

...
aspect?

Width-to-height aspect ratio.

1

Properties


readonlytype: number

View source

Numeric projection discriminator for this value.

Default: ProjectionType.Perspective

aspect: number

View source

Width-to-height aspect ratio used to size the near-plane frustum.

Methods


isPerspective(): boolean

View source

Reports that this projection uses perspective mapping.

Returns

Always true for a perspective projection.

asProjectionMatrix(near: number, far: number): Matrix4

View source

Builds a perspective matrix from this projection and clipping distances.

The near-plane bounds are computed as top = near * Math.tan(0.5 * fov), height = 2 * top, width = aspect * height, and left = -0.5 * width. The method then calls PerspectiveProjection.from with (left, left + width, top, top - height, near, far) and returns its new matrix. This projection object is not mutated.

Parameters

NameDescriptionDefault
near

Near clipping distance used to size the frustum.

—
far

Far clipping distance used by the depth terms.

—

Returns

A new perspective projection matrix.

staticfrom(left: number, right: number, top: number, bottom: number, near: number, far: number, out?: Matrix4): Matrix4

View source

Builds a perspective frustum matrix from explicit bounds and clipping distances.

The scale and offset terms are computed as follows: x = 2 * near / (right - left), y = 2 * near / (top - bottom), a = (right + left) / (right - left), b = (top + bottom) / (top - bottom), c = -(far + near) / (far - near), and d = -2 * far * near / (far - near). The method overwrites all entries of out with those terms and the perspective constants, then returns out. It performs no bounds or clipping-distance validation.

Parameters

NameDescriptionDefault
left

Left frustum bound at the near plane.

—
right

Right frustum bound at the near plane.

—
top

Top frustum bound at the near plane.

—
bottom

Bottom frustum bound at the near plane.

—
near

Near clipping distance.

—
far

Far clipping distance.

—
out?

Matrix to overwrite; defaults to a new matrix.

...

Returns

The newly created or supplied matrix.

staticserialize(value: PerspectiveProjection): PerspectiveProjectionSerial

View source

Serializes a perspective projection’s discriminator and settings.

The returned object always has type: 'perspective' and copies fov and aspect from value without validating them.

Parameters

NameDescriptionDefault
value

Projection value to serialize.

—

Returns

Serialized perspective settings.

staticdeserialize(value: PerspectiveProjectionSerial, out?: PerspectiveProjection): PerspectiveProjection

View source

Deserializes perspective settings into a projection value.

The method copies value.fov and value.aspect directly. It does not validate the input or copy its type field. Without out, it creates a new PerspectiveProjection; with out, it updates and returns that same object.

Parameters

NameDescriptionDefault
value

Serialized perspective settings.

—
out?

Projection value to update.

...

Returns

The new or supplied projection value.

staticvalidateSerial(value: unknown): value is PerspectiveProjectionSerial

View source

Checks whether a value has the serialized perspective shape.

The value must be a non-null object containing type, fov, and aspect, with type === 'perspective' and both settings having the number JavaScript type. Extra properties are allowed; numeric values are not required to be finite or within a projection-specific range.

Parameters

NameDescriptionDefault
value

Value to check.

—

Returns

true when the required serialized fields match.