Ejemplo n.º 1
0
        XRInputSubsystem GetActiveSubsystemInstance()
        {
            XRInputSubsystem activeSubsystem = null;

#if USE_XR_MANAGEMENT
            // If the XR management package has been included, query the currently
            // active loader for the created subsystem, if one exists.
            if (XRGeneralSettings.Instance != null && XRGeneralSettings.Instance.Manager != null)
            {
                XRLoader loader = XRGeneralSettings.Instance.Manager.activeLoader;
                if (loader != null)
                {
                    activeSubsystem = loader.GetLoadedSubsystem <XRInputSubsystem>();
                }
            }
#endif
            // If XR management is not used or no loader has been set, check for
            // any active subsystem instances in the SubsystemManager.
            if (activeSubsystem == null)
            {
                List <XRInputSubsystem> subsystemInstances = new List <XRInputSubsystem>();
                SubsystemManager.GetInstances(subsystemInstances);
                if (subsystemInstances.Count > 0)
                {
                    activeSubsystem = subsystemInstances[0];
                }
            }

            return(activeSubsystem);
        }
Ejemplo n.º 2
0
    /// <summary>
    /// Recenters the head pose.
    /// </summary>
    public void RecenterPose()
    {
#if USING_XR_SDK
        XRInputSubsystem currentInputSubsystem = OVRManager.GetCurrentInputSubsystem();
        if (currentInputSubsystem != null)
        {
            currentInputSubsystem.TryRecenter();
        }
#elif !REQUIRES_XR_SDK
#pragma warning disable 618
        InputTracking.Recenter();
#pragma warning restore 618
#endif

        // The current poses are cached for the current frame and won't be updated immediately
        // after UnityEngine.VR.InputTracking.Recenter(). So we need to wait until next frame
        // to trigger the RecenteredPose delegate. The application could expect the correct pose
        // when the RecenteredPose delegate get called.
        recenterRequested           = true;
        recenterRequestedFrameCount = Time.frameCount;

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
        OVRMixedReality.RecenterPose();
#endif
    }
Ejemplo n.º 3
0
        void Update()
        {
            XRInputSubsystem subsystem = null;

            SubsystemManager.GetInstances(s_InputSubsystems);
            if (s_InputSubsystems.Count > 0)
            {
                subsystem = s_InputSubsystems[0];
            }

            m_SupportedTrackingOriginModes = subsystem?.GetSupportedTrackingOriginModes() ?? TrackingOriginModeFlags.Unknown;

            if (m_CurrentTrackingOriginMode != m_DesiredTrackingOriginMode & m_DesiredTrackingOriginMode != TrackingOriginModeFlags.Unknown)
            {
                subsystem?.TrySetTrackingOriginMode(m_DesiredTrackingOriginMode);
            }
            m_CurrentTrackingOriginMode = subsystem?.GetTrackingOriginMode() ?? TrackingOriginModeFlags.Unknown;

            if (m_CurrentTrackingOriginModeDisplay != null)
            {
                m_CurrentTrackingOriginModeDisplay.text = m_CurrentTrackingOriginMode.ToString();
            }

            if (m_RecenteredImage != null)
            {
                float lerp = (Time.time - m_LastRecenteredTime) / m_RecenteredColorResetTime;
                lerp = Mathf.Clamp(lerp, 0.0f, 1.0f);
                m_RecenteredImage.color = Color.Lerp(m_RecenteredColor, m_RecenteredOffColor, lerp);
            }
        }
Ejemplo n.º 4
0
        public static void GetCurrentSourceStates(this XRInputSubsystem input, List <System.Object> states)
        {
            if (states == null)
            {
                return;
            }

            states.Clear();
            int count = NativeApi.GetCountOfSourceStates();

            if (count > 0)
            {
                IntPtr[] ptrs = new IntPtr[count];
                NativeApi.GetAllSourceStates(ptrs, count);

                foreach (var ip in ptrs)
                {
                    if (ip != IntPtr.Zero)
                    {
                        states.Add(Marshal.GetObjectForIUnknown(ip));
                    }
                }

                NativeApi.ReleaseAllSourceStates(ptrs, states.Count);
            }
        }
        private static XRInputSubsystem GetXRInputSubsystem()
        {
            XRGeneralSettings xrSettings = XRGeneralSettings.Instance;

            if (xrSettings == null)
            {
                Debug.LogWarning($"GetXRInputSubsystem: XRGeneralSettings is null.");
                return(null);
            }

            XRManagerSettings xrManager = xrSettings.Manager;

            if (xrManager == null)
            {
                Debug.LogWarning($"GetXRInputSubsystem: XRManagerSettings is null.");
                return(null);
            }

            XRLoader xrLoader = xrManager.activeLoader;

            if (xrLoader == null)
            {
                Debug.LogWarning($"GetXRInputSubsystem: XRLoader is null.");
                return(null);
            }

            XRInputSubsystem xrInputSubsystem = xrLoader.GetLoadedSubsystem <XRInputSubsystem>();

            if (xrInputSubsystem == null)
            {
                Debug.LogWarning($"GetXRInputSubsystem: XRInputSubsystem is null.");
                return(null);
            }
            return(xrInputSubsystem);
        }
Ejemplo n.º 6
0
        void OnDestroy()
        {
            if (m_CleanupSubsystemOnDestroy && subsystem != null)
            {
                subsystem.Destroy();
            }

            subsystem = null;
        }
Ejemplo n.º 7
0
        void OnEnable()
        {
            subsystem = GetActiveSubsystemInstance();

            if (subsystem != null)
            {
                subsystem.Start();
            }
        }
        public IEnumerator TestInputSubsystem()
        {
            yield return(new WaitForSeconds(1));

            Assert.IsNotNull(ActiveLoader);
            XRInputSubsystem inputSub = ActiveLoader.GetLoadedSubsystem <XRInputSubsystem>();

            Assert.IsNotNull(inputSub);
        }
Ejemplo n.º 9
0
        void OnDestroy()
        {
            if (subsystem != null)
            {
                subsystem.Destroy();
            }

            subsystem = null;
        }
Ejemplo n.º 10
0
        void OnEnable()
        {
            if (subsystem == null)
            {
                subsystem = CreateSubsystem();
            }

            if (subsystem != null)
            {
                subsystem.Start();
            }
        }
 void OnDeviceConnected(InputDevice device)
 {
     if (m_XrInputSubsystem == null)
     {
         m_XrInputSubsystem = device.subsystem;
         if (m_XrInputSubsystem != null)
         {
             m_XrInputSubsystem.boundaryChanged += BoundaryChanged;
             BoundaryChanged(m_XrInputSubsystem);
         }
     }
 }
Ejemplo n.º 12
0
        bool SetupCamera(XRInputSubsystem subsystem)
        {
            if (subsystem == null)
            {
                return(false);
            }

            bool trackingSettingsSet = false;

            float desiredOffset = cameraYOffset;

            if (m_TrackingSpace == TrackingSpaceType.RoomScale)
            {
                // We need to check for Unknown because we may not be in a state where we can read this data yet.
                if ((subsystem.GetSupportedTrackingOriginModes() & (TrackingOriginModeFlags.Floor | TrackingOriginModeFlags.Unknown)) == 0)
                {
                    Debug.LogWarning("XRRig.SetupCamera: Attempting to set the tracking space to Room, but that is not supported by the SDK.");
                    return(true);
                }

                if (subsystem.TrySetTrackingOriginMode(TrackingOriginModeFlags.Floor))
                {
                    desiredOffset       = 0;
                    trackingSettingsSet = true;
                }
            }

            if (m_TrackingSpace == TrackingSpaceType.Stationary)
            {
                // We need to check for Unknown because we may not be in a state where we can read this data yet.
                if ((subsystem.GetSupportedTrackingOriginModes() & (TrackingOriginModeFlags.Device | TrackingOriginModeFlags.Unknown)) == 0)
                {
                    Debug.LogWarning("XRRig.SetupCamera: Attempting to set the tracking space to Stationary, but that is not supported by the SDK.");
                    return(true);
                }

                if (subsystem.TrySetTrackingOriginMode(TrackingOriginModeFlags.Device))
                {
                    trackingSettingsSet = subsystem.TryRecenter();
                }
            }

            if (trackingSettingsSet)
            {
                // Move camera to correct height
                if (m_CameraFloorOffsetObject)
                {
                    m_CameraFloorOffsetObject.transform.localPosition = new Vector3(m_CameraFloorOffsetObject.transform.localPosition.x, desiredOffset, m_CameraFloorOffsetObject.transform.localPosition.z);
                }
            }
            return(trackingSettingsSet);
        }
        public IEnumerator TestFloorTrackingOriginMode()
        {
            yield return(new WaitForSeconds(1));

            XRInputSubsystem inputSub = ActiveLoader.GetLoadedSubsystem <XRInputSubsystem>();

            if (inputSub.GetTrackingOriginMode() == TrackingOriginModeFlags.Floor)
            {
                List <Vector3> boundaryPoints = new List <Vector3>();
                inputSub.TryGetBoundaryPoints(boundaryPoints);
                Assert.IsFalse(boundaryPoints.Count == 0);
            }
        }
Ejemplo n.º 14
0
        void SetToNextMode(XRInputSubsystem subsystem)
        {
            TrackingOriginModeFlags currentOriginMode = subsystem.GetTrackingOriginMode();

            for (int i = 0; i < s_SupportedTrackingOriginModes.Count; i++)
            {
                if (currentOriginMode == s_SupportedTrackingOriginModes[i])
                {
                    int nextModeIndex = (i + 1) % s_SupportedTrackingOriginModes.Count;
                    subsystem.TrySetTrackingOriginMode(s_SupportedTrackingOriginModes[nextModeIndex]);
                    break;
                }
            }
        }
Ejemplo n.º 15
0
        void UpdateSupportedTrackingOriginModes(XRInputSubsystem subsystem)
        {
            TrackingOriginModeFlags supportedOriginModes = subsystem.GetSupportedTrackingOriginModes();

            s_SupportedTrackingOriginModes.Clear();
            for (int i = 0; i < 31; i++)
            {
                uint modeToCheck = 1u << i;
                if ((modeToCheck & ((UInt32)supportedOriginModes)) != 0)
                {
                    s_SupportedTrackingOriginModes.Add((TrackingOriginModeFlags)modeToCheck);
                }
            }
        }
    void AddElement(XRInputSubsystem inputSubsystem, int systemNumber)
    {
        if (Mathf.Abs(m_NextPosition.y) > m_ParentRect.rect.height)
        {
            m_ParentRect.sizeDelta = new Vector2(m_ParentRect.sizeDelta.x, m_ParentRect.sizeDelta.y + m_RowSeparation);
        }
        GameObject NewSubsystemUI = Instantiate(uiPrefab, m_ParentRect);

        m_Elements.Add(NewSubsystemUI);
        NewSubsystemUI.transform.localPosition = m_NextPosition;
        NewSubsystemUI.GetComponent <RectTransform>().anchorMin = new Vector2(0.5f, 1f);
        NewSubsystemUI.GetComponent <RectTransform>().anchorMax = new Vector2(0.5f, 1f);
        m_NextPosition = new Vector3(0, m_NextPosition.y - m_RowSeparation, 0);

        NewSubsystemUI.GetComponent <InputSubsystemPanel>().Setup(xrRig, systemNumber, inputSubsystem);
    }
Ejemplo n.º 17
0
 void Update()
 {
     m_TimeRemainingTillChange -= Time.deltaTime;
     if (m_TimeRemainingTillChange <= 0.0f)
     {
         List <XRInputSubsystem> inputSubsystems = new List <XRInputSubsystem>();
         SubsystemManager.GetInstances(inputSubsystems);
         XRInputSubsystem subsystem = inputSubsystems?[0];
         if (subsystem != null)
         {
             UpdateSupportedTrackingOriginModes(subsystem);
             SetToNextMode(subsystem);
         }
         m_TimeRemainingTillChange += m_ChangeInterval;
     }
 }
 public bool DrawPlayArea(XRInputSubsystem subsystem)
 {
     if (subsystem != null)
     {
         if (subsystem.TryGetBoundaryPoints(boundaryPoints))
         {
             Debug.Log($"Got {boundaryPoints.Count} boundary points");
             if (boundaryPoints.Count > 0)
             {
                 ReadBoundPoints();
                 return(true);
             }
         }
     }
     return(false);
 }
        public IEnumerator TestBoundaryIsAtGround()
        {
            yield return(new WaitForSeconds(1));

            XRInputSubsystem inputSub = ActiveLoader.GetLoadedSubsystem <XRInputSubsystem>();

            if (inputSub.GetTrackingOriginMode() == TrackingOriginModeFlags.Floor)
            {
                List <Vector3> boundaryPoints = new List <Vector3>();
                inputSub.TryGetBoundaryPoints(boundaryPoints);
                for (int i = 0; i < boundaryPoints.Count; i++)
                {
                    Assert.That(boundaryPoints[i].y, Is.EqualTo(0.0f).Within(1).Ulps);
                }
            }
        }
        private void ConnectCardboardInputSystem()
        {
            List <XRInputSubsystemDescriptor> inputs = new List <XRInputSubsystemDescriptor>();

            SubsystemManager.GetSubsystemDescriptors(inputs);

            foreach (var d in inputs)
            {
                if (d.id.Equals(inputMatch))
                {
                    XRInputSubsystem inputInst = d.Create();

                    if (inputInst != null)
                    {
                        GCHandle handle = GCHandle.Alloc(inputInst);
                        inputPointer = GCHandle.ToIntPtr(handle);
                    }
                }
            }
        }
Ejemplo n.º 21
0
        XRInputSubsystem GetActiveSubsystemInstance()
        {
            XRInputSubsystem activeSubsystem = null;

            // Query the currently active loader for the created subsystem, if one exists.
            if (XRGeneralSettings.Instance != null && XRGeneralSettings.Instance.Manager != null)
            {
                XRLoader loader = XRGeneralSettings.Instance.Manager.activeLoader;
                if (loader != null)
                {
                    activeSubsystem = loader.GetLoadedSubsystem <XRInputSubsystem>();
                }
            }

            if (activeSubsystem == null)
            {
                Debug.LogWarning($"No active {typeof(XRInputSubsystem).FullName} is available. Please ensure that a " +
                                 "valid loader configuration exists in the XR project settings.");
            }

            return(activeSubsystem);
        }
Ejemplo n.º 22
0
        void CreateSubsystemIfNecessary()
        {
            // Use the subsystem that has been instantiated by XR Management
            // if available, otherwise create the subsystem.

            if (subsystem == null)
            {
                subsystem = GetActiveSubsystemInstance();

                // If the subsystem has already been created by XR management, it controls the lifetime
                // of the subsystem.
                if (subsystem != null)
                {
                    m_CleanupSubsystemOnDestroy = false;
                }
            }

            if (subsystem == null)
            {
                subsystem = CreateSubsystem();
            }
        }
Ejemplo n.º 23
0
    public void VerifyXRDevice_userPresence_isPresent()
    {
        XRGeneralSettings xrGeneralSettings = XRGeneralSettings.Instance;
        XRInputSubsystem  inputs            = xrGeneralSettings.Manager.activeLoader.GetLoadedSubsystem <XRInputSubsystem>();
        var expUserPresenceState            = UserPresenceState.Present;

        var mockHmd = "MockHMD";

        if (Settings.EnabledXrTarget == mockHmd || Application.isEditor)
        {
            var reasonString = Settings.EnabledXrTarget == mockHmd ? $"EnabledXrTarget == {mockHmd}" : "Test is running in the Editor";

            Assert.Ignore("{0}: UserPresenceState.Present will always be false. Ignoring", reasonString);
        }
        else
        {
            List <InputDevice> devices = new List <InputDevice>();
            var userPresenceState      = UserPresenceState.Unknown;
            inputs.TryGetInputDevices(devices);
            foreach (var device in devices)
            {
                if ((device.characteristics & InputDeviceCharacteristics.HeadMounted) == InputDeviceCharacteristics.HeadMounted)
                {
                    var userPresence = new InputFeatureUsage <bool>("UserPresence");
                    if (device.TryGetFeatureValue(userPresence, out bool value) == true)
                    {
                        userPresenceState = value == true ? UserPresenceState.Present : UserPresenceState.NotPresent;
                    }
                    else if (userPresenceState != UserPresenceState.Present)
                    {
                        userPresenceState = UserPresenceState.Unsupported;
                    }
                }
            }
            Assert.AreEqual(userPresenceState, expUserPresenceState, string.Format("Not mobile platform. Expected userPresenceState to be {0}, but is {1}.", expUserPresenceState, userPresenceState));
        }
    }
        void BoundaryChanged(XRInputSubsystem inputSubsystem)
        {
            // m_XrInputSubsystem = inputSubsystem;
            // var allPoints = new List<Vector3>();

            // TODO Figure out why this boundary is not correct
            // if (m_XrInputSubsystem.TryGetBoundaryPoints(allPoints))
            // {
            //     m_DefaultBoundary.SetActive(false);
            //     m_BoundaryLineRenderer.enabled = true;
            //     var prevPoint = allPoints[0];
            //     var delta = Vector3.forward;
            //     var prevDelta = Vector3.zero;
            //     m_BoundaryPoints.Clear();
            //     for (var i = 1; i < allPoints.Count; i++)
            //     {
            //         var point = allPoints[i];
            //         delta = prevPoint - point;
            //         if (Vector3.Magnitude(delta) > 0.05f || Vector3.Dot(delta.normalized, prevDelta.normalized) < 0.5f)
            //         {
            //             prevPoint = point;
            //             prevDelta = delta;
            //             m_BoundaryPoints.Add(point);
            //         }
            //
            //     }
            //
            //     m_BoundaryLineRenderer.SetVertexCount(m_BoundaryPoints.Count);
            //     m_BoundaryLineRenderer.SetPositions(m_BoundaryPoints.ToArray());
            // }
            // else
            {
                m_DefaultBoundary.SetActive(true);
                m_BoundaryLineRenderer.enabled = false;
            }
        }
Ejemplo n.º 25
0
 public static NativeTypes.SpatialLocatability GetSpatialLocatability(this XRInputSubsystem input)
 {
     return(Native.GetSpatialLocatability());
 }
Ejemplo n.º 26
0
 void OnDestroy()
 {
     subsystem = null;
 }
Ejemplo n.º 27
0
        bool SetupCamera(XRInputSubsystem subsystem)
        {
            if (subsystem == null)
            {
                return(false);
            }

            bool trackingSettingsSet = false;

            float desiredOffset = cameraYOffset;

            var currentMode    = subsystem.GetTrackingOriginMode();
            var supportedModes = subsystem.GetSupportedTrackingOriginModes();
            TrackingOriginModeFlags requestedMode = TrackingOriginModeFlags.Unknown;

            // map between the user requested options, and the actual options.
            if (m_RequestedTrackingMode == UserRequestedTrackingMode.Default)
            {
                requestedMode = currentMode;
            }
            else if (m_RequestedTrackingMode == UserRequestedTrackingMode.Device)
            {
                requestedMode = TrackingOriginModeFlags.Device;
            }
            else if (m_RequestedTrackingMode == UserRequestedTrackingMode.Floor)
            {
                requestedMode = TrackingOriginModeFlags.Floor;
            }
            else
            {
                Debug.LogWarning("Unknown Requested Tracking Mode");
            }

            // now we've mapped em. actually go set em.
            if (requestedMode == TrackingOriginModeFlags.Floor)
            {
                // We need to check for Unknown because we may not be in a state where we can read this data yet.
                if ((supportedModes & (TrackingOriginModeFlags.Floor | TrackingOriginModeFlags.Unknown)) == 0)
                {
                    Debug.LogWarning("CameraOffset.SetupCamera: Attempting to set the tracking space to Floor, but that is not supported by the SDK.");
                    m_TrackingOriginMode = subsystem.GetTrackingOriginMode();
                    return(true);
                }

                if (subsystem.TrySetTrackingOriginMode(requestedMode))
                {
                    desiredOffset       = 0.0f;
                    trackingSettingsSet = true;
                }
            }
            else if (requestedMode == TrackingOriginModeFlags.Device)
            {
                // We need to check for Unknown because we may not be in a state where we can read this data yet.
                if ((supportedModes & (TrackingOriginModeFlags.Device | TrackingOriginModeFlags.Unknown)) == 0)
                {
                    Debug.LogWarning("CameraOffset.SetupCamera: Attempting to set the tracking space to Device, but that is not supported by the SDK.");
                    m_TrackingOriginMode = subsystem.GetTrackingOriginMode();
                    return(true);
                }

                if (subsystem.TrySetTrackingOriginMode(requestedMode))
                {
                    trackingSettingsSet = subsystem.TryRecenter();
                }
            }

            // what did we actually set?
            m_TrackingOriginMode = subsystem.GetTrackingOriginMode();

            if (trackingSettingsSet)
            {
                // Move camera to correct height
                if (m_CameraFloorOffsetObject)
                {
                    m_CameraFloorOffsetObject.transform.localPosition = new Vector3(m_CameraFloorOffsetObject.transform.localPosition.x, desiredOffset, m_CameraFloorOffsetObject.transform.localPosition.z);
                }
            }
            return(trackingSettingsSet);
        }
Ejemplo n.º 28
0
 private void OnTrackingOriginUpdated(XRInputSubsystem subsystem) => UpdateTrackingOrigin(subsystem.GetTrackingOriginMode());
Ejemplo n.º 29
0
        bool SetupCamera(XRInputSubsystem subsystem)
        {
            if (subsystem == null)
            {
                return(false);
            }

            bool trackingSettingsSet = false;

            var currentMode    = subsystem.GetTrackingOriginMode();
            var supportedModes = subsystem.GetSupportedTrackingOriginModes();
            TrackingOriginModeFlags requestedMode = TrackingOriginModeFlags.Unknown;

            // map between the user requested options, and the actual options.
            if (m_RequestedTrackingMode == UserRequestedTrackingMode.Default)
            {
                requestedMode = currentMode;
            }
            else if (m_RequestedTrackingMode == UserRequestedTrackingMode.Device)
            {
                requestedMode = TrackingOriginModeFlags.Device;
            }
            else if (m_RequestedTrackingMode == UserRequestedTrackingMode.Floor)
            {
                requestedMode = TrackingOriginModeFlags.Floor;
            }
            else
            {
                Debug.LogWarning("Unknown Requested Tracking Mode");
            }

            // now we've mapped em. actually go set em.
            if (requestedMode == TrackingOriginModeFlags.Floor)
            {
                // We need to check for Unknown because we may not be in a state where we can read this data yet.
                if ((supportedModes & (TrackingOriginModeFlags.Floor | TrackingOriginModeFlags.Unknown)) == 0)
                {
                    Debug.LogWarning("CameraOffset.SetupCamera: Attempting to set the tracking space to Floor, but that is not supported by the SDK.");
                }
                else
                {
                    trackingSettingsSet = subsystem.TrySetTrackingOriginMode(requestedMode);
                }
            }
            else if (requestedMode == TrackingOriginModeFlags.Device)
            {
                // We need to check for Unknown because we may not be in a state where we can read this data yet.
                if ((supportedModes & (TrackingOriginModeFlags.Device | TrackingOriginModeFlags.Unknown)) == 0)
                {
                    Debug.LogWarning("CameraOffset.SetupCamera: Attempting to set the tracking space to Device, but that is not supported by the SDK.");
                }
                else
                {
                    trackingSettingsSet = subsystem.TrySetTrackingOriginMode(requestedMode) && subsystem.TryRecenter();
                }
            }

            if (trackingSettingsSet)
            {
                UpdateTrackingOrigin(subsystem.GetTrackingOriginMode());
            }

            return(trackingSettingsSet);
        }
Ejemplo n.º 30
0
 private void TrackingOriginUpdated(XRInputSubsystem obj)
 {
     m_LastRecenteredTime = Time.time;
 }