BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRGridCube.cs

OVRGridCube.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRGridCube.cs 5.1 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using UnityEngine;
18using System.Collections;
19
20/// <summary>
21/// Diagnostic display with a regular grid of cubes for visual testing of
22/// tracking and distortion.
23/// </summary>
24public class OVRGridCube : MonoBehaviour
25{
26 /// <summary>
27 /// The key that toggles the grid of cubes.
28 /// </summary>
29 public KeyCode GridKey = KeyCode.G;
30
31 private GameObject CubeGrid = null;
32
33 private bool CubeGridOn = false;
34 private bool CubeSwitchColorOld = false;
35 private bool CubeSwitchColor = false;
36
37 private int gridSizeX = 6;
38 private int gridSizeY = 4;
39 private int gridSizeZ = 6;
40 private float gridScale = 0.3f;
41 private float cubeScale = 0.03f;
42
43 // Handle to OVRCameraRig
44 private OVRCameraRig CameraController = null;
45
46 /// <summary>
47 /// Update this instance.
48 /// </summary>
49 void Update ()
50 {
51 UpdateCubeGrid();
52 }
53
54 /// <summary>
55 /// Sets the OVR camera controller.
56 /// </summary>
57 /// <param name="cameraController">Camera controller.</param>
58 public void SetOVRCameraController(ref OVRCameraRig cameraController)
59 {
60 CameraController = cameraController;
61 }
62
63 void UpdateCubeGrid()
64 {
65 // Toggle the grid cube display on 'G'
66 if(Input.GetKeyDown(GridKey))
67 {
68 if(CubeGridOn == false)
69 {
70 CubeGridOn = true;
71 Debug.LogWarning("CubeGrid ON");
72 if(CubeGrid != null)
73 CubeGrid.SetActive(true);
74 else
75 CreateCubeGrid();
76 }
77 else
78 {
79 CubeGridOn = false;
80 Debug.LogWarning("CubeGrid OFF");
81
82 if(CubeGrid != null)
83 CubeGrid.SetActive(false);
84 }
85 }
86
87 if(CubeGrid != null)
88 {
89 // Set cube colors to let user know if camera is tracking
90 CubeSwitchColor = !OVRManager.tracker.isPositionTracked;
91
92 if(CubeSwitchColor != CubeSwitchColorOld)
93 CubeGridSwitchColor(CubeSwitchColor);
94 CubeSwitchColorOld = CubeSwitchColor;
95 }
96 }
97
98 void CreateCubeGrid()
99 {
100 Debug.LogWarning("Create CubeGrid");
101
102 // Create the visual cube grid
103 CubeGrid = new GameObject("CubeGrid");
104 // Set a layer to target a specific camera
105 CubeGrid.layer = CameraController.gameObject.layer;
106
107 for (int x = -gridSizeX; x <= gridSizeX; x++)
108 for (int y = -gridSizeY; y <= gridSizeY; y++)
109 for (int z = -gridSizeZ; z <= gridSizeZ; z++)
110 {
111 // Set the cube type:
112 // 0 = non-axis cube
113 // 1 = axis cube
114 // 2 = center cube
115 int CubeType = 0;
116 if ((x == 0 && y == 0) || (x == 0 && z == 0) || (y == 0 && z == 0))
117 {
118 if((x == 0) && (y == 0) && (z == 0))
119 CubeType = 2;
120 else
121 CubeType = 1;
122 }
123
124 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
125
126 BoxCollider bc = cube.GetComponent<BoxCollider>();
127 bc.enabled = false;
128
129 cube.layer = CameraController.gameObject.layer;
130
131 // No shadows
132 Renderer r = cube.GetComponent<Renderer>();
133
134#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6
135 // Renderer.castShadows was deprecated starting in Unity 5.0
136 r.castShadows = false;
137#else
138 r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
139#endif
140
141 r.receiveShadows = false;
142
143 // Cube line is white down the middle
144 if (CubeType == 0)
145 r.material.color = Color.red;
146 else if (CubeType == 1)
147 r.material.color = Color.white;
148 else
149 r.material.color = Color.yellow;
150
151 cube.transform.position =
152 new Vector3(((float)x * gridScale),
153 ((float)y * gridScale),
154 ((float)z * gridScale));
155
156 float s = 0.7f;
157
158 // Axis cubes are bigger
159 if(CubeType == 1)
160 s = 1.0f;
161 // Center cube is the largest
162 if(CubeType == 2)
163 s = 2.0f;
164
165 cube.transform.localScale =
166 new Vector3(cubeScale * s, cubeScale * s, cubeScale * s);
167
168 cube.transform.parent = CubeGrid.transform;
169 }
170 }
171
172 /// <summary>
173 /// Switch the Cube grid color.
174 /// </summary>
175 /// <param name="CubeSwitchColor">If set to <c>true</c> cube switch color.</param>
176 void CubeGridSwitchColor(bool CubeSwitchColor)
177 {
178 Color c = Color.red;
179 if(CubeSwitchColor == true)
180 c = Color.blue;
181
182 foreach(Transform child in CubeGrid.transform)
183 {
184 Material m = child.GetComponent<Renderer>().material;
185 // Cube line is white down the middle
186 if(m.color == Color.red || m.color == Color.blue)
187 m.color = c;
188 }
189 }
190}