Exemplo n.º 1
0
        private void UpdateDebugAxis(InteractionSourceState state)
        {
            Vector3 pointerForward = Vector3.zero, gripForward = Vector3.zero, gripPosition = Vector3.zero, pointerPosition = Vector3.zero;
            bool    hasPointerPosition = false, hasGripPosition = false, hasGripRotation = false,
                    hasPointerRotation = false, hasPointerForward = false, hasGripForward = false;
            Quaternion pointerRotation = Quaternion.identity, gripRotation = Quaternion.identity;
            Ray        pointerRay;

            //          TraceHelper.Log("Updating debug axis");

            if (
#if !UNITY_5
                (hasPointerPosition = state.sourcePose.TryGetPosition(out pointerPosition, InteractionSourceNode.Pointer)) &&
                (hasPointerForward = state.sourcePose.TryGetForward(out pointerForward, InteractionSourceNode.Pointer)) &&
                (hasPointerRotation = state.sourcePose.TryGetRotation(out pointerRotation, InteractionSourceNode.Pointer)) &&
                (hasGripForward = state.sourcePose.TryGetForward(out gripForward, InteractionSourceNode.Grip)) &&
                (hasGripPosition = state.sourcePose.TryGetPosition(out gripPosition, InteractionSourceNode.Grip)) &&
                (hasGripRotation = state.sourcePose.TryGetRotation(out gripRotation, InteractionSourceNode.Grip))
#else
                (hasPointerPosition = hasPointerForward = state.sourcePose.TryGetPointerRay(out pointerRay)) &&
                //(hasGripForward = state.sourcePose.TryGetForward(out gripForward )) &&
                (hasGripPosition = state.sourcePose.TryGetPosition(out gripPosition)) &&
                (hasGripRotation = state.sourcePose.TryGetRotation(out gripRotation))
#endif
                )
            {
#if UNITY_5
                if (hasPointerPosition && hasPointerForward)
                {
                    pointerPosition = pointerRay.origin;
                    pointerForward  = pointerRay.direction;
                }
#endif
                AxisRenderer axis = (state.IsLeftHand() ? axisRendererLeft : axisRendererRight);
                if (axis != null)
                {
                    if (hasPointerPosition && hasPointerRotation)
                    {
                        axis.SetWorldValues(pointerPosition, pointerForward, pointerRotation, AxisRenderer.ControllerElement.Pointer);
                    }
                    if (hasGripPosition && hasGripRotation)
                    {
                        axis.SetWorldValues(gripPosition, gripForward, gripRotation, AxisRenderer.ControllerElement.Grip);
                    }
                }
            }

            //TraceHelper.Assert(hasPointerForward && hasGripForward && hasGripPosition &&
            //                     hasPointerPosition && hasGripRotation && hasPointerRotation, "Show debug axis should not fail");


#if SKIPNPUTMODULE
            WinMRSnippets.Samples.Input.MotionControllerInputModule.Instance.SetPosition(state.source.id, pointerPosition);
            WinMRSnippets.Samples.Input.MotionControllerInputModule.Instance.SetForwardPointer(state.source.id, pointerForward);
            WinMRSnippets.Samples.Input.MotionControllerInputModule.Instance.SetButtonStates(state.source.id,
                                                                                             state.selectPressed, state.grasped, state.menuPressed);
#endif
        }
Exemplo n.º 2
0
    private void UpdateUnityInput()
    {
        UpdateTrackedControllers();

        var left  = InputState.Current.GetController(XRNode.LeftHand);
        var right = InputState.Current.GetController(XRNode.RightHand);

        // Motion
        foreach (XRNode nodeType in uDevices.Keys)
        {
            MotionControllerState state;
            if (nodeType == XRNode.LeftHand)
            {
                state = left;
            }
            else if (nodeType == XRNode.RightHand)
            {
                state = right;
            }
            else
            {
                TraceHelper.Log("Ignoring nodetype" + nodeType);
                continue;
            }
            var position = InputTracking.GetLocalPosition(nodeType);
            var rotation = InputTracking.GetLocalRotation(nodeType);

            SetTransform(uDevices[nodeType], position, rotation);

            if (ShowDebugAxis)
            {
                AxisRenderer axis   = null;
                var          angles = rotation.eulerAngles;

                axis = (nodeType == XRNode.RightHand) ? axisRendererRight : axisRendererLeft;
                if (axis != null)
                {
                    axis.SetWorldValues(position, (rotation * Vector3.forward), rotation, AxisRenderer.ControllerElement.Grip);
                }

                if (axis != null)
                {
                    position.y += .05f;
                    axis.SetWorldValues(position, angles, rotation, AxisRenderer.ControllerElement.Pointer);
                }
            }

            state.GripPosition = position;
            state.GripRotation = rotation;
        }

        // Buttons
        if (Input.GetButtonDown(UnityInputAxis.MotionController_Select_Left))
        {
            left.SelectPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_Select_Right))
        {
            right.SelectPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_Menu_Left))
        {
            left.MenuPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_Menu_Right))
        {
            right.MenuPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_Grasp_Left))
        {
            left.GraspPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_Grasp_Right))
        {
            right.GraspPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_TouchpadTouched_Left))
        {
            left.TouchPadTouched = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_TouchpadTouched_Right))
        {
            right.TouchPadTouched = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_TouchpadPressed_Left))
        {
            left.TouchPadPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_TouchpadPressed_Right))
        {
            right.TouchPadPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_ThumbstickPressed_Left))
        {
            left.ThumbstickPressed = true;
        }
        if (Input.GetButtonDown(UnityInputAxis.MotionController_ThumbstickPressed_Right))
        {
            right.ThumbstickPressed = true;
        }

        // Axes

        left.SelectValue       = Input.GetAxis(UnityInputAxis.MotionController_SelectPressedValue_Left);
        right.SelectValue      = Input.GetAxis(UnityInputAxis.MotionController_SelectPressedValue_Right);
        left.TouchPadXValue    = Input.GetAxis(UnityInputAxis.MotionController_TouchpadX_Left);
        left.TouchPadYValue    = Input.GetAxis(UnityInputAxis.MotionController_TouchpadY_Left);
        right.TouchPadXValue   = Input.GetAxis(UnityInputAxis.MotionController_TouchpadX_Right);
        right.TouchPadYValue   = Input.GetAxis(UnityInputAxis.MotionController_TouchpadY_Right);
        left.ThumbstickXValue  = Input.GetAxis(UnityInputAxis.MotionController_ThumbstickX_Left);
        left.ThumbstickYValue  = Input.GetAxis(UnityInputAxis.MotionController_ThumbstickY_Left);
        right.ThumbstickXValue = Input.GetAxis(UnityInputAxis.MotionController_ThumbstickX_Right);
        right.ThumbstickYValue = Input.GetAxis(UnityInputAxis.MotionController_ThumbstickY_Right);

#if TRACING_VERBOSE
        right.TraceState(PoseSource.Any);
        left.TraceState(PoseSource.Any);
#endif
    }
        private void UpdateInteractionSourceState(InteractionSourceState sourceState)
        {
            uint id = sourceState.source.id;
            MotionControllerState state = InputState.Current.GetController(sourceState);

            if (imDevices.ContainsKey(id))
            {
                var        sourcePose      = sourceState.sourcePose;
                Vector3    position        = Vector3.zero;
                Quaternion rotation        = Quaternion.identity;
                Vector3    pointerPosition = Vector3.zero;
                Quaternion pointerRotation = Quaternion.identity;

#if UNITY_5
                bool hasRotation = sourcePose.TryGetRotation(out rotation);
                bool hasPosition = sourcePose.TryGetPosition(out position);
#else
                bool hasRotation = sourcePose.TryGetRotation(out rotation, InteractionSourceNode.Grip);
                bool hasPosition = sourcePose.TryGetPosition(out position, InteractionSourceNode.Grip);
#endif

#if UNITY_5
                bool hasPointerPosition = sourcePose.TryGetPointerPosition(out pointerPosition);
                bool hasPointerRotation = sourcePose.TryGetPointerRotation(out pointerRotation);
#else
                bool hasPointerPosition = sourcePose.TryGetPosition(out pointerPosition, InteractionSourceNode.Pointer);
                bool hasPointerRotation = sourcePose.TryGetRotation(out pointerRotation, InteractionSourceNode.Pointer);
#endif

                if (hasPosition || hasRotation || hasPointerPosition || hasPointerRotation)
                {
#if VALIDATE_INPUT
                    if (pointerPosition.IsDebugValid() && position.IsDebugValid() &&
                        pointerRotation.IsDebugValid() && rotation.IsDebugValid()
                        )
#endif
                    {
                        state.GripPosition = position;
                        state.GripRotation = rotation;

                        if ((hasPosition && hasRotation) && ((FilterInput & PoseSource.Grip) != PoseSource.None))
                        {
                            SetTransform(imDevices[id], position, rotation);
                        }

                        else if ((hasPointerPosition && hasPointerRotation) && ((FilterInput & PoseSource.Pointer) != PoseSource.None))
                        {
                            SetTransform(imDevices[id], pointerPosition, pointerRotation);
                        }


                        if (hasPointerPosition)
                        {
                            Vector3 forward;
    #if UNITY_5
                            Ray ray;
                            if (sourcePose.TryGetPointerRay(out ray))
                            {
                                forward         = ray.direction;
                                pointerPosition = ray.origin;
    #else
                            if (sourcePose.TryGetForward(out forward, InteractionSourceNode.Pointer))
                            {
    #endif
    #if VERBOSE_POSITION
                                TraceHelper.LogDiff(string.Format("Pos:{0}, Fwd:{1}",
                                                                  pointerPosition, forward), Cache.ForwardPosition);
    #endif
                                if (cursor != null)
                                {
                                    cursor.transform.position = pointerPosition + forward;
                                }
                            }
                            else
                            {
                                TraceHelper.Log("Failed get forward");
                            }
                        }
                    }
#if VALIDATE_INPUT
                    else
                    {
                        TraceHelper.LogModulus(string.Format("Invalid: Pointer: {0}, Grip: {1} ", pointerPosition, position), TraceVariables.InteractionManagerLoop);
                    }
#endif
                }
                else
                {
                    TraceHelper.LogModulus(string.Format("No readings:{0}-- Grip: Pos-{1}, Rot-{2}, PointerPos-{3}, Rot-{4}",
#if UNITY_5
                                                         sourceState.source.sourceKind.ToString(),
#else
                                                         sourceState.source.kind.ToString(),
#endif
                                                         hasPosition, hasRotation,
                                                         hasPointerPosition, hasPointerRotation
                                                         ), TraceVariables.InteractionManagerLoop);
                }
                var rootproperties = sourceState;
#if UNITY_5
                state.SelectValue       = (float)rootproperties.selectPressedValue;
                state.TouchPadXValue    = (float)rootproperties.controllerProperties.touchpadX;
                state.TouchPadYValue    = (float)rootproperties.controllerProperties.touchpadY;
                state.ThumbstickXValue  = (float)rootproperties.controllerProperties.thumbstickX;
                state.ThumbstickYValue  = (float)rootproperties.controllerProperties.thumbstickY;
                state.TouchPadPressed   = rootproperties.controllerProperties.touchpadPressed;
                state.TouchPadTouched   = rootproperties.controllerProperties.touchpadTouched;
                state.ThumbstickPressed = rootproperties.controllerProperties.thumbstickPressed;
#else
                state.SelectValue       = rootproperties.selectPressedAmount;
                state.TouchPadXValue    = rootproperties.touchpadPosition.x;
                state.TouchPadYValue    = rootproperties.touchpadPosition.y;
                state.ThumbstickXValue  = rootproperties.thumbstickPosition.x;
                state.ThumbstickYValue  = rootproperties.thumbstickPosition.y;
                state.TouchPadPressed   = rootproperties.touchpadPressed;
                state.TouchPadTouched   = rootproperties.touchpadTouched;
                state.ThumbstickPressed = rootproperties.thumbstickPressed;
#endif

                state.SelectPressed   = rootproperties.selectPressed;
                state.MenuPressed     = rootproperties.menuPressed;
                state.GraspPressed    = rootproperties.grasped;
                state.GripPosition    = position;
                state.PointerPosition = pointerPosition;
                state.GripRotation    = rotation;
                state.PointerRotation = pointerRotation;
                state.IsLeftHand      = sourceState.source.handedness == InteractionSourceHandedness.Left;
                state.IsRightHand     = sourceState.source.handedness == InteractionSourceHandedness.Right;

#if VERBOSE_STATE
                bool   hasChanges, hasEvent;
                string traceState = state.GetTraceState(FilterInput, false, out hasChanges, out hasEvent);
                if (hasChanges || hasEvent)
                {
                    Debug.Log(traceState);
                }
#endif

                Vector3 pointerForward = Vector3.zero, gripForward = Vector3.zero;
                bool    hasPointerForward = false, hasGripForward = false;
#if !UNITY_5
                if ((hasPointerForward = sourcePose.TryGetForward(out pointerForward, InteractionSourceNode.Pointer)) &&
                    (hasGripForward = sourcePose.TryGetForward(out gripForward, InteractionSourceNode.Grip)))
                {
#else
                Ray rayAxis;
                if ((hasPointerForward = hasPointerPosition = sourcePose.TryGetPointerRay(out rayAxis)))
                {
                    pointerForward  = rayAxis.direction;
                    pointerPosition = rayAxis.origin;
                    hasGripForward  = true; //it is a zero vector.
#endif
                    if (ShowDebugAxis)
                    {
                        AxisRenderer axis = (state.IsLeftHand ? axisRendererLeft : axisRendererRight);
                        if (axis != null)
                        {
                            if (hasPointerForward && hasPointerPosition)
                            {
                                axis.SetWorldValues(state.PointerPosition, pointerForward, state.PointerRotation, AxisRenderer.ControllerElement.Pointer);
                            }
                            if (hasGripForward && hasPosition)
                            {
                                axis.SetWorldValues(state.GripPosition, gripForward, state.GripRotation, AxisRenderer.ControllerElement.Grip);
                            }
                        }
                    }
                }
#if VERBOSE_STATE
                else
                {
                    TraceHelper.LogModulus(string.Format("No Forward vectors. Grip: {0}, pointer:{1}",
                                                         hasPointerForward, hasGripForward), TraceVariables.InteractionManagerLoop);
                }
#endif


                if (AnimateControllerModel)
                {
                    MotionControllerInfo currentController;
                    if (controllerInfoForAnimation.TryGetValue(sourceState.source.id, out currentController))
                    {
#if !UNITY_5
                        currentController.AnimateSelect(sourceState.selectPressedAmount);
                        if (sourceState.source.supportsGrasp)
                        {
                            currentController.AnimateGrasp(sourceState.grasped);
                        }

                        if (sourceState.source.supportsMenu)
                        {
                            currentController.AnimateMenu(sourceState.menuPressed);
                        }
                        if (sourceState.source.supportsThumbstick)
                        {
                            currentController.AnimateThumbstick(sourceState.thumbstickPressed, sourceState.thumbstickPosition);
                        }

                        if (sourceState.source.supportsTouchpad)
                        {
                            currentController.AnimateTouchpad(sourceState.touchpadPressed, sourceState.touchpadTouched, sourceState.touchpadPosition);
                        }
#else
                        currentController.AnimateSelect((float)sourceState.selectPressedValue);
                        if (sourceState.source.supportsGrasp)
                        {
                            currentController.AnimateGrasp(sourceState.grasped);
                        }

                        if (sourceState.source.supportsMenu)
                        {
                            currentController.AnimateMenu(sourceState.menuPressed);
                        }

                        InteractionController controller;
                        if (sourceState.source.TryGetController(out controller))
                        {
                            if (controller.hasThumbstick)
                            {
                                currentController.AnimateThumbstick(sourceState.controllerProperties.thumbstickPressed,
                                                                    new Vector2((float)sourceState.controllerProperties.thumbstickX,
                                                                                (float)sourceState.controllerProperties.thumbstickY));
                            }

                            if (controller.hasTouchpad)
                            {
                                currentController.AnimateTouchpad(sourceState.controllerProperties.touchpadPressed,
                                                                  sourceState.controllerProperties.touchpadTouched,
                                                                  new Vector2((float)sourceState.controllerProperties.touchpadX,
                                                                              (float)sourceState.controllerProperties.touchpadX));
                            }
                        }
#endif
                    }
                    else
                    {
                        TraceHelper.Log("Could not animate " + sourceState.source.id);
                    }
                }

                if (state.ThumbstickPressed &&
                    (Time.unscaledTime > (debounceThumbstickTime + 3f))
                    )
                {
                    ShowDebugAxis = !ShowDebugAxis;
                    UpdateDebugAxis(ShowDebugAxis);
                    debounceThumbstickTime = Time.unscaledTime;
                }
            }
        }

        float debounceThumbstickTime = 0f;