comparison Orchestland/Assets/OVR/Scripts/Util/OVRCrosshair.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
24 /// <summary>
25 /// OVRCrosshair is a component that adds a stereoscoppic cross-hair into a scene.
26 /// </summary>
27 public class OVRCrosshair
28 {
29 #region Variables
30 public Texture ImageCrosshair = null;
31
32 public OVRCameraRig CameraController = null;
33 public OVRPlayerController PlayerController = null;
34
35 public float FadeTime = 0.3f;
36 public float FadeScale = 0.6f;
37 public float CrosshairDistance = 1.0f;
38
39 public KeyCode CrosshairKey = KeyCode.C;
40
41 private float DeadZoneX = 400.0f;
42 private float DeadZoneY = 75.0f;
43
44 private float ScaleSpeedX = 7.0f;
45 private float ScaleSpeedY = 7.0f;
46
47 private bool DisplayCrosshair;
48 private bool CollisionWithGeometry;
49 private float FadeVal;
50 private Transform UIAnchor;
51
52 private float XL = 0.0f;
53 private float YL = 0.0f;
54
55 private float ScreenWidth = 1280.0f;
56 private float ScreenHeight = 800.0f;
57
58 #endregion
59
60 #region Public Functions
61
62 /// <summary>
63 /// Sets the crosshair texture.
64 /// </summary>
65 /// <param name="image">Image.</param>
66 public void SetCrosshairTexture(ref Texture image)
67 {
68 ImageCrosshair = image;
69 }
70
71 /// <summary>
72 /// Sets the OVR camera controller.
73 /// </summary>
74 /// <param name="cameraController">Camera controller.</param>
75 public void SetOVRCameraController(ref OVRCameraRig cameraController)
76 {
77 CameraController = cameraController;
78 UIAnchor = CameraController.centerEyeAnchor;
79 }
80
81 /// <summary>
82 /// Sets the OVR player controller.
83 /// </summary>
84 /// <param name="playerController">Player controller.</param>
85 public void SetOVRPlayerController(ref OVRPlayerController playerController)
86 {
87 PlayerController = playerController;
88 }
89
90 /// <summary>
91 /// Determines whether the crosshair is visible.
92 /// </summary>
93 /// <returns><c>true</c> if this instance is crosshair visible; otherwise, <c>false</c>.</returns>
94 public bool IsCrosshairVisible()
95 {
96 if(FadeVal > 0.0f)
97 return true;
98
99 return false;
100 }
101
102 /// <summary>
103 /// Init this instance.
104 /// </summary>
105 public void Init()
106 {
107 DisplayCrosshair = false;
108 CollisionWithGeometry = false;
109 FadeVal = 0.0f;
110
111 ScreenWidth = Screen.width;
112 ScreenHeight = Screen.height;
113
114 // Initialize screen location of cursor
115 XL = ScreenWidth * 0.5f;
116 YL = ScreenHeight * 0.5f;
117 }
118
119 /// <summary>
120 /// Updates the crosshair.
121 /// </summary>
122 public void UpdateCrosshair()
123 {
124 if (ShouldDisplayCrosshair())
125 {
126 // Do not do these tests within OnGUI since they will be called twice
127 CollisionWithGeometryCheck();
128 }
129 }
130
131 /// <summary>
132 /// The GUI crosshair event.
133 /// </summary>
134 public void OnGUICrosshair()
135 {
136 if ((DisplayCrosshair == true) && (CollisionWithGeometry == false))
137 FadeVal += Time.deltaTime / FadeTime;
138 else
139 FadeVal -= Time.deltaTime / FadeTime;
140
141 FadeVal = Mathf.Clamp(FadeVal, 0.0f, 1.0f);
142
143 // Check to see if crosshair influences mouse rotation
144 if(PlayerController != null)
145 PlayerController.SetSkipMouseRotation(false);
146
147 if ((ImageCrosshair != null) && (FadeVal != 0.0f))
148 {
149 // Assume cursor is on-screen (unless it goes into the dead-zone)
150 // Other systems will check this to see if it is false for example
151 // allowing rotation to take place
152 if(PlayerController != null)
153 PlayerController.SetSkipMouseRotation(true);
154
155 GUI.color = new Color(1, 1, 1, FadeVal * FadeScale);
156
157 // Calculate X
158 XL += Input.GetAxis("Mouse X") * ScaleSpeedX;
159 if(XL < DeadZoneX)
160 {
161 if(PlayerController != null)
162 PlayerController.SetSkipMouseRotation(false);
163
164 XL = DeadZoneX - 0.001f;
165 }
166 else if (XL > (Screen.width - DeadZoneX))
167 {
168 if(PlayerController != null)
169 PlayerController.SetSkipMouseRotation(false);
170
171 XL = ScreenWidth - DeadZoneX + 0.001f;
172 }
173
174 // Calculate Y
175 YL -= Input.GetAxis("Mouse Y") * ScaleSpeedY;
176 if(YL < DeadZoneY)
177 {
178 //CursorOnScreen = false;
179 if(YL < 0.0f) YL = 0.0f;
180 }
181 else if (YL > ScreenHeight - DeadZoneY)
182 {
183 //CursorOnScreen = false;
184 if(YL > ScreenHeight) YL = ScreenHeight;
185 }
186
187 // Finally draw cursor
188 bool skipMouseRotation = true;
189 if(PlayerController != null)
190 PlayerController.GetSkipMouseRotation(ref skipMouseRotation);
191
192 if(skipMouseRotation == true)
193 {
194 // Left
195 GUI.DrawTexture(new Rect( XL - (ImageCrosshair.width * 0.5f),
196 YL - (ImageCrosshair.height * 0.5f),
197 ImageCrosshair.width,
198 ImageCrosshair.height),
199 ImageCrosshair);
200 }
201
202 GUI.color = Color.white;
203 }
204 }
205 #endregion
206
207 #region Private Functions
208 /// <summary>
209 /// Shoulds the crosshair be displayed.
210 /// </summary>
211 /// <returns><c>true</c>, if display crosshair was shoulded, <c>false</c> otherwise.</returns>
212 bool ShouldDisplayCrosshair()
213 {
214 if(Input.GetKeyDown (CrosshairKey))
215 {
216 if(DisplayCrosshair == false)
217 {
218 DisplayCrosshair = true;
219
220 // Always initialize screen location of cursor to center
221 XL = ScreenWidth * 0.5f;
222 YL = ScreenHeight * 0.5f;
223 }
224 else
225 DisplayCrosshair = false;
226 }
227
228 return DisplayCrosshair;
229 }
230
231 /// <summary>
232 /// Do a collision raycast on geometry for crosshair.
233 /// </summary>
234 /// <returns><c>true</c>, if with geometry check was collisioned, <c>false</c> otherwise.</returns>
235 bool CollisionWithGeometryCheck()
236 {
237 CollisionWithGeometry = false;
238
239 Vector3 startPos = UIAnchor.position;
240 Vector3 dir = Vector3.forward;
241 dir = UIAnchor.rotation * dir;
242 dir *= CrosshairDistance;
243 Vector3 endPos = startPos + dir;
244
245 RaycastHit hit;
246 if (Physics.Linecast(startPos, endPos, out hit))
247 {
248 if (!hit.collider.isTrigger)
249 {
250 CollisionWithGeometry = true;
251 }
252 }
253
254 return CollisionWithGeometry;
255 }
256 #endregion
257 }