Exemple #1
0
    private bool GetUserBoneLength(KinectManager manager, KinectInterop.JointType baseJoint, KinectInterop.JointType endJoint, float scaleFactor, ref float length)
    {
        length = 0f;

        Vector3 vPos1 = GetJointPosition(manager, (int)baseJoint);
        Vector3 vPos2 = GetJointPosition(manager, (int)endJoint);

        if (vPos1 != Vector3.zero && vPos2 != Vector3.zero)
        {
            length = (vPos2 - vPos1).magnitude * scaleFactor;
            return(true);
        }

        return(false);
    }
    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (userIndex != playerIndex)
        {
            return(false);
        }

        switch (gesture)
        {
        case KinectGestures.Gestures.SwipeLeft:
            currentIndex++;
            if (currentIndex >= maskCount)
            {
                currentIndex = 0;
            }
            break;

        case KinectGestures.Gestures.SwipeRight:
            currentIndex--;
            if (currentIndex < 0)
            {
                currentIndex = maskCount - 1;
            }
            break;
        }

        return(true);
    }
 // returns the index of the given joint in joint's array or -1 if joint is not applicable
 public int GetJointIndex(KinectInterop.JointType joint)
 {
     return((int)joint);
 }
 void Start()
 {
     if (isLeftHand) {
         handJoint = KinectInterop.JointType.HandTipLeft;
     } else {
         handJoint = KinectInterop.JointType.HandTipRight;
     }
 }
Exemple #5
0
 public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture,
                              KinectInterop.JointType joint, Vector3 screenPos)
 {
     return(true);
 }
        // Update is called once per frame
        void Update()
        {
            KinectManager kinectManager = KinectManager.Instance;

            if (!flag && char_animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1)
            {
                /**************************This is one way of storing pose data******************************/

                /*
                 * for (int i = 0; i < poseJoints.Count; i++)
                 * {
                 *  savePose[i] = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(poseJoints[i], false)).position;
                 * }
                 *
                 * PoseArchive.ArraySavedPoses[Exercise_index] = savePose;
                 * PoseArchive.saveCounter++;
                 * flag = true;
                 * //Debug.Log("Exercise number   " + Exercise_index + "   has one of its bone transform value as " + savePose[4]);
                 */


                ///Using Head as reference point and calculating joint positions with respect to head
                ///Save them in the array to be referenced later for comparision with user motion data

                /*
                 * Transform headTransform = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(Head, false));
                 *
                 * for (int i = 0; i < poseJoints.Count; i++)
                 * {
                 *  KinectInterop.JointType joint = poseJoints[i];
                 *
                 *  Transform jointTransform = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, false));
                 *
                 *  if(jointTransform != null && headTransform != null)
                 *  {
                 *      savePose[i] = (headTransform.position - jointTransform.position).normalized;
                 *  }
                 * }*/


                /****************************This is another way of storing joint data of a pose************************/

                ///Get joint data wrt the next joints

                for (int i = 0; i < poseJoints.Count; i++)
                {
                    KinectInterop.JointType joint     = poseJoints[i];
                    KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint);

                    int jointCount = kinectManager.GetJointCount();
                    if (nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < jointCount)
                    {
                        Transform avatarTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, false));
                        Transform avatarTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, false));

                        if (avatarTransform1 != null && avatarTransform2 != null)
                        {
                            savePose[i] = (avatarTransform2.position - avatarTransform1.position).normalized;
                            //Debug.Log(savePose[i]);
                        }
                    }
                }

                //Store the poses captured in an array to be referenced later
                PoseArchive.ArraySavedPoses[Exercise_index] = savePose;
                PoseArchive.saveCounter++;
                flag = true;
                //Debug.Log("Exercise number   " + Exercise_index + "   has one of its bone transform value as " + savePose[4]);
            }
        }
 public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
 {
     throw new NotImplementedException();
 }
    private bool GetUserBoneLength(KinectManager manager, KinectInterop.JointType baseJoint, KinectInterop.JointType endJoint, float scaleFactor, ref float length)
    {
        length = 0f;

        if (manager && manager.IsJointTracked(currentUserId, (int)baseJoint) &&
            manager.IsJointTracked(currentUserId, (int)endJoint))
        {
            Vector3 vPos1 = manager.GetJointPosition(currentUserId, (int)baseJoint);
            Vector3 vPos2 = manager.GetJointPosition(currentUserId, (int)endJoint);

            length = (vPos2 - vPos1).magnitude * scaleFactor;

            return(true);
        }

        return(false);
    }
 public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
 {
     //throw new NotImplementedException();
     return(true);
 }
 public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// 当一个手势识别完成后被调用
        /// </summary>
        /// <returns>true</returns>
        /// <c>false</c>
        /// <param name="userId">被识别者的ID</param>
        /// <param name="userIndex">被识别者的序号</param>
        /// <param name="gesture">被识别到的手势类型</param>
        /// <param name="joint">被识别到的关节类型</param>
        /// <param name="screenPos">视图坐标的单位向量</param>
        public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture,
                                     KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (progressDisplayed)
        {
            return(true);
        }


        string sGestureText = gesture + " detected";

        if (gestureInfo != null)
        {
            gestureInfo.GetComponent <Text>().text = sGestureText;
        }
                // 可以在这里写识别到的所有手势,完成之后去调用某个方法,
                // 但为了右面可能不止一个场景可能使用到相同的手势,所以使用bool跳出去,在使用到的时候去写完成动作之后的方法
                switch (gesture)
        {
        case KinectGestures.Gestures.None:
            break;

        case KinectGestures.Gestures.RaiseRightHand:
            RaiseRightHand = true;
            break;

        case KinectGestures.Gestures.RaiseLeftHand:
            RaiseLeftHand = true;
            break;

        case KinectGestures.Gestures.Psi:
            Psi = true;
            break;

        case KinectGestures.Gestures.Tpose:
            Tpose = true;
            break;

        case KinectGestures.Gestures.Stop:
            Stop = true;
            break;

        case KinectGestures.Gestures.Wave:
            Wave = true;
            break;

        case KinectGestures.Gestures.SwipeLeft:
            SwipeLeft = true;
            break;

        case KinectGestures.Gestures.SwipeRight:
            SwipeRight = true;
            break;

        case KinectGestures.Gestures.SwipeUp:
            SwipeUp = true;
            break;

        case KinectGestures.Gestures.SwipeDown:
            SwipeDown = true;
            break;

        case KinectGestures.Gestures.ZoomIn:
            ZoomIn = true;
            break;

        case KinectGestures.Gestures.ZoomOut:
            ZoomOut = true;
            break;

        case KinectGestures.Gestures.Wheel:
            Wheel = true;
            break;

        case KinectGestures.Gestures.Jump:
            Jump = true;
            break;

        case KinectGestures.Gestures.Squat:
            Squat = true;
            break;

        case KinectGestures.Gestures.Push:
            Push = true;
            break;

        case KinectGestures.Gestures.Pull:
            Pull = true;
            break;

        case KinectGestures.Gestures.ShoulderLeftFront:
            break;

        case KinectGestures.Gestures.ShoulderRightFront:
            break;

        case KinectGestures.Gestures.LeanLeft:
            break;

        case KinectGestures.Gestures.LeanRight:
            break;

        case KinectGestures.Gestures.LeanForward:
            break;

        case KinectGestures.Gestures.LeanBack:
            break;

        case KinectGestures.Gestures.KickLeft:
            break;

        case KinectGestures.Gestures.KickRight:
            break;

        case KinectGestures.Gestures.Run:
            break;

        case KinectGestures.Gestures.RaisedRightHorizontalLeftHand:
            break;

        case KinectGestures.Gestures.RaisedLeftHorizontalRightHand:
            break;

        case KinectGestures.Gestures.UserGesture1:
            break;

        case KinectGestures.Gestures.UserGesture2:
            break;

        case KinectGestures.Gestures.UserGesture3:
            break;

        case KinectGestures.Gestures.UserGesture4:
            break;

        case KinectGestures.Gestures.UserGesture5:
            break;

        case KinectGestures.Gestures.UserGesture6:
            break;

        case KinectGestures.Gestures.UserGesture7:
            break;

        case KinectGestures.Gestures.UserGesture8:
            break;

        case KinectGestures.Gestures.UserGesture9:
            break;

        case KinectGestures.Gestures.UserGesture10:
            break;

        default:
            break;
        }

        return(true);
    }
        /// <summary>
        /// Invoked when a gesture is in progress.
        /// </summary>
        /// <param name="userId">被识别者的id</param>
        /// <param name="userIndex">被识别者的序号</param>
        /// <param name="gesture">手势类型</param>
        /// <param name="progress">手势识别的进度,可以认为是相似度。范围是[0,1]</param>
        /// <param name="joint">关节类型</param>
        /// <param name="screenPos">视图坐标的单位向量</param>
        public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture,
                                      float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
                /*
                 *          * 主要将一些需要动态监测的手势放在这个函数下
                 *          * 比如说缩放、滚轮都是依据你两手之间的距离来判断应该缩放或旋转多少度
                 *          */


                //监测缩放,如果相似度大于50%
                // the gestures are allowed for the primary user only
                if (userIndex != playerIndex)
        {
            return;
        }


        if (gesture == KinectGestures.Gestures.ZoomOut)
        {
            if (progress > 0.5f)
            {
                ZoomOut    = true;
                zoomFactor = screenPos.z;


                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} factor: {1:F0}%", gesture, screenPos.z * 100f);
                    gestureInfo.text = sGestureText;


                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                ZoomOut = false;
            }
        }
        else if (gesture == KinectGestures.Gestures.ZoomIn)
        {
            if (progress > 0.5f)
            {
                ZoomIn     = true;
                zoomFactor = screenPos.z;


                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} factor: {1:F0}%", gesture, screenPos.z * 100f);
                    gestureInfo.text = sGestureText;


                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                ZoomIn = false;
            }
        }
        else if ((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft ||
                  gesture == KinectGestures.Gestures.LeanRight) && progress > 0.5f)
        {
            if (gestureInfo != null)
            {
                string sGestureText = string.Format("{0} - {1:F0} degrees", gesture, screenPos.z);
                gestureInfo.GetComponent <Text>().text = sGestureText;


                progressDisplayed   = true;
                progressGestureTime = Time.realtimeSinceStartup;
            }
        }
        else if (gesture == KinectGestures.Gestures.Run && progress > 0.5f)
        {
            if (gestureInfo != null)
            {
                string sGestureText = string.Format("{0} - progress: {1:F0}%", gesture, progress * 100);
                gestureInfo.GetComponent <Text>().text = sGestureText;


                progressDisplayed   = true;
                progressGestureTime = Time.realtimeSinceStartup;
            }
        }
    }
    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (gesture == KinectGestures.Gestures.Push)
        {
            text.text += "做push";
        }

        if (gesture == KinectGestures.Gestures.SwipeRight)
        {
            text.text += "做SwipeRight";
        }

        return(true);
    }
Exemple #14
0
    /// <summary>
    /// Invoked when a gesture is in progress.
    /// </summary>
    /// <param name="userId">User ID</param>
    /// <param name="userIndex">User index</param>
    /// <param name="gesture">Gesture type</param>
    /// <param name="progress">Gesture progress [0..1]</param>
    /// <param name="joint">Joint type</param>
    /// <param name="screenPos">Normalized viewport position</param>
    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture,
                                  float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        // the gestures are allowed for the primary user only
        KinectManager manager = KinectManager.Instance;

        if (!manager || (userId != manager.GetPrimaryUserID()))
        {
            return;
        }

        if (gesture == KinectGestures.Gestures.ZoomOut)
        {
            if (progress > 0.5f)
            {
                zoomOut    = true;
                zoomFactor = screenPos.z;

                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} - {1:F0}%", gesture, screenPos.z * 100f);
                    gestureInfo.GetComponent <GUIText>().text = sGestureText;

                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                zoomOut = false;
            }
        }
        else if (gesture == KinectGestures.Gestures.ZoomIn)
        {
            if (progress > 0.5f)
            {
                zoomIn     = true;
                zoomFactor = screenPos.z;

                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} factor: {1:F0}%", gesture, screenPos.z * 100f);
                    gestureInfo.GetComponent <GUIText>().text = sGestureText;

                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                zoomIn = false;
            }
        }
        else if (gesture == KinectGestures.Gestures.Wheel)
        {
            if (progress > 0.5f)
            {
                wheel      = true;
                wheelAngle = screenPos.z;

                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("Wheel angle: {0:F0} degrees", screenPos.z);
                    //gestureInfo.GetComponent<GUIText>().text = sGestureText;  // Wenchuan

                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                wheel = false;
            }
        }
    }
 public bool GestureCancelled(ulong userId, int userIndex, GestureType gesture, KinectInterop.JointType joint)
 {
     return(true);
 }
    /// <summary>
    /// Updates the avatar each frame.
    /// </summary>
    /// <param name="UserID">User ID</param>
    public void UpdateAvatar(Int64 UserID)
    {
        PlayerDetected = true;
        if (!gameObject.activeInHierarchy)
        {
            return;
        }

        // Get the KinectManager instance
        if (kinectManager == null)
        {
            kinectManager    = KinectManager.Instance;
            ColorImageWidth  = kinectManager.GetColorImageWidth();
            ColorImageHeight = kinectManager.GetColorImageHeight();
        }
        // for rendering the head
        //****************************************
        //
        if (rawImage)
        {
            rawImage.texture = kinectManager.GetUsersClrTex();
            int HeadIndex = 3;// (int)TrackedHead;


            if (kinectManager.IsJointTracked(UserID, HeadIndex))
            {
                Vector3 posJoint = kinectManager.GetRawSkeletonJointPos(UserID, HeadIndex);

                if (posJoint != Vector3.zero)
                {
                    // 3d position to depth
                    Vector2 posDepth = kinectManager.GetDepthMapPosForJointPos(posJoint);

                    // depth pos to color pos
                    Vector2 posColor = kinectManager.GetColorMapPosForDepthPos(posDepth);

                    float UVrecPosX = (float)posColor.x / ColorImageWidth;
                    float UVrecPosY = (float)posColor.y / ColorImageHeight;
                    UVrecPosX -= HeadImageWidth / 2.5f;
                    UVrecPosY -= HeadImageHeight / 2f;

                    //						Vector3 localPos = new Vector3(scaleX * 10f - 5f, 0f, scaleY * 10f - 5f); // 5f is 1/2 of 10f - size of the plane
                    //						Vector3 vPosOverlay = backgroundImage.transform.TransformPoint(localPos);
                    //Vector3 vPosOverlay = BottomLeft + ((vRight * scaleX) + (vUp * scaleY));

                    rawImage.uvRect = new Rect(Mathf.Lerp(rawImage.uvRect.x, UVrecPosX, smoothFactor * Time.deltaTime), Mathf.Lerp(rawImage.uvRect.y, UVrecPosY, smoothFactor * Time.deltaTime), HeadImageHeight, HeadImageWidth);
                    //	Vector3 vPosOverlay = Camera.main.ViewportToWorldPoint(new Vector3(scaleX, scaleY, distanceToCamera));
                    //	OverlayObject.transform.position = Vector3.Lerp(OverlayObject.transform.position, vPosOverlay, smoothFactor * Time.deltaTime);
                }
            }
        }
        //****************************		// move the avatar to its Kinect position
        if (!externalRootMotion)
        {
            MoveAvatar(UserID);
        }

        // get the left hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          leftHandState = kinectManager.GetLeftHandState(UserID);
            InteractionManager.HandEventType leftHandEvent = InteractionManager.HandStateToEvent(leftHandState, lastLeftHandEvent);

            if (leftHandEvent != InteractionManager.HandEventType.None)
            {
                lastLeftHandEvent = leftHandEvent;
            }
        }

        // get the right hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          rightHandState = kinectManager.GetRightHandState(UserID);
            InteractionManager.HandEventType rightHandEvent = InteractionManager.HandStateToEvent(rightHandState, lastRightHandEvent);

            if (rightHandEvent != InteractionManager.HandEventType.None)
            {
                lastRightHandEvent = rightHandEvent;
            }
        }

        // rotate the avatar bones
        for (var boneIndex = 0; boneIndex < bones.Length; boneIndex++)
        {
            if (!bones[boneIndex])
            {
                continue;
            }

            if (boneIndex2JointMap.ContainsKey(boneIndex))
            {
                KinectInterop.JointType joint = !mirroredMovement ? boneIndex2JointMap[boneIndex] : boneIndex2MirrorJointMap[boneIndex];
                TransformBone(UserID, joint, boneIndex, !mirroredMovement);
            }
            else if (specIndex2JointMap.ContainsKey(boneIndex))
            {
                // special bones (clavicles)
                List <KinectInterop.JointType> alJoints = !mirroredMovement ? specIndex2JointMap[boneIndex] : specIndex2MirrorMap[boneIndex];

                if (alJoints.Count >= 2)
                {
                    //Debug.Log(alJoints[0].ToString());
                    Vector3 baseDir = alJoints[0].ToString().EndsWith("Left") ? Vector3.left : Vector3.right;
                    TransformSpecialBone(UserID, alJoints[0], alJoints[1], boneIndex, baseDir, !mirroredMovement);
                }
            }
        }
    }
Exemple #17
0
    // Update the avatar each frame.
    public void UpdateAvatar(Int64 UserID)
    {
        if (!transform.gameObject.activeInHierarchy)
        {
            return;
        }

        // Get the KinectManager instance
        if (kinectManager == null)
        {
            kinectManager = KinectManager.Instance;
        }

        // move the avatar to its Kinect position
        MoveAvatar(UserID);

//		bool flipJoint = !mirroredMovement;
//
//		// Update Head, Neck, Spine, and Hips
//		TransformBone(UserID, KinectInterop.JointType.SpineBase, 0, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.SpineMid, 1, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.SpineShoulder, 2, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.Neck, 3, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.Head, 4, flipJoint);
//
//		// Beyond this, switch the arms and legs.
//
//		// Left Arm --> Right Arm
//		TransformSpecialBone(UserID, KinectInterop.JointType.ShoulderLeft, KinectInterop.JointType.SpineShoulder, !mirroredMovement ? 25 : 26, Vector3.left, flipJoint);  // clavicle left
//		TransformBone(UserID, KinectInterop.JointType.ShoulderLeft, !mirroredMovement ? 5 : 11, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.ElbowLeft, !mirroredMovement ? 6 : 12, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.WristLeft, !mirroredMovement ? 7 : 13, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.HandLeft, !mirroredMovement ? 8 : 14, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.HandTipLeft, !mirroredMovement ? 9 : 15, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.ThumbLeft, !mirroredMovement ? 10 : 16, flipJoint);
//
//		// Right Arm --> Left Arm
//		TransformSpecialBone(UserID, KinectInterop.JointType.ShoulderRight, KinectInterop.JointType.SpineShoulder, !mirroredMovement ? 26 : 25, Vector3.right, flipJoint);  // clavicle right
//		TransformBone(UserID, KinectInterop.JointType.ShoulderRight, !mirroredMovement ? 11 : 5, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.ElbowRight, !mirroredMovement ? 12 : 6, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.WristRight, !mirroredMovement ? 13 : 7, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.HandRight, !mirroredMovement ? 14 : 8, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.HandTipRight, !mirroredMovement ? 15 : 9, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.ThumbRight, !mirroredMovement ? 16 : 10, flipJoint);
//
//		// Left Leg --> Right Leg
//		TransformBone(UserID, KinectInterop.JointType.HipLeft, !mirroredMovement ? 17 : 21, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.KneeLeft, !mirroredMovement ? 18 : 22, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.AnkleLeft, !mirroredMovement ? 19 : 23, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.FootLeft, !mirroredMovement ? 20 : 24, flipJoint);
//
//		// Right Leg --> Left Leg
//		TransformBone(UserID, KinectInterop.JointType.HipRight, !mirroredMovement ? 21 : 17, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.KneeRight, !mirroredMovement ? 22 : 18, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.AnkleRight, !mirroredMovement ? 23 : 19, flipJoint);
//		TransformBone(UserID, KinectInterop.JointType.FootRight, !mirroredMovement ? 24 : 20, flipJoint);

        for (var boneIndex = 0; boneIndex < bones.Length; boneIndex++)
        {
            if (!bones[boneIndex])
            {
                continue;
            }

            if (boneIndex2JointMap.ContainsKey(boneIndex))
            {
                KinectInterop.JointType joint = !mirroredMovement ? boneIndex2JointMap[boneIndex] : boneIndex2MirrorJointMap[boneIndex];
                TransformBone(UserID, joint, boneIndex, !mirroredMovement);
            }
            else if (specIndex2JointMap.ContainsKey(boneIndex))
            {
                // special bones (clavicles)
                List <KinectInterop.JointType> alJoints = !mirroredMovement ? specIndex2JointMap[boneIndex] : specIndex2MirrorJointMap[boneIndex];

                if (alJoints.Count >= 2)
                {
                    //Debug.Log(alJoints[0].ToString());
                    Vector3 baseDir = alJoints[0].ToString().EndsWith("Left") ? Vector3.left : Vector3.right;
                    TransformSpecialBone(UserID, alJoints[0], alJoints[1], boneIndex, baseDir, !mirroredMovement);
                }
            }
        }
    }
    /// <summary>
    /// Updates the avatar each frame.
    /// </summary>
    /// <param name="UserID">User ID</param>
    public void UpdateAvatar(Int64 UserID)
    {
        if (!gameObject.activeInHierarchy)
        {
            return;
        }

        // Get the KinectManager instance
        if (kinectManager == null)
        {
            kinectManager = KinectManager.Instance;
        }

        // move the avatar to its Kinect position
        if (!externalRootMotion)
        {
            MoveAvatar(UserID);
        }

        // get the left hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          leftHandState = kinectManager.GetLeftHandState(UserID);
            InteractionManager.HandEventType leftHandEvent = InteractionManager.HandStateToEvent(leftHandState, lastLeftHandEvent);

            if (leftHandEvent != InteractionManager.HandEventType.None)
            {
                lastLeftHandEvent = leftHandEvent;
            }
        }

        // get the right hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          rightHandState = kinectManager.GetRightHandState(UserID);
            InteractionManager.HandEventType rightHandEvent = InteractionManager.HandStateToEvent(rightHandState, lastRightHandEvent);

            if (rightHandEvent != InteractionManager.HandEventType.None)
            {
                lastRightHandEvent = rightHandEvent;
            }
        }

        // rotate the avatar bones
        for (var boneIndex = 0; boneIndex < bones.Length; boneIndex++)
        {
            if (!bones[boneIndex])
            {
                continue;
            }

            if (boneIndex2JointMap.ContainsKey(boneIndex))
            {
                KinectInterop.JointType joint = !mirroredMovement ? boneIndex2JointMap[boneIndex] : boneIndex2MirrorJointMap[boneIndex];
                TransformBone(UserID, joint, boneIndex, !mirroredMovement);
            }
            else if (specIndex2JointMap.ContainsKey(boneIndex))
            {
                // special bones (clavicles)
                List <KinectInterop.JointType> alJoints = !mirroredMovement ? specIndex2JointMap[boneIndex] : specIndex2MirrorMap[boneIndex];

                if (alJoints.Count >= 2)
                {
                    //Debug.Log(alJoints[0].ToString());
                    Vector3 baseDir = alJoints[0].ToString().EndsWith("Left") ? Vector3.left : Vector3.right;
                    TransformSpecialBone(UserID, alJoints[0], alJoints[1], boneIndex, baseDir, !mirroredMovement);
                }
            }
        }

        //update relative position info
        updatePositionInfo_(UserID);
    }
Exemple #19
0
        /// <summary>
        /// 获取关节信息
        /// </summary>
        /// <returns></returns>
        public PlayerGestureJointData GetPlayerGestureJointData(GestureType gestureType, KinectInterop.JointType jointType)
        {
            PlayerGestureJointData playerGestureJoints = new PlayerGestureJointData();

            if (EndJointDirection.ContainsKey(jointType))
            {
                switch (gestureType)
                {
                case GestureType.End:
                    if (EndJointDirection.ContainsKey(jointType))
                    {
                        playerGestureJoints = EndJointDirection[jointType];
                    }
                    break;

                case GestureType.Start:
                    if (StartJointDirection.ContainsKey(jointType))
                    {
                        playerGestureJoints = StartJointDirection[jointType];
                    }
                    break;

                default:
                    break;
                }
            }

            return(playerGestureJoints);
        }
    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (userIndex != playerIndex)
        {
            return(false);
        }

        switch (gesture)
        {
        case KinectGestures.Gestures.RaiseRightHand:
            ActivateNextModelSelector();
            break;

        case KinectGestures.Gestures.RaiseLeftHand:
            ActivatePrevModelSelector();
            break;

        case KinectGestures.Gestures.SwipeLeft:
            if (modelSelector)
            {
                modelSelector.SelectNextModel();
            }
            break;

        case KinectGestures.Gestures.SwipeRight:
            if (modelSelector)
            {
                modelSelector.SelectPrevModel();
            }
            break;
        }

        return(true);
    }
Exemple #21
0
 public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
 {
     if (ifDetectGesture)
     {
         if (gesture == KinectGestures.Gestures.SwipeRight)
         {
             messageDispatcher.dispatchMessage(delay, EntityType.FirstPanelEntity, MessageType.Msg_SwipeRight, new Vector2(0, 0), 0);
             messageDispatcher.dispatchMessage(delay, EntityType.GameOverEntity, MessageType.Msg_SwipeRight, new Vector2(0, 0), 0);
         }
         else if (gesture == KinectGestures.Gestures.SwipeLeft)
         {
             messageDispatcher.dispatchMessage(delay, EntityType.FirstPanelEntity, MessageType.Msg_SwipeLeft, new Vector2(0, 0), 0);
             messageDispatcher.dispatchMessage(delay, EntityType.GameOverEntity, MessageType.Msg_SwipeLeft, new Vector2(0, 0), 0);
         }
         else if (gesture == KinectGestures.Gestures.Push)
         {
             messageDispatcher.dispatchMessage(delay, EntityType.FirstPanelEntity, MessageType.Msg_Push, new Vector2(0, 0), 0);
             messageDispatcher.dispatchMessage(delay, EntityType.GameOverEntity, MessageType.Msg_Push, new Vector2(0, 0), 0);
         }
     }
     return(true);
 }
    /// <summary>
    /// Invoked when a gesture is in progress.
    /// </summary>
    /// <param name="userId">User ID</param>
    /// <param name="userIndex">User index</param>
    /// <param name="gesture">Gesture type</param>
    /// <param name="progress">Gesture progress [0..1]</param>
    /// <param name="joint">Joint type</param>
    /// <param name="screenPos">Normalized viewport position</param>
    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture,
                                  float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        // the gestures are allowed for the primary user only
        if (userIndex != playerIndex)
        {
            return;
        }

        if (gesture == KinectGestures.Gestures.ZoomOut)
        {
            if (progress > 0.5f)
            {
                zoomOut    = true;
                zoomFactor = screenPos.z;

                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} factor: {1:F0}%", gesture, screenPos.z * 100f);
                    gestureInfo.text = sGestureText;

                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                zoomOut = false;
            }
        }
        else if (gesture == KinectGestures.Gestures.ZoomIn)
        {
            if (progress > 0.5f)
            {
                zoomIn     = true;
                zoomFactor = screenPos.z;

                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} factor: {1:F0}%", gesture, screenPos.z * 100f);
                    gestureInfo.text = sGestureText;

                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                zoomIn = false;
            }
        }
        else if (gesture == KinectGestures.Gestures.Wheel)
        {
            if (progress > 0.5f)
            {
                wheel      = true;
                wheelAngle = screenPos.z;

                if (gestureInfo != null)
                {
                    string sGestureText = string.Format("Wheel angle: {0:F0} deg.", screenPos.z);
                    gestureInfo.text = sGestureText;

                    progressDisplayed   = true;
                    progressGestureTime = Time.realtimeSinceStartup;
                }
            }
            else
            {
                wheel = false;
            }
        }
    }
Exemple #23
0
    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (userIndex != playerIndex)
        {
            return(false);
        }

        // save in manager
        PlayerManager.Instance.SetLastGesture(userIndex, gesture);

        Debug.Log(PlayerManager.Instance.GetLastGesture(userIndex) + "@" + Time.timeSinceLevelLoad);

        // raise event
        if (GestureCompletedEvent != null)
        {
            GestureCompletedEvent(gesture, userId);
        }

        return(true);
    }
    // returns the next joint in the hierarchy, as to the given joint
    public KinectInterop.JointType GetNextJoint(KinectInterop.JointType joint)
    {
        switch (joint)
        {
        case KinectInterop.JointType.SpineBase:
            return(KinectInterop.JointType.SpineMid);

        case KinectInterop.JointType.SpineMid:
            return(KinectInterop.JointType.SpineShoulder);

        case KinectInterop.JointType.SpineShoulder:
            return(KinectInterop.JointType.Neck);

        case KinectInterop.JointType.Neck:
            return(KinectInterop.JointType.Head);

        case KinectInterop.JointType.ShoulderLeft:
            return(KinectInterop.JointType.ElbowLeft);

        case KinectInterop.JointType.ElbowLeft:
            return(KinectInterop.JointType.WristLeft);

        case KinectInterop.JointType.WristLeft:
            return(KinectInterop.JointType.HandLeft);

        case KinectInterop.JointType.HandLeft:
            return(KinectInterop.JointType.HandTipLeft);

        case KinectInterop.JointType.ShoulderRight:
            return(KinectInterop.JointType.ElbowRight);

        case KinectInterop.JointType.ElbowRight:
            return(KinectInterop.JointType.WristRight);

        case KinectInterop.JointType.WristRight:
            return(KinectInterop.JointType.HandRight);

        case KinectInterop.JointType.HandRight:
            return(KinectInterop.JointType.HandTipRight);

        case KinectInterop.JointType.HipLeft:
            return(KinectInterop.JointType.KneeLeft);

        case KinectInterop.JointType.KneeLeft:
            return(KinectInterop.JointType.AnkleLeft);

        case KinectInterop.JointType.AnkleLeft:
            return(KinectInterop.JointType.FootLeft);

        case KinectInterop.JointType.HipRight:
            return(KinectInterop.JointType.KneeRight);

        case KinectInterop.JointType.KneeRight:
            return(KinectInterop.JointType.AnkleRight);

        case KinectInterop.JointType.AnkleRight:
            return(KinectInterop.JointType.FootRight);
        }

        return(joint);         // in case of end joint - Head, HandTipLeft, HandTipRight, FootLeft, FootRight
    }
Exemple #25
0
    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (userIndex != playerIndex)
        {
            return;
        }

        // shield and hadouken charge are continuous gestures -> check for begin
        if (gesture == KinectGestures.Gestures.ShieldBegin || gesture == KinectGestures.Gestures.HadoukenCharge)
        {
            // at start progress is 0.1
            if (progress <= 0.1f)
            {
                // save in manager
                PlayerManager.Instance.SetLastGesture(userIndex, gesture);

                Debug.Log(PlayerManager.Instance.GetLastGesture(userIndex) + "@" + Time.timeSinceLevelLoad);

                // raise event
                if (GestureCompletedEvent != null)
                {
                    GestureCompletedEvent(gesture, userId);
                }
            }
        }

        // raise event
        if (GestureInProgressEvent != null)
        {
            GestureInProgressEvent(gesture, progress, userId);
        }
    }
Exemple #26
0
    private bool GetModelBoneLength(AvatarController avatarController, KinectInterop.JointType baseJoint, KinectInterop.JointType endJoint, ref float length)
    {
        length = 0f;

        if (avatarController)
        {
            Transform joint1 = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(baseJoint, false));
            Transform joint2 = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(endJoint, false));

            if (joint1 && joint2)
            {
                length = (joint2.position - joint1.position).magnitude;
                return(true);
            }
        }

        return(false);
    }
Exemple #27
0
    public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
    {
        if (userIndex != playerIndex)
        {
            return(false);
        }

        // shield is continous -> check for cancelled, the end of the gesture
        if (gesture == KinectGestures.Gestures.ShieldBegin)
        {
            // save in manager
            PlayerManager.Instance.SetLastGesture(userIndex, KinectGestures.Gestures.ShieldEnd);
            // raise event shield end manually
            if (GestureCompletedEvent != null)
            {
                GestureCompletedEvent(KinectGestures.Gestures.ShieldEnd, userId);
            }

            Debug.Log(PlayerManager.Instance.GetLastGesture(userIndex) + "@" + Time.timeSinceLevelLoad);
        }
        else
        {
            // otherwise last gesture none
            PlayerManager.Instance.SetLastGesture(userIndex, KinectGestures.Gestures.None);
            // raise event
            if (GestureCancelledEvent != null)
            {
                GestureCancelledEvent(gesture, userId);
            }
        }

        return(true);
    }
 public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
 {
     // nothing to do here
 }
Exemple #29
0
    void Update()
    {
        if (!isSaving)
        {
            return;
        }

        // check the start time
        if (saveStartTime < 0f)
        {
            saveStartTime = Time.time;
        }

        // get the joint position
        KinectManager manager = KinectManager.Instance;

        if (manager && manager.IsInitialized())
        {
            if (manager.IsUserDetected())
            {
                long userId = manager.GetPrimaryUserID();

                if (start)  // write motion data
                {
                    for (int k = 0; k < 25; k++)
                    {
                        if (manager.IsJointTracked(userId, k))                          // else ????????????????
                        {
                            // output the joint position for easy tracking
                            Vector3 jointPos = manager.GetJointPosition(userId, k);
                            jointPosition = jointPos;

                            if ((secondsToSave == 0f) || ((Time.time - saveStartTime) <= secondsToSave))
                            {
                                using (StreamWriter writer = File.AppendText(saveFilePath))
                                {
                                    string sLine = jointPos.x.ToString() + " " + jointPos.y.ToString() + " " + jointPos.z.ToString();   // Wenchuan
                                    writer.WriteLine(sLine);
                                }
                            }
                        }
                    }
                }
                else  // check whether start saving data
                {
                    KinectInterop.JointType Hand = KinectInterop.JointType.HandRight;
                    KinectInterop.JointType Head = KinectInterop.JointType.Head;
                    if (manager.IsJointTracked(userId, (int)Hand) && manager.IsJointTracked(userId, (int)Head))
                    {
                        Vector3 HandPos = manager.GetJointPosition(userId, (int)Hand);
                        Vector3 HeadPos = manager.GetJointPosition(userId, (int)Head);
                        if (HandPos.y > HeadPos.y)  // raise the right hand
                        {
                            flagRaiseHand = true;
                        }
                    }
                    if (flagRaiseHand && !start)
                    {
                        count++;
                        if (count == FrameRate * 3)  // start after 3 second
                        {
                            start = true;
                        }
                    }
                }
            }
        }
    }
    public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
    {
        if (userIndex != playerIndex)
        {
            return(false);
        }

        return(true);
    }
Exemple #31
0
    // Invoked when a gesture is in progress.
    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture,
                                  float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        // the gestures are allowed for the primary user only
        KinectManager manager = KinectManager.Instance;

        if (!manager || (userId != manager.GetPrimaryUserID()))
        {
            return;
        }

        // this function is currently needed only to display gesture progress, skip it otherwise
        if (gestureInfo == null)
        {
            return;
        }

        if ((gesture == KinectGestures.Gestures.ZoomOut || gesture == KinectGestures.Gestures.ZoomIn) && progress > 0.5f)
        {
            if (gestureInfo != null)
            {
                if (gesture == KinectGestures.Gestures.ZoomIn)
                {
                    string GestureText = "Zoom in";
                    gestureInfo.GetComponent <GUIText>().text = GestureText;
                    zoomIn = true;
                }
                else if (gesture == KinectGestures.Gestures.ZoomOut)
                {
                    string GestureText = "Zoom out";
                    gestureInfo.GetComponent <GUIText>().text = GestureText;
                    zoomOut = true;
                }
                //string sGestureText = string.Format("{0} - {1:F0}%", gesture, screenPos.z * 100f);
                //gestureInfo.GetComponent<GUIText>().text = sGestureText;

                progressDisplayed   = true;
                progressGestureTime = Time.realtimeSinceStartup;
            }
        }
        else if ((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft ||
                  gesture == KinectGestures.Gestures.LeanRight) && progress > 0.5f)
        {
            if (gestureInfo != null)
            {
                string sGestureText = string.Format("{0} - {1:F0} degrees", gesture, screenPos.z);
                gestureInfo.GetComponent <GUIText>().text = sGestureText;

                progressDisplayed   = true;
                progressGestureTime = Time.realtimeSinceStartup;
            }
        }
        if (gesture == KinectGestures.Gestures.LeanLeft)
        {
            leanLeft  = true;
            leanRight = false;
        }

        else if (gesture == KinectGestures.Gestures.LeanRight)
        {
            leanRight = true;
            leanLeft  = false;
        }

        if (gesture == KinectGestures.Gestures.ShoulderLeftFront)
        {
            //string sGestureText = "Shoulder Left Front";
            //gestureInfo.GetComponent<GUIText>().text = sGestureText;
        }
        else if (gesture == KinectGestures.Gestures.ShoulderRightFront)
        {
            //string sGestureText = "Shoulder Right Front";
            //gestureInfo.GetComponent<GUIText>().text = sGestureText;
        }
    }