-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathProjectionRenderer.cs
More file actions
771 lines (683 loc) · 26.7 KB
/
Copy pathProjectionRenderer.cs
File metadata and controls
771 lines (683 loc) · 26.7 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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace LlockhamIndustries.Decals
{
/**
* This core component of the system. Attach this to empty gameObjects to create decals or omni-decals.
*/
[ExecuteInEditMode]
public class ProjectionRenderer : MonoBehaviour
{
//Properties
/**
* The instanced properties unique to this renderer. Each projection type will have different properties available to change. You should rarely need to get and set them as a group though.
* This is usually only used to copy all the instanced properties from one projection to another. Remember to call UpdateProperties() once your done modifiying a renderers properties.
*/
public ProjectionProperty[] Properties
{
get { return properties; }
set
{
if (value != null) properties = (ProjectionProperty[])value.Clone();
else properties = null;
MarkProperties();
}
}
/**
* Resets the instanced properties to match the default values of the selected projection. If your making further immediate changes leave UpdateImmediately as false and remember to call UpdateProperties() once your done.
* @param UpdateImmediately calls UpdateProperties() for you.
*/
public void ResetProperties(bool UpdateImmediately = false)
{
//Reset tiling / offset
tiling = Vector2.one;
offset = Vector2.zero;
//Reset masking
maskMethod = MaskMethod.DrawOnEverythingExcept;
masks = new bool[4];
//Reset properties
if (projection != null) properties = (ProjectionProperty[])projection.Properties.Clone();
else properties = null;
MarkProperties(UpdateImmediately);
}
/**
* Applies any changes made to the instanced properties of the renderer. Tiling, Offset, Masking & Projection specific properties all count as instanced properties.
* This should be called once after your done making all your changes to these properties, not once per property.
*/
public void UpdateProperties()
{
if (meshRenderer != null)
{
//Update our property block
if (marked && Projection != null)
{
UpdateRendererBlock(Properties, Projection.Properties);
meshRenderer.SetPropertyBlock(block);
//No longer needs to be updated
marked = false;
}
}
}
public void MarkProperties(bool UpdateImmediately = false)
{
marked = true;
if (UpdateImmediately && meshRenderer != null) UpdateProperties();
}
/**
* Modifies the float value (Such as emission strength) of a renderers projection specific properties.
* Remember to call UpdateProperties() once your done modifiying a renderers properties.
* @param PropertyIndex The index of the property being modified. If your not sure of the index look at the UI of your projectionRenderer, the instanced properties appear here in order. In all cases 0 is the albedo color and it's alpha is used for transparency.
* @param Float The value you wish to set the property too.
*/
public void SetFloat(int PropertyIndex, float Float)
{
if ((properties[PropertyIndex].type == PropertyType.Float || properties[PropertyIndex].type == PropertyType.Combo) && properties[PropertyIndex].value != Float)
{
//Adjust property
properties[PropertyIndex].value = Float;
properties[PropertyIndex].enabled = true;
//Mark renderers to update
MarkProperties();
}
}
/**
* Modifies the color value (Such as emission color) of a renderers projection specific properties.
* Remember to call UpdateProperties() once your done modifiying a renderers properties.
* @param PropertyIndex The index of the property being modified. If your not sure of the index look at the UI of your projectionRenderer, the instanced properties appear here in order. In all cases 0 is the albedo color and it's alpha is used for transparency.
* @param Color The value you wish to set the property too.
*/
public void SetColor(int PropertyIndex, Color Color)
{
if ((properties[PropertyIndex].type == PropertyType.Color || properties[PropertyIndex].type == PropertyType.Combo) && properties[PropertyIndex].color != Color)
{
//Adjust property
properties[PropertyIndex].color = Color;
properties[PropertyIndex].enabled = true;
//Mark renderers to update
MarkProperties();
}
}
//Tiling & Offset
/**
* The tiling of your projection. This can be used to sample a specific part of your projection (below 1), or have your projection tile (above 1).
* Remember to call UpdateProperties() once your done modifiying a renderers properties.
*/
public Vector2 Tiling
{
get { return tiling; }
set
{
if (tiling != value)
{
//Adjust property
tiling = value;
//Mark renderers to update
MarkProperties();
}
}
}
/**
* The offset of your projection. This allows you to offset where you begin sampling your projection.
* Remember to call UpdateProperties() once your done modifiying a renderers properties.
*/
public Vector2 Offset
{
get { return offset; }
set
{
if (offset != value)
{
//Adjust property
offset = value;
//Mark renderers to update
MarkProperties();
}
}
}
//Masking
/**
* Defines which masking method we should apply to this projection. Either "DrawOnEverythingExcept" or "OnlyDrawOn".
* Draw On Everything Except - will draw on all surface except those in the selected mask layers.
* Only Draw On - will only draw on surfaces that are part of the selected mask layers.
*/
public MaskMethod MaskMethod
{
get { return maskMethod; }
set
{
if (maskMethod != value)
{
//Adjust property
maskMethod = value;
//Mark renderers to update
MarkProperties();
}
}
}
/**
* Defines whether this projection is affected by the first masking layer.
* To add surfaces to this mask layer add a Mask component to a renderable gameObject and toggle on the appropriate mask layer.
*/
public bool MaskLayer1
{
get { return masks[0]; }
set
{
if (masks[0] != value)
{
//Adjust property
masks[0] = value;
//Mark renderers to update
MarkProperties();
}
}
}
/**
* Defines whether this projection is affected by the second masking layer.
* To add surfaces to this mask layer add a Mask component to a renderable gameObject and toggle on the appropriate mask layer.
*/
public bool MaskLayer2
{
get { return masks[1]; }
set
{
if (masks[1] != value)
{
//Adjust property
masks[1] = value;
//Mark renderers to update
MarkProperties();
}
}
}
/**
* Defines whether this projection is affected by the third masking layer.
* To add surfaces to this mask layer add a Mask component to a renderable gameObject and toggle on the appropriate mask layer.
*/
public bool MaskLayer3
{
get { return masks[2]; }
set
{
if (masks[2] != value)
{
//Adjust property
masks[2] = value;
//Mark renderers to update
MarkProperties();
}
}
}
/**
* Defines whether this projection is affected by the fourth masking layer.
* To add surfaces to this mask layer add a Mask component to a renderable gameObject and toggle on the appropriate mask layer.
*/
public bool MaskLayer4
{
get { return masks[3]; }
set
{
if (masks[3] != value)
{
//Adjust property
masks[3] = value;
//Mark renderers to update
MarkProperties();
}
}
}
//Projection
/**
* The projection your renderer will render.
* Changing the projection will reset all of your projections per-instance properties to the default value of the new projection.
*/
public Projection Projection
{
get
{
if (gameObject.activeInHierarchy && active != null) return active;
else return projection;
}
set
{
if (projection != value)
{
projection = value;
ChangeProjection();
}
}
}
public void ChangeProjection()
{
if (active != projection)
{
//Deregister old projection
if (gameObject.activeInHierarchy && enabled && active != null) Deregister();
//Set new projection
active = projection;
//Register new projection
if (gameObject.activeInHierarchy && enabled && active != null) Register();
//Reset tiling / offset
tiling = Vector2.one;
offset = Vector2.zero;
//Get new properties
if (active != null) Properties = active.Properties;
else Properties = null;
}
//Assign Projection to renderer
UpdateProjection();
}
public void UpdateProjection()
{
if (meshRenderer != null)
{
if (Projection != null && Projection.Valid)
{
//Enable renderer
meshRenderer.gameObject.SetActive(true);
//Apply materials
meshRenderer.sharedMaterial = Projection.Mat;
//Reset properties
UpdateRendererBlock(Properties, Projection.Properties);
meshRenderer.SetPropertyBlock(block);
//No longer needs to be updated
marked = false;
}
else
{
//Disable renderer
meshRenderer.gameObject.SetActive(false);
//Remove materials
meshRenderer.sharedMaterial = null;
//Reset properties
if (block != null) block.Clear();
meshRenderer.SetPropertyBlock(block);
}
}
}
private bool Register()
{
if (this != null)
{
#if UNITY_EDITOR
PrefabType prefabType = PrefabUtility.GetPrefabType(gameObject);
if (prefabType == PrefabType.ModelPrefab || prefabType == PrefabType.Prefab) return false;
#endif
return DynamicDecals.System.Register(this);
}
return false;
}
private void Deregister()
{
if (this != null)
{
#if UNITY_EDITOR
PrefabType prefabType = PrefabUtility.GetPrefabType(gameObject);
if (prefabType == PrefabType.ModelPrefab || prefabType == PrefabType.Prefab) return;
#endif
DynamicDecals.System.Deregister(this);
}
}
//Renderer
public MeshRenderer Renderer
{
get { return meshRenderer; }
}
private MaterialPropertyBlock block;
private bool marked = true;
public void InitializeRenderer(bool Active)
{
//Check if we already have a forward renderer
if (meshRenderer == null)
{
foreach (Transform child in transform)
{
if (child.name == "Renderer")
{
meshRenderer = child.GetComponent<MeshRenderer>();
break;
}
}
}
//If none could be found, create one
if (meshRenderer == null)
{
//Create renderer
GameObject go = new GameObject("Renderer");
go.transform.SetParent(transform, false);
go.layer = gameObject.layer;
//Hide renderer
go.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor | HideFlags.HideInHierarchy;
//Setup mesh filter
MeshFilter filter = go.AddComponent<MeshFilter>();
filter.sharedMesh = DynamicDecals.System.Cube;
//Setup mesh renderer
meshRenderer = go.AddComponent<MeshRenderer>();
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
meshRenderer.motionVectorGenerationMode = MotionVectorGenerationMode.Camera;
meshRenderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.BlendProbes;
//Hide editor outline
#if UNITY_EDITOR
EditorUtility.SetSelectedRenderState(meshRenderer, EditorSelectedRenderState.Hidden);
#endif
}
//Make sure renderer is enabled
meshRenderer.gameObject.SetActive(Active);
}
public void TerminateRenderer()
{
meshRenderer.gameObject.SetActive(false);
}
private void UpdateRendererBlock(ProjectionProperty[] Local, ProjectionProperty[] Global)
{
//Initialize / Clear block
if (block == null) block = new MaterialPropertyBlock();
else block.Clear();
//Update tiling / offset
block.SetVector(_TilingOffset, new Vector4(Tiling.x, Tiling.y, Offset.x, Offset.y));
//Masking
switch (maskMethod)
{
case MaskMethod.DrawOnEverythingExcept:
block.SetFloat(_MaskBase, 1);
Color except = Color.clear;
except.r = (masks[0]) ? 0 : 0.5f;
except.g = (masks[1]) ? 0 : 0.5f;
except.b = (masks[2]) ? 0 : 0.5f;
except.a = (masks[3]) ? 0 : 0.5f;
block.SetVector(_MaskLayers, except);
break;
case MaskMethod.OnlyDrawOn:
block.SetFloat(_MaskBase, 0);
Color only = Color.clear;
only.r = (masks[0]) ? 1 : 0.5f;
only.g = (masks[1]) ? 1 : 0.5f;
only.b = (masks[2]) ? 1 : 0.5f;
only.a = (masks[3]) ? 1 : 0.5f;
block.SetVector(_MaskLayers, only);
break;
}
//Update and apply our material property block
for (int i = 0; i < Local.Length; i++)
{
if (Local[i].type == PropertyType.Float)
{
float value = (Local[i].enabled) ? Local[i].value : Global[i].value;
block.SetFloat(Global[i].nameID, value);
}
if (Local[i].type == PropertyType.Color)
{
Color color = (Local[i].enabled) ? Local[i].color : Global[i].color;
block.SetColor(Global[i].nameID, color);
}
if (Local[i].type == PropertyType.Combo)
{
Color color = (Local[i].enabled) ? Local[i].color * Local[i].value : Global[i].color * Global[i].value;
block.SetColor(Global[i].nameID, color);
}
}
}
//Enable / Disable
private void OnEnable()
{
//Grab our IDs
_TilingOffset = Shader.PropertyToID("_TilingOffset");
_MaskBase = Shader.PropertyToID("_MaskBase");
_MaskLayers = Shader.PropertyToID("_MaskLayers");
Initialize();
}
private void OnDisable()
{
Terminate();
}
private void Initialize()
{
if (projection != null)
{
//Set new projection
active = projection;
//Register
bool registeredState = Register();
//Initialize renderer
InitializeRenderer(registeredState);
}
else InitializeRenderer(false);
//Assign Projection to renderer
UpdateProjection();
}
private void Terminate()
{
//Deregister projection
if (projection != null) Deregister();
//Terminate renderer
TerminateRenderer();
}
//Sub-Order
public ProjectionData Data
{
get { return data; }
set { data = value; }
}
/**
* Each projection is rendered before or after other projections. Each renderer has a sub-order within it's projection.
* MoveToTop moves this renderer in front of all other renderers rendering the same projection.
* Projection must be active (enabled) for this to have any effect.
*/
public void MoveToTop()
{
if (data != null)
{
data.MoveToTop(this);
}
}
/**
* Each projection is rendered before or after other projections. Each renderer has a sub-order within it's projection.
* MoveToBottom moves this renderer behind all other renderers rendering the same projection.
* Projection must be active (enabled) for this to have any effect.
*/
public void MoveToBottom()
{
if (data != null)
{
data.MoveToBottom(this);
}
}
//Pooling
/**
* The projection pool your renderer belongs too. Will return null if the renderer is not currently in any pool.
*/
public ProjectionPool Pool
{
get
{
if (poolItem != null) return poolItem.Pool;
else return null;
}
}
public PoolItem PoolItem
{
get { return poolItem; }
set { poolItem = value; }
}
/**
* Checks to see how much a point is intersecting with the projection bounds.
* The closer the point is to the center of the projection bounds, the higher the returned value will be.
* A perfectly intersecting point (ie. At the centre of the projection bounds) will return 1, while a non-intersecting point will return 0.
* We can use this method to cheaply determine how much overlap is occuring between projections, or to see if some other object would be projected onto and act accordingly.
* @param Point defines the point (in world space) to check.
*/
public float CheckIntersecting(Vector3 Point)
{
if (transform.lossyScale == Vector3.zero) return 0;
Vector3 localPoint = transform.InverseTransformPoint(Point);
return Mathf.Clamp01(2 * (0.5f - Mathf.Max(Mathf.Max(Mathf.Abs(localPoint.x), Mathf.Abs(localPoint.y)), Mathf.Abs(localPoint.z))));
}
//Destroy
/**
* Use this instead of GameObject.Destroy().
* If the renderer is part of a pool returns it back to the pool so it can be used again. Otherwise Destroys the object.
*/
public void Destroy()
{
if (poolItem != null) poolItem.Return();
else Destroy(gameObject);
}
#region Backing Fields
[SerializeField]
private Projection projection;
private Projection active;
[SerializeField]
private ProjectionProperty[] properties;
[SerializeField]
private Vector2 tiling;
[SerializeField]
private Vector2 offset;
[SerializeField]
protected MaskMethod maskMethod;
[SerializeField]
protected bool[] masks = new bool[4];
//Renderers
private MeshRenderer meshRenderer;
//Sub-Order
private ProjectionData data;
//Pooling
private PoolItem poolItem;
//Property Ids
public int _TilingOffset;
public int _MaskBase;
public int _MaskLayers;
#endregion
#region Editor
#if UNITY_EDITOR
//Duplicate check
[SerializeField]
private int instanceID = 0;
private void Awake()
{
if (!Application.isPlaying)
{
if (instanceID == 0)
{
instanceID = GetInstanceID();
}
else if (instanceID != GetInstanceID())
{
//Duplicate detected
instanceID = GetInstanceID();
//Clone properties
properties = (ProjectionProperty[])properties.Clone();
}
}
}
//Gizmo type
protected virtual SelectionGizmo Gizmo
{
get
{
if (projection != null)
{
switch (projection.ProjectionType)
{
case ProjectionType.Decal:
return SelectionGizmo.Cube;
case ProjectionType.OmniDecal:
return SelectionGizmo.Sphere;
}
}
return SelectionGizmo.Cube;
}
}
protected enum SelectionGizmo { Cube, Sphere };
//Gizmo rendering
public void OnDrawGizmos()
{
DrawGizmo(false);
}
public void OnDrawGizmosSelected()
{
DrawGizmo(true);
}
private void DrawGizmo(bool Selected)
{
if (isActiveAndEnabled)
{
//Decalare color and matrix
Color color = new Color(0.8f, 0.8f, 0.8f, 1.0f);
Gizmos.matrix = transform.localToWorldMatrix;
//Draw selection gizmo
switch (Gizmo)
{
case SelectionGizmo.Cube:
if (!Selected)
{
color.a = 0.0f;
Gizmos.color = color;
Gizmos.DrawCube(Vector3.zero, Vector3.one);
}
color.a = Selected ? 0.5f : 0.05f;
Gizmos.color = color;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
Gizmos.DrawLine(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0.5f, 0.5f, -0.5f));
Gizmos.DrawLine(new Vector3(0.5f, -0.5f, -0.5f), new Vector3(-0.5f, 0.5f, -0.5f));
break;
case SelectionGizmo.Sphere:
if (!Selected)
{
color.a = 0.0f;
Gizmos.color = color;
Gizmos.DrawSphere(Vector3.zero, 1);
}
color.a = Selected ? 0.5f : 0.05f;
Gizmos.color = color;
Gizmos.DrawWireSphere(Vector3.zero, 0.5f);
break;
}
}
}
#endif
#endregion
}
[System.Serializable]
public struct ProjectionProperty
{
public string name;
public int nameID;
public PropertyType type;
public Color color;
public float value;
public bool enabled;
//Constructors
public ProjectionProperty(string Name, int ID, Color Color)
{
name = Name;
nameID = ID;
type = PropertyType.Color;
color = Color;
value = 0;
enabled = false;
}
public ProjectionProperty(string Name, int ID, float Value)
{
name = Name;
nameID = ID;
type = PropertyType.Float;
color = Color.white;
value = Value;
enabled = false;
}
public ProjectionProperty(string Name, int ID, Color Color, float Value)
{
name = Name;
nameID = ID;
type = PropertyType.Combo;
color = Color;
value = Value;
enabled = false;
}
}
public enum PropertyType { Color, Float, Combo }
}