A game about forced loneliness, made by TACStudios
1using System;
2using System.Runtime.CompilerServices;
3using Unity.IL2CPP.CompilerServices;
4using static Unity.Mathematics.math;
5
6namespace Unity.Mathematics
7{
8 /// <summary>
9 /// A rigid transformation type.
10 /// </summary>
11 [Il2CppEagerStaticClassConstruction]
12 [Serializable]
13 public struct RigidTransform
14 {
15 /// <summary>
16 /// The rotation part of the rigid transformation.
17 /// </summary>
18 public quaternion rot;
19
20 /// <summary>
21 /// The translation part of the rigid transformation.
22 /// </summary>
23 public float3 pos;
24
25 /// <summary>A RigidTransform representing the identity transform.</summary>
26 public static readonly RigidTransform identity = new RigidTransform(new quaternion(0.0f, 0.0f, 0.0f, 1.0f), new float3(0.0f, 0.0f, 0.0f));
27
28 /// <summary>Constructs a RigidTransform from a rotation represented by a unit quaternion and a translation represented by a float3 vector.</summary>
29 /// <param name="rotation">The quaternion rotation.</param>
30 /// <param name="translation">The translation vector.</param>
31 [MethodImpl(MethodImplOptions.AggressiveInlining)]
32 public RigidTransform(quaternion rotation, float3 translation)
33 {
34 this.rot = rotation;
35 this.pos = translation;
36 }
37
38 /// <summary>Constructs a RigidTransform from a rotation represented by a float3x3 matrix and a translation represented by a float3 vector.</summary>
39 /// <param name="rotation">The float3x3 rotation matrix.</param>
40 /// <param name="translation">The translation vector.</param>
41 [MethodImpl(MethodImplOptions.AggressiveInlining)]
42 public RigidTransform(float3x3 rotation, float3 translation)
43 {
44 this.rot = new quaternion(rotation);
45 this.pos = translation;
46 }
47
48 /// <summary>Constructs a RigidTransform from a float4x4. Assumes the matrix is orthonormal.</summary>
49 /// <param name="transform">The float4x4 transformation matrix, must be orthonormal.</param>
50 [MethodImpl(MethodImplOptions.AggressiveInlining)]
51 public RigidTransform(float4x4 transform)
52 {
53 this.rot = new quaternion(transform);
54 this.pos = transform.c3.xyz;
55 }
56
57
58 /// <summary>
59 /// Returns a RigidTransform representing a rotation around a unit axis by an angle in radians.
60 /// The rotation direction is clockwise when looking along the rotation axis towards the origin.
61 /// </summary>
62 /// <param name="axis">The axis of rotation.</param>
63 /// <param name="angle">The rotation angle in radians.</param>
64 /// <returns>The RigidTransform from a rotation axis and angle of rotation.</returns>
65 [MethodImpl(MethodImplOptions.AggressiveInlining)]
66 public static RigidTransform AxisAngle(float3 axis, float angle) { return new RigidTransform(quaternion.AxisAngle(axis, angle), float3.zero); }
67
68 /// <summary>
69 /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
70 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
71 /// </summary>
72 /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
73 /// <returns>The RigidTransform of the Euler angle transformation in x-y-z order.</returns>
74 [MethodImpl(MethodImplOptions.AggressiveInlining)]
75 public static RigidTransform EulerXYZ(float3 xyz) { return new RigidTransform(quaternion.EulerXYZ(xyz), float3.zero); }
76
77 /// <summary>
78 /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
79 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
80 /// </summary>
81 /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
82 /// <returns>The RigidTransform of the Euler angle transformation in x-z-y order.</returns>
83 [MethodImpl(MethodImplOptions.AggressiveInlining)]
84 public static RigidTransform EulerXZY(float3 xyz) { return new RigidTransform(quaternion.EulerXZY(xyz), float3.zero); }
85
86 /// <summary>
87 /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
88 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
89 /// </summary>
90 /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
91 /// <returns>The RigidTransform of the Euler angle transformation in y-x-z order.</returns>
92 [MethodImpl(MethodImplOptions.AggressiveInlining)]
93 public static RigidTransform EulerYXZ(float3 xyz) { return new RigidTransform(quaternion.EulerYXZ(xyz), float3.zero); }
94
95 /// <summary>
96 /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
97 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
98 /// </summary>
99 /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
100 /// <returns>The RigidTransform of the Euler angle transformation in y-z-x order.</returns>
101 [MethodImpl(MethodImplOptions.AggressiveInlining)]
102 public static RigidTransform EulerYZX(float3 xyz) { return new RigidTransform(quaternion.EulerYZX(xyz), float3.zero); }
103
104
105 /// <summary>
106 /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
107 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
108 /// This is the default order rotation order in Unity.
109 /// </summary>
110 /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
111 /// <returns>The RigidTransform of the Euler angle transformation in z-x-y order.</returns>
112 [MethodImpl(MethodImplOptions.AggressiveInlining)]
113 public static RigidTransform EulerZXY(float3 xyz) { return new RigidTransform(quaternion.EulerZXY(xyz), float3.zero); }
114
115 /// <summary>
116 /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
117 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
118 /// </summary>
119 /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
120 /// <returns>The RigidTransform of the Euler angle transformation in z-y-x order.</returns>
121 [MethodImpl(MethodImplOptions.AggressiveInlining)]
122 public static RigidTransform EulerZYX(float3 xyz) { return new RigidTransform(quaternion.EulerZYX(xyz), float3.zero); }
123
124 /// <summary>
125 /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
126 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
127 /// </summary>
128 /// <param name="x">The rotation angle around the x-axis in radians.</param>
129 /// <param name="y">The rotation angle around the y-axis in radians.</param>
130 /// <param name="z">The rotation angle around the z-axis in radians.</param>
131 /// <returns>The RigidTransform of the Euler angle transformation in x-y-z order.</returns>
132 [MethodImpl(MethodImplOptions.AggressiveInlining)]
133 public static RigidTransform EulerXYZ(float x, float y, float z) { return EulerXYZ(float3(x, y, z)); }
134
135 /// <summary>
136 /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
137 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
138 /// </summary>
139 /// <param name="x">The rotation angle around the x-axis in radians.</param>
140 /// <param name="y">The rotation angle around the y-axis in radians.</param>
141 /// <param name="z">The rotation angle around the z-axis in radians.</param>
142 /// <returns>The RigidTransform of the Euler angle transformation in x-z-y order.</returns>
143 [MethodImpl(MethodImplOptions.AggressiveInlining)]
144 public static RigidTransform EulerXZY(float x, float y, float z) { return EulerXZY(float3(x, y, z)); }
145
146 /// <summary>
147 /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
148 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
149 /// </summary>
150 /// <param name="x">The rotation angle around the x-axis in radians.</param>
151 /// <param name="y">The rotation angle around the y-axis in radians.</param>
152 /// <param name="z">The rotation angle around the z-axis in radians.</param>
153 /// <returns>The RigidTransform of the Euler angle transformation in y-x-z order.</returns>
154 [MethodImpl(MethodImplOptions.AggressiveInlining)]
155 public static RigidTransform EulerYXZ(float x, float y, float z) { return EulerYXZ(float3(x, y, z)); }
156
157 /// <summary>
158 /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
159 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
160 /// </summary>
161 /// <param name="x">The rotation angle around the x-axis in radians.</param>
162 /// <param name="y">The rotation angle around the y-axis in radians.</param>
163 /// <param name="z">The rotation angle around the z-axis in radians.</param>
164 /// <returns>The RigidTransform of the Euler angle transformation in y-z-x order.</returns>
165 [MethodImpl(MethodImplOptions.AggressiveInlining)]
166 public static RigidTransform EulerYZX(float x, float y, float z) { return EulerYZX(float3(x, y, z)); }
167
168 /// <summary>
169 /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
170 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
171 /// This is the default order rotation order in Unity.
172 /// </summary>
173 /// <param name="x">The rotation angle around the x-axis in radians.</param>
174 /// <param name="y">The rotation angle around the y-axis in radians.</param>
175 /// <param name="z">The rotation angle around the z-axis in radians.</param>
176 /// <returns>The RigidTransform of the Euler angle transformation in z-x-y order.</returns>
177 [MethodImpl(MethodImplOptions.AggressiveInlining)]
178 public static RigidTransform EulerZXY(float x, float y, float z) { return EulerZXY(float3(x, y, z)); }
179
180 /// <summary>
181 /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
182 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
183 /// </summary>
184 /// <param name="x">The rotation angle around the x-axis in radians.</param>
185 /// <param name="y">The rotation angle around the y-axis in radians.</param>
186 /// <param name="z">The rotation angle around the z-axis in radians.</param>
187 /// <returns>The RigidTransform of the Euler angle transformation in z-y-x order.</returns>
188 [MethodImpl(MethodImplOptions.AggressiveInlining)]
189 public static RigidTransform EulerZYX(float x, float y, float z) { return EulerZYX(float3(x, y, z)); }
190
191 /// <summary>
192 /// Returns a RigidTransform constructed by first performing 3 rotations around the principal axes in a given order.
193 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
194 /// When the rotation order is known at compile time, it is recommended for performance reasons to use specific
195 /// Euler rotation constructors such as EulerZXY(...).
196 /// </summary>
197 /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
198 /// <param name="order">The order in which the rotations are applied.</param>
199 /// <returns>The RigidTransform of the Euler angle transformation in the given rotation order.</returns>
200 [MethodImpl(MethodImplOptions.AggressiveInlining)]
201 public static RigidTransform Euler(float3 xyz, RotationOrder order = RotationOrder.ZXY)
202 {
203 switch (order)
204 {
205 case RotationOrder.XYZ:
206 return EulerXYZ(xyz);
207 case RotationOrder.XZY:
208 return EulerXZY(xyz);
209 case RotationOrder.YXZ:
210 return EulerYXZ(xyz);
211 case RotationOrder.YZX:
212 return EulerYZX(xyz);
213 case RotationOrder.ZXY:
214 return EulerZXY(xyz);
215 case RotationOrder.ZYX:
216 return EulerZYX(xyz);
217 default:
218 return RigidTransform.identity;
219 }
220 }
221
222 /// <summary>
223 /// Returns a RigidTransform constructed by first performing 3 rotations around the principal axes in a given order.
224 /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
225 /// When the rotation order is known at compile time, it is recommended for performance reasons to use specific
226 /// Euler rotation constructors such as EulerZXY(...).
227 /// </summary>
228 /// <param name="x">The rotation angle around the x-axis in radians.</param>
229 /// <param name="y">The rotation angle around the y-axis in radians.</param>
230 /// <param name="z">The rotation angle around the z-axis in radians.</param>
231 /// <param name="order">The order in which the rotations are applied.</param>
232 /// <returns>The RigidTransform of the Euler angle transformation in the given rotation order.</returns>
233 [MethodImpl(MethodImplOptions.AggressiveInlining)]
234 public static RigidTransform Euler(float x, float y, float z, RotationOrder order = RotationOrder.Default)
235 {
236 return Euler(float3(x, y, z), order);
237 }
238
239 /// <summary>Returns a RigidTransform that rotates around the x-axis by a given number of radians.</summary>
240 /// <param name="angle">The clockwise rotation angle when looking along the x-axis towards the origin in radians.</param>
241 /// <returns>The RigidTransform of rotating around the x-axis by the given angle.</returns>
242 [MethodImpl(MethodImplOptions.AggressiveInlining)]
243 public static RigidTransform RotateX(float angle)
244 {
245 return new RigidTransform(quaternion.RotateX(angle), float3.zero);
246 }
247
248 /// <summary>Returns a RigidTransform that rotates around the y-axis by a given number of radians.</summary>
249 /// <param name="angle">The clockwise rotation angle when looking along the y-axis towards the origin in radians.</param>
250 /// <returns>The RigidTransform of rotating around the y-axis by the given angle.</returns>
251 [MethodImpl(MethodImplOptions.AggressiveInlining)]
252 public static RigidTransform RotateY(float angle)
253 {
254 return new RigidTransform(quaternion.RotateY(angle), float3.zero);
255 }
256
257 /// <summary>Returns a RigidTransform that rotates around the z-axis by a given number of radians.</summary>
258 /// <param name="angle">The clockwise rotation angle when looking along the z-axis towards the origin in radians.</param>
259 /// <returns>The RigidTransform of rotating around the z-axis by the given angle.</returns>
260 [MethodImpl(MethodImplOptions.AggressiveInlining)]
261 public static RigidTransform RotateZ(float angle)
262 {
263 return new RigidTransform(quaternion.RotateZ(angle), float3.zero);
264 }
265
266 /// <summary>Returns a RigidTransform that translates by an amount specified by a float3 vector.</summary>
267 /// <param name="vector">The translation vector.</param>
268 /// <returns>The RigidTransform that translates by the given translation vector.</returns>
269 [MethodImpl(MethodImplOptions.AggressiveInlining)]
270 public static RigidTransform Translate(float3 vector)
271 {
272 return new RigidTransform(quaternion.identity, vector);
273 }
274
275
276 /// <summary>Returns true if the RigidTransform is equal to a given RigidTransform, false otherwise.</summary>
277 /// <param name="x">The RigidTransform to compare with.</param>
278 /// <returns>True if the RigidTransform is equal to the input, false otherwise.</returns>
279 [MethodImpl(MethodImplOptions.AggressiveInlining)]
280 public bool Equals(RigidTransform x) { return rot.Equals(x.rot) && pos.Equals(x.pos); }
281
282 /// <summary>Returns true if the RigidTransform is equal to a given RigidTransform, false otherwise.</summary>
283 /// <param name="x">The object to compare with.</param>
284 /// <returns>True if the RigidTransform is equal to the input, false otherwise.</returns>
285 [MethodImpl(MethodImplOptions.AggressiveInlining)]
286 public override bool Equals(object x) { return x is RigidTransform converted && Equals(converted); }
287
288 /// <summary>Returns a hash code for the RigidTransform.</summary>
289 /// <returns>The hash code of the RigidTransform.</returns>
290 [MethodImpl(MethodImplOptions.AggressiveInlining)]
291 public override int GetHashCode() { return (int)math.hash(this); }
292
293 /// <summary>Returns a string representation of the RigidTransform.</summary>
294 /// <returns>The string representation of the RigidTransform.</returns>
295 [MethodImpl(MethodImplOptions.AggressiveInlining)]
296 public override string ToString()
297 {
298 return string.Format("RigidTransform(({0}f, {1}f, {2}f, {3}f), ({4}f, {5}f, {6}f))",
299 rot.value.x, rot.value.y, rot.value.z, rot.value.w, pos.x, pos.y, pos.z);
300 }
301
302 /// <summary>Returns a string representation of the RigidTransform using a specified format and culture-specific format information.</summary>
303 /// <param name="format">The format string.</param>
304 /// <param name="formatProvider">The format provider to use during formatting.</param>
305 /// <returns>The formatted string representation of the RigidTransform.</returns>
306 [MethodImpl(MethodImplOptions.AggressiveInlining)]
307 public string ToString(string format, IFormatProvider formatProvider)
308 {
309 return string.Format("float4x4(({0}f, {1}f, {2}f, {3}f), ({4}f, {5}f, {6}f))",
310 rot.value.x.ToString(format, formatProvider),
311 rot.value.y.ToString(format, formatProvider),
312 rot.value.z.ToString(format, formatProvider),
313 rot.value.w.ToString(format, formatProvider),
314 pos.x.ToString(format, formatProvider),
315 pos.y.ToString(format, formatProvider),
316 pos.z.ToString(format, formatProvider));
317 }
318 }
319
320 public static partial class math
321 {
322 /// <summary>Returns a RigidTransform constructed from a rotation represented by a unit quaternion and a translation represented by a float3 vector.</summary>
323 /// <param name="rot">The quaternion rotation.</param>
324 /// <param name="pos">The translation vector.</param>
325 /// <returns>The RigidTransform of the given rotation quaternion and translation vector.</returns>
326 [MethodImpl(MethodImplOptions.AggressiveInlining)]
327 public static RigidTransform RigidTransform(quaternion rot, float3 pos) { return new RigidTransform(rot, pos); }
328
329 /// <summary>Returns a RigidTransform constructed from a rotation represented by a float3x3 rotation matrix and a translation represented by a float3 vector.</summary>
330 /// <param name="rotation">The float3x3 rotation matrix.</param>
331 /// <param name="translation">The translation vector.</param>
332 /// <returns>The RigidTransform of the given rotation matrix and translation vector.</returns>
333 [MethodImpl(MethodImplOptions.AggressiveInlining)]
334 public static RigidTransform RigidTransform(float3x3 rotation, float3 translation) { return new RigidTransform(rotation, translation); }
335
336 /// <summary>Returns a RigidTransform constructed from a rotation represented by a float3x3 matrix and a translation represented by a float3 vector.</summary>
337 /// <param name="transform">The float4x4 transformation matrix.</param>
338 /// <returns>The RigidTransform of the given float4x4 transformation matrix.</returns>
339 [MethodImpl(MethodImplOptions.AggressiveInlining)]
340 public static RigidTransform RigidTransform(float4x4 transform) { return new RigidTransform(transform); }
341
342 /// <summary>Returns the inverse of a RigidTransform.</summary>
343 /// <param name="t">The RigidTransform to invert.</param>
344 /// <returns>The inverse RigidTransform.</returns>
345 [MethodImpl(MethodImplOptions.AggressiveInlining)]
346 public static RigidTransform inverse(RigidTransform t)
347 {
348 quaternion invRotation = inverse(t.rot);
349 float3 invTranslation = mul(invRotation, -t.pos);
350 return new RigidTransform(invRotation, invTranslation);
351 }
352
353 /// <summary>Returns the result of transforming the RigidTransform b by the RigidTransform a.</summary>
354 /// <param name="a">The RigidTransform on the left.</param>
355 /// <param name="b">The RigidTransform on the right.</param>
356 /// <returns>The RigidTransform of a transforming b.</returns>
357 [MethodImpl(MethodImplOptions.AggressiveInlining)]
358 public static RigidTransform mul(RigidTransform a, RigidTransform b)
359 {
360 return new RigidTransform(mul(a.rot, b.rot), mul(a.rot, b.pos) + a.pos);
361 }
362
363 /// <summary>Returns the result of transforming a float4 homogeneous coordinate by a RigidTransform.</summary>
364 /// <param name="a">The RigidTransform.</param>
365 /// <param name="pos">The position to be transformed.</param>
366 /// <returns>The transformed position.</returns>
367 [MethodImpl(MethodImplOptions.AggressiveInlining)]
368 public static float4 mul(RigidTransform a, float4 pos)
369 {
370 return float4(mul(a.rot, pos.xyz) + a.pos * pos.w, pos.w);
371 }
372
373 /// <summary>Returns the result of rotating a float3 vector by a RigidTransform.</summary>
374 /// <param name="a">The RigidTransform.</param>
375 /// <param name="dir">The direction vector to rotate.</param>
376 /// <returns>The rotated direction vector.</returns>
377 [MethodImpl(MethodImplOptions.AggressiveInlining)]
378 public static float3 rotate(RigidTransform a, float3 dir)
379 {
380 return mul(a.rot, dir);
381 }
382
383 /// <summary>Returns the result of transforming a float3 point by a RigidTransform.</summary>
384 /// <param name="a">The RigidTransform.</param>
385 /// <param name="pos">The position to transform.</param>
386 /// <returns>The transformed position.</returns>
387 [MethodImpl(MethodImplOptions.AggressiveInlining)]
388 public static float3 transform(RigidTransform a, float3 pos)
389 {
390 return mul(a.rot, pos) + a.pos;
391 }
392
393 /// <summary>Returns a uint hash code of a RigidTransform.</summary>
394 /// <param name="t">The RigidTransform to hash.</param>
395 /// <returns>The hash code of the input RigidTransform</returns>
396 [MethodImpl(MethodImplOptions.AggressiveInlining)]
397 public static uint hash(RigidTransform t)
398 {
399 return hash(t.rot) + 0xC5C5394Bu * hash(t.pos);
400 }
401
402 /// <summary>
403 /// Returns a uint4 vector hash code of a RigidTransform.
404 /// When multiple elements are to be hashes together, it can more efficient to calculate and combine wide hash
405 /// that are only reduced to a narrow uint hash at the very end instead of at every step.
406 /// </summary>
407 /// <param name="t">The RigidTransform to hash.</param>
408 /// <returns>The uint4 wide hash code.</returns>
409 [MethodImpl(MethodImplOptions.AggressiveInlining)]
410 public static uint4 hashwide(RigidTransform t)
411 {
412 return hashwide(t.rot) + 0xC5C5394Bu * hashwide(t.pos).xyzz;
413 }
414 }
415}