Wanzyee Studio
MatrixUtility

Include some operations or properties for TRS UnityEngine.Matrix4x4. More...

Static Public Member Functions

static Matrix4x4 Create (Vector3 position, Vector3 scale, Vector3 axisX, Vector3 axisY, Vector3 axisZ)
 Create a TRS matrix with position, abs scale and normalized axes. More...
 
static Matrix4x4 Create (Vector3 position, Vector3 axisX, Vector3 axisY, Vector3 axisZ)
 Create a TRS matrix, directly fill up the columns with position and axes. More...
 
static Matrix4x4 Convert (ref Matrix4x4 matrix, ref Matrix4x4 from, ref Matrix4x4 to, bool scale=true)
 Get converted matrix from a parent matrix to another. More...
 
static Matrix4x4 LocalToWorld (ref Matrix4x4 matrix, ref Matrix4x4 parent, bool scale=true)
 Get converted matrix from local in parent to world space. More...
 
static Matrix4x4 WorldToLocal (ref Matrix4x4 matrix, ref Matrix4x4 parent, bool scale=true)
 Get converted matrix from world to local in parent space. More...
 
static float GetDeterminant (ref Matrix4x4 matrix)
 Get the determinant of the matrix. More...
 
static Vector3 GetAxisX (ref Matrix4x4 matrix)
 Get the X axis from TRS matrix. More...
 
static void SetAxisX (ref Matrix4x4 matrix, Vector3 value)
 Set the X axis to TRS matrix. More...
 
static Vector3 GetAxisY (ref Matrix4x4 matrix)
 Get the Y axis from TRS matrix. More...
 
static void SetAxisY (ref Matrix4x4 matrix, Vector3 value)
 Set the Y axis to TRS matrix. More...
 
static Vector3 GetAxisZ (ref Matrix4x4 matrix)
 Get the Z axis from TRS matrix. More...
 
static void SetAxisZ (ref Matrix4x4 matrix, Vector3 value)
 Set the Z axis to TRS matrix. More...
 
static Vector3 GetPosition (ref Matrix4x4 matrix)
 Get the position from TRS matrix. More...
 
static void SetPosition (ref Matrix4x4 matrix, Vector3 value)
 Set the position to TRS matrix. More...
 
static Quaternion GetRotation (ref Matrix4x4 matrix)
 Get the rotation from TRS matrix. More...
 
static Quaternion GetRotation (ref Matrix4x4 matrix, Vector3 scale)
 Get the rotation from TRS matrix. More...
 
static void SetRotation (ref Matrix4x4 matrix, Quaternion value)
 Set the rotation to TRS matrix. More...
 
static Vector3 GetScale (ref Matrix4x4 matrix)
 Get the scale from TRS matrix. More...
 
static Vector3 GetScale (ref Matrix4x4 matrix, Quaternion rotation)
 Get the scale from TRS matrix. More...
 
static void SetScale (ref Matrix4x4 matrix, Vector3 value)
 Set the scale from TRS matrix. More...
 
static bool IsSkew (ref Matrix4x4 matrix, float approx=1e-6f)
 Determine if the specified TRS matrix skewed. More...
 
static bool IsSkew (Vector3 axisX, Vector3 axisY, Vector3 axisZ, float approx=1e-6f)
 Determine if the matrix skewed by axes. More...
 

Detailed Description

Include some operations or properties for TRS UnityEngine.Matrix4x4.

Basically use ref to reduce lots of data copying.

Member Function Documentation

static Matrix4x4 Create ( Vector3  position,
Vector3  scale,
Vector3  axisX,
Vector3  axisY,
Vector3  axisZ 
)
static

Create a TRS matrix with position, abs scale and normalized axes.

A normalized axis multiplies the scale as length to fill up the matrix. Different from Matrix4x4.TRS(), using axes to include skew.

Parameters
positionPosition.
scaleScale.
axisXX-Axis.
axisYY-Axis.
axisZZ-Axis.
Returns
The matrix.
static Matrix4x4 Create ( Vector3  position,
Vector3  axisX,
Vector3  axisY,
Vector3  axisZ 
)
static

Create a TRS matrix, directly fill up the columns with position and axes.

An axis is a segment with direction and length. Different from Matrix4x4.TRS(), using axes to include skew.

Parameters
positionPosition.
axisXX-Axis.
axisYY-Axis.
axisZZ-Axis.
Returns
The matrix.
static Matrix4x4 Convert ( ref Matrix4x4  matrix,
ref Matrix4x4  from,
ref Matrix4x4  to,
bool  scale = true 
)
static

Get converted matrix from a parent matrix to another.

If set scale false, treat the parent space with scale one. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
fromParent from.
toParent to.
scaleIf set to true scale.
Returns
The matrix.
static Matrix4x4 LocalToWorld ( ref Matrix4x4  matrix,
ref Matrix4x4  parent,
bool  scale = true 
)
static

Get converted matrix from local in parent to world space.

If set scale false, treat the parent space with scale one. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
parentParent from.
scaleIf set to true scale.
Returns
The matrix.
static Matrix4x4 WorldToLocal ( ref Matrix4x4  matrix,
ref Matrix4x4  parent,
bool  scale = true 
)
static

Get converted matrix from world to local in parent space.

If set scale false, treat the parent space with scale one. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
parentParent to.
scaleIf set to true scale.
Returns
The matrix.
static float GetDeterminant ( ref Matrix4x4  matrix)
static

Get the determinant of the matrix.

This will NOT modify the referred matrix.

Parameters
matrixMatrix.
Returns
The determinant.
static Vector3 GetAxisX ( ref Matrix4x4  matrix)
static

Get the X axis from TRS matrix.

The axis vector means direction and magnitude. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
Returns
The axis x.
static void SetAxisX ( ref Matrix4x4  matrix,
Vector3  value 
)
static

Set the X axis to TRS matrix.

The axis vector means direction and magnitude. This WILL modify the referred matrix.

Parameters
matrixMatrix.
valueValue.
static Vector3 GetAxisY ( ref Matrix4x4  matrix)
static

Get the Y axis from TRS matrix.

The axis vector means direction and magnitude. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
Returns
The axis y.
static void SetAxisY ( ref Matrix4x4  matrix,
Vector3  value 
)
static

Set the Y axis to TRS matrix.

The axis vector means direction and magnitude. This WILL modify the referred matrix.

Parameters
matrixMatrix.
valueValue.
static Vector3 GetAxisZ ( ref Matrix4x4  matrix)
static

Get the Z axis from TRS matrix.

The axis vector means direction and magnitude. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
Returns
The axis z.
static void SetAxisZ ( ref Matrix4x4  matrix,
Vector3  value 
)
static

Set the Z axis to TRS matrix.

The axis vector means direction and magnitude. This WILL modify the referred matrix.

Parameters
matrixMatrix.
valueValue.
static Vector3 GetPosition ( ref Matrix4x4  matrix)
static

Get the position from TRS matrix.

This will NOT modify the referred matrix.

Parameters
matrixMatrix.
Returns
The position.
static void SetPosition ( ref Matrix4x4  matrix,
Vector3  value 
)
static

Set the position to TRS matrix.

This WILL modify the referred matrix.

Parameters
matrixMatrix.
valueValue.
static Quaternion GetRotation ( ref Matrix4x4  matrix)
static

Get the rotation from TRS matrix.

Note first, UnityEngine.Quaternion can't represent the correct axes if matrix skewed. Second, the directions might be incorrect if matrix made from negative or zero scale. The other, the sign might differ from the rotation passed to Matrix4x4.TRS(). If you wanna compare the rotation, for use, you could compare eulerAngles instead. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
Returns
The rotation.
static Quaternion GetRotation ( ref Matrix4x4  matrix,
Vector3  scale 
)
static

Get the rotation from TRS matrix.

Correct direction with signed scale of matrix to handle with negative value. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
scaleSigned scale.
Returns
The rotation.
static void SetRotation ( ref Matrix4x4  matrix,
Quaternion  value 
)
static

Set the rotation to TRS matrix.

Rotates all axes in once to make new z-axis face to new rotation. This WILL modify the referred matrix.

Parameters
matrixMatrix.
valueValue.
static Vector3 GetScale ( ref Matrix4x4  matrix)
static

Get the scale from TRS matrix.

This method can only calculate scale into positive value. It might not the same as the scale passed to Matrix4x4.TRS() if with negative. Since it's able to generate the same matrix by different scale and rotation pair. There's not enough information to know the correct signs of scale components. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
Returns
The scale.
static Vector3 GetScale ( ref Matrix4x4  matrix,
Quaternion  rotation 
)
static

Get the scale from TRS matrix.

This corrects the sign with known rotation of the matrix. If the matrix is made from UnityEngine.Transform, the return is lossyScale. Calculating might cause little tolerance, it's better to compare by approx. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
rotationRotation.
Returns
The scale.
static void SetScale ( ref Matrix4x4  matrix,
Vector3  value 
)
static

Set the scale from TRS matrix.

Modify the length of the axes, reverse if assigning negative. Nothing happens if an axis zero. This WILL modify the referred matrix.

Parameters
matrixMatrix.
valueValue.
static bool IsSkew ( ref Matrix4x4  matrix,
float  approx = 1e-6f 
)
static

Determine if the specified TRS matrix skewed.

Return false if all the axes pairs are perpendicular or zero. This will NOT modify the referred matrix.

Parameters
matrixMatrix.
approxApprox to judge.
Returns
true if is skewed matrix; otherwise, false.
static bool IsSkew ( Vector3  axisX,
Vector3  axisY,
Vector3  axisZ,
float  approx = 1e-6f 
)
static

Determine if the matrix skewed by axes.

Return true if anyone of pairs axes includes non-right angle, means skewed. Otherwise, the vectors are perpendicular or zero.

Parameters
axisXX-Axis.
axisYY-Axis.
axisZZ-Axis.
approxApprox to judge.
Returns
true if is skewed axes; otherwise, false.