Esempio n. 1
0
    protected override void OnFixedUpdate()
    {
#if (UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0 || UNITY_4_5)
        // We want to call this as soon after Present as possible.
        SteamVR_Utils.QueueEventOnRenderThread(SteamVR.Unity.k_nRenderEventID_PostPresentHandoff);
#endif
    }
Esempio n. 2
0
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (SteamVR_Render.Top() == this)
        {
            int eventID;
            if (SteamVR_Render.eye == EVREye.Eye_Left)
            {
                // Get gpu started on work early to avoid bubbles at the top of the frame.
                SteamVR_Utils.QueueEventOnRenderThread(Unity.k_nRenderEventID_Flush);

                eventID = Unity.k_nRenderEventID_SubmitL;
            }
            else
            {
                eventID = Unity.k_nRenderEventID_SubmitR;
            }

            // Queue up a call on the render thread to Submit our render target to the compositor.
            SteamVR_Utils.QueueEventOnRenderThread(eventID);
        }

        Graphics.SetRenderTarget(dest);
        SteamVR_Camera.blitMaterial.mainTexture = src;

        GL.PushMatrix();
        GL.LoadOrtho();
        SteamVR_Camera.blitMaterial.SetPass(0);
        GL.Begin(GL.QUADS);
        GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(-1, 1, 0);
        GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1, 1, 0);
        GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1, -1, 0);
        GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(-1, -1, 0);
        GL.End();
        GL.PopMatrix();
    }
    void FixedUpdate()
    {
#if (UNITY_5_3_OR_NEWER || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
        // We want to call this as soon after Present as possible.
        SteamVR_Utils.QueueEventOnRenderThread(SteamVR.Unity.k_nRenderEventID_PostPresentHandoff);
#endif
    }
Esempio n. 4
0
 private void OnRenderImage(RenderTexture src, RenderTexture dest)
 {
     if (SteamVR_Render.Top() == this)
     {
         int eventID;
         if (SteamVR_Render.eye == EVREye.Eye_Left)
         {
             SteamVR_Utils.QueueEventOnRenderThread(201510023);
             eventID = 201510021;
         }
         else
         {
             eventID = 201510022;
         }
         SteamVR_Utils.QueueEventOnRenderThread(eventID);
     }
     Graphics.SetRenderTarget(dest);
     SteamVR_Camera.blitMaterial.mainTexture = src;
     GL.PushMatrix();
     GL.LoadOrtho();
     SteamVR_Camera.blitMaterial.SetPass(0);
     GL.Begin(7);
     GL.TexCoord2(0f, 0f);
     GL.Vertex3(-1f, 1f, 0f);
     GL.TexCoord2(1f, 0f);
     GL.Vertex3(1f, 1f, 0f);
     GL.TexCoord2(1f, 1f);
     GL.Vertex3(1f, -1f, 0f);
     GL.TexCoord2(0f, 1f);
     GL.Vertex3(-1f, -1f, 0f);
     GL.End();
     GL.PopMatrix();
     Graphics.SetRenderTarget(null);
 }
Esempio n. 5
0
    void Update()
    {
        if (cameras.Length == 0)
        {
            enabled = false;
            return;
        }

        // If our FixedUpdate rate doesn't match our render framerate, then catch the handoff here.
        SteamVR_Utils.QueueEventOnRenderThread(Unity.k_nRenderEventID_PostPresentHandoff);

        // Force controller update in case no one else called this frame to ensure prevState gets updated.
        SteamVR_Controller.Update();

        // Dispatch any OpenVR events.
        var vr      = SteamVR.instance;
        var vrEvent = new VREvent_t();

        for (int i = 0; i < 64; i++)
        {
            if (!vr.hmd.PollNextEvent(ref vrEvent))
            {
                break;
            }

            switch ((EVREventType)vrEvent.eventType)
            {
            case EVREventType.VREvent_InputFocusCaptured:                     // another app has taken focus (likely dashboard)
                SteamVR_Utils.Event.Send("input_focus", false);
                break;

            case EVREventType.VREvent_InputFocusReleased:                     // that app has released input focus
                SteamVR_Utils.Event.Send("input_focus", true);
                break;

            default:
                var name = System.Enum.GetName(typeof(EVREventType), vrEvent.eventType);
                if (name != null)
                {
                    SteamVR_Utils.Event.Send(name.Substring(8) /*strip VREvent_*/, vrEvent);
                }
                break;
            }
        }

        // Ensure various settings to minimize latency.
        Application.targetFrameRate     = -1;
        Application.runInBackground     = true; // don't require companion window focus
        QualitySettings.maxQueuedFrames = -1;
        QualitySettings.vSyncCount      = 0;    // this applies to the companion window

        if (lockPhysicsUpdateRateToRenderFrequency)
        {
            Time.fixedDeltaTime = 1.0f / vr.hmd_DisplayFrequency;
        }
    }
Esempio n. 6
0
    private IEnumerator RenderLoop()
    {
        while (true)
        {
            yield return(new WaitForEndOfFrame());

            if (pauseRendering)
            {
                continue;
            }

            var vr = SteamVR.instance;

            if (!vr.compositor.CanRenderScene())
            {
                continue;
            }

            vr.compositor.SetTrackingSpace(trackingSpace);
            SteamVR_Utils.QueueEventOnRenderThread(Unity.k_nRenderEventID_WaitGetPoses);

            // Hack to flush render event that was queued in Update (this ensures WaitGetPoses has returned before we grab the new values).
            Unity.EventWriteString("[UnityMain] GetNativeTexturePtr - Begin");
            SteamVR_Camera.GetSceneTexture(cameras[0].GetComponent <Camera>().hdr).GetNativeTexturePtr();
            Unity.EventWriteString("[UnityMain] GetNativeTexturePtr - End");

            vr.compositor.GetLastPoses(poses, gamePoses);
            SteamVR_Utils.Event.Send("new_poses", poses);

            var overlay = SteamVR_Overlay.instance;
            if (overlay != null)
            {
                overlay.UpdateOverlay(vr);
            }

            RenderEye(vr, EVREye.Eye_Left);
            RenderEye(vr, EVREye.Eye_Right);

            // Move cameras back to head position so they can be tracked reliably
            foreach (var c in cameras)
            {
                c.transform.localPosition = Vector3.zero;
                c.transform.localRotation = Quaternion.identity;
            }

            if (cameraMask != null)
            {
                cameraMask.Clear();
            }
        }
    }
Esempio n. 7
0
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        try
        {
            if (SteamVR_Render.Top() == this)
            {
                int eventID;
                if (SteamVR_Render.eye == EVREye.Eye_Left)
                {
                    // Get gpu started on work early to avoid bubbles at the top of the frame.
                    SteamVR_Utils.QueueEventOnRenderThread(SteamVR.Unity.k_nRenderEventID_Flush);

                    eventID = SteamVR.Unity.k_nRenderEventID_SubmitL;
                }
                else
                {
                    eventID = SteamVR.Unity.k_nRenderEventID_SubmitR;
                }
                // Queue up a call on the render thread to Submit our render target to the compositor.
                SteamVR_Utils.QueueEventOnRenderThread(eventID);
                //Texture_t t = new Texture_t() { eColorSpace = EColorSpace.Auto, eType = EGraphicsAPIConvention.API_DirectX, handle = _sceneTexture.GetNativeTexturePtr() };
                //VRTextureBounds_t b = SteamVR_Render.eye == EVREye.Eye_Left ? SteamVR.instance.textureBounds[0] : SteamVR.instance.textureBounds[1];
                //var res = OpenVR.Compositor.Submit(SteamVR_Render.eye, ref t, ref b, EVRSubmitFlags.Submit_Default);
                //Console.WriteLine(res);
            }
            Graphics.SetRenderTarget(dest);

            SteamVR_Camera.blitMaterial.mainTexture = src;

            GL.PushMatrix();
            GL.LoadOrtho();
            SteamVR_Camera.blitMaterial.SetPass(0);
            GL.Begin(GL.QUADS);
            GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(-1, 1, 0);
            GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1, 1, 0);
            GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1, -1, 0);
            GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(-1, -1, 0);
            GL.End();
            GL.PopMatrix();

            Graphics.SetRenderTarget(null);
        } catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
Esempio n. 8
0
    protected override void OnUpdate()
    {
#if !(UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0 || UNITY_4_5)
        if (poseUpdater == null)
        {
            var go = new GameObject("poseUpdater");
            go.transform.parent = transform;
            poseUpdater         = go.AddComponent <SteamVR_UpdatePoses>();
        }
#else
        if (cameras.Length == 0)
        {
            enabled = false;
            return;
        }

        // If our FixedUpdate rate doesn't match our render framerate, then catch the handoff here.
        SteamVR_Utils.QueueEventOnRenderThread(SteamVR.Unity.k_nRenderEventID_PostPresentHandoff);
#endif
        // Force controller update in case no one else called this frame to ensure prevState gets updated.
        SteamVR_Controller.Update();

        // Dispatch any OpenVR events.
        var system = OpenVR.System;
        if (system != null)
        {
            var vrEvent = new VREvent_t();
            var size    = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VREvent_t));
            for (int i = 0; i < 64; i++)
            {
                if (!system.PollNextEvent(ref vrEvent, size))
                {
                    break;
                }

                switch ((EVREventType)vrEvent.eventType)
                {
                case EVREventType.VREvent_InputFocusCaptured:                         // another app has taken focus (likely dashboard)
                    SteamVR_Utils.Event.Send("input_focus", false);
                    break;

                case EVREventType.VREvent_InputFocusReleased:                         // that app has released input focus
                    SteamVR_Utils.Event.Send("input_focus", true);
                    break;

                case EVREventType.VREvent_ShowRenderModels:
                    SteamVR_Utils.Event.Send("hide_render_models", false);
                    break;

                case EVREventType.VREvent_HideRenderModels:
                    SteamVR_Utils.Event.Send("hide_render_models", true);
                    break;

                default:
                    var name = System.Enum.GetName(typeof(EVREventType), vrEvent.eventType);
                    if (name != null)
                    {
                        SteamVR_Utils.Event.Send(name.Substring(8) /*strip VREvent_*/, vrEvent);
                    }
                    break;
                }
            }
        }

        // Ensure various settings to minimize latency.
        Application.targetFrameRate     = -1;
        Application.runInBackground     = true; // don't require companion window focus
        QualitySettings.maxQueuedFrames = -1;
        QualitySettings.vSyncCount      = 0;    // this applies to the companion window

        if (lockPhysicsUpdateRateToRenderFrequency)
        {
            var vr = SteamVR.instance;
            if (vr != null)
            {
                var timing = new Compositor_FrameTiming();
                timing.m_nSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Compositor_FrameTiming));
                vr.compositor.GetFrameTiming(ref timing, 0);

                Time.fixedDeltaTime = Time.timeScale * timing.m_nNumFramePresents / SteamVR.instance.hmd_DisplayFrequency;
            }
        }
    }
    private IEnumerator RenderLoop()
    {
        while (Application.isPlaying)
        {
            yield return(waitForEndOfFrame);

            if (pauseRendering)
            {
                continue;
            }

            var compositor = OpenVR.Compositor;
            if (compositor != null)
            {
                if (!compositor.CanRenderScene())
                {
                    continue;
                }

                compositor.SetTrackingSpace(trackingSpace);

#if (UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
                SteamVR_Utils.QueueEventOnRenderThread(SteamVR.Unity.k_nRenderEventID_WaitGetPoses);

                // Hack to flush render event that was queued in Update (this ensures WaitGetPoses has returned before we grab the new values).
                SteamVR.Unity.EventWriteString("[UnityMain] GetNativeTexturePtr - Begin");
                SteamVR_Camera.GetSceneTexture(cameras[0].GetComponent <Camera>().hdr).GetNativeTexturePtr();
                SteamVR.Unity.EventWriteString("[UnityMain] GetNativeTexturePtr - End");

                compositor.GetLastPoses(poses, gamePoses);
                SteamVR_Events.NewPoses.Send(poses);
                SteamVR_Events.NewPosesApplied.Send();
#endif
            }

            var overlay = SteamVR_Overlay.instance;
            if (overlay != null)
            {
                overlay.UpdateOverlay();
            }

            RenderExternalCamera();

#if (UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
            var vr = SteamVR.instance;
            RenderEye(vr, EVREye.Eye_Left);
            RenderEye(vr, EVREye.Eye_Right);

            // Move cameras back to head position so they can be tracked reliably
            foreach (var c in cameras)
            {
                c.transform.localPosition = Vector3.zero;
                c.transform.localRotation = Quaternion.identity;
            }

            if (cameraMask != null)
            {
                cameraMask.Clear();
            }
#endif
        }
    }
Esempio n. 10
0
 void FixedUpdate()
 {
     // We want to call this as soon after Present as possible.
     SteamVR_Utils.QueueEventOnRenderThread(Unity.k_nRenderEventID_PostPresentHandoff);
 }
Esempio n. 11
0
    void Update()
    {
#if (UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
        if (cameras.Length == 0)
        {
            enabled = false;
            return;
        }

        // If our FixedUpdate rate doesn't match our render framerate, then catch the handoff here.
        SteamVR_Utils.QueueEventOnRenderThread(SteamVR.Unity.k_nRenderEventID_PostPresentHandoff);
#endif
        // Force controller update in case no one else called this frame to ensure prevState gets updated.
        SteamVR_Controller.Update();

        // Dispatch any OpenVR events.
        var system = OpenVR.System;
        if (system != null)
        {
            var vrEvent = new VREvent_t();
            var size    = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VREvent_t));
            for (int i = 0; i < 64; i++)
            {
                if (!system.PollNextEvent(ref vrEvent, size))
                {
                    break;
                }

                switch ((EVREventType)vrEvent.eventType)
                {
                case EVREventType.VREvent_InputFocusCaptured:                         // another app has taken focus (likely dashboard)
                    if (vrEvent.data.process.oldPid == 0)
                    {
                        SteamVR_Events.InputFocus.Send(false);
                    }
                    break;

                case EVREventType.VREvent_InputFocusReleased:                         // that app has released input focus
                    if (vrEvent.data.process.pid == 0)
                    {
                        SteamVR_Events.InputFocus.Send(true);
                    }
                    break;

                case EVREventType.VREvent_ShowRenderModels:
                    SteamVR_Events.HideRenderModels.Send(false);
                    break;

                case EVREventType.VREvent_HideRenderModels:
                    SteamVR_Events.HideRenderModels.Send(true);
                    break;

                default:
                    SteamVR_Events.System((EVREventType)vrEvent.eventType).Send(vrEvent);
                    break;
                }
            }
        }

        // Ensure various settings to minimize latency.
        Application.targetFrameRate     = -1;
        Application.runInBackground     = true; // don't require companion window focus
        QualitySettings.maxQueuedFrames = -1;
        QualitySettings.vSyncCount      = 0;    // this applies to the companion window

        if (lockPhysicsUpdateRateToRenderFrequency && Time.timeScale > 0.0f)
        {
            var vr = SteamVR.instance;
            if (vr != null)
            {
                var timing = new Compositor_FrameTiming();
                timing.m_nSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Compositor_FrameTiming));
                vr.compositor.GetFrameTiming(ref timing, 0);

                Time.fixedDeltaTime = Time.timeScale / vr.hmd_DisplayFrequency;
            }
        }
    }