OVRCameraRig.cs 7.3 KB

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