Exemple #1
0
        // Update is called once per frame
        void Update()
        {
            VRInputDevice tempDevice = VRInput.GetDevice("Xbox");

            // Find a PlayerIndex, for a single player game
            // Will find the first controller that is connected ans use it
            if (!playerIndexSet || !prevState.IsConnected)
            {
                for (int i = 0; i < 4; ++i)
                {
                    PlayerIndex  testPlayerIndex = (PlayerIndex)i;
                    GamePadState testState       = GamePad.GetState(testPlayerIndex);
                    if (testState.IsConnected)
                    {
                        Debug.Log(string.Format("GamePad found {0}", testPlayerIndex));
                        playerIndex    = testPlayerIndex;
                        playerIndexSet = true;
                    }
                }
            }

            prevState = state;
            state     = GamePad.GetState(playerIndex);

            tempDevice.SetAxis("LeftStickX", state.ThumbSticks.Left.X);
            tempDevice.SetAxis("LeftStickY", state.ThumbSticks.Left.Y);
            tempDevice.SetAxis("RightStickX", state.ThumbSticks.Right.X);
            tempDevice.SetAxis("RightStickY", state.ThumbSticks.Right.Y);
            tempDevice.SetAxis("LeftTrigger", state.Triggers.Left);
            tempDevice.SetAxis("RightTrigger", state.Triggers.Right);
            tempDevice.SetAxis("DPadX", state.DPad.Left.Equals(XInputDotNetPure.ButtonState.Pressed) ? -1f : state.DPad.Right.Equals(XInputDotNetPure.ButtonState.Pressed) ? 1f : 0f);
            tempDevice.SetAxis("DPadY", state.DPad.Down.Equals(XInputDotNetPure.ButtonState.Pressed) ? -1f : state.DPad.Up.Equals(XInputDotNetPure.ButtonState.Pressed) ? 1f : 0f);
            tempDevice.SetAxis("Triggers", state.Triggers.Right - state.Triggers.Left);

            tempDevice.SetButton("A", getButtonInt(prevState.Buttons.A, state.Buttons.A));
            tempDevice.SetButton("B", getButtonInt(prevState.Buttons.B, state.Buttons.B));
            tempDevice.SetButton("X", getButtonInt(prevState.Buttons.X, state.Buttons.X));
            tempDevice.SetButton("Y", getButtonInt(prevState.Buttons.Y, state.Buttons.Y));
            tempDevice.SetButton("DPadUp", getButtonInt(prevState.DPad.Up, state.DPad.Up));
            tempDevice.SetButton("DPadDown", getButtonInt(prevState.DPad.Down, state.DPad.Down));
            tempDevice.SetButton("DPadLeft", getButtonInt(prevState.DPad.Left, state.DPad.Left));
            tempDevice.SetButton("DPadRight", getButtonInt(prevState.DPad.Right, state.DPad.Right));
            tempDevice.SetButton("LeftShoulder", getButtonInt(prevState.Buttons.LeftShoulder, state.Buttons.LeftShoulder));
            tempDevice.SetButton("RightShoulder", getButtonInt(prevState.Buttons.RightShoulder, state.Buttons.RightShoulder));
            tempDevice.SetButton("LeftTrigger", getTriggerButtonInt(prevState.Triggers.Left, state.Triggers.Left));
            tempDevice.SetButton("RightTrigger", getTriggerButtonInt(prevState.Triggers.Right, state.Triggers.Right));
            tempDevice.SetButton("LeftStick", getButtonInt(prevState.Buttons.LeftStick, state.Buttons.LeftStick));
            tempDevice.SetButton("RightStick", getButtonInt(prevState.Buttons.RightStick, state.Buttons.RightStick));
            tempDevice.SetButton("Back", getButtonInt(prevState.Buttons.Back, state.Buttons.Back));
            tempDevice.SetButton("Start", getButtonInt(prevState.Buttons.Start, state.Buttons.Start));

            tempDevice.Vibrate();
        }
        void Update()
        {
            //if we don't have one of these, something is wrong, and we shouldn't do anything
            if (hmd == null || Left == null || Right == null)
            {
                return;
            }

            //If we have the left controller
            if (Left.isValid)
            {
                deviceLeft = SteamVR_Controller.Input((int)Left.index);

                VRInputDevice tempDevice = VRInput.GetDevice("ViveLeft");

                tempDevice.isTracked = true;
                tempDevice.SetInt("Index", (int)Left.index);
                tempDevice.transform = Left.transform;

                tempDevice.position        = Left.transform.position;
                tempDevice.forward         = Left.transform.forward;
                tempDevice.up              = Left.transform.up;
                tempDevice.right           = Left.transform.right;
                tempDevice.velocity        = deviceLeft.velocity;
                tempDevice.rotation        = Left.transform.rotation;
                tempDevice.angularVelocity = deviceLeft.angularVelocity;

                tempDevice.SetButton("Trigger", GetButtonInt(deviceLeft, EVRButtonId.k_EButton_SteamVR_Trigger));
                tempDevice.SetButton("Menu", GetButtonInt(deviceLeft, EVRButtonId.k_EButton_ApplicationMenu));
                tempDevice.SetButton("Touchpad", GetButtonInt(deviceLeft, EVRButtonId.k_EButton_SteamVR_Touchpad));
                tempDevice.SetButton("Grip", GetButtonInt(deviceLeft, EVRButtonId.k_EButton_Grip));

                tempDevice.SetTouchButton("Touchpad", GetTouchButtonInt(deviceLeft, EVRButtonId.k_EButton_SteamVR_Touchpad));

                Vector2 touchpadVector = deviceLeft.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
                tempDevice.SetAxis("TouchpadX", touchpadVector.x);
                tempDevice.SetAxis("TouchpadY", touchpadVector.y);
                tempDevice.SetAxis("TouchpadR", Mathf.Sqrt(touchpadVector.x * touchpadVector.x + touchpadVector.y * touchpadVector.y));
                tempDevice.SetAxis("TouchpadT", Mathf.Atan2(touchpadVector.y, touchpadVector.x) * Mathf.Rad2Deg);
                tempDevice.SetAxis("Trigger", deviceLeft.GetAxis(EVRButtonId.k_EButton_SteamVR_Trigger).x);

                tempDevice.Vibrate();

                /*
                 * ViveInput.Left.IsTracked = true;
                 * ViveInput.Left.Index = (int)Left.index;
                 * ViveInput.Left.Transform = Left.transform;
                 *
                 * ViveInput.Left.Position = Left.transform.position;//deviceLeft.transform.pos;
                 * ViveInput.Left.Forward = Left.transform.forward;
                 * ViveInput.Left.Up = Left.transform.up;
                 * ViveInput.Left.Right = Left.transform.right;
                 * ViveInput.Left.Velocity = deviceLeft.velocity;
                 * ViveInput.Left.Rotation = Left.transform.rotation;//deviceLeft.transform.rot;
                 * ViveInput.Left.AngularVelocity = deviceLeft.angularVelocity;
                 *
                 *
                 * ViveInput.Left.Buttons.Trigger = getButtonState(deviceLeft, EVRButtonId.k_EButton_SteamVR_Trigger);
                 * ViveInput.Left.TriggerAxis = deviceLeft.GetAxis(EVRButtonId.k_EButton_SteamVR_Trigger);
                 * ViveInput.Left.Buttons.Grip = getButtonState(deviceLeft, EVRButtonId.k_EButton_Grip);
                 * ViveInput.Left.Buttons.TouchpadButton = getButtonState (deviceLeft, EVRButtonId.k_EButton_SteamVR_Touchpad);
                 * ViveInput.Left.Buttons.Menu = getButtonState (deviceLeft, EVRButtonId.k_EButton_ApplicationMenu);
                 *
                 * ViveInput.Left.TouchpadAxis = deviceLeft.GetAxis (EVRButtonId.k_EButton_SteamVR_Touchpad);
                 * if(ViveInput.Left.Buttons.TouchpadButton.Equals(ButtonState.Touched) || ViveInput.Left.Buttons.TouchpadButton.Equals(ButtonState.Pressed)) {
                 *      ViveInput.Left.TouchpadDeltaX = (ViveInput.Left.TouchpadAxis.x - lastLeftTouchpad.x) * Time.deltaTime;
                 *      ViveInput.Left.TouchpadDeltaY = (ViveInput.Left.TouchpadAxis.y - lastLeftTouchpad.y) * Time.deltaTime;
                 *      ViveInput.Left.TouchpadDeltaR = ((ViveInput.Left.TouchpadAxis).magnitude - (lastLeftTouchpad).magnitude) * Time.deltaTime;
                 *      ViveInput.Left.TouchpadDeltaT = getDeltaAngle(ViveInput.Left.TouchpadAxis, lastLeftTouchpad) * Time.deltaTime;
                 * } else {
                 *      ViveInput.Left.TouchpadDeltaX = ViveInput.Left.TouchpadDeltaY = ViveInput.Left.TouchpadDeltaR = ViveInput.Left.TouchpadDeltaT = 0f;
                 * }
                 * ViveInput.Left.TouchpadDir = getTouchpadDir(ViveInput.Left.TouchpadAxis, ViveInput.Left.Buttons.TouchpadButton);
                 *
                 * lastLeftTouchpad = ViveInput.Left.TouchpadAxis;
                 */
            }
            else
            {
                VRInput.GetDevice("ViveLeft").isTracked = false;
                deviceLeft = null;
                //deviceLeft = null;
                //ViveInput.Left.IsTracked = false;
            }
            //If we have the right controller
            if (Right.isValid)
            {
                deviceRight = SteamVR_Controller.Input((int)Right.index);

                VRInputDevice tempDevice = VRInput.GetDevice("ViveRight");

                tempDevice.isTracked = true;
                tempDevice.SetInt("Index", (int)Right.index);
                tempDevice.transform = Right.transform;

                tempDevice.position        = Right.transform.position;
                tempDevice.forward         = Right.transform.forward;
                tempDevice.up              = Right.transform.up;
                tempDevice.right           = Right.transform.right;
                tempDevice.velocity        = deviceRight.velocity;
                tempDevice.rotation        = Right.transform.rotation;
                tempDevice.angularVelocity = deviceRight.angularVelocity;

                tempDevice.SetButton("Trigger", GetButtonInt(deviceRight, EVRButtonId.k_EButton_SteamVR_Trigger));
                tempDevice.SetButton("Menu", GetButtonInt(deviceRight, EVRButtonId.k_EButton_ApplicationMenu));
                tempDevice.SetButton("Touchpad", GetButtonInt(deviceRight, EVRButtonId.k_EButton_SteamVR_Touchpad));
                tempDevice.SetButton("Grip", GetButtonInt(deviceRight, EVRButtonId.k_EButton_Grip));

                tempDevice.SetTouchButton("Touchpad", GetTouchButtonInt(deviceRight, EVRButtonId.k_EButton_SteamVR_Touchpad));

                Vector2 touchpadVector = deviceRight.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
                tempDevice.SetAxis("TouchpadX", touchpadVector.x);
                tempDevice.SetAxis("TouchpadY", touchpadVector.y);
                tempDevice.SetAxis("TouchpadR", Mathf.Sqrt(touchpadVector.x * touchpadVector.x + touchpadVector.y * touchpadVector.y));
                tempDevice.SetAxis("TouchpadT", Mathf.Atan2(touchpadVector.y, touchpadVector.x) * Mathf.Rad2Deg);
                tempDevice.SetAxis("Trigger", deviceRight.GetAxis(EVRButtonId.k_EButton_SteamVR_Trigger).x);

                tempDevice.Vibrate();

                /*
                 * ViveInput.Right.IsTracked = true;
                 * ViveInput.Right.Index = (int)Right.index;
                 * ViveInput.Right.Transform = Right.transform;
                 *
                 * ViveInput.Right.Position = Right.transform.position;//deviceRight.transform.pos;
                 * ViveInput.Right.Forward = Right.transform.forward;
                 * ViveInput.Right.Up = Right.transform.up;
                 * ViveInput.Right.Right = Right.transform.right;
                 * ViveInput.Right.Velocity = deviceRight.velocity;
                 * ViveInput.Right.Rotation = Right.transform.rotation;//deviceRight.transform.rot;
                 * ViveInput.Right.AngularVelocity = deviceRight.angularVelocity;
                 *
                 * ViveInput.Right.Buttons.Trigger = getButtonState(deviceRight, EVRButtonId.k_EButton_SteamVR_Trigger);
                 * ViveInput.Right.TriggerAxis = deviceRight.GetAxis(EVRButtonId.k_EButton_SteamVR_Trigger);
                 * ViveInput.Right.Buttons.Grip = getButtonState(deviceRight, EVRButtonId.k_EButton_Grip);
                 * ViveInput.Right.Buttons.TouchpadButton = getButtonState (deviceRight, EVRButtonId.k_EButton_SteamVR_Touchpad);
                 * ViveInput.Right.Buttons.Menu = getButtonState (deviceRight, EVRButtonId.k_EButton_ApplicationMenu);
                 *
                 * ViveInput.Right.TouchpadAxis = deviceRight.GetAxis (EVRButtonId.k_EButton_SteamVR_Touchpad);
                 * if(ViveInput.Right.Buttons.TouchpadButton.Equals(ButtonState.Touched) || ViveInput.Right.Buttons.TouchpadButton.Equals(ButtonState.Pressed)) {
                 *      ViveInput.Right.TouchpadDeltaX = (ViveInput.Right.TouchpadAxis.x - lastRightTouchpad.x) * Time.deltaTime;
                 *      ViveInput.Right.TouchpadDeltaY = (ViveInput.Right.TouchpadAxis.y - lastRightTouchpad.y) * Time.deltaTime;
                 *      ViveInput.Right.TouchpadDeltaR = ((ViveInput.Right.TouchpadAxis).magnitude - (lastRightTouchpad).magnitude) * Time.deltaTime;
                 *      ViveInput.Right.TouchpadDeltaT = getDeltaAngle(ViveInput.Right.TouchpadAxis, lastRightTouchpad) * Time.deltaTime;
                 * } else {
                 *      ViveInput.Right.TouchpadDeltaX = ViveInput.Right.TouchpadDeltaY = ViveInput.Right.TouchpadDeltaR = ViveInput.Right.TouchpadDeltaT = 0f;
                 * }
                 * ViveInput.Right.TouchpadDir = getTouchpadDir(ViveInput.Right.TouchpadAxis, ViveInput.Right.Buttons.TouchpadButton);
                 *
                 * lastRightTouchpad = ViveInput.Right.TouchpadAxis;
                 */
            }
            else
            {
                VRInput.GetDevice("ViveRight").isTracked = false;
                deviceRight = null;
                //ViveInput.Right.IsTracked = false;
            }
                        #if UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0 || UNITY_4
            //If we have the HMD
            if (hmd.isValid)
            {
                deviceHMD = SteamVR_Controller.Input((int)hmd.index);

                VRInputDevice tempDevice = VRInput.GetDevice("ViveHMD");

                tempDevice.isTracked = true;
                tempDevice.SetInt("Index", (int)hmd.index);
                tempDevice.transform = hmd.transform;

                tempDevice.position        = hmd.transform.position;
                tempDevice.forward         = hmd.transform.forward;
                tempDevice.up              = hmd.transform.up;
                tempDevice.right           = hmd.transform.right;
                tempDevice.velocity        = deviceHMD.velocity;
                tempDevice.rotation        = hmd.transform.rotation;
                tempDevice.angularVelocity = deviceHMD.angularVelocity;

                /*
                 * ViveInput.HMD.IsTracked = true;
                 * ViveInput.HMD.Index = (int)hmd.index;
                 * ViveInput.HMD.Transform = hmd.transform;
                 *
                 * ViveInput.HMD.Position = hmd.transform.position;
                 * ViveInput.HMD.Forward = hmd.transform.forward;
                 * ViveInput.HMD.Up = hmd.transform.up;
                 * ViveInput.HMD.Right = hmd.transform.right;
                 * ViveInput.HMD.Velocity = deviceHMD.velocity;
                 * ViveInput.HMD.Rotation = deviceHMD.transform.rot;
                 * ViveInput.HMD.AngularVelocity = deviceHMD.angularVelocity;
                 *
                 * ViveInput.HMD.Buttons.Trigger = ButtonState.Inactive;
                 * ViveInput.HMD.Buttons.Grip = ButtonState.Inactive;
                 * ViveInput.HMD.Buttons.TouchpadButton = ButtonState.Inactive;
                 * ViveInput.HMD.Buttons.Menu = ButtonState.Inactive;
                 *
                 * ViveInput.HMD.TouchpadAxis = Vector3.zero;
                 */
            }
            else
            {
                VRInput.GetDevice("ViveHMD").isTracked = false;
                deviceHMD = null;
                //ViveInput.HMD.IsTracked = false;
            }
                        #else
            if (VRDevice.isPresent)
            {
                VRInputDevice tempDevice = VRInput.GetDevice("ViveHMD");

                tempDevice.isTracked = true;
                tempDevice.transform = hmd;

                tempDevice.position = hmd.position;
                tempDevice.forward  = hmd.forward;
                tempDevice.up       = hmd.up;
                tempDevice.right    = hmd.right;
                tempDevice.rotation = hmd.transform.rotation;

                tempDevice.velocity        = (hmd.position - lastPosition) / Time.deltaTime;
                tempDevice.angularVelocity = (hmd.rotation * Quaternion.Inverse(lastRotation)).eulerAngles / Time.deltaTime;

                lastPosition = hmd.position;
                lastRotation = hmd.rotation;

                tempDevice.SetString("Model", VRDevice.model);
                tempDevice.SetFloat("RefreshRate", VRDevice.refreshRate);

                tempDevice.SetVector3("Head", InputTracking.GetLocalPosition(VRNode.Head));
                tempDevice.SetVector3("LeftEye", InputTracking.GetLocalPosition(VRNode.LeftEye));
                tempDevice.SetVector3("RightEye", InputTracking.GetLocalPosition(VRNode.RightEye));
                tempDevice.SetVector3("CenterEye", InputTracking.GetLocalPosition(VRNode.CenterEye));

                tempDevice.SetQuaternion("Head", InputTracking.GetLocalRotation(VRNode.Head));
                tempDevice.SetQuaternion("LeftEye", InputTracking.GetLocalRotation(VRNode.LeftEye));
                tempDevice.SetQuaternion("RightEye", InputTracking.GetLocalRotation(VRNode.RightEye));
                tempDevice.SetQuaternion("CenterEye", InputTracking.GetLocalRotation(VRNode.CenterEye));
            }
            else
            {
                VRInput.GetDevice("ViveHMD").isTracked = false;
            }
                        #endif
        }