Class Matrix4
Package: @aspose/3d (v24.12.0)
Matrix4 stores a 4×4 transformation matrix as a flat array of 16 numbers in row-major order. It is the type of Transform.transformMatrix and GlobalTransform.transformMatrix. Individual elements are accessible as m00–m33 properties, or by index via getItem(i) / setItem(i, v). Matrix4 is exported from the @aspose/3d/utilities sub-path.
export class Matrix4import { Matrix4 } from '@aspose/3d/utilities';Examples
Build a combined TRS matrix and decompose it back into components.
import { Matrix4, Vector3, Quaternion } from '@aspose/3d/utilities';
const t = Matrix4.translate(new Vector3(5, 0, 0));
const r = Matrix4.rotate(Quaternion.fromEulerAngle(0, Math.PI / 4, 0));
const s = Matrix4.scale(new Vector3(2, 2, 2));
const trs = t.concatenate(r).concatenate(s);
const translation: any[] = [null];
const scaling: any[] = [null];
const rotation: any[] = [null];
trs.decompose(translation, scaling, rotation);
console.log(`Translation X: ${translation[0].x}`); // Translation X: 5
console.log(`Scale X: ${scaling[0].x.toFixed(4)}`); // Scale X: 2.0000
Constructors
| Signature | Description |
|---|---|
new Matrix4() | Creates the 4×4 identity matrix. |
new Matrix4(matrix: number[]) | Creates a matrix from a 16-element array in row-major order. |
new Matrix4(...args: number[]) | Creates a matrix from 16 individual number arguments. |
Static Properties
| Property | Type | Description |
|---|---|---|
Matrix4.identity | Matrix4 | Returns a new identity matrix. |
Properties
The 16 matrix elements are accessible as individual named properties m00, m01, m02, m03, m10, …, m33, where the first digit is the row and the second is the column. All are readable and writable.
| Property | Type | Description |
|---|---|---|
m00–m33 | number | Individual matrix elements (row-major layout). |
determinant | number (read-only) | The scalar determinant of the matrix. |
Static Methods
Matrix4.translate(v)
Creates a pure translation matrix from a Vector3, or from individual (tx, ty, tz) numbers.
static translate(v: Vector3): Matrix4
static translate(tx: number, ty?: number, tz?: number): Matrix4Examples
import { Matrix4, Vector3 } from '@aspose/3d/utilities';
const m = Matrix4.translate(new Vector3(3, 0, 0));
console.log(`m03: ${m.m03}`); // m03: 3
Matrix4.scale(v)
Creates a pure scaling matrix from a Vector3, or from individual (sx, sy, sz) numbers.
static scale(v: Vector3): Matrix4
static scale(sx: number, sy?: number, sz?: number): Matrix4Matrix4.rotate(q)
Creates a rotation matrix from a Quaternion, or from an angle (in radians) and an axis Vector3.
static rotate(q: Quaternion): Matrix4
static rotate(angle: number, axis: Vector3): Matrix4Examples
import { Matrix4, Quaternion } from '@aspose/3d/utilities';
const q = Quaternion.fromEulerAngle(0, Math.PI / 2, 0);
const m = Matrix4.rotate(q);
console.log(`Rotation matrix m00: ${m.m00.toFixed(4)}`);
// Rotation matrix m00: 0.0000
Matrix4.rotateFromEuler(rx, ry, rz)
Creates a rotation matrix from Euler angles in radians.
static rotateFromEuler(rx: number, ry?: number, rz?: number): Matrix4
static rotateFromEuler(v: Vector3): Matrix4Instance Methods
concatenate(m2)
Returns the matrix product this × m2. Use this to combine transformations (apply this first, then m2).
concatenate(m2: Matrix4): Matrix4Examples
import { Matrix4, Vector3 } from '@aspose/3d/utilities';
const translate = Matrix4.translate(new Vector3(1, 0, 0));
const scale = Matrix4.scale(new Vector3(2, 2, 2));
const combined = translate.concatenate(scale);
console.log(`m03: ${combined.m03}`); // m03: 1
inverse()
Returns the inverse of this matrix. Throws if the matrix is singular (determinant near zero).
inverse(): Matrix4transpose()
Returns the transpose of this matrix (rows become columns).
transpose(): Matrix4normalize()
Returns a new matrix with the rotation (upper-left 3×3) sub-matrix orthonormalized by dividing each row by its length. The translation column is preserved.
normalize(): Matrix4decompose(translation, scaling, rotation)
Decomposes this matrix into translation, scaling, and rotation components. Results are written into the first element of each output array.
decompose(translation: any[], scaling: any[], rotation: any[]): voidParameters
translation any[] — Output array. After the call, translation[0] is an object {x, y, z} with the translation.
scaling any[] — Output array. After the call, scaling[0] is an object {x, y, z} with the scale factors.
rotation any[] — Output array. After the call, rotation[0] is an object {w, x, y, z} with the rotation quaternion components.
Examples
import { Matrix4, Vector3, Quaternion } from '@aspose/3d/utilities';
const m = Matrix4.translate(new Vector3(0, 5, 0));
const trans: any[] = [null], scale: any[] = [null], rot: any[] = [null];
m.decompose(trans, scale, rot);
console.log(`Y: ${trans[0].y}`); // Y: 5
setTRS(translation, rotation, scale)
Sets this matrix to the product of a translation, rotation, and scale. The inputs can be plain objects with x, y, z (and w for rotation) fields.
setTRS(translation: any, rotation: any, scale: any): voidtoArray()
Returns a copy of the internal 16-element row-major array.
toArray(): number[]getItem(index) / setItem(index, value)
Element access by linear index (0–15, row-major order).
getItem(key: number): number
setItem(key: number, value: number): voidequals(other)
Returns true if all 16 elements are strictly equal.
equals(other: Matrix4): boolean