SteamVR_Overlay.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. using Valve.VR;
  5. public class SteamVR_Overlay : MonoBehaviour
  6. {
  7. public static SteamVR_Overlay instance { get; private set; }
  8. public static string key
  9. {
  10. get
  11. {
  12. return "unity:" + Application.companyName + "." + Application.productName;
  13. }
  14. }
  15. private void OnEnable()
  16. {
  17. CVROverlay overlay = OpenVR.Overlay;
  18. if (overlay != null)
  19. {
  20. EVROverlayError evroverlayError = overlay.CreateOverlay(SteamVR_Overlay.key, base.gameObject.name, ref this.handle);
  21. if (evroverlayError != EVROverlayError.None)
  22. {
  23. Debug.Log(overlay.GetOverlayErrorNameFromEnum(evroverlayError));
  24. base.enabled = false;
  25. return;
  26. }
  27. }
  28. SteamVR_Overlay.instance = this;
  29. }
  30. private void OnDisable()
  31. {
  32. if (this.handle != 0UL)
  33. {
  34. CVROverlay overlay = OpenVR.Overlay;
  35. if (overlay != null)
  36. {
  37. overlay.DestroyOverlay(this.handle);
  38. }
  39. this.handle = 0UL;
  40. }
  41. SteamVR_Overlay.instance = null;
  42. }
  43. public void UpdateOverlay()
  44. {
  45. CVROverlay overlay = OpenVR.Overlay;
  46. if (overlay == null)
  47. {
  48. return;
  49. }
  50. if (this.texture != null)
  51. {
  52. EVROverlayError evroverlayError = overlay.ShowOverlay(this.handle);
  53. if ((evroverlayError == EVROverlayError.InvalidHandle || evroverlayError == EVROverlayError.UnknownOverlay) && overlay.FindOverlay(SteamVR_Overlay.key, ref this.handle) != EVROverlayError.None)
  54. {
  55. return;
  56. }
  57. Texture_t texture_t = default(Texture_t);
  58. texture_t.handle = this.texture.GetNativeTexturePtr();
  59. texture_t.eType = SteamVR.instance.graphicsAPI;
  60. texture_t.eColorSpace = EColorSpace.Auto;
  61. overlay.SetOverlayTexture(this.handle, ref texture_t);
  62. overlay.SetOverlayAlpha(this.handle, this.alpha);
  63. overlay.SetOverlayWidthInMeters(this.handle, this.scale);
  64. overlay.SetOverlayAutoCurveDistanceRangeInMeters(this.handle, this.curvedRange.x, this.curvedRange.y);
  65. VRTextureBounds_t vrtextureBounds_t = default(VRTextureBounds_t);
  66. vrtextureBounds_t.uMin = this.uvOffset.x * this.uvOffset.z;
  67. vrtextureBounds_t.vMin = (1f + this.uvOffset.y) * this.uvOffset.w;
  68. vrtextureBounds_t.uMax = (1f + this.uvOffset.x) * this.uvOffset.z;
  69. vrtextureBounds_t.vMax = this.uvOffset.y * this.uvOffset.w;
  70. overlay.SetOverlayTextureBounds(this.handle, ref vrtextureBounds_t);
  71. HmdVector2_t hmdVector2_t = default(HmdVector2_t);
  72. hmdVector2_t.v0 = this.mouseScale.x;
  73. hmdVector2_t.v1 = this.mouseScale.y;
  74. overlay.SetOverlayMouseScale(this.handle, ref hmdVector2_t);
  75. SteamVR_Camera steamVR_Camera = SteamVR_Render.Top();
  76. if (steamVR_Camera != null && steamVR_Camera.origin != null)
  77. {
  78. SteamVR_Utils.RigidTransform rigidTransform = new SteamVR_Utils.RigidTransform(steamVR_Camera.origin, base.transform);
  79. rigidTransform.pos.x = rigidTransform.pos.x / steamVR_Camera.origin.localScale.x;
  80. rigidTransform.pos.y = rigidTransform.pos.y / steamVR_Camera.origin.localScale.y;
  81. rigidTransform.pos.z = rigidTransform.pos.z / steamVR_Camera.origin.localScale.z;
  82. rigidTransform.pos.z = rigidTransform.pos.z + this.distance;
  83. HmdMatrix34_t hmdMatrix34_t = rigidTransform.ToHmdMatrix34();
  84. overlay.SetOverlayTransformAbsolute(this.handle, SteamVR_Render.instance.trackingSpace, ref hmdMatrix34_t);
  85. }
  86. overlay.SetOverlayInputMethod(this.handle, this.inputMethod);
  87. if (this.curved || this.antialias)
  88. {
  89. this.highquality = true;
  90. }
  91. if (this.highquality)
  92. {
  93. overlay.SetHighQualityOverlay(this.handle);
  94. overlay.SetOverlayFlag(this.handle, VROverlayFlags.Curved, this.curved);
  95. overlay.SetOverlayFlag(this.handle, VROverlayFlags.RGSS4X, this.antialias);
  96. }
  97. else if (overlay.GetHighQualityOverlay() == this.handle)
  98. {
  99. overlay.SetHighQualityOverlay(0UL);
  100. }
  101. }
  102. else
  103. {
  104. overlay.HideOverlay(this.handle);
  105. }
  106. }
  107. public bool PollNextEvent(ref VREvent_t pEvent)
  108. {
  109. CVROverlay overlay = OpenVR.Overlay;
  110. if (overlay == null)
  111. {
  112. return false;
  113. }
  114. uint uncbVREvent = (uint)Marshal.SizeOf(typeof(VREvent_t));
  115. return overlay.PollNextOverlayEvent(this.handle, ref pEvent, uncbVREvent);
  116. }
  117. public bool ComputeIntersection(Vector3 source, Vector3 direction, ref SteamVR_Overlay.IntersectionResults results)
  118. {
  119. CVROverlay overlay = OpenVR.Overlay;
  120. if (overlay == null)
  121. {
  122. return false;
  123. }
  124. VROverlayIntersectionParams_t vroverlayIntersectionParams_t = default(VROverlayIntersectionParams_t);
  125. vroverlayIntersectionParams_t.eOrigin = SteamVR_Render.instance.trackingSpace;
  126. vroverlayIntersectionParams_t.vSource.v0 = source.x;
  127. vroverlayIntersectionParams_t.vSource.v1 = source.y;
  128. vroverlayIntersectionParams_t.vSource.v2 = -source.z;
  129. vroverlayIntersectionParams_t.vDirection.v0 = direction.x;
  130. vroverlayIntersectionParams_t.vDirection.v1 = direction.y;
  131. vroverlayIntersectionParams_t.vDirection.v2 = -direction.z;
  132. VROverlayIntersectionResults_t vroverlayIntersectionResults_t = default(VROverlayIntersectionResults_t);
  133. if (!overlay.ComputeOverlayIntersection(this.handle, ref vroverlayIntersectionParams_t, ref vroverlayIntersectionResults_t))
  134. {
  135. return false;
  136. }
  137. results.point = new Vector3(vroverlayIntersectionResults_t.vPoint.v0, vroverlayIntersectionResults_t.vPoint.v1, -vroverlayIntersectionResults_t.vPoint.v2);
  138. results.normal = new Vector3(vroverlayIntersectionResults_t.vNormal.v0, vroverlayIntersectionResults_t.vNormal.v1, -vroverlayIntersectionResults_t.vNormal.v2);
  139. results.UVs = new Vector2(vroverlayIntersectionResults_t.vUVs.v0, vroverlayIntersectionResults_t.vUVs.v1);
  140. results.distance = vroverlayIntersectionResults_t.fDistance;
  141. return true;
  142. }
  143. public Texture texture;
  144. public bool curved = true;
  145. public bool antialias = true;
  146. public bool highquality = true;
  147. public float scale = 3f;
  148. public float distance = 1.25f;
  149. public float alpha = 1f;
  150. public Vector4 uvOffset = new Vector4(0f, 0f, 1f, 1f);
  151. public Vector2 mouseScale = new Vector2(1f, 1f);
  152. public Vector2 curvedRange = new Vector2(1f, 2f);
  153. public VROverlayInputMethod inputMethod;
  154. private ulong handle;
  155. public struct IntersectionResults
  156. {
  157. public Vector3 point;
  158. public Vector3 normal;
  159. public Vector2 UVs;
  160. public float distance;
  161. }
  162. }