Skip to content

Matrix Extensions

Andrzej Kebab edited this page Feb 2, 2024 · 2 revisions

UtilityLibrary.Unity.Runtime

Deconstruct

Deconstructs a Matrix4x4 instance into its column vectors.

Matrix4x4 matrix = // initialize your matrix
Vector4 column0, column1, column2, column3;
matrix.Deconstruct(out column0, out column1, out column2, out column3);

Parameters:

  • matrix: The Matrix4x4 instance.

Deconstruct (individual elements)

Deconstructs a Matrix4x4 instance into its individual elements.

Matrix4x4 matrix = // initialize your matrix
float m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33;
matrix.Deconstruct(out m00, out m01, out m02, out m03,
                   out m10, out m11, out m12, out m13,
                   out m20, out m21, out m22, out m23,
                   out m30, out m31, out m32, out m33);

Parameters:

  • matrix: The Matrix4x4 instance.

With

Creates a new Matrix4x4 instance with the specified column vectors, or the original column vectors if not specified.

Matrix4x4 originalMatrix = // initialize your matrix
Vector4 newColumn0 = // initialize your new column vector (optional)
Vector4 newColumn1 = // initialize your new column vector (optional)
Vector4 newColumn2 = // initialize your new column vector (optional)
Vector4 newColumn3 = // initialize your new column vector (optional)

Matrix4x4 resultMatrix = originalMatrix.With(column0: newColumn0, column1: newColumn1,
                                             column2: newColumn2, column3: newColumn3);

Parameters:

  • matrix: The original Matrix4x4 instance.
  • column0: The first column vector of the new Matrix4x4. If null, the first column vector of the original Matrix4x4 is used.
  • column1: The second column vector of the new Matrix4x4. If null, the second column vector of the original Matrix4x4 is used.
  • column2: The third column vector of the new Matrix4x4. If null, the third column vector of the original Matrix4x4 is used.
  • column3: The fourth column vector of the new Matrix4x4. If null, the fourth column vector of the original Matrix4x4 is used.

Returns:

  • resultMatrix: A new Matrix4x4 instance with the specified column vectors.

With (individual elements)

Creates a new Matrix4x4 instance with the specified elements, or the original elements if not specified.

Matrix4x4 originalMatrix = // initialize your matrix
float newM00 = // initialize your new element (optional)
float newM01 = // initialize your new element (optional)
// ... repeat for other elements ...

Matrix4x4 resultMatrix = originalMatrix.With(m00: newM00, m01: newM01, m02: newM02, m03: newM03,
                                             m10: newM10, m11: newM11, m12: newM12, m13: newM13,
                                             m20: newM20, m21: newM21, m22: newM22, m23: newM23,
                                             m30: newM30, m31: newM31, m32: newM32, m33: newM33);

Parameters:

  • matrix: The original Matrix4x4 instance.
  • m00 - m33: The element at the first row and first column of the new Matrix4x4. If null, the corresponding element of the original Matrix4x4 is used.

Returns:

  • resultMatrix: A new Matrix4x4 instance with the specified elements.

Clone this wiki locally