HoverLight.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC
  5. {
  6. [ExecuteInEditMode]
  7. public class HoverLight : MonoBehaviour
  8. {
  9. // Three hover lights are supported at this time.
  10. private const int hoverLightCount = 3;
  11. private const int hoverLightDataSize = 2;
  12. private const string multiHoverLightKeyword = "_MULTI_HOVER_LIGHT";
  13. private static List<HoverLight> activeHoverLights = new List<HoverLight>(hoverLightCount);
  14. private static Vector4[] hoverLightData = new Vector4[hoverLightCount * hoverLightDataSize];
  15. private static int _HoverLightDataID;
  16. private static int lastHoverLightUpdate = -1;
  17. /// <summary>
  18. /// Specifies the Radius of the HoverLight effect
  19. /// </summary>
  20. public float Radius => radius;
  21. [Tooltip("Specifies the radius of the HoverLight effect")]
  22. [SerializeField]
  23. [Range(0.0f, 1.0f)]
  24. private float radius = 0.15f;
  25. /// <summary>
  26. /// Specifies the highlight color
  27. /// </summary>
  28. public Color Color => color;
  29. [Tooltip("Specifies the highlight color")]
  30. [SerializeField]
  31. private Color color = new Color(0.3f, 0.3f, 0.3f, 1.0f);
  32. private void OnEnable()
  33. {
  34. AddHoverLight(this);
  35. }
  36. private void OnDisable()
  37. {
  38. RemoveHoverLight(this);
  39. UpdateHoverLights(true);
  40. }
  41. #if UNITY_EDITOR
  42. private void Update()
  43. {
  44. if (Application.isPlaying)
  45. {
  46. return;
  47. }
  48. Initialize();
  49. UpdateHoverLights();
  50. }
  51. #endif // UNITY_EDITOR
  52. private void LateUpdate()
  53. {
  54. UpdateHoverLights();
  55. }
  56. private void OnDrawGizmosSelected()
  57. {
  58. if (!enabled)
  59. {
  60. return;
  61. }
  62. Gizmos.color = Color;
  63. Gizmos.DrawWireSphere(transform.position, Radius);
  64. Gizmos.DrawIcon(transform.position + Vector3.right * Radius, string.Empty, false);
  65. Gizmos.DrawIcon(transform.position + Vector3.left * Radius, string.Empty, false);
  66. Gizmos.DrawIcon(transform.position + Vector3.up * Radius, string.Empty, false);
  67. Gizmos.DrawIcon(transform.position + Vector3.down * Radius, string.Empty, false);
  68. Gizmos.DrawIcon(transform.position + Vector3.forward * Radius, string.Empty, false);
  69. Gizmos.DrawIcon(transform.position + Vector3.back * Radius, string.Empty, false);
  70. }
  71. private void AddHoverLight(HoverLight light)
  72. {
  73. if (activeHoverLights.Count >= hoverLightCount)
  74. {
  75. Debug.LogWarningFormat("Max hover light count ({0}) exceeded.", hoverLightCount);
  76. }
  77. activeHoverLights.Add(light);
  78. }
  79. private void RemoveHoverLight(HoverLight light)
  80. {
  81. activeHoverLights.Remove(light);
  82. }
  83. private void Initialize()
  84. {
  85. _HoverLightDataID = Shader.PropertyToID("_HoverLightData");
  86. }
  87. private void UpdateHoverLights(bool forceUpdate = false)
  88. {
  89. if (lastHoverLightUpdate == -1)
  90. {
  91. Initialize();
  92. }
  93. if (!forceUpdate && (Time.frameCount == lastHoverLightUpdate))
  94. {
  95. return;
  96. }
  97. if (activeHoverLights.Count > 1)
  98. {
  99. Shader.EnableKeyword(multiHoverLightKeyword);
  100. }
  101. else
  102. {
  103. Shader.DisableKeyword(multiHoverLightKeyword);
  104. }
  105. for (int i = 0; i < hoverLightCount; ++i)
  106. {
  107. HoverLight light = (i >= activeHoverLights.Count) ? null : activeHoverLights[i];
  108. int dataIndex = i * hoverLightDataSize;
  109. if (light)
  110. {
  111. hoverLightData[dataIndex] = new Vector4(light.transform.position.x,
  112. light.transform.position.y,
  113. light.transform.position.z,
  114. 1.0f);
  115. hoverLightData[dataIndex + 1] = new Vector4(light.Color.r,
  116. light.Color.g,
  117. light.Color.b,
  118. 1.0f / Mathf.Clamp(light.Radius, 0.001f, 1.0f));
  119. }
  120. else
  121. {
  122. hoverLightData[dataIndex] = Vector4.zero;
  123. }
  124. }
  125. Shader.SetGlobalVectorArray(_HoverLightDataID, hoverLightData);
  126. lastHoverLightUpdate = Time.frameCount;
  127. }
  128. }
  129. }