OVRSceneSampleController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /// Sample that allows you to play with various VR settings.
  17. /// </summary>
  18. public class OVRSceneSampleController : MonoBehaviour
  19. {
  20. /// <summary>
  21. /// The key that quits the application.
  22. /// </summary>
  23. public KeyCode quitKey = KeyCode.Escape;
  24. /// <summary>
  25. /// An optional texture that appears before the menu fades in.
  26. /// </summary>
  27. public Texture fadeInTexture = null;
  28. /// <summary>
  29. /// Controls how quickly the player's speed and rotation change based on input.
  30. /// </summary>
  31. public float speedRotationIncrement = 0.05f;
  32. private OVRPlayerController playerController = null;
  33. // Handle to OVRCameraRig
  34. private OVRCameraRig cameraController = null;
  35. /// <summary>
  36. /// We can set the layer to be anything we want to, this allows
  37. /// a specific camera to render it.
  38. /// </summary>
  39. public string layerName = "Default";
  40. // Vision mode on/off
  41. private bool visionMode = true;
  42. // We want to hold onto GridCube, for potential sharing
  43. // of the menu RenderTarget
  44. OVRGridCube gridCube = null;
  45. #if SHOW_DK2_VARIABLES
  46. private string strVisionMode = "Vision Enabled: ON";
  47. #endif
  48. #region MonoBehaviour Message Handlers
  49. /// <summary>
  50. /// Awake this instance.
  51. /// </summary>
  52. void Awake()
  53. {
  54. // Find camera controller
  55. OVRCameraRig[] cameraControllers;
  56. cameraControllers = gameObject.GetComponentsInChildren<OVRCameraRig>();
  57. if (cameraControllers.Length == 0)
  58. {
  59. Debug.LogWarning("OVRMainMenu: No OVRCameraRig attached.");
  60. }
  61. else if (cameraControllers.Length > 1)
  62. {
  63. Debug.LogWarning("OVRMainMenu: More then 1 OVRCameraRig attached.");
  64. }
  65. else
  66. {
  67. cameraController = cameraControllers[0];
  68. }
  69. // Find player controller
  70. OVRPlayerController[] playerControllers;
  71. playerControllers = gameObject.GetComponentsInChildren<OVRPlayerController>();
  72. if (playerControllers.Length == 0)
  73. {
  74. Debug.LogWarning("OVRMainMenu: No OVRPlayerController attached.");
  75. }
  76. else if (playerControllers.Length > 1)
  77. {
  78. Debug.LogWarning("OVRMainMenu: More then 1 OVRPlayerController attached.");
  79. }
  80. else
  81. {
  82. playerController = playerControllers[0];
  83. }
  84. }
  85. /// <summary>
  86. /// Start this instance.
  87. /// </summary>
  88. void Start()
  89. {
  90. // Make sure to hide cursor
  91. if (Application.isEditor == false)
  92. {
  93. Cursor.visible = false;
  94. Cursor.lockState = CursorLockMode.Locked;
  95. }
  96. // CameraController updates
  97. if (cameraController != null)
  98. {
  99. // Add a GridCube component to this object
  100. gridCube = gameObject.AddComponent<OVRGridCube>();
  101. gridCube.SetOVRCameraController(ref cameraController);
  102. }
  103. }
  104. /// <summary>
  105. /// Update this instance.
  106. /// </summary>
  107. void Update()
  108. {
  109. // Recenter pose
  110. UpdateRecenterPose();
  111. // Turn On/Off Vision Mode
  112. UpdateVisionMode();
  113. // Update Speed and Rotation Scale
  114. if (playerController != null)
  115. UpdateSpeedAndRotationScaleMultiplier();
  116. // Toggle Fullscreen
  117. if (Input.GetKeyDown(KeyCode.F11))
  118. Screen.fullScreen = !Screen.fullScreen;
  119. if (Input.GetKeyDown(KeyCode.M))
  120. #if UNITY_2017_2_OR_NEWER
  121. UnityEngine.XR.XRSettings.showDeviceView = !UnityEngine.XR.XRSettings.showDeviceView;
  122. #else
  123. UnityEngine.VR.VRSettings.showDeviceView = !UnityEngine.VR.VRSettings.showDeviceView;
  124. #endif
  125. #if !UNITY_ANDROID || UNITY_EDITOR
  126. // Escape Application
  127. if (Input.GetKeyDown(quitKey))
  128. Application.Quit();
  129. #endif
  130. }
  131. #endregion
  132. /// <summary>
  133. /// Updates the vision mode.
  134. /// </summary>
  135. void UpdateVisionMode()
  136. {
  137. if (Input.GetKeyDown(KeyCode.F2))
  138. {
  139. visionMode ^= visionMode;
  140. OVRManager.tracker.isEnabled = visionMode;
  141. }
  142. }
  143. /// <summary>
  144. /// Updates the speed and rotation scale multiplier.
  145. /// </summary>
  146. void UpdateSpeedAndRotationScaleMultiplier()
  147. {
  148. float moveScaleMultiplier = 0.0f;
  149. playerController.GetMoveScaleMultiplier(ref moveScaleMultiplier);
  150. if (Input.GetKeyDown(KeyCode.Alpha7))
  151. {
  152. moveScaleMultiplier -= speedRotationIncrement;
  153. }
  154. else if (Input.GetKeyDown(KeyCode.Alpha8))
  155. {
  156. moveScaleMultiplier += speedRotationIncrement;
  157. }
  158. playerController.SetMoveScaleMultiplier(moveScaleMultiplier);
  159. float rotationScaleMultiplier = 0.0f;
  160. playerController.GetRotationScaleMultiplier(ref rotationScaleMultiplier);
  161. if (Input.GetKeyDown(KeyCode.Alpha9))
  162. {
  163. rotationScaleMultiplier -= speedRotationIncrement;
  164. }
  165. else if (Input.GetKeyDown(KeyCode.Alpha0))
  166. {
  167. rotationScaleMultiplier += speedRotationIncrement;
  168. }
  169. playerController.SetRotationScaleMultiplier(rotationScaleMultiplier);
  170. }
  171. /// <summary>
  172. /// Recenter pose
  173. /// </summary>
  174. void UpdateRecenterPose()
  175. {
  176. if (Input.GetKeyDown(KeyCode.R))
  177. OVRManager.display.RecenterPose();
  178. }
  179. }