4 using System.Collections.Generic;
18 private float radius = 0.15f;
20 private Color color =
new Color(0.3f, 0.3f, 0.3f, 1.0f);
23 private const int hoverLightCount = 3;
24 private const int hoverLightDataSize = 2;
25 private static List<HoverLight> activeHoverLights =
new List<HoverLight>(hoverLightCount);
26 private static Vector4[] hoverLightData =
new Vector4[hoverLightCount * hoverLightDataSize];
27 private static int _HoverLightDataID;
28 private static int lastHoverLightUpdate = -1;
56 private void OnEnable()
61 private void OnDisable()
63 RemoveHoverLight(
this);
64 UpdateHoverLights(
true);
70 if (Application.isPlaying)
80 private void LateUpdate()
85 private void OnDrawGizmosSelected()
93 Gizmos.DrawWireSphere(transform.position, Radius);
94 Gizmos.DrawIcon(transform.position + Vector3.right * Radius,
string.Empty,
false);
95 Gizmos.DrawIcon(transform.position + Vector3.left * Radius,
string.Empty,
false);
96 Gizmos.DrawIcon(transform.position + Vector3.up * Radius,
string.Empty,
false);
97 Gizmos.DrawIcon(transform.position + Vector3.down * Radius,
string.Empty,
false);
98 Gizmos.DrawIcon(transform.position + Vector3.forward * Radius,
string.Empty,
false);
99 Gizmos.DrawIcon(transform.position + Vector3.back * Radius,
string.Empty,
false);
102 private static void AddHoverLight(
HoverLight light)
104 if (activeHoverLights.Count >= hoverLightCount)
106 Debug.LogWarningFormat(
"Max hover light count ({0}) exceeded.", hoverLightCount);
109 activeHoverLights.Add(light);
112 private static void RemoveHoverLight(
HoverLight light)
114 activeHoverLights.Remove(light);
117 private static void Initialize()
119 _HoverLightDataID = Shader.PropertyToID(
"_HoverLightData");
122 private static void UpdateHoverLights(
bool forceUpdate =
false)
124 if (lastHoverLightUpdate == -1)
129 if (!forceUpdate && (Time.frameCount == lastHoverLightUpdate))
134 if (activeHoverLights.Count > 1)
136 Shader.EnableKeyword(
"_MULTI_HOVER_LIGHT");
140 Shader.DisableKeyword(
"_MULTI_HOVER_LIGHT");
143 for (
int i = 0; i < hoverLightCount; ++i)
145 HoverLight light = (i >= activeHoverLights.Count) ? null : activeHoverLights[i];
146 int dataIndex = i * hoverLightDataSize;
150 hoverLightData[dataIndex] =
new Vector4(light.transform.position.x,
151 light.transform.position.y,
152 light.transform.position.z,
154 hoverLightData[dataIndex + 1] =
new Vector4(light.
Color.r,
161 hoverLightData[dataIndex] = Vector4.zero;
162 hoverLightData[dataIndex + 1] = Vector4.zero;
166 Shader.SetGlobalVectorArray(_HoverLightDataID, hoverLightData);
168 lastHoverLightUpdate = Time.frameCount;
176 private bool HasFrameBounds() {
return true; }
178 private Bounds OnGetFrameBounds()
181 return new Bounds(light.transform.position, Vector3.one * light.
Radius);