UnityExtensions.cs
8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF
{
public struct PosRot
{
public Vector3 Position;
public Quaternion Rotation;
public static PosRot FromGlobalTransform(Transform t)
{
return new PosRot
{
Position = t.position,
Rotation = t.rotation,
};
}
}
public class BlendShape
{
public string Name;
public BlendShape(string name)
{
Name = name;
}
public List<Vector3> Positions = new List<Vector3>();
public List<Vector3> Normals = new List<Vector3>();
public List<Vector3> Tangents = new List<Vector3>();
}
public static class UnityExtensions
{
public static Vector4 ReverseZ(this Vector4 v)
{
return new Vector4(v.x, v.y, -v.z, v.w);
}
public static Vector3 ReverseZ(this Vector3 v)
{
return new Vector3(v.x, v.y, -v.z);
}
[Obsolete]
public static Vector2 ReverseY(this Vector2 v)
{
return new Vector2(v.x, -v.y);
}
public static Vector2 ReverseUV(this Vector2 v)
{
return new Vector2(v.x, 1.0f - v.y);
}
public static Quaternion ReverseZ(this Quaternion q)
{
float angle;
Vector3 axis;
q.ToAngleAxis(out angle, out axis);
return Quaternion.AngleAxis(-angle, ReverseZ(axis));
}
public static Matrix4x4 Matrix4x4FromColumns(Vector4 c0, Vector4 c1, Vector4 c2, Vector4 c3)
{
#if UNITY_2017_1_OR_NEWER
return new Matrix4x4(c0, c1, c2, c3);
#else
var m = default(Matrix4x4);
m.SetColumn(0, c0);
m.SetColumn(1, c1);
m.SetColumn(2, c2);
m.SetColumn(3, c3);
return m;
#endif
}
public static Matrix4x4 Matrix4x4FromRotation(Quaternion q)
{
#if UNITY_2017_1_OR_NEWER
return Matrix4x4.Rotate(q);
#else
var m = default(Matrix4x4);
m.SetTRS(Vector3.zero, q, Vector3.one);
return m;
#endif
}
public static Matrix4x4 ReverseZ(this Matrix4x4 m)
{
m.SetTRS(m.ExtractPosition().ReverseZ(), m.ExtractRotation().ReverseZ(), m.ExtractScale());
return m;
}
public static Matrix4x4 MatrixFromArray(float[] values)
{
var m = new Matrix4x4();
m.m00 = values[0];
m.m10 = values[1];
m.m20 = values[2];
m.m30 = values[3];
m.m01 = values[4];
m.m11 = values[5];
m.m21 = values[6];
m.m31 = values[7];
m.m02 = values[8];
m.m12 = values[9];
m.m22 = values[10];
m.m32 = values[11];
m.m03 = values[12];
m.m13 = values[13];
m.m23 = values[14];
m.m33 = values[15];
return m;
}
// https://forum.unity.com/threads/how-to-assign-matrix4x4-to-transform.121966/
public static Quaternion ExtractRotation(this Matrix4x4 matrix)
{
Vector3 forward;
forward.x = matrix.m02;
forward.y = matrix.m12;
forward.z = matrix.m22;
Vector3 upwards;
upwards.x = matrix.m01;
upwards.y = matrix.m11;
upwards.z = matrix.m21;
return Quaternion.LookRotation(forward, upwards);
}
public static Vector3 ExtractPosition(this Matrix4x4 matrix)
{
Vector3 position;
position.x = matrix.m03;
position.y = matrix.m13;
position.z = matrix.m23;
return position;
}
public static Vector3 ExtractScale(this Matrix4x4 matrix)
{
Vector3 scale;
scale.x = new Vector4(matrix.m00, matrix.m10, matrix.m20, matrix.m30).magnitude;
scale.y = new Vector4(matrix.m01, matrix.m11, matrix.m21, matrix.m31).magnitude;
scale.z = new Vector4(matrix.m02, matrix.m12, matrix.m22, matrix.m32).magnitude;
return scale;
}
public static string RelativePathFrom(this Transform self, Transform root)
{
var path = new List<String>();
for (var current = self; current != null; current = current.parent)
{
if (current == root)
{
return String.Join("/", path.ToArray());
}
path.Insert(0, current.name);
}
throw new Exception("no RelativePath");
}
public static Transform GetChildByName(this Transform self, string childName)
{
foreach (Transform child in self)
{
if (child.name == childName)
{
return child;
}
}
throw new KeyNotFoundException();
}
public static Transform GetFromPath(this Transform self, string path)
{
var current = self;
var splited = path.Split('/');
foreach (var childName in splited)
{
current = current.GetChildByName(childName);
}
return current;
}
public static IEnumerable<Transform> GetChildren(this Transform self)
{
foreach (Transform child in self)
{
yield return child;
}
}
public static IEnumerable<Transform> Traverse(this Transform t)
{
yield return t;
foreach (Transform x in t)
{
foreach (Transform y in x.Traverse())
{
yield return y;
}
}
}
public static Transform FindDescenedant(this Transform t, string name)
{
return t.Traverse().First(x => x.name == name);
}
public static IEnumerable<Transform> Ancestors(this Transform t)
{
yield return t;
if (t.parent != null)
{
foreach (Transform x in t.parent.Ancestors())
{
yield return x;
}
}
}
public static float[] ToArray(this Quaternion q)
{
return new float[] { q.x, q.y, q.z, q.w };
}
public static float[] ToArray(this Vector3 v)
{
return new float[] { v.x, v.y, v.z };
}
public static float[] ToArray(this Vector4 v)
{
return new float[] { v.x, v.y, v.z, v.w };
}
public static float[] ToArray(this Color c)
{
return new float[] { c.r, c.g, c.b, c.a };
}
public static void ReverseZRecursive(this Transform root)
{
var globalMap = root.Traverse().ToDictionary(x => x, x => PosRot.FromGlobalTransform(x));
foreach (var x in root.Traverse())
{
x.position = globalMap[x].Position.ReverseZ();
x.rotation = globalMap[x].Rotation.ReverseZ();
}
}
public static Mesh GetSharedMesh(this Transform t)
{
var meshFilter = t.GetComponent<MeshFilter>();
if (meshFilter != null)
{
return meshFilter.sharedMesh;
}
var skinnedMeshRenderer = t.GetComponent<SkinnedMeshRenderer>();
if (skinnedMeshRenderer != null)
{
return skinnedMeshRenderer.sharedMesh;
}
return null;
}
public static Material[] GetSharedMaterials(this Transform t)
{
var renderer = t.GetComponent<Renderer>();
if (renderer != null)
{
return renderer.sharedMaterials;
}
return new Material[] { };
}
public static bool Has<T>(this Transform transform, T t) where T : Component
{
return transform.GetComponent<T>() == t;
}
public static T GetOrAddComponent<T>(this GameObject go) where T : Component
{
var c = go.GetComponent<T>();
if (c != null)
{
return c;
}
return go.AddComponent<T>();
}
}
}