-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathChiselModelManager.cs
More file actions
610 lines (520 loc) · 18.8 KB
/
ChiselModelManager.cs
File metadata and controls
610 lines (520 loc) · 18.8 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Pool;
using Chisel.Core;
using UnityEngine.Profiling;
using Unity.Jobs;
namespace Chisel.Components
{
public class ChiselModelManager : ScriptableObject, ISerializationCallbackReceiver
{
public const string kGeneratedDefaultModelName = "‹[default-model]›";
const string kDefaultModelName = "Model";
#region Instance
static ChiselModelManager _instance;
public static ChiselModelManager Instance
{
get
{
if (_instance)
return _instance;
var foundInstances = UnityEngine.Object.FindObjectsByType<ChiselModelManager>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
if (foundInstances == null ||
foundInstances.Length == 0)
{
_instance = ScriptableObject.CreateInstance<ChiselModelManager>();
_instance.hideFlags = HideFlags.HideAndDontSave;
return _instance;
}
if (foundInstances.Length > 1)
{
for (int i = 1; i < foundInstances.Length; i++)
ChiselObjectUtility.SafeDestroy(foundInstances[i]);
}
_instance = foundInstances[0];
return _instance;
}
}
#endregion
public event Action PostReset;
public event Action PostUpdateModels;
internal void Reset()
{
PostReset?.Invoke();
}
static int FinishMeshUpdates(CSGTree tree, ChiselMeshUpdates meshUpdates, JobHandle dependencies)
{
ChiselModelComponent foundModel = null;
var models = Instance.Models;
foreach (var model in models)
{
if (!model)
continue;
if (model.Node == tree)
foundModel = model;
}
if (foundModel == null)
{
if (meshUpdates.meshDataArray.Length > 0) meshUpdates.meshDataArray.Dispose();
meshUpdates.meshDataArray = default;
return 0;
}
if (foundModel.generated == null ||
!foundModel.generated.IsValid())
{
foundModel.generated?.Destroy();
foundModel.generated = ChiselGeneratedObjects.Create(foundModel.gameObject);
}
var count = foundModel.generated.FinishMeshUpdates(foundModel, meshUpdates, dependencies);
Instance.Rebuild(foundModel);
return count;
}
readonly static FinishMeshUpdate finishMeshUpdatesMethod = (FinishMeshUpdate)FinishMeshUpdates;
public void UpdateModels()
{
// Update the tree meshes
Profiler.BeginSample("Flush");
try
{
if (!CompactHierarchyManager.Flush(finishMeshUpdatesMethod))
{
ChiselUnityUVGenerationManager.DelayedUVGeneration();
return; // Nothing to update ..
}
}
finally
{
Profiler.EndSample();
}
{
Profiler.BeginSample("PostUpdateModels");
PostUpdateModels?.Invoke();
Profiler.EndSample();
}
}
// TODO: potentially have a history per scene, so when one model turns out to be invalid, go back to the previously selected model
readonly Dictionary<Scene, ChiselModelComponent> activeModels = new();
#region ActiveModel Serialization
[Serializable] public struct SceneModelPair { public Scene Key; public ChiselModelComponent Value; }
[SerializeField] SceneModelPair[] activeModelsArray;
public void OnBeforeSerialize()
{
var foundModels = ListPool<SceneModelPair>.Get();
//if (foundModels != null)
{
if (foundModels.Capacity < activeModels.Count)
foundModels.Capacity = activeModels.Count;
foreach (var pair in activeModels)
foundModels.Add(new SceneModelPair { Key = pair.Key, Value = pair.Value });
if (activeModelsArray != null && activeModelsArray.Length == foundModels.Count)
{
foundModels.CopyTo(activeModelsArray);
}
else
activeModelsArray = foundModels.ToArray();
ListPool<SceneModelPair>.Release(foundModels);
}
}
public void OnAfterDeserialize()
{
if (activeModelsArray == null)
return;
foreach (var pair in activeModelsArray)
activeModels[pair.Key] = pair.Value;
activeModelsArray = null;
}
#endregion
readonly HashSet<ChiselModelComponent> s_RegisteredModels = new();
readonly HashSet<ChiselNodeComponent> s_RegisteredNodes = new();
readonly HashSet<ChiselGeneratorComponent> s_RegisteredGenerators = new();
public IEnumerable<ChiselModelComponent> Models { get { return s_RegisteredModels; } }
public IEnumerable<ChiselNodeComponent> Nodes { get { return s_RegisteredNodes; } }
public IEnumerable<ChiselGeneratorComponent> Generators { get { return s_RegisteredGenerators; } }
public void Register(ChiselNodeComponent node)
{
if (!s_RegisteredNodes.Add(node))
return;
var generator = node as ChiselGeneratorComponent;
if (!ReferenceEquals(generator, null)) { s_RegisteredGenerators.Add(generator); }
var model = node as ChiselModelComponent;
if (!ReferenceEquals(model, null)) { s_RegisteredModels.Add(model); }
}
public void Unregister(ChiselNodeComponent node)
{
if (!s_RegisteredNodes.Remove(node))
return;
var generator = node as ChiselGeneratorComponent;
if (!ReferenceEquals(generator, null)) { s_RegisteredGenerators.Remove(generator); }
var model = node as ChiselModelComponent;
if (!ReferenceEquals(model, null))
{
// If we removed our model component, we should remove the containers
if (!model && model.hierarchyItem.GameObject)
RemoveContainerGameObjectWithUndo(model);
s_RegisteredModels.Remove(model);
}
}
public void Rebuild(ChiselModelComponent model)
{
if (!model.IsInitialized)
{
model.OnInitialize();
}
if (model.generated == null ||
!model.generated.IsValid())
{
model.generated?.Destroy();
model.generated = ChiselGeneratedObjects.Create(model.gameObject);
}
UpdateModelFlags(model);
if(model.AutoRebuildUVs)
{
ForceUpdateDelayedUVGeneration();
}
}
public bool IsDefaultModel(UnityEngine.Object obj)
{
var component = obj as Component;
if (!Equals(component, null))
return IsDefaultModel(component);
var gameObject = obj as GameObject;
if (!Equals(gameObject, null))
return IsDefaultModel(gameObject);
return false;
}
internal bool IsDefaultModel(GameObject gameObject)
{
if (!gameObject)
return false;
var model = gameObject.GetComponent<ChiselModelComponent>();
if (!model)
return false;
return (model.IsDefaultModel);
}
internal bool IsDefaultModel(Component component)
{
if (!component)
return false;
ChiselModelComponent model = component as ChiselModelComponent;
if (!model)
{
model = component.GetComponent<ChiselModelComponent>();
if (!model)
return false;
}
return (model.IsDefaultModel);
}
internal bool IsDefaultModel(ChiselModelComponent model)
{
if (!model)
return false;
return (model.IsDefaultModel);
}
internal ChiselModelComponent CreateDefaultModel(ChiselSceneHierarchy sceneHierarchy)
{
var currentScene = sceneHierarchy.Scene;
var rootGameObjects = ListPool<GameObject>.Get();
currentScene.GetRootGameObjects(rootGameObjects);
for (int i = 0; i < rootGameObjects.Count; i++)
{
if (!IsDefaultModel(rootGameObjects[i]))
continue;
var gameObject = rootGameObjects[i];
var model = gameObject.GetComponent<ChiselModelComponent>();
if (model)
return model;
var transform = gameObject.GetComponent<Transform>();
ChiselObjectUtility.ResetTransform(transform);
model = gameObject.AddComponent<ChiselModelComponent>();
UpdateModelFlags(model);
return model;
}
ListPool<GameObject>.Release(rootGameObjects);
var oldActiveScene = SceneManager.GetActiveScene();
if (currentScene != oldActiveScene)
SceneManager.SetActiveScene(currentScene);
try
{
var model = ChiselComponentFactory.Create<ChiselModelComponent>(kGeneratedDefaultModelName);
model.IsDefaultModel = true;
UpdateModelFlags(model);
return model;
}
finally
{
if (currentScene != oldActiveScene)
SceneManager.SetActiveScene(oldActiveScene);
}
}
// TODO: find a better place for this
public bool IsValidModelToBeSelected(ChiselModelComponent model)
{
if (!model || !model.isActiveAndEnabled || model.generated == null)
return false;
#if UNITY_EDITOR
var gameObject = model.gameObject;
var sceneVisibilityManager = UnityEditor.SceneVisibilityManager.instance;
if (sceneVisibilityManager.AreAllDescendantsHidden(gameObject) ||
sceneVisibilityManager.IsPickingDisabledOnAllDescendants(gameObject))
return false;
#endif
return true;
}
public void OnRenderModels(Camera camera, DrawModeFlags drawModeFlags)
{
foreach (var model in Models)
{
if (model == null)
continue;
ChiselRenderObjects.OnRenderModel(camera, model, drawModeFlags);
}
}
private void UpdateModelFlags(ChiselModelComponent model)
{
if (!IsDefaultModel(model))
return;
const HideFlags DefaultGameObjectHideFlags = HideFlags.NotEditable;
const HideFlags DefaultTransformHideFlags = HideFlags.NotEditable;// | HideFlags.HideInInspector;
var gameObject = model.gameObject;
var transform = model.transform;
if (gameObject.hideFlags != DefaultGameObjectHideFlags) gameObject.hideFlags = DefaultGameObjectHideFlags;
if (transform.hideFlags != DefaultTransformHideFlags) transform.hideFlags = DefaultTransformHideFlags;
if (transform.parent != null)
{
transform.SetParent(null, false);
ChiselObjectUtility.ResetTransform(transform);
}
}
private void RemoveContainerGameObjectWithUndo(ChiselModelComponent model)
{
if (model.generated != null)
model.generated.DestroyWithUndo();
}
public bool IsSelectable(ChiselModelComponent model)
{
if (!model || !model.isActiveAndEnabled)
return false;
var sceneVisibilityManager = UnityEditor.SceneVisibilityManager.instance;
var visible = !sceneVisibilityManager.IsHidden(model.gameObject);
var pickingEnabled = !sceneVisibilityManager.IsPickingDisabled(model.gameObject);
return visible && pickingEnabled;
}
public ChiselModelComponent ActiveModel
{
get
{
// Find our active model for the current active scene
var activeScene = SceneManager.GetActiveScene();
Instance.activeModels.TryGetValue(activeScene, out var activeModel);
// Check if the activeModel is valid & if it's scene actually points to the active Scene
if (ReferenceEquals(activeModel, null) ||
!activeModel || activeModel.gameObject.scene != activeScene)
{
// If active model is invalid or missing, find another model the active model
Instance.activeModels[activeScene] = FindModelInScene(activeScene);
return null;
}
// If we have an active model, but it's actually disabled, do not use it
// This prevents users from accidentally adding generators to a model that is inactive,
// and then be confused why nothing is visible.
if (!IsSelectable(activeModel))
return null;
return activeModel;
}
set
{
// When we set a model to be active, make sure we use the scene of its gameobject
var modelScene = value.gameObject.scene;
Instance.activeModels[modelScene] = value;
// And then make sure that scene is active
if (modelScene != SceneManager.GetActiveScene())
SceneManager.SetActiveScene(modelScene);
}
}
// Get all brushes directly contained by this CSGNode (not its children)
public void GetAllTreeBrushes(ChiselGeneratorComponent component, HashSet<CSGTreeBrush> foundBrushes)
{
if (foundBrushes == null ||
!component.TopTreeNode.Valid)
return;
var brush = (CSGTreeBrush)component.TopTreeNode;
if (brush.Valid)
{
foundBrushes.Add(brush);
}
else
{
var nodes = new List<CSGTreeNode>();
nodes.Add(component.TopTreeNode);
while (nodes.Count > 0)
{
var lastIndex = nodes.Count - 1;
var current = nodes[lastIndex];
nodes.RemoveAt(lastIndex);
var nodeType = current.Type;
if (nodeType == CSGNodeType.Brush)
{
brush = (CSGTreeBrush)current;
foundBrushes.Add(brush);
}
else
{
for (int i = current.Count - 1; i >= 0; i--)
nodes.Add(current[i]);
}
}
}
}
public ChiselModelComponent GetActiveModelOrCreate(ChiselModelComponent overrideModel = null)
{
if (overrideModel)
{
ActiveModel = overrideModel;
return overrideModel;
}
var activeModel = ActiveModel;
if (!activeModel)
{
// TODO: handle scene being locked by version control
activeModel = CreateNewModel();
ActiveModel = activeModel;
}
return activeModel;
}
public ChiselModelComponent CreateNewModel(Transform parent = null)
{
return ChiselComponentFactory.Create<ChiselModelComponent>(kDefaultModelName, parent);
}
public ChiselModelComponent FindModelInScene(Scene scene)
{
if (!scene.isLoaded ||
!scene.IsValid())
return null;
var allRootGameObjects = scene.GetRootGameObjects();
if (allRootGameObjects == null)
return null;
// We prever last model (more likely last created), so we iterate backwards
for (int n = allRootGameObjects.Length - 1; n >= 0; n--)
{
var rootGameObject = allRootGameObjects[n];
// Skip all gameobjects that are disabled
if (!rootGameObject.activeInHierarchy)
continue;
// Go through all it's models, this method returns the top most models first
var models = rootGameObject.GetComponentsInChildren<ChiselModelComponent>(includeInactive: false);
foreach (var model in models)
{
// Skip all inactive models
if (!IsSelectable(model))
continue;
return model;
}
}
return null;
}
public void CheckActiveModels()
{
// Go through all activeModels, which we store per scene, and make sure they still make sense
var removeScenes = ListPool<Scene>.Get();
var setSceneModels = DictionaryPool<Scene, ChiselModelComponent>.Get();
try
{
foreach (var pair in Instance.activeModels)
{
// If the scene is no longer loaded, remove it from our list
if (!pair.Key.isLoaded || !pair.Key.IsValid())
{
removeScenes.Add(pair.Key);
}
// Check if a current activeModel still exists
var sceneActiveModel = pair.Value;
if (!sceneActiveModel)
{
setSceneModels[pair.Key] = FindModelInScene(pair.Key);
//Instance.activeModels[pair.Key] = FindModelInScene(pair.Key);
continue;
}
// Check if a model has been moved to another scene, and correct this if it has
var gameObjectScene = sceneActiveModel.gameObject.scene;
if (gameObjectScene != pair.Key)
{
setSceneModels[pair.Key] = FindModelInScene(pair.Key);
setSceneModels[gameObjectScene] = FindModelInScene(pair.Key);
}
}
foreach (var scene in removeScenes)
Instance.activeModels.Remove(scene);
foreach (var pair in setSceneModels)
Instance.activeModels[pair.Key] = pair.Value;
}
finally
{
ListPool<Scene>.Release(removeScenes);
DictionaryPool<Scene, ChiselModelComponent>.Release(setSceneModels);
}
}
#if UNITY_EDITOR
public void InitializeOnLoad(Scene scene)
{
HideDebugVisualizationSurfaces(scene);
}
public void HideDebugVisualizationSurfaces()
{
var scene = SceneManager.GetActiveScene();
HideDebugVisualizationSurfaces(scene);
}
public void HideDebugVisualizationSurfaces(Scene scene)
{
foreach (var go in scene.GetRootGameObjects())
{
foreach (var model in go.GetComponentsInChildren<ChiselModelComponent>())
{
if (!model || !model.isActiveAndEnabled || model.generated == null)
continue;
model.generated.HideDebugVisualizationSurfaces();
}
}
}
public void OnWillFlushUndoRecord()
{
// Called on Undo, which happens when moving model to another scene
CheckActiveModels();
}
public void OnActiveSceneChanged(Scene _, Scene newScene)
{
if (Instance.activeModels.TryGetValue(newScene, out var activeModel) && IsSelectable(activeModel))
return;
Instance.activeModels[newScene] = FindModelInScene(newScene);
CheckActiveModels();
}
ChiselModelComponent GetSelectedModel()
{
var selectedGameObjects = UnityEditor.Selection.gameObjects;
if (selectedGameObjects == null ||
selectedGameObjects.Length == 1)
{
var selection = selectedGameObjects[0];
return selection.GetComponent<ChiselModelComponent>();
}
return null;
}
const string SetActiveModelMenuName = "GameObject/Set Active Model";
[UnityEditor.MenuItem(SetActiveModelMenuName, false, -100000)]
internal static void SetActiveModel()
{
var model = Instance.GetSelectedModel();
if (!model)
return;
Instance.ActiveModel = model;
}
[UnityEditor.MenuItem(SetActiveModelMenuName, true, -100000)]
internal static bool ValidateSetActiveModel()
{
var model = Instance.GetSelectedModel();
return (model != null);
}
#endif
}
}