blob: a503bf68a5c1eaa1e7066d5e214c6d866830442b (
plain)
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;
using UnityEngine;
[AddComponentMenu("Simple Mesh Combine")]
public class SimpleMeshCombine : MonoBehaviour
{
public GameObject[] combinedGameOjects;
public GameObject combined;
public string meshName = "Combined_Meshes";
public bool _canGenerateLightmapUV;
public int vCount;
public bool generateLightmapUV;
public float lightmapScale = 1f;
public GameObject copyTarget;
public bool destroyOldColliders;
public bool keepStructure = true;
public Mesh autoOverwrite;
public bool setStatic = true;
public void EnableRenderers(bool e)
{
for (int i = 0; i < combinedGameOjects.Length && !(combinedGameOjects[i] == null); i++)
{
Renderer component = combinedGameOjects[i].GetComponent<Renderer>();
if (component != null)
{
component.enabled = e;
}
}
}
public MeshFilter[] FindEnabledMeshes()
{
MeshFilter[] array = null;
int num = 0;
array = base.transform.GetComponentsInChildren<MeshFilter>();
for (int i = 0; i < array.Length; i++)
{
if (array[i].GetComponent<MeshRenderer>() != null && array[i].GetComponent<MeshRenderer>().enabled)
{
num++;
}
}
MeshFilter[] array2 = new MeshFilter[num];
num = 0;
for (int j = 0; j < array.Length; j++)
{
if (array[j].GetComponent<MeshRenderer>() != null && array[j].GetComponent<MeshRenderer>().enabled)
{
array2[num] = array[j];
num++;
}
}
return array2;
}
public void CombineMeshes()
{
GameObject gameObject = new GameObject();
gameObject.name = "_Combined Mesh [" + base.name + "]";
gameObject.gameObject.AddComponent<MeshFilter>();
gameObject.gameObject.AddComponent<MeshRenderer>();
MeshFilter[] array = null;
array = FindEnabledMeshes();
ArrayList arrayList = new ArrayList();
ArrayList arrayList2 = new ArrayList();
combinedGameOjects = new GameObject[array.Length];
for (int i = 0; i < array.Length; i++)
{
combinedGameOjects[i] = array[i].gameObject;
MeshRenderer component = array[i].GetComponent<MeshRenderer>();
array[i].transform.gameObject.GetComponent<Renderer>().enabled = false;
if (array[i].sharedMesh == null)
{
break;
}
for (int j = 0; j < array[i].sharedMesh.subMeshCount; j++)
{
if (component == null)
{
break;
}
if (j < component.sharedMaterials.Length && j < array[i].sharedMesh.subMeshCount)
{
int num = Contains(arrayList, component.sharedMaterials[j]);
if (num == -1)
{
arrayList.Add(component.sharedMaterials[j]);
num = arrayList.Count - 1;
}
arrayList2.Add(new ArrayList());
CombineInstance combineInstance = default(CombineInstance);
combineInstance.transform = component.transform.localToWorldMatrix;
combineInstance.mesh = array[i].sharedMesh;
combineInstance.subMeshIndex = j;
(arrayList2[num] as ArrayList).Add(combineInstance);
}
}
}
Mesh[] array2 = new Mesh[arrayList.Count];
CombineInstance[] array3 = new CombineInstance[arrayList.Count];
for (int k = 0; k < arrayList.Count; k++)
{
CombineInstance[] combine = (arrayList2[k] as ArrayList).ToArray(typeof(CombineInstance)) as CombineInstance[];
array2[k] = new Mesh();
array2[k].CombineMeshes(combine, mergeSubMeshes: true, useMatrices: true);
array3[k] = default(CombineInstance);
array3[k].mesh = array2[k];
array3[k].subMeshIndex = 0;
}
Mesh mesh2 = (gameObject.GetComponent<MeshFilter>().sharedMesh = new Mesh());
Mesh mesh3 = mesh2;
mesh3.Clear();
mesh3.CombineMeshes(array3, mergeSubMeshes: false, useMatrices: false);
gameObject.GetComponent<MeshFilter>().sharedMesh = mesh3;
Mesh[] array4 = array2;
foreach (Mesh obj in array4)
{
obj.Clear();
Object.DestroyImmediate(obj);
}
MeshRenderer meshRenderer = gameObject.GetComponent<MeshFilter>().GetComponent<MeshRenderer>();
if (meshRenderer == null)
{
meshRenderer = base.gameObject.AddComponent<MeshRenderer>();
}
Material[] materials = arrayList.ToArray(typeof(Material)) as Material[];
meshRenderer.materials = materials;
combined = gameObject.gameObject;
EnableRenderers(e: false);
gameObject.transform.parent = base.transform;
gameObject.GetComponent<MeshFilter>().sharedMesh.RecalculateBounds();
vCount = gameObject.GetComponent<MeshFilter>().sharedMesh.vertexCount;
if (vCount > 65536)
{
Debug.LogWarning("Vertex Count: " + vCount + "- Vertex Count too high, please divide mesh combine into more groups. Max 65536 for each mesh");
_canGenerateLightmapUV = false;
}
else
{
_canGenerateLightmapUV = true;
}
if (setStatic)
{
combined.isStatic = true;
}
}
public int Contains(ArrayList l, Material n)
{
for (int i = 0; i < l.Count; i++)
{
if (l[i] as Material == n)
{
return i;
}
}
return -1;
}
}
|