2
0

SteamVR_Overlay.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Displays 2d content on a large virtual screen.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. using Valve.VR;
  9. namespace Valve.VR
  10. {
  11. public class SteamVR_Overlay : MonoBehaviour
  12. {
  13. public Texture texture;
  14. [Tooltip("Size of overlay view.")]
  15. public float scale = 3.0f;
  16. [Tooltip("Distance from surface.")]
  17. public float distance = 1.25f;
  18. [Tooltip("Opacity"), Range(0.0f, 1.0f)]
  19. public float alpha = 1.0f;
  20. public Vector4 uvOffset = new Vector4(0, 0, 1, 1);
  21. public Vector2 mouseScale = new Vector2(1, 1);
  22. public VROverlayInputMethod inputMethod = VROverlayInputMethod.None;
  23. static public SteamVR_Overlay instance { get; private set; }
  24. static public string key { get { return "unity:" + Application.companyName + "." + Application.productName; } }
  25. private ulong handle = OpenVR.k_ulOverlayHandleInvalid;
  26. void OnEnable()
  27. {
  28. var overlay = OpenVR.Overlay;
  29. if (overlay != null)
  30. {
  31. var error = overlay.CreateOverlay(key, gameObject.name, ref handle);
  32. if (error != EVROverlayError.None)
  33. {
  34. Debug.Log("<b>[SteamVR]</b> " + overlay.GetOverlayErrorNameFromEnum(error));
  35. enabled = false;
  36. return;
  37. }
  38. }
  39. SteamVR_Overlay.instance = this;
  40. }
  41. void OnDisable()
  42. {
  43. if (handle != OpenVR.k_ulOverlayHandleInvalid)
  44. {
  45. var overlay = OpenVR.Overlay;
  46. if (overlay != null)
  47. {
  48. overlay.DestroyOverlay(handle);
  49. }
  50. handle = OpenVR.k_ulOverlayHandleInvalid;
  51. }
  52. SteamVR_Overlay.instance = null;
  53. }
  54. public void UpdateOverlay()
  55. {
  56. var overlay = OpenVR.Overlay;
  57. if (overlay == null)
  58. return;
  59. if (texture != null)
  60. {
  61. var error = overlay.ShowOverlay(handle);
  62. if (error == EVROverlayError.InvalidHandle || error == EVROverlayError.UnknownOverlay)
  63. {
  64. if (overlay.FindOverlay(key, ref handle) != EVROverlayError.None)
  65. return;
  66. }
  67. var tex = new Texture_t();
  68. tex.handle = texture.GetNativeTexturePtr();
  69. tex.eType = SteamVR.instance.textureType;
  70. tex.eColorSpace = EColorSpace.Auto;
  71. overlay.SetOverlayTexture(handle, ref tex);
  72. overlay.SetOverlayAlpha(handle, alpha);
  73. overlay.SetOverlayWidthInMeters(handle, scale);
  74. var textureBounds = new VRTextureBounds_t();
  75. textureBounds.uMin = (0 + uvOffset.x) * uvOffset.z;
  76. textureBounds.vMin = (1 + uvOffset.y) * uvOffset.w;
  77. textureBounds.uMax = (1 + uvOffset.x) * uvOffset.z;
  78. textureBounds.vMax = (0 + uvOffset.y) * uvOffset.w;
  79. overlay.SetOverlayTextureBounds(handle, ref textureBounds);
  80. var vecMouseScale = new HmdVector2_t();
  81. vecMouseScale.v0 = mouseScale.x;
  82. vecMouseScale.v1 = mouseScale.y;
  83. overlay.SetOverlayMouseScale(handle, ref vecMouseScale);
  84. var vrcam = SteamVR_Render.Top();
  85. if (vrcam != null && vrcam.origin != null)
  86. {
  87. var offset = new SteamVR_Utils.RigidTransform(vrcam.origin, transform);
  88. offset.pos.x /= vrcam.origin.localScale.x;
  89. offset.pos.y /= vrcam.origin.localScale.y;
  90. offset.pos.z /= vrcam.origin.localScale.z;
  91. offset.pos.z += distance;
  92. var t = offset.ToHmdMatrix34();
  93. overlay.SetOverlayTransformAbsolute(handle, SteamVR.settings.trackingSpace, ref t);
  94. }
  95. overlay.SetOverlayInputMethod(handle, inputMethod);
  96. }
  97. else
  98. {
  99. overlay.HideOverlay(handle);
  100. }
  101. }
  102. public bool PollNextEvent(ref VREvent_t pEvent)
  103. {
  104. var overlay = OpenVR.Overlay;
  105. if (overlay == null)
  106. return false;
  107. var size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Valve.VR.VREvent_t));
  108. return overlay.PollNextOverlayEvent(handle, ref pEvent, size);
  109. }
  110. public struct IntersectionResults
  111. {
  112. public Vector3 point;
  113. public Vector3 normal;
  114. public Vector2 UVs;
  115. public float distance;
  116. }
  117. public bool ComputeIntersection(Vector3 source, Vector3 direction, ref IntersectionResults results)
  118. {
  119. var overlay = OpenVR.Overlay;
  120. if (overlay == null)
  121. return false;
  122. var input = new VROverlayIntersectionParams_t();
  123. input.eOrigin = SteamVR.settings.trackingSpace;
  124. input.vSource.v0 = source.x;
  125. input.vSource.v1 = source.y;
  126. input.vSource.v2 = -source.z;
  127. input.vDirection.v0 = direction.x;
  128. input.vDirection.v1 = direction.y;
  129. input.vDirection.v2 = -direction.z;
  130. var output = new VROverlayIntersectionResults_t();
  131. if (!overlay.ComputeOverlayIntersection(handle, ref input, ref output))
  132. return false;
  133. results.point = new Vector3(output.vPoint.v0, output.vPoint.v1, -output.vPoint.v2);
  134. results.normal = new Vector3(output.vNormal.v0, output.vNormal.v1, -output.vNormal.v2);
  135. results.UVs = new Vector2(output.vUVs.v0, output.vUVs.v1);
  136. results.distance = output.fDistance;
  137. return true;
  138. }
  139. }
  140. }