comparison Orchestland/Assets/OVR/Scripts/Util/OVRVisionGuide.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 /************************************************************************************
2
3 Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved.
4
5 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
6 you may not use the Oculus VR Rift SDK except in compliance with the License,
7 which is provided at the time of installation or download, or which
8 otherwise accompanies this software in either electronic or hard copy form.
9
10 You may obtain a copy of the License at
11
12 http://www.oculusvr.com/licenses/LICENSE-3.2
13
14 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ************************************************************************************/
21
22 using UnityEngine;
23 using System.Collections;
24
25 /// <summary>
26 /// OVR vision guide.
27 /// </summary>
28 public class OVRVisionGuide : MonoBehaviour
29 {
30 // Manual fade (used when out of view; will add textures on top)
31 private Texture FadeTexture = null;
32 private float FadeTextureAlpha = 0.0f;
33
34 // Clip Camera (takes position offset into account)
35 private Vector3 CameraPositionClampMin = new Vector3(-0.45f, -0.25f, -0.5f);
36 private Vector3 CameraPositionClampMax = new Vector3( 0.45f, 1.35f, 1.0f);
37 private float CameraPositionOverlap = 0.125f;
38 private float CameraPositionMaxFade = 0.65f;
39
40 // Handle to OVRCameraRig
41 private OVRCameraRig CameraController = null;
42
43 // Handle to Vision Guide
44 private GameObject VisionGuide = null;
45 private float VisionGuideFlashSpeed = 5.0f; // Radians / sec
46
47 // Layer to render to
48 private string LayerName = "Default";
49
50 /// <summary>
51 /// Start this instance.
52 /// </summary>
53 void Start ()
54 {
55 if(CameraController != null)
56 {
57 // Set the GUI target
58 VisionGuide = GameObject.Instantiate(Resources.Load("OVRVisionGuideMessage")) as GameObject;
59 // Grab transform of GUI object
60 Transform t = VisionGuide.transform;
61 // Attach the GUI object to the camera
62 VisionGuide.transform.parent = CameraController.centerEyeAnchor;
63 // Reset the transform values
64 VisionGuide.transform.localPosition = t.position;
65 VisionGuide.transform.localRotation = t.rotation;
66 VisionGuide.transform.localScale = t.localScale;
67 // Deactivate the object
68 VisionGuide.SetActive(false);
69 // Set layer on object
70 VisionGuide.layer = LayerMask.NameToLayer(LayerName);
71 }
72 }
73
74 /// <summary>
75 /// Update this instance.
76 /// </summary>
77 void Update()
78 {
79 Vector3 relVisionCam = Vector3.zero;
80 // Fade screen out based on location of relative Vision Camera
81 UpdateFadeValueFromRelCamPosition(ref relVisionCam);
82
83 if (Input.GetKeyDown(KeyCode.T))
84 OVRManager.instance.timeWarp = !OVRManager.instance.timeWarp;
85
86 if (Input.GetKeyDown(KeyCode.F))
87 OVRManager.instance.freezeTimeWarp = !OVRManager.instance.freezeTimeWarp;
88 }
89
90 /// <summary>
91 /// Updates the fade value from rel cam position.
92 /// </summary>
93 /// <returns><c>true</c>, if fade value from rel cam position was updated, <c>false</c> otherwise.</returns>
94 /// <param name="relCamPosition">Rel cam position.</param>
95 bool UpdateFadeValueFromRelCamPosition(ref Vector3 relCamPosition)
96 {
97 bool result = false;
98 FadeTextureAlpha = 0.0f;
99
100 // Clip camera to min amd max values
101 // MIN
102 if((relCamPosition.x < CameraPositionClampMin.x) &&
103 (CalculateFadeValue(ref FadeTextureAlpha,
104 relCamPosition.x, CameraPositionClampMin.x) == true))
105 result = true;
106
107 if((relCamPosition.y < CameraPositionClampMin.y) &&
108 (CalculateFadeValue(ref FadeTextureAlpha,
109 relCamPosition.y, CameraPositionClampMin.y) == true))
110 result = true;
111
112 if((relCamPosition.z < CameraPositionClampMin.z) &&
113 (CalculateFadeValue(ref FadeTextureAlpha,
114 relCamPosition.z, CameraPositionClampMin.z) == true))
115 result = true;
116
117 // MAX
118 if((relCamPosition.x > CameraPositionClampMax.x) &&
119 (CalculateFadeValue(ref FadeTextureAlpha,
120 CameraPositionClampMax.x, relCamPosition.x ) == true))
121 result = true;
122
123 if((relCamPosition.y > CameraPositionClampMax.y) &&
124 (CalculateFadeValue(ref FadeTextureAlpha,
125 CameraPositionClampMax.y, relCamPosition.y ) == true))
126 result = true;
127
128 if((relCamPosition.z > CameraPositionClampMax.z) &&
129 (CalculateFadeValue(ref FadeTextureAlpha,
130 CameraPositionClampMax.z, relCamPosition.z ) == true))
131 result = true;
132
133 return result;
134 }
135
136 /// <summary>
137 /// CalculateFadeValue
138 /// return value tells us which axis is the furthest out, so we
139 /// can tell the user which direction to go
140 /// </summary>
141 /// <returns><c>true</c>, if fade value was calculated, <c>false</c> otherwise.</returns>
142 /// <param name="curFade">Current fade.</param>
143 /// <param name="a">The alpha component.</param>
144 /// <param name="b">The blue component.</param>
145 bool CalculateFadeValue(ref float curFade, float a, float b)
146 {
147 bool result = false;
148
149 float tmpFade = (b - a) / CameraPositionOverlap;
150
151 if(tmpFade > 1.0f) tmpFade = 1.0f;
152 tmpFade *= CameraPositionMaxFade;
153
154 if(tmpFade > curFade)
155 {
156 curFade = tmpFade;
157
158 // We want to show a bit more then the fade
159 if(tmpFade >= CameraPositionMaxFade)
160 result = true;
161 }
162
163 return result;
164 }
165
166
167 //
168 // PUBLIC FUNCTIONS
169 //
170
171 /// <summary>
172 /// Sets the camera controller.
173 /// </summary>
174 /// <param name="cameraController">Camera controller.</param>
175 public void SetOVRCameraController(ref OVRCameraRig cameraController)
176 {
177 CameraController = cameraController;
178 }
179
180 /// <summary>
181 /// Sets the fade texture.
182 /// </summary>
183 /// <param name="fadeTexture">Fade texture.</param>
184 public void SetFadeTexture(ref Texture fadeTexture)
185 {
186 FadeTexture = fadeTexture;
187 }
188
189 /// <summary>
190 /// Gets the fade alpha value.
191 /// </summary>
192 /// <returns>The fade alpha value.</returns>
193 public float GetFadeAlphaValue()
194 {
195 return FadeTextureAlpha;
196 }
197
198 /// <summary>
199 /// Sets the vision guide layer.
200 /// </summary>
201 /// <param name="layer">Layer.</param>
202 public void SetVisionGuideLayer(ref string layer)
203 {
204 LayerName = layer;
205 }
206
207 /// <summary>
208 /// Raises the GUI vision guide event.
209 /// </summary>
210 public void OnGUIVisionGuide()
211 {
212 // Separate fade value (externally driven)
213 if((FadeTexture != null) && (FadeTextureAlpha > 0.0f))
214 {
215 GUI.color = new Color(0.1f, 0.1f, 0.1f, FadeTextureAlpha);
216 GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), FadeTexture );
217 GUI.color = Color.white;
218
219 if(VisionGuide != null)
220 {
221 // Activate the message
222 VisionGuide.SetActive(true);
223
224 // Sharper curve for fading text
225 float fade = FadeTextureAlpha / CameraPositionMaxFade;
226 fade *= fade;
227
228 // Fade and flash the VisionGuide message
229 float VisionGuideAlpha =
230 fade * ((Mathf.Sin(Time.time * VisionGuideFlashSpeed) + 1.0f) * 0.5f);
231
232 Material m = VisionGuide.GetComponent<Renderer>().material;
233 Color c = m.GetColor("_Color");
234 c.a = VisionGuideAlpha;
235 m.SetColor("_Color", c);
236 }
237 }
238 else
239 {
240 if(VisionGuide != null)
241 VisionGuide.SetActive(false);
242 }
243 }
244 }