# base/matrix4 (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/matrix4/

4x4 transform matrix. (Column-major, can be uploaded as is with WebGL uniformMatrix4fv)

## Matrix4 extends Object

4x4 transform matrix. (Column-major, can be uploaded as is with WebGL uniformMatrix4fv)
- Column-major array index: elements[column * 4 + row]
- Point transform: transformed = matrix x (x, y, z, 1)

Example: Transform a point with a model matrix and back

```js
import { Matrix4 } from "./build/vanilla.js";

const model = Matrix4.createTranslation(10, 0, 0);
model.rotateY(Math.PI / 2);
model.scale(2, 2, 2);
const point = model.transformPoint(1, 0, 0);
console.log(point.x, point.y, point.z, point.w);
const inverse = Matrix4.createInverse(model);
const restored = inverse.transformPoint(point.x, point.y, point.z);
console.log(restored.x, restored.y, restored.z);
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `setIdentity()` (method, since 0.3.0): Resets to the identity matrix.
- `copyFrom(other)` (method, since 0.3.0): Copies from another matrix.
  - `other` Matrix4: Matrix to copy from.
- `clone()` (method, since 0.3.0): Clone.
  - returns this: A new matrix with the same elements.
- `multiply(other)` (method, since 0.3.0): Accumulates by multiplying another matrix on the right. (this = this x other)
  - `other` Matrix4: Matrix to multiply on the right.
- `translate(x, y, z)` (method, since 0.3.0): Accumulates a translation. (Right multiplication in local space)
  - `x` number: X translation.
  - `y` number: Y translation.
  - `z` number: Z translation.
- `rotateX(radian)` (method, since 0.3.0): Accumulates an X-axis rotation. (Right multiplication in local space)
  - `radian` number: Rotation angle. (radians)
- `rotateY(radian)` (method, since 0.3.0): Accumulates a Y-axis rotation. (Right multiplication in local space)
  - `radian` number: Rotation angle. (radians)
- `rotateZ(radian)` (method, since 0.3.0): Accumulates a Z-axis rotation. (Right multiplication in local space)
  - `radian` number: Rotation angle. (radians)
- `scale(x, y, z)` (method, since 0.3.0): Accumulates a scale. (Right multiplication in local space)
  - `x` number: X scale.
  - `y` number: Y scale.
  - `z` number: Z scale.
- `transformPoint(x, y, z)` (method, since 0.3.0): Transforms a point. (Includes the w component, perspective division is done by the caller)
  - `x` number: X coordinate of the point.
  - `y` number: Y coordinate of the point.
  - `z` number: Z coordinate of the point.
  - returns { x: number, y: number, z: number, w: number }: Transformed homogeneous coordinates. (not divided by w)
- `transformDirection(x, y, z)` (method, since 0.3.0): Transforms a direction. (Ignores the translation, applies only rotation/scale)
  - `x` number: X component of the direction.
  - `y` number: Y component of the direction.
  - `z` number: Z component of the direction.
  - returns { x: number, y: number, z: number }: Transformed direction. (not normalized)
- `getElements()` (method, since 0.3.0): Returns the column-major element array.
  - returns Float32Array: The 16 column-major elements. (the internal array, not a copy)
- `writeToArray(array)` (method, since 0.4.0): Writes as a shader mat4 (column-major) array. (16 slots)
  - `array` Float32Array | number[]: Array to write into. (16 slots from index 0)
- `writeToSlice(array, offset)` (method, since 0.4.0): Writes 16 values starting at offset in a large array. (When packing several matrices back to back, like a joint matrix array)
  - `array` Float32Array | number[]: Array to write into.
  - `offset` number: Index to start writing at.
- `static createIdentity()` (static, since 0.3.0): Creates an identity matrix. (static)
  - returns Matrix4: Identity matrix.
- `static createTranslation(x, y, z)` (static, since 0.3.0): Creates a translation matrix. (static)
  - `x` number: X translation.
  - `y` number: Y translation.
  - `z` number: Z translation.
  - returns Matrix4: Translation matrix.
- `static createRotationX(radian)` (static, since 0.3.0): Creates an X-axis rotation matrix. (static)
  - `radian` number: Rotation angle. (radians)
  - returns Matrix4: X axis rotation matrix.
- `static createRotationY(radian)` (static, since 0.3.0): Creates a Y-axis rotation matrix. (static)
  - `radian` number: Rotation angle. (radians)
  - returns Matrix4: Y axis rotation matrix.
- `static createRotationZ(radian)` (static, since 0.3.0): Creates a Z-axis rotation matrix. (static)
  - `radian` number: Rotation angle. (radians)
  - returns Matrix4: Z axis rotation matrix.
- `static createRotationFromQuaternion(quaternion)` (static, since 0.3.0): Creates a quaternion rotation matrix. (static)
  - `quaternion` { x: number, y: number, z: number, w: number }: Rotation quaternion. (a Quaternion or an object with x, y, z, w, must have length 1)
  - returns Matrix4: Rotation matrix.
- `static createScale(x, y, z)` (static, since 0.3.0): Creates a scale matrix. (static)
  - `x` number: X scale.
  - `y` number: Y scale.
  - `z` number: Z scale.
  - returns Matrix4: Scale matrix.
- `static createPerspective(fieldOfViewRadian, aspectRatio, nearDistance, farDistance)` (static, since 0.3.0): Creates a perspective projection matrix. (static)
  - `fieldOfViewRadian` number: Vertical field of view. (radians)
  - `aspectRatio` number: Width divided by height.
  - `nearDistance` number: Distance to the near clipping plane. (must be greater than 0)
  - `farDistance` number: Distance to the far clipping plane.
  - returns Matrix4: Perspective projection matrix. (the camera looks down -Z, depth is mapped to -1 ~ 1)

  Example: Build a view projection matrix for a perspective camera

  ```js
  import { Matrix4, Vector3 } from "./build/vanilla.js";
  
  const projection = Matrix4.createPerspective(Math.PI / 3, 16 / 9, 0.1, 100);
  const eyePosition = Vector3.create(0, 2, 5);
  const targetPosition = Vector3.zero();
  const upDirection = Vector3.up();
  const view = Matrix4.createLookAt(eyePosition, targetPosition, upDirection);
  const viewProjection = projection.clone();
  viewProjection.multiply(view);
  const clip = viewProjection.transformPoint(0, 0, 0);
  console.log(clip.x / clip.w, clip.y / clip.w, clip.z / clip.w);
  ```

- `static createOrthographic(left, right, bottom, top, nearDistance, farDistance)` (static, since 0.3.0): Creates an orthographic projection matrix. (static)
  - `left` number: Left bound.
  - `right` number: Right bound.
  - `bottom` number: Bottom bound.
  - `top` number: Top bound.
  - `nearDistance` number: Distance to the near clipping plane.
  - `farDistance` number: Distance to the far clipping plane.
  - returns Matrix4: Orthographic projection matrix. (maps the bounding box to -1 ~ 1)
- `static createLookAt(eyePosition, targetPosition, upDirection)` (static, since 0.3.0): Creates a look-at matrix. (static, a view matrix looking at a target from a given position)
  - `eyePosition` Vector3: Camera position.
  - `targetPosition` Vector3: Point to look at.
  - `upDirection` Vector3: Up direction. (must not be parallel to the viewing direction)
  - returns Matrix4: View matrix. (right-handed, the camera looks down -Z)
- `static createInverse(matrix)` (static, since 0.3.0): Creates an inverse matrix. (static, general 4x4 cofactor expansion, identity matrix if non-invertible)
  - `matrix` Matrix4: Matrix to invert.
  - returns Matrix4: Inverse matrix. (identity if the matrix is not invertible)
