-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMeshCombiner.cs
171 lines (136 loc) · 6.68 KB
/
MeshCombiner.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
using System.Collections.Generic;
using UnityEngine;
namespace SpellcastStudios
{
public class MeshCombiner
{
/// <summary>
/// Combines meshes and creates a new skinned mesh renderer, parenting to baseBone
/// </summary>
/// <param name="baseBone"></param>
/// <param name="material"></param>
/// <param name="bones"></param>
/// <param name="meshes"></param>
/// <param name="rects">Optional UV's to apply to each mesh</param>
/// <returns></returns>
public static SkinnedMeshRenderer CombineFast(Transform baseBone, Material material, Transform[] bones, Mesh[] meshes, Rect[] rects = null)
{
// Create mesh renderer
GameObject newGameObject = new GameObject("Avatar", typeof(SkinnedMeshRenderer));
newGameObject.transform.parent = baseBone.parent;
newGameObject.transform.localPosition = Vector3.zero;
newGameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
SkinnedMeshRenderer skinnedMeshRenderer = newGameObject.GetComponent<SkinnedMeshRenderer>();
CombineFast(skinnedMeshRenderer, baseBone, material, bones, meshes, rects);
return skinnedMeshRenderer;
}
/// <summary>
/// Combines meshes and implements onto an existing skinned mesh renderer + bones
/// </summary>
/// <param name="skinnedMeshRenderer"></param>
/// <param name="baseBone"></param>
/// <param name="material"></param>
/// <param name="bones"></param>
/// <param name="meshes"></param>
/// <param name="uvs">Optional UV's to apply to each mesh</param>
public static void CombineFast(SkinnedMeshRenderer skinnedMeshRenderer, Transform baseBone, Material material, Transform[] bones, Mesh[] meshes, Rect[] uvs=null)
{
if (meshes.Length == 0)
return;
CombineInstance[] combineInstances = new CombineInstance[meshes.Length];
for(int i =0;i<meshes.Length;i++)
{
if (meshes[i] == null)
continue;
combineInstances[i] = new CombineInstance();
combineInstances[i].transform = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(Vector3.zero),new Vector3(100,100,100));
combineInstances[i].mesh = meshes[i];
}
//Copy bind poses from first mesh in array
Matrix4x4[] bindPoses = meshes[0].bindposes;
Mesh combined_new_mesh = new Mesh();
combined_new_mesh.CombineMeshes(combineInstances,true,false);
combined_new_mesh.bindposes = bindPoses;
//Note: Mesh.boneWeights returns a copy of bone weights (this is undocumented)
BoneWeight[] newboneweights = combined_new_mesh.boneWeights;
Vector2[] newUvs = combined_new_mesh.uv;
//Blendshape dictionary contains list of matching blendshape indices per mesh.
//Index is offset by 1, so 0 = no shape exists for that mesh
Dictionary<string, int[]> blendshapes = new Dictionary<string, int[]>();
//Realign boneweights, apply uv's, and map blendshapes
int offset = 0;
for (int i=0;i<meshes.Length;i++)
{
for(int k=0;k<meshes[i].vertexCount;k++)
{
if(i > 0)
{
newboneweights[offset + k].boneIndex0 -= bones.Length * i;
newboneweights[offset + k].boneIndex1 -= bones.Length * i;
newboneweights[offset + k].boneIndex2 -= bones.Length * i;
newboneweights[offset + k].boneIndex3 -= bones.Length * i;
}
if(uvs != null)
newUvs[offset + k] = new Vector2(newUvs[offset + k].x * uvs[i].width + uvs[i].x, newUvs[offset + k].y * uvs[i].height + uvs[i].y);
}
offset += meshes[i].vertexCount;
for(int k=0;k<meshes[i].blendShapeCount;k++)
{
string key = meshes[i].GetBlendShapeName(k);
if(!blendshapes.ContainsKey(key))
blendshapes[key] = new int[meshes.Length];
blendshapes[key][i] = k + 1;
}
}
Vector3[] deltaVertices = null;
Vector3[] deltaTangents = null;
Vector3[] deltaNormals = null;
if (blendshapes.Count > 0)
{
deltaVertices = new Vector3[combined_new_mesh.vertexCount];
deltaTangents = new Vector3[combined_new_mesh.vertexCount];
deltaNormals = new Vector3[combined_new_mesh.vertexCount];
}
//We assume all blendshapes only have a single frame, aka 0 (empty) to 1 (full).
//So we just copy the last frame in each blendshape to a weight of 1
foreach (KeyValuePair<string,int[]> shape in blendshapes)
{
offset = 0;
for (int i = 0; i < meshes.Length; i++)
{
int vcount = meshes[i].vertexCount;
//No blendshape for this mesh
if (shape.Value[i] == 0)
{
//TODO: Research whether it's better to create a new array initially, or manually clear them as needed
System.Array.Clear(deltaVertices, offset, vcount);
System.Array.Clear(deltaTangents, offset, vcount);
System.Array.Clear(deltaNormals, offset, vcount);
offset += vcount;
continue;
}
//Since GetBlendShapeFrameVertices requires matching sizes of arrays (stupid), we gotta create these every time -_-
Vector3[] tempDeltaVertices = new Vector3[vcount];
Vector3[] tempDeltaTangents = new Vector3[vcount];
Vector3[] tempDeltaNormals = new Vector3[vcount];
int frame = (meshes[i].GetBlendShapeFrameCount(shape.Value[i] - 1) - 1);
meshes[i].GetBlendShapeFrameVertices(shape.Value[i] - 1, frame, tempDeltaVertices, tempDeltaNormals, tempDeltaTangents);
System.Array.Copy(tempDeltaVertices, 0, deltaVertices, offset, vcount);
System.Array.Copy(tempDeltaNormals, 0, deltaNormals, offset, vcount);
System.Array.Copy(tempDeltaTangents, 0, deltaTangents, offset, vcount);
offset += vcount;
}
//Apply
combined_new_mesh.AddBlendShapeFrame(shape.Key, 1, deltaVertices, deltaNormals, deltaTangents);
}
if (uvs != null)
combined_new_mesh.uv = newUvs;
combined_new_mesh.boneWeights = newboneweights;
combined_new_mesh.RecalculateBounds();
skinnedMeshRenderer.sharedMesh = combined_new_mesh;
skinnedMeshRenderer.sharedMaterial = material;
skinnedMeshRenderer.bones = bones;
skinnedMeshRenderer.rootBone = baseBone;
}
}
}