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