-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathShadowCaster2DEditor.cs
More file actions
139 lines (111 loc) · 5.17 KB
/
ShadowCaster2DEditor.cs
File metadata and controls
139 lines (111 loc) · 5.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
using UnityEditor.EditorTools;
using UnityEditor.Rendering.Universal.Path2D;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
internal class ShadowCasterPath : ScriptablePath
{
internal Bounds GetBounds()
{
ShadowCaster2D shadowCaster = (ShadowCaster2D)owner;
if (shadowCaster.TryGetComponent<Renderer>(out var m_Renderer))
{
return m_Renderer.bounds;
}
else
{
if (shadowCaster.TryGetComponent<Collider2D>(out var collider))
return collider.bounds;
}
return new Bounds(shadowCaster.transform.position, shadowCaster.transform.lossyScale);
}
public override void SetDefaultShape()
{
Clear();
Bounds bounds = GetBounds();
AddPoint(new ControlPoint(bounds.min));
AddPoint(new ControlPoint(new Vector3(bounds.min.x, bounds.max.y)));
AddPoint(new ControlPoint(bounds.max));
AddPoint(new ControlPoint(new Vector3(bounds.max.x, bounds.min.y)));
base.SetDefaultShape();
}
}
[CustomEditor(typeof(ShadowCaster2D))]
[CanEditMultipleObjects]
internal class ShadowCaster2DEditor : PathComponentEditor<ShadowCasterPath>
{
[EditorTool("Edit Shadow Caster Shape", typeof(ShadowCaster2D))]
class ShadowCaster2DShadowCasterShapeTool : ShadowCaster2DShapeTool { };
private static class Styles
{
public static GUIContent shadowMode = EditorGUIUtility.TrTextContent("Use Renderer Silhouette", "When this and Self Shadows are enabled, the Renderer's silhouette is considered part of the shadow. When this is enabled and Self Shadows disabled, the Renderer's silhouette is excluded from the shadow.");
public static GUIContent selfShadows = EditorGUIUtility.TrTextContent("Self Shadows", "When enabled, the Renderer casts shadows on itself.");
public static GUIContent castsShadows = EditorGUIUtility.TrTextContent("Casts Shadows", "Specifies if this renderer will cast shadows");
public static GUIContent sortingLayerPrefixLabel = EditorGUIUtility.TrTextContent("Target Sorting Layers", "Apply shadows to the specified sorting layers.");
}
SerializedProperty m_UseRendererSilhouette;
SerializedProperty m_CastsShadows;
SerializedProperty m_SelfShadows;
SortingLayerDropDown m_SortingLayerDropDown;
public void OnEnable()
{
m_UseRendererSilhouette = serializedObject.FindProperty("m_UseRendererSilhouette");
m_SelfShadows = serializedObject.FindProperty("m_SelfShadows");
m_CastsShadows = serializedObject.FindProperty("m_CastsShadows");
m_SortingLayerDropDown = new SortingLayerDropDown();
m_SortingLayerDropDown.OnEnable(serializedObject, "m_ApplyToSortingLayers");
}
public void ShadowCaster2DSceneGUI()
{
ShadowCaster2D shadowCaster = target as ShadowCaster2D;
Transform t = shadowCaster.transform;
Vector3[] shape = shadowCaster.shapePath;
Handles.color = Color.white;
for (int i = 0; i < shape.Length - 1; ++i)
{
Handles.DrawAAPolyLine(4, new Vector3[] { t.TransformPoint(shape[i]), t.TransformPoint(shape[i + 1]) });
}
if (shape.Length > 1)
Handles.DrawAAPolyLine(4, new Vector3[] { t.TransformPoint(shape[shape.Length - 1]), t.TransformPoint(shape[0]) });
}
public void ShadowCaster2DInspectorGUI<T>() where T : ShadowCaster2DShapeTool
{
DoEditButton<T>(PathEditorToolContents.icon, "Edit Shape");
DoPathInspector<T>();
DoSnappingInspector<T>();
}
public void OnSceneGUI()
{
if (m_CastsShadows.boolValue)
ShadowCaster2DSceneGUI();
}
public bool HasRenderer()
{
if (targets != null)
{
for (int i = 0; i < targets.Length; i++)
{
ShadowCaster2D shadowCaster = (ShadowCaster2D)targets[i];
if (shadowCaster.TryGetComponent<Renderer>(out var renderer))
return true;
}
}
return false;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
using (new EditorGUI.DisabledScope(!HasRenderer())) // Done to support multiedit
{
EditorGUILayout.PropertyField(m_UseRendererSilhouette, Styles.shadowMode);
}
EditorGUILayout.PropertyField(m_CastsShadows, Styles.castsShadows);
EditorGUILayout.PropertyField(m_SelfShadows, Styles.selfShadows);
m_SortingLayerDropDown.OnTargetSortingLayers(serializedObject, targets, Styles.sortingLayerPrefixLabel, null);
if (m_CastsShadows.boolValue)
ShadowCaster2DInspectorGUI<ShadowCaster2DShadowCasterShapeTool>();
serializedObject.ApplyModifiedProperties();
}
}
}