OVRSceneSampleController.cs 6.5 KB

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