Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.9 KiB

  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 System.Collections;
  14. using System.Collections.Generic;
  15. using UnityEngine;
  16. public class OVRHeadsetEmulator : MonoBehaviour {
  17. public enum OpMode
  18. {
  19. Off,
  20. EditorOnly,
  21. AlwaysOn
  22. }
  23. public OpMode opMode = OpMode.EditorOnly;
  24. public bool resetHmdPoseOnRelease = true;
  25. public bool resetHmdPoseByMiddleMouseButton = true;
  26. public KeyCode[] activateKeys = new KeyCode[] { KeyCode.LeftControl, KeyCode.RightControl };
  27. public KeyCode[] pitchKeys = new KeyCode[] { KeyCode.LeftAlt, KeyCode.RightAlt };
  28. OVRManager manager;
  29. const float MOUSE_SCALE_X = -2.0f;
  30. const float MOUSE_SCALE_X_PITCH = -2.0f;
  31. const float MOUSE_SCALE_Y = 2.0f;
  32. const float MOUSE_SCALE_HEIGHT = 1.0f;
  33. const float MAX_ROLL = 85.0f;
  34. private bool lastFrameEmulationActivated = false;
  35. private Vector3 recordedHeadPoseRelativeOffsetTranslation;
  36. private Vector3 recordedHeadPoseRelativeOffsetRotation;
  37. private bool hasSentEvent = false;
  38. private bool emulatorHasInitialized = false;
  39. // Use this for initialization
  40. void Start () {
  41. }
  42. // Update is called once per frame
  43. void Update () {
  44. if (!emulatorHasInitialized)
  45. {
  46. if (OVRManager.OVRManagerinitialized)
  47. {
  48. Cursor.lockState = CursorLockMode.None;
  49. manager = OVRManager.instance;
  50. recordedHeadPoseRelativeOffsetTranslation = manager.headPoseRelativeOffsetTranslation;
  51. recordedHeadPoseRelativeOffsetRotation = manager.headPoseRelativeOffsetRotation;
  52. emulatorHasInitialized = true;
  53. }
  54. else
  55. return;
  56. }
  57. bool emulationActivated = IsEmulationActivated();
  58. if (emulationActivated)
  59. {
  60. Cursor.lockState = CursorLockMode.Locked;
  61. if (!lastFrameEmulationActivated && resetHmdPoseOnRelease)
  62. {
  63. manager.headPoseRelativeOffsetTranslation = recordedHeadPoseRelativeOffsetTranslation;
  64. manager.headPoseRelativeOffsetRotation = recordedHeadPoseRelativeOffsetRotation;
  65. }
  66. if (resetHmdPoseByMiddleMouseButton && Input.GetMouseButton(2))
  67. {
  68. manager.headPoseRelativeOffsetTranslation = Vector3.zero;
  69. manager.headPoseRelativeOffsetRotation = Vector3.zero;
  70. }
  71. else
  72. {
  73. Vector3 emulatedTranslation = manager.headPoseRelativeOffsetTranslation;
  74. float deltaMouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
  75. float emulatedHeight = deltaMouseScrollWheel * MOUSE_SCALE_HEIGHT;
  76. emulatedTranslation.y += emulatedHeight;
  77. manager.headPoseRelativeOffsetTranslation = emulatedTranslation;
  78. float deltaX = Input.GetAxis("Mouse X");
  79. float deltaY = Input.GetAxis("Mouse Y");
  80. Vector3 emulatedAngles = manager.headPoseRelativeOffsetRotation;
  81. float emulatedRoll = emulatedAngles.x;
  82. float emulatedYaw = emulatedAngles.y;
  83. float emulatedPitch = emulatedAngles.z;
  84. if (IsTweakingPitch())
  85. {
  86. emulatedPitch += deltaX * MOUSE_SCALE_X_PITCH;
  87. }
  88. else
  89. {
  90. emulatedRoll += deltaY * MOUSE_SCALE_Y;
  91. emulatedYaw += deltaX * MOUSE_SCALE_X;
  92. }
  93. manager.headPoseRelativeOffsetRotation = new Vector3(emulatedRoll, emulatedYaw, emulatedPitch);
  94. }
  95. if (!hasSentEvent)
  96. {
  97. OVRPlugin.SendEvent("headset_emulator", "activated");
  98. hasSentEvent = true;
  99. }
  100. }
  101. else
  102. {
  103. Cursor.lockState = CursorLockMode.None;
  104. if (lastFrameEmulationActivated)
  105. {
  106. recordedHeadPoseRelativeOffsetTranslation = manager.headPoseRelativeOffsetTranslation;
  107. recordedHeadPoseRelativeOffsetRotation = manager.headPoseRelativeOffsetRotation;
  108. if (resetHmdPoseOnRelease)
  109. {
  110. manager.headPoseRelativeOffsetTranslation = Vector3.zero;
  111. manager.headPoseRelativeOffsetRotation = Vector3.zero;
  112. }
  113. }
  114. }
  115. lastFrameEmulationActivated = emulationActivated;
  116. }
  117. bool IsEmulationActivated()
  118. {
  119. if (opMode == OpMode.Off)
  120. {
  121. return false;
  122. }
  123. else if (opMode == OpMode.EditorOnly && !Application.isEditor)
  124. {
  125. return false;
  126. }
  127. foreach (KeyCode key in activateKeys)
  128. {
  129. if (Input.GetKey(key))
  130. return true;
  131. }
  132. return false;
  133. }
  134. bool IsTweakingPitch()
  135. {
  136. if (!IsEmulationActivated())
  137. return false;
  138. foreach (KeyCode key in pitchKeys)
  139. {
  140. if (Input.GetKey(key))
  141. return true;
  142. }
  143. return false;
  144. }
  145. }