OVRGridCube.cs 5.1 KB

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