Esempio n. 1
0
        private void AssignHandJoint(RigidPose handPose, bool isLeft, JointEnumArray joints, HandJointName jointName, VRBoneTransform_t[] boneTransforms, int steamBoneIndex)
        {
            VRBoneTransform_t bone     = boneTransforms[steamBoneIndex];
            Vector3           position = new Vector3(-bone.position.v0, bone.position.v1, bone.position.v2);
            Quaternion        rotation = new Quaternion(bone.orientation.x, -bone.orientation.y, -bone.orientation.z, bone.orientation.w);

            if (steamBoneIndex == SteamVR_Skeleton_JointIndexes.wrist)
            {
                if (isLeft)
                {
                    rotation *= s_leftSkeletonWristFixRotation;
                }
                else
                {
                    rotation *= s_rightSkeletonWristFixRotation;
                }
            }
            else
            {
                if (isLeft)
                {
                    rotation *= s_leftSkeletonFixRotation;
                }
                else
                {
                    rotation *= s_rightSkeletonFixRotation;
                }
            }

            joints[jointName] = new JointPose(handPose * new RigidPose(position, rotation));
        }
    // Update is called once per frame
    void Update()
    {
        EVRInputError err;

        if (initialized == false)
        {
            if (OpenVR.Input == null)
            {
                return;
            }
            if (activeActionSetSize == 0)
            {
                activeActionSetSize = (uint)(Marshal.SizeOf(typeof(VRActiveActionSet_t)));
            }
            if (skeletalActionData_size == 0)
            {
                skeletalActionData_size = (uint)Marshal.SizeOf(typeof(InputSkeletalActionData_t));
            }
            if (digitalActionData_size == 0)
            {
                digitalActionData_size = (uint)Marshal.SizeOf(typeof(InputDigitalActionData_t));
            }
            if (analogActionData_size == 0)
            {
                analogActionData_size = (uint)Marshal.SizeOf(typeof(InputAnalogActionData_t));
            }

            rangeOfMotion          = EVRSkeletalMotionRange.WithoutController;
            skeletalTransformSpace = EVRSkeletalTransformSpace.Parent;

            var fullPath = SteamVR_Input.GetActionsFilePath();

            err = OpenVR.Input.SetActionManifestPath(fullPath);
            if (err != EVRInputError.None)
            {
                Debug.LogError($"<b>[SteamVR]</b> Error loading action manifest into SteamVR: {err}");
            }

            //parse actions.json
            var json = File.ReadAllText(fullPath);
            CurrentActionData = JsonUtility.FromJson <SteamVRActions>(json);

            foreach (var action in CurrentActionData.actions)
            {
                err = OpenVR.Input.GetActionHandle(action.name, ref action.handle);
                if (err != EVRInputError.None)
                {
                    Debug.LogError($"<b>[SteamVR]</b> GetActionHandle error ({action.name}): {err}");
                }
            }

            initialized = true;

            var   actionSetPath   = CurrentActionData.action_sets.First().name;
            ulong actionSetHandle = 0;
            err = OpenVR.Input.GetActionSetHandle(actionSetPath, ref actionSetHandle);
            if (err != EVRInputError.None)
            {
                Debug.LogError($"<b>[SteamVR]</b> GetActionSetHandle error ({actionSetPath}): {err}");
            }

            var inputSourceNames = System.Enum.GetNames(typeof(SteamVR_Input_Sources));
            foreach (var inputSourceName in inputSourceNames)
            {
                ulong inputSourceHandle = 0;
                var   inputSourcePath   = GetPath(inputSourceName); // Any,LeftHand,RightHand,...
                err = OpenVR.Input.GetInputSourceHandle(inputSourcePath, ref inputSourceHandle);
                if (err != EVRInputError.None)
                {
                    Debug.LogError($"<b>[SteamVR]</b> GetInputSourceHandle error ({inputSourcePath}): {err}");
                }
                else
                {
                    ActionSetList.Add(new VRActionSet
                    {
                        ulActionSet          = actionSetHandle,
                        ulRestrictedToDevice = inputSourceHandle,
                        InputSourcePath      = inputSourcePath,
                        IsLeft = inputSourcePath.Contains("left") != handSwap,
                    });
                }
            }
            //UpdateSkeleton();
            rawActiveActionSetArray = ActionSetList.Select(d => new VRActiveActionSet_t
            {
                ulActionSet          = d.ulActionSet,
                nPriority            = 0,//同プライオリティのアクションセットが複数ある場合同時に実行される
                ulRestrictedToDevice = d.ulRestrictedToDevice
            }).ToArray();

            OpenVR.Compositor.SetTrackingSpace(ETrackingUniverseOrigin.TrackingUniverseStanding);
        }

        //すべてのActionSetに対して新しいイベントがないか更新する
        err = OpenVR.Input.UpdateActionState(rawActiveActionSetArray, activeActionSetSize);
        if (err != EVRInputError.None)
        {
            Debug.LogError($"<b>[SteamVR]</b> UpdateActionState error: {err}");
        }

        foreach (var actionset in ActionSetList)
        {
            foreach (var action in CurrentActionData.actions)
            {
                if (action.type == "boolean")
                {
                    //オンオフ系のデータ取得(クリックやタッチ)
                    action.lastDigitalActionData = action.digitalActionData;
                    err = OpenVR.Input.GetDigitalActionData(action.handle, ref action.digitalActionData, digitalActionData_size, actionset.ulRestrictedToDevice);
                    if (err != EVRInputError.None)
                    {
                        Debug.LogWarning($"<b>[SteamVR]</b> GetDigitalActionData error ({action.name}): {err} handle: {action.handle}");
                        continue;
                    }
                    if (IsKeyDown(action.digitalActionData))
                    {
                        Debug.Log($"<b>[SteamVR]</b> GetDigitalActionData IsKeyDown ({action.name}): {err} handle: {action.handle}");

                        bool    isTouch = action.ShortName.StartsWith("Touch") && action.ShortName.Contains("Trigger") == false;
                        Vector3 axis    = isTouch ? GetLastPosition(action.ShortName) : Vector3.zero;
                        KeyDownEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, axis != Vector3.zero, isTouch));
                    }
                    if (IsKeyUp(action.digitalActionData))
                    {
                        Debug.Log($"<b>[SteamVR]</b> GetDigitalActionData IsKeyUp ({action.name}): {err} handle: {action.handle}");

                        bool    isTouch = action.ShortName.StartsWith("Touch") && action.ShortName.Contains("Trigger") == false;
                        Vector3 axis    = isTouch ? GetLastPosition(action.ShortName) : Vector3.zero;
                        KeyUpEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, axis != Vector3.zero, isTouch));
                    }
                }
                else if (action.type == "vector1" || action.type == "vector2" || action.type == "vector3")
                {
                    //アナログ入力のデータ取得(スティックやタッチパッド)
                    action.lastAnalogActionData = action.analogActionData;
                    err = OpenVR.Input.GetAnalogActionData(action.handle, ref action.analogActionData, analogActionData_size, actionset.ulRestrictedToDevice);
                    if (err != EVRInputError.None)
                    {
                        Debug.LogWarning($"<b>[SteamVR]</b> GetAnalogActionData error ({action.name}): {err} handle: {action.handle}");
                        continue;
                    }
                    //Debug.Log($"<b>[SteamVR]</b> GetAnalogActionData Position:{action.analogActionData.x},{action.analogActionData.y} ({action.name}): {err} handle: {action.handle}");
                    var axis = new Vector3(action.analogActionData.x, action.analogActionData.y, action.analogActionData.z);
                    if (axis != Vector3.zero)
                    {
                        LastPositions[action.name] = axis;
                        AxisChangedEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, true, false));
                    }
                }
                else if (action.type == "skeleton")
                {
                    if (EnableSkeletal)
                    {
                        //実際にBoneのTransformを取得する
                        //rangeOfMotionは実際のコントローラーの形に指を曲げる(WithController)か、完全にグーが出来るようにする(WithoutController)か
                        var tempBoneTransforms = new VRBoneTransform_t[SteamVR_Action_Skeleton.numBones];
                        err = OpenVR.Input.GetSkeletalBoneData(action.handle, skeletalTransformSpace, rangeOfMotion, tempBoneTransforms);
                        if (err != EVRInputError.None)
                        {
                            //特定の条件においてものすごい勢いでログが出る
                            //Debug.LogWarning($"<b>[SteamVR]</b> GetDigitalActionData error ({action.name}): {err} handle: {action.handle}");
                            continue;
                        }
                        handTracking_Skeletal.SetSkeltalBoneData(action.name.Contains("Left") != handSwap, tempBoneTransforms);
                    }
                }
            }
        }
    }