SkeletonMeshUtility.cs
9.02 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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UniHumanoid
{
public static class SkeletonMeshUtility
{
class MeshBuilder
{
List<Vector3> m_positioins = new List<Vector3>();
List<int> m_indices = new List<int>();
List<BoneWeight> m_boneWeights = new List<BoneWeight>();
public void AddBone(Vector3 head, Vector3 tail, int boneIndex, float xWidth, float zWidth)
{
var dir = (tail - head).normalized;
Vector3 xaxis;
Vector3 zaxis;
if (Vector3.Dot(dir, Vector3.forward) >= 1.0f - float.Epsilon)
{
xaxis = Vector3.right;
zaxis = Vector3.down;
}
else
{
xaxis = Vector3.Cross(dir, Vector3.forward).normalized;
zaxis = Vector3.forward;
}
AddBox((head+tail)*0.5f,
xaxis*xWidth,
(tail-head)*0.5f,
zaxis*zWidth,
boneIndex);
}
void AddBox(Vector3 center, Vector3 xaxis, Vector3 yaxis, Vector3 zaxis, int boneIndex)
{
AddQuad(
center - yaxis - xaxis - zaxis,
center - yaxis + xaxis - zaxis,
center - yaxis + xaxis + zaxis,
center - yaxis - xaxis + zaxis,
boneIndex);
AddQuad(
center + yaxis - xaxis - zaxis,
center + yaxis + xaxis - zaxis,
center + yaxis + xaxis + zaxis,
center + yaxis - xaxis + zaxis,
boneIndex, true);
AddQuad(
center - xaxis - yaxis - zaxis,
center - xaxis + yaxis - zaxis,
center - xaxis + yaxis + zaxis,
center - xaxis - yaxis + zaxis,
boneIndex, true);
AddQuad(
center + xaxis - yaxis - zaxis,
center + xaxis + yaxis - zaxis,
center + xaxis + yaxis + zaxis,
center + xaxis - yaxis + zaxis,
boneIndex);
AddQuad(
center - zaxis - xaxis - yaxis,
center - zaxis + xaxis - yaxis,
center - zaxis + xaxis + yaxis,
center - zaxis - xaxis + yaxis,
boneIndex, true);
AddQuad(
center + zaxis - xaxis - yaxis,
center + zaxis + xaxis - yaxis,
center + zaxis + xaxis + yaxis,
center + zaxis - xaxis + yaxis,
boneIndex);
}
void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, int boneIndex, bool reverse=false)
{
var i = m_positioins.Count;
m_positioins.Add(v0);
m_positioins.Add(v1);
m_positioins.Add(v2);
m_positioins.Add(v3);
var bw = new BoneWeight
{
boneIndex0=boneIndex,
weight0=1.0f,
};
m_boneWeights.Add(bw);
m_boneWeights.Add(bw);
m_boneWeights.Add(bw);
m_boneWeights.Add(bw);
if (reverse)
{
m_indices.Add(i + 3);
m_indices.Add(i + 2);
m_indices.Add(i + 1);
m_indices.Add(i + 1);
m_indices.Add(i);
m_indices.Add(i + 3);
}
else
{
m_indices.Add(i);
m_indices.Add(i + 1);
m_indices.Add(i + 2);
m_indices.Add(i + 2);
m_indices.Add(i + 3);
m_indices.Add(i);
}
}
public Mesh CreateMesh()
{
var mesh = new Mesh();
mesh.SetVertices(m_positioins);
mesh.boneWeights = m_boneWeights.ToArray();
mesh.triangles = m_indices.ToArray();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;
}
}
struct BoneHeadTail
{
public HumanBodyBones Head;
public HumanBodyBones Tail;
public Vector3 TailOffset;
public float XWidth;
public float ZWidth;
public BoneHeadTail(HumanBodyBones head, HumanBodyBones tail, float xWidth = 0.05f, float zWidth = 0.05f)
{
Head = head;
Tail = tail;
TailOffset = Vector3.zero;
XWidth = xWidth;
ZWidth = zWidth;
}
public BoneHeadTail(HumanBodyBones head, Vector3 tailOffset, float xWidth = 0.05f, float zWidth = 0.05f)
{
Head = head;
Tail = HumanBodyBones.LastBone;
TailOffset = tailOffset;
XWidth = xWidth;
ZWidth = zWidth;
}
}
static BoneHeadTail[] Bones = new BoneHeadTail[]
{
new BoneHeadTail(HumanBodyBones.Hips, HumanBodyBones.Spine, 0.1f, 0.06f),
new BoneHeadTail(HumanBodyBones.Spine, HumanBodyBones.Chest),
new BoneHeadTail(HumanBodyBones.Chest, HumanBodyBones.Neck, 0.1f, 0.06f),
new BoneHeadTail(HumanBodyBones.Neck, HumanBodyBones.Head, 0.03f, 0.03f),
new BoneHeadTail(HumanBodyBones.Head, new Vector3(0, 0.1f, 0), 0.1f, 0.1f),
new BoneHeadTail(HumanBodyBones.LeftShoulder, HumanBodyBones.LeftUpperArm),
new BoneHeadTail(HumanBodyBones.LeftUpperArm, HumanBodyBones.LeftLowerArm),
new BoneHeadTail(HumanBodyBones.LeftLowerArm, HumanBodyBones.LeftHand),
new BoneHeadTail(HumanBodyBones.LeftHand, new Vector3(-0.1f, 0, 0)),
new BoneHeadTail(HumanBodyBones.LeftUpperLeg, HumanBodyBones.LeftLowerLeg),
new BoneHeadTail(HumanBodyBones.LeftLowerLeg, HumanBodyBones.LeftFoot),
new BoneHeadTail(HumanBodyBones.LeftFoot, HumanBodyBones.LeftToes),
new BoneHeadTail(HumanBodyBones.LeftToes, new Vector3(0, 0, 0.1f)),
new BoneHeadTail(HumanBodyBones.RightShoulder, HumanBodyBones.RightUpperArm),
new BoneHeadTail(HumanBodyBones.RightUpperArm, HumanBodyBones.RightLowerArm),
new BoneHeadTail(HumanBodyBones.RightLowerArm, HumanBodyBones.RightHand),
new BoneHeadTail(HumanBodyBones.RightHand, new Vector3(0.1f, 0, 0)),
new BoneHeadTail(HumanBodyBones.RightUpperLeg, HumanBodyBones.RightLowerLeg),
new BoneHeadTail(HumanBodyBones.RightLowerLeg, HumanBodyBones.RightFoot),
new BoneHeadTail(HumanBodyBones.RightFoot, HumanBodyBones.RightToes),
new BoneHeadTail(HumanBodyBones.RightToes, new Vector3(0, 0, 0.1f)),
};
public static SkinnedMeshRenderer CreateRenderer(Animator animator)
{
//var bodyBones = (HumanBodyBones[])Enum.GetValues(typeof(HumanBodyBones));
var bones = animator.transform.Traverse().ToList();
var builder = new MeshBuilder();
foreach(var headTail in Bones)
{
var head = animator.GetBoneTransform(headTail.Head);
if (head!=null)
{
Transform tail = null;
if(headTail.Tail!= HumanBodyBones.LastBone)
{
tail = animator.GetBoneTransform(headTail.Tail);
}
if (tail != null)
{
builder.AddBone(head.position, tail.position, bones.IndexOf(head), headTail.XWidth, headTail.ZWidth);
}
else
{
builder.AddBone(head.position, head.position + headTail.TailOffset, bones.IndexOf(head), headTail.XWidth, headTail.ZWidth);
}
}
else
{
Debug.LogWarningFormat("{0} not found", headTail.Head);
}
}
var mesh = builder.CreateMesh();
mesh.name = "box-man";
mesh.bindposes = bones.Select(x =>
x.worldToLocalMatrix * animator.transform.localToWorldMatrix).ToArray();
var renderer = animator.gameObject.AddComponent<SkinnedMeshRenderer>();
renderer.bones = bones.ToArray();
renderer.rootBone = animator.GetBoneTransform(HumanBodyBones.Hips);
renderer.sharedMesh = mesh;
//var bounds = new Bounds(Vector3.zero, mesh.bounds.size);
//renderer.localBounds = bounds;
return renderer;
}
}
}