comparison Orchestland/Assets/OVR/Scripts/Util/OVRGridCube.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 /// Diagnostic display with a regular grid of cubes for visual testing of
27 /// tracking and distortion.
28 /// </summary>
29 public class OVRGridCube : MonoBehaviour
30 {
31 /// <summary>
32 /// The key that toggles the grid of cubes.
33 /// </summary>
34 public KeyCode GridKey = KeyCode.G;
35
36 private GameObject CubeGrid = null;
37
38 private bool CubeGridOn = false;
39 private bool CubeSwitchColorOld = false;
40 private bool CubeSwitchColor = false;
41
42 private int gridSizeX = 6;
43 private int gridSizeY = 4;
44 private int gridSizeZ = 6;
45 private float gridScale = 0.3f;
46 private float cubeScale = 0.03f;
47
48 // Handle to OVRCameraRig
49 private OVRCameraRig CameraController = null;
50
51 /// <summary>
52 /// Update this instance.
53 /// </summary>
54 void Update ()
55 {
56 UpdateCubeGrid();
57 }
58
59 /// <summary>
60 /// Sets the OVR camera controller.
61 /// </summary>
62 /// <param name="cameraController">Camera controller.</param>
63 public void SetOVRCameraController(ref OVRCameraRig cameraController)
64 {
65 CameraController = cameraController;
66 }
67
68 void UpdateCubeGrid()
69 {
70 // Toggle the grid cube display on 'G'
71 if(Input.GetKeyDown(GridKey))
72 {
73 if(CubeGridOn == false)
74 {
75 CubeGridOn = true;
76 Debug.LogWarning("CubeGrid ON");
77 if(CubeGrid != null)
78 CubeGrid.SetActive(true);
79 else
80 CreateCubeGrid();
81 }
82 else
83 {
84 CubeGridOn = false;
85 Debug.LogWarning("CubeGrid OFF");
86
87 if(CubeGrid != null)
88 CubeGrid.SetActive(false);
89 }
90 }
91
92 if(CubeGrid != null)
93 {
94 // Set cube colors to let user know if camera is tracking
95 CubeSwitchColor = !OVRManager.tracker.isPositionTracked;
96
97 if(CubeSwitchColor != CubeSwitchColorOld)
98 CubeGridSwitchColor(CubeSwitchColor);
99 CubeSwitchColorOld = CubeSwitchColor;
100 }
101 }
102
103 void CreateCubeGrid()
104 {
105 Debug.LogWarning("Create CubeGrid");
106
107 // Create the visual cube grid
108 CubeGrid = new GameObject("CubeGrid");
109 // Set a layer to target a specific camera
110 CubeGrid.layer = CameraController.gameObject.layer;
111
112 for (int x = -gridSizeX; x <= gridSizeX; x++)
113 for (int y = -gridSizeY; y <= gridSizeY; y++)
114 for (int z = -gridSizeZ; z <= gridSizeZ; z++)
115 {
116 // Set the cube type:
117 // 0 = non-axis cube
118 // 1 = axis cube
119 // 2 = center cube
120 int CubeType = 0;
121 if ((x == 0 && y == 0) || (x == 0 && z == 0) || (y == 0 && z == 0))
122 {
123 if((x == 0) && (y == 0) && (z == 0))
124 CubeType = 2;
125 else
126 CubeType = 1;
127 }
128
129 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
130
131 BoxCollider bc = cube.GetComponent<BoxCollider>();
132 bc.enabled = false;
133
134 cube.layer = CameraController.gameObject.layer;
135
136 // No shadows
137 Renderer r = cube.GetComponent<Renderer>();
138 r.castShadows = false;
139 r.receiveShadows = false;
140
141 // Cube line is white down the middle
142 if (CubeType == 0)
143 r.material.color = Color.red;
144 else if (CubeType == 1)
145 r.material.color = Color.white;
146 else
147 r.material.color = Color.yellow;
148
149 cube.transform.position =
150 new Vector3(((float)x * gridScale),
151 ((float)y * gridScale),
152 ((float)z * gridScale));
153
154 float s = 0.7f;
155
156 // Axis cubes are bigger
157 if(CubeType == 1)
158 s = 1.0f;
159 // Center cube is the largest
160 if(CubeType == 2)
161 s = 2.0f;
162
163 cube.transform.localScale =
164 new Vector3(cubeScale * s, cubeScale * s, cubeScale * s);
165
166 cube.transform.parent = CubeGrid.transform;
167 }
168 }
169
170 /// <summary>
171 /// Switch the Cube grid color.
172 /// </summary>
173 /// <param name="CubeSwitchColor">If set to <c>true</c> cube switch color.</param>
174 void CubeGridSwitchColor(bool CubeSwitchColor)
175 {
176 Color c = Color.red;
177 if(CubeSwitchColor == true)
178 c = Color.blue;
179
180 foreach(Transform child in CubeGrid.transform)
181 {
182 Material m = child.GetComponent<Renderer>().material;
183 // Cube line is white down the middle
184 if(m.color == Color.red || m.color == Color.blue)
185 m.color = c;
186 }
187 }
188 }