-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathChiselModelComponent.cs
More file actions
415 lines (351 loc) · 17 KB
/
ChiselModelComponent.cs
File metadata and controls
415 lines (351 loc) · 17 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Chisel.Core;
using UnityEngine;
using UnityEngine.Rendering;
namespace Chisel.Components
{
// Can't use UnityEditor.UnwrapParam since it's not marked as Serializable
[Serializable]
public sealed class SerializableUnwrapParam
{
public const string kAngleErrorName = nameof(angleError);
public const string kAreaErrorName = nameof(areaError);
public const string kHardAngleName = nameof(hardAngle);
public const string kPackMarginPixelsName = nameof(packMarginPixels);
public const float minAngleError = 0.001f;
public const float maxAngleError = 1.000f;
public const float minAreaError = 0.001f;
public const float maxAreaError = 1.000f;
public const float minHardAngle = 1;
public const float maxHardAngle = 179;
public const float minPackMargin = 1;
public const float maxPackMargin = 256;
[Range(minAngleError, maxAngleError)] public float angleError;
[Range(minAreaError, maxAreaError )] public float areaError;
[Range(minHardAngle, maxHardAngle )] public float hardAngle;
[Range(minPackMargin, maxPackMargin)] public float packMarginPixels;
#if UNITY_EDITOR
public static float GetDefaultAngleError()
{
UnityEditor.UnwrapParam.SetDefaults(out var defaults);
return defaults.angleError;
}
public static float GetDefaultAreaError()
{
UnityEditor.UnwrapParam.SetDefaults(out var defaults);
return defaults.areaError;
}
public static float GetDefaultHardAngle()
{
UnityEditor.UnwrapParam.SetDefaults(out var defaults);
return defaults.hardAngle;
}
public static float GetDefaultPackMarginPixels()
{
UnityEditor.UnwrapParam.SetDefaults(out var defaults);
return defaults.packMargin * 256;
}
public void Reset()
{
UnityEditor.UnwrapParam.SetDefaults(out var defaults);
angleError = defaults.angleError;
areaError = defaults.areaError;
hardAngle = defaults.hardAngle;
packMarginPixels = defaults.packMargin * 256;
}
#endif
}
[Serializable]
public sealed class ChiselGeneratedColliderSettings
{
public const string kIsTriggerName = nameof(isTrigger);
public const string kConvexName = nameof(convex);
public bool isTrigger;
public bool convex;
// If the cookingOptions are not the default values it would force a full slow rebake later,
// even if we already did a Bake in a job
public const string kCookingOptionsName = nameof(cookingOptions);
// public const string kSkinWidthName = nameof(skinWidth);
public MeshColliderCookingOptions cookingOptions;
// public float skinWidth;
public void Reset()
{
isTrigger = false;
convex = false;
cookingOptions = (MeshColliderCookingOptions)(2|4|8);
//skinWidth = 0.01f;
}
}
[Serializable]
public sealed class ChiselGeneratedRenderSettings
{
public const string kUVGenerationSettingsName = nameof(uvGenerationSettings);
public const string kMotionVectorGenerationModeName = nameof(motionVectorGenerationMode);
public const string kAllowOcclusionWhenDynamicName = nameof(allowOcclusionWhenDynamic);
public const string kRenderingLayerMaskName = nameof(renderingLayerMask);
public const string kReflectionProbeUsageName = nameof(reflectionProbeUsage);
public const string kLightProbeUsageName = nameof(lightProbeUsage);
public const string kLightProbeVolumeOverrideName = nameof(lightProbeProxyVolumeOverride);
public const string kProbeAnchorName = nameof(probeAnchor);
public const string kReceiveGIName = nameof(receiveGI);
public const string kSubtractiveWorkflowName = nameof(subtractiveWorkflow);
public const string kNormalSmoothingName = nameof(normalSmoothing);
public const string kNormalSmoothingAngleName = nameof(normalSmoothingAngle);
#if UNITY_EDITOR
public const string kLightmapParametersName = nameof(lightmapParameters);
public const string kImportantGIName = nameof(importantGI);
public const string kOptimizeUVsName = nameof(optimizeUVs);
public const string kIgnoreNormalsForChartDetectionName = nameof(ignoreNormalsForChartDetection);
public const string kScaleInLightmapName = nameof(scaleInLightmap);
public const string kAutoUVMaxDistanceName = nameof(autoUVMaxDistance);
public const string kAutoUVMaxAngleName = nameof(autoUVMaxAngle);
public const string kMinimumChartSizeName = nameof(minimumChartSize);
public const string kStitchLightmapSeamsName = nameof(stitchLightmapSeams);
#endif
public GameObject lightProbeProxyVolumeOverride;
public Transform probeAnchor;
public MotionVectorGenerationMode motionVectorGenerationMode = MotionVectorGenerationMode.Object;
public ReflectionProbeUsage reflectionProbeUsage = ReflectionProbeUsage.BlendProbes;
public LightProbeUsage lightProbeUsage = LightProbeUsage.BlendProbes;
public bool allowOcclusionWhenDynamic = true;
public uint renderingLayerMask = ~(uint)0;
public ReceiveGI receiveGI = ReceiveGI.LightProbes;
public bool subtractiveWorkflow = false;
public bool normalSmoothing = false;
[Range(0, 180)] public float normalSmoothingAngle = 45.0f;
#if UNITY_EDITOR
// SerializedObject access Only
[SerializeField]
UnityEditor.LightmapParameters lightmapParameters = null; // TODO: figure out how to apply this, safely, using SerializedObject
[SerializeField]
bool importantGI = false;
[SerializeField]
bool optimizeUVs = false; // "Preserve UVs"
[SerializeField]
bool ignoreNormalsForChartDetection = false;
[SerializeField]
float autoUVMaxDistance = 0.5f;
[SerializeField]
float autoUVMaxAngle = 89;
[SerializeField]
int minimumChartSize = 4;
[NonSerialized]
internal bool serializedObjectFieldsDirty = true;
public void SetDirty() { serializedObjectFieldsDirty = true; }
public UnityEditor.LightmapParameters LightmapParameters { get { return lightmapParameters; } set { lightmapParameters = value; serializedObjectFieldsDirty = true; } }
public bool ImportantGI { get { return importantGI; } set { importantGI = value; serializedObjectFieldsDirty = true; } }
public bool OptimizeUVs { get { return optimizeUVs; } set { optimizeUVs = value; serializedObjectFieldsDirty = true; } }
public bool IgnoreNormalsForChartDetection {get { return ignoreNormalsForChartDetection; } set { ignoreNormalsForChartDetection = value; serializedObjectFieldsDirty = true; } }
public float AutoUVMaxDistance {get { return autoUVMaxDistance; } set { autoUVMaxDistance = value; serializedObjectFieldsDirty = true; } }
public float AutoUVMaxAngle {get { return autoUVMaxAngle; } set { autoUVMaxAngle = value; serializedObjectFieldsDirty = true; } }
public int MinimumChartSize {get { return minimumChartSize; } set { minimumChartSize = value; serializedObjectFieldsDirty = true; } }
// SerializedObject access Only
public bool stitchLightmapSeams = false;
public float scaleInLightmap = 1.0f;
public SerializableUnwrapParam uvGenerationSettings;
#endif
public void Reset()
{
lightProbeProxyVolumeOverride = null;
probeAnchor = null;
motionVectorGenerationMode = MotionVectorGenerationMode.Object;
reflectionProbeUsage = ReflectionProbeUsage.BlendProbes;
lightProbeUsage = LightProbeUsage.BlendProbes;
allowOcclusionWhenDynamic = true;
renderingLayerMask = ~(uint)0;
receiveGI = ReceiveGI.LightProbes;
subtractiveWorkflow = false;
normalSmoothing = false;
normalSmoothingAngle = 45.0f;
#if UNITY_EDITOR
lightmapParameters = new UnityEditor.LightmapParameters();
importantGI = false;
optimizeUVs = false;
ignoreNormalsForChartDetection = false;
scaleInLightmap = 1.0f;
autoUVMaxDistance = 0.5f;
autoUVMaxAngle = 89;
minimumChartSize = 4;
stitchLightmapSeams = false;
if (uvGenerationSettings == null)
{
uvGenerationSettings = new SerializableUnwrapParam();
UnityEditor.UnwrapParam.SetDefaults(out var defaults);
uvGenerationSettings.angleError = defaults.angleError;
uvGenerationSettings.areaError = defaults.areaError;
uvGenerationSettings.hardAngle = defaults.hardAngle;
uvGenerationSettings.packMarginPixels = defaults.packMargin * 256;
}
#endif
}
}
[ExecuteInEditMode, HelpURL(kDocumentationBaseURL + kNodeTypeName + kDocumentationExtension)]
[DisallowMultipleComponent, AddComponentMenu("Chisel/" + kNodeTypeName)]
public sealed class ChiselModelComponent : ChiselNodeComponent
{
public const string kRenderSettingsName = nameof(renderSettings);
public const string kColliderSettingsName = nameof(colliderSettings);
public const string kCreateRenderComponentsName = nameof(CreateRenderComponents);
public const string kCreateColliderComponentsName = nameof(CreateColliderComponents);
public const string kAutoRebuildUVsName = nameof(AutoRebuildUVs);
public const string kVertexChannelMaskName = nameof(VertexChannelMask);
public const string kNodeTypeName = "Model";
public override string ChiselNodeTypeName { get { return kNodeTypeName; } }
public ChiselGeneratedRenderSettings RenderSettings { get { return renderSettings; } }
public ChiselGeneratedColliderSettings ColliderSettings { get { return colliderSettings; } }
public override CSGTreeNode TopTreeNode { get { return Node; } protected set { Node = (CSGTree)value; } }
public override bool IsContainer { get { return IsActive; } }
public bool IsInitialized { get; private set; } = false;
public bool IsDefaultModel { get; internal set; } = false;
[HideInInspector] public ChiselGeneratedObjects generated;
[HideInInspector] public CSGTree Node;
public ChiselGeneratedColliderSettings colliderSettings;
public ChiselGeneratedRenderSettings renderSettings;
// TODO: put all bools in flags (makes it harder to work with in the ModelEditor though)
public bool CreateRenderComponents = true;
public bool CreateColliderComponents = true;
public bool AutoRebuildUVs = true;
public VertexChannelFlags VertexChannelMask = VertexChannelFlags.All;
public ChiselModelComponent() : base() { }
// Will show a warning icon in hierarchy when generator has a problem (do not make this method slow, it is called a lot!)
public override void GetMessages(IChiselMessageHandler messages)
{
// TODO: improve warning messages
const string kModelHasNoChildrenMessage = kNodeTypeName + " has no children and will not have an effect";
const string kFailedToGenerateNodeMessage = "Failed to generate internal representation of " + kNodeTypeName + "\nThe model might not have any (active) brushes inside it.";
//const string kChildNodesWithProblems = kNodeTypeName + " has children with errors";
if (!Node.Valid)
messages.Warning(kFailedToGenerateNodeMessage);
// A model makes no sense without any children
if (transform.childCount == 0)
{
messages.Warning(kModelHasNoChildrenMessage);
} else
{
if (transform.GetComponentInChildren<ChiselNodeComponent>() == null)
{
messages.Warning(kModelHasNoChildrenMessage);
}
}
//if (invalidChildNodes.Count > 0)
// messages.Warning(kChildNodesWithProblems);
}
public override void SetDirty() { if (Node.Valid) Node.SetDirty(); }
protected override void OnCleanup()
{
ModelSettingsStore.Remove(GetInstanceID());
if (generated != null)
{
if (!this && generated.generatedDataContainer)
generated.DestroyWithUndo();
}
base.OnCleanup();
}
internal override CSGTreeNode RebuildTreeNodes()
{
if (Node.Valid)
Debug.LogWarning($"{nameof(ChiselModelComponent)} already has a treeNode, but trying to create a new one?", this);
var instanceID = GetInstanceID();
Node = CSGTree.Create(instanceID: instanceID);
return Node;
}
public override void OnInitialize()
{
base.OnInitialize();
if (colliderSettings == null)
{
colliderSettings = new ChiselGeneratedColliderSettings();
colliderSettings.Reset();
}
if (renderSettings == null)
{
renderSettings = new ChiselGeneratedRenderSettings();
renderSettings.Reset();
}
UpdateModelSettingsLookup();
if (generated != null &&
!generated.generatedDataContainer)
generated.Destroy();
generated ??= ChiselGeneratedObjects.Create(gameObject);
// TODO: move out of here
#if !UNITY_EDITOR
if (generated != null && generated.meshRenderers != null)
{
foreach(var renderable in generated.renderables)
{
renderable.meshRenderer.forceRenderingOff = true;
renderable.meshRenderer.enabled = renderable.sharedMesh.vertexCount == 0;
}
}
#endif
// Legacy solution
if (!IsDefaultModel &&
name == ChiselModelManager.kGeneratedDefaultModelName)
IsDefaultModel = true;
IsInitialized = true;
}
void MarkAllBrushesDirty()
{
if (!Node.Valid)
return;
var stack = new Stack<CSGTreeNode>();
stack.Push(Node);
while (stack.Count > 0)
{
var current = stack.Pop();
if (!current.Valid)
continue;
switch (current.Type)
{
case CSGNodeType.Brush:
((CSGTreeBrush)current).SetDirty();
break;
case CSGNodeType.Branch:
case CSGNodeType.Tree:
for (int i = 0; i < current.Count; i++)
stack.Push(current[i]);
break;
}
}
}
internal void UpdateModelSettingsLookup()
{
if (renderSettings == null)
{
renderSettings = new ChiselGeneratedRenderSettings();
renderSettings.Reset();
}
ModelSettingsStore.Set(GetInstanceID(), new ModelSettings
{
SubtractiveWorkflow = renderSettings.subtractiveWorkflow,
NormalSmoothing = renderSettings.normalSmoothing,
NormalSmoothingAngle = renderSettings.normalSmoothingAngle
});
SetDirty();
MarkAllBrushesDirty();
if (ChiselModelManager.Instance != null)
ChiselModelManager.Instance.UpdateModels();
}
protected override void OnValidateState()
{
base.OnValidateState();
UpdateModelSettingsLookup();
}
public void SyncModelSettingsStore()
{
UpdateModelSettingsLookup();
}
#if UNITY_EDITOR
// TODO: remove from here, shouldn't be public
public MaterialPropertyBlock materialPropertyBlock;
public VisibilityState VisibilityState
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return generated.visibilityState; }
}
#endif
}
}