-
Notifications
You must be signed in to change notification settings - Fork 22
/
AssetPostProcessorReorderBones.cs
82 lines (69 loc) · 1.96 KB
/
AssetPostProcessorReorderBones.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
75
76
77
78
79
80
81
82
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
/// <summary>
/// Sorts bone indexes in imported meshes.<br>
/// SkinnedMeshRenderer requires bone indexes to be sorted based on hierarchy.
/// </summary>
public class AssetPostProcessorReorderBones : AssetPostprocessor
{
void OnPostprocessModel(GameObject g)
{
this.Process(g);
}
void Process(GameObject g)
{
SkinnedMeshRenderer smr = g.GetComponentInChildren<SkinnedMeshRenderer>();
if (smr == null)
{
Debug.LogWarning("Unable to find Renderer" + smr.name);
return;
}
//list of bones
List<Transform> boneTransforms = smr.bones.ToList();
//sort based on hierarchy
boneTransforms.Sort(CompareTransform);
//record bone index mappings (richardf advice)
//build a Dictionary<int, int> that records the old bone index => new bone index mappings,
//then run through every vertex and just do boneIndexN = dict[boneIndexN] for each weight on each vertex.
var remap = new Dictionary<int, int>();
for (int i = 0; i < smr.bones.Length; i++)
{
remap[i] = boneTransforms.IndexOf(smr.bones[i]);
}
//remap bone weight indexes
BoneWeight[] bw = smr.sharedMesh.boneWeights;
for (int i = 0; i < bw.Length; i++)
{
bw[i].boneIndex0 = remap[bw[i].boneIndex0];
bw[i].boneIndex1 = remap[bw[i].boneIndex1];
bw[i].boneIndex2 = remap[bw[i].boneIndex2];
bw[i].boneIndex3 = remap[bw[i].boneIndex3];
}
//remap bindposes
var bp = new Matrix4x4[smr.sharedMesh.bindposes.Length];
for (int i = 0; i < bp.Length; i++)
{
bp[remap[i]] = smr.sharedMesh.bindposes[i];
}
//assign new data
smr.bones = boneTransforms.ToArray();
smr.sharedMesh.boneWeights = bw;
smr.sharedMesh.bindposes = bp;
}
private static int CompareTransform(Transform A, Transform B)
{
if (B.IsChildOf(A))
{
return -1;
}
if (A.IsChildOf(B))
{
return -1;
}
return 0;
}
}
#endif