Esempio n. 1
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;
            }
        }
    }
Esempio n. 2
0
    private void Update()
    {
#if !(UNITY_5_6)
        if (poseUpdater == null)
        {
            var go = new GameObject("poseUpdater");
            go.transform.parent = transform;
            poseUpdater         = go.AddComponent <SteamVR_UpdatePoses>();
        }
#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)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)Marshal.SizeOf(typeof(Compositor_FrameTiming));
                vr.compositor.GetFrameTiming(ref timing, 0);

                Time.fixedDeltaTime = Time.timeScale / vr.hmd_DisplayFrequency;
            }
        }
    }
    void Update()
    {
        if (poseUpdater == null)
        {
            var go = new GameObject("poseUpdater");
            go.transform.parent = transform;
            poseUpdater = go.AddComponent<SteamVR_UpdatePoses>();
        }

        // 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_Utils.Event.Send("input_focus", false);
                        }
                        break;
                    case EVREventType.VREvent_InputFocusReleased: // that app has released input focus
                        if (vrEvent.data.process.pid == 0)
                        {
                            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 && 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;
                Time.maximumDeltaTime = Time.fixedDeltaTime * timing.m_nNumFramePresents;
            }
        }
    }