예제 #1
0
    protected override void InitializeBones()
    {
        bool flipX = (_skeletonType == SkeletonType.HandLeft || _skeletonType == SkeletonType.HandRight);

        if (_bones == null || _bones.Count != _skeleton.NumBones)
        {
            _bones = new List <OVRBone>(new OVRBone[_skeleton.NumBones]);
            Bones  = _bones.AsReadOnly();
        }

        for (int i = 0; i < _bones.Count; ++i)
        {
            OVRBone bone = _bones[i] ?? (_bones[i] = new OVRBone());
            bone.Id = (OVRSkeleton.BoneId)_skeleton.Bones[i].Id;
            bone.ParentBoneIndex = _skeleton.Bones[i].ParentBoneIndex;
            bone.Transform       = _customBones_V2[(int)bone.Id];

            if (_applyBoneTranslations)
            {
                bone.Transform.localPosition = flipX ? _skeleton.Bones[i].Pose.Position.FromFlippedXVector3f() : _skeleton.Bones[i].Pose.Position.FromFlippedZVector3f();
            }

            bone.Transform.localRotation = flipX ? _skeleton.Bones[i].Pose.Orientation.FromFlippedXQuatf() : _skeleton.Bones[i].Pose.Orientation.FromFlippedZQuatf();
        }
    }
예제 #2
0
        protected void UpdateBone(OVRBone bone)
        {
            var boneId        = bone.Id;
            var boneTransform = bone.Transform;

            if (boneJointMapping.TryGetValue(boneId, out var joint))
            {
                Quaternion boneRotation = bone.Transform.rotation;

                // WARNING THIS CODE IS SUBJECT TO CHANGE WITH THE OCULUS SDK - This fix is a hack to fix broken and inconsistent rotations for hands
                if (ControllerHandedness == Handedness.Left)
                {
                    // Rotate palm 180 on X to flip up
                    boneRotation *= Quaternion.Euler(180f, 0f, 0f);

                    // Rotate palm 90 degrees on y to align x with right
                    boneRotation *= Quaternion.Euler(0f, -90, 0f);
                }
                else
                {
                    // Right Up direction is correct

                    // Rotate palm 90 degrees on y to align x with right
                    boneRotation *= Quaternion.Euler(0f, 90f, 0f);
                }

                UpdateJointPose(joint, boneTransform.position, boneRotation);
            }
        }
        protected void UpdateBone(OVRBone bone)
        {
            var boneId        = bone.Id;
            var boneTransform = bone.Transform;

            if (boneJointMapping.TryGetValue(boneId, out var joint))
            {
                UpdateJointPose(joint, boneTransform.position, boneTransform.rotation);
            }
        }
    // Update is called once per frame
    void Update()
    {
        if (boneToTrack == null)
        {
            boneToTrack = ovrSkeleton.Bones
                          .Where(b => b.Id == OVRSkeleton.BoneId.Hand_Index3)
                          .SingleOrDefault();
        }

        CheckPinchState();
    }
예제 #5
0
    protected override void InitializeBones(OVRPlugin.Skeleton skeleton)
    {
        _bones = new List <OVRBone>(new OVRBone[skeleton.NumBones]);
        Bones  = _bones.AsReadOnly();

        for (int i = 0; i < skeleton.NumBones; ++i)
        {
            BoneId    id        = (BoneId)skeleton.Bones[i].Id;
            short     parentIdx = skeleton.Bones[i].ParentBoneIndex;
            Transform t         = _customBones[(int)id];
            _bones[i] = new OVRBone(id, parentIdx, t);
        }
    }
예제 #6
0
        void Update()
        {
            if (boneToTrack == null)
            {
                boneToTrack = ovrSkeleton.Bones
                              .Where(b => b.Id == OVRSkeleton.BoneId.Hand_Index3)
                              .SingleOrDefault();

                objectToTrackMovement = boneToTrack.Transform.gameObject;
            }

            CheckPinchState();
        }
예제 #7
0
    protected virtual void InitializeBones()
    {
        bool flipX = (_skeletonType == SkeletonType.HandLeft || _skeletonType == SkeletonType.HandRight);

        if (!_bonesGO)
        {
            _bonesGO = new GameObject("Bones");
            _bonesGO.transform.SetParent(transform, false);
            _bonesGO.transform.localPosition = Vector3.zero;
            _bonesGO.transform.localRotation = Quaternion.identity;
        }

        if (_bones == null || _bones.Count != _skeleton.NumBones)
        {
            _bones = new List <OVRBone>(new OVRBone[_skeleton.NumBones]);
            Bones  = _bones.AsReadOnly();
        }

        // pre-populate bones list before attempting to apply bone hierarchy
        for (int i = 0; i < _bones.Count; ++i)
        {
            OVRBone bone = _bones[i] ?? (_bones[i] = new OVRBone());
            bone.Id = (OVRSkeleton.BoneId)_skeleton.Bones[i].Id;
            bone.ParentBoneIndex = _skeleton.Bones[i].ParentBoneIndex;

            Transform trans = bone.Transform ??
                              (bone.Transform = new GameObject(BoneLabelFromBoneId(_skeletonType, bone.Id)).transform);
            trans.localPosition = flipX ? _skeleton.Bones[i].Pose.Position.FromFlippedXVector3f() : _skeleton.Bones[i].Pose.Position.FromFlippedZVector3f();
            trans.localRotation = flipX ? _skeleton.Bones[i].Pose.Orientation.FromFlippedXQuatf() : _skeleton.Bones[i].Pose.Orientation.FromFlippedZQuatf();
        }

        for (int i = 0; i < _bones.Count; ++i)
        {
            if ((BoneId)_bones[i].ParentBoneIndex == BoneId.Invalid)
            {
                _bones[i].Transform.SetParent(_bonesGO.transform, false);
            }
            else
            {
                _bones[i].Transform.SetParent(_bones[_bones[i].ParentBoneIndex].Transform, false);
            }
        }
    }
예제 #8
0
    private void InitializeBindPose()
    {
        if (!_bindPosesGO)
        {
            _bindPosesGO = new GameObject("BindPoses");
            _bindPosesGO.transform.SetParent(transform, false);
            _bindPosesGO.transform.localPosition = Vector3.zero;
            _bindPosesGO.transform.localRotation = Quaternion.identity;
        }

        if (_bindPoses == null || _bindPoses.Count != _bones.Count)
        {
            _bindPoses = new List <OVRBone>(new OVRBone[_bones.Count]);
            BindPoses  = _bindPoses.AsReadOnly();
        }

        // pre-populate bones list before attempting to apply bone hierarchy
        for (int i = 0; i < _bindPoses.Count; ++i)
        {
            OVRBone bone         = _bones[i];
            OVRBone bindPoseBone = _bindPoses[i] ?? (_bindPoses[i] = new OVRBone());
            bindPoseBone.Id = bone.Id;
            bindPoseBone.ParentBoneIndex = bone.ParentBoneIndex;

            Transform trans = bindPoseBone.Transform ?? (bindPoseBone.Transform =
                                                             new GameObject(BoneLabelFromBoneId(_skeletonType, bindPoseBone.Id)).transform);
            trans.localPosition = bone.Transform.localPosition;
            trans.localRotation = bone.Transform.localRotation;
        }

        for (int i = 0; i < _bindPoses.Count; ++i)
        {
            if ((BoneId)_bindPoses[i].ParentBoneIndex == BoneId.Invalid)
            {
                _bindPoses[i].Transform.SetParent(_bindPosesGO.transform, false);
            }
            else
            {
                _bindPoses[i].Transform.SetParent(_bindPoses[_bindPoses[i].ParentBoneIndex].Transform, false);
            }
        }
    }
예제 #9
0
    private void InitializeBindPose(OVRPlugin.Skeleton skeleton)
    {
        _bindPoses = new List <OVRBone>(new OVRBone[skeleton.NumBones]);
        BindPoses  = _bindPoses.AsReadOnly();

        if (!_bindPosesGO)
        {
            _bindPosesGO = new GameObject("BindPoses");
            _bindPosesGO.transform.SetParent(transform, false);
            _bindPosesGO.transform.localPosition = Vector3.zero;
            _bindPosesGO.transform.localRotation = Quaternion.identity;
        }

        for (int i = 0; i < skeleton.NumBones; ++i)
        {
            BoneId  id         = (OVRSkeleton.BoneId)skeleton.Bones[i].Id;
            short   parentIdx  = skeleton.Bones[i].ParentBoneIndex;
            var     bindPoseGO = new GameObject(id.ToString());
            OVRBone bone       = _bones[i];

            if (bone.Transform != null)
            {
                bindPoseGO.transform.localPosition = bone.Transform.localPosition;
                bindPoseGO.transform.localRotation = bone.Transform.localRotation;
            }

            _bindPoses[i] = new OVRBone(id, parentIdx, bindPoseGO.transform);
        }

        for (int i = 0; i < skeleton.NumBones; ++i)
        {
            if (((OVRPlugin.BoneId)skeleton.Bones[i].ParentBoneIndex) == OVRPlugin.BoneId.Invalid)
            {
                _bindPoses[i].Transform.SetParent(_bindPosesGO.transform, false);
            }
            else
            {
                _bindPoses[i].Transform.SetParent(_bindPoses[_bones[i].ParentBoneIndex].Transform, false);
            }
        }
    }
예제 #10
0
    private void Start()
    {
        leftSkeleton = left.GetComponent <OVRSkeleton>();
        foreach (OVRBone bone in leftSkeleton.Bones)
        {
            if (bone.Id == OVRSkeleton.BoneId.Hand_IndexTip)
            {
                leftIndexTip = bone;
            }
        }


        rightSkeleton = right.GetComponent <OVRSkeleton>();
        foreach (OVRBone bone in rightSkeleton.Bones)
        {
            if (bone.Id == OVRSkeleton.BoneId.Hand_IndexTip)
            {
                rightIndexTip = bone;
            }
        }
    }
예제 #11
0
    virtual protected void InitializeBones(OVRPlugin.Skeleton skeleton)
    {
        _bones = new List <OVRBone>(new OVRBone[skeleton.NumBones]);
        Bones  = _bones.AsReadOnly();

        if (!_bonesGO)
        {
            _bonesGO = new GameObject("Bones");
            _bonesGO.transform.SetParent(transform, false);
            _bonesGO.transform.localPosition = Vector3.zero;
            _bonesGO.transform.localRotation = Quaternion.identity;
        }

        // pre-populate bones list before attempting to apply bone hierarchy
        for (int i = 0; i < skeleton.NumBones; ++i)
        {
            BoneId     id        = (OVRSkeleton.BoneId)skeleton.Bones[i].Id;
            short      parentIdx = skeleton.Bones[i].ParentBoneIndex;
            Vector3    pos       = skeleton.Bones[i].Pose.Position.FromFlippedXVector3f();
            Quaternion rot       = skeleton.Bones[i].Pose.Orientation.FromFlippedXQuatf();

            var boneGO = new GameObject(id.ToString());
            boneGO.transform.localPosition = pos;
            boneGO.transform.localRotation = rot;
            _bones[i] = new OVRBone(id, parentIdx, boneGO.transform);
        }

        for (int i = 0; i < skeleton.NumBones; ++i)
        {
            if (((OVRPlugin.BoneId)skeleton.Bones[i].ParentBoneIndex) == OVRPlugin.BoneId.Invalid)
            {
                _bones[i].Transform.SetParent(_bonesGO.transform, false);
            }
            else
            {
                _bones[i].Transform.SetParent(_bones[_bones[i].ParentBoneIndex].Transform, false);
            }
        }
    }
        // Start is called before the first frame update
        void Start()
        {
            skeleton = trackedHand.GetComponent <OVRSkeleton>();
            mr       = trackedHand.GetComponent <OVRMeshRenderer>();
            smr      = trackedHand.GetComponent <SkinnedMeshRenderer>();

            foreach (OVRBone bone in skeleton.Bones)
            {
                if (bone.Id == OVRSkeleton.BoneId.Hand_IndexTip)
                {
                    indexTipBone = bone;
                }
                if (bone.Id == OVRSkeleton.BoneId.Hand_MiddleTip)
                {
                    middleTipBone = bone;
                }
                if (bone.Id == OVRSkeleton.BoneId.Hand_RingTip)
                {
                    ringTipBone = bone;
                }
                if (bone.Id == OVRSkeleton.BoneId.Hand_PinkyTip)
                {
                    pinkyTipBone = bone;
                }
                if (bone.Id == OVRSkeleton.BoneId.Hand_ThumbTip)
                {
                    thumbTipBone = bone;
                }
                if (bone.Id == OVRSkeleton.BoneId.Hand_Start)
                {
                    handBone = bone;
                }
            }

            for (int i = 0; i < smoothSteps; i++)
            {
                lastPositions.Add(IndexTipObject.transform.position);
            }
        }
예제 #13
0
        void Awake()
        {
            ovrHand     = objectToTrackMovement.GetComponent <OVRHand>();
            ovrSkeleton = objectToTrackMovement.GetComponent <OVRSkeleton>();

            #if UNITY_EDITOR
            // if we allow editor controls use the editor object to track movement because oculus
            // blocks the movement of LeftControllerAnchor and RightControllerAnchor
            if (allowEditorControls)
            {
                objectToTrackMovement = editorObjectToTrackMovement != null ? editorObjectToTrackMovement : objectToTrackMovement;
            }
            #endif
            // get initial bone to track
            boneToTrack = ovrSkeleton.Bones
                          .Where(b => b.Id == OVRSkeleton.BoneId.Hand_Index1)
                          .SingleOrDefault();

            VRLogInfo.Instance.LogInfo("boneToTrack is null: " + (boneToTrack == null));

            // add initial line rerderer
            AddNewLineRenderer();
        }
예제 #14
0
        /// <summary>
        /// Bonesを生成
        /// </summary>
        /// <param name="skeleton">あらかじめ用意されたボーンの情報</param>
        /// <param name="hand">左右どちらかの手</param>
        /// <param name="isInitialize">初期化完了フラグ</param>
        private void InitializeBones(OVRPlugin.Skeleton skeleton, GameObject hand, out bool isInitialize)
        {
            _bones = new List <OVRBone>(new OVRBone[skeleton.NumBones]);

            var bonesGO = new GameObject("Bones");

            bonesGO.transform.SetParent(hand.transform, false);
            bonesGO.transform.localPosition = Vector3.zero;
            bonesGO.transform.localRotation = Quaternion.identity;

            for (var i = 0; i < skeleton.NumBones; ++i)
            {
                var id        = (OVRSkeleton.BoneId)skeleton.Bones[i].Id;
                var parentIdx = skeleton.Bones[i].ParentBoneIndex;
                var pos       = skeleton.Bones[i].Pose.Position.FromFlippedXVector3f();
                var rot       = skeleton.Bones[i].Pose.Orientation.FromFlippedXQuatf();

                var boneGO = new GameObject(id.ToString());
                boneGO.transform.localPosition = pos;
                boneGO.transform.localRotation = rot;
                _bones[i] = new OVRBone(id, parentIdx, boneGO.transform);
            }

            for (var i = 0; i < skeleton.NumBones; ++i)
            {
                if (((OVRPlugin.BoneId)skeleton.Bones[i].ParentBoneIndex) == OVRPlugin.BoneId.Invalid)
                {
                    _bones[i].Transform.SetParent(bonesGO.transform, false);
                }
                else
                {
                    _bones[i].Transform.SetParent(_bones[_bones[i].ParentBoneIndex].Transform, false);
                }
            }

            isInitialize = true;
        }
예제 #15
0
    IEnumerator _Initialize()
    {
        yield return(null);

        while (skeleton.Bones == null)
        {
            yield return(null);
        }
        if (debug)
        {
            Debug.Log("has bones");
        }

        bone = null;
        while (bone == null)
        {
            bone = skeleton.Bones.FirstOrDefault(x => x.Id == boneId);
            yield return(null);
        }
        if (debug)
        {
            Debug.Log("Found Bone!");
        }

        while (!bone.Transform)
        {
            yield return(null);
        }
        if (debug)
        {
            Debug.Log("Found Transform!");
        }

        transform.SetParent(bone.Transform, false);
        transform.localPosition = Vector3.zero;
        transform.localRotation = Quaternion.identity;
    }
예제 #16
0
        void Update()
        {
            updateHandTracking();

            if (IsHandTracking)
            {
                LeftHandConfidence  = LeftHand.GetFingerConfidence(HandFinger.Index);
                RightHandConfidence = RightHand.GetFingerConfidence(HandFinger.Index);

                if (leftSkele != null && leftSkele.Bones != null)
                {
                    leftIndexBone = leftSkele.Bones.FirstOrDefault(x => x.Id == OVRSkeleton.BoneId.Hand_IndexTip);
                    if (leftIndexBone != null)
                    {
                        LeftIndexPosition = leftIndexBone.Transform.position;
                    }
                }

                IsLeftIndexPinching    = LeftHand.GetFingerIsPinching(HandFinger.Index) && LeftHandConfidence == TrackingConfidence.High;
                LeftIndexPinchStrength = LeftHand.GetFingerPinchStrength(HandFinger.Index);

                if (rightSkele && rightSkele.Bones != null)
                {
                    rightIndexBone = rightSkele.Bones.FirstOrDefault(x => x.Id == OVRSkeleton.BoneId.Hand_IndexTip);
                    if (rightIndexBone != null)
                    {
                        RightIndexPosition = rightIndexBone.Transform.position;
                    }
                }

                IsRightIndexPinching    = RightHand.GetFingerIsPinching(HandFinger.Index) && RightHandConfidence == TrackingConfidence.High;
                RightIndexPinchStrength = RightHand.GetFingerPinchStrength(HandFinger.Index);
            }

            updateGrabbers();
        }
예제 #17
0
 public void AttachMarionetteToBone(OVRBone bone, Transform marionetteSource)
 {
     marionetteSource.SetParent(bone.Transform);
 }
예제 #18
0
        void DoSkeletalTracking()
        {
            rawSkeleton = (OVRPlugin.Skeleton2)skeletonField.GetValue(skeleton);

            // do any pre-flight checks here
            // confidence maybe?
            if (true)
            {
                controllerHand.Confidence = skeleton.IsDataHighConfidence ? 1 : 0;
                handAnimator.enabled      = false;

                // do our wrist pose
                OVRBone wristBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_WristRoot];

                Vector3    wristPos             = wristBone.Transform.position;
                Quaternion wristboneOrientation = controllerHand.GetLocalBasis();
                controllerHand.Wrist.rotation = GlobalRotationFromBasis(wristBone.Transform, fingerBasis) * wristboneOrientation;

                // do our fingers. Skip metacarpals, Oculus does not provide them.
                // we could possibly compute the missing bone rotation, if needed.
                // do the index bones
                OVRBone   indexProximalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Index1];
                OVRBone   indexMedialBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Index2];
                OVRBone   indexDistalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Index3];
                OVRBone   indexDistalTip    = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip];
                Transform indexProximal     = controllerHand.IndexMetacarpal.GetChild(0);
                Transform indexMedial       = indexProximal.GetChild(0);
                Transform indexDistal       = indexMedial.GetChild(0);
                Transform indexTip          = indexDistal.GetChild(0);

                controllerHand.FingerWidth[(int)Leap.Finger.FingerType.TYPE_INDEX] = rawSkeleton.BoneCapsules[(int)OVRSkeleton.BoneId.Hand_Index1].Radius * 0.5f;
                MatchBone(indexProximalBone.Transform, indexProximal, fingerBasis, wristboneOrientation);
                MatchBone(indexMedialBone.Transform, indexMedial, fingerBasis, wristboneOrientation);
                MatchBone(indexDistalBone.Transform, indexDistal, fingerBasis, wristboneOrientation);
                Vector3 indexTipLocal = indexDistal.InverseTransformPoint(indexDistalTip.Transform.position);

                // do the middle bones
                OVRBone   middleProximalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Middle1];
                OVRBone   middleMedialBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Middle2];
                OVRBone   middleDistalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Middle3];
                OVRBone   middleDistalTip    = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_MiddleTip];
                Transform middleProximal     = controllerHand.MiddleMetacarpal.GetChild(0);
                Transform middleMedial       = middleProximal.GetChild(0);
                Transform middleDistal       = middleMedial.GetChild(0);
                Transform middleTip          = middleDistal.GetChild(0);

                controllerHand.FingerWidth[(int)Leap.Finger.FingerType.TYPE_MIDDLE] = rawSkeleton.BoneCapsules[(int)OVRSkeleton.BoneId.Hand_Middle1].Radius * 0.5f;
                MatchBone(middleProximalBone.Transform, middleProximal, fingerBasis, wristboneOrientation);
                MatchBone(middleMedialBone.Transform, middleMedial, fingerBasis, wristboneOrientation);
                MatchBone(middleDistalBone.Transform, middleDistal, fingerBasis, wristboneOrientation);
                Vector3 middleTipLocal = middleDistal.InverseTransformPoint(middleDistalTip.Transform.position);

                // do the ring bones
                OVRBone   ringProximalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Ring1];
                OVRBone   ringMedialBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Ring2];
                OVRBone   ringDistalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Ring3];
                OVRBone   ringDistalTip    = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_RingTip];
                Transform ringProximal     = controllerHand.RingMetacarpal.GetChild(0);
                Transform ringMedial       = ringProximal.GetChild(0);
                Transform ringDistal       = ringMedial.GetChild(0);
                Transform ringTip          = ringDistal.GetChild(0);

                controllerHand.FingerWidth[(int)Leap.Finger.FingerType.TYPE_RING] = rawSkeleton.BoneCapsules[(int)OVRSkeleton.BoneId.Hand_Ring1].Radius * 0.5f;
                MatchBone(ringProximalBone.Transform, ringProximal, fingerBasis, wristboneOrientation);
                MatchBone(ringMedialBone.Transform, ringMedial, fingerBasis, wristboneOrientation);
                MatchBone(ringDistalBone.Transform, ringDistal, fingerBasis, wristboneOrientation);
                Vector3 ringTipLocal = ringDistal.InverseTransformPoint(ringDistalTip.Transform.position);

                // do the pinky bones
                OVRBone   pinkyMetacarpalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky0];
                OVRBone   pinkyProximalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky1];
                OVRBone   pinkyMedialBone     = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky2];
                OVRBone   pinkyDistalBone     = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky3];
                OVRBone   pinkyDistalTip      = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_PinkyTip];
                Transform pinkyMetacarpal     = controllerHand.PinkyMetacarpal;
                Transform pinkyPromial        = pinkyMetacarpal.GetChild(0);
                Transform pinkyMedial         = pinkyPromial.GetChild(0);
                Transform pinkyDistal         = pinkyMedial.GetChild(0);
                Transform pinkyTip            = pinkyDistal.GetChild(0);

                controllerHand.FingerWidth[(int)Leap.Finger.FingerType.TYPE_PINKY] = rawSkeleton.BoneCapsules[(int)OVRSkeleton.BoneId.Hand_Pinky1].Radius * 0.5f;
                MatchBone(pinkyMetacarpalBone.Transform, pinkyMetacarpal, fingerBasis, wristboneOrientation);
                MatchBone(pinkyProximalBone.Transform, pinkyPromial, fingerBasis, wristboneOrientation);
                MatchBone(pinkyMedialBone.Transform, pinkyMedial, fingerBasis, wristboneOrientation);
                MatchBone(pinkyDistalBone.Transform, pinkyDistal, fingerBasis, wristboneOrientation);
                Vector3 pinkyLocal = pinkyDistal.InverseTransformPoint(pinkyDistalTip.Transform.position);

                // do the thumb bones
                OVRBone   thumbRootBone       = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb0];
                OVRBone   thumbMetacarpalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb1];
                OVRBone   thumbProximalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb2];
                OVRBone   thumbDistalBone     = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb3];
                OVRBone   thumbDistalTip      = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_ThumbTip];
                Transform thumbMetacarpal     = controllerHand.ThumbMetacarpal;             // naming gets weird here, sorry.
                Transform thumbProximal       = thumbMetacarpal.GetChild(0);
                Transform thumbDistal         = thumbProximal.GetChild(0);
                Transform thumbTip            = thumbDistal.GetChild(0);

                controllerHand.FingerWidth[(int)Leap.Finger.FingerType.TYPE_THUMB] = rawSkeleton.BoneCapsules[(int)OVRSkeleton.BoneId.Hand_Thumb0].Radius * 0.5f;
                MatchBone(thumbMetacarpalBone.Transform, thumbMetacarpal, fingerBasis, wristboneOrientation);
                MatchBone(thumbProximalBone.Transform, thumbProximal, fingerBasis, wristboneOrientation);
                MatchBone(thumbDistalBone.Transform, thumbDistal, fingerBasis, wristboneOrientation);
                Vector3 thumbLocal = thumbDistal.InverseTransformPoint(thumbDistalTip.Transform.position);

                // Apply our tip shortening
                Vector3 scaleFactor = new Vector3(
                    (fingerForward.x != 0) ? tipScale : 1,
                    (fingerForward.y != 0) ? tipScale : 1,
                    (fingerForward.z != 0) ? tipScale : 1);                     // do this once at startup maybe?
                indexTip.transform.localPosition = Vector3.Scale(indexTipLocal, scaleFactor);
                middleTip.localPosition          = Vector3.Scale(middleTipLocal, scaleFactor);
                ringTip.transform.localPosition  = Vector3.Scale(ringTipLocal, scaleFactor);
                pinkyTip.transform.localPosition = Vector3.Scale(pinkyLocal, scaleFactor);
                thumbTip.localPosition           = Vector3.Scale(thumbLocal, scaleFactor * 1.25f);
            }
        }
예제 #19
0
    private void InitializeCapsules()
    {
        bool flipX = (_skeletonType == SkeletonType.HandLeft || _skeletonType == SkeletonType.HandRight);

        if (_enablePhysicsCapsules)
        {
            if (!_capsulesGO)
            {
                _capsulesGO = new GameObject("Capsules");
                _capsulesGO.transform.SetParent(transform, false);
                _capsulesGO.transform.localPosition = Vector3.zero;
                _capsulesGO.transform.localRotation = Quaternion.identity;
            }

            if (_capsules == null || _capsules.Count != _skeleton.NumBoneCapsules)
            {
                _capsules = new List <OVRBoneCapsule>(new OVRBoneCapsule[_skeleton.NumBoneCapsules]);
                Capsules  = _capsules.AsReadOnly();
            }

            for (int i = 0; i < _capsules.Count; ++i)
            {
                OVRBone        bone    = _bones[_skeleton.BoneCapsules[i].BoneIndex];
                OVRBoneCapsule capsule = _capsules[i] ?? (_capsules[i] = new OVRBoneCapsule());
                capsule.BoneIndex = _skeleton.BoneCapsules[i].BoneIndex;

                if (capsule.CapsuleRigidbody == null)
                {
                    capsule.CapsuleRigidbody                        = new GameObject((bone.Id).ToString() + "_CapsuleRigidbody").AddComponent <Rigidbody>();
                    capsule.CapsuleRigidbody.mass                   = 1.0f;
                    capsule.CapsuleRigidbody.isKinematic            = true;
                    capsule.CapsuleRigidbody.useGravity             = false;
                    capsule.CapsuleRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
                }

                GameObject rbGO = capsule.CapsuleRigidbody.gameObject;
                rbGO.transform.SetParent(_capsulesGO.transform, false);
                rbGO.transform.position = bone.Transform.position;
                rbGO.transform.rotation = bone.Transform.rotation;

                if (capsule.CapsuleCollider == null)
                {
                    capsule.CapsuleCollider           = new GameObject((bone.Id).ToString() + "_CapsuleCollider").AddComponent <CapsuleCollider>();
                    capsule.CapsuleCollider.isTrigger = false;
                }

                var p0    = flipX ? _skeleton.BoneCapsules[i].StartPoint.FromFlippedXVector3f() : _skeleton.BoneCapsules[i].StartPoint.FromFlippedZVector3f();
                var p1    = flipX ? _skeleton.BoneCapsules[i].EndPoint.FromFlippedXVector3f() : _skeleton.BoneCapsules[i].EndPoint.FromFlippedZVector3f();
                var delta = p1 - p0;
                var mag   = delta.magnitude;
                var rot   = Quaternion.FromToRotation(Vector3.right, delta);
                capsule.CapsuleCollider.radius    = _skeleton.BoneCapsules[i].Radius;
                capsule.CapsuleCollider.height    = mag + _skeleton.BoneCapsules[i].Radius * 2.0f;
                capsule.CapsuleCollider.direction = 0;
                capsule.CapsuleCollider.center    = Vector3.right * mag * 0.5f;

                GameObject ccGO = capsule.CapsuleCollider.gameObject;
                ccGO.transform.SetParent(rbGO.transform, false);
                ccGO.transform.localPosition = p0;
                ccGO.transform.localRotation = rot;
            }
        }
    }
예제 #20
0
        void DoSkeletalTracking()
        {
            // do any pre-flight checks here
            // confidence maybe?
            if (true)
            {
                handAnimator.enabled = true;
                BoneBasis basis = new BoneBasis()
                {
                    Forward = fingerForward, Up = fingerUp
                };

                // do our wrist pose
                OVRBone wristBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_WristRoot];

                Vector3    wristPos             = wristBone.Transform.position;
                Quaternion wristboneOrientation = controllerHand.GetLocalBasis();

                /*controllerHand.Wrist.SetPositionAndRotation(wristBone.Transform.position,
                 *      GlobalRotationFromBasis(wristBone.Transform, basis) * wristboneOrientation);*/
                controllerHand.Wrist.rotation = GlobalRotationFromBasis(wristBone.Transform, basis) * wristboneOrientation;

                // do our fingers. Skip metacarpals, Oculus does not provide them.
                // we could possibly compute the missing bone rotation, if needed.
                // do the index bones
                OVRBone   indexProximalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Index1];
                OVRBone   indexMedialBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Index2];
                OVRBone   indexDistalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Index3];
                Transform indexProximal     = controllerHand.IndexMetacarpal.GetChild(0);
                Transform indexMedial       = indexProximal.GetChild(0);
                Transform indexDistal       = indexMedial.GetChild(0);

                MatchBone(indexProximalBone.Transform, indexProximal, basis, wristboneOrientation);
                MatchBone(indexMedialBone.Transform, indexMedial, basis, wristboneOrientation);
                MatchBone(indexDistalBone.Transform, indexDistal, basis, wristboneOrientation);

                // do the middle bones
                OVRBone   middleProximalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Middle1];
                OVRBone   middleMedialBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Middle2];
                OVRBone   middleDistalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Middle3];
                Transform middleProximal     = controllerHand.MiddleMetacarpal.GetChild(0);
                Transform middleMedial       = middleProximal.GetChild(0);
                Transform middleDistal       = middleMedial.GetChild(0);

                MatchBone(middleProximalBone.Transform, middleProximal, basis, wristboneOrientation);
                MatchBone(middleMedialBone.Transform, middleMedial, basis, wristboneOrientation);
                MatchBone(middleDistalBone.Transform, middleDistal, basis, wristboneOrientation);

                // do the ring bones
                OVRBone   ringProximalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Ring1];
                OVRBone   ringMedialBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Ring2];
                OVRBone   ringDistalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Ring3];
                Transform ringProximal     = controllerHand.RingMetacarpal.GetChild(0);
                Transform ringMedial       = ringProximal.GetChild(0);
                Transform ringDistal       = ringMedial.GetChild(0);

                MatchBone(ringProximalBone.Transform, ringProximal, basis, wristboneOrientation);
                MatchBone(ringMedialBone.Transform, ringMedial, basis, wristboneOrientation);
                MatchBone(ringDistalBone.Transform, ringDistal, basis, wristboneOrientation);

                // do the pinky bones
                OVRBone   pinkyMetacarpalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky0];
                OVRBone   pinkyProximalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky1];
                OVRBone   pinkyMedialBone     = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky2];
                OVRBone   pinkyDistalBone     = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Pinky2];
                Transform pinkyMetacarpal     = controllerHand.PinkyMetacarpal;
                Transform pinkyPromial        = pinkyMetacarpal.GetChild(0);
                Transform pinkyMedial         = pinkyPromial.GetChild(0);
                Transform pinkyDistal         = pinkyMedial.GetChild(0);

                MatchBone(pinkyMetacarpalBone.Transform, pinkyMetacarpal, basis, wristboneOrientation);
                MatchBone(pinkyProximalBone.Transform, pinkyPromial, basis, wristboneOrientation);
                MatchBone(pinkyMedialBone.Transform, pinkyMedial, basis, wristboneOrientation);
                MatchBone(pinkyDistalBone.Transform, pinkyDistal, basis, wristboneOrientation);

                // do the thumb bones
                OVRBone   thumbRootBone       = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb0];
                OVRBone   thumbMetacarpalBone = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb1];
                OVRBone   thumbProximalBone   = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb2];
                OVRBone   thumbDistalBone     = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_Thumb3];
                Transform thumbMetacarpal     = controllerHand.ThumbMetacarpal;             // naming gets weird here, sorry.
                Transform thumbProximal       = thumbMetacarpal.GetChild(0);
                Transform thumbDistal         = thumbProximal.GetChild(0);

                MatchBone(thumbMetacarpalBone.Transform, thumbMetacarpal, basis, wristboneOrientation);
                MatchBone(thumbProximalBone.Transform, thumbProximal, basis, wristboneOrientation);
                MatchBone(thumbDistalBone.Transform, thumbDistal, basis, wristboneOrientation);
            }
        }
예제 #21
0
    private void Initialize()
    {
        var skeleton = new OVRPlugin.Skeleton();

        if (OVRPlugin.GetSkeleton((OVRPlugin.SkeletonType)_skeletonType, out skeleton))
        {
            if (!_bonesGO)
            {
                _bonesGO = new GameObject("Bones");
                _bonesGO.transform.SetParent(transform, false);
                _bonesGO.transform.localPosition = Vector3.zero;
                _bonesGO.transform.localRotation = Quaternion.identity;
            }

            if (!_bindPosesGO)
            {
                _bindPosesGO = new GameObject("BindPoses");
                _bindPosesGO.transform.SetParent(transform, false);
                _bindPosesGO.transform.localPosition = Vector3.zero;
                _bindPosesGO.transform.localRotation = Quaternion.identity;
            }

            if (_enablePhysicsCapsules)
            {
                if (!_capsulesGO)
                {
                    _capsulesGO = new GameObject("Capsules");
                    _capsulesGO.transform.SetParent(transform, false);
                    _capsulesGO.transform.localPosition = Vector3.zero;
                    _capsulesGO.transform.localRotation = Quaternion.identity;
                }
            }

            _bones = new List <OVRBone>(new OVRBone[skeleton.NumBones]);
            Bones  = _bones.AsReadOnly();

            _bindPoses = new List <OVRBone>(new OVRBone[skeleton.NumBones]);
            BindPoses  = _bindPoses.AsReadOnly();

            // pre-populate bones list before attempting to apply bone hierarchy
            for (int i = 0; i < skeleton.NumBones; ++i)
            {
                BoneId     id        = (OVRSkeleton.BoneId)skeleton.Bones[i].Id;
                short      parentIdx = skeleton.Bones[i].ParentBoneIndex;
                Vector3    pos       = skeleton.Bones[i].Pose.Position.FromFlippedZVector3f();
                Quaternion rot       = skeleton.Bones[i].Pose.Orientation.FromFlippedZQuatf();

                var boneGO = new GameObject(id.ToString());
                boneGO.transform.localPosition = pos;
                boneGO.transform.localRotation = rot;
                _bones[i] = new OVRBone(id, parentIdx, boneGO.transform);

                var bindPoseGO = new GameObject(id.ToString());
                bindPoseGO.transform.localPosition = pos;
                bindPoseGO.transform.localRotation = rot;
                _bindPoses[i] = new OVRBone(id, parentIdx, bindPoseGO.transform);
            }

            for (int i = 0; i < skeleton.NumBones; ++i)
            {
                if (((OVRPlugin.BoneId)skeleton.Bones[i].ParentBoneIndex) == OVRPlugin.BoneId.Invalid)
                {
                    _bones[i].Transform.SetParent(_bonesGO.transform, false);
                    _bindPoses[i].Transform.SetParent(_bindPosesGO.transform, false);
                }
                else
                {
                    _bones[i].Transform.SetParent(_bones[_bones[i].ParentBoneIndex].Transform, false);
                    _bindPoses[i].Transform.SetParent(_bindPoses[_bones[i].ParentBoneIndex].Transform, false);
                }
            }

            if (_enablePhysicsCapsules)
            {
                _capsules = new List <OVRBoneCapsule>(new OVRBoneCapsule[skeleton.NumBoneCapsules]);
                Capsules  = _capsules.AsReadOnly();

                for (int i = 0; i < skeleton.NumBoneCapsules; ++i)
                {
                    var       capsule = skeleton.BoneCapsules[i];
                    Transform bone    = Bones[capsule.BoneIndex].Transform;

                    var capsuleRigidBodyGO = new GameObject((_bones[capsule.BoneIndex].Id).ToString() + "_CapsuleRigidBody");
                    capsuleRigidBodyGO.transform.SetParent(_capsulesGO.transform, false);
                    capsuleRigidBodyGO.transform.localPosition = bone.position;
                    capsuleRigidBodyGO.transform.localRotation = bone.rotation;

                    var capsuleRigidBody = capsuleRigidBodyGO.AddComponent <Rigidbody>();
                    capsuleRigidBody.mass        = 1.0f;
                    capsuleRigidBody.isKinematic = true;
                    capsuleRigidBody.useGravity  = false;
#if UNITY_2018_3_OR_NEWER
                    capsuleRigidBody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
#else
                    capsuleRigidBody.collisionDetectionMode = CollisionDetectionMode.Continuous;
#endif

                    var capsuleColliderGO = new GameObject((_bones[capsule.BoneIndex].Id).ToString() + "_CapsuleCollider");
                    capsuleColliderGO.transform.SetParent(capsuleRigidBodyGO.transform, false);
                    var capsuleCollider = capsuleColliderGO.AddComponent <CapsuleCollider>();
                    var p0    = capsule.Points[0].FromFlippedZVector3f();
                    var p1    = capsule.Points[1].FromFlippedZVector3f();
                    var delta = p1 - p0;
                    var mag   = delta.magnitude;
                    var rot   = Quaternion.FromToRotation(capsuleRigidBodyGO.transform.localRotation * Vector3.right, delta);
                    capsuleCollider.radius    = capsule.Radius;
                    capsuleCollider.height    = mag + capsule.Radius * 2.0f;
                    capsuleCollider.isTrigger = false;
                    capsuleCollider.direction = 0;
                    capsuleColliderGO.transform.localPosition = p0;
                    capsuleColliderGO.transform.localRotation = rot;
                    capsuleCollider.center = Vector3.right * mag * 0.5f;

                    _capsules[i] = new OVRBoneCapsule(capsule.BoneIndex, capsuleRigidBody, capsuleCollider);
                }
            }

            _isInitialized = true;
        }
    }