forked from Blightbuster/The-Forest
/
MB3_MeshBakerGrouperCore.cs
270 lines (235 loc) · 7.23 KB
/
MB3_MeshBakerGrouperCore.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
using System;
using System.Collections.Generic;
using DigitalOpus.MB.Core;
using UnityEngine;
[Serializable]
public class MB3_MeshBakerGrouperCore
{
public void DoClustering(MB3_TextureBaker tb)
{
if (this.clusterGrouper == null)
{
Debug.LogError("Cluster Grouper was null.");
return;
}
Dictionary<string, List<Renderer>> dictionary = this.clusterGrouper.FilterIntoGroups(tb.GetObjectsToCombine());
Debug.Log("Found " + dictionary.Count + " cells with Renderers. Creating bakers.");
if (this.clusterOnLMIndex)
{
Dictionary<string, List<Renderer>> dictionary2 = new Dictionary<string, List<Renderer>>();
foreach (string text in dictionary.Keys)
{
List<Renderer> gaws = dictionary[text];
Dictionary<int, List<Renderer>> dictionary3 = this.GroupByLightmapIndex(gaws);
foreach (int num in dictionary3.Keys)
{
string key = text + "-LM-" + num;
dictionary2.Add(key, dictionary3[num]);
}
}
dictionary = dictionary2;
}
foreach (string key2 in dictionary.Keys)
{
List<Renderer> gaws2 = dictionary[key2];
this.AddMeshBaker(tb, key2, gaws2);
}
}
private Dictionary<int, List<Renderer>> GroupByLightmapIndex(List<Renderer> gaws)
{
Dictionary<int, List<Renderer>> dictionary = new Dictionary<int, List<Renderer>>();
for (int i = 0; i < gaws.Count; i++)
{
List<Renderer> list;
if (dictionary.ContainsKey(gaws[i].lightmapIndex))
{
list = dictionary[gaws[i].lightmapIndex];
}
else
{
list = new List<Renderer>();
dictionary.Add(gaws[i].lightmapIndex, list);
}
list.Add(gaws[i]);
}
return dictionary;
}
private void AddMeshBaker(MB3_TextureBaker tb, string key, List<Renderer> gaws)
{
int num = 0;
for (int i = 0; i < gaws.Count; i++)
{
Mesh mesh = MB_Utility.GetMesh(gaws[i].gameObject);
if (mesh != null)
{
num += mesh.vertexCount;
}
}
GameObject gameObject = new GameObject("MeshBaker-" + key);
gameObject.transform.position = Vector3.zero;
MB3_MeshBakerCommon mb3_MeshBakerCommon;
if (num >= 65535)
{
mb3_MeshBakerCommon = gameObject.AddComponent<MB3_MultiMeshBaker>();
mb3_MeshBakerCommon.useObjsToMeshFromTexBaker = false;
}
else
{
mb3_MeshBakerCommon = gameObject.AddComponent<MB3_MeshBaker>();
mb3_MeshBakerCommon.useObjsToMeshFromTexBaker = false;
}
mb3_MeshBakerCommon.textureBakeResults = tb.textureBakeResults;
mb3_MeshBakerCommon.transform.parent = tb.transform;
for (int j = 0; j < gaws.Count; j++)
{
mb3_MeshBakerCommon.GetObjectsToCombine().Add(gaws[j].gameObject);
}
}
public MB3_MeshBakerGrouperCore.ClusterGrouper clusterGrouper;
public bool clusterOnLMIndex;
public enum ClusterType
{
none,
grid,
pie
}
[Serializable]
public class ClusterGrouper
{
public Dictionary<string, List<Renderer>> FilterIntoGroups(List<GameObject> selection)
{
if (this.clusterType == MB3_MeshBakerGrouperCore.ClusterType.none)
{
return this.FilterIntoGroupsNone(selection);
}
if (this.clusterType == MB3_MeshBakerGrouperCore.ClusterType.grid)
{
return this.FilterIntoGroupsGrid(selection);
}
if (this.clusterType == MB3_MeshBakerGrouperCore.ClusterType.pie)
{
return this.FilterIntoGroupsPie(selection);
}
return new Dictionary<string, List<Renderer>>();
}
public Dictionary<string, List<Renderer>> FilterIntoGroupsNone(List<GameObject> selection)
{
Debug.Log("Filtering into groups none");
Dictionary<string, List<Renderer>> dictionary = new Dictionary<string, List<Renderer>>();
List<Renderer> list = new List<Renderer>();
for (int i = 0; i < selection.Count; i++)
{
list.Add(selection[i].GetComponent<Renderer>());
}
dictionary.Add("MeshBaker", list);
return dictionary;
}
public Dictionary<string, List<Renderer>> FilterIntoGroupsGrid(List<GameObject> selection)
{
Dictionary<string, List<Renderer>> dictionary = new Dictionary<string, List<Renderer>>();
if (this.cellSize.x <= 0f || this.cellSize.y <= 0f || this.cellSize.z <= 0f)
{
Debug.LogError("cellSize x,y,z must all be greater than zero.");
return dictionary;
}
Debug.Log("Collecting renderers in each cell");
foreach (GameObject gameObject in selection)
{
GameObject gameObject2 = gameObject;
Renderer[] componentsInChildren = gameObject2.GetComponentsInChildren<Renderer>();
for (int i = 0; i < componentsInChildren.Length; i++)
{
if (componentsInChildren[i] is MeshRenderer || componentsInChildren[i] is SkinnedMeshRenderer)
{
Vector3 position = componentsInChildren[i].transform.position;
position.x = Mathf.Floor((position.x - this.origin.x) / this.cellSize.x) * this.cellSize.x;
position.y = Mathf.Floor((position.y - this.origin.y) / this.cellSize.y) * this.cellSize.y;
position.z = Mathf.Floor((position.z - this.origin.z) / this.cellSize.z) * this.cellSize.z;
string key = position.ToString();
List<Renderer> list;
if (dictionary.ContainsKey(key))
{
list = dictionary[key];
}
else
{
list = new List<Renderer>();
dictionary.Add(key, list);
}
if (!list.Contains(componentsInChildren[i]))
{
list.Add(componentsInChildren[i]);
}
}
}
}
return dictionary;
}
public Dictionary<string, List<Renderer>> FilterIntoGroupsPie(List<GameObject> selection)
{
Dictionary<string, List<Renderer>> dictionary = new Dictionary<string, List<Renderer>>();
if (this.pieNumSegments == 0)
{
Debug.LogError("pieNumSegments must be greater than zero.");
return dictionary;
}
if (this.pieAxis.magnitude <= 1E-06f)
{
Debug.LogError("Pie axis must have length greater than zero.");
return dictionary;
}
this.pieAxis.Normalize();
Quaternion rotation = Quaternion.FromToRotation(this.pieAxis, Vector3.up);
Debug.Log("Collecting renderers in each cell");
foreach (GameObject gameObject in selection)
{
GameObject gameObject2 = gameObject;
Renderer[] componentsInChildren = gameObject2.GetComponentsInChildren<Renderer>();
for (int i = 0; i < componentsInChildren.Length; i++)
{
if (componentsInChildren[i] is MeshRenderer || componentsInChildren[i] is SkinnedMeshRenderer)
{
Vector3 point = componentsInChildren[i].transform.position - this.origin;
point.Normalize();
point = rotation * point;
float num;
if (Mathf.Abs(point.x) < 0.0001f && Mathf.Abs(point.z) < 0.0001f)
{
num = 0f;
}
else
{
num = Mathf.Atan2(point.z, point.x) * 57.29578f;
if (num < 0f)
{
num = 360f + num;
}
}
int num2 = Mathf.FloorToInt(num / 360f * (float)this.pieNumSegments);
string key = "seg_" + num2;
List<Renderer> list;
if (dictionary.ContainsKey(key))
{
list = dictionary[key];
}
else
{
list = new List<Renderer>();
dictionary.Add(key, list);
}
if (!list.Contains(componentsInChildren[i]))
{
list.Add(componentsInChildren[i]);
}
}
}
}
return dictionary;
}
public MB3_MeshBakerGrouperCore.ClusterType clusterType;
public Vector3 origin;
public Vector3 cellSize;
public int pieNumSegments = 4;
public Vector3 pieAxis = Vector3.up;
}
}