LeapVRCameraControl.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. namespace Leap.Unity
  6. {
  7. [RequireComponent(typeof(Camera))]
  8. [ExecuteInEditMode]
  9. public class LeapVRCameraControl : MonoBehaviour
  10. {
  11. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  12. public static event Action<LeapVRCameraControl.CameraParams> OnValidCameraParams;
  13. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  14. public static event Action<Camera> OnLeftPreRender;
  15. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  16. public static event Action<Camera> OnRightPreRender;
  17. public bool OverrideEyePosition
  18. {
  19. get
  20. {
  21. return this._overrideEyePosition;
  22. }
  23. set
  24. {
  25. this._overrideEyePosition = value;
  26. }
  27. }
  28. private Camera _camera
  29. {
  30. get
  31. {
  32. if (this._cachedCamera == null)
  33. {
  34. this._cachedCamera = base.GetComponent<Camera>();
  35. }
  36. return this._cachedCamera;
  37. }
  38. }
  39. private void Start()
  40. {
  41. this._deviceInfo = new LeapDeviceInfo(LeapDeviceType.Peripheral);
  42. }
  43. private void Update()
  44. {
  45. LeapVRCameraControl._hasDispatchedValidCameraParams = false;
  46. }
  47. private void OnPreCull()
  48. {
  49. this._camera.ResetWorldToCameraMatrix();
  50. this._finalCenterMatrix = this._camera.worldToCameraMatrix;
  51. if (!LeapVRCameraControl._hasDispatchedValidCameraParams)
  52. {
  53. LeapVRCameraControl.CameraParams obj = new LeapVRCameraControl.CameraParams(this._cachedCamera);
  54. if (LeapVRCameraControl.OnValidCameraParams != null)
  55. {
  56. LeapVRCameraControl.OnValidCameraParams(obj);
  57. }
  58. LeapVRCameraControl._hasDispatchedValidCameraParams = true;
  59. }
  60. }
  61. private void OnPreRender()
  62. {
  63. this._eyeType.BeginCamera();
  64. if (this._eyeType.IsLeftEye)
  65. {
  66. Shader.SetGlobalVector("_LeapGlobalStereoUVOffset", LeapVRCameraControl.LEFT_EYE_UV_OFFSET);
  67. if (LeapVRCameraControl.OnLeftPreRender != null)
  68. {
  69. LeapVRCameraControl.OnLeftPreRender(this._cachedCamera);
  70. }
  71. }
  72. else
  73. {
  74. Shader.SetGlobalVector("_LeapGlobalStereoUVOffset", LeapVRCameraControl.RIGHT_EYE_UV_OFFSET);
  75. if (LeapVRCameraControl.OnRightPreRender != null)
  76. {
  77. LeapVRCameraControl.OnRightPreRender(this._cachedCamera);
  78. }
  79. }
  80. Matrix4x4 matrix4x;
  81. if (this._overrideEyePosition)
  82. {
  83. matrix4x = this._finalCenterMatrix;
  84. Vector3 a = (float)((!this._eyeType.IsLeftEye) ? -1 : 1) * base.transform.right * this._deviceInfo.baseline * 0.5f;
  85. Vector3 b = -base.transform.forward * this._deviceInfo.focalPlaneOffset;
  86. matrix4x *= Matrix4x4.TRS(a + b, Quaternion.identity, Vector3.one);
  87. }
  88. else
  89. {
  90. matrix4x = this._camera.worldToCameraMatrix;
  91. }
  92. this._camera.worldToCameraMatrix = matrix4x;
  93. }
  94. public const string GLOBAL_EYE_UV_OFFSET_NAME = "_LeapGlobalStereoUVOffset";
  95. private static Vector2 LEFT_EYE_UV_OFFSET = new Vector2(0f, 0f);
  96. private static Vector2 RIGHT_EYE_UV_OFFSET = new Vector2(0f, 0.5f);
  97. private static bool _hasDispatchedValidCameraParams = false;
  98. [SerializeField]
  99. private EyeType _eyeType = new EyeType(EyeType.OrderType.CENTER);
  100. [SerializeField]
  101. private bool _overrideEyePosition = true;
  102. private Camera _cachedCamera;
  103. private Matrix4x4 _finalCenterMatrix;
  104. private LeapDeviceInfo _deviceInfo;
  105. public struct CameraParams
  106. {
  107. public CameraParams(Camera camera)
  108. {
  109. this.CenterEyeTransform = camera.transform;
  110. this.ProjectionMatrix = camera.projectionMatrix;
  111. GraphicsDeviceType graphicsDeviceType = SystemInfo.graphicsDeviceType;
  112. if (graphicsDeviceType == GraphicsDeviceType.Direct3D9 || graphicsDeviceType == GraphicsDeviceType.Direct3D11 || graphicsDeviceType == GraphicsDeviceType.Direct3D12)
  113. {
  114. for (int i = 0; i < 4; i++)
  115. {
  116. this.ProjectionMatrix[1, i] = -this.ProjectionMatrix[1, i];
  117. }
  118. for (int j = 0; j < 4; j++)
  119. {
  120. this.ProjectionMatrix[2, j] = this.ProjectionMatrix[2, j] * 0.5f + this.ProjectionMatrix[3, j] * 0.5f;
  121. }
  122. }
  123. this.Width = camera.pixelWidth;
  124. this.Height = camera.pixelHeight;
  125. }
  126. public readonly Transform CenterEyeTransform;
  127. public readonly Matrix4x4 ProjectionMatrix;
  128. public readonly int Width;
  129. public readonly int Height;
  130. }
  131. }
  132. }