Ejemplo n.º 1
0
        //---------------アクション設定系-------------------

        //振動を発生させる
        public void TriggerHaptic(string ActionPath, float StartTime, float DurationTime, float Frequency, float Amplitude, string RestrictToDevicePath = "")
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;
            ulong         handle     = GetActionHandle(ActionPath); //無効なハンドルならthrowされる

            //制約デバイス指定されていれば適用
            ulong DeviceHandle = OpenVR.k_ulInvalidInputValueHandle;

            if (RestrictToDevicePath != "")
            {
                DeviceHandle = GetInputSourceHandle(RestrictToDevicePath); //無効なハンドルならthrowされる
            }

            //取得処理
            inputError = vrinput.TriggerHapticVibrationAction(handle, StartTime, DurationTime, Frequency, Amplitude, DeviceHandle);
            if (inputError == EVRInputError.WrongType)
            {
                //姿勢ではない
                throw new ArgumentException(inputError.ToString());
            }
            if (inputError != EVRInputError.None)
            {
                //致命的エラー
                throw new IOException(inputError.ToString());
            }

            return;
        }
Ejemplo n.º 2
0
        //デジタルボタンの状態を取得する(生データ)
        private void GetDigitalActionDataRaw(string ActionPath, out InputDigitalActionData_t data, string RestrictToDevicePath = "")
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;

            data = new InputDigitalActionData_t();

            var   size   = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(InputDigitalActionData_t));
            ulong handle = GetActionHandle(ActionPath); //無効なハンドルならthrowされる

            //制約デバイス指定されていれば適用
            ulong DeviceHandle = OpenVR.k_ulInvalidInputValueHandle;

            if (RestrictToDevicePath != "")
            {
                DeviceHandle = GetInputSourceHandle(RestrictToDevicePath); //無効なハンドルならthrowされる
            }

            //取得処理
            inputError = vrinput.GetDigitalActionData(handle, ref data, size, DeviceHandle);
            if (inputError == EVRInputError.WrongType)
            {
                //デジタルボタンではない
                throw new ArgumentException(inputError.ToString());
            }
            if (inputError != EVRInputError.None)
            {
                //致命的エラー
                throw new IOException(inputError.ToString());
            }

            return;
        }
Ejemplo n.º 3
0
        public static void Initialize()
        {
            string[] enumNames = System.Enum.GetNames(enumType);
            inputSourceHandlesBySource = new Dictionary <SteamVR_Input_Sources, ulong>(new SteamVR_Input_Sources_Comparer());
            inputSourceSourcesByHandle = new Dictionary <ulong, SteamVR_Input_Sources>();

            for (int enumIndex = 0; enumIndex < enumNames.Length; enumIndex++)
            {
                string path = GetPath(enumNames[enumIndex]);

                ulong         handle = 0;
                EVRInputError err    = OpenVR.Input.GetInputSourceHandle(path, ref handle);

                if (err != EVRInputError.None)
                {
                    Debug.LogError("GetInputSourceHandle (" + path + ") error: " + err.ToString());
                }

                if (enumNames[enumIndex] == SteamVR_Input_Sources.Any.ToString()) //todo: temporary hack
                {
                    inputSourceHandlesBySource.Add((SteamVR_Input_Sources)enumIndex, 0);
                    inputSourceSourcesByHandle.Add(0, (SteamVR_Input_Sources)enumIndex);
                }
                else
                {
                    inputSourceHandlesBySource.Add((SteamVR_Input_Sources)enumIndex, handle);
                    inputSourceSourcesByHandle.Add(handle, (SteamVR_Input_Sources)enumIndex);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Trigger the haptics at a certain time for a certain length
        /// </summary>
        /// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
        /// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
        /// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
        /// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
        /// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
        public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude)
        {
            if (SteamVR_Input.isStartupFrame)
            {
                return;
            }

            timeLastExecuted = Time.realtimeSinceStartup;

            EVRInputError err = OpenVR.Input.TriggerHapticVibrationAction(handle, secondsFromNow, durationSeconds,
                                                                          frequency, amplitude, inputSourceHandle);

            //Debug.Log(string.Format("[{5}: haptic] secondsFromNow({0}), durationSeconds({1}), frequency({2}), amplitude({3}), inputSource({4})", secondsFromNow, durationSeconds, frequency, amplitude, inputSource, this.GetShortName()));

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR]</b> TriggerHapticVibrationAction (" + fullPath + ") error: " +
                               err.ToString() + " handle: " + handle.ToString());
            }

            if (onExecute != null)
            {
                onExecute.Invoke(vibrationAction, inputSource, secondsFromNow, durationSeconds, frequency, amplitude);
            }
        }
Ejemplo n.º 5
0
        //----------------アクションシステム----------------------------

        //アクションシステムを初期化
        public bool InitActionSystem(string ActionManifestPath = "")
        {
            EVRInputError inputError = EVRInputError.None;

            //VRシステムが使えるか
            ReadyCheck(); //実行可能な状態かチェック

            //空白ならカレントディレクトリ
            if (ActionManifestPath == "")
            {
                ActionManifestPath = Directory.GetCurrentDirectory() + "\\actions.json";
            }

            //カレントパスのActionManifestを設定(これによりLegacy Modeにならなくなる)
            inputError = vrinput.SetActionManifestPath(ActionManifestPath);

            //エラーが起きたとき(ただしミスマッチエラーは起きうるので無視)
            if (inputError != EVRInputError.None && inputError != EVRInputError.MismatchedActionManifest)
            {
                //Steam VRと通信ができていないので強制終了する
                if (inputError == EVRInputError.IPCError)
                {
                    Debug.LogError("Emergency Stop(DLL Handle Invalid!)");
                    ApplicationQuit();
                }

                //基本ここでエラーが起きた場合は致命的である
                throw new IOException(inputError.ToString());
            }
            return(true);
        }
        /// <summary>
        /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public bool GetPoseAtTimeOffset(SteamVR_Input_Sources inputSource, float secondsFromNow, out Vector3 position, out Quaternion rotation, out Vector3 velocity, out Vector3 angularVelocity)
        {
            EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                if (err == EVRInputError.InvalidHandle)
                {
                    //todo: ignoring this error for now since it throws while the dashboard is up
                }
                else
                {
                    Debug.LogError("GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString()); //todo: this should be an error
                }

                velocity        = Vector3.zero;
                angularVelocity = Vector3.zero;
                position        = Vector3.zero;
                rotation        = Quaternion.identity;
                return(false);
            }

            velocity        = new Vector3(tempPoseActionData.pose.vVelocity.v0, tempPoseActionData.pose.vVelocity.v1, -tempPoseActionData.pose.vVelocity.v2);
            angularVelocity = new Vector3(-tempPoseActionData.pose.vAngularVelocity.v0, -tempPoseActionData.pose.vAngularVelocity.v1, tempPoseActionData.pose.vAngularVelocity.v2);
            position        = SteamVR_Utils.GetPosition(tempPoseActionData.pose.mDeviceToAbsoluteTracking);
            rotation        = SteamVR_Utils.GetRotation(tempPoseActionData.pose.mDeviceToAbsoluteTracking);

            return(true);
        }
Ejemplo n.º 7
0
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public override void UpdateValue(SteamVR_Input_Sources inputSource)
        {
            lastActionData[inputSource] = actionData[inputSource];

            EVRInputError err = OpenVR.Input.GetAnalogActionData(handle, ref tempActionData, actionData_size, SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                Debug.LogError("GetAnalogActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
            }

            active[inputSource]       = tempActionData.bActive;
            activeOrigin[inputSource] = tempActionData.activeOrigin;
            updateTime[inputSource]   = tempActionData.fUpdateTime;
            changed[inputSource]      = false;
            actionData[inputSource]   = tempActionData;

            if (Mathf.Abs(GetAxisDelta(inputSource)) > changeTolerance)
            {
                changed[inputSource]     = true;
                lastChanged[inputSource] = Time.time;

                if (onChange[inputSource] != null)
                {
                    onChange[inputSource].Invoke(this);
                }
            }

            if (onUpdate[inputSource] != null)
            {
                onUpdate[inputSource].Invoke(this);
            }
        }
        /// <summary><strong>[Should not be called by user code]</strong>
        /// Updates the data for this action and this input source. Sends related events.
        /// </summary>
        public override void UpdateValue()
        {
            lastActionData = actionData;
            lastActive     = active;

            EVRInputError err =
                OpenVR.Input.GetDigitalActionData(action.handle, ref actionData, actionData_size, inputSourceHandle);

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR]</b> GetDigitalActionData error (" + action.fullPath + "): " +
                               err.ToString() + " handle: " + action.handle.ToString());
            }

            if (changed)
            {
                changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime;
            }

            updateTime = Time.realtimeSinceStartup;

            if (active)
            {
                if (onStateDown != null && stateDown)
                {
                    onStateDown.Invoke(booleanAction, inputSource);
                }

                if (onStateUp != null && stateUp)
                {
                    onStateUp.Invoke(booleanAction, inputSource);
                }

                if (onState != null && state)
                {
                    onState.Invoke(booleanAction, inputSource);
                }

                if (onChange != null && changed)
                {
                    onChange.Invoke(booleanAction, inputSource, state);
                }

                if (onUpdate != null)
                {
                    onUpdate.Invoke(booleanAction, inputSource, state);
                }
            }

            if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
            {
                onActiveBindingChange.Invoke(booleanAction, inputSource, activeBinding);
            }

            if (onActiveChange != null && lastActive != active)
            {
                onActiveChange.Invoke(booleanAction, inputSource, activeBinding);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes the handle for the action
        /// </summary>
        public virtual void Initialize()
        {
            EVRInputError err = OpenVR.Input.GetActionHandle(fullPath.ToLower(), ref handle);

            if (err != EVRInputError.None)
            {
                Debug.LogError("GetActionHandle (" + fullPath + ") error: " + err.ToString());
            }
            //else Debug.Log("handle: " + handle);
        }
Ejemplo n.º 10
0
        public void Initialize()
        {
            EVRInputError err = OpenVR.Input.GetActionSetHandle(fullPath.ToLower(), ref handle);

            if (err != EVRInputError.None)
            {
                Debug.LogError("GetActionSetHandle (" + fullPath + ") error: " + err.ToString());
            }

            activeActionSetSize = (uint)(Marshal.SizeOf(typeof(VRActiveActionSet_t)));
        }
Ejemplo n.º 11
0
        //ActionセットとActionを指定すると、それが取得できる元のパスのListを返す
        public List <OriginSource> GetActionOriginsList(string ActionSetPath, string ActionPath)
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;
            ulong         sethandle  = GetActionSetHandle(ActionSetPath); //無効なハンドルならthrowされる
            ulong         handle     = GetActionHandle(ActionPath);       //無効なハンドルならthrowされる

            //取得処理
            ulong[] origins = new ulong[1024];
            inputError = vrinput.GetActionOrigins(sethandle, handle, origins);
            if (inputError == EVRInputError.WrongType)
            {
                //姿勢ではない
                throw new ArgumentException(inputError.ToString());
            }
            if (inputError != EVRInputError.None)
            {
                //致命的エラー
                throw new IOException(inputError.ToString());
            }

            List <OriginSource> list = new List <OriginSource>();

            for (int i = 0; i < 1024; i++)
            {
                if (origins[i] == OpenVR.k_ulInvalidInputValueHandle)
                {
                    break;
                }
                OriginSource origin = GetOriginSourceFromInternalHandle(origins[i]);
                if (origin != null)
                {
                    list.Add(origin);
                }
            }

            return(list);
        }
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public virtual void UpdateValue(SteamVR_Input_Sources inputSource, bool skipStateAndEventUpdates)
        {
            changed[inputSource] = false;
            if (skipStateAndEventUpdates == false)
            {
                ResetLastStates(inputSource);
            }

            EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, predictedSecondsFromNow, ref tempPoseActionData, poseActionData_size, SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                Debug.LogError("GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
            }

            poseActionData[inputSource] = tempPoseActionData;
            active[inputSource]         = tempPoseActionData.bActive;
            activeOrigin[inputSource]   = tempPoseActionData.activeOrigin;
            updateTime[inputSource]     = Time.time;

            if (Vector3.Distance(GetLocalPosition(inputSource), GetLastLocalPosition(inputSource)) > changeTolerance)
            {
                changed[inputSource] = true;
            }
            else if (Mathf.Abs(Quaternion.Angle(GetLocalRotation(inputSource), GetLastLocalRotation(inputSource))) > changeTolerance)
            {
                changed[inputSource] = true;
            }

            if (skipStateAndEventUpdates == false)
            {
                CheckAndSendEvents(inputSource);
            }

            if (changed[inputSource])
            {
                lastChanged[inputSource] = Time.time;
            }

            if (onUpdate[inputSource] != null)
            {
                onUpdate[inputSource].Invoke(this);
            }

            if (skipStateAndEventUpdates == false)
            {
                lastRecordedActive[inputSource]         = active[inputSource];
                lastRecordedPoseActionData[inputSource] = poseActionData[inputSource];
            }
        }
Ejemplo n.º 13
0
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public override void UpdateValue(SteamVR_Input_Sources inputSource)
        {
            lastActionData[inputSource] = actionData[inputSource];
            lastActive[inputSource]     = active[inputSource];

            EVRInputError err = OpenVR.Input.GetDigitalActionData(handle, ref tempActionData, actionData_size, SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                Debug.LogError("GetDigitalActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
            }

            actionData[inputSource]   = tempActionData;
            changed[inputSource]      = tempActionData.bChanged;
            active[inputSource]       = tempActionData.bActive;
            activeOrigin[inputSource] = tempActionData.activeOrigin;
            updateTime[inputSource]   = tempActionData.fUpdateTime;

            if (changed[inputSource])
            {
                lastChanged[inputSource] = Time.time;
            }


            if (onStateDown[inputSource] != null && GetStateDown(inputSource))
            {
                onStateDown[inputSource].Invoke(this);
            }

            if (onStateUp[inputSource] != null && GetStateUp(inputSource))
            {
                onStateUp[inputSource].Invoke(this);
            }

            if (onChange[inputSource] != null && GetChanged(inputSource))
            {
                onChange[inputSource].Invoke(this);
            }

            if (onUpdate[inputSource] != null)
            {
                onUpdate[inputSource].Invoke(this);
            }

            if (onActiveChange[inputSource] != null && lastActive[inputSource] != active[inputSource])
            {
                onActiveChange[inputSource].Invoke(this, active[inputSource]);
            }
        }
Ejemplo n.º 14
0
        //アクションを渡すとアクションに関する情報を表示する(Binding UIを開く)
        public void ShowActionBinding(string ActionSetPath, string ActionPath)
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;
            ulong         sethandle  = GetActionSetHandle(ActionSetPath); //無効なハンドルならthrowされる
            ulong         handle     = GetActionHandle(ActionPath);       //無効なハンドルならthrowされる

            inputError = vrinput.ShowActionOrigins(sethandle, handle);
            if (inputError != EVRInputError.None)
            {
                //致命的エラー
                throw new IOException(inputError.ToString());
            }
            return;
        }
        /// <summary>
        /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public bool GetVelocitiesAtTimeOffset(SteamVR_Input_Sources inputSource, float secondsFromNow, out Vector3 velocity, out Vector3 angularVelocity)
        {
            EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                Debug.LogError("GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString()); //todo: this should be an error

                velocity        = Vector3.zero;
                angularVelocity = Vector3.zero;
                return(false);
            }

            velocity        = new Vector3(tempPoseActionData.pose.vVelocity.v0, tempPoseActionData.pose.vVelocity.v1, -tempPoseActionData.pose.vVelocity.v2);
            angularVelocity = new Vector3(-tempPoseActionData.pose.vAngularVelocity.v0, -tempPoseActionData.pose.vAngularVelocity.v1, tempPoseActionData.pose.vAngularVelocity.v2);

            return(true);
        }
        protected void UpdateOriginTrackedDeviceInfo()
        {
            if (lastOriginGetFrame != Time.frameCount) //only get once per frame
            {
                EVRInputError err =
                    OpenVR.Input.GetOriginTrackedDeviceInfo(activeOrigin, ref inputOriginInfo, inputOriginInfo_size);

                if (err != EVRInputError.None)
                {
                    Debug.LogError("<b>[SteamVR]</b> GetOriginTrackedDeviceInfo error (" + fullPath + "): " +
                                   err.ToString() + " handle: " + handle.ToString() + " activeOrigin: " +
                                   activeOrigin.ToString() + " active: " + active);
                }

                lastInputOriginInfo = inputOriginInfo;
                lastOriginGetFrame  = Time.frameCount;
            }
        }
Ejemplo n.º 17
0
        //アクションセットを更新する
        public void UpdateActionSetState(ActiveActionSets ActiveSets)
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;

            //サイズ取得
            var size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRActiveActionSet_t));

            //更新処理実行
            inputError = vrinput.UpdateActionState(ActiveSets.Get(), size);

            //ここでエラーになることはそうそうない
            if (inputError != EVRInputError.None)
            {
                //致命的エラー
                throw new IOException(inputError.ToString());
            }
            return;
        }
Ejemplo n.º 18
0
        //InputSourceを登録してハンドルを格納
        public void RegisterInputSource(string path)
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;
            ulong         handle     = InvalidInputHandle;

            //ハンドルが存在しない場合登録。すでにある場合は無視
            if (!InputSourceHandles.ContainsKey(path))
            {
                inputError = vrinput.GetInputSourceHandle(path, ref handle);
                if (inputError != EVRInputError.None)
                {
                    //だいたいハンドル名が間違っている。いずれにせよ致命的エラー
                    throw new IOException(inputError.ToString());
                }
                InputSourceHandles.Add(path, handle);
            }
            return;
        }
Ejemplo n.º 19
0
        //originを渡すと、そのコントローラのローカライズされた名前が出る(ゲーム内説明用)
        public string GetLocalizedButtonNameFromOriginSource(OriginSource origin)
        {
            ReadyCheck(); //実行可能な状態かチェック

            EVRInputError inputError = EVRInputError.None;

            //取得処理
            StringBuilder s = new StringBuilder();

            s.Length = 8192;

            inputError = vrinput.GetOriginLocalizedName(origin.DeviceInternalHandle, s, 8192);
            if (inputError != EVRInputError.None)
            {
                //致命的エラー
                throw new IOException(inputError.ToString());
            }

            return(s.ToString());
        }
Ejemplo n.º 20
0
        public static void UpdateActionSetsState(bool force = false)
        {
            if (force || Time.frameCount != lastFrameUpdated)
            {
                lastFrameUpdated = Time.frameCount;

                if (activeActionSets != null && activeActionSets.Length > 0)
                {
                    EVRInputError err = OpenVR.Input.UpdateActionState(activeActionSets, activeActionSetSize);
                    if (err != EVRInputError.None)
                    {
                        Debug.LogError("UpdateActionState error: " + err.ToString());
                    }
                    //else Debug.Log("Action sets activated: " + activeActionSets.Length);
                }
                else
                {
                    //Debug.LogWarning("No sets active");
                }
            }
        }
Ejemplo n.º 21
0
        public static void Initialize()
        {
            List <SteamVR_Input_Sources> allSourcesList = new List <SteamVR_Input_Sources>();

            string[] enumNames = System.Enum.GetNames(enumType);
            inputSourceHandlesBySource = new ulong[enumNames.Length];
            inputSourceSourcesByHandle = new Dictionary <ulong, SteamVR_Input_Sources>();

            for (int enumIndex = 0; enumIndex < enumNames.Length; enumIndex++)
            {
                string path = GetPath(enumNames[enumIndex]);

                ulong         handle = 0;
                EVRInputError err    = OpenVR.Input.GetInputSourceHandle(path, ref handle);

                if (err != EVRInputError.None)
                {
                    Debug.LogError("<b>[SteamVR_Standalone]</b> GetInputSourceHandle (" + path + ") error: " + err.ToString());
                }

                if (enumNames[enumIndex] == SteamVR_Input_Sources.Any.ToString()) //todo: temporary hack
                {
                    inputSourceHandlesBySource[enumIndex] = 0;
                    inputSourceSourcesByHandle.Add(0, (SteamVR_Input_Sources)enumIndex);
                }
                else
                {
                    inputSourceHandlesBySource[enumIndex] = handle;
                    inputSourceSourcesByHandle.Add(handle, (SteamVR_Input_Sources)enumIndex);
                }

                allSourcesList.Add((SteamVR_Input_Sources)enumIndex);
            }

            allSources = allSourcesList.ToArray();
        }
        public override void UpdateValue(SteamVR_Input_Sources inputSource, bool skipStateAndEventUpdates)
        {
            if (skipStateAndEventUpdates == false)
            {
                base.ResetLastStates(inputSource);
            }

            base.UpdateValue(inputSource, true);
            bool poseChanged = base.changed[inputSource];

            int inputSourceInt = (int)inputSource;

            if (skipStateAndEventUpdates == false)
            {
                changed[inputSource] = false;

                for (int boneIndex = 0; boneIndex < numBones; boneIndex++)
                {
                    lastBonePositions[inputSourceInt][boneIndex] = bonePositions[inputSourceInt][boneIndex];
                    lastBoneRotations[inputSourceInt][boneIndex] = boneRotations[inputSourceInt][boneIndex];
                }
            }

            EVRInputError err = OpenVR.Input.GetSkeletalActionData(handle, ref tempSkeletonActionData, skeletonActionData_size, SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                // Debug.LogError("GetSkeletalActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
                active[inputSource] = false;
                return;
            }

            active[inputSource]       = active[inputSource] && tempSkeletonActionData.bActive; //anding from the pose active state
            activeOrigin[inputSource] = tempSkeletonActionData.activeOrigin;

            if (active[inputSource])
            {
                err = OpenVR.Input.GetSkeletalBoneData(handle, skeletalTransformSpace[inputSource], rangeOfMotion[inputSource], tempBoneTransforms, SteamVR_Input_Source.GetHandle(inputSource));
                if (err != EVRInputError.None)
                {
                    Debug.LogError("GetSkeletalBoneData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
                }

                for (int boneIndex = 0; boneIndex < tempBoneTransforms.Length; boneIndex++)
                {
                    // SteamVR's coordinate system is right handed, and Unity's is left handed.  The FBX data has its
                    // X axis flipped when Unity imports it, so here we need to flip the X axis as well
                    bonePositions[inputSourceInt][boneIndex].x = -tempBoneTransforms[boneIndex].position.v0;
                    bonePositions[inputSourceInt][boneIndex].y = tempBoneTransforms[boneIndex].position.v1;
                    bonePositions[inputSourceInt][boneIndex].z = tempBoneTransforms[boneIndex].position.v2;

                    boneRotations[inputSourceInt][boneIndex].x = tempBoneTransforms[boneIndex].orientation.x;
                    boneRotations[inputSourceInt][boneIndex].y = -tempBoneTransforms[boneIndex].orientation.y;
                    boneRotations[inputSourceInt][boneIndex].z = -tempBoneTransforms[boneIndex].orientation.z;
                    boneRotations[inputSourceInt][boneIndex].w = tempBoneTransforms[boneIndex].orientation.w;
                }

                // Now that we're in the same handedness as Unity, rotate the root bone around the Y axis
                // so that forward is facing down +Z
                Quaternion qFixUpRot = Quaternion.AngleAxis(Mathf.PI * Mathf.Rad2Deg, Vector3.up);

                boneRotations[inputSourceInt][0] = qFixUpRot * boneRotations[inputSourceInt][0];
            }

            changed[inputSource] = changed[inputSource] || poseChanged;

            if (skipStateAndEventUpdates == false)
            {
                for (int boneIndex = 0; boneIndex < tempBoneTransforms.Length; boneIndex++)
                {
                    if (Vector3.Distance(lastBonePositions[inputSourceInt][boneIndex], bonePositions[inputSourceInt][boneIndex]) > changeTolerance)
                    {
                        changed[inputSource] |= true;
                        break;
                    }

                    if (Mathf.Abs(Quaternion.Angle(lastBoneRotations[inputSourceInt][boneIndex], boneRotations[inputSourceInt][boneIndex])) > changeTolerance)
                    {
                        changed[inputSource] |= true;
                        break;
                    }
                }

                base.CheckAndSendEvents(inputSource);
            }

            if (changed[inputSource])
            {
                lastChanged[inputSource] = Time.time;
            }

            if (skipStateAndEventUpdates == false)
            {
                lastRecordedActive[inputSource]         = active[inputSource];
                lastRecordedPoseActionData[inputSource] = poseActionData[inputSource];
            }
        }
        /// <summary>
        /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
        /// </summary>
        /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
        /// <returns>true if we successfully returned a pose</returns>
        public bool GetPoseAtTimeOffset(float secondsFromNow, out Vector3 positionAtTime, out Quaternion rotationAtTime, out Vector3 velocityAtTime, out Vector3 angularVelocityAtTime)
        {
            EVRInputError err = OpenVR.Input.GetPoseActionDataRelativeToNow(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, inputSourceHandle);

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString()); //todo: this should be an error

                velocityAtTime        = Vector3.zero;
                angularVelocityAtTime = Vector3.zero;
                positionAtTime        = Vector3.zero;
                rotationAtTime        = Quaternion.identity;
                return(false);
            }

            velocityAtTime        = GetUnityCoordinateVelocity(tempPoseActionData.pose.vVelocity);
            angularVelocityAtTime = GetUnityCoordinateAngularVelocity(tempPoseActionData.pose.vAngularVelocity);
            positionAtTime        = tempPoseActionData.pose.mDeviceToAbsoluteTracking.GetPosition();
            rotationAtTime        = tempPoseActionData.pose.mDeviceToAbsoluteTracking.GetRotation();

            return(true);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
        /// </summary>
        /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
        /// <returns>true if we successfully returned a pose</returns>
        public bool GetVelocitiesAtTimeOffset(float secondsFromNow, out Vector3 velocityAtTime, out Vector3 angularVelocityAtTime)
        {
            EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, inputSourceHandle);

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());

                velocityAtTime        = Vector3.zero;
                angularVelocityAtTime = Vector3.zero;
                return(false);
            }

            velocityAtTime        = GetUnityCoordinateVelocity(tempPoseActionData.pose.vVelocity);
            angularVelocityAtTime = GetUnityCoordinateAngularVelocity(tempPoseActionData.pose.vAngularVelocity);

            return(true);
        }
Ejemplo n.º 25
0
        /// <summary><strong>[Should not be called by user code]</strong>
        /// Updates the data for this action and this input source. Sends related events.
        /// </summary>
        public virtual void UpdateValue(bool skipStateAndEventUpdates)
        {
            lastChanged         = changed;
            lastPoseActionData  = poseActionData;
            lastLocalPosition   = localPosition;
            lastLocalRotation   = localRotation;
            lastVelocity        = velocity;
            lastAngularVelocity = angularVelocity;

            EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, predictedSecondsFromNow,
                                                               ref poseActionData, poseActionData_size, inputSourceHandle);

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() +
                               " Handle: " + handle.ToString() + ". Input source: " + inputSource.ToString());
            }

            SetCacheVariables();
            changed = GetChanged();

            if (changed)
            {
                changedTime = updateTime + predictedSecondsFromNow;
            }

            if (skipStateAndEventUpdates == false)
            {
                CheckAndSendEvents();
            }
        }
        public static void UpdateActionStates(bool force = false)
        {
            if (force || Time.frameCount != lastFrameUpdated)
            {
                lastFrameUpdated = Time.frameCount;

                if (changed)
                {
                    UpdateActionSetsArray();
                }

                if (rawActiveActionSetArray != null && rawActiveActionSetArray.Length > 0)
                {
                    EVRInputError err = OpenVR.Input.UpdateActionState(rawActiveActionSetArray, activeActionSetSize);
                    if (err != EVRInputError.None)
                    {
                        Debug.LogError("<b>[SteamVR]</b> UpdateActionState error: " + err.ToString());
                    }
                    //else Debug.Log("Action sets activated: " + activeActionSets.Length);
                }
                else
                {
                    //Debug.LogWarning("No sets active");
                }
            }
        }
        /// <summary><strong>[Should not be called by user code]</strong>
        /// Updates the data for this action and this input source. Sends related events.
        /// </summary>
        public override void UpdateValue()
        {
            lastActionData = actionData;
            lastActive     = active;
            lastAxis       = axis;
            lastDelta      = delta;

            EVRInputError err = OpenVR.Input.GetAnalogActionData(handle, ref actionData, actionData_size, SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR_Standalone]</b> GetAnalogActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
            }

            updateTime = Time.realtimeSinceStartup;
            axis       = new Vector3(actionData.x, actionData.y, actionData.z);
            delta      = new Vector3(actionData.deltaX, actionData.deltaY, actionData.deltaZ);

            changed = false;

            if (active)
            {
                if (delta.magnitude > changeTolerance)
                {
                    changed     = true;
                    changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime; //fUpdateTime is the time from the time the action was called that the action changed

                    if (onChange != null)
                    {
                        onChange.Invoke(vector3Action, inputSource, axis, delta);
                    }
                }

                if (axis != Vector3.zero)
                {
                    if (onAxis != null)
                    {
                        onAxis.Invoke(vector3Action, inputSource, axis, delta);
                    }
                }

                if (onUpdate != null)
                {
                    onUpdate.Invoke(vector3Action, inputSource, axis, delta);
                }
            }


            if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
            {
                onActiveBindingChange.Invoke(vector3Action, inputSource, activeBinding);
            }

            if (onActiveChange != null && lastActive != active)
            {
                onActiveChange.Invoke(vector3Action, inputSource, activeBinding);
            }
        }
        /// <summary><strong>[Should not be called by user code]</strong>
        /// Updates the data for this action and this input source. Sends related events.
        /// </summary>
        public override void UpdateValue()
        {
            lastActionData = actionData;
            lastActive     = active;

            EVRInputError err = OpenVR.Input.GetAnalogActionData(handle, ref actionData, actionData_size,
                                                                 SteamVR_Input_Source.GetHandle(inputSource));

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR]</b> GetAnalogActionData error (" + fullPath + "): " + err.ToString() +
                               " handle: " + handle.ToString());
            }

            updateTime = Time.realtimeSinceStartup;

            changed = false;

            if (active)
            {
                if (delta > changeTolerance || delta < -changeTolerance)
                {
                    changed     = true;
                    changedTime =
                        Time.realtimeSinceStartup +
                        actionData
                        .fUpdateTime;     //fUpdateTime is the time from the time the action was called that the action changed

                    if (onChange != null)
                    {
                        onChange.Invoke(singleAction, inputSource, axis, delta);
                    }
                }

                if (axis != 0)
                {
                    if (onAxis != null)
                    {
                        onAxis.Invoke(singleAction, inputSource, axis, delta);
                    }
                }

                if (onUpdate != null)
                {
                    onUpdate.Invoke(singleAction, inputSource, axis, delta);
                }
            }


            if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
            {
                onActiveBindingChange.Invoke(singleAction, inputSource, activeBinding);
            }

            if (onActiveChange != null && lastActive != active)
            {
                onActiveChange.Invoke(singleAction, inputSource, activeBinding);
            }
        }
Ejemplo n.º 29
0
        public void Initialize()
        {
            ulong         newHandle = 0;
            EVRInputError err       = OpenVR.Input.GetActionSetHandle(fullPath.ToLower(), ref newHandle);

            handle = newHandle;

            if (err != EVRInputError.None)
            {
                Debug.LogError("<b>[SteamVR]</b> GetActionSetHandle (" + fullPath + ") error: " + err.ToString());
            }

            initialized = true;
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Trigger the haptics at a certain time for a certain length
        /// </summary>
        /// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
        /// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
        /// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
        /// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
        /// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
        public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource)
        {
            lastChanged[inputSource] = Time.time;

            EVRInputError err = OpenVR.Input.TriggerHapticVibrationAction(handle, secondsFromNow, durationSeconds, frequency, amplitude, SteamVR_Input_Source.GetHandle(inputSource));

            //Debug.Log(string.Format("haptic: {5}: {0}, {1}, {2}, {3}, {4}", secondsFromNow, durationSeconds, frequency, amplitude, inputSource, this.GetShortName()));

            if (err != EVRInputError.None)
            {
                Debug.LogError("TriggerHapticVibrationAction (" + fullPath + ") error: " + err.ToString() + " handle: " + handle.ToString());
            }
        }