Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.4 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Highlights the controller when hovering over interactables
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class ControllerHoverHighlight : MonoBehaviour
  12. {
  13. public Material highLightMaterial;
  14. public bool fireHapticsOnHightlight = true;
  15. private Hand hand;
  16. private MeshRenderer bodyMeshRenderer;
  17. private MeshRenderer trackingHatMeshRenderer;
  18. private SteamVR_RenderModel renderModel;
  19. private bool renderModelLoaded = false;
  20. SteamVR_Events.Action renderModelLoadedAction;
  21. //-------------------------------------------------
  22. void Start()
  23. {
  24. hand = GetComponentInParent<Hand>();
  25. }
  26. //-------------------------------------------------
  27. void Awake()
  28. {
  29. renderModelLoadedAction = SteamVR_Events.RenderModelLoadedAction( OnRenderModelLoaded );
  30. }
  31. //-------------------------------------------------
  32. void OnEnable()
  33. {
  34. renderModelLoadedAction.enabled = true;
  35. }
  36. //-------------------------------------------------
  37. void OnDisable()
  38. {
  39. renderModelLoadedAction.enabled = false;
  40. }
  41. //-------------------------------------------------
  42. private void OnHandInitialized( int deviceIndex )
  43. {
  44. renderModel = gameObject.AddComponent<SteamVR_RenderModel>();
  45. renderModel.SetDeviceIndex( deviceIndex );
  46. renderModel.updateDynamically = false;
  47. }
  48. //-------------------------------------------------
  49. private void OnRenderModelLoaded( SteamVR_RenderModel renderModel, bool success )
  50. {
  51. if ( renderModel != this.renderModel )
  52. {
  53. return;
  54. }
  55. Transform bodyTransform = transform.Find( "body" );
  56. if ( bodyTransform != null )
  57. {
  58. bodyTransform.gameObject.layer = gameObject.layer;
  59. bodyTransform.gameObject.tag = gameObject.tag;
  60. bodyMeshRenderer = bodyTransform.GetComponent<MeshRenderer>();
  61. bodyMeshRenderer.material = highLightMaterial;
  62. bodyMeshRenderer.enabled = false;
  63. }
  64. Transform trackingHatTransform = transform.Find( "trackhat" );
  65. if ( trackingHatTransform != null )
  66. {
  67. trackingHatTransform.gameObject.layer = gameObject.layer;
  68. trackingHatTransform.gameObject.tag = gameObject.tag;
  69. trackingHatMeshRenderer = trackingHatTransform.GetComponent<MeshRenderer>();
  70. trackingHatMeshRenderer.material = highLightMaterial;
  71. trackingHatMeshRenderer.enabled = false;
  72. }
  73. foreach ( Transform child in transform )
  74. {
  75. if ( ( child.name != "body" ) && ( child.name != "trackhat" ) )
  76. {
  77. Destroy( child.gameObject );
  78. }
  79. }
  80. renderModelLoaded = true;
  81. }
  82. //-------------------------------------------------
  83. private void OnParentHandHoverBegin( Interactable other )
  84. {
  85. if ( !this.isActiveAndEnabled )
  86. {
  87. return;
  88. }
  89. if ( other.transform.parent != transform.parent )
  90. {
  91. ShowHighlight();
  92. }
  93. }
  94. //-------------------------------------------------
  95. private void OnParentHandHoverEnd( Interactable other )
  96. {
  97. HideHighlight();
  98. }
  99. //-------------------------------------------------
  100. private void OnParentHandInputFocusAcquired()
  101. {
  102. if ( !this.isActiveAndEnabled )
  103. {
  104. return;
  105. }
  106. if ( hand.hoveringInteractable && hand.hoveringInteractable.transform.parent != transform.parent )
  107. {
  108. ShowHighlight();
  109. }
  110. }
  111. //-------------------------------------------------
  112. private void OnParentHandInputFocusLost()
  113. {
  114. HideHighlight();
  115. }
  116. //-------------------------------------------------
  117. public void ShowHighlight()
  118. {
  119. if ( renderModelLoaded == false )
  120. {
  121. return;
  122. }
  123. if ( fireHapticsOnHightlight )
  124. {
  125. hand.controller.TriggerHapticPulse( 500 );
  126. }
  127. if ( bodyMeshRenderer != null )
  128. {
  129. bodyMeshRenderer.enabled = true;
  130. }
  131. if ( trackingHatMeshRenderer != null )
  132. {
  133. trackingHatMeshRenderer.enabled = true;
  134. }
  135. }
  136. //-------------------------------------------------
  137. public void HideHighlight()
  138. {
  139. if ( renderModelLoaded == false )
  140. {
  141. return;
  142. }
  143. if ( fireHapticsOnHightlight )
  144. {
  145. hand.controller.TriggerHapticPulse( 300 );
  146. }
  147. if ( bodyMeshRenderer != null )
  148. {
  149. bodyMeshRenderer.enabled = false;
  150. }
  151. if ( trackingHatMeshRenderer != null )
  152. {
  153. trackingHatMeshRenderer.enabled = false;
  154. }
  155. }
  156. }
  157. }