# Mat3

Class · category: Math

Source: https://github.com/playcanvas/engine/blob/b5b983982a9860d21e0c1dafb2f85f72e2c01afb/src/core/math/mat3.js#L14

A 3x3 matrix. Mat3 is commonly used to represent rotation matrices, 2D transformations or the
upper-left portion of a 4x4 matrix for transforming normals.

## Constructors

### constructor

```ts
new Mat3()
```

Create a new Mat3 instance. It is initialized to the identity matrix.

## Properties

### data

```ts
data: Float32Array<ArrayBufferLike>
```

Matrix elements in the form of a flat array.

### IDENTITY

```ts
static readonly IDENTITY: Mat3
```

A constant matrix set to the identity.

### ZERO

```ts
static readonly ZERO: Mat3
```

A constant matrix with all elements set to 0.

## Methods

### clone

```ts
clone(): Mat3
```

Creates a duplicate of the specified matrix.

**Returns** [`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md): A duplicate matrix.

**Example**

```ts
const src = new Mat3().setFromQuat(new Quat(0, 0, 0.383, 0.924));
const dst = src.clone();
console.log("The two matrices are " + (src.equals(dst) ? "equal" : "different"));
```

### copy

```ts
copy(rhs: Mat3): Mat3
```

Copies the contents of a source 3x3 matrix to a destination 3x3 matrix.

**Parameters**

- `rhs` ([`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md)): A 3x3 matrix to be copied.

**Returns** [`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md): Self for chaining.

**Example**

```ts
const src = new Mat3().setFromQuat(new Quat(0, 0, 0.383, 0.924));
const dst = new Mat3();
dst.copy(src);
console.log("The two matrices are " + (src.equals(dst) ? "equal" : "different"));
```

### equals

```ts
equals(rhs: Mat3): boolean
```

Reports whether two matrices are equal.

**Parameters**

- `rhs` ([`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md)): The other matrix.

**Returns** `boolean`: True if the matrices are equal and false otherwise.

**Example**

```ts
const a = new Mat3().setFromQuat(new Quat(0, 0, 0.383, 0.924));
const b = new Mat3();
console.log("The two matrices are " + (a.equals(b) ? "equal" : "different"));
```

### getX

```ts
getX(x?: Vec3): Vec3
```

Extracts the x-axis from the specified matrix.

**Parameters**

- `x` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): The vector to receive the x axis of the matrix.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The x-axis of the specified matrix.

**Example**

```ts
const m = new Mat3();
const xAxis = m.getX(); // Vec3(1, 0, 0) for identity matrix
```

### getY

```ts
getY(y?: Vec3): Vec3
```

Extracts the y-axis from the specified matrix.

**Parameters**

- `y` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): The vector to receive the y axis of the matrix.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The y-axis of the specified matrix.

**Example**

```ts
const m = new Mat3();
const yAxis = m.getY(); // Vec3(0, 1, 0) for identity matrix
```

### getZ

```ts
getZ(z?: Vec3): Vec3
```

Extracts the z-axis from the specified matrix.

**Parameters**

- `z` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): The vector to receive the z axis of the matrix.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The z-axis of the specified matrix.

**Example**

```ts
const m = new Mat3();
const zAxis = m.getZ(); // Vec3(0, 0, 1) for identity matrix
```

### isIdentity

```ts
isIdentity(): boolean
```

Reports whether the specified matrix is the identity matrix.

**Returns** `boolean`: True if the matrix is identity and false otherwise.

**Example**

```ts
const m = new Mat3();
console.log("The matrix is " + (m.isIdentity() ? "identity" : "not identity"));
```

### set

```ts
set(src: number[]): Mat3
```

Copies the contents of a source array[9] to a destination 3x3 matrix.

**Parameters**

- `src` (`number[]`): An array[9] to be copied.

**Returns** [`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md): Self for chaining.

**Example**

```ts
const dst = new Mat3();
dst.set([0, 1, 2, 3, 4, 5, 6, 7, 8]);
```

### setFromMat4

```ts
setFromMat4(m: Mat4): Mat3
```

Converts the specified 4x4 matrix to a Mat3.

**Parameters**

- `m` ([`Mat4`](https://api.playcanvas.com/engine/classes/Mat4.md)): The 4x4 matrix to convert.

**Returns** [`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md): Self for chaining.

**Example**

```ts
const m4 = new Mat4();
const m3 = new Mat3().setFromMat4(m4);
```

### setFromQuat

```ts
setFromQuat(r: Quat): Mat3
```

Sets this matrix to the given quaternion rotation.

**Parameters**

- `r` ([`Quat`](https://api.playcanvas.com/engine/classes/Quat.md)): A quaternion rotation.

**Returns** [`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md): Self for chaining.

**Example**

```ts
const r = new Quat(1, 2, 3, 4).normalize();

const m = new Mat3();
m.setFromQuat(r);
```

### setIdentity

```ts
setIdentity(): Mat3
```

Sets the matrix to the identity matrix.

**Returns** [`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md): Self for chaining.

**Example**

```ts
m.setIdentity();
console.log("The matrix is " + (m.isIdentity() ? "identity" : "not identity"));
```

### toString

```ts
toString(): string
```

Converts the matrix to string form.

**Returns** `string`: The matrix in string form.

**Example**

```ts
const m = new Mat3();
// Outputs [1, 0, 0, 0, 1, 0, 0, 0, 1]
console.log(m.toString());
```

### transformVector

```ts
transformVector(vec: Vec3, res?: Vec3): Vec3
```

Transforms a 3-dimensional vector by a 3x3 matrix.

**Parameters**

- `vec` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The 3-dimensional vector to be transformed.
- `res` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): An optional 3-dimensional vector to receive the result of the
  transformation.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The input vector v transformed by the current instance.

**Example**

```ts
const m = new Mat3();
const v = new Vec3(1, 2, 3);
const result = m.transformVector(v);
```

### transpose

```ts
transpose(src?: Mat3): Mat3
```

Generates the transpose of the specified 3x3 matrix.

**Parameters**

- `src` ([`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md), optional): The matrix to transpose. If not set, the matrix is transposed in-place.

**Returns** [`Mat3`](https://api.playcanvas.com/engine/classes/Mat3.md): Self for chaining.

**Example**

```ts
const m = new Mat3();

// Transpose in place
m.transpose();
```
