Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.2 KiB

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