Exemplo n.º 1
0
        public bool DoPulse()
        {
            m_vrSystem.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseRawAndUncalibrated, 0f, m_trackedPoses);

            while (m_vrSystem.PollNextEvent(ref m_vrEvent, m_eventSize))
            {
                switch (m_vrEvent.eventType)
                {
                case (uint)EVREventType.VREvent_Quit:
                case (uint)EVREventType.VREvent_RestartRequested:
                case (uint)EVREventType.VREvent_ProcessQuit:
                    m_active = false;
                    break;

                case (uint)EVREventType.VREvent_TrackedDeviceDeactivated:
                {
                    if (m_leftHandController == m_vrEvent.trackedDeviceIndex)
                    {
                        m_leftHandController = OpenVR.k_unTrackedDeviceIndexInvalid;
                    }
                    if (m_rightHandController == m_vrEvent.trackedDeviceIndex)
                    {
                        m_rightHandController = OpenVR.k_unTrackedDeviceIndexInvalid;
                    }
                }
                break;
                }
            }

            if (m_trackedPoses[OpenVR.k_unTrackedDeviceIndex_Hmd].bPoseIsValid)
            {
                GlmSharp.mat4 l_matrix = GlmSharp.mat4.Identity;
                m_trackedPoses[OpenVR.k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.Convert(ref l_matrix);
                m_core.GetControlManager().SetHeadTransform(l_matrix);
            }

            if (m_leftHandController != OpenVR.k_unTrackedDeviceIndexInvalid)
            {
                GlmSharp.mat4 l_matrix = GlmSharp.mat4.Identity;
                m_trackedPoses[m_leftHandController].mDeviceToAbsoluteTracking.Convert(ref l_matrix);
                m_core.GetControlManager().SetHandTransform(ControlManager.Hand.Left, l_matrix);
            }
            else
            {
                m_leftHandController = m_vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            }

            if (m_rightHandController != OpenVR.k_unTrackedDeviceIndexInvalid)
            {
                GlmSharp.mat4 l_matrix = GlmSharp.mat4.Identity;
                m_trackedPoses[m_rightHandController].mDeviceToAbsoluteTracking.Convert(ref l_matrix);
                m_core.GetControlManager().SetHandTransform(ControlManager.Hand.Right, l_matrix);
            }
            else
            {
                m_rightHandController = m_vrSystem.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
            }

            return(m_active);
        }
Exemplo n.º 2
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.º 3
0
        //Copy & Past openvr_api.dll [32bit version - Can be found in Unity folder] into **FilePath/bin/Debug [together with the build *.exe]
        private void GetViveControllerBattery()
        {
            CVRSystem             hmd   = OpenVR.System;
            ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success;
            float leftBat  = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error);
            float rightBat = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error);

            leftBat  = leftBat * 100f;
            rightBat = rightBat * 100f;
            int    leftBatInt     = (int)leftBat;
            int    rightBatInt    = (int)rightBat;
            string leftBatString  = ((int)leftBat).ToString() + " %";
            string rightBatString = ((int)rightBat).ToString() + " %";

            if (leftBatInt == 0)
            {
                leftBatString = "Off";
            }
            if (rightBatInt == 0)
            {
                rightBatString = "Off";
            }
            controllerBattery = "Controller Battery : Left - " + leftBatString + " / Right - " + rightBatString;

            ChangeBatteryImage(leftBatInt, rightBatInt);
        }
    // Token: 0x06005ED5 RID: 24277 RVA: 0x002130CC File Offset: 0x002114CC
    public void Refresh()
    {
        int       i      = 0;
        CVRSystem system = OpenVR.System;

        if (system != null)
        {
            this.leftIndex  = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            this.rightIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
        }
        if (this.leftIndex == 4294967295u && this.rightIndex == 4294967295u)
        {
            uint num = 0u;
            while ((ulong)num < (ulong)((long)this.connected.Length))
            {
                if (i >= this.objects.Length)
                {
                    break;
                }
                if (this.connected[(int)((UIntPtr)num)])
                {
                    this.SetTrackedDeviceIndex(i++, num);
                    if (!this.assignAllBeforeIdentified)
                    {
                        break;
                    }
                }
                num += 1u;
            }
        }
        else
        {
            this.SetTrackedDeviceIndex(i++, ((ulong)this.rightIndex >= (ulong)((long)this.connected.Length) || !this.connected[(int)((UIntPtr)this.rightIndex)]) ? uint.MaxValue : this.rightIndex);
            this.SetTrackedDeviceIndex(i++, ((ulong)this.leftIndex >= (ulong)((long)this.connected.Length) || !this.connected[(int)((UIntPtr)this.leftIndex)]) ? uint.MaxValue : this.leftIndex);
            if (this.leftIndex != 4294967295u && this.rightIndex != 4294967295u)
            {
                uint num2 = 0u;
                while ((ulong)num2 < (ulong)((long)this.connected.Length))
                {
                    if (i >= this.objects.Length)
                    {
                        break;
                    }
                    if (this.connected[(int)((UIntPtr)num2)])
                    {
                        if (num2 != this.leftIndex && num2 != this.rightIndex)
                        {
                            this.SetTrackedDeviceIndex(i++, num2);
                        }
                    }
                    num2 += 1u;
                }
            }
        }
        while (i < this.objects.Length)
        {
            this.SetTrackedDeviceIndex(i++, uint.MaxValue);
        }
    }
Exemplo n.º 5
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);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public void UpdatePosition()
        {
            //回転を生成
            Quaternion quaternion = Quaternion.CreateFromYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z);
            //座標系を変更(右手系と左手系の入れ替え)
            Vector3 position = Position;

            position.Z = -Position.Z;
            //HMD視点位置変換行列に書き込む。
            Matrix4x4 t = Matrix4x4.CreateTranslation(position);
            Matrix4x4 r = Matrix4x4.CreateFromQuaternion(quaternion);
            Matrix4x4 s = Matrix4x4.CreateScale(Scale);

            Matrix4x4 m = s * r * t;
            //鏡像反転
            Vector3 Mirroring = new Vector3(MirrorX ? -1 : 1, MirrorY ? -1 : 1, 1);

            //4x4行列を3x4行列に変換する。
            p.m0 = Mirroring.X * m.M11; p.m1 = Mirroring.Y * m.M21; p.m2 = Mirroring.Z * m.M31; p.m3 = m.M41;
            p.m4 = Mirroring.X * m.M12; p.m5 = Mirroring.Y * m.M22; p.m6 = Mirroring.Z * m.M32; p.m7 = m.M42;
            p.m8 = Mirroring.X * m.M13; p.m9 = Mirroring.Y * m.M23; p.m10 = Mirroring.Z * m.M33; p.m11 = m.M43;


            //回転行列を元に相対位置で表示
            if (DeviceTracking)
            {
                //deviceindexを処理(コントローラーなどはその時その時で変わるため)
                var idx = OpenVR.k_unTrackedDeviceIndex_Hmd;
                switch (DeviceIndex)
                {
                case TrackingDeviceSelect.LeftController:
                    idx = openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
                    break;

                case TrackingDeviceSelect.RightController:
                    idx = openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
                    break;

                default:
                    idx = (uint)DeviceIndex;
                    break;
                }

                //HMDからの相対的な位置にオーバーレイを表示する。
                overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, idx, ref p);
            }
            else
            {
                //空間の絶対位置にオーバーレイを表示する
                if (!Seated)
                {
                    overlay.SetOverlayTransformAbsolute(overlayHandle, ETrackingUniverseOrigin.TrackingUniverseStanding, ref p);
                }
                else
                {
                    overlay.SetOverlayTransformAbsolute(overlayHandle, ETrackingUniverseOrigin.TrackingUniverseSeated, ref p);
                }
            }
        }
Exemplo n.º 7
0
 public uint GetLeftControllerIndex()
 {
     if (!IsReady())
     {
         return(InvalidDeviceIndex);
     }
     return(openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand));
 }
Exemplo n.º 8
0
        public override void Evaluate(int SpreadMax, CVRSystem system)
        {
            VREvent_t evt = default(VREvent_t);

            FEventsOut.SliceCount      = 0;
            FDeviceIndexOut.SliceCount = 0;

            while (system.PollNextEvent(ref evt, FEvtSize))
            {
                var evtType = (EVREventType)evt.eventType;
                FEventsOut.Add(evtType.ToString());
                ProcessEvent(evtType, evt);
            }

            //controller states
            OpenVRController.Update(FFrame++);

            FDeviceIndexOut.SliceCount = 0;
            FDeviceRoleOut.SliceCount  = 0;

            FControllerLeftOut.SliceCount      = 0;
            FControllerRightOut.SliceCount     = 0;
            FControllerLeftRightOut.SliceCount = 0;
            FDevicesOut.SliceCount             = 0;

            var indexLeft  = (int)system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            var indexRight = (int)system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);

            if (indexLeft > 0)
            {
                var c = OpenVRController.Input(indexLeft);
                FControllerLeftOut.Add(c);
                FControllerLeftRightOut.Add(c);
            }

            if (indexRight > 0)
            {
                var c = OpenVRController.Input(indexRight);
                FControllerRightOut.Add(c);
                FControllerLeftRightOut.Add(c);
            }

            //output all in one
            for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
            {
                //if(FOpenVRSystem.GetTrackedDeviceClass((uint)i) != ETrackedDeviceClass.Controller) continue;
                var c = OpenVRController.Input(i);
                if (!c.connected || !c.valid)
                {
                    continue;
                }
                FDevicesOut.Add(c);
                FDeviceRoleOut.Add(system.GetControllerRoleForTrackedDeviceIndex((uint)i));
            }
        }
Exemplo n.º 9
0
    // Token: 0x0600570A RID: 22282 RVA: 0x001DFABC File Offset: 0x001DDEBC
    public override void GenerateHapticEvent(VRCTracking.ID id, float duration, float amplitude, float frequency)
    {
        CVRSystem system = OpenVR.System;

        if (system == null)
        {
            return;
        }
        ETrackedControllerRole unDeviceType = ETrackedControllerRole.Invalid;

        if (id != VRCTracking.ID.HandTracker_LeftWrist)
        {
            if (id == VRCTracking.ID.HandTracker_RightWrist)
            {
                unDeviceType = ETrackedControllerRole.RightHand;
            }
        }
        else
        {
            unDeviceType = ETrackedControllerRole.LeftHand;
        }
        VRCTrackingTouch.HapticWave hapticWave = default(VRCTrackingTouch.HapticWave);
        hapticWave.controllerIndex = system.GetTrackedDeviceIndexForControllerRole(unDeviceType);
        hapticWave.duration        = duration;
        hapticWave.strength        = Mathf.Clamp01(amplitude);
        hapticWave.frequency       = frequency;
        if (this.currentHapticLoop[(int)((UIntPtr)hapticWave.controllerIndex)] != null)
        {
            base.StopCoroutine(this.currentHapticLoop[(int)((UIntPtr)hapticWave.controllerIndex)]);
        }
        this.currentHapticLoop[(int)((UIntPtr)hapticWave.controllerIndex)] = base.StartCoroutine("HapticLoop", hapticWave);
    }
Exemplo n.º 10
0
    // Update is called once per frame
    void Update()
    {
        ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success;
        float leftBat  = 0f;
        float rightBat = 0f;

        if (leftController.activeInHierarchy)
        {
            leftBat = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error);
        }
        if (rightController.activeInHierarchy)
        {
            rightBat = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error);
        }
        int leftBatInt  = (int)(leftBat * 100f);
        int rightBatInt = (int)(rightBat * 100f);

        batText.text = "Left : " + leftBatInt + " % / Right : " + rightBatInt + " %";
    }
Exemplo n.º 11
0
        void AssignControllers(CVRSystem system)
        {
            bool controllerIndexChanged = false;

            if (_rightControllerIndex != system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand))
            {
                _rightControllerIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
                Debug.Log("Right controller index assigned to " + _rightControllerIndex);
                controllerIndexChanged = true;
            }

            if (_leftControllerIndex != system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand))
            {
                _leftControllerIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
                Debug.Log("Left controller index assigned to " + _leftControllerIndex);
                controllerIndexChanged = true;
            }

            if ((OnControllerAssignmentChanged != null) && controllerIndexChanged)
            {
                OnControllerAssignmentChanged();
            }
        }
Exemplo n.º 12
0
        private void RefreshAssignedRoles()
        {
            CVRSystem system = OpenVR.System;

            SteamVR_Controller.Device device          = this.controller;
            uint trackedDeviceIndexForControllerRole  = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            uint trackedDeviceIndexForControllerRole2 = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
            uint num  = (this.controller != null) ? this.controller.index : uint.MaxValue;
            uint num2 = uint.MaxValue;

            if (this.startingHandType == Hand.HandType.Left && num != trackedDeviceIndexForControllerRole)
            {
                num2 = trackedDeviceIndexForControllerRole;
            }
            else if (this.startingHandType == Hand.HandType.Right && num != trackedDeviceIndexForControllerRole2)
            {
                num2 = trackedDeviceIndexForControllerRole2;
            }
            if (num2 == 4294967295u)
            {
                return;
            }
            try
            {
                this.controller = SteamVR_Controller.Input((int)num2);
                if (this.controller != device)
                {
                    if (this.otherHand != null)
                    {
                        this.otherHand.controller = device;
                    }
                }
            }
            catch
            {
            }
        }
        public void TriggerHapticPulse(XRNode node, byte strength)
        {
            if (strength == 0)
            {
                return;
            }

            float     pulseDuration  = Time.smoothDeltaTime * 1000000.0f;
            float     biasedStrength = Plugin.SteamBiasTable[Math.Min(9, (int)strength)];
            float     F      = pulseDuration * biasedStrength;
            CVRSystem system = OpenVR.System;
            ETrackedControllerRole unDeviceType      = (node == XRNode.LeftHand) ? ETrackedControllerRole.LeftHand : ETrackedControllerRole.RightHand;
            uint trackedDeviceIndexForControllerRole = system.GetTrackedDeviceIndexForControllerRole(unDeviceType);

            system.TriggerHapticPulse(trackedDeviceIndexForControllerRole, 0u, (char)F);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Gets the current controller state.
        /// </summary>
        /// <param name="left">Left or right hand controller.</param>
        /// <returns>The controller state.</returns>
        public VRController GetController(bool left)
        {
            VRControllerState_t vrcont = new VRControllerState_t();
            TrackedDevicePose_t vrpose = new TrackedDevicePose_t();

            bool valid = VR.GetControllerStateWithPose(ETrackingUniverseOrigin.TrackingUniverseStanding, VR.GetTrackedDeviceIndexForControllerRole(left ? ETrackedControllerRole.LeftHand : ETrackedControllerRole.RightHand), ref vrcont, ref vrpose);

            if (!valid || !vrpose.bPoseIsValid)
            {
                return(null);
            }
            HmdMatrix34_t tmat = vrpose.mDeviceToAbsoluteTracking;
            Matrix4       resp = new Matrix4(tmat.m0, tmat.m1, tmat.m2, tmat.m3, tmat.m4, tmat.m5, tmat.m6, tmat.m7, tmat.m8, tmat.m9, tmat.m10, tmat.m11, 0, 0, 0, 1);

            resp.Transpose();
            resp = resp.ClearTranslation() * Matrix4.CreateTranslation(resp.ExtractTranslation() * VRScale);
            resp = resp * Matrix4.CreateRotationX((float)(Math.PI * 0.5));
            VRController res = new VRController()
            {
                Position = resp,
                Touched  = (VRButtons)vrcont.ulButtonTouched,
                Pressed  = (VRButtons)vrcont.ulButtonPressed
            };

            res.Axes[0] = new Vector2(vrcont.rAxis0.x, vrcont.rAxis0.y);
            res.Axes[1] = new Vector2(vrcont.rAxis1.x, vrcont.rAxis1.y);
            res.Axes[2] = new Vector2(vrcont.rAxis2.x, vrcont.rAxis2.y);
            res.Axes[3] = new Vector2(vrcont.rAxis3.x, vrcont.rAxis3.y);
            res.Axes[4] = new Vector2(vrcont.rAxis4.x, vrcont.rAxis4.y);
            return(res);
        }
Exemplo n.º 15
0
 public uint GetLeftControllerIndex()
 {
     ReadyCheck();
     return(openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand));
 }
Exemplo n.º 16
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.º 17
0
        private IEnumerator Start()
        {
            this.playerInstance = Player.instance;
            if (!this.playerInstance)
            {
                Debug.LogError("No player instance found in Hand Start()");
            }
            this.overlappingColliders = new Collider[16];
            if (this.noSteamVRFallbackCamera)
            {
                yield break;
            }
            SteamVR vr = SteamVR.instance;

            for (;;)
            {
                yield return(new WaitForSeconds(1f));

                if (this.controller != null)
                {
                    break;
                }
                if (this.startingHandType == Hand.HandType.Left || this.startingHandType == Hand.HandType.Right)
                {
                    CVRSystem system = OpenVR.System;
                    uint      trackedDeviceIndexForControllerRole  = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
                    uint      trackedDeviceIndexForControllerRole2 = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
                    int       num;
                    if (trackedDeviceIndexForControllerRole != 4294967295u)
                    {
                        num = (int)trackedDeviceIndexForControllerRole;
                    }
                    else
                    {
                        num = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost, ETrackedDeviceClass.Controller, 0);
                    }
                    int num2;
                    if (trackedDeviceIndexForControllerRole2 != 4294967295u)
                    {
                        num2 = (int)trackedDeviceIndexForControllerRole2;
                    }
                    else
                    {
                        num2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost, ETrackedDeviceClass.Controller, 0);
                    }
                    if (num != -1 && num2 != -1 && num != num2)
                    {
                        int index  = (this.startingHandType != Hand.HandType.Right) ? num : num2;
                        int index2 = (this.startingHandType != Hand.HandType.Right) ? num2 : num;
                        this.InitController(index);
                        if (this.otherHand)
                        {
                            this.otherHand.InitController(index2);
                        }
                    }
                }
                else
                {
                    int num3 = 0;
                    while ((long)num3 < 64L)
                    {
                        if (vr.hmd.GetTrackedDeviceClass((uint)num3) == ETrackedDeviceClass.Controller)
                        {
                            SteamVR_Controller.Device device = SteamVR_Controller.Input(num3);
                            if (device.valid)
                            {
                                if (!(this.otherHand != null) || this.otherHand.controller == null || num3 != (int)this.otherHand.controller.index)
                                {
                                    this.InitController(num3);
                                }
                            }
                        }
                        num3++;
                    }
                }
            }
            yield break;
        }
Exemplo n.º 18
0
    //位置情報を更新
    private void updatePosition()
    {
#pragma warning disable 0219
        string Tag = "[" + this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(); //クラス名とメソッド名を自動取得
#pragma warning restore 0219

        //RenderTextureが生成されているかチェック
        if (!renderTexture.IsCreated())
        {
            Debug.Log(Tag + "RenderTextureがまだ生成されていない");
            return;
        }

        //回転を生成
        Quaternion quaternion = Quaternion.Euler(Rotation.x, Rotation.y, Rotation.z);
        //座標系を変更(右手系と左手系の入れ替え)
        Vector3 position = Position;
        position.z = -Position.z;
        //HMD視点位置変換行列に書き込む。
        Matrix4x4 m = Matrix4x4.TRS(position, quaternion, Scale);

        //鏡像反転
        Vector3 Mirroring = new Vector3(MirrorX ? -1 : 1, MirrorY ? -1 : 1, 1);

        //4x4行列を3x4行列に変換する。
        p.m0  = Mirroring.x * m.m00;
        p.m1  = Mirroring.y * m.m01;
        p.m2  = Mirroring.z * m.m02;
        p.m3  = m.m03;
        p.m4  = Mirroring.x * m.m10;
        p.m5  = Mirroring.y * m.m11;
        p.m6  = Mirroring.z * m.m12;
        p.m7  = m.m13;
        p.m8  = Mirroring.x * m.m20;
        p.m9  = Mirroring.y * m.m21;
        p.m10 = Mirroring.z * m.m22;
        p.m11 = m.m23;

        //回転行列を元に相対位置で表示
        if (DeviceTracking)
        {
            //deviceindexを処理(コントローラーなどはその時その時で変わるため)
            var idx = OpenVR.k_unTrackedDeviceIndex_Hmd;
            switch (DeviceIndex)
            {
            case TrackingDeviceSelect.LeftController:
                idx = openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
                break;

            case TrackingDeviceSelect.RightController:
                idx = openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
                break;

            default:
                idx = (uint)DeviceIndex;
                break;
            }
            //device情報に変化があったらInspectorに反映
            if (DeviceIndexOld != (int)idx)
            {
                Debug.Log(Tag + "Device Updated");
                UpdateDeviceInfo(idx);
                DeviceIndexOld = (int)idx;
            }

            //HMDからの相対的な位置にオーバーレイを表示する。
            overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, idx, ref p);
        }
        else
        {
            //空間の絶対位置にオーバーレイを表示する
            if (!Seated)
            {
                overlay.SetOverlayTransformAbsolute(overlayHandle, ETrackingUniverseOrigin.TrackingUniverseStanding, ref p);
            }
            else
            {
                overlay.SetOverlayTransformAbsolute(overlayHandle, ETrackingUniverseOrigin.TrackingUniverseSeated, ref p);
            }
        }

        if (ResetSeatedCamera)
        {
            OpenVR.System.ResetSeatedZeroPose();
            ResetSeatedCamera = false;
        }

        //オーバーレイの大きさ設定(幅のみ。高さはテクスチャの比から自動計算される)
        overlay.SetOverlayWidthInMeters(overlayHandle, width);

        //オーバーレイの透明度を設定
        overlay.SetOverlayAlpha(overlayHandle, alpha);

        //マウスカーソルスケールを設定する(これにより表示領域のサイズも決定される)
        try
        {
            HmdVector2_t vecMouseScale = new HmdVector2_t
            {
                v0 = renderTexture.width,
                v1 = renderTexture.height
            };
            overlay.SetOverlayMouseScale(overlayHandle, ref vecMouseScale);
        }
        catch (UnassignedReferenceException e)
        {
            Debug.LogError(Tag + "RenderTextureがセットされていません " + e.ToString());
            ProcessError();
            return;
        }
    }