Newer
Older
HoloAnatomy / Assets / HoloToolkit / Input / Scripts / Utilities / Interactions / ManualGazeControl.cs
SURFACEBOOK2\jackwynne on 25 May 2018 5 KB v1
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3.  
  4. using UnityEngine;
  5.  
  6. namespace HoloToolkit.Unity.InputModule
  7. {
  8. /// <summary>
  9. /// Class for manually controlling the camera when not running on HoloLens (in editor).
  10. /// Attach to same main camera game object.
  11. /// </summary>
  12. public class ManualGazeControl : MonoBehaviour
  13. {
  14. public bool MouseSupported = true;
  15. public AxisController MouseXYRotationAxisControl;
  16. public AxisController MouseXYTranslationAxisControl;
  17. public AxisController MouseXZTranslationAxisControl;
  18.  
  19. public bool KeyboardSupported = true;
  20. public AxisController KeyboardXYRotationAxisControl;
  21. public AxisController KeyboardXZRotationAxisControl;
  22. public AxisController KeyboardXYTranslationAxisControl;
  23. public AxisController KeyboardXZTranslationAxisControl;
  24.  
  25. public bool JoystickSupported = false;
  26. public AxisController JoystickXYRotationAxisControl;
  27. public AxisController JoystickXYTranslationAxisControl;
  28. public AxisController JoystickXZTranslationAxisControl;
  29.  
  30. private Vector3 lastTrackerToUnityTranslation = Vector3.zero;
  31. private Quaternion lastTrackerToUnityRotation = Quaternion.identity;
  32.  
  33. private Transform cameraTransform;
  34.  
  35. private void Awake()
  36. {
  37. if (Application.isEditor)
  38. {
  39.  
  40. #if UNITY_2017_2_OR_NEWER
  41. if (UnityEngine.XR.XRDevice.isPresent)
  42. #else
  43. if (UnityEngine.VR.VRDevice.isPresent)
  44. #endif
  45. {
  46. Destroy(this);
  47. return;
  48. }
  49. }
  50. else
  51. {
  52. Destroy(this);
  53. return;
  54. }
  55.  
  56. cameraTransform = GetComponent<Camera>().transform;
  57. if (cameraTransform == null)
  58. {
  59. Debug.LogError("ManualGazeControl being used on a game object without a Camera.");
  60. }
  61.  
  62. MouseXYRotationAxisControl.enabled = MouseSupported;
  63. MouseXYTranslationAxisControl.enabled = MouseSupported;
  64. MouseXZTranslationAxisControl.enabled = MouseSupported;
  65.  
  66. KeyboardXYRotationAxisControl.enabled = KeyboardSupported;
  67. KeyboardXZRotationAxisControl.enabled = KeyboardSupported;
  68. KeyboardXYTranslationAxisControl.enabled = KeyboardSupported;
  69. KeyboardXZTranslationAxisControl.enabled = KeyboardSupported;
  70.  
  71. JoystickXYRotationAxisControl.enabled = JoystickSupported;
  72. JoystickXYTranslationAxisControl.enabled = JoystickSupported;
  73. JoystickXZTranslationAxisControl.enabled = JoystickSupported;
  74. }
  75.  
  76. private void Update()
  77. {
  78. // Undo the last tracker to Unity transforms applied.
  79. cameraTransform.Translate(-this.lastTrackerToUnityTranslation, Space.World);
  80. cameraTransform.Rotate(-this.lastTrackerToUnityRotation.eulerAngles, Space.World);
  81.  
  82. // Undo the last local Z-axis tilt rotation.
  83. float previousZTilt = this.transform.localEulerAngles.z;
  84. cameraTransform.Rotate(0, 0, -previousZTilt, Space.Self);
  85.  
  86. // Calculate and apply the camera control movement this frame
  87. Vector3 rotate = Vector3.zero;
  88. Vector3 translate = Vector3.zero;
  89.  
  90. if (MouseSupported)
  91. {
  92. Vector3 mouseXYRotate = MouseXYRotationAxisControl.GetDisplacementVector3();
  93. Vector3 mouseXYTranslate = MouseXYTranslationAxisControl.GetDisplacementVector3();
  94. Vector3 mouseXZTranslate = MouseXZTranslationAxisControl.GetDisplacementVector3();
  95. rotate += mouseXYRotate;
  96. translate += mouseXYTranslate;
  97. translate += mouseXZTranslate;
  98. }
  99.  
  100. if (KeyboardSupported)
  101. {
  102. Vector3 keyboardXYRotate = KeyboardXYRotationAxisControl.GetDisplacementVector3();
  103. Vector3 keyboardXZRotate = KeyboardXZRotationAxisControl.GetDisplacementVector3();
  104. Vector3 keyboardXYTranslate = KeyboardXYTranslationAxisControl.GetDisplacementVector3();
  105. Vector3 keyboardXZTranslate = KeyboardXZTranslationAxisControl.GetDisplacementVector3();
  106. rotate += keyboardXYRotate;
  107. rotate += keyboardXZRotate;
  108. translate += keyboardXYTranslate;
  109. translate += keyboardXZTranslate;
  110. }
  111.  
  112. if (JoystickSupported)
  113. {
  114. Vector3 joystickXYRotate = JoystickXYRotationAxisControl.GetDisplacementVector3();
  115. Vector3 joystickXYTranslate = JoystickXYTranslationAxisControl.GetDisplacementVector3();
  116. Vector3 joystickXZTranslate = JoystickXZTranslationAxisControl.GetDisplacementVector3();
  117. rotate += joystickXYRotate;
  118. translate += joystickXYTranslate;
  119. translate += joystickXZTranslate;
  120. }
  121.  
  122. rotate *= Mathf.Rad2Deg; // change to degrees for the Rotate function
  123.  
  124. // Now apply the displacements to the camera
  125. cameraTransform.Rotate(rotate.x, 0.0f, 0.0f, Space.Self);
  126. cameraTransform.Rotate(0.0f, rotate.y, 0.0f, Space.World);
  127. cameraTransform.Translate(translate, Space.Self);
  128.  
  129. // Apply updated local Z-axis tilt rotation.
  130. cameraTransform.Rotate(0.0f, 0.0f, rotate.z + previousZTilt, Space.Self);
  131.  
  132. // Re-apply the last tracker to Unity transform.
  133. cameraTransform.Rotate(this.lastTrackerToUnityRotation.eulerAngles, Space.World);
  134. cameraTransform.Translate(this.lastTrackerToUnityTranslation, Space.World);
  135. }
  136. }
  137. }