A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using UnityEngine.Experimental.Rendering;
5
6namespace UnityEngine.Rendering
7{
8 using UnityObject = UnityEngine.Object;
9
10 /// <summary>
11 /// Set of utility functions for the Core Scriptable Render Pipeline Library related to Matrix operations
12 /// </summary>
13 public static class CoreMatrixUtils
14 {
15 /// <summary>
16 /// This function provides the equivalent of multiplying matrix parameter inOutMatrix with a translation matrix defined by the parameter translation.
17 /// The order of the equivalent multiplication is inOutMatrix * translation.
18 /// </summary>
19 /// <param name="inOutMatrix">Matrix to multiply with translation.</param>
20 /// <param name="translation">Translation component to multiply to the matrix.</param>
21 public static void MatrixTimesTranslation(ref Matrix4x4 inOutMatrix, Vector3 translation)
22 {
23 inOutMatrix.m03 += (inOutMatrix.m00 * translation.x + inOutMatrix.m01 * translation.y + inOutMatrix.m02 * translation.z);
24 inOutMatrix.m13 += (inOutMatrix.m10 * translation.x + inOutMatrix.m11 * translation.y + inOutMatrix.m12 * translation.z);
25 inOutMatrix.m23 += (inOutMatrix.m20 * translation.x + inOutMatrix.m21 * translation.y + inOutMatrix.m22 * translation.z);
26 }
27
28 /// <summary>
29 /// This function provides the equivalent of multiplying a translation matrix defined by the parameter translation with the matrix specified by the parameter inOutMatrix.
30 /// The order of the equivalent multiplication is translation * inOutMatrix.
31 /// </summary>
32 /// <param name="inOutMatrix">Matrix to multiply with translation.</param>
33 /// <param name="translation">Translation component to multiply to the matrix.</param>
34 public static void TranslationTimesMatrix(ref Matrix4x4 inOutMatrix, Vector3 translation)
35 {
36 inOutMatrix.m00 += translation.x * inOutMatrix.m30;
37 inOutMatrix.m01 += translation.x * inOutMatrix.m31;
38 inOutMatrix.m02 += translation.x * inOutMatrix.m32;
39 inOutMatrix.m03 += translation.x * inOutMatrix.m33;
40
41 inOutMatrix.m10 += translation.y * inOutMatrix.m30;
42 inOutMatrix.m11 += translation.y * inOutMatrix.m31;
43 inOutMatrix.m12 += translation.y * inOutMatrix.m32;
44 inOutMatrix.m13 += translation.y * inOutMatrix.m33;
45
46 inOutMatrix.m20 += translation.z * inOutMatrix.m30;
47 inOutMatrix.m21 += translation.z * inOutMatrix.m31;
48 inOutMatrix.m22 += translation.z * inOutMatrix.m32;
49 inOutMatrix.m23 += translation.z * inOutMatrix.m33;
50 }
51
52 /// <summary>
53 /// Multiplies a matrix with a perspective matrix. This function is faster than performing the full matrix multiplication.
54 /// The operation order is perspective * rhs.
55 /// </summary>
56 /// <param name="perspective">The perspective matrix to multiply with rhs.</param>
57 /// <param name="rhs">A matrix to be multiply to perspective.</param>
58 /// <returns>Returns the matrix that is the result of the multiplication.</returns>
59 public static Matrix4x4 MultiplyPerspectiveMatrix(Matrix4x4 perspective, Matrix4x4 rhs)
60 {
61 Matrix4x4 outMat;
62 outMat.m00 = perspective.m00 * rhs.m00;
63 outMat.m01 = perspective.m00 * rhs.m01;
64 outMat.m02 = perspective.m00 * rhs.m02;
65 outMat.m03 = perspective.m00 * rhs.m03;
66
67 outMat.m10 = perspective.m11 * rhs.m10;
68 outMat.m11 = perspective.m11 * rhs.m11;
69 outMat.m12 = perspective.m11 * rhs.m12;
70 outMat.m13 = perspective.m11 * rhs.m13;
71
72 outMat.m20 = perspective.m22 * rhs.m20 + perspective.m23 * rhs.m30;
73 outMat.m21 = perspective.m22 * rhs.m21 + perspective.m23 * rhs.m31;
74 outMat.m22 = perspective.m22 * rhs.m22 + perspective.m23 * rhs.m32;
75 outMat.m23 = perspective.m22 * rhs.m23 + perspective.m23 * rhs.m33;
76
77 outMat.m30 = -rhs.m20;
78 outMat.m31 = -rhs.m21;
79 outMat.m32 = -rhs.m22;
80 outMat.m33 = -rhs.m23;
81
82 return outMat;
83 }
84
85 // An orthographic projection is centered if (right+left) == 0 and (top+bottom) == 0
86 private static Matrix4x4 MultiplyOrthoMatrixCentered(Matrix4x4 ortho, Matrix4x4 rhs)
87 {
88 Matrix4x4 outMat;
89 outMat.m00 = ortho.m00 * rhs.m00;
90 outMat.m01 = ortho.m00 * rhs.m01;
91 outMat.m02 = ortho.m00 * rhs.m02;
92 outMat.m03 = ortho.m00 * rhs.m03;
93
94 outMat.m10 = ortho.m11 * rhs.m10;
95 outMat.m11 = ortho.m11 * rhs.m11;
96 outMat.m12 = ortho.m11 * rhs.m12;
97 outMat.m13 = ortho.m11 * rhs.m13;
98
99 outMat.m20 = ortho.m22 * rhs.m20 + ortho.m23 * rhs.m30;
100 outMat.m21 = ortho.m22 * rhs.m21 + ortho.m23 * rhs.m31;
101 outMat.m22 = ortho.m22 * rhs.m22 + ortho.m23 * rhs.m32;
102 outMat.m23 = ortho.m22 * rhs.m23 + ortho.m23 * rhs.m33;
103
104 outMat.m30 = rhs.m20;
105 outMat.m31 = rhs.m21;
106 outMat.m32 = rhs.m22;
107 outMat.m33 = rhs.m23;
108
109 return outMat;
110 }
111
112 // General case has m03 and m13 != 0
113 private static Matrix4x4 MultiplyGenericOrthoMatrix(Matrix4x4 ortho, Matrix4x4 rhs)
114 {
115 Matrix4x4 outMat;
116 outMat.m00 = ortho.m00 * rhs.m00 + ortho.m03 * rhs.m30;
117 outMat.m01 = ortho.m00 * rhs.m01 + ortho.m03 * rhs.m31;
118 outMat.m02 = ortho.m00 * rhs.m02 + ortho.m03 * rhs.m32;
119 outMat.m03 = ortho.m00 * rhs.m03 + ortho.m03 * rhs.m33;
120
121 outMat.m10 = ortho.m11 * rhs.m10 + ortho.m13 * rhs.m30;
122 outMat.m11 = ortho.m11 * rhs.m11 + ortho.m13 * rhs.m31;
123 outMat.m12 = ortho.m11 * rhs.m12 + ortho.m13 * rhs.m32;
124 outMat.m13 = ortho.m11 * rhs.m13 + ortho.m13 * rhs.m33;
125
126 outMat.m20 = ortho.m22 * rhs.m20 + ortho.m23 * rhs.m30;
127 outMat.m21 = ortho.m22 * rhs.m21 + ortho.m23 * rhs.m31;
128 outMat.m22 = ortho.m22 * rhs.m22 + ortho.m23 * rhs.m32;
129 outMat.m23 = ortho.m22 * rhs.m23 + ortho.m23 * rhs.m33;
130
131 outMat.m30 = rhs.m20;
132 outMat.m31 = rhs.m21;
133 outMat.m32 = rhs.m22;
134 outMat.m33 = rhs.m23;
135 return outMat;
136 }
137
138 /// <summary>
139 /// Multiplies a matrix with an orthographic matrix. This function is faster than performing the full matrix multiplication.
140 /// The operation order is ortho * rhs.
141 /// </summary>
142 /// <param name="ortho">The ortho matrix to multiply with rhs.</param>
143 /// <param name="rhs">A matrix to be multiply to perspective.</param>
144 /// <param name="centered">If true, it means that right and left are equivalently distant from center and similarly top/bottom are equivalently distant from center.</param>
145 /// <returns>Returns the matrix that is the result of the multiplication.</returns>
146 public static Matrix4x4 MultiplyOrthoMatrix(Matrix4x4 ortho, Matrix4x4 rhs, bool centered)
147 {
148 return centered ? MultiplyGenericOrthoMatrix(ortho, rhs) : MultiplyOrthoMatrixCentered(ortho, rhs);
149 }
150
151 /// <summary>
152 /// Multiplies a matrix with a projection matrix. This function is faster than performing the full matrix multiplication.
153 /// The operation order is projMatrix * rhs.
154 /// </summary>
155 /// <param name="projMatrix">The projection matrix to multiply with rhs.</param>
156 /// <param name="rhs">A matrix to be multiply to perspective.</param>
157 /// <param name="orthoCentered">If true, the projection matrix is a centered ( right+left == top+bottom == 0) orthographic projection, otherwise it is a perspective matrix..</param>
158 /// <returns>Returns the matrix that is the result of the multiplication.</returns>
159 public static Matrix4x4 MultiplyProjectionMatrix(Matrix4x4 projMatrix, Matrix4x4 rhs, bool orthoCentered)
160 {
161 return orthoCentered
162 ? MultiplyOrthoMatrixCentered(projMatrix, rhs)
163 : MultiplyPerspectiveMatrix(projMatrix, rhs);
164 }
165 }
166}