OVRCameraRig.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. using UnityEngine.VR;
  5. [ExecuteInEditMode]
  6. public class OVRCameraRig : MonoBehaviour
  7. {
  8. public Camera leftEyeCamera
  9. {
  10. get
  11. {
  12. return (!this.usePerEyeCameras) ? this._centerEyeCamera : this._leftEyeCamera;
  13. }
  14. }
  15. public Camera rightEyeCamera
  16. {
  17. get
  18. {
  19. return (!this.usePerEyeCameras) ? this._centerEyeCamera : this._rightEyeCamera;
  20. }
  21. }
  22. public Transform trackingSpace { get; private set; }
  23. public Transform leftEyeAnchor { get; private set; }
  24. public Transform centerEyeAnchor { get; private set; }
  25. public Transform rightEyeAnchor { get; private set; }
  26. public Transform leftHandAnchor { get; private set; }
  27. public Transform rightHandAnchor { get; private set; }
  28. public Transform trackerAnchor { get; private set; }
  29. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  30. public event Action<OVRCameraRig> UpdatedAnchors;
  31. private void Awake()
  32. {
  33. this.EnsureGameObjectIntegrity();
  34. }
  35. private void Start()
  36. {
  37. this.EnsureGameObjectIntegrity();
  38. if (!Application.isPlaying)
  39. {
  40. return;
  41. }
  42. this.UpdateAnchors();
  43. }
  44. private void Update()
  45. {
  46. this.EnsureGameObjectIntegrity();
  47. if (!Application.isPlaying)
  48. {
  49. return;
  50. }
  51. this.UpdateAnchors();
  52. }
  53. private void UpdateAnchors()
  54. {
  55. bool monoscopic = OVRManager.instance.monoscopic;
  56. OVRPose pose = OVRManager.tracker.GetPose(0);
  57. this.trackerAnchor.localRotation = pose.orientation;
  58. this.centerEyeAnchor.localRotation = InputTracking.GetLocalRotation(VRNode.CenterEye);
  59. this.leftEyeAnchor.localRotation = ((!monoscopic) ? InputTracking.GetLocalRotation(VRNode.LeftEye) : this.centerEyeAnchor.localRotation);
  60. this.rightEyeAnchor.localRotation = ((!monoscopic) ? InputTracking.GetLocalRotation(VRNode.RightEye) : this.centerEyeAnchor.localRotation);
  61. this.leftHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
  62. this.rightHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
  63. this.trackerAnchor.localPosition = pose.position;
  64. this.centerEyeAnchor.localPosition = InputTracking.GetLocalPosition(VRNode.CenterEye);
  65. this.leftEyeAnchor.localPosition = ((!monoscopic) ? InputTracking.GetLocalPosition(VRNode.LeftEye) : this.centerEyeAnchor.localPosition);
  66. this.rightEyeAnchor.localPosition = ((!monoscopic) ? InputTracking.GetLocalPosition(VRNode.RightEye) : this.centerEyeAnchor.localPosition);
  67. this.leftHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
  68. this.rightHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
  69. if (this.UpdatedAnchors != null)
  70. {
  71. this.UpdatedAnchors(this);
  72. }
  73. }
  74. public void EnsureGameObjectIntegrity()
  75. {
  76. if (this.trackingSpace == null)
  77. {
  78. this.trackingSpace = this.ConfigureRootAnchor(this.trackingSpaceName);
  79. }
  80. if (this.leftEyeAnchor == null)
  81. {
  82. this.leftEyeAnchor = this.ConfigureEyeAnchor(this.trackingSpace, VRNode.LeftEye);
  83. }
  84. if (this.centerEyeAnchor == null)
  85. {
  86. this.centerEyeAnchor = this.ConfigureEyeAnchor(this.trackingSpace, VRNode.CenterEye);
  87. }
  88. if (this.rightEyeAnchor == null)
  89. {
  90. this.rightEyeAnchor = this.ConfigureEyeAnchor(this.trackingSpace, VRNode.RightEye);
  91. }
  92. if (this.leftHandAnchor == null)
  93. {
  94. this.leftHandAnchor = this.ConfigureHandAnchor(this.trackingSpace, OVRPlugin.Node.HandLeft);
  95. }
  96. if (this.rightHandAnchor == null)
  97. {
  98. this.rightHandAnchor = this.ConfigureHandAnchor(this.trackingSpace, OVRPlugin.Node.HandRight);
  99. }
  100. if (this.trackerAnchor == null)
  101. {
  102. this.trackerAnchor = this.ConfigureTrackerAnchor(this.trackingSpace);
  103. }
  104. if (this._centerEyeCamera == null || this._leftEyeCamera == null || this._rightEyeCamera == null)
  105. {
  106. this._centerEyeCamera = this.centerEyeAnchor.GetComponent<Camera>();
  107. this._leftEyeCamera = this.leftEyeAnchor.GetComponent<Camera>();
  108. this._rightEyeCamera = this.rightEyeAnchor.GetComponent<Camera>();
  109. if (this._centerEyeCamera == null)
  110. {
  111. this._centerEyeCamera = this.centerEyeAnchor.gameObject.AddComponent<Camera>();
  112. this._centerEyeCamera.tag = "MainCamera";
  113. }
  114. if (this._leftEyeCamera == null)
  115. {
  116. this._leftEyeCamera = this.leftEyeAnchor.gameObject.AddComponent<Camera>();
  117. this._leftEyeCamera.tag = "MainCamera";
  118. }
  119. if (this._rightEyeCamera == null)
  120. {
  121. this._rightEyeCamera = this.rightEyeAnchor.gameObject.AddComponent<Camera>();
  122. this._rightEyeCamera.tag = "MainCamera";
  123. }
  124. this._centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Both;
  125. this._leftEyeCamera.stereoTargetEye = StereoTargetEyeMask.Left;
  126. this._rightEyeCamera.stereoTargetEye = StereoTargetEyeMask.Right;
  127. }
  128. this._centerEyeCamera.enabled = !this.usePerEyeCameras;
  129. this._leftEyeCamera.enabled = this.usePerEyeCameras;
  130. this._rightEyeCamera.enabled = this.usePerEyeCameras;
  131. }
  132. private Transform ConfigureRootAnchor(string name)
  133. {
  134. Transform transform = base.transform.Find(name);
  135. if (transform == null)
  136. {
  137. transform = new GameObject(name).transform;
  138. }
  139. transform.parent = base.transform;
  140. transform.localScale = Vector3.one;
  141. transform.localPosition = Vector3.zero;
  142. transform.localRotation = Quaternion.identity;
  143. return transform;
  144. }
  145. private Transform ConfigureEyeAnchor(Transform root, VRNode eye)
  146. {
  147. string str = (eye != VRNode.CenterEye) ? ((eye != VRNode.LeftEye) ? "Right" : "Left") : "Center";
  148. string text = str + this.eyeAnchorName;
  149. Transform transform = base.transform.Find(root.name + "/" + text);
  150. if (transform == null)
  151. {
  152. transform = base.transform.Find(text);
  153. }
  154. if (transform == null)
  155. {
  156. string name = this.legacyEyeAnchorName + eye.ToString();
  157. transform = base.transform.Find(name);
  158. }
  159. if (transform == null)
  160. {
  161. transform = new GameObject(text).transform;
  162. }
  163. transform.name = text;
  164. transform.parent = root;
  165. transform.localScale = Vector3.one;
  166. transform.localPosition = Vector3.zero;
  167. transform.localRotation = Quaternion.identity;
  168. return transform;
  169. }
  170. private Transform ConfigureHandAnchor(Transform root, OVRPlugin.Node hand)
  171. {
  172. string str = (hand != OVRPlugin.Node.HandLeft) ? "Right" : "Left";
  173. string text = str + this.handAnchorName;
  174. Transform transform = base.transform.Find(root.name + "/" + text);
  175. if (transform == null)
  176. {
  177. transform = base.transform.Find(text);
  178. }
  179. if (transform == null)
  180. {
  181. transform = new GameObject(text).transform;
  182. }
  183. transform.name = text;
  184. transform.parent = root;
  185. transform.localScale = Vector3.one;
  186. transform.localPosition = Vector3.zero;
  187. transform.localRotation = Quaternion.identity;
  188. return transform;
  189. }
  190. private Transform ConfigureTrackerAnchor(Transform root)
  191. {
  192. string text = this.trackerAnchorName;
  193. Transform transform = base.transform.Find(root.name + "/" + text);
  194. if (transform == null)
  195. {
  196. transform = new GameObject(text).transform;
  197. }
  198. transform.parent = root;
  199. transform.localScale = Vector3.one;
  200. transform.localPosition = Vector3.zero;
  201. transform.localRotation = Quaternion.identity;
  202. return transform;
  203. }
  204. public bool usePerEyeCameras;
  205. private readonly string trackingSpaceName = "TrackingSpace";
  206. private readonly string trackerAnchorName = "TrackerAnchor";
  207. private readonly string eyeAnchorName = "EyeAnchor";
  208. private readonly string handAnchorName = "HandAnchor";
  209. private readonly string legacyEyeAnchorName = "Camera";
  210. private Camera _centerEyeCamera;
  211. private Camera _leftEyeCamera;
  212. private Camera _rightEyeCamera;
  213. }