-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSkinnedMeshCombiner.cs
303 lines (265 loc) · 10.4 KB
/
SkinnedMeshCombiner.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SkinnedMeshCombiner
{
private readonly List<List<int>> resultIndices = new List<List<int>>();
private readonly List<Vector3> resultVertices = new List<Vector3>();
private readonly List<Vector2> resultUvs = new List<Vector2>();
private readonly List<Vector3> resultNormals = new List<Vector3>();
private readonly List<Vector4> resultTangents = new List<Vector4>();
private readonly List<Color> resultColors = new List<Color>();
private readonly List<BoneWeight> resultBoneWeight = new List<BoneWeight>();
private readonly List<int> tempIndices = new List<int>();
private readonly List<Vector3> tempVertices = new List<Vector3>();
private readonly List<Vector2> tempUvs = new List<Vector2>();
private readonly List<Vector3> tempNormals = new List<Vector3>();
private readonly List<Vector4> tempTangents = new List<Vector4>();
private readonly List<Color> tempColors = new List<Color>();
private readonly List<BoneWeight> tempBoneWeights = new List<BoneWeight>();
private readonly List<int[]> tempIndicesAssignment = new List<int[]>();
private readonly List<int> boneMapping = new List<int>();
private readonly Dictionary<int, HumanBodyBones> transformBoneMapping1 = new Dictionary<int, HumanBodyBones>();
private readonly Dictionary<HumanBodyBones, int> transformBoneMapping2 = new Dictionary<HumanBodyBones, int>();
private readonly Dictionary<int, int> transformBoneMapping3 = new Dictionary<int, int>();
/// <summary>
/// Combines Unity SkinnedMeshRenderers. Takes a list of SkinnedMeshInstance and outputs a single mesh with working bones and blendshapes.
/// </summary>
/// <param name="combine">List of SkinnedMeshInstance</param>
/// <param name="result">Output, will set sharedMesh and sharedMaterials</param>
public void CombineMeshes(List<SkinnedMeshInstance> combine, SkinnedMeshRenderer result)
{
//The instance to use as the base for rigging and blend shapes.
var mainMesh = combine.First();
Clear();
var currentIndiceCount = 0;
var currentSubmeshCount = 0;
var isMainMesh = true;
var previousSMR = mainMesh.SMR;
for (var i = 0; i < combine.Count; i++)
{
var comb = combine[i];
var hasBone = comb.SMR != null;
if (hasBone && comb.SMR != previousSMR)
{
previousSMR = comb.SMR;
MapBones(comb, mainMesh);
isMainMesh = false;
}
if (resultIndices.Count <= currentSubmeshCount)
{
resultIndices.Add(new List<int>());
tempIndicesAssignment.Add(new int[65535]);
}
var resultInd = resultIndices[currentSubmeshCount];
comb.Mesh.GetIndices(tempIndices, comb.SubMeshIndex);
comb.Mesh.GetVertices(tempVertices);
comb.Mesh.GetUVs(0, tempUvs);
comb.Mesh.GetNormals(tempNormals);
comb.Mesh.GetTangents(tempTangents);
comb.Mesh.GetColors(tempColors);
if (hasBone)
{
comb.Mesh.GetBoneWeights(tempBoneWeights);
}
var tempAssign = tempIndicesAssignment[currentSubmeshCount];
foreach (var ind in tempIndices)
{
if (tempAssign[ind] == 0)
{
AddVertex(currentIndiceCount, resultInd, ind, comb.Transform);
if (hasBone)
{
if (isMainMesh)
{
resultBoneWeight.Add(tempBoneWeights[ind]);
}
else
{
resultBoneWeight.Add(GetMappedBoneWeight(tempBoneWeights[ind]));
}
}
else
{
resultBoneWeight.Add(default(BoneWeight));
}
tempAssign[ind] = currentIndiceCount;
currentIndiceCount += 1;
}
else
{
resultInd.Add(tempAssign[ind]);
}
}
currentSubmeshCount += 1;
}
var resultMesh = new Mesh();
resultMesh.subMeshCount = currentSubmeshCount;
resultMesh.SetVertices(resultVertices);
resultMesh.SetUVs(0, resultUvs);
resultMesh.SetNormals(resultNormals);
resultMesh.SetTangents(resultTangents);
resultMesh.SetColors(resultColors);
resultMesh.boneWeights = resultBoneWeight.ToArray();
for (var i = 0; i < currentSubmeshCount; i += 1)
{
resultMesh.SetTriangles(resultIndices[i], i, false);
}
CopyBlendShapes(mainMesh.Mesh, resultMesh);
resultMesh.bindposes = mainMesh.Mesh.bindposes;
resultMesh.RecalculateBounds();
result.sharedMesh = resultMesh;
result.sharedMaterials = combine.Select(c => c.Material).ToArray();
}
private void Clear()
{
foreach (var ind in resultIndices)
{
ind.Clear();
}
foreach (var ind in tempIndicesAssignment)
{
for (var i = 0; i < ind.Length; i += 1)
{
ind[i] = 0;
}
}
resultVertices.Clear();
resultUvs.Clear();
resultNormals.Clear();
resultTangents.Clear();
resultColors.Clear();
resultBoneWeight.Clear();
}
private void AddVertex(int currentIndiceCount, List<int> resultInd, int ind, Matrix4x4 transform)
{
resultInd.Add(currentIndiceCount);
resultVertices.Add(transform * tempVertices[ind]);
resultUvs.Add(tempUvs[ind]);
resultNormals.Add(tempNormals[ind]);
resultTangents.Add(tempTangents[ind]);
if (tempColors.Count > 0)
{
resultColors.Add(tempColors[ind]);
}
else
{
resultColors.Add(default(Color));
}
}
private void MapBones(SkinnedMeshInstance extraMesh, SkinnedMeshInstance targetMesh)
{
boneMapping.Clear();
transformBoneMapping1.Clear();
transformBoneMapping2.Clear();
transformBoneMapping3.Clear();
for (var val = HumanBodyBones.Hips; val < HumanBodyBones.LastBone; val += 1)
{
var itemBone = extraMesh.Animator.GetBoneTransform(val);
if (itemBone != null)
{
transformBoneMapping1.Add(itemBone.GetInstanceID(), val);
}
var mainBone = targetMesh.Animator.GetBoneTransform(val);
if (mainBone != null)
{
transformBoneMapping2.Add(val, mainBone.GetInstanceID());
}
}
for (var i = 0; i < targetMesh.SMR.bones.Length; i += 1)
{
var bone = targetMesh.SMR.bones[i].GetInstanceID();
transformBoneMapping3[bone] = i;
}
for (var i = 0; i < extraMesh.SMR.bones.Length; i += 1)
{
var result = -1;
var bone = extraMesh.SMR.bones[i].GetInstanceID();
if (transformBoneMapping1.ContainsKey(bone))
{
var key = transformBoneMapping1[bone];
if (transformBoneMapping2.ContainsKey(key))
{
var finalTransformId = transformBoneMapping2[key];
if (transformBoneMapping3.ContainsKey(finalTransformId))
{
result = transformBoneMapping3[finalTransformId];
}
}
}
boneMapping.Add(result);
}
}
private BoneWeight GetMappedBoneWeight(BoneWeight boneWeight)
{
if (this.boneMapping[boneWeight.boneIndex0] == -1)
{
boneWeight.boneIndex0 = 0;
boneWeight.weight0 = 0;
}
else
{
boneWeight.boneIndex0 = this.boneMapping[boneWeight.boneIndex0];
}
if (this.boneMapping[boneWeight.boneIndex1] == -1)
{
boneWeight.boneIndex1 = 0;
boneWeight.weight1 = 0;
}
else
{
boneWeight.boneIndex1 = this.boneMapping[boneWeight.boneIndex1];
}
if (this.boneMapping[boneWeight.boneIndex2] == -1)
{
boneWeight.boneIndex2 = 0;
boneWeight.weight2 = 0;
}
else
{
boneWeight.boneIndex2 = this.boneMapping[boneWeight.boneIndex2];
}
if (this.boneMapping[boneWeight.boneIndex3] == -1)
{
boneWeight.boneIndex3 = 0;
boneWeight.weight3 = 0;
}
else
{
boneWeight.boneIndex3 = this.boneMapping[boneWeight.boneIndex3];
}
return boneWeight;
}
private void CopyBlendShapes(Mesh input, Mesh output)
{
if (input.blendShapeCount <= 0)
{
return;
}
var inputDeltaVertices = new Vector3[input.vertexCount];
var inputDeltaNormals = new Vector3[input.vertexCount];
var inputDeltaTangents = new Vector3[input.vertexCount];
var deltaVertices = new Vector3[output.vertexCount];
var deltaNormals = new Vector3[output.vertexCount];
var deltaTangents = new Vector3[output.vertexCount];
for (var i = 0; i < input.blendShapeCount; i += 1)
{
var frameCount = input.GetBlendShapeFrameCount(i);
for (var frameIndex = 0; frameIndex < frameCount; frameIndex++)
{
var name = input.GetBlendShapeName(i);
var weight = input.GetBlendShapeFrameWeight(i, frameIndex);
input.GetBlendShapeFrameVertices(i, frameIndex, inputDeltaVertices, inputDeltaNormals, inputDeltaTangents);
var inputLength = Math.Min(inputDeltaVertices.Length, deltaVertices.Length);
for (var j = 0; j < inputLength; j += 1)
{
deltaVertices[tempIndicesAssignment[0][j]] = inputDeltaVertices[j];
deltaNormals[tempIndicesAssignment[0][j]] = inputDeltaNormals[j];
deltaTangents[tempIndicesAssignment[0][j]] = inputDeltaTangents[j];
}
output.AddBlendShapeFrame(name, weight, deltaVertices, deltaNormals, deltaTangents);
}
}
}
}