SteamVR_Frustum.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using UnityEngine;
  3. using Valve.VR;
  4. [ExecuteInEditMode]
  5. [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
  6. public class SteamVR_Frustum : MonoBehaviour
  7. {
  8. public void UpdateModel()
  9. {
  10. this.fovLeft = Mathf.Clamp(this.fovLeft, 1f, 89f);
  11. this.fovRight = Mathf.Clamp(this.fovRight, 1f, 89f);
  12. this.fovTop = Mathf.Clamp(this.fovTop, 1f, 89f);
  13. this.fovBottom = Mathf.Clamp(this.fovBottom, 1f, 89f);
  14. this.farZ = Mathf.Max(this.farZ, this.nearZ + 0.01f);
  15. this.nearZ = Mathf.Clamp(this.nearZ, 0.01f, this.farZ - 0.01f);
  16. float num = Mathf.Sin(-this.fovLeft * 0.017453292f);
  17. float num2 = Mathf.Sin(this.fovRight * 0.017453292f);
  18. float num3 = Mathf.Sin(this.fovTop * 0.017453292f);
  19. float num4 = Mathf.Sin(-this.fovBottom * 0.017453292f);
  20. float num5 = Mathf.Cos(-this.fovLeft * 0.017453292f);
  21. float num6 = Mathf.Cos(this.fovRight * 0.017453292f);
  22. float num7 = Mathf.Cos(this.fovTop * 0.017453292f);
  23. float num8 = Mathf.Cos(-this.fovBottom * 0.017453292f);
  24. Vector3[] array = new Vector3[]
  25. {
  26. new Vector3(num * this.nearZ / num5, num3 * this.nearZ / num7, this.nearZ),
  27. new Vector3(num2 * this.nearZ / num6, num3 * this.nearZ / num7, this.nearZ),
  28. new Vector3(num2 * this.nearZ / num6, num4 * this.nearZ / num8, this.nearZ),
  29. new Vector3(num * this.nearZ / num5, num4 * this.nearZ / num8, this.nearZ),
  30. new Vector3(num * this.farZ / num5, num3 * this.farZ / num7, this.farZ),
  31. new Vector3(num2 * this.farZ / num6, num3 * this.farZ / num7, this.farZ),
  32. new Vector3(num2 * this.farZ / num6, num4 * this.farZ / num8, this.farZ),
  33. new Vector3(num * this.farZ / num5, num4 * this.farZ / num8, this.farZ)
  34. };
  35. int[] array2 = new int[]
  36. {
  37. 0,
  38. 4,
  39. 7,
  40. 0,
  41. 7,
  42. 3,
  43. 0,
  44. 7,
  45. 4,
  46. 0,
  47. 3,
  48. 7,
  49. 1,
  50. 5,
  51. 6,
  52. 1,
  53. 6,
  54. 2,
  55. 1,
  56. 6,
  57. 5,
  58. 1,
  59. 2,
  60. 6,
  61. 0,
  62. 4,
  63. 5,
  64. 0,
  65. 5,
  66. 1,
  67. 0,
  68. 5,
  69. 4,
  70. 0,
  71. 1,
  72. 5,
  73. 2,
  74. 3,
  75. 7,
  76. 2,
  77. 7,
  78. 6,
  79. 2,
  80. 7,
  81. 3,
  82. 2,
  83. 6,
  84. 7
  85. };
  86. int num9 = 0;
  87. Vector3[] array3 = new Vector3[array2.Length];
  88. Vector3[] array4 = new Vector3[array2.Length];
  89. for (int i = 0; i < array2.Length / 3; i++)
  90. {
  91. Vector3 vector = array[array2[i * 3]];
  92. Vector3 vector2 = array[array2[i * 3 + 1]];
  93. Vector3 vector3 = array[array2[i * 3 + 2]];
  94. Vector3 normalized = Vector3.Cross(vector2 - vector, vector3 - vector).normalized;
  95. array4[i * 3] = normalized;
  96. array4[i * 3 + 1] = normalized;
  97. array4[i * 3 + 2] = normalized;
  98. array3[i * 3] = vector;
  99. array3[i * 3 + 1] = vector2;
  100. array3[i * 3 + 2] = vector3;
  101. array2[i * 3] = num9++;
  102. array2[i * 3 + 1] = num9++;
  103. array2[i * 3 + 2] = num9++;
  104. }
  105. Mesh mesh = new Mesh();
  106. mesh.vertices = array3;
  107. mesh.normals = array4;
  108. mesh.triangles = array2;
  109. base.GetComponent<MeshFilter>().mesh = mesh;
  110. }
  111. private void OnDeviceConnected(params object[] args)
  112. {
  113. int num = (int)args[0];
  114. if (num != (int)this.index)
  115. {
  116. return;
  117. }
  118. base.GetComponent<MeshFilter>().mesh = null;
  119. bool flag = (bool)args[1];
  120. if (flag)
  121. {
  122. CVRSystem system = OpenVR.System;
  123. if (system != null && system.GetTrackedDeviceClass((uint)num) == ETrackedDeviceClass.TrackingReference)
  124. {
  125. ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
  126. float floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewLeftDegrees_Float, ref etrackedPropertyError);
  127. if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
  128. {
  129. this.fovLeft = floatTrackedDeviceProperty;
  130. }
  131. floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewRightDegrees_Float, ref etrackedPropertyError);
  132. if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
  133. {
  134. this.fovRight = floatTrackedDeviceProperty;
  135. }
  136. floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewTopDegrees_Float, ref etrackedPropertyError);
  137. if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
  138. {
  139. this.fovTop = floatTrackedDeviceProperty;
  140. }
  141. floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewBottomDegrees_Float, ref etrackedPropertyError);
  142. if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
  143. {
  144. this.fovBottom = floatTrackedDeviceProperty;
  145. }
  146. floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_TrackingRangeMinimumMeters_Float, ref etrackedPropertyError);
  147. if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
  148. {
  149. this.nearZ = floatTrackedDeviceProperty;
  150. }
  151. floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_TrackingRangeMaximumMeters_Float, ref etrackedPropertyError);
  152. if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
  153. {
  154. this.farZ = floatTrackedDeviceProperty;
  155. }
  156. this.UpdateModel();
  157. }
  158. }
  159. }
  160. private void OnEnable()
  161. {
  162. base.GetComponent<MeshFilter>().mesh = null;
  163. SteamVR_Utils.Event.Listen("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  164. }
  165. private void OnDisable()
  166. {
  167. SteamVR_Utils.Event.Remove("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  168. base.GetComponent<MeshFilter>().mesh = null;
  169. }
  170. public SteamVR_TrackedObject.EIndex index;
  171. public float fovLeft = 45f;
  172. public float fovRight = 45f;
  173. public float fovTop = 45f;
  174. public float fovBottom = 45f;
  175. public float nearZ = 0.5f;
  176. public float farZ = 2.5f;
  177. }