Exemplo n.º 1
0
        /// <summary>
        /// Set the joint rotation or position (optional) based on the MRTK joint position
        /// </summary>
        /// <param name="sensorJoint">Joint type</param>
        /// <param name="movePosition">Set the position</param>
        /// <returns></returns>
        bool SetJoint(TrackedHandJoint sensorJoint, bool movePosition = false)
        {
            TrackedHandJoint prev = sensorJoint - 1;

            if ((sensorJoint == TrackedHandJoint.IndexMetacarpal) || (sensorJoint == TrackedHandJoint.MiddleMetacarpal) || (sensorJoint == TrackedHandJoint.PinkyMetacarpal) || (sensorJoint == TrackedHandJoint.RingMetacarpal) || (sensorJoint == TrackedHandJoint.ThumbMetacarpalJoint))
            {
                prev = TrackedHandJoint.Wrist;
            }

            if (_mixedRealityHand.TryGetJoint(sensorJoint, out MixedRealityPose pose) &&
                defaultRotationData.ContainsKey(sensorJoint) && (_mixedRealityHand.TryGetJoint(prev, out MixedRealityPose poseParent) || sensorJoint == TrackedHandJoint.Wrist))
            {
                int index = (int)sensorJoint;
                if (index >= 0 && index < jointObjects.Length)
                {
                    GameObject objectJoint = jointObjects[index];
                    Vector3    rot         = Vector3.zero;
                    //no parent for the wrist
                    if (poseParent != null)
                    {
                        rot = (Quaternion.Inverse(poseParent.Rotation * Quaternion.Euler(axisRot)) * pose.Rotation * Quaternion.Euler(axisRot)).eulerAngles;
                    }

                    if (movePosition && sensorJoint == TrackedHandJoint.Wrist)
                    {
                        //process the wrist as world for stability. note its default is its world as true if no parents
                        objectJoint.transform.position = pose.Position;
                        objectJoint.transform.rotation = pose.Rotation * Quaternion.Euler(defaultRotationData[sensorJoint].defaultValue);
                    }
                    else
                    {
                        //these thumb joints are rotated
                        if (sensorJoint == TrackedHandJoint.ThumbProximalJoint || sensorJoint == TrackedHandJoint.ThumbDistalJoint)
                        {
                            objectJoint.transform.localRotation = Quaternion.Euler(rot.x, -rot.z, -rot.y) * Quaternion.Euler(defaultRotationData[sensorJoint].defaultValue);
                        }
                        else
                        {
                            //this tracks the other joints but the wrist
                            objectJoint.transform.localRotation = Quaternion.Euler(rot) * Quaternion.Euler(defaultRotationData[sensorJoint].defaultValue);
                        }
                    }

                    DefaultAngleData data = defaultRotationData[sensorJoint];
                    int rIdx = (int)sensorJoint * 4;
                    _rotArrayShorts[rIdx + 0] = FloatToShort(objectJoint.transform.localRotation.x);
                    _rotArrayShorts[rIdx + 1] = FloatToShort(objectJoint.transform.localRotation.y);
                    _rotArrayShorts[rIdx + 2] = FloatToShort(objectJoint.transform.localRotation.z);
                    _rotArrayShorts[rIdx + 3] = FloatToShort(objectJoint.transform.localRotation.w);

                    if (movePosition && sensorJoint != TrackedHandJoint.Wrist)
                    {
                        objectJoint.transform.position = pose.Position;
                    }

                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
    public void OnSourceDetected(SourceStateEventData eventData)
    {
        IMixedRealityHand hand = eventData.Controller as IMixedRealityHand;

        if (hand != null)
        {
            if (hand.TryGetJoint(TrackedHandJoint.Wrist, out MixedRealityPose pose))
            {
                Vector3 positionOfWrist = pose.Position;
            }
        }
    }
Exemplo n.º 3
0
        /// <summary>
        /// Try to find the first matching hand controller and return the pose of the requested joint for that hand.
        /// </summary>
        public static bool TryGetJointPose(TrackedHandJoint joint, Handedness handedness, out MixedRealityPose pose)
        {
            IMixedRealityHand hand = FindHand(handedness);

            if (hand != null)
            {
                return(hand.TryGetJoint(joint, out pose));
            }

            pose = MixedRealityPose.ZeroIdentity;
            return(false);
        }
Exemplo n.º 4
0
        void LateUpdate()
        {
            IMixedRealityHand hand = GetController(trackedHandedness) as IMixedRealityHand;

            if (hand == null || !hand.TryGetJoint(trackedJoint, out MixedRealityPose pose))
            {
                SetChildrenActive(false);
                return;
            }
            SetChildrenActive(true);
            transform.position = pose.Position;
            transform.rotation = pose.Rotation;
        }
        /// <inheritdoc />
        public override void LateUpdate()
        {
            base.LateUpdate();

            leftHand  = null;
            rightHand = null;

            foreach (var detectedController in Service.DetectedControllers)
            {
                var hand = detectedController as IMixedRealityHand;
                if (hand != null)
                {
                    if (detectedController.ControllerHandedness == Handedness.Left)
                    {
                        if (leftHand == null)
                        {
                            leftHand = hand;
                        }
                    }
                    else if (detectedController.ControllerHandedness == Handedness.Right)
                    {
                        if (rightHand == null)
                        {
                            rightHand = hand;
                        }
                    }
                }
            }

            if (leftHand != null)
            {
                foreach (var fauxJoint in leftHandFauxJoints)
                {
                    if (leftHand.TryGetJoint(fauxJoint.Key, out MixedRealityPose pose))
                    {
                        fauxJoint.Value.SetPositionAndRotation(pose.Position, pose.Rotation);
                    }
                }
            }

            if (rightHand != null)
            {
                foreach (var fauxJoint in rightHandFauxJoints)
                {
                    if (rightHand.TryGetJoint(fauxJoint.Key, out MixedRealityPose pose))
                    {
                        fauxJoint.Value.SetPositionAndRotation(pose.Position, pose.Rotation);
                    }
                }
            }
        }
    void Update()
    {
        if (hand != null)
        {
            handsText.GetComponent <TextMeshPro>().text  = "Source detected: " + hand.ControllerHandedness + "\n";
            handsText.GetComponent <TextMeshPro>().text += "Velocity " + hand.Velocity + "\n";
            handsText.GetComponent <TextMeshPro>().text += "is pointing? " + hand.IsInPointingPose + "\n";

            if (hand.TryGetJoint(TrackedHandJoint.IndexTip, out MixedRealityPose jointPose))
            {
                handsText.GetComponent <TextMeshPro>().text += "Got joint " + jointPose.ToString();
            }
        }
    }
Exemplo n.º 7
0
        /// <summary>
        /// Determines if a hand meets the requirements for use with constraining the tracked object and determines if the
        /// palm is currently facing the user.
        /// </summary>
        /// <param name="hand">The hand to check against.</param>
        /// <returns>True if this hand should be used from tracking.</returns>
        protected override bool IsHandActive(IMixedRealityHand hand)
        {
            if (!base.IsHandActive(hand))
            {
                return(false);
            }

            MixedRealityPose pose;

            if (hand.TryGetJoint(TrackedHandJoint.Palm, out pose))
            {
                return(Vector3.Angle(pose.Up, CameraCache.Main.transform.forward) < facingThreshold);
            }

            return(true);
        }
    public bool TryGetSpecificControllerPose(Handedness handedness, TrackedHandJoint handJointToTrack, out Vector3 position, out Quaternion rotation)
    {
        bool retrieved = false;

        position = Vector3.zero;
        rotation = Quaternion.identity;

        IMixedRealityHand       currentHand       = detectedHand;
        IMixedRealityController currentController = detectedController;

        if (handedness == Handedness.Left)
        {
            currentHand       = leftHand;
            currentController = leftController;
        }
        else if (handedness == Handedness.Right)
        {
            currentHand       = rightHand;
            currentController = rightController;
        }

        if (currentHand != null && currentHand.TryGetJoint(handJointToTrack, out handPose))
        {
            position  = handPose.Position;
            rotation  = handPose.Rotation;
            retrieved = true;
        }
        else if (currentController != null)
        {
            try
            {
                position  = currentController.InputSource.Pointers[0].Position;
                rotation  = currentController.InputSource.Pointers[0].Rotation;
                retrieved = true;
            }
            catch (Exception)
            {
                retrieved = false;
            }
        }

        return(retrieved);
    }
        public Transform RequestJointTransform(TrackedHandJoint joint, Handedness handedness)
        {
            IMixedRealityHand hand = null;
            Dictionary <TrackedHandJoint, Transform> fauxJoints = null;

            if (handedness == Handedness.Left)
            {
                hand       = leftHand;
                fauxJoints = leftHandFauxJoints;
            }
            else if (handedness == Handedness.Right)
            {
                hand       = rightHand;
                fauxJoints = rightHandFauxJoints;
            }
            else
            {
                return(null);
            }

            Transform jointTransform = null;

            if (fauxJoints != null && !fauxJoints.TryGetValue(joint, out jointTransform))
            {
                jointTransform = new GameObject().transform;
                // Since this service survives scene loading and unloading, the fauxJoints it manages need to as well.
                Object.DontDestroyOnLoad(jointTransform.gameObject);
                jointTransform.name = string.Format("Joint Tracker: {1} {0}", joint, handedness);

                if (hand != null && hand.TryGetJoint(joint, out MixedRealityPose pose))
                {
                    jointTransform.SetPositionAndRotation(pose.Position, pose.Rotation);
                }

                fauxJoints.Add(joint, jointTransform);
            }

            return(jointTransform);
        }