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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
using System;
using System.Collections.Generic;
using UnityEngine;
namespace NGS.MeshFusionPro;
[DisallowMultipleComponent]
public abstract class MeshFusionSource : MonoBehaviour
{
private RuntimeMeshFusion _controller;
private int _combinedSourcesCount;
private int _failedSourcesCount;
private int _totalSourcesCount;
[field: SerializeField]
public SourceCombineStatus CombineStatus { get; private set; }
[field: SerializeField]
public int ControllerIndex { get; set; }
[field: SerializeField]
[field: HideInInspector]
public bool CombineAtStart { get; set; } = true;
[field: SerializeField]
[field: HideInInspector]
public CombineErrorStrategy CombineErrorStrategy { get; set; }
[field: SerializeField]
public AfterCombineAction AfterCombineAction { get; set; }
[field: SerializeField]
public bool IsIncompatible { get; private set; }
[field: SerializeField]
public string IncompatibilityReason { get; private set; }
[field: SerializeField]
public bool HasCombineErrors { get; private set; }
[field: SerializeField]
public string CombineErrors { get; private set; }
public event Action<MeshFusionSource, IEnumerable<ICombinedObjectPart>> onCombineFinished;
private void Reset()
{
CheckCompatibility();
}
private void Start()
{
if (CombineAtStart)
{
AssignToController();
}
}
private void OnDestroy()
{
Unfreeze();
}
public void Unfreeze()
{
if (CombineStatus == SourceCombineStatus.AssignedToController)
{
UnassignFromController();
}
ClearSourcesAndUnsubscribe();
}
public abstract bool TryGetBounds(ref Bounds bounds);
public bool CheckCompatibility()
{
try
{
string incompatibilityReason;
bool flag = CheckCompatibilityAndGetComponents(out incompatibilityReason);
IsIncompatible = !flag;
IncompatibilityReason = incompatibilityReason;
return flag;
}
catch (Exception ex)
{
IsIncompatible = true;
IncompatibilityReason += $"\n{ex.Message}{ex.StackTrace}";
return false;
}
}
public bool AssignToController()
{
try
{
if (CombineStatus != 0)
{
return false;
}
if (!CheckCompatibility())
{
return false;
}
_controller = RuntimeMeshFusion.FindByIndex(ControllerIndex);
if (_controller == null)
{
throw new NullReferenceException("RuntimeMeshFusion with index " + ControllerIndex + " not found");
}
CreateSourcesAndSubscribe();
HasCombineErrors = false;
CombineErrors = "";
_combinedSourcesCount = 0;
_failedSourcesCount = 0;
_totalSourcesCount = 0;
foreach (ICombineSource combineSource in GetCombineSources())
{
_totalSourcesCount++;
_controller.AddSource(combineSource);
}
CombineStatus = SourceCombineStatus.AssignedToController;
return true;
}
catch (Exception ex)
{
HasCombineErrors = true;
CombineErrors += $"\n{ex.Message}\n{ex.StackTrace}";
UnassignFromController();
CombineStatus = SourceCombineStatus.FailedToCombine;
return false;
}
}
public void UndoCombine()
{
try
{
foreach (ICombinedObjectPart combinedPart in GetCombinedParts())
{
combinedPart.Destroy();
}
foreach (ICombineSource combineSource in GetCombineSources())
{
combineSource.onCombined += delegate(ICombinedObject root, ICombinedObjectPart part)
{
part.Destroy();
};
}
UnassignFromController();
ClearSourcesAndUnsubscribe();
ClearParts();
_combinedSourcesCount = 0;
_failedSourcesCount = 0;
ToggleComponents(enabled: true);
CombineStatus = SourceCombineStatus.NotCombined;
}
catch (Exception ex)
{
HasCombineErrors = true;
CombineErrors += $"\n{ex.Message}\n{ex.StackTrace}";
CombineStatus = SourceCombineStatus.FailedToCombine;
}
}
private void CreateSourcesAndSubscribe()
{
CreateSources();
foreach (ICombineSource combineSource in GetCombineSources())
{
combineSource.onCombined += OnSourceCombinedHandler;
combineSource.onCombineError += OnCombineErrorHandler;
combineSource.onCombineFailed += OnFailedCombineSourceHandler;
}
}
private void ClearSourcesAndUnsubscribe()
{
foreach (ICombineSource combineSource in GetCombineSources())
{
combineSource.onCombined -= OnSourceCombinedHandler;
combineSource.onCombineError -= OnCombineErrorHandler;
combineSource.onCombineFailed -= OnFailedCombineSourceHandler;
}
ClearSources();
}
private void UnassignFromController()
{
if (_controller != null)
{
foreach (ICombineSource combineSource in GetCombineSources())
{
_controller.RemoveSource(combineSource);
}
}
_controller = null;
}
private void OnSourceCombinedHandler(ICombinedObject root, ICombinedObjectPart part)
{
CombineStatus = SourceCombineStatus.CombinedPartially;
_combinedSourcesCount++;
OnSourceCombinedInternal(root, part);
if (_combinedSourcesCount + _failedSourcesCount == _totalSourcesCount)
{
OnCombineFinishedHandler();
}
}
private void OnCombineErrorHandler(ICombinedObject root, string errorMessage)
{
HasCombineErrors = true;
CombineErrors = CombineErrors + errorMessage + "\n";
OnCombineErrorInternal(root, errorMessage);
if (CombineErrorStrategy == CombineErrorStrategy.UndoCombining)
{
UndoCombine();
}
}
private void OnFailedCombineSourceHandler(ICombinedObject root)
{
_failedSourcesCount++;
OnFailedCombineSourceInternal(root);
if (_combinedSourcesCount + _failedSourcesCount == _totalSourcesCount)
{
OnCombineFinishedHandler();
}
}
private void OnCombineFinishedHandler()
{
ClearSourcesAndUnsubscribe();
if (_combinedSourcesCount == _totalSourcesCount)
{
CombineStatus = SourceCombineStatus.Combined;
}
else if (_failedSourcesCount == _totalSourcesCount)
{
CombineStatus = SourceCombineStatus.FailedToCombine;
return;
}
OnCombineFinishedInternal();
this.onCombineFinished?.Invoke(this, GetCombinedParts());
if (AfterCombineAction == AfterCombineAction.DisableComponents)
{
ToggleComponents(enabled: false);
}
else if (AfterCombineAction == AfterCombineAction.DestroyGameObject)
{
UnityEngine.Object.Destroy(base.gameObject);
}
}
protected virtual void OnSourceCombinedInternal(ICombinedObject root, ICombinedObjectPart part)
{
}
protected virtual void OnCombineErrorInternal(ICombinedObject root, string errorMessage)
{
}
protected virtual void OnFailedCombineSourceInternal(ICombinedObject root)
{
}
protected virtual void OnCombineFinishedInternal()
{
}
protected bool CanCreateCombineSource(GameObject go, ref string incompatibilityReason, ref MeshRenderer renderer, ref MeshFilter filter, ref Mesh mesh)
{
if (renderer == null)
{
renderer = go.GetComponent<MeshRenderer>();
}
if (renderer == null)
{
incompatibilityReason = "MeshRenderer not found";
return false;
}
if (renderer.isPartOfStaticBatch)
{
incompatibilityReason = "MeshRenderer is PartOfStaticBatching\nDisable Static Batching";
return false;
}
if (filter == null)
{
filter = go.GetComponent<MeshFilter>();
}
if (filter == null)
{
incompatibilityReason = "MeshFilter not found";
return false;
}
mesh = filter.sharedMesh;
if (mesh == null)
{
incompatibilityReason = "Mesh not found";
return false;
}
if (!mesh.isReadable)
{
incompatibilityReason = "Mesh is not readable. Enable Read/Write in import settings";
return false;
}
if (mesh.subMeshCount != renderer.sharedMaterials.Length)
{
incompatibilityReason = "Submesh count and Materials count isn't equal";
return false;
}
incompatibilityReason = "";
return true;
}
protected abstract bool CheckCompatibilityAndGetComponents(out string incompatibilityReason);
protected abstract void CreateSources();
protected abstract void ClearSources();
protected abstract void ClearParts();
protected abstract IEnumerable<ICombineSource> GetCombineSources();
protected abstract IEnumerable<ICombinedObjectPart> GetCombinedParts();
protected abstract void ToggleComponents(bool enabled);
}
|