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