forked from alvani/Unity-glTF-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGlTF_Skin.cs
74 lines (60 loc) · 2.08 KB
/
GlTF_Skin.cs
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
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GlTF_Skin : GlTF_Writer {
public int invBindMatricesAccessorIndex;
public List<Transform> joints;
public Transform mesh;
public GlTF_Skin() { }
public static string GetNameFromObject(Object o)
{
return "skin_" + GlTF_Writer.GetNameFromObject(o, true);
}
public void Populate (Transform m, ref GlTF_Accessor invBindMatricesAccessor, int invBindAccessorIndex)
{
SkinnedMeshRenderer skinMesh = m.GetComponent<SkinnedMeshRenderer>();
if (!skinMesh)
return;
// Populate bind poses. From https://docs.unity3d.com/ScriptReference/Mesh-bindposes.html:
// The bind pose is bone's inverse transformation matrix
// In this case we also make this matrix relative to the root
// So that we can move the root game object around freely
joints = new List<Transform>();
//Collect all bones from skin object. Order should be kept here since bones are referenced in the mesh
foreach(Transform t in skinMesh.bones)
{
joints.Add(t);
}
Matrix4x4[] invBindMatrices = new Matrix4x4[joints.Count];
for (int i = 0; i < skinMesh.bones.Length; ++i)
{
// Generates inverseWorldMatrix in right-handed coordinate system
Matrix4x4 invBind = skinMesh.sharedMesh.bindposes[i];
convertMatrixLeftToRightHandedness(ref invBind);
invBindMatrices[i] = invBind;
}
invBindMatricesAccessor.Populate(invBindMatrices, m);
invBindMatricesAccessorIndex = invBindAccessorIndex;
}
public override void Write ()
{
Indent(); jsonWriter.Write ("{\n");
IndentIn();
Indent(); jsonWriter.Write("\"inverseBindMatrices\": "+ invBindMatricesAccessorIndex + ",\n");
Indent(); jsonWriter.Write ("\"joints\": [\n");
IndentIn();
foreach (Transform j in joints)
{
CommaNL();
Indent(); jsonWriter.Write ("" + GlTF_Writer.nodeNames.IndexOf(GlTF_Node.GetNameFromObject(j)));
}
IndentOut();
jsonWriter.WriteLine();
Indent(); jsonWriter.Write ("],\n");
Indent(); jsonWriter.Write("\"name\": \"" + name + "\"\n");
IndentOut();
Indent(); jsonWriter.Write ("}");
}
}
#endif