Exemplo n.º 1
0
        public void Go(GraphicsDevice graphicsDevice, Action <BasicEffect> render)
        {
            var leftHandIndex  = _vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            var rightHandIndex = _vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
            var maxHandIndex   = Math.Max(leftHandIndex, rightHandIndex);

            if (maxHandIndex >= _renderPose.Length && maxHandIndex != uint.MaxValue)
            {
                _renderPose = new TrackedDevicePose_t[maxHandIndex + 1];
                _gamePose   = new TrackedDevicePose_t[maxHandIndex + 1];
            }
            Check(_vrCompositor.WaitGetPoses(_renderPose, _gamePose));
            if (leftHandIndex != uint.MaxValue && rightHandIndex != uint.MaxValue)
            {
                var leftControllerState = default(VRControllerState_t);
                _vrSystem.GetControllerState(leftHandIndex, ref leftControllerState, (uint)Marshal.SizeOf <VRControllerState_t>());
                var rightControllerState = default(VRControllerState_t);
                _vrSystem.GetControllerState(rightHandIndex, ref rightControllerState, (uint)Marshal.SizeOf <VRControllerState_t>());
                var left  = Matrix.Transpose(ToMonogameMatrix(_renderPose[leftHandIndex].mDeviceToAbsoluteTracking));
                var right = Matrix.Transpose(ToMonogameMatrix(_renderPose[rightHandIndex].mDeviceToAbsoluteTracking));
                _vrControls.Update(leftControllerState, left, rightControllerState, right);
            }

            Render(EVREye.Eye_Left, ref _leftEffect, ref _leftTarget, ref _leftTargetHandle, graphicsDevice, _renderPose[0], render);
            Render(EVREye.Eye_Right, ref _rightEffect, ref _rightTarget, ref _rightTargetHandle, graphicsDevice, _renderPose[0], render);
        }
Exemplo n.º 2
0
        public void VesselControl(FlightCtrlState s)
        {
            if (hmdIsAllowed && hmdIsInitialized)
            {
                // handle left controller inputs
                //--------------------------------------------------------------
                bool ctrlStateOk = vrSystem.GetControllerState(ctrlIndexLeft, ref ctrlStateLeft);
                if (ctrlStateOk && ctrlStateLeft.unPacketNum != ctrlStateLeft_lastPacketNum)
                {
                    // activate next stage with app menu button
                    if ((ctrlStateLeft.ulButtonPressed & CONTROLLER_BUTTON_MASK_APP_MENU) > 0)
                    {
                        KSP.UI.Screens.StageManager.ActivateNextStage();
                    }

                    // control the throttle by touching the touchpad (no need to click)
                    if ((ctrlStateLeft.ulButtonTouched & CONTROLLER_BUTTON_MASK_TOUCHPAD) > 0)
                    {
                        float throttleCmd = Mathf.Clamp((ctrlStateLeft.rAxis0.y + 1.0f) / 2.0f, 0.0f, 1.0f);
                        s.mainThrottle = throttleCmd;
                    }

                    ctrlStateLeft_lastPacketNum = ctrlStateLeft.unPacketNum;
                }

                // handle right controller inputs
                //--------------------------------------------------------------
                ctrlStateOk = vrSystem.GetControllerState(ctrlIndexRight, ref ctrlStateRight);
                if (ctrlStateOk && ctrlStateRight.unPacketNum != ctrlStateRight_lastPacketNum)
                {
                    // control pitch and roll by touching the touchpad (no need to click)
                    if ((ctrlStateRight.ulButtonTouched & CONTROLLER_BUTTON_MASK_TOUCHPAD) > 0)
                    {
                        float pitchCmd = -ctrlStateRight.rAxis0.y;
                        float rollCmd  = ctrlStateRight.rAxis0.x;
                        s.pitch = pitchCmd;
                        s.roll  = rollCmd;
                    }

                    // brake with the trigger

                    /*if ((ctrlStateRight.ulButtonPressed & CONTROLLER_BUTTON_MASK_TRIGGER) > 0)
                     * {
                     *  FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, true);
                     * }
                     * else
                     * {
                     *  FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, false);
                     * }*/

                    ctrlStateRight_lastPacketNum = ctrlStateRight.unPacketNum;
                }
            }

            // feed inputs to vessel
            FlightInputHandler.state = s;
        }
Exemplo n.º 3
0
        private static void GetConrollerState(CVRSystem system, uint index, out VRControllerState_t ctrlState)
        {
            ctrlState = default(VRControllerState_t);

            if (system != null)
            {
#if VIU_STEAMVR_1_2_0_OR_NEWER
                system.GetControllerState(index, ref ctrlState, s_sizeOfControllerStats);
#else
                system.GetControllerState(index, ref ctrlState);
#endif
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Updates the devices.
        /// </summary>
        public override void UpdateDevices()
        {
            // Get indexes of left and right controllers
            _leftControllerIndex  = -1;
            _rightControllerIndex = -1;
            int lhIndex = (int)_vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            int rhIndex = (int)_vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);

            _controllers.Clear();
            _trackingReferences.Clear();

            // Update all tracked devices
            for (uint i = 0; i < OVR.k_unMaxTrackedDeviceCount; i++)
            {
                TrackedDevicePose_t pose = _devicePoses[i];

                // We are interested in valid and connected devices only
                if (pose.bDeviceIsConnected && pose.bPoseIsValid)
                {
                    ToVRPose(pose, out Vector3 posePosition, out Quaternion poseOrientation);
                    ETrackedDeviceClass c = _vrSystem.GetTrackedDeviceClass(i);

                    // Update controller
                    if (c == ETrackedDeviceClass.Controller)
                    {
                        VRControllerState_t state_t = default(VRControllerState_t);
                        if (_vrSystem.GetControllerState(i, ref state_t, (uint)System.Runtime.InteropServices.Marshal.SizeOf(state_t)))
                        {
                            VRControllerRole role;
                            if (i == lhIndex)
                            {
                                role = VRControllerRole.LeftHand;
                                _leftControllerIndex = _controllers.Count;
                            }
                            else if (i == rhIndex)
                            {
                                role = VRControllerRole.RightHand;
                                _rightControllerIndex = _controllers.Count;
                            }
                            else
                            {
                                role = VRControllerRole.Undefined;
                            }

                            VRControllerState state = new VRControllerState();
                            state.Update(role, ref state_t, posePosition, poseOrientation);
                            _controllers.Add(state);
                        }
                    }
                    // Update generic reference (base station etc...)
                    else if (c == ETrackedDeviceClass.TrackingReference)
                    {
                        VRTrackingReference reference = new VRTrackingReference();
                        reference.Update(posePosition, poseOrientation);
                        _trackingReferences.Add(reference);
                    }
                }
            }
        }
 private bool TryGetComponentState(CVRSystem vrSystem, CVRRenderModels vrRenderModels, string componentName, out RenderModel_ComponentState_t componentState)
 {
     componentState = default(RenderModel_ComponentState_t);
     var modeState = default(RenderModel_ControllerMode_State_t);
     var controllerState = default(VRControllerState_t);
     if (!vrSystem.GetControllerState(m_deviceIndex, ref controllerState, s_sizeOfControllerStats)) { return false; }
     if (!vrRenderModels.GetComponentState(loadedModelName, componentName, ref controllerState, ref modeState, ref componentState)) { return false; }
     return true;
 }
Exemplo n.º 6
0
        private void TrackingLoop()
        {
            try
            {
                EVRInitError initError = EVRInitError.None;
                CVRSystem    cvrSystem = OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Utility);
                var          chaperone = OpenVR.Chaperone;
                var          quadArea  = new HmdQuad_t();
                chaperone.GetPlayAreaRect(ref quadArea);
                _playArea = quadArea.ToPlayArea();
                if (initError != EVRInitError.None)
                {
                    throw new InvalidOperationException($"EVR init erro: {initError}");
                }
                while (_keepReading)
                {
                    TrackedDevicePose_t[] trackedDevicePoses = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
                    cvrSystem.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, 0f, trackedDevicePoses);
                    for (uint trackedDeviceIndex = 0; trackedDeviceIndex < OpenVR.k_unMaxTrackedDeviceCount; trackedDeviceIndex++)
                    {
                        if (cvrSystem.IsTrackedDeviceConnected(trackedDeviceIndex))
                        {
                            ETrackedDeviceClass deviceClass = cvrSystem.GetTrackedDeviceClass(trackedDeviceIndex);
                            if (true)
                            {
                                VRControllerState_t controllerState = new VRControllerState_t();
                                cvrSystem.GetControllerState(1, ref controllerState, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t)));
                                ETrackingResult trackingResult = trackedDevicePoses[trackedDeviceIndex].eTrackingResult;

                                bool trigger    = controllerState.rAxis1.x > 0.9f;
                                bool menuButton = (controllerState.ulButtonPressed & (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu)) != 0;
                                bool gripButton = (controllerState.ulButtonPressed & (1ul << (int)EVRButtonId.k_EButton_Grip)) != 0;

                                if (trackingResult == ETrackingResult.Running_OK)
                                {
                                    HmdMatrix34_t trackingMatrix = trackedDevicePoses[trackedDeviceIndex].mDeviceToAbsoluteTracking;

                                    Vector3            speedVector    = trackedDevicePoses[trackedDeviceIndex].vVelocity.ToVelocityVector();
                                    Vector3            position       = trackingMatrix.ToPositionVector();
                                    DeviceTrackingData trackingUpdate = new DeviceTrackingData((int)trackedDeviceIndex, deviceClass.ToString(), position, trackingMatrix.ToRotationQuaternion());
                                    NewPoseUpdate?.Invoke(this, trackingUpdate);
                                }
                            }
                        }
                    }
                    Thread.Sleep(UpdatedInterval);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error: {e}");
            }
            finally
            {
                OpenVR.Shutdown();
            }
        }
Exemplo n.º 7
0
        private static void SteamVr()
        {
            Console.WriteLine("Starting");
            EVRInitError initError = EVRInitError.None;
            CVRSystem    cvrSystem = OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Utility);

            Console.WriteLine("Error: " + initError.ToString());
            if (cvrSystem == null)
            {
                Console.WriteLine("Error!");
            }
            while (true)
            {
                Thread.Sleep(1);
                TrackedDevicePose_t[] trackedDevicePose = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
                cvrSystem.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseRawAndUncalibrated, 0f, trackedDevicePose);
                VRControllerState_t controllerState = new VRControllerState_t();
                cvrSystem.GetControllerState(1, ref controllerState,
                                             (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t)));
                int  trigger   = controllerState.rAxis1.x > 0.9f ? 1 : 0;
                bool topButtom = (controllerState.ulButtonPressed & (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu)) != 0;

                TrackedDevicePose_t pose           = trackedDevicePose[1];
                ETrackingResult     trackingResult = pose.eTrackingResult;
                HmdMatrix34_t       hmdMatrix      = pose.mDeviceToAbsoluteTracking;
                Position            pos            = new Position(hmdMatrix);
                Rotation            rot            = new Rotation(hmdMatrix);
                Console.WriteLine($"Position: {pos} Rotation: {rot} trigger {trigger} app {topButtom}");
                foreach (Socket client in _clients.ToArray())
                {
                    try
                    {
                        client.Send(Encoding.ASCII.GetBytes($"S{pos.ToData()} {rot.ToData()} {trigger} {(topButtom ? 1 : 0)}E"));
                    }
                    catch (Exception)
                    {
                        _clients.Remove(client);
                    }
                }
            }
            for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
            {
                if (cvrSystem?.IsTrackedDeviceConnected((uint)i) ?? false)
                {
                    ETrackedDeviceClass deviceClass = cvrSystem.GetTrackedDeviceClass((uint)i);
                    Console.WriteLine($"index: {i} is {deviceClass}");
                }
            }
            Console.ReadLine();
            OpenVR.Shutdown();
            Console.WriteLine("Shut down");
            Console.ReadLine();
        }
Exemplo n.º 8
0
        public bool GetControllerState(uint index, out VRControllerState_t state)
        {
            state = new VRControllerState_t();

            //有効なデバイスか
            if (!IsDeviceValid(index))
            {
                return(false);
            }

            uint size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t));

            return(openvr.GetControllerState(index, ref state, size));
        }
Exemplo n.º 9
0
        public static VRControllerState_t ControllerState(this CVRSystem hmd, int contIndex)
        {
            if (hmd == null)
            {
                throw new ArgumentNullException(nameof(hmd));
            }
            if (contIndex < 0 || contIndex > OpenVR.k_unMaxTrackedDeviceCount)
            {
                throw new ArgumentOutOfRangeException(nameof(contIndex));
            }

            VRControllerState_t state = new VRControllerState_t();
            bool success = hmd.GetControllerState((uint)contIndex, ref state);

            if (!success)
            {
                throw new InvalidOperationException("Unable to retrieve controller state");
            }
            return(state);
        }
Exemplo n.º 10
0
    void handleStateChanges()
    {
        bool stateIsValid = vrSystem.GetControllerState((uint)index, ref state,
                                                        (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t)));


        if (!stateIsValid)
        {
            Debug.Log("Invalid State for Idx: " + index);
        }

        if (stateIsValid && state.GetHashCode() != prevState.GetHashCode())
        {
            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) != 0 &&
                (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) == 0)
            {
                if (activeBeamInterceptObj == null)
                {
                    return;
                }
                activeBeamInterceptObj.grab(this);
            }
            else if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) == 0 &&
                     (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) != 0)
            {
                if (activeBeamInterceptObj == null)
                {
                    return;
                }
                activeBeamInterceptObj.release(this);
                activeBeamInterceptObj = null;
            }

            prevState = state;
        }
    }
Exemplo n.º 11
0
        /// <summary>
        /// Updates the devices.
        /// </summary>
        private void UpdateDevices()
        {
            // Cache HMD
            CVRSystem hmd = Hmd;

            // Get indexes of left and right controllers
            LeftControllerIndex  = -1;
            RightControllerIndex = -1;
            int lhIndex = (int)hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            int rhIndex = (int)hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);

            controllers.Clear();

            // Update all tracked devices
            for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
            {
                TrackedDevicePose_t pose = renderPoses[i];

                // We are interested in valid and connected devices only
                if (pose.bDeviceIsConnected && pose.bPoseIsValid)
                {
                    pose.ToVRPose(out poses[i]);
                    ETrackedDeviceClass c = hmd.GetTrackedDeviceClass(i);

                    // Update controller
                    if (c == ETrackedDeviceClass.Controller)
                    {
                        VRControllerState_t state_t = default(VRControllerState_t);
                        if (hmd.GetControllerState(i, ref state_t, (uint)Marshal.SizeOf(state_t)))
                        {
                            VRControllerRole role;
                            if (i == lhIndex)
                            {
                                role = VRControllerRole.LeftHand;
                                LeftControllerIndex = controllers.Count;
                            }
                            else if (i == rhIndex)
                            {
                                role = VRControllerRole.RightHand;
                                RightControllerIndex = controllers.Count;
                            }
                            else
                            {
                                role = VRControllerRole.Undefined;
                            }

                            VRControllerState state = new VRControllerState();
                            state.Update(role, ref state_t, ref poses[i]);
                            controllers.Add(state);
                        }
                    }
                    // Update generic reference (base station etc...)
                    else if (c == ETrackedDeviceClass.TrackingReference)
                    {
                        VRTrackingReference reference = new VRTrackingReference();
                        reference.Update(poses[i]);
                        trackingReferences.Add(reference);
                    }
                }
            }
            // Convert to array
            trackingReferencesArray = trackingReferences.ToArray();
            controllersArray        = controllers.ToArray();
        }
Exemplo n.º 12
0
 public static bool GetControllerState(this CVRSystem system, uint unControllerDeviceIndex, out VRControllerState_t pControllerState)
 {
     pControllerState = default(VRControllerState_t);
     return(system.GetControllerState(unControllerDeviceIndex, ref pControllerState, CONTROLLER_STATE_SIZE));
 }
Exemplo n.º 13
0
    void handleStateChanges()
    {
        bool stateIsValid = vrSystem.GetControllerState((uint)index, ref state);

        if (!stateIsValid)
        {
            Debug.Log("Invalid State for Idx: " + index);
        }

        if (stateIsValid && state.GetHashCode() != prevState.GetHashCode())
        {
            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.ApplicationMenu) != 0 &&
                (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.ApplicationMenu) == 0)
            {
                toggleMenu();
            }


            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) != 0 &&
                (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) == 0)
            {
                // just pulled the trigger
                castBeamAnyway = true;
                triggerPulled  = true;

                if (currNodeCollided != null)
                {
                    updateNodeCollidedPosition = true;
                    currNodeCollided.GetComponentInChildren <Collider>().enabled = false;
                    NodeInfo info = fDirScript.getNodeInfo(currNodeCollided.name);
                    info.positionIsStationary = true;
                }
            }
            else if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) == 0 &&
                     (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) != 0)
            {
                // just released the trigger
                castBeamAnyway             = false;
                updateSlider               = false;
                updateNodeSelectedPosition = false;
                triggerPulled              = false;

                if (currNodeSelected != null)
                {
                    NodeInfo info = fDirScript.getNodeInfo(currNodeSelected.name);
                    if (info != null)
                    {
                        info.positionIsStationary = false;
                        info.interState           = NodeInteractionState.NONE;
                        fDirScript.numHighlighed--;
                    }

                    currNodeSelected = null;
                }

                if (currNodeCollided != null)
                {
                    updateNodeCollidedPosition = false;
                    currNodeCollided.GetComponentInChildren <Collider>().enabled = true;
                    NodeInfo info = fDirScript.getNodeInfo(currNodeCollided.name);
                    info.positionIsStationary = false;
                }
            }

            if (prevState.rAxis1.x < 1.0f && state.rAxis1.x == 1.0f)
            {
                // just pulled the trigger in all the way
                if (currNodeCollided != null)
                {
                    NodeInfo info = fDirScript.getNodeInfo(currNodeCollided.name);
                    if (info.prevInterState == NodeInteractionState.SELECTED)
                    {
                        info.prevInterState = NodeInteractionState.NONE;
                    }
                    else
                    {
                        info.prevInterState = NodeInteractionState.SELECTED;
                    }
                }
                else if (currMenuSubObject != null)
                {
                    if (currMenuSubObject.name.Equals("ForceBox"))
                    {
                        menuManager.toggleForce();
                    }
                    else if (currMenuSubObject.name.Equals("ShowLinesBox"))
                    {
                        menuManager.toggleShowLines();
                    }
                    else if (currMenuSubObject.name.Equals("NodePointerBox"))
                    {
                        menuManager.toggleNodePointers();
                    }
                }
            }


            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) != 0 &&
                (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) == 0)
            {
                fDirScript.grabSphereWithObject(gameObject);
            }
            else if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) == 0 &&
                     (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) != 0)
            {
                fDirScript.releaseSphereWithObject(gameObject);
            }



            prevState = state;
        }


        if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Touchpad) != 0)
        {
            float h = state.rAxis0.x;
            float v = state.rAxis0.y;

            if (Mathf.Abs(h) > Mathf.Abs(v))
            {
                fDirScript.rotateGraphHorizontal(h);
            }
            else
            {
                fDirScript.rotateGraphVertical(v);
            }
        }
    }
Exemplo n.º 14
0
    void updateControllerStates()
    {
        bool stateIsValid = vrSystem.GetControllerState((uint)index, ref state);

        if (stateIsValid && state.GetHashCode() != prevState.GetHashCode())
        {
            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.ApplicationMenu) != 0 &&
                (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.ApplicationMenu) == 0)
            {
                // hit the menu button
                if (hasController)
                {
                    releaseController();
                }
                else
                {
                    hasController = true;
                    otherControllerScript.releaseController();
                    timelineGameObject.SetActive(true);
                    timelineGameObject.transform.SetParent(gameObject.transform);
                    timelineGameObject.transform.localPosition = new Vector3(0.0f, 0.0f, 0.1f);
                    timelineGameObject.transform.localRotation = Quaternion.Euler(90.0f, 0.0f, 0.0f);
                }
            }


            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) != 0 &&
                (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) == 0)
            {
                // hit the grip button
                gripPressed = true;

                if (otherControllerScript.isGripPressed())
                {
                    accordianManager.deactivate();
                    scaleTimeline = true;
                    currSliderScript.startScale(transform.position, otherController.transform.position);
                }
                else
                {
                    Vector3 dir = transform.forward - transform.up;
                    dir.y = 0.0f;
                    dir.Normalize();
                    Vector3 pos = transform.position;

                    // shoot a ray and see if the controller is pointed at the timeline

                    RaycastHit hitInfo;

                    // try for timeline slider
                    if (Physics.Raycast(deviceRay.origin, deviceRay.direction, out hitInfo, 30.0f, sliderMask))
                    {
                        accordianManager.setAccordianType(AccordianActionType.TIMELINE);
                    }
                    else
                    {
                        accordianManager.setAccordianType(AccordianActionType.SUBNETS);
                    }

                    accordianManager.activate(pos, dir);
                }
            }

            else if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) == 0 &&
                     (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) != 0)
            {
                gripPressed = false;
                currSliderScript.endScale();
                scaleTimeline = false;
                accordianManager.deactivate();
            }



            // trigger is being pressed
            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Trigger) != 0)
            {
                // trigger completely pressed
                if (prevState.rAxis1.x < 1.0f && state.rAxis1.x >= 1.0f)
                {
                    handleTriggerClick();
                }
                // trigger released from completely pressed
                else if (state.rAxis1.x < 1.0f && prevState.rAxis1.x >= 1.0f)
                {
                    handleTriggerUnclick();
                }
            }

            if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Touchpad) != 0 &&
                (prevState.ulButtonPressed & SteamVR_Controller.ButtonMask.Touchpad) == 0)
            {
                if (Mathf.Abs(state.rAxis0.y) >= Mathf.Abs(state.rAxis0.x) && state.rAxis0.y < 0.0f)
                {
                    togglePlayAnimation();
                    otherControllerScript.togglePlayAnimation();
                }
                else
                {
                    int currTime = sqlConnClass.getTimeSliceIdx();
                    if (state.rAxis0.x < 0.0f)
                    {
                        sqlConnClass.setTimeSlice(currTime - 1);
                    }
                    else if (state.rAxis0.x > 0.0f)
                    {
                        sqlConnClass.setTimeSlice(currTime + 1);
                    }

                    float currPos = sqlConnClass.getTimeSliceFloat();
                    currSliderScript.updateSliderPosition(currPos);
                }
            }


            prevState = state;
        }


        if ((state.ulButtonPressed & SteamVR_Controller.ButtonMask.Grip) != 0)
        {
            accordianManager.tryToAdjustDistance(transform.position);
        }
    }
Exemplo n.º 15
0
    protected virtual void Update()
    {
        CVRSystem system = OpenVR.System;

        if (system != null && system.GetControllerState(this.controllerIndex, ref this.controllerState, (uint)Marshal.SizeOf(typeof(VRControllerState_t))))
        {
            ulong num = this.controllerState.ulButtonPressed & 8589934592UL;
            if (num > 0UL && !this.triggerPressed)
            {
                this.triggerPressed = true;
                ClickedEventArgs e;
                e.controllerIndex = this.controllerIndex;
                e.flags           = (uint)this.controllerState.ulButtonPressed;
                e.padX            = this.controllerState.rAxis0.x;
                e.padY            = this.controllerState.rAxis0.y;
                this.OnTriggerClicked(e);
            }
            else if (num == 0UL && this.triggerPressed)
            {
                this.triggerPressed = false;
                ClickedEventArgs e2;
                e2.controllerIndex = this.controllerIndex;
                e2.flags           = (uint)this.controllerState.ulButtonPressed;
                e2.padX            = this.controllerState.rAxis0.x;
                e2.padY            = this.controllerState.rAxis0.y;
                this.OnTriggerUnclicked(e2);
            }
            ulong num2 = this.controllerState.ulButtonPressed & 4UL;
            if (num2 > 0UL && !this.gripped)
            {
                this.gripped = true;
                ClickedEventArgs e3;
                e3.controllerIndex = this.controllerIndex;
                e3.flags           = (uint)this.controllerState.ulButtonPressed;
                e3.padX            = this.controllerState.rAxis0.x;
                e3.padY            = this.controllerState.rAxis0.y;
                this.OnGripped(e3);
            }
            else if (num2 == 0UL && this.gripped)
            {
                this.gripped = false;
                ClickedEventArgs e4;
                e4.controllerIndex = this.controllerIndex;
                e4.flags           = (uint)this.controllerState.ulButtonPressed;
                e4.padX            = this.controllerState.rAxis0.x;
                e4.padY            = this.controllerState.rAxis0.y;
                this.OnUngripped(e4);
            }
            ulong num3 = this.controllerState.ulButtonPressed & 4294967296UL;
            if (num3 > 0UL && !this.padPressed)
            {
                this.padPressed = true;
                ClickedEventArgs e5;
                e5.controllerIndex = this.controllerIndex;
                e5.flags           = (uint)this.controllerState.ulButtonPressed;
                e5.padX            = this.controllerState.rAxis0.x;
                e5.padY            = this.controllerState.rAxis0.y;
                this.OnPadClicked(e5);
            }
            else if (num3 == 0UL && this.padPressed)
            {
                this.padPressed = false;
                ClickedEventArgs e6;
                e6.controllerIndex = this.controllerIndex;
                e6.flags           = (uint)this.controllerState.ulButtonPressed;
                e6.padX            = this.controllerState.rAxis0.x;
                e6.padY            = this.controllerState.rAxis0.y;
                this.OnPadUnclicked(e6);
            }
            ulong num4 = this.controllerState.ulButtonPressed & 2UL;
            if (num4 > 0UL && !this.menuPressed)
            {
                this.menuPressed = true;
                ClickedEventArgs e7;
                e7.controllerIndex = this.controllerIndex;
                e7.flags           = (uint)this.controllerState.ulButtonPressed;
                e7.padX            = this.controllerState.rAxis0.x;
                e7.padY            = this.controllerState.rAxis0.y;
                this.OnMenuClicked(e7);
            }
            else if (num4 == 0UL && this.menuPressed)
            {
                this.menuPressed = false;
                ClickedEventArgs e8;
                e8.controllerIndex = this.controllerIndex;
                e8.flags           = (uint)this.controllerState.ulButtonPressed;
                e8.padX            = this.controllerState.rAxis0.x;
                e8.padY            = this.controllerState.rAxis0.y;
                this.OnMenuUnclicked(e8);
            }
            num3 = (this.controllerState.ulButtonTouched & 4294967296UL);
            if (num3 > 0UL && !this.padTouched)
            {
                this.padTouched = true;
                ClickedEventArgs e9;
                e9.controllerIndex = this.controllerIndex;
                e9.flags           = (uint)this.controllerState.ulButtonPressed;
                e9.padX            = this.controllerState.rAxis0.x;
                e9.padY            = this.controllerState.rAxis0.y;
                this.OnPadTouched(e9);
            }
            else if (num3 == 0UL && this.padTouched)
            {
                this.padTouched = false;
                ClickedEventArgs e10;
                e10.controllerIndex = this.controllerIndex;
                e10.flags           = (uint)this.controllerState.ulButtonPressed;
                e10.padX            = this.controllerState.rAxis0.x;
                e10.padY            = this.controllerState.rAxis0.y;
                this.OnPadUntouched(e10);
            }
        }
    }
Exemplo n.º 16
0
        private static void UpdateDevices(CVRSystem system, ref uint overlayIndex)
        {
            m_Lock.EnterWriteLock();
            try
            {
                m_Devices.Clear();
            }
            finally
            {
                m_Lock.ExitWriteLock();
            }
            var sb    = new StringBuilder(256);
            var state = new VRControllerState_t();

            for (var i = 0u; i < OpenVR.k_unMaxTrackedDeviceCount; ++i)
            {
                var devClass = system.GetTrackedDeviceClass(i);
                if (devClass == ETrackedDeviceClass.Controller ||
                    devClass == ETrackedDeviceClass.GenericTracker ||
                    devClass == ETrackedDeviceClass.TrackingReference)
                {
                    var err = ETrackedPropertyError.TrackedProp_Success;
                    var batteryPercentage = system.GetFloatTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref err);
                    if (err != ETrackedPropertyError.TrackedProp_Success)
                    {
                        batteryPercentage = 1f;
                    }
                    sb.Clear();
                    system.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_TrackingSystemName_String, sb, (uint)sb.Capacity, ref err);
                    var isOculus = sb.ToString().IndexOf("oculus", StringComparison.OrdinalIgnoreCase) >= 0;
                    // Oculus : B/Y, Bit 1, Mask 2
                    // Oculus : A/X, Bit 7, Mask 128
                    // Vive : Menu, Bit 1, Mask 2,
                    // Vive : Grip, Bit 2, Mask 4
                    var role = system.GetControllerRoleForTrackedDeviceIndex(i);
                    if (role == ETrackedControllerRole.LeftHand ||
                        role == ETrackedControllerRole.RightHand)
                    {
                        if (system.GetControllerState(i, ref state, (uint)Marshal.SizeOf(state)) &&
                            (state.ulButtonPressed & (isOculus ? 2u : 4u)) != 0)
                        {
                            if (role == ETrackedControllerRole.LeftHand)
                            {
                                Array.Copy(m_L_Translation, m_Translation, 3);
                                Array.Copy(m_L_Rotation, m_Rotation, 3);
                            }
                            else
                            {
                                Array.Copy(m_R_Translation, m_Translation, 3);
                                Array.Copy(m_R_Rotation, m_Rotation, 3);
                            }
                            overlayIndex = i;
                        }
                    }
                    var type = string.Empty;
                    if (devClass == ETrackedDeviceClass.Controller)
                    {
                        if (role == ETrackedControllerRole.LeftHand)
                        {
                            type = "leftController";
                        }
                        else if (role == ETrackedControllerRole.RightHand)
                        {
                            type = "rightController";
                        }
                        else
                        {
                            type = "controller";
                        }
                    }
                    else if (devClass == ETrackedDeviceClass.GenericTracker)
                    {
                        type = "tracker";
                    }
                    else if (devClass == ETrackedDeviceClass.TrackingReference)
                    {
                        type = "base";
                    }
                    var item = new[]
                    {
                        type,
                        system.IsTrackedDeviceConnected(i)
                            ? "connected"
                            : "disconnected",
                        (batteryPercentage * 100).ToString()
                    };
                    m_Lock.EnterWriteLock();
                    try
                    {
                        m_Devices.Add(item);
                    }
                    finally
                    {
                        m_Lock.ExitWriteLock();
                    }
                }
            }
        }
Exemplo n.º 17
0
    // Update is called once per frame
    void Update()
    {
        CVRSystem system = OpenVR.System;

        //leftHand
        if (null != system && system.GetControllerState((uint)leftHand.index, ref leftControllerState))
        {
            SteamVR_Controller.Device device = SteamVR_Controller.Input((int)(uint)leftHand.index);
            leftdevice = device;
            if (device.GetPress(EVRButtonId.k_EButton_Grip))
            {
                //root.transform.RotateAround(new Vector3(0, 1, 0), -Time.deltaTime* 0.5f);
//                print("leftHand: k_EButton_Grip has been touched!");
                //draw the line
                GetComponent <LineRenderer>().SetPosition(0, leftrayPos.transform.position);


                RaycastHit  hitInfo;
                Highlighter highlighter;
                if (Physics.Raycast(leftrayPos.transform.position, leftrayPos.transform.forward, out hitInfo, Mathf.Infinity))
                {
                    processHighlightObj.SendMessage("ProcessHiglight", hitInfo);
                    GetComponent <LineRenderer> ().SetPosition(1, hitInfo.point);
                }
                else
                {
                    GetComponent <LineRenderer> ().SetPosition(1, leftrayPos.transform.forward * 1000 + leftrayPos.transform.position);
                }
            }
            if (device.GetPressUp(EVRButtonId.k_EButton_Grip))
            {
//                print("rightHand: k_EButton_Grip has been touched!");
                //                root.transform.RotateAround(new Vector3(0, 1, 0), Time.deltaTime*0.5f);
                GetComponent <LineRenderer>().SetPosition(0, Vector3.zero);
                GetComponent <LineRenderer> ().SetPosition(1, Vector3.zero);
            }
            if (device.GetTouchDown(EVRButtonId.k_EButton_ApplicationMenu))
            {
//                print("leftHand: k_EButton_ApplicationMenu has been touched!");
                UnityEngine.VR.VRSettings.enabled = false;
                Application.LoadLevel("ShangFei");
            }
            if (device.GetPress(EVRButtonId.k_EButton_SteamVR_Touchpad))
            {
                // OnPadButtonPressDown(px, py);
                OnPadButtonPressDownNew(device.GetAxis());
            }
        }

        //righthand
        if (null != system && system.GetControllerState((uint)rightHand.index, ref rightControllerState))
        {
            SteamVR_Controller.Device device = SteamVR_Controller.Input((int)(uint)rightHand.index);
            rightdevice = device;
            if (device.GetPress(EVRButtonId.k_EButton_Grip))
            {
                // draw the line startend
                GetComponent <LineRenderer>().SetPosition(0, rightrayPos.transform.position);

                RaycastHit  hitInfo;
                Highlighter highlighter;
                if (Physics.Raycast(rightrayPos.transform.position, rightrayPos.transform.forward, out hitInfo, Mathf.Infinity))
                {
                    processHighlightObj.SendMessage("ProcessHiglight", hitInfo);
                    GetComponent <LineRenderer> ().SetPosition(1, hitInfo.point);
                }
                else
                {
                    GetComponent <LineRenderer> ().SetPosition(1, rightrayPos.transform.forward * 1000 + rightrayPos.transform.position);
                }
//                print("rightHand: k_EButton_Grip has been touched!");
            }
            if (device.GetPressUp(EVRButtonId.k_EButton_Grip))
            {
//                print("rightHand: k_EButton_Grip has been touched!");
                //                root.transform.RotateAround(new Vector3(0, 1, 0), Time.deltaTime*0.5f);
                GetComponent <LineRenderer>().SetPosition(0, Vector3.zero);
                GetComponent <LineRenderer> ().SetPosition(1, Vector3.zero);
            }
            if (device.GetTouchDown(EVRButtonId.k_EButton_ApplicationMenu))
            {
//                print("rightHand: k_EButton_ApplicationMenu has been touched!");
                UnityEngine.VR.VRSettings.enabled = false;
                Application.LoadLevel("ShangFei");
            }
            if (device.GetPress(EVRButtonId.k_EButton_SteamVR_Touchpad))
            {
                // OnPadButtonPressDown(px, py);
                OnPadButtonPressDownNew(device.GetAxis());
            }
        }
    }