OVRCameraRig.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using UnityEngine;
  19. /// <summary>
  20. /// A head-tracked stereoscopic virtual reality camera rig.
  21. /// </summary>
  22. [ExecuteInEditMode]
  23. public class OVRCameraRig : MonoBehaviour
  24. {
  25. /// <summary>
  26. /// The left eye camera.
  27. /// </summary>
  28. public Camera leftEyeCamera { get { return (usePerEyeCameras) ? _leftEyeCamera : _centerEyeCamera; } }
  29. /// <summary>
  30. /// The right eye camera.
  31. /// </summary>
  32. public Camera rightEyeCamera { get { return (usePerEyeCameras) ? _rightEyeCamera : _centerEyeCamera; } }
  33. /// <summary>
  34. /// Provides a root transform for all anchors in tracking space.
  35. /// </summary>
  36. public Transform trackingSpace { get; private set; }
  37. /// <summary>
  38. /// Always coincides with the pose of the left eye.
  39. /// </summary>
  40. public Transform leftEyeAnchor { get; private set; }
  41. /// <summary>
  42. /// Always coincides with average of the left and right eye poses.
  43. /// </summary>
  44. public Transform centerEyeAnchor { get; private set; }
  45. /// <summary>
  46. /// Always coincides with the pose of the right eye.
  47. /// </summary>
  48. public Transform rightEyeAnchor { get; private set; }
  49. /// <summary>
  50. /// Always coincides with the pose of the left hand.
  51. /// </summary>
  52. public Transform leftHandAnchor { get; private set; }
  53. /// <summary>
  54. /// Always coincides with the pose of the right hand.
  55. /// </summary>
  56. public Transform rightHandAnchor { get; private set; }
  57. /// <summary>
  58. /// Always coincides with the pose of the sensor.
  59. /// </summary>
  60. public Transform trackerAnchor { get; private set; }
  61. /// <summary>
  62. /// Occurs when the eye pose anchors have been set.
  63. /// </summary>
  64. public event System.Action<OVRCameraRig> UpdatedAnchors;
  65. /// <summary>
  66. /// If true, separate cameras will be used for the left and right eyes.
  67. /// </summary>
  68. public bool usePerEyeCameras = false;
  69. /// <summary>
  70. /// If true, all tracked anchors are updated in FixedUpdate instead of Update to favor physics fidelity.
  71. /// \note: If the fixed update rate doesn't match the rendering framerate (OVRManager.display.appFramerate), the anchors will visibly judder.
  72. /// </summary>
  73. public bool useFixedUpdateForTracking = false;
  74. protected bool _skipUpdate = false;
  75. protected readonly string trackingSpaceName = "TrackingSpace";
  76. protected readonly string trackerAnchorName = "TrackerAnchor";
  77. protected readonly string leftEyeAnchorName = "LeftEyeAnchor";
  78. protected readonly string centerEyeAnchorName = "CenterEyeAnchor";
  79. protected readonly string rightEyeAnchorName = "RightEyeAnchor";
  80. protected readonly string leftHandAnchorName = "LeftHandAnchor";
  81. protected readonly string rightHandAnchorName = "RightHandAnchor";
  82. protected Camera _centerEyeCamera;
  83. protected Camera _leftEyeCamera;
  84. protected Camera _rightEyeCamera;
  85. #region Unity Messages
  86. protected virtual void Awake()
  87. {
  88. _skipUpdate = true;
  89. EnsureGameObjectIntegrity();
  90. }
  91. protected virtual void Start()
  92. {
  93. UpdateAnchors();
  94. }
  95. protected virtual void FixedUpdate()
  96. {
  97. if (useFixedUpdateForTracking)
  98. UpdateAnchors();
  99. }
  100. protected virtual void Update()
  101. {
  102. _skipUpdate = false;
  103. if (!useFixedUpdateForTracking)
  104. UpdateAnchors();
  105. }
  106. #endregion
  107. protected virtual void UpdateAnchors()
  108. {
  109. EnsureGameObjectIntegrity();
  110. if (!Application.isPlaying)
  111. return;
  112. if (_skipUpdate)
  113. {
  114. centerEyeAnchor.FromOVRPose(OVRPose.identity, true);
  115. leftEyeAnchor.FromOVRPose(OVRPose.identity, true);
  116. rightEyeAnchor.FromOVRPose(OVRPose.identity, true);
  117. return;
  118. }
  119. bool monoscopic = OVRManager.instance.monoscopic;
  120. OVRPose tracker = OVRManager.tracker.GetPose();
  121. trackerAnchor.localRotation = tracker.orientation;
  122. #if UNITY_2017_2_OR_NEWER
  123. centerEyeAnchor.localRotation = UnityEngine.XR.InputTracking.GetLocalRotation(UnityEngine.XR.XRNode.CenterEye);
  124. leftEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : UnityEngine.XR.InputTracking.GetLocalRotation(UnityEngine.XR.XRNode.LeftEye);
  125. rightEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : UnityEngine.XR.InputTracking.GetLocalRotation(UnityEngine.XR.XRNode.RightEye);
  126. #else
  127. centerEyeAnchor.localRotation = UnityEngine.VR.InputTracking.GetLocalRotation(UnityEngine.VR.VRNode.CenterEye);
  128. leftEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : UnityEngine.VR.InputTracking.GetLocalRotation(UnityEngine.VR.VRNode.LeftEye);
  129. rightEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : UnityEngine.VR.InputTracking.GetLocalRotation(UnityEngine.VR.VRNode.RightEye);
  130. #endif
  131. leftHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
  132. rightHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
  133. trackerAnchor.localPosition = tracker.position;
  134. #if UNITY_2017_2_OR_NEWER
  135. centerEyeAnchor.localPosition = UnityEngine.XR.InputTracking.GetLocalPosition(UnityEngine.XR.XRNode.CenterEye);
  136. leftEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : UnityEngine.XR.InputTracking.GetLocalPosition(UnityEngine.XR.XRNode.LeftEye);
  137. rightEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : UnityEngine.XR.InputTracking.GetLocalPosition(UnityEngine.XR.XRNode.RightEye);
  138. #else
  139. centerEyeAnchor.localPosition = UnityEngine.VR.InputTracking.GetLocalPosition(UnityEngine.VR.VRNode.CenterEye);
  140. leftEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : UnityEngine.VR.InputTracking.GetLocalPosition(UnityEngine.VR.VRNode.LeftEye);
  141. rightEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : UnityEngine.VR.InputTracking.GetLocalPosition(UnityEngine.VR.VRNode.RightEye);
  142. #endif
  143. leftHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
  144. rightHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
  145. RaiseUpdatedAnchorsEvent();
  146. }
  147. protected virtual void RaiseUpdatedAnchorsEvent()
  148. {
  149. if (UpdatedAnchors != null)
  150. {
  151. UpdatedAnchors(this);
  152. }
  153. }
  154. public virtual void EnsureGameObjectIntegrity()
  155. {
  156. bool monoscopic = OVRManager.instance != null ? OVRManager.instance.monoscopic : false;
  157. if (trackingSpace == null)
  158. trackingSpace = ConfigureAnchor(null, trackingSpaceName);
  159. if (leftEyeAnchor == null)
  160. leftEyeAnchor = ConfigureAnchor(trackingSpace, leftEyeAnchorName);
  161. if (centerEyeAnchor == null)
  162. centerEyeAnchor = ConfigureAnchor(trackingSpace, centerEyeAnchorName);
  163. if (rightEyeAnchor == null)
  164. rightEyeAnchor = ConfigureAnchor(trackingSpace, rightEyeAnchorName);
  165. if (leftHandAnchor == null)
  166. leftHandAnchor = ConfigureAnchor(trackingSpace, leftHandAnchorName);
  167. if (rightHandAnchor == null)
  168. rightHandAnchor = ConfigureAnchor(trackingSpace, rightHandAnchorName);
  169. if (trackerAnchor == null)
  170. trackerAnchor = ConfigureAnchor(trackingSpace, trackerAnchorName);
  171. if (_centerEyeCamera == null || _leftEyeCamera == null || _rightEyeCamera == null)
  172. {
  173. _centerEyeCamera = centerEyeAnchor.GetComponent<Camera>();
  174. _leftEyeCamera = leftEyeAnchor.GetComponent<Camera>();
  175. _rightEyeCamera = rightEyeAnchor.GetComponent<Camera>();
  176. if (_centerEyeCamera == null)
  177. {
  178. _centerEyeCamera = centerEyeAnchor.gameObject.AddComponent<Camera>();
  179. _centerEyeCamera.tag = "MainCamera";
  180. }
  181. if (_leftEyeCamera == null)
  182. {
  183. _leftEyeCamera = leftEyeAnchor.gameObject.AddComponent<Camera>();
  184. _leftEyeCamera.tag = "MainCamera";
  185. }
  186. if (_rightEyeCamera == null)
  187. {
  188. _rightEyeCamera = rightEyeAnchor.gameObject.AddComponent<Camera>();
  189. _rightEyeCamera.tag = "MainCamera";
  190. }
  191. _centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Both;
  192. _leftEyeCamera.stereoTargetEye = StereoTargetEyeMask.Left;
  193. _rightEyeCamera.stereoTargetEye = StereoTargetEyeMask.Right;
  194. }
  195. if (monoscopic && !OVRPlugin.EyeTextureArrayEnabled)
  196. {
  197. // Output to left eye only when in monoscopic mode
  198. if (_centerEyeCamera.stereoTargetEye != StereoTargetEyeMask.Left)
  199. {
  200. _centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Left;
  201. }
  202. }
  203. else
  204. {
  205. if (_centerEyeCamera.stereoTargetEye != StereoTargetEyeMask.Both)
  206. {
  207. _centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Both;
  208. }
  209. }
  210. // disable the right eye camera when in monoscopic mode
  211. if (_centerEyeCamera.enabled == usePerEyeCameras ||
  212. _leftEyeCamera.enabled == !usePerEyeCameras ||
  213. _rightEyeCamera.enabled == !(usePerEyeCameras && (!monoscopic || OVRPlugin.EyeTextureArrayEnabled)))
  214. {
  215. _skipUpdate = true;
  216. }
  217. _centerEyeCamera.enabled = !usePerEyeCameras;
  218. _leftEyeCamera.enabled = usePerEyeCameras;
  219. _rightEyeCamera.enabled = (usePerEyeCameras && (!monoscopic || OVRPlugin.EyeTextureArrayEnabled));
  220. }
  221. protected virtual Transform ConfigureAnchor(Transform root, string name)
  222. {
  223. Transform anchor = (root != null) ? transform.Find(root.name + "/" + name) : null;
  224. if (anchor == null)
  225. {
  226. anchor = transform.Find(name);
  227. }
  228. if (anchor == null)
  229. {
  230. anchor = new GameObject(name).transform;
  231. }
  232. anchor.name = name;
  233. anchor.parent = (root != null) ? root : transform;
  234. anchor.localScale = Vector3.one;
  235. anchor.localPosition = Vector3.zero;
  236. anchor.localRotation = Quaternion.identity;
  237. return anchor;
  238. }
  239. public virtual Matrix4x4 ComputeTrackReferenceMatrix()
  240. {
  241. if (centerEyeAnchor == null)
  242. {
  243. Debug.LogError("centerEyeAnchor is required");
  244. return Matrix4x4.identity;
  245. }
  246. // The ideal approach would be using UnityEngine.VR.VRNode.TrackingReference, then we would not have to depend on the OVRCameraRig. Unfortunately, it is not available in Unity 5.4.3
  247. OVRPose headPose;
  248. #if UNITY_2017_2_OR_NEWER
  249. headPose.position = UnityEngine.XR.InputTracking.GetLocalPosition(UnityEngine.XR.XRNode.Head);
  250. headPose.orientation = UnityEngine.XR.InputTracking.GetLocalRotation(UnityEngine.XR.XRNode.Head);
  251. #else
  252. headPose.position = UnityEngine.VR.InputTracking.GetLocalPosition(UnityEngine.VR.VRNode.Head);
  253. headPose.orientation = UnityEngine.VR.InputTracking.GetLocalRotation(UnityEngine.VR.VRNode.Head);
  254. #endif
  255. OVRPose invHeadPose = headPose.Inverse();
  256. Matrix4x4 invHeadMatrix = Matrix4x4.TRS(invHeadPose.position, invHeadPose.orientation, Vector3.one);
  257. Matrix4x4 ret = centerEyeAnchor.localToWorldMatrix * invHeadMatrix;
  258. return ret;
  259. }
  260. }