示例#1
0
        private Input.MagicLeapHand GetOrAddHand(MLHandTracking.Hand mlHand, Handedness handedness)
        {
            if (trackedHands.ContainsKey(handedness))
            {
                return(trackedHands[handedness]);
            }

            // Add new hand
            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"Magic Leap {handedness} Hand", pointers, inputSourceType);

            var controller = new Input.MagicLeapHand(TrackingState.Tracked, handedness, inputSource);

            controller.Initalize(new ManagedHand(mlHand));

            for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
            {
                controller.InputSource.Pointers[i].Controller = controller;
            }

            inputSystem?.RaiseSourceDetected(controller.InputSource, controller);

            trackedHands.Add(handedness, controller);

            return(controller);
        }
        private SimpleWebXRController GetOrAddController(Handedness handedness)
        {
            if (trackedControllers.ContainsKey(handedness))
            {
                return(trackedControllers[handedness]);
            }

            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"WebXR {handedness} Controller", pointers, inputSourceType);

            if (!inactiveControllerCache.TryGetValue(handedness, out var controller))
            {
                controller = new SimpleWebXRController(TrackingState.Tracked, handedness, inputSource);
            }

            for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
            {
                controller.InputSource.Pointers[i].Controller = controller;
            }

            inputSystem?.RaiseSourceDetected(controller.InputSource, controller);

            trackedControllers.Add(handedness, controller);

            return(controller);
        }
        private OculusHand GetOrAddHand(Handedness handedness, OVRHand ovrHand)
        {
            if (trackedHands.ContainsKey(handedness))
            {
                return(trackedHands[handedness]);
            }

            // Add new hand
            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"Oculus Quest {handedness} Hand", pointers, inputSourceType);


            OculusHand handDevice = new OculusHand(TrackingState.Tracked, handedness, inputSource);

            handDevice.InitializeHand(ovrHand, SettingsProfile);

            for (int i = 0; i < handDevice.InputSource?.Pointers?.Length; i++)
            {
                handDevice.InputSource.Pointers[i].Controller = handDevice;
            }

            inputSystem?.RaiseSourceDetected(handDevice.InputSource, handDevice);

            trackedHands.Add(handedness, handDevice);

            return(handDevice);
        }
        private SimpleWebXRHand GetOrAddHand(Handedness handedness)
        {
            if (trackedHands.ContainsKey(handedness))
            {
                return(trackedHands[handedness]);
            }

            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"WebXR {handedness} Hand", pointers, inputSourceType);

            var handController = new SimpleWebXRHand(TrackingState.Tracked, handedness, inputSource);

            for (int i = 0; i < handController.InputSource?.Pointers?.Length; i++)
            {
                handController.InputSource.Pointers[i].Controller = handController;
            }

            inputSystem?.RaiseSourceDetected(handController.InputSource, handController);

            trackedHands.Add(handedness, handController);

            return(handController);
        }
示例#5
0
        private OculusQuestHand GetOrAddHand(Handedness handedness)
        {
            if (trackedHands.ContainsKey(handedness))
            {
                return(trackedHands[handedness]);
            }

            // Add new hand
            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"Oculus Quest {handedness} Hand", pointers, inputSourceType);

            var controller = new OculusQuestHand(TrackingState.Tracked, handedness, inputSource);

            controller.SetupConfiguration(typeof(OculusQuestHand));

            for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
            {
                controller.InputSource.Pointers[i].Controller = controller;
            }

            inputSystem?.RaiseSourceDetected(controller.InputSource, controller);

            trackedHands.Add(handedness, controller);

            return(controller);
        }
示例#6
0
        private OculusQuestController GetOrAddController(Handedness handedness)
        {
            if (trackedControllers.ContainsKey(handedness))
            {
                return(trackedControllers[handedness]);
            }

            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"Oculus Quest {handedness} Controller", pointers, inputSourceType);

            var controller = new OculusQuestController(TrackingState.Tracked, handedness, inputSource);

            // Code is obsolete later on, but older MRTK versions require it.
#pragma warning disable 618
            controller.SetupConfiguration(typeof(OculusQuestController));
#pragma warning restore 618

            for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
            {
                controller.InputSource.Pointers[i].Controller = controller;
            }

            inputSystem?.RaiseSourceDetected(controller.InputSource, controller);

            trackedControllers.Add(handedness, controller);

            return(controller);
        }
        private void InitializeKeywordRecognizer()
        {
            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            InputSource = inputSystem?.RequestNewGenericInputSource("Magic Leap Speech Input Source", sourceType: InputSourceType.Voice);

            InitializeMLSpeech();
        }
示例#8
0
        /// <summary>
        /// Gets or adds a controller using the InputDevice name provided.
        /// </summary>
        /// <param name="inputDevice">The InputDevice from XR SDK.</param>
        /// <returns>The controller reference.</returns>
        protected virtual GenericXRSDKController GetOrAddController(InputDevice inputDevice)
        {
            // If a device is already registered with the ID provided, just return it.
            if (ActiveControllers.ContainsKey(inputDevice.name))
            {
                var controller = ActiveControllers[inputDevice.name];
                Debug.Assert(controller != null);
                return(controller);
            }

            Handedness controllingHand;

            if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.Left))
            {
                controllingHand = Handedness.Left;
            }
            else if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.Right))
            {
                controllingHand = Handedness.Right;
            }
            else
            {
                controllingHand = Handedness.None;
            }

            var             currentControllerType = GetCurrentControllerType(inputDevice);
            Type            controllerType        = GetControllerType(currentControllerType);
            InputSourceType inputSourceType       = GetInputSourceType(currentControllerType);

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            IMixedRealityPointer[]   pointers    = RequestPointers(currentControllerType, controllingHand);
            IMixedRealityInputSource inputSource = inputSystem?.RequestNewGenericInputSource($"{currentControllerType} Controller {controllingHand}", pointers, inputSourceType);

            if (!(Activator.CreateInstance(controllerType, TrackingState.NotTracked, controllingHand, inputSource, null) is GenericXRSDKController detectedController))
            {
                Debug.LogError($"Failed to create {controllerType.Name} controller");
                return(null);
            }

            if (!detectedController.SetupConfiguration(controllerType))
            {
                // Controller failed to be set up correctly.
                Debug.LogError($"Failed to set up {controllerType.Name} controller");
                // Return null so we don't raise the source detected.
                return(null);
            }

            for (int i = 0; i < detectedController.InputSource?.Pointers?.Length; i++)
            {
                detectedController.InputSource.Pointers[i].Controller = detectedController;
            }

            ActiveControllers.Add(inputDevice.name, detectedController);
            return(detectedController);
        }
        /// <inheritdoc />
        public override void Enable()
        {
            if (!UInput.mousePresent)
            {
                Disable();
                return;
            }

#if UNITY_EDITOR
            if (UnityEditor.EditorWindow.focusedWindow != null)
            {
                UnityEditor.EditorWindow.focusedWindow.ShowNotification(new GUIContent("Press \"ESC\" to regain mouse control"));
            }
#endif

            Cursor.visible   = false;
            Cursor.lockState = CursorLockMode.Locked;

            IMixedRealityInputSource mouseInputSource = null;

            MixedRealityRaycaster.DebugEnabled = true;

            const Handedness handedness     = Handedness.Any;
            System.Type      controllerType = typeof(MouseController);

            // Make sure that the handedness declared in the controller attribute matches what we expect
            {
                var controllerAttribute = MixedRealityControllerAttribute.Find(controllerType);
                if (controllerAttribute != null)
                {
                    Handedness[] handednesses = controllerAttribute.SupportedHandedness;
                    Debug.Assert(handednesses.Length == 1 && handednesses[0] == Handedness.Any, "Unexpected mouse handedness declared in MixedRealityControllerAttribute");
                }
            }

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            if (inputSystem != null)
            {
                var pointers = RequestPointers(SupportedControllerType.Mouse, handedness);
                mouseInputSource = inputSystem.RequestNewGenericInputSource("Mouse Input", pointers);
            }

            Controller = new MouseController(TrackingState.NotApplicable, handedness, mouseInputSource);

            if (mouseInputSource != null)
            {
                for (int i = 0; i < mouseInputSource.Pointers.Length; i++)
                {
                    mouseInputSource.Pointers[i].Controller = Controller;
                }
            }

            Controller.SetupConfiguration(typeof(MouseController));
            inputSystem?.RaiseSourceDetected(Controller.InputSource, Controller);
        }
示例#10
0
        void MLControllerConnected(byte controllerId)
        {
#if PLATFORM_LUMIN
            MLInput.Controller mlController = MLInput.GetController(controllerId);
            if (mlController.Type == MLInput.Controller.ControlType.Control)
            {
                if (!ConnectedControllers.ContainsKey(controllerId))
                {
                    Handedness handedness = mlController.Hand == MLInput.Hand.Right ? Handedness.Right : Handedness.Left;
                    switch (CurrentControllerSettings)
                    {
                    case HandSettings.None:
                        return;

                    case HandSettings.Left:
                        if (handedness != Handedness.Left)
                        {
                            return;
                        }
                        break;

                    case HandSettings.Right:
                        if (handedness != Handedness.Right)
                        {
                            return;
                        }
                        break;

                    case HandSettings.Both:
                        break;
                    }

                    var pointers        = RequestPointers(SupportedControllerType.GenericUnity, handedness);
                    var inputSourceType = InputSourceType.Controller;

                    IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
                    var inputSource = inputSystem?.RequestNewGenericInputSource($"Magic Leap {handedness} Controller", pointers, inputSourceType);

                    MagicLeapMRTKController controller = new MagicLeapMRTKController(mlController, TrackingState.Tracked, handedness, inputSource);
                    for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
                    {
                        controller.InputSource.Pointers[i].Controller = controller;
                    }

                    inputSystem?.RaiseSourceDetected(controller.InputSource, controller);
                    ConnectedControllers[controllerId] = new MLControllerContainer()
                    {
                        controllerId   = controllerId,
                        mrtkController = controller
                    };
                    trackedControls.Add(controller);
                }
            }
#endif
        }
示例#11
0
        /// <inheritdoc />
        public override void Enable()
        {
            if (!UInput.mousePresent)
            {
                Disable();
                return;
            }

            if (Controller != null)
            {
                // device manager has already been set up
                return;
            }

            IMixedRealityInputSource mouseInputSource = null;

            MixedRealityRaycaster.DebugEnabled = true;

            const Handedness handedness = Handedness.Any;

            System.Type controllerType = typeof(MouseController);

            // Make sure that the handedness declared in the controller attribute matches what we expect
            {
                var controllerAttribute = MixedRealityControllerAttribute.Find(controllerType);
                if (controllerAttribute != null)
                {
                    Handedness[] handednesses = controllerAttribute.SupportedHandedness;
                    Debug.Assert(handednesses.Length == 1 && handednesses[0] == Handedness.Any, "Unexpected mouse handedness declared in MixedRealityControllerAttribute");
                }
            }

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            if (inputSystem != null)
            {
                var pointers = RequestPointers(SupportedControllerType.Mouse, handedness);
                mouseInputSource = inputSystem.RequestNewGenericInputSource("Mouse Input", pointers);
            }

            Controller = new MouseController(TrackingState.NotApplicable, handedness, mouseInputSource);

            if (mouseInputSource != null)
            {
                for (int i = 0; i < mouseInputSource.Pointers.Length; i++)
                {
                    mouseInputSource.Pointers[i].Controller = Controller;
                }
            }

            Controller.SetupConfiguration(typeof(MouseController));
            inputSystem?.RaiseSourceDetected(Controller.InputSource, Controller);
        }
        /// <summary>
        /// Gets or adds a controller using the joystick name provided.
        /// </summary>
        /// <param name="joystickName">The name of the joystick from Unity's <see href="https://docs.unity3d.com/ScriptReference/Input.GetJoystickNames.html">Input.GetJoystickNames</see></param>
        /// <returns>A new controller reference.</returns>
        protected virtual GenericJoystickController GetOrAddController(string joystickName)
        {
            Profiler.BeginSample("[MRTK] UnityJoystickManager.GetOrAddController");

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            if (ActiveControllers.ContainsKey(joystickName))
            {
                var controller = ActiveControllers[joystickName];
                Debug.Assert(controller != null);

                Profiler.EndSample(); // GetOrAddController - already registered

                return(controller);
            }

            Type controllerType;

            switch (GetCurrentControllerType(joystickName))
            {
            default:
                Profiler.EndSample();     // GetOrAddController - unknown type
                return(null);

            case SupportedControllerType.GenericUnity:
                controllerType = typeof(GenericJoystickController);
                break;

            case SupportedControllerType.Xbox:
                controllerType = typeof(XboxController);
                break;
            }

            var inputSource        = inputSystem?.RequestNewGenericInputSource($"{controllerType.Name} Controller", sourceType: InputSourceType.Controller);
            var detectedController = Activator.CreateInstance(controllerType, TrackingState.NotTracked, Handedness.None, inputSource, null) as GenericJoystickController;

            if (detectedController == null || !detectedController.Enabled)
            {
                // Controller failed to be setup correctly.
                Debug.LogError($"Failed to create {controllerType.Name} controller");

                Profiler.EndSample(); // GetOrAddController - failure

                // Return null so we don't raise the source detected.
                return(null);
            }

            ActiveControllers.Add(joystickName, detectedController);

            Profiler.EndSample(); // GetOrAddController

            return(detectedController);
        }
示例#13
0
        private OculusQuestHand GetOrAddHand(Handedness handedness, OVRHand ovrHand)
        {
            if (trackedHands.ContainsKey(handedness))
            {
                return(trackedHands[handedness]);
            }

            Material handMaterial = null;

            if (handedness == Handedness.Right)
            {
                if (rightHandMaterial == null)
                {
                    rightHandMaterial = new Material(MRTKOculusConfig.Instance.CustomHandMaterial);
                }
                handMaterial = rightHandMaterial;
            }
            else
            {
                if (leftHandMaterial == null)
                {
                    leftHandMaterial = new Material(MRTKOculusConfig.Instance.CustomHandMaterial);
                }
                handMaterial = leftHandMaterial;
            }

            // Add new hand
            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"Oculus Quest {handedness} Hand", pointers, inputSourceType);

            var controller = new OculusQuestHand(TrackingState.Tracked, handedness, ovrHand, handMaterial, inputSource);

            // Code is obsolete later on, but older MRTK versions require it.
#pragma warning disable 618
            controller.SetupConfiguration(typeof(OculusQuestHand));
#pragma warning restore 618

            for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
            {
                controller.InputSource.Pointers[i].Controller = controller;
            }

            inputSystem?.RaiseSourceDetected(controller.InputSource, controller);

            trackedHands.Add(handedness, controller);

            return(controller);
        }
示例#14
0
        /// <inheritdoc />
        public override void Enable()
        {
            if (!Application.isPlaying ||
                (Commands == null) ||
                (Commands.Length == 0))
            {
                return;
            }

            if (InputSystemProfile == null)
            {
                return;
            }

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            InputSource = inputSystem?.RequestNewGenericInputSource("Windows Speech Input Source", sourceType: InputSourceType.Voice);

            var newKeywords = new string[Commands.Length];

            for (int i = 0; i < Commands.Length; i++)
            {
                newKeywords[i] = Commands[i].LocalizedKeyword;
            }

            RecognitionConfidenceLevel = InputSystemProfile.SpeechCommandsProfile.SpeechRecognitionConfidenceLevel;

            if (keywordRecognizer == null)
            {
                try
                {
                    keywordRecognizer = new KeywordRecognizer(newKeywords, (ConfidenceLevel)RecognitionConfidenceLevel);
                }
                catch (UnityException ex)
                {
                    Debug.LogWarning($"Failed to start keyword recognizer. Are microphone permissions granted? Exception: {ex}");
                    keywordRecognizer = null;
                    return;
                }

                keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
            }

            if (InputSystemProfile.SpeechCommandsProfile.SpeechRecognizerStartBehavior == AutoStartBehavior.AutoStart)
            {
                StartRecognition();
            }
        }
示例#15
0
        private void HandleOnControllerConnected(byte controllerId)
        {
            MLInputController controller = MLInput.GetController(controllerId);

            if (mlInputControllers.Exists((device) => device.Id == controllerId))
            {
                Debug.LogWarning(string.Format("Connected controller with id {0} already connected.", controllerId));
                return;
            }

            mlInputControllers.Add(controller);

            // Generate MRTK Controller
            Handedness controllingHand;

            if (controller.Type == MLInputControllerType.Control && controller.Hand == MLInput.Hand.Left)
            {
                controllingHand = Handedness.Left;
            }
            else if (controller.Type == MLInputControllerType.Control && controller.Hand == MLInput.Hand.Right)
            {
                controllingHand = Handedness.Right;
            }
            else
            {
                controllingHand = Handedness.Other;
            }

            var currentControllerType = SupportedControllerType.GenericOpenVR;
            var controllerType        = typeof(MagicLeapController);
            var pointers = RequestPointers(currentControllerType, controllingHand);

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            var inputSource        = inputSystem?.RequestNewGenericInputSource($"{currentControllerType} Controller {controllingHand}", pointers, InputSourceType.Controller);
            var detectedController = new MagicLeapController(TrackingState.Tracked, controllingHand, inputSource);

            detectedController.SetupConfiguration(controllerType);

            for (int i = 0; i < detectedController.InputSource?.Pointers?.Length; i++)
            {
                detectedController.InputSource.Pointers[i].Controller = detectedController;
            }
            ActiveControllers.Add(controllerId, detectedController);
            inputSystem?.RaiseSourceDetected(detectedController.InputSource, detectedController);
        }
        /// <summary>
        /// Gets or adds a controller using the joystick name provided.
        /// </summary>
        /// <param name="joystickName">The name of the joystick from Unity's <see href="https://docs.unity3d.com/ScriptReference/Input.GetJoystickNames.html">Input.GetJoystickNames</see></param>
        /// <returns>A new controller reference.</returns>
        protected virtual GenericJoystickController GetOrAddController(string joystickName)
        {
            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            if (ActiveControllers.ContainsKey(joystickName))
            {
                var controller = ActiveControllers[joystickName];
                Debug.Assert(controller != null);
                return(controller);
            }

            Type controllerType;

            switch (GetCurrentControllerType(joystickName))
            {
            default:
                return(null);

            case SupportedControllerType.GenericUnity:
                controllerType = typeof(GenericJoystickController);
                break;

            case SupportedControllerType.Xbox:
                controllerType = typeof(XboxController);
                break;
            }

            var inputSource        = inputSystem?.RequestNewGenericInputSource($"{controllerType.Name} Controller", sourceType: InputSourceType.Controller);
            var detectedController = Activator.CreateInstance(controllerType, TrackingState.NotTracked, Handedness.None, inputSource, null) as GenericJoystickController;

            if (detectedController == null)
            {
                Debug.LogError($"Failed to create {controllerType.Name} controller");
                return(null);
            }

            if (!detectedController.SetupConfiguration(controllerType))
            {
                // Controller failed to be setup correctly.
                // Return null so we don't raise the source detected.
                return(null);
            }

            ActiveControllers.Add(joystickName, detectedController);
            return(detectedController);
        }
        private OculusQuestHand GetOrAddHand(Handedness handedness, OVRHand ovrHand)
        {
            if (trackedHands.ContainsKey(handedness))
            {
                return(trackedHands[handedness]);
            }

            // Add new hand
            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"Oculus Quest {handedness} Hand", pointers, inputSourceType);

            if (!inactiveHandCache.TryGetValue(handedness, out var handController))
            {
                handController = new OculusQuestHand(TrackingState.Tracked, handedness, inputSource);
                handController.InitializeHand(ovrHand, MRTKOculusConfig.Instance.CustomHandMaterial);
            }
            inactiveHandCache.Remove(handedness);

            for (int i = 0; i < handController.InputSource?.Pointers?.Length; i++)
            {
                handController.InputSource.Pointers[i].Controller = handController;
            }

            if (MRTKOculusConfig.Instance.ActiveTeleportPointerMode == MRTKOculusConfig.TeleportPointerMode.Custom && MixedRealityToolkit.IsTeleportSystemEnabled)
            {
                if (!teleportPointers.TryGetValue(handedness, out CustomTeleportPointer pointer))
                {
                    pointer = GameObject.Instantiate(MRTKOculusConfig.Instance.CustomTeleportPrefab).GetComponent <CustomTeleportPointer>();
                    pointer.gameObject.SetActive(false);
                    teleportPointers.Add(handedness, pointer);
                }
                pointer.Controller             = handController;
                handController.TeleportPointer = pointer;
            }

            inputSystem?.RaiseSourceDetected(handController.InputSource, handController);

            trackedHands.Add(handedness, handController);

            return(handController);
        }
        private OculusQuestController GetOrAddController(Handedness handedness)
        {
            if (trackedControllers.ContainsKey(handedness))
            {
                return(trackedControllers[handedness]);
            }

            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
            var inputSource = inputSystem?.RequestNewGenericInputSource($"Oculus Quest {handedness} Controller", pointers, inputSourceType);

            if (!inactiveControllerCache.TryGetValue(handedness, out var controller))
            {
                controller = new OculusQuestController(TrackingState.Tracked, handedness, inputSource);
                controller.UpdateAvatarMaterial(MRTKOculusConfig.Instance.CustomHandMaterial);
            }
            inactiveHandCache.Remove(handedness);
            controller.ApplyHandMaterial();

            for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
            {
                controller.InputSource.Pointers[i].Controller = controller;
            }

            if (MixedRealityToolkit.IsTeleportSystemEnabled)
            {
                if (!teleportPointers.TryGetValue(handedness, out CustomTeleportPointer pointer))
                {
                    pointer = GameObject.Instantiate(MRTKOculusConfig.Instance.CustomTeleportPrefab).GetComponent <CustomTeleportPointer>();
                    teleportPointers.Add(handedness, pointer);
                }
                pointer.Controller         = controller;
                controller.TeleportPointer = pointer;
            }

            inputSystem?.RaiseSourceDetected(controller.InputSource, controller);

            trackedControllers.Add(handedness, controller);

            return(controller);
        }
        private LeapMotionHand GetOrAddHand(Hand hand)
        {
            var handId     = hand.Id;
            var handedness = GetHandedness(hand);

            if (trackedHands.ContainsKey(handId))
            {
                var existingHand = trackedHands[handId];
                if (existingHand.ControllerHandedness == handedness)
                {
                    return(existingHand);
                }
                else
                {
                    RemoveHandDevice(handId);
                }
            }

            // Add new hand
            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            var pointers        = RequestPointers(SupportedControllerType.ArticulatedHand, handedness);
            var inputSourceType = InputSourceType.Hand;
            var inputSource     = inputSystem?.RequestNewGenericInputSource($"Leap Motion Hand {handId}", pointers, inputSourceType);

            var controller = new LeapMotionHand(TrackingState.Tracked, handedness, inputSource);

            controller.SetupConfiguration(typeof(LeapMotionHand), inputSourceType);

            for (int i = 0; i < controller.InputSource?.Pointers?.Length; i++)
            {
                controller.InputSource.Pointers[i].Controller = controller;
            }

            inputSystem?.RaiseSourceDetected(inputSource, controller);

            trackedHands.Add(handId, controller);
            UpdateActiveControllers();

            return(controller);
        }
示例#20
0
        /// <inheritdoc />
        public override void Enable()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            if (inputSystem == null)
            {
                Debug.LogError($"Unable to start {Name}. An Input System is required for this feature.");
                return;
            }

            inputSource     = inputSystem.RequestNewGenericInputSource(Name, sourceType: InputSourceType.Voice);
            dictationResult = string.Empty;

            if (dictationRecognizer == null && InputSystemProfile.SpeechCommandsProfile.SpeechRecognizerStartBehavior == AutoStartBehavior.AutoStart)
            {
                InitializeDictationRecognizer();
            }
        }
        /// <inheritdoc />
        public override void Enable()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            if (inputSystem == null)
            {
                Debug.LogError($"Unable to start {Name}. An Input System is required for this feature.");
                return;
            }

            inputSource     = inputSystem.RequestNewGenericInputSource(Name, sourceType: InputSourceType.Voice);
            dictationResult = string.Empty;

            try
            {
                if (dictationRecognizer == null)
                {
                    dictationRecognizer = new DictationRecognizer();

                    dictationRecognizer.DictationHypothesis += DictationRecognizer_DictationHypothesis;
                    dictationRecognizer.DictationResult     += DictationRecognizer_DictationResult;
                    dictationRecognizer.DictationComplete   += DictationRecognizer_DictationComplete;
                    dictationRecognizer.DictationError      += DictationRecognizer_DictationError;
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogWarning($"Failed to start dictation recognizer. Are microphone permissions granted? Exception: {ex}");
                Disable();
                dictationRecognizer = null;
            }
        }
示例#22
0
    void PlugSimulatedMotionController(Handedness controllerHandedness)
    {
        MixedRealityRaycaster.DebugEnabled = true;

        System.Type controllerType = typeof(SimulatedMotionController);

        // Make sure that the handedness declared in the controller attribute matches what we expect
        {
            var controllerAttribute = MixedRealityControllerAttribute.Find(controllerType);
            if (controllerAttribute != null)
            {
                Handedness[] handednesses = controllerAttribute.SupportedHandedness;
                //Debug.Assert(handednesses.Length == 3 && handednesses[0] == Handedness.Any, "Unexpected mouse handedness declared in MixedRealityControllerAttribute");
            }
        }

        IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
        var pointers = RequestPointers(SupportedControllerType.GenericOpenVR, controllerHandedness);
        IMixedRealityInputSource inputSource = inputSystem.RequestNewGenericInputSource("Mouse Input", pointers, InputSourceType.Controller);

        SimulatedMotionController simulatedMotionController = new SimulatedMotionController(TrackingState.Tracked, controllerHandedness, inputSource);

        simulatedMotionController.SetupConfiguration(controllerType);

        if (inputSource != null)
        {
            for (int i = 0; i < inputSource.Pointers.Length; i++)
            {
                inputSource.Pointers[i].Controller = simulatedMotionController;
            }
        }

        SimulatedMotionControllerDictionary.Add(controllerHandedness, simulatedMotionController);

        inputSystem?.RaiseSourceDetected(simulatedMotionController.InputSource, simulatedMotionController);
    }
        private void AddTouchController(Touch touch, Ray ray)
        {
            UnityTouchController     controller;
            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            if (!ActiveTouches.TryGetValue(touch.fingerId, out controller))
            {
                IMixedRealityInputSource inputSource = null;

                if (inputSystem != null)
                {
                    var pointers = RequestPointers(SupportedControllerType.TouchScreen, Handedness.Any);
                    inputSource = inputSystem.RequestNewGenericInputSource($"Touch {touch.fingerId}", pointers);
                }

                controller = new UnityTouchController(TrackingState.NotApplicable, Handedness.Any, inputSource);

                if (inputSource != null)
                {
                    for (int i = 0; i < inputSource.Pointers.Length; i++)
                    {
                        inputSource.Pointers[i].Controller = controller;
                        var touchPointer = (IMixedRealityTouchPointer)inputSource.Pointers[i];
                        touchPointer.TouchRay = ray;
                        touchPointer.FingerId = touch.fingerId;
                    }
                }

                controller.SetupConfiguration(typeof(UnityTouchController));
                ActiveTouches.Add(touch.fingerId, controller);
            }

            inputSystem?.RaiseSourceDetected(controller.InputSource, controller);
            controller.StartTouch();
            UpdateTouchData(touch, ray);
        }
        /// <inheritdoc />
        public override void Enable()
        {
            if (!Application.isPlaying || Commands.Length == 0)
            {
                return;
            }

            if (InputSystemProfile == null)
            {
                return;
            }

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            InputSource = inputSystem?.RequestNewGenericInputSource("Windows Speech Input Source", sourceType: InputSourceType.Voice);

            var newKeywords = new string[Commands.Length];

            for (int i = 0; i < Commands.Length; i++)
            {
                newKeywords[i] = Commands[i].LocalizedKeyword;
            }

            RecognitionConfidenceLevel = InputSystemProfile.SpeechCommandsProfile.SpeechRecognitionConfidenceLevel;

            if (keywordRecognizer == null)
            {
                keywordRecognizer = new KeywordRecognizer(newKeywords, (ConfidenceLevel)RecognitionConfidenceLevel);
                keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
            }

            if (InputSystemProfile.SpeechCommandsProfile.SpeechRecognizerStartBehavior == AutoStartBehavior.AutoStart)
            {
                StartRecognition();
            }
        }
        /// <inheritdoc />
        protected override GenericJoystickController GetOrAddController(string joystickName)
        {
            // If a device is already registered with the ID provided, just return it.
            if (ActiveControllers.ContainsKey(joystickName))
            {
                var controller = ActiveControllers[joystickName];
                Debug.Assert(controller != null);
                return(controller);
            }

            Handedness controllingHand;

            if (joystickName.Contains("Left"))
            {
                controllingHand = Handedness.Left;
            }
            else if (joystickName.Contains("Right"))
            {
                controllingHand = Handedness.Right;
            }
            else
            {
                controllingHand = Handedness.None;
            }

            var  currentControllerType = GetCurrentControllerType(joystickName);
            Type controllerType;

            switch (currentControllerType)
            {
            case SupportedControllerType.GenericOpenVR:
                controllerType = typeof(GenericOpenVRController);
                break;

            case SupportedControllerType.ViveWand:
                controllerType = typeof(ViveWandController);
                break;

            case SupportedControllerType.ViveKnuckles:
                controllerType = typeof(ViveKnucklesController);
                break;

            case SupportedControllerType.OculusTouch:
                controllerType = typeof(OculusTouchController);
                break;

            case SupportedControllerType.OculusRemote:
                controllerType = typeof(OculusRemoteController);
                break;

            case SupportedControllerType.WindowsMixedReality:
                controllerType = typeof(WindowsMixedRealityOpenVRMotionController);
                break;

            default:
                return(null);
            }

            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            var pointers           = RequestPointers(currentControllerType, controllingHand);
            var inputSource        = inputSystem?.RequestNewGenericInputSource($"{currentControllerType} Controller {controllingHand}", pointers, InputSourceType.Controller);
            var detectedController = Activator.CreateInstance(controllerType, TrackingState.NotTracked, controllingHand, inputSource, null) as GenericOpenVRController;

            if (detectedController == null)
            {
                Debug.LogError($"Failed to create {controllerType.Name} controller");
                return(null);
            }

            if (!detectedController.SetupConfiguration(controllerType))
            {
                // Controller failed to be set up correctly.
                Debug.LogError($"Failed to set up {controllerType.Name} controller");
                // Return null so we don't raise the source detected.
                return(null);
            }

            for (int i = 0; i < detectedController.InputSource?.Pointers?.Length; i++)
            {
                detectedController.InputSource.Pointers[i].Controller = detectedController;
            }

            ActiveControllers.Add(joystickName, detectedController);
            return(detectedController);
        }
        /// <summary>
        /// Retrieve the source controller from the Active Store, or create a new device and register it
        /// </summary>
        /// <param name="interactionSource">Source State provided by the SDK</param>
        /// <param name="addController">Should the Source be added as a controller if it isn't found?</param>
        /// <returns>New or Existing Controller Input Source</returns>
        private WindowsMixedRealityController GetController(InteractionSource interactionSource, bool addController = true)
        {
            //If a device is already registered with the ID provided, just return it.
            if (activeControllers.ContainsKey(interactionSource.id))
            {
                var controller = activeControllers[interactionSource.id] as WindowsMixedRealityController;
                Debug.Assert(controller != null);
                return(controller);
            }

            if (!addController)
            {
                return(null);
            }

            Handedness controllingHand;

            switch (interactionSource.handedness)
            {
            default:
                controllingHand = Handedness.None;
                break;

            case InteractionSourceHandedness.Left:
                controllingHand = Handedness.Left;
                break;

            case InteractionSourceHandedness.Right:
                controllingHand = Handedness.Right;
                break;
            }


            IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;

            IMixedRealityPointer[] pointers        = null;
            InputSourceType        inputSourceType = InputSourceType.Other;

            switch (interactionSource.kind)
            {
            case InteractionSourceKind.Controller:
                pointers        = RequestPointers(SupportedControllerType.WindowsMixedReality, controllingHand);
                inputSourceType = InputSourceType.Controller;
                break;

            case InteractionSourceKind.Hand:
                if (interactionSource.supportsPointing)
                {
                    pointers = RequestPointers(SupportedControllerType.ArticulatedHand, controllingHand);
                }
                else
                {
                    pointers = RequestPointers(SupportedControllerType.GGVHand, controllingHand);
                }
                inputSourceType = InputSourceType.Hand;
                break;

            case InteractionSourceKind.Voice:
                // set to null: when pointers are null we use head gaze, which is what we want for voice
                break;

            default:
                Debug.LogError($"Unknown new type in WindowsMixedRealityDeviceManager {interactionSource.kind}, make sure to add a SupportedControllerType");
                break;
            }

            string nameModifier = controllingHand == Handedness.None ? interactionSource.kind.ToString() : controllingHand.ToString();
            var    inputSource  = inputSystem?.RequestNewGenericInputSource($"Mixed Reality Controller {nameModifier}", pointers, inputSourceType);

            WindowsMixedRealityController detectedController;

            if (interactionSource.kind == InteractionSourceKind.Hand)
            {
                if (interactionSource.supportsPointing)
                {
                    detectedController = new WindowsMixedRealityArticulatedHand(TrackingState.NotTracked, controllingHand, inputSource);
                    if (!detectedController.SetupConfiguration(typeof(WindowsMixedRealityArticulatedHand), inputSourceType))
                    {
                        // Controller failed to be setup correctly.
                        // Return null so we don't raise the source detected.
                        return(null);
                    }
                }
                else
                {
                    detectedController = new WindowsMixedRealityGGVHand(TrackingState.NotTracked, controllingHand, inputSource);
                    if (!detectedController.SetupConfiguration(typeof(WindowsMixedRealityGGVHand), inputSourceType))
                    {
                        // Controller failed to be setup correctly.
                        // Return null so we don't raise the source detected.
                        return(null);
                    }
                }
            }
            else
            {
                detectedController = new WindowsMixedRealityController(TrackingState.NotTracked, controllingHand, inputSource);

                if (!detectedController.SetupConfiguration(typeof(WindowsMixedRealityController), inputSourceType))
                {
                    // Controller failed to be setup correctly.
                    // Return null so we don't raise the source detected.
                    return(null);
                }
            }

            for (int i = 0; i < detectedController.InputSource?.Pointers?.Length; i++)
            {
                detectedController.InputSource.Pointers[i].Controller = detectedController;
            }

            activeControllers.Add(interactionSource.id, detectedController);
            return(detectedController);
        }