-
Notifications
You must be signed in to change notification settings - Fork 867
Expand file tree
/
Copy pathDecalProjector.cs
More file actions
438 lines (393 loc) · 13.5 KB
/
DecalProjector.cs
File metadata and controls
438 lines (393 loc) · 13.5 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
using System;
using UnityEditor;
using UnityEngine.Serialization;
namespace UnityEngine.Rendering.Universal
{
/// <summary>The scaling mode to apply to decals that use the Decal Projector.</summary>
public enum DecalScaleMode
{
/// <summary>Ignores the transformation hierarchy and uses the scale values in the Decal Projector component directly.</summary>
ScaleInvariant,
/// <summary>Multiplies the lossy scale of the Transform with the Decal Projector's own scale then applies this to the decal.</summary>
[InspectorName("Inherit from Hierarchy")]
InheritFromHierarchy,
}
/// <summary>
/// Decal Projector component.
/// </summary>
[CoreRPHelpURL("renderer-feature-decal", "com.unity.render-pipelines.universal")]
[ExecuteAlways]
#if UNITY_EDITOR
[CanEditMultipleObjects]
#endif
[AddComponentMenu("Rendering/URP Decal Projector")]
public partial class DecalProjector : MonoBehaviour, ISerializationCallbackReceiver
{
internal delegate void DecalProjectorAction(DecalProjector decalProjector);
internal static event DecalProjectorAction onDecalAdd;
internal static event DecalProjectorAction onDecalRemove;
internal static event DecalProjectorAction onDecalPropertyChange;
internal static event Action onAllDecalPropertyChange;
internal static event DecalProjectorAction onDecalMaterialChange;
internal static Material defaultMaterial { get; set; }
internal static bool isSupported => onDecalAdd != null;
internal DecalEntity decalEntity { get; set; }
[SerializeField]
private Material m_Material = null;
/// <summary>
/// The material used by the decal.
/// </summary>
public Material material
{
get
{
return m_Material;
}
set
{
m_Material = value;
OnValidate();
}
}
[SerializeField]
private float m_DrawDistance = 1000.0f;
/// <summary>
/// Distance from camera at which the Decal is not rendered anymore.
/// </summary>
public float drawDistance
{
get
{
return m_DrawDistance;
}
set
{
m_DrawDistance = Mathf.Max(0f, value);
OnValidate();
}
}
[SerializeField]
[Range(0, 1)]
private float m_FadeScale = 0.9f;
/// <summary>
/// Percent of the distance from the camera at which this Decal start to fade off.
/// </summary>
public float fadeScale
{
get
{
return m_FadeScale;
}
set
{
m_FadeScale = Mathf.Clamp01(value);
OnValidate();
}
}
[SerializeField]
[Range(0, 180)]
private float m_StartAngleFade = 180.0f;
/// <summary>
/// Angle between decal backward orientation and vertex normal of receiving surface at which the Decal start to fade off.
/// </summary>
public float startAngleFade
{
get
{
return m_StartAngleFade;
}
set
{
m_StartAngleFade = Mathf.Clamp(value, 0.0f, 180.0f);
OnValidate();
}
}
[SerializeField]
[Range(0, 180)]
private float m_EndAngleFade = 180.0f;
/// <summary>
/// Angle between decal backward orientation and vertex normal of receiving surface at which the Decal end to fade off.
/// </summary>
public float endAngleFade
{
get
{
return m_EndAngleFade;
}
set
{
m_EndAngleFade = Mathf.Clamp(value, m_StartAngleFade, 180.0f);
OnValidate();
}
}
[SerializeField]
private Vector2 m_UVScale = new Vector2(1, 1);
/// <summary>
/// Tilling of the UV of the projected texture.
/// </summary>
public Vector2 uvScale
{
get
{
return m_UVScale;
}
set
{
m_UVScale = value;
OnValidate();
}
}
[SerializeField]
private Vector2 m_UVBias = new Vector2(0, 0);
/// <summary>
/// Offset of the UV of the projected texture.
/// </summary>
public Vector2 uvBias
{
get
{
return m_UVBias;
}
set
{
m_UVBias = value;
OnValidate();
}
}
[SerializeField] RenderingLayerMask m_RenderingLayerMask = RenderingLayerMask.defaultRenderingLayerMask;
/// <summary>
/// The layer of the decal.
/// </summary>
public RenderingLayerMask renderingLayerMask
{
get => m_RenderingLayerMask;
set => m_RenderingLayerMask = value;
}
[SerializeField]
private DecalScaleMode m_ScaleMode = DecalScaleMode.ScaleInvariant;
/// <summary>
/// The scaling mode to apply to decals that use this Decal Projector.
/// </summary>
public DecalScaleMode scaleMode
{
get => m_ScaleMode;
set
{
m_ScaleMode = value;
OnValidate();
}
}
[SerializeField]
internal Vector3 m_Offset = new Vector3(0, 0, 0.5f);
/// <summary>
/// Change the offset position.
/// Do not expose: Could be changed by the inspector when manipulating the gizmo.
/// </summary>
public Vector3 pivot
{
get
{
return m_Offset;
}
set
{
m_Offset = value;
OnValidate();
}
}
[SerializeField]
internal Vector3 m_Size = new Vector3(1, 1, 1);
/// <summary>
/// The size of the projection volume.
/// </summary>
public Vector3 size
{
get
{
return m_Size;
}
set
{
m_Size = value;
OnValidate();
}
}
[SerializeField]
[Range(0, 1)]
private float m_FadeFactor = 1.0f;
/// <summary>
/// Controls the transparency of the decal.
/// </summary>
public float fadeFactor
{
get
{
return m_FadeFactor;
}
set
{
m_FadeFactor = Mathf.Clamp01(value);
OnValidate();
}
}
#if UNITY_EDITOR
[SerializeField]
private bool m_VisibleInScene = true;
public bool visibleInScene
{
get
{
return m_VisibleInScene;
}
set
{
m_VisibleInScene = value;
OnValidate();
}
}
#endif
private Material m_OldMaterial = null;
private float m_OldDrawDistance = 1000.0f;
private float m_OldFadeScale = 0.9f;
private float m_OldStartAngleFade = 180.0f;
private float m_OldEndAngleFade = 180.0f;
private Vector2 m_OldUVScale = new Vector2(1, 1);
private Vector2 m_OldUVBias = new Vector2(0, 0);
private DecalScaleMode m_OldScaleMode = DecalScaleMode.ScaleInvariant;
private Vector3 m_OldOffset = new Vector3(0, 0, 0.5f);
private Vector3 m_OldSize = new Vector3(1, 1, 1);
private float m_OldFadeFactor = 1.0f;
/// <summary>A scale that should be used for rendering and handles.</summary>
internal Vector3 effectiveScale => m_ScaleMode == DecalScaleMode.InheritFromHierarchy ? transform.lossyScale : Vector3.one;
/// <summary>current size in a way the DecalSystem will be able to use it</summary>
internal Vector3 decalSize => new Vector3(m_Size.x, m_Size.z, m_Size.y);
/// <summary>current size in a way the DecalSystem will be able to use it</summary>
internal Vector3 decalOffset => new Vector3(m_Offset.x, -m_Offset.z, m_Offset.y);
/// <summary>current uv parameters in a way the DecalSystem will be able to use it</summary>
internal Vector4 uvScaleBias => new Vector4(m_UVScale.x, m_UVScale.y, m_UVBias.x, m_UVBias.y);
void InitMaterial()
{
if (m_Material == null)
{
#if UNITY_EDITOR
m_Material = defaultMaterial;
#endif
}
}
void OnEnable()
{
InitMaterial();
m_OldMaterial = m_Material;
onDecalAdd?.Invoke(this);
#if UNITY_EDITOR
// Handle scene visibility
UnityEditor.SceneVisibilityManager.visibilityChanged += UpdateDecalVisibility;
#endif
}
#if UNITY_EDITOR
void UpdateDecalVisibility()
{
// Change serialized property when decal is hidden in scene
visibleInScene = !UnityEditor.SceneVisibilityManager.instance.IsHidden(gameObject);
// Force proeprty update that will look at visibleInScene to adjust scene culling mask
onDecalPropertyChange?.Invoke(this);
}
#endif
void OnDisable()
{
onDecalRemove?.Invoke(this);
#if UNITY_EDITOR
UnityEditor.SceneVisibilityManager.visibilityChanged -= UpdateDecalVisibility;
#endif
}
internal void OnValidate()
{
if (!isActiveAndEnabled)
return;
if (m_Material != m_OldMaterial)
{
onDecalMaterialChange?.Invoke(this);
m_OldMaterial = m_Material;
}
else
onDecalPropertyChange?.Invoke(this);
m_OldDrawDistance = m_DrawDistance;
m_OldFadeScale = m_FadeScale;
m_OldStartAngleFade = m_StartAngleFade;
m_OldEndAngleFade = m_EndAngleFade;
m_OldUVScale = m_UVScale;
m_OldUVBias = m_UVBias;
m_OldScaleMode = m_ScaleMode;
m_OldOffset = m_Offset;
m_OldSize = m_Size;
m_OldFadeFactor = m_FadeFactor;
}
void OnDidApplyAnimationProperties()
{
// Needed to be able to update state properly for animated serialized-properties.
if (m_OldMaterial != m_Material ||
Mathf.Abs(m_OldDrawDistance - m_DrawDistance) > Mathf.Epsilon ||
Mathf.Abs(m_OldFadeScale - m_FadeScale) > Mathf.Epsilon ||
Mathf.Abs(m_OldStartAngleFade - m_StartAngleFade) > Mathf.Epsilon ||
Mathf.Abs(m_OldEndAngleFade - m_EndAngleFade) > Mathf.Epsilon ||
m_OldUVScale != m_UVScale ||
m_OldUVBias != m_UVBias ||
m_OldScaleMode != m_ScaleMode ||
m_OldOffset != m_Offset ||
m_OldSize != m_Size ||
Mathf.Abs(m_OldFadeFactor - m_FadeFactor) > Mathf.Epsilon)
{
OnValidate();
}
}
/// <summary>
/// Checks if material is valid for rendering decals.
/// </summary>
/// <returns>True if material is valid.</returns>
public bool IsValid()
{
if (material == null)
return false;
if (material.FindPass(DecalShaderPassNames.DBufferProjector) != -1)
return true;
if (material.FindPass(DecalShaderPassNames.DecalProjectorForwardEmissive) != -1)
return true;
if (material.FindPass(DecalShaderPassNames.DecalScreenSpaceProjector) != -1)
return true;
if (material.FindPass(DecalShaderPassNames.DecalGBufferProjector) != -1)
return true;
return false;
}
internal static void UpdateAllDecalProperties()
{
onAllDecalPropertyChange?.Invoke();
}
enum Version
{
Initial,
RenderingLayerMask,
Count
}
[SerializeField] Version version = Version.Count;
// This piece of code is needed because some objects could have been created before existence of Version enum
/// <summary>OnBeforeSerialize needed to handle migration before the versioning system was in place.</summary>
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
if (version == Version.Count) // serializing a newly created object
version = Version.Count - 1; // mark as up to date
}
/// <summary>OnAfterDeserialize needed to handle migration before the versioning system was in place.</summary>
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (version == Version.Count) // deserializing and object without version
version = Version.Initial; // reset to run the migration
if (version < Version.RenderingLayerMask)
{
#pragma warning disable 618 // Obsolete warning
m_RenderingLayerMask = m_DecalLayerMask;
#pragma warning restore 618 // Obsolete warning
version = Version.RenderingLayerMask;
}
}
}
}