Cesium for Unity 1.15.4
Loading...
Searching...
No Matches
Helpers.cs
Go to the documentation of this file.
1using Unity.Mathematics;
2using UnityEditor;
3using UnityEngine;
4
5namespace CesiumForUnity
6{
7 internal class Helpers
8 {
9 public static string ToString<T>(T value)
10 {
11 return value.ToString();
12 }
13
14 public static Vector3 FromMathematics(double3 vector)
15 {
16 return new Vector3((float)vector.x, (float)vector.y, (float)vector.z);
17 }
18
19 public static Vector4 FromMathematics(double4 vector)
20 {
21 return new Vector4((float)vector.x, (float)vector.y, (float)vector.z, (float)vector.w);
22 }
23
24 public static double4x4 ToMathematics(Matrix4x4 matrix)
25 {
26 return new double4x4(
27 matrix.m00, matrix.m01, matrix.m02, matrix.m03,
28 matrix.m10, matrix.m11, matrix.m12, matrix.m13,
29 matrix.m20, matrix.m21, matrix.m22, matrix.m23,
30 matrix.m30, matrix.m31, matrix.m32, matrix.m33);
31 }
32
33 public static Matrix4x4 FromMathematics(double4x4 matrix)
34 {
35 return new Matrix4x4(FromMathematics(matrix.c0), FromMathematics(matrix.c1), FromMathematics(matrix.c2), FromMathematics(matrix.c3));
36 }
37
38 public static double3x3 ToMathematicsDouble3x3(Matrix4x4 matrix)
39 {
40 return new double3x3(
41 matrix.m00, matrix.m01, matrix.m02,
42 matrix.m10, matrix.m11, matrix.m12,
43 matrix.m20, matrix.m21, matrix.m22);
44 }
45
46 public static float3x3 ToMathematicsFloat3x3(Matrix4x4 matrix)
47 {
48 return new float3x3(
49 matrix.m00, matrix.m01, matrix.m02,
50 matrix.m10, matrix.m11, matrix.m12,
51 matrix.m20, matrix.m21, matrix.m22);
52 }
53
54 internal static void MatrixToRotationAndScale(double3x3 matrix, out quaternion rotation, out double3 scale)
55 {
56 double lengthColumn0 = math.length(matrix.c0);
57 double lengthColumn1 = math.length(matrix.c1);
58 double lengthColumn2 = math.length(matrix.c2);
59
60 float3x3 rotationMatrix = new float3x3(
61 (float3)(matrix.c0 / lengthColumn0),
62 (float3)(matrix.c1 / lengthColumn1),
63 (float3)(matrix.c2 / lengthColumn2));
64
65 scale = new double3(lengthColumn0, lengthColumn1, lengthColumn2);
66
67 double3 cross = math.cross(matrix.c0, matrix.c1);
68 if (math.dot(cross, matrix.c2) < 0.0)
69 {
70 rotationMatrix *= -1.0f;
71 scale *= -1.0f;
72 }
73
74 rotation = math.quaternion(rotationMatrix);
75 }
76
77 internal static void MatrixToTranslationRotationAndScale(double4x4 matrix, out double3 translation, out quaternion rotation, out double3 scale)
78 {
79 translation = matrix.c3.xyz;
80
81 Helpers.MatrixToRotationAndScale(
82 new double3x3(matrix.c0.xyz, matrix.c1.xyz, matrix.c2.xyz),
83 out rotation,
84 out scale);
85 }
86
87 internal static void MatrixToInaccurateRotationAndScale(double3x3 matrix, out Quaternion rotation, out Vector3 scale)
88 {
89 quaternion rotationTemp;
90 double3 scaleTemp;
91 MatrixToRotationAndScale(matrix, out rotationTemp, out scaleTemp);
92
93 rotation = rotationTemp;
94 scale = (float3)scaleTemp;
95 }
96
97 internal static void MatrixToInaccurateTranslationRotationAndScale(double4x4 matrix, out Vector3 translation, out Quaternion rotation, out Vector3 scale)
98 {
99 translation = Helpers.FromMathematics(matrix.c3.xyz);
100
101 Helpers.MatrixToInaccurateRotationAndScale(
102 new double3x3(matrix.c0.xyz, matrix.c1.xyz, matrix.c2.xyz),
103 out rotation,
104 out scale);
105 }
106
107 internal static double4x4 TranslationRotationAndScaleToMatrix(double3 translation, quaternion rotation, double3 scale)
108 {
109 double3x3 scaleMatrix = new double3x3(
110 new double3(scale.x, 0.0, 0.0),
111 new double3(0.0, scale.y, 0.0),
112 new double3(0.0, 0.0, scale.z));
113 double3x3 rotationMatrix = new float3x3(rotation);
114 double3x3 scaleAndRotate = math.mul(rotationMatrix, scaleMatrix);
115 return new double4x4(
116 new double4(scaleAndRotate.c0, 0.0),
117 new double4(scaleAndRotate.c1, 0.0),
118 new double4(scaleAndRotate.c2, 0.0),
119 new double4(translation, 1.0));
120 }
121 }
122}