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.3 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: An area that the player can teleport to
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. namespace Valve.VR.InteractionSystem
  11. {
  12. //-------------------------------------------------------------------------
  13. public class TeleportArea : TeleportMarkerBase
  14. {
  15. //Public properties
  16. public Bounds meshBounds { get; private set; }
  17. //Private data
  18. private MeshRenderer areaMesh;
  19. private int tintColorId = 0;
  20. private Color visibleTintColor = Color.clear;
  21. private Color highlightedTintColor = Color.clear;
  22. private Color lockedTintColor = Color.clear;
  23. private bool highlighted = false;
  24. //-------------------------------------------------
  25. public void Awake()
  26. {
  27. areaMesh = GetComponent<MeshRenderer>();
  28. tintColorId = Shader.PropertyToID( "_TintColor" );
  29. CalculateBounds();
  30. }
  31. //-------------------------------------------------
  32. public void Start()
  33. {
  34. visibleTintColor = Teleport.instance.areaVisibleMaterial.GetColor( tintColorId );
  35. highlightedTintColor = Teleport.instance.areaHighlightedMaterial.GetColor( tintColorId );
  36. lockedTintColor = Teleport.instance.areaLockedMaterial.GetColor( tintColorId );
  37. }
  38. //-------------------------------------------------
  39. public override bool ShouldActivate( Vector3 playerPosition )
  40. {
  41. return true;
  42. }
  43. //-------------------------------------------------
  44. public override bool ShouldMovePlayer()
  45. {
  46. return true;
  47. }
  48. //-------------------------------------------------
  49. public override void Highlight( bool highlight )
  50. {
  51. if ( !locked )
  52. {
  53. highlighted = highlight;
  54. if ( highlight )
  55. {
  56. areaMesh.material = Teleport.instance.areaHighlightedMaterial;
  57. }
  58. else
  59. {
  60. areaMesh.material = Teleport.instance.areaVisibleMaterial;
  61. }
  62. }
  63. }
  64. //-------------------------------------------------
  65. public override void SetAlpha( float tintAlpha, float alphaPercent )
  66. {
  67. Color tintedColor = GetTintColor();
  68. tintedColor.a *= alphaPercent;
  69. areaMesh.material.SetColor( tintColorId, tintedColor );
  70. }
  71. //-------------------------------------------------
  72. public override void UpdateVisuals()
  73. {
  74. if ( locked )
  75. {
  76. areaMesh.material = Teleport.instance.areaLockedMaterial;
  77. }
  78. else
  79. {
  80. areaMesh.material = Teleport.instance.areaVisibleMaterial;
  81. }
  82. }
  83. //-------------------------------------------------
  84. public void UpdateVisualsInEditor()
  85. {
  86. areaMesh = GetComponent<MeshRenderer>();
  87. if ( locked )
  88. {
  89. areaMesh.sharedMaterial = Teleport.instance.areaLockedMaterial;
  90. }
  91. else
  92. {
  93. areaMesh.sharedMaterial = Teleport.instance.areaVisibleMaterial;
  94. }
  95. }
  96. //-------------------------------------------------
  97. private bool CalculateBounds()
  98. {
  99. MeshFilter meshFilter = GetComponent<MeshFilter>();
  100. if ( meshFilter == null )
  101. {
  102. return false;
  103. }
  104. Mesh mesh = meshFilter.sharedMesh;
  105. if ( mesh == null )
  106. {
  107. return false;
  108. }
  109. meshBounds = mesh.bounds;
  110. return true;
  111. }
  112. //-------------------------------------------------
  113. private Color GetTintColor()
  114. {
  115. if ( locked )
  116. {
  117. return lockedTintColor;
  118. }
  119. else
  120. {
  121. if ( highlighted )
  122. {
  123. return highlightedTintColor;
  124. }
  125. else
  126. {
  127. return visibleTintColor;
  128. }
  129. }
  130. }
  131. }
  132. #if UNITY_EDITOR
  133. //-------------------------------------------------------------------------
  134. [CustomEditor( typeof( TeleportArea ) )]
  135. public class TeleportAreaEditor : Editor
  136. {
  137. //-------------------------------------------------
  138. void OnEnable()
  139. {
  140. if ( Selection.activeTransform != null )
  141. {
  142. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  143. if ( teleportArea != null )
  144. {
  145. teleportArea.UpdateVisualsInEditor();
  146. }
  147. }
  148. }
  149. //-------------------------------------------------
  150. public override void OnInspectorGUI()
  151. {
  152. DrawDefaultInspector();
  153. if ( Selection.activeTransform != null )
  154. {
  155. TeleportArea teleportArea = Selection.activeTransform.GetComponent<TeleportArea>();
  156. if ( GUI.changed && teleportArea != null )
  157. {
  158. teleportArea.UpdateVisualsInEditor();
  159. }
  160. }
  161. }
  162. }
  163. #endif
  164. }