OVRGridCube.cs 5.4 KB

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