HoverLight.cs 4.9 KB

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