1+ using System ;
2+ using UnityEngine ;
3+ using Valve . VR ;
4+
5+ public class SteamVR_FirstPersonOverlay : MonoBehaviour
6+ {
7+ private ulong handle = OpenVR . k_ulOverlayHandleInvalid ;
8+ private RenderTexture renderTexture ;
9+ private Texture_t textureData ;
10+
11+ private void Start ( )
12+ {
13+ if ( OpenVR . Overlay == null )
14+ {
15+ Debug . LogError ( "Failed to retrieve OpenVR.Overlay." ) ;
16+ return ;
17+ }
18+ handle = OpenVR . k_ulOverlayHandleInvalid ;
19+ EVROverlayError error ;
20+
21+ error = OpenVR . Overlay . CreateOverlay ( "FirstPersonOverlayKey" , "FirstPersonOverlay" , ref handle ) ;
22+ if ( error != EVROverlayError . None )
23+ {
24+ Debug . LogError ( "OpenVR.Overlay.CreateOverlay() failed with error: " + error . ToString ( ) ) ;
25+ return ;
26+ }
27+
28+ VRTextureBounds_t bounds = new VRTextureBounds_t
29+ {
30+ uMin = 0 ,
31+ uMax = 1 ,
32+ vMin = 1 ,
33+ vMax = 0
34+ } ;
35+
36+ error = OpenVR . Overlay . SetOverlayTextureBounds ( handle , ref bounds ) ;
37+ if ( error != EVROverlayError . None )
38+ {
39+ Debug . LogError ( "OpenVR.Overlay.SetOverlayTextureBounds() failed with error: " + error . ToString ( ) ) ;
40+ return ;
41+ }
42+
43+ error = OpenVR . Overlay . SetOverlayWidthInMeters ( handle , 0.5f ) ;
44+ if ( error != EVROverlayError . None )
45+ {
46+ Debug . LogError ( "OpenVR.Overlay.SetOverlayWidthInMeters() failed with error: " + error . ToString ( ) ) ;
47+ return ;
48+ }
49+
50+ Camera camera = Camera . main ;
51+ renderTexture = new RenderTexture ( 512 , 512 , 16 , RenderTextureFormat . ARGBFloat ) ;
52+ camera . targetTexture = renderTexture ;
53+
54+ IntPtr nativeTexturePtr = renderTexture . GetNativeTexturePtr ( ) ;
55+ textureData = new Texture_t
56+ {
57+ eColorSpace = EColorSpace . Auto ,
58+ eType = GetTextureType ( ) ,
59+ handle = nativeTexturePtr
60+ } ;
61+
62+
63+ error = OpenVR . Overlay . SetOverlayTexture ( handle , ref textureData ) ;
64+ if ( error != EVROverlayError . None )
65+ {
66+ Debug . LogError ( "OpenVR.Overlay.SetOverlayTexture() failed with error: " + error . ToString ( ) ) ;
67+ return ;
68+ }
69+
70+ HmdMatrix34_t matrix = new SteamVR_Utils . RigidTransform ( new Vector3 ( 0 , 0 , 1 ) , Quaternion . identity ) . ToHmdMatrix34 ( ) ;
71+ error = OpenVR . Overlay . SetOverlayTransformTrackedDeviceRelative ( handle , 0 , ref matrix ) ; //tracked device 0 is the hmd
72+ if ( error != EVROverlayError . None )
73+ {
74+ Debug . LogError ( "OpenVR.Overlay.SetOverlayTransformTrackedDeviceRelative() failed with error: " + error . ToString ( ) ) ;
75+ return ;
76+ }
77+
78+ error = OpenVR . Overlay . ShowOverlay ( handle ) ;
79+ if ( error != EVROverlayError . None )
80+ {
81+ Debug . LogError ( "OpenVR.Overlay.ShowOverlay() failed with error: " + error . ToString ( ) ) ;
82+ return ;
83+ }
84+ }
85+
86+ private EVROverlayError setTextureError ;
87+ private void Update ( )
88+ {
89+ if ( OpenVR . Overlay != null )
90+ {
91+ setTextureError = OpenVR . Overlay . SetOverlayTexture ( handle , ref textureData ) ;
92+ if ( setTextureError != EVROverlayError . None )
93+ {
94+ Debug . LogError ( "OpenVR.Overlay.SetOverlayTexture() failed with error: " + setTextureError . ToString ( ) ) ;
95+ }
96+ }
97+ }
98+
99+ private void OnDisable ( )
100+ {
101+ if ( OpenVR . Overlay != null && handle != OpenVR . k_ulOverlayHandleInvalid )
102+ {
103+ EVROverlayError error = OpenVR . Overlay . DestroyOverlay ( handle ) ;
104+ if ( error != EVROverlayError . None )
105+ {
106+ Debug . LogError ( "OpenVR.Overlay.DestroyOverlay() failed with error: " + error . ToString ( ) ) ;
107+ }
108+ else
109+ {
110+ handle = OpenVR . k_ulOverlayHandleInvalid ;
111+ }
112+ }
113+ }
114+
115+ private ETextureType GetTextureType ( )
116+ {
117+ switch ( SystemInfo . graphicsDeviceType )
118+ {
119+ case UnityEngine . Rendering . GraphicsDeviceType . Direct3D11 :
120+ return ETextureType . DirectX ;
121+ case UnityEngine . Rendering . GraphicsDeviceType . Direct3D12 :
122+ return ETextureType . DirectX12 ;
123+ case UnityEngine . Rendering . GraphicsDeviceType . Vulkan :
124+ return ETextureType . Vulkan ;
125+ case UnityEngine . Rendering . GraphicsDeviceType . OpenGLCore :
126+ return ETextureType . OpenGL ;
127+
128+ default :
129+ Debug . LogError ( "Unsupported graphics device: " + SystemInfo . graphicsDeviceType . ToString ( ) ) ;
130+ return ETextureType . Invalid ;
131+ }
132+ }
133+
134+
135+ #if UNITY_EDITOR
136+ private void OnEnable ( )
137+ {
138+ #if ! UNITY_2019_1_OR_NEWER || ! OPENVR_XR_API
139+ Debug . LogError ( "This example requires Unity 2019+ and the OpenVR plugin for Unity XR." ) ;
140+
141+ UnityEditor . EditorApplication . isPlaying = false ;
142+ this . enabled = false ;
143+ return ;
144+ #endif
145+ #if OPENVR_XR_API
146+ if ( Unity . XR . OpenVR . OpenVRSettings . GetSettings ( ) . InitializationType != Unity . XR . OpenVR . OpenVRSettings . InitializationTypes . Overlay )
147+ {
148+ Debug . LogError ( "You must set the OpenVR Application Type to Overlay for this example to work. (Project Settings -> XR Plug-in Management -> OpenVR -> Application Type)" ) ;
149+
150+ this . enabled = false ;
151+ UnityEditor . EditorApplication . isPlaying = false ;
152+ }
153+ #endif
154+ }
155+ #endif
156+ }
0 commit comments