Math Matrix Functions
The scope of applications for UnigineScript is limited to implementing materials-related logic (material expressions, scriptable materials, brush materials). Do not use UnigineScript as a language for application logic, please consider C#/C++ instead, as these APIs are the preferred ones. Availability of new Engine features in UnigineScript (beyond its scope of applications) is not guaranteed, as the current level of support assumes only fixing critical issues.
Math Class
Members
mat4 cubeTransform ( int face ) #
Returns cube viewing matrix for the given cube face.Arguments
- int face - Cube face number.
Return value
Cube viewing matrix.mat4 frustum ( float left, float right, float bottom, float top, float znear, float zfar ) #
Returns perspective projection matrix:2.0 * znear / (right - left) | 0.0 | (right + left) / (right - left) | 0.0 |
0.0 | 2.0 * znear / (top - bottom) | (top + bottom) / (top - bottom) | 0.0 |
0.0 | 0.0 | -(zfar + znear) / (zfar - znear) | -2.0 * zfar * znear / (zfar - znear) |
0.0 | 0.0 | -1.0 | 0.0 |
Coordinates of top, left, right, bottom are set relatively to center point of the znear plane.
There are two different points (A and B) on the picture above. Since the top, left, right, bottom are coordinates relatively to the center point of the znear plane, coordinates of the A point should be A(left, bottom, znear). Coordinates of the B point are B(k * left, k * bottom, zfar), where k = zfar/znear.
Arguments
- float left - Left coordinate of the near clipping plane relatively to the center.
- float right - Right coordinate of the near clipping plane relatively to the center.
- float bottom - Bottom coordinate of the near clipping plane relatively to the center.
- float top - Top coordinate of the near clipping plane relatively to the center.
- float znear - Distance to the near depth clipping plane.
- float zfar - Distance to the farther depth clipping plane.
Return value
Perspective projection matrix.Variable inverse ( Variable matrix ) #
Returns inverse of a matrix. Supports mat3, mat4 and dmat4 arguments. The inverse of a matrix is a matrix that if multiplied by the original would result in identity matrix: AA -1 = A -1A = I.Arguments
- Variable matrix - Matrix to be inverted (mat3, mat4 or dmat4).
Return value
Inverse of the matrix.quat inverse ( quat q ) #
Returns inverse of a quaternion. The inverse of a quaternion is a quaternion that if multiplied by the original would result in identity matrix: qq -1 = 1. If q = a + bi + cj + dk, then q-1 = a - bi - cj - dkArguments
- quat q - Quaternion to be inverted.
Return value
Inverse of the quaternion.Variable inverse4 ( mat4 matrix ) #
Inverts a matrix that consists of a 3×4 sub-matrix (upper left) and a translation vector. The last row of the matrix is ignored. Compared to the inverse() function, this one is a bit faster and, which is more important, more stable. A matrix suitable for such inversion looks like this:m00 | m10 | m20 | m30 |
m01 | m11 | m21 | m31 |
m02 | m12 | m22 | m32 |
0 | 0 | 0 | 1 |
Arguments
- mat4 matrix - Matrix to be inverted.
Return value
Inverse of the matrix.Variable lookAt ( Variable position, Variable target, vec3 up, int axis = AXIS_NZ ) #
Returns the viewing matrix for the given eye point, target point, up and forward direction vectors. The forward direction vector is pointed along the specified axis. By default, it is negative Z.Arguments
- Variable position - Position coordinates of the eye point (vec3 or dvec3).
- Variable target - Position coordinates of the target point (vec3 or dvec3).
- vec3 up - Direction of the up vector.
- int axis - Axis along which the forward direction vector is pointed. The default is negative Z.
Return value
Viewing matrix:- mat4, if the first input argument is vec3
- dmat4 if the first argument is dvec3
Variable obliqueProjection ( mat4 projection, vec4 plane ) #
Returns the oblique projection matrix.Arguments
Return value
Oblique projection matrix.Variable orthoTangent ( vec3 tangent, vec3 binormal, vec3 normal ) #
Calculates the tangent space basis basing on the 3 given ortho basis vectors.The w component will contain the sign of the binormal.
Arguments
Return value
Tangent space basis.Examples
The following example demonstrates how to convert the mesh tangent vector to the normal and binormal vectors. The mesh tangent vector is calculated via the orthoTangent() function.
vec3 t = vec3(1.0f,0.0f,0.0f); // the tangent basis: tangent vector
vec3 b = vec3(0.0f,1.0f,0.0f); // binormal vector
vec3 n = vec3(0.0f,0.0f,1.0f); // normal vector
vec4 tangent = orthoTangent(t,b,n); // this is the mesh compressed tangent vector
quat compact_tangent_quat = quat(tangent); // convert vec4 to quat
log.message("b: %s\n",typeinfo(compact_tangent_quat.binormal)); // get the col1 (binormal) with w normalization
log.message("n: %s\n\n",typeinfo(compact_tangent_quat.normal)); // get the col2 (normal) with w normalization
// manually normalize the w component of the quaternion
// and convert the quaternion into the matrix
mat4 compact_tangent_mat = mat4(normalize3(compact_tangent_quat)); // when the w component of the quaternion is normalized, the sign of binormal will be lost for the matrix
// get the col1 and col2 of the matrix
log.message("b: %s\n",typeinfo(compact_tangent_mat.binormal * tangent.w)); // you should multiply the binormal by the tangent w component to restore the sign
log.message("n: %s\n",typeinfo(compact_tangent_mat.normal));
Variable orthoTangent ( vec4 tangent, vec3 normal ) #
Calculates the tangent space basis basing on the given ortho basis vectors.The w component will contain the sign of the binormal.
Arguments
- vec4 tangent - Tangent vector: the first 3 components are coordinates of the tangent vector, and the fourth w component is the sign of the binormal.
- vec3 normal - Normal vector.
Return value
Tangent space basis.mat4 ortho ( float left, float right, float bottom, float top, float znear, float zfar ) #
Returns parallel projection matrix:2.0 / (right - left) | 0.0 | 0.0 | -(right + left) / (right - left) |
0.0 | 2.0 / (top - bottom) | 0.0 | -(top + bottom) / (top - bottom) |
0.0 | 0.0 | -2.0 / (zfar - znear) | -(zfar + znear) / (zfar - znear) |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- float left - Left vertical clipping plane.
- float right - Right vertical clipping plane.
- float bottom - Bottom horizontal clipping plane.
- float top - Top horizontal clipping plane.
- float znear - Nearest depth clipping plane.
- float zfar - Farther depth clipping plane.
Return value
Parallel projection matrix.Variable orthonormalize ( Variable v ) #
Orthonormalizes a matrix.Arguments
Return value
Orthonormal matrix (either mat4 or dmat4 depending on the input argument type).mat4 perspective ( float fov, float aspect, float znear, float zfar ) #
Returns perspective projection matrix.Arguments
- float fov - Field of view angle.
- float aspect - Aspect ratio. The aspect ratio is the ratio of width to height.
- float znear - Nearest depth clipping plane.
- float zfar - Farther depth clipping plane.
Return value
Perspective projection matrix.Variable reflect ( Variable plane ) #
Returns the reflection matrix.Arguments
- Variable plane - Reflection plane.
Return value
Reflection matrix.Variable reflect ( Variable v0, Variable v1 ) #
Reflects a three-component vector about a plane with the specified normal vector. (either vec3 or dvec3).Arguments
- Variable v0 - Vector to be reflected (vec3 or dvec3)
- Variable v1 - Normal vector to the reflection plane (of the same type as v0 vector).
Return value
Reflected vector.mat4 rotateX ( float angle ) #
Returns rotation matrix for the given angle around X axis:1.0 | 0.0 | 0.0 | 0.0 |
0.0 | cos | -sin | 0.0 |
0.0 | sin | cos | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- float angle - Rotation angle, in degrees.
Return value
Rotation matrix.mat4 rotateY ( float angle ) #
Returns rotation matrix for the given angle around Y axis:cos | 0.0 | sin | 0.0 |
0.0 | 1.0 | 0.0 | 0.0 |
-sin | 0.0 | cos | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- float angle - Rotation angle, in degrees.
Return value
Rotation matrix.mat4 rotateZ ( float angle ) #
Returns rotation matrix for the given angle around Z axis:cos | -sin | 0.0 | 0.0 |
sin | cos | 0.0 | 0.0 |
0.0 | 0.0 | 1.0 | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- float angle - Rotation angle, in degrees.
Return value
Rotation matrix.mat4 rotate ( float x, float y, float z, float angle ) #
Returns rotation matrix for the given angle around the given axis (X, Y, Z).Arguments
- float x - X component of rotation axis.
- float y - Y component of rotation axis.
- float z - Z component of rotation axis.
- float angle - Rotation angle, in degrees.
Return value
Rotation matrix.mat4 rotate ( vec3 axis, float angle ) #
Returns rotation matrix for the given angle around the given axis (X, Y, Z).Arguments
- vec3 axis - Rotation axis. This vector does not have to be normalized.
- float angle - Rotation angle, in degrees.
Return value
Rotation matrix.Variable rotateTowards ( Variable source, Variable target, float max_angle ) #
Rotates the source vector towards the target vector by an angular step of max_angle (note, that the rotation will not overshoot).Arguments
- Variable source - Source vector/quaternion. Can be of the following types:
- Variable target - Target vector/quaternion. Can be of the following types:
- float max_angle - Angular step, in degrees. If a negative value is specified, 0 will be used instead.
Return value
Resulting vector/quaternion. The type of return value depends on the argument type:Variable rotation ( Variable v ) #
Returns the rotation matrix of the input source, supports quat, mat4 and dmat4 arguments. For more information see Matrix Transformations.Arguments
- Variable v - Source, can be of quat, mat4 or dmat4 type.
Return value
Rotation matrix.mat4 scale ( float x, float y, float z ) #
Returns scaling matrix for the specified scaling vector (X, Y, Z):X | 0.0 | 0.0 | 0.0 |
0.0 | Y | 0.0 | 0.0 |
0.0 | 0.0 | Z | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- float x - X component of the scaling vector.
- float y - Y component of the scaling vector.
- float z - Z component of the scaling vector.
Return value
Scaling matrix.mat4 scale ( vec3 vector ) #
Returns scaling matrix for the specified scaling vector (X, Y, Z):X | 0.0 | 0.0 | 0.0 |
0.0 | Y | 0.0 | 0.0 |
0.0 | 0.0 | Z | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- vec3 vector - Scaling vector.
Return value
Scaling matrix.Variable setTo ( Variable position, Variable target, vec3 up, int axis = AXIS_NZ ) #
Returns the transformation matrix, which puts an object to the specified position and sets it orientation to the specified target point. At that, the forward direction vector is pointed along the specified axis. By default, it is negative Z.Arguments
- Variable position - Position of the eye point (vec3 or dvec3).
- Variable target - Position of the target point (vec3 or dvec3).
- vec3 up - Direction of the up vector.
- int axis - Axis along which the forward direction vector is pointed. The default is negative Z.
Return value
Transformation matrix:- mat4, if the first input argument is vec3
- dmat4 if the first argument is dvec3
Variable symmetryProjection ( mat4 projection ) #
Returns the symmetric projection matrix.Arguments
- mat4 projection - Projection matrix.
Return value
Symmetric projection matrix.Variable translate ( Variable v ) #
Returns translation matrix:1.0 | 0.0 | 0.0 | X |
0.0 | 1.0 | 0.0 | Y |
0.0 | 0.0 | 1.0 | Z |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- Variable v - Translation vector (either vec3 or dvec3).
Return value
Translation matrix:- mat4, if the input argument is vec3
- dmat4 if the argument is dvec3
Variable translate ( Variable x, Variable y, Variable z ) #
Returns the translation matrix:1.0 | 0.0 | 0.0 | X |
0.0 | 1.0 | 0.0 | Y |
0.0 | 0.0 | 1.0 | Z |
0.0 | 0.0 | 0.0 | 1.0 |
Arguments
- Variable x - X component of the translation vector (int, float or double).
- Variable y - Y component of the translation vector (int, float or double).
- Variable z - Z component of the translation vector (int, float or double).
Return value
Translation matrix:- mat4, if the first input argument is int or float
- dmat4, if the first argument is double
mat4 transpose ( mat4 matrix ) #
Returns the transpose matrix of the input matrix:m00 | m10 | m20 | m30 |
m01 | m11 | m21 | m31 |
m02 | m12 | m22 | m32 |
m03 | m13 | m23 | m33 |
Arguments
- mat4 matrix - Argument.
Return value
Transposed matrix.Variable transpose3 ( mat4 matrix ) #
Transposes the upper left 3×3 sub-matrix of a matrix.Arguments
- mat4 matrix - Matrix, a part of which will be transposed.
Return value
Matrix, in which the upper left 3×3 sub-matrix is transposed.Last update:
2021-12-13
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)