// Helper to create (or reuse if possible) each of our different overlay types.
        ulong GetOverlayHandle(string overlayName, Transform transform, float widthInMeters = 1.0f)
        {
            ulong handle = OpenVR.k_ulOverlayHandleInvalid;

            var overlay = OpenVR.Overlay;

            if (overlay == null)
            {
                return(handle);
            }

            var key = SteamVR_Overlay.key + "." + overlayName;

            var error = overlay.FindOverlay(key, ref handle);

            if (error != EVROverlayError.None)
            {
                error = overlay.CreateOverlay(key, overlayName, ref handle);
            }
            if (error == EVROverlayError.None)
            {
                overlay.ShowOverlay(handle);
                overlay.SetOverlayAlpha(handle, alpha);
                overlay.SetOverlayWidthInMeters(handle, widthInMeters);

                // D3D textures are upside-down in Unity to match OpenGL.
                if (SteamVR.instance.textureType == ETextureType.DirectX)
                {
                    var textureBounds = new VRTextureBounds_t();
                    textureBounds.uMin = 0;
                    textureBounds.vMin = 1;
                    textureBounds.uMax = 1;
                    textureBounds.vMax = 0;
                    overlay.SetOverlayTextureBounds(handle, ref textureBounds);
                }

                // Convert from world space to tracking space using the top-most camera.
                var vrcam = (loadingScreenDistance == 0.0f) ? SteamVR_Render.Top() : null;
                if (vrcam != null && vrcam.origin != null)
                {
                    var offset = new SteamVR_Utils.RigidTransform(vrcam.origin, transform);
                    offset.pos.x /= vrcam.origin.localScale.x;
                    offset.pos.y /= vrcam.origin.localScale.y;
                    offset.pos.z /= vrcam.origin.localScale.z;

                    var t = offset.ToHmdMatrix34();
                    overlay.SetOverlayTransformAbsolute(handle, SteamVR_Render.instance.trackingSpace, ref t);
                }
                else
                {
                    var t = new SteamVR_Utils.RigidTransform(transform).ToHmdMatrix34();
                    overlay.SetOverlayTransformAbsolute(handle, SteamVR_Render.instance.trackingSpace, ref t);
                }
            }

            return(handle);
        }
Beispiel #2
0
        void SetScale(float scale)
        {
            this.scale = scale;

            var tracker = SteamVR_Render.Top();

            if (tracker != null)
            {
                tracker.origin.localScale = new Vector3(scale, scale, scale);
            }
        }
Beispiel #3
0
        public void ShowMenu()
        {
            var overlay = SteamVR_Overlay.instance;

            if (overlay == null)
            {
                return;
            }

            var texture = overlay.texture as RenderTexture;

            if (texture == null)
            {
                Debug.LogError("Menu requires overlay texture to be a render texture.");
                return;
            }

            SaveCursorState();

            Cursor.visible   = true;
            Cursor.lockState = CursorLockMode.None;

            this.overlay = overlay;
            uvOffset     = overlay.uvOffset;
            distance     = overlay.distance;

            // If an existing camera is rendering into the overlay texture, we need
            // to temporarily disable it to keep it from clearing the texture on us.
            var cameras = Object.FindObjectsOfType(typeof(Camera)) as Camera[];

            foreach (var cam in cameras)
            {
                if (cam.enabled && cam.targetTexture == texture)
                {
                    overlayCam         = cam;
                    overlayCam.enabled = false;
                    break;
                }
            }

            var tracker = SteamVR_Render.Top();

            if (tracker != null)
            {
                scale = tracker.origin.localScale.x;
            }
        }
Beispiel #4
0
        public void UpdateOverlay()
        {
            var overlay = OpenVR.Overlay;

            if (overlay == null)
            {
                return;
            }

            if (texture != null)
            {
                var error = overlay.ShowOverlay(handle);
                if (error == EVROverlayError.InvalidHandle || error == EVROverlayError.UnknownOverlay)
                {
                    if (overlay.FindOverlay(key, ref handle) != EVROverlayError.None)
                    {
                        return;
                    }
                }

                var tex = new Texture_t();
                tex.handle      = texture.GetNativeTexturePtr();
                tex.eType       = SteamVR.instance.textureType;
                tex.eColorSpace = EColorSpace.Auto;
                overlay.SetOverlayTexture(handle, ref tex);

                overlay.SetOverlayAlpha(handle, alpha);
                overlay.SetOverlayWidthInMeters(handle, scale);
                overlay.SetOverlayAutoCurveDistanceRangeInMeters(handle, curvedRange.x, curvedRange.y);

                var textureBounds = new VRTextureBounds_t();
                textureBounds.uMin = (0 + uvOffset.x) * uvOffset.z;
                textureBounds.vMin = (1 + uvOffset.y) * uvOffset.w;
                textureBounds.uMax = (1 + uvOffset.x) * uvOffset.z;
                textureBounds.vMax = (0 + uvOffset.y) * uvOffset.w;
                overlay.SetOverlayTextureBounds(handle, ref textureBounds);

                var vecMouseScale = new HmdVector2_t();
                vecMouseScale.v0 = mouseScale.x;
                vecMouseScale.v1 = mouseScale.y;
                overlay.SetOverlayMouseScale(handle, ref vecMouseScale);

                var vrcam = SteamVR_Render.Top();
                if (vrcam != null && vrcam.origin != null)
                {
                    var offset = new SteamVR_Utils.RigidTransform(vrcam.origin, transform);
                    offset.pos.x /= vrcam.origin.localScale.x;
                    offset.pos.y /= vrcam.origin.localScale.y;
                    offset.pos.z /= vrcam.origin.localScale.z;

                    offset.pos.z += distance;

                    var t = offset.ToHmdMatrix34();
                    overlay.SetOverlayTransformAbsolute(handle, SteamVR_Render.instance.trackingSpace, ref t);
                }

                overlay.SetOverlayInputMethod(handle, inputMethod);

                if (curved || antialias)
                {
                    highquality = true;
                }

                if (highquality)
                {
                    overlay.SetHighQualityOverlay(handle);
                    overlay.SetOverlayFlag(handle, VROverlayFlags.Curved, curved);
                    overlay.SetOverlayFlag(handle, VROverlayFlags.RGSS4X, antialias);
                }
                else if (overlay.GetHighQualityOverlay() == handle)
                {
                    overlay.SetHighQualityOverlay(OpenVR.k_ulOverlayHandleInvalid);
                }
            }
            else
            {
                overlay.HideOverlay(handle);
            }
        }
Beispiel #5
0
        void OnGUI()
        {
            if (overlay == null)
            {
                return;
            }

            var texture = overlay.texture as RenderTexture;

            var prevActive = RenderTexture.active;

            RenderTexture.active = texture;

            if (Event.current.type == EventType.Repaint)
            {
                GL.Clear(false, true, Color.clear);
            }

            var area = new Rect(0, 0, texture.width, texture.height);

            // Account for screen smaller than texture (since mouse position gets clamped)
            if (Screen.width < texture.width)
            {
                area.width         = Screen.width;
                overlay.uvOffset.x = -(float)(texture.width - Screen.width) / (2 * texture.width);
            }
            if (Screen.height < texture.height)
            {
                area.height        = Screen.height;
                overlay.uvOffset.y = (float)(texture.height - Screen.height) / (2 * texture.height);
            }

            GUILayout.BeginArea(area);

            if (background != null)
            {
                GUI.DrawTexture(new Rect(
                                    (area.width - background.width) / 2,
                                    (area.height - background.height) / 2,
                                    background.width, background.height), background);
            }

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();

            if (logo != null)
            {
                GUILayout.Space(area.height / 2 - logoHeight);
                GUILayout.Box(logo);
            }

            GUILayout.Space(menuOffset);

            bool bHideMenu = GUILayout.Button("[Esc] - Close menu");

            GUILayout.BeginHorizontal();
            GUILayout.Label(string.Format("Scale: {0:N4}", scale));
            {
                var result = GUILayout.HorizontalSlider(scale, scaleLimits.x, scaleLimits.y);
                if (result != scale)
                {
                    SetScale(result);
                }
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label(string.Format("Scale limits:"));
            {
                var result = GUILayout.TextField(scaleLimitX);
                if (result != scaleLimitX)
                {
                    if (float.TryParse(result, out scaleLimits.x))
                    {
                        scaleLimitX = result;
                    }
                }
            }
            {
                var result = GUILayout.TextField(scaleLimitY);
                if (result != scaleLimitY)
                {
                    if (float.TryParse(result, out scaleLimits.y))
                    {
                        scaleLimitY = result;
                    }
                }
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label(string.Format("Scale rate:"));
            {
                var result = GUILayout.TextField(scaleRateText);
                if (result != scaleRateText)
                {
                    if (float.TryParse(result, out scaleRate))
                    {
                        scaleRateText = result;
                    }
                }
            }
            GUILayout.EndHorizontal();

            if (SteamVR.active)
            {
                var vr = SteamVR.instance;

                GUILayout.BeginHorizontal();
                {
                    var t   = SteamVR_Camera.sceneResolutionScale;
                    int w   = (int)(vr.sceneWidth * t);
                    int h   = (int)(vr.sceneHeight * t);
                    int pct = (int)(100.0f * t);
                    GUILayout.Label(string.Format("Scene quality: {0}x{1} ({2}%)", w, h, pct));
                    var result = Mathf.RoundToInt(GUILayout.HorizontalSlider(pct, 50, 200));
                    if (result != pct)
                    {
                        SteamVR_Camera.sceneResolutionScale = (float)result / 100.0f;
                    }
                }
                GUILayout.EndHorizontal();
            }

            overlay.highquality = GUILayout.Toggle(overlay.highquality, "High quality");

            if (overlay.highquality)
            {
                overlay.curved    = GUILayout.Toggle(overlay.curved, "Curved overlay");
                overlay.antialias = GUILayout.Toggle(overlay.antialias, "Overlay RGSS(2x2)");
            }
            else
            {
                overlay.curved    = false;
                overlay.antialias = false;
            }

            var tracker = SteamVR_Render.Top();

            if (tracker != null)
            {
                tracker.wireframe = GUILayout.Toggle(tracker.wireframe, "Wireframe");

                var render = SteamVR_Render.instance;
                if (render.trackingSpace == ETrackingUniverseOrigin.TrackingUniverseSeated)
                {
                    if (GUILayout.Button("Switch to Standing"))
                    {
                        render.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseStanding;
                    }
                    if (GUILayout.Button("Center View"))
                    {
                        var system = OpenVR.System;
                        if (system != null)
                        {
                            system.ResetSeatedZeroPose();
                        }
                    }
                }
                else
                {
                    if (GUILayout.Button("Switch to Seated"))
                    {
                        render.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseSeated;
                    }
                }
            }

#if !UNITY_EDITOR
            if (GUILayout.Button("Exit"))
            {
                Application.Quit();
            }
#endif
            GUILayout.Space(menuOffset);

            var env = System.Environment.GetEnvironmentVariable("VR_OVERRIDE");
            if (env != null)
            {
                GUILayout.Label("VR_OVERRIDE=" + env);
            }

            GUILayout.Label("Graphics device: " + SystemInfo.graphicsDeviceVersion);

            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.EndArea();

            if (cursor != null)
            {
                float x = Input.mousePosition.x, y = Screen.height - Input.mousePosition.y;
                float w = cursor.width, h = cursor.height;
                GUI.DrawTexture(new Rect(x, y, w, h), cursor);
            }

            RenderTexture.active = prevActive;

            if (bHideMenu)
            {
                HideMenu();
            }
        }