LeapVRCameraControl.cs 3.9 KB

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