SteamVR_PlayArea.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using Valve.VR;
  6. [ExecuteInEditMode]
  7. [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
  8. public class SteamVR_PlayArea : MonoBehaviour
  9. {
  10. public static bool GetBounds(SteamVR_PlayArea.Size size, ref HmdQuad_t pRect)
  11. {
  12. if (size == SteamVR_PlayArea.Size.Calibrated)
  13. {
  14. bool flag = !SteamVR.active && !SteamVR.usingNativeSupport;
  15. if (flag)
  16. {
  17. EVRInitError evrinitError = EVRInitError.None;
  18. OpenVR.Init(ref evrinitError, EVRApplicationType.VRApplication_Other);
  19. }
  20. CVRChaperone chaperone = OpenVR.Chaperone;
  21. bool flag2 = chaperone != null && chaperone.GetPlayAreaRect(ref pRect);
  22. if (!flag2)
  23. {
  24. Debug.LogWarning("Failed to get Calibrated Play Area bounds! Make sure you have tracking first, and that your space is calibrated.");
  25. }
  26. if (flag)
  27. {
  28. OpenVR.Shutdown();
  29. }
  30. return flag2;
  31. }
  32. try
  33. {
  34. string text = size.ToString().Substring(1);
  35. string[] array = text.Split(new char[]
  36. {
  37. 'x'
  38. }, 2);
  39. float num = float.Parse(array[0]) / 200f;
  40. float num2 = float.Parse(array[1]) / 200f;
  41. pRect.vCorners0.v0 = num;
  42. pRect.vCorners0.v1 = 0f;
  43. pRect.vCorners0.v2 = num2;
  44. pRect.vCorners1.v0 = num;
  45. pRect.vCorners1.v1 = 0f;
  46. pRect.vCorners1.v2 = -num2;
  47. pRect.vCorners2.v0 = -num;
  48. pRect.vCorners2.v1 = 0f;
  49. pRect.vCorners2.v2 = -num2;
  50. pRect.vCorners3.v0 = -num;
  51. pRect.vCorners3.v1 = 0f;
  52. pRect.vCorners3.v2 = num2;
  53. return true;
  54. }
  55. catch
  56. {
  57. }
  58. return false;
  59. }
  60. public void BuildMesh()
  61. {
  62. HmdQuad_t hmdQuad_t = default(HmdQuad_t);
  63. if (!SteamVR_PlayArea.GetBounds(this.size, ref hmdQuad_t))
  64. {
  65. return;
  66. }
  67. HmdVector3_t[] array = new HmdVector3_t[]
  68. {
  69. hmdQuad_t.vCorners0,
  70. hmdQuad_t.vCorners1,
  71. hmdQuad_t.vCorners2,
  72. hmdQuad_t.vCorners3
  73. };
  74. this.vertices = new Vector3[array.Length * 2];
  75. for (int i = 0; i < array.Length; i++)
  76. {
  77. HmdVector3_t hmdVector3_t = array[i];
  78. this.vertices[i] = new Vector3(hmdVector3_t.v0, 0.01f, hmdVector3_t.v2);
  79. }
  80. if (this.borderThickness == 0f)
  81. {
  82. base.GetComponent<MeshFilter>().mesh = null;
  83. return;
  84. }
  85. for (int j = 0; j < array.Length; j++)
  86. {
  87. int num = (j + 1) % array.Length;
  88. int num2 = (j + array.Length - 1) % array.Length;
  89. Vector3 normalized = (this.vertices[num] - this.vertices[j]).normalized;
  90. Vector3 normalized2 = (this.vertices[num2] - this.vertices[j]).normalized;
  91. Vector3 vector = this.vertices[j];
  92. vector += Vector3.Cross(normalized, Vector3.up) * this.borderThickness;
  93. vector += Vector3.Cross(normalized2, Vector3.down) * this.borderThickness;
  94. this.vertices[array.Length + j] = vector;
  95. }
  96. int[] triangles = new int[]
  97. {
  98. 0,
  99. 1,
  100. 4,
  101. 1,
  102. 5,
  103. 4,
  104. 1,
  105. 2,
  106. 5,
  107. 2,
  108. 6,
  109. 5,
  110. 2,
  111. 3,
  112. 6,
  113. 3,
  114. 7,
  115. 6,
  116. 3,
  117. 0,
  118. 7,
  119. 0,
  120. 4,
  121. 7
  122. };
  123. Vector2[] uv = new Vector2[]
  124. {
  125. new Vector2(0f, 0f),
  126. new Vector2(1f, 0f),
  127. new Vector2(0f, 0f),
  128. new Vector2(1f, 0f),
  129. new Vector2(0f, 1f),
  130. new Vector2(1f, 1f),
  131. new Vector2(0f, 1f),
  132. new Vector2(1f, 1f)
  133. };
  134. Color[] colors = new Color[]
  135. {
  136. this.color,
  137. this.color,
  138. this.color,
  139. this.color,
  140. new Color(this.color.r, this.color.g, this.color.b, 0f),
  141. new Color(this.color.r, this.color.g, this.color.b, 0f),
  142. new Color(this.color.r, this.color.g, this.color.b, 0f),
  143. new Color(this.color.r, this.color.g, this.color.b, 0f)
  144. };
  145. Mesh mesh = new Mesh();
  146. base.GetComponent<MeshFilter>().mesh = mesh;
  147. mesh.vertices = this.vertices;
  148. mesh.uv = uv;
  149. mesh.colors = colors;
  150. mesh.triangles = triangles;
  151. MeshRenderer component = base.GetComponent<MeshRenderer>();
  152. component.material = Resources.GetBuiltinResource<Material>("Sprites-Default.mat");
  153. component.reflectionProbeUsage = ReflectionProbeUsage.Off;
  154. component.shadowCastingMode = ShadowCastingMode.Off;
  155. component.receiveShadows = false;
  156. component.lightProbeUsage = LightProbeUsage.Off;
  157. }
  158. private void OnDrawGizmos()
  159. {
  160. if (!this.drawWireframeWhenSelectedOnly)
  161. {
  162. this.DrawWireframe();
  163. }
  164. }
  165. private void OnDrawGizmosSelected()
  166. {
  167. if (this.drawWireframeWhenSelectedOnly)
  168. {
  169. this.DrawWireframe();
  170. }
  171. }
  172. public void DrawWireframe()
  173. {
  174. if (this.vertices == null || this.vertices.Length == 0)
  175. {
  176. return;
  177. }
  178. Vector3 b = base.transform.TransformVector(Vector3.up * this.wireframeHeight);
  179. for (int i = 0; i < 4; i++)
  180. {
  181. int num = (i + 1) % 4;
  182. Vector3 vector = base.transform.TransformPoint(this.vertices[i]);
  183. Vector3 vector2 = vector + b;
  184. Vector3 vector3 = base.transform.TransformPoint(this.vertices[num]);
  185. Vector3 to = vector3 + b;
  186. Gizmos.DrawLine(vector, vector2);
  187. Gizmos.DrawLine(vector, vector3);
  188. Gizmos.DrawLine(vector2, to);
  189. }
  190. }
  191. public void OnEnable()
  192. {
  193. if (Application.isPlaying)
  194. {
  195. base.GetComponent<MeshRenderer>().enabled = this.drawInGame;
  196. base.enabled = false;
  197. if (this.drawInGame && this.size == SteamVR_PlayArea.Size.Calibrated)
  198. {
  199. base.StartCoroutine("UpdateBounds");
  200. }
  201. }
  202. }
  203. private IEnumerator UpdateBounds()
  204. {
  205. base.GetComponent<MeshFilter>().mesh = null;
  206. CVRChaperone chaperone = OpenVR.Chaperone;
  207. if (chaperone == null)
  208. {
  209. yield break;
  210. }
  211. while (chaperone.GetCalibrationState() != ChaperoneCalibrationState.OK)
  212. {
  213. yield return null;
  214. }
  215. this.BuildMesh();
  216. yield break;
  217. }
  218. public float borderThickness = 0.15f;
  219. public float wireframeHeight = 2f;
  220. public bool drawWireframeWhenSelectedOnly;
  221. public bool drawInGame = true;
  222. public SteamVR_PlayArea.Size size;
  223. public Color color = Color.cyan;
  224. [HideInInspector]
  225. public Vector3[] vertices;
  226. public enum Size
  227. {
  228. Calibrated,
  229. _400x300,
  230. _300x225,
  231. _200x150
  232. }
  233. }