Exemple #1
0
    public void measureEngagement()
    {
//		Debug.Log("in measure engagement");
//		Debug.Log("(faceManager.GetFaceTrackingID(p1Index)) = " + (faceManager.GetFaceTrackingID(p1Index)));
//		if(faceManager.IsTrackingFace(faceManager.GetFaceTrackingID(p1Index)))
//			{
//				Debug.Log("is tracking");
//			}
//			else
//			{
//			Debug.Log("isnt tracking");
//			}

        //Debug.Log("faceManager.IsTrackingFace(faceManager.GetFaceTrackingID(p1Index) "+ faceManager.IsTrackingFace(faceManager.GetFaceTrackingID(p1Index))+"faceManager.IsTrackingFace(faceManager.GetFaceTrackingID(p2Index)) "+faceManager.IsTrackingFace(faceManager.GetFaceTrackingID(p2Index))+"_KinectManager.IsUserDetected() "+_KinectManager.IsUserDetected());

        assignIdByPosition();
        if (faceManager.IsTrackingFace(faceManager.GetFaceTrackingID(p1Index)) && faceManager.IsTrackingFace(faceManager.GetFaceTrackingID(p2Index)) && _KinectManager.IsUserDetected())
        {
            Debug.Log("in faceManager.isTrackingFace");
            //process left player
            // allocate storage for the point cloud points in a vector
            //get the face point cloud from the FaceTrackingManager and fill up the vector
            //get the joint position for the left shoulder
            avModelVertices1 = new Vector3[faceManager.GetFaceModelVertexCount(p1Index)];
            faceManager.GetUserFaceVertices(leftId, ref avModelVertices1);
            p1shoulderpos = _KinectManager.GetJointPosition(leftId, 4);
            //get the distance between the chin and the left shoulder
            p1shoulderToChin = Mathf.Abs((((avModelVertices1[4].x) - (p1shoulderpos.x)) * (100f)));
            //get the joint position for the right shoulder
            p1rightShoulderPos = _KinectManager.GetJointPosition(leftId, 8);
            //same as above (left)
            p1leftShoulderPos = _KinectManager.GetJointPosition(leftId, 4);
            //difference of the .z positions for the shoulders (if body turns)
            p1shoulderToShoulder = (p1rightShoulderPos.z - p1leftShoulderPos.z) * 20f;
            //head distance plus shoulder distance
            p1TotalEngagement = p1shoulderToChin + p1shoulderToShoulder;
            Debug.Log("p1TotalEngagement " + p1TotalEngagement);
            // process right player
            p2rightShoulderPos   = _KinectManager.GetJointPosition(rightId, 8);
            p2leftShoulderPos    = _KinectManager.GetJointPosition(rightId, 4);
            p2shoulderToShoulder = ((p2leftShoulderPos.z) - (p2rightShoulderPos.z)) * 20f;
            //get the face point cloud from the FaceTrackingManager and fill up the vector
            avModelVertices2 = new Vector3[faceManager.GetFaceModelVertexCount(p2Index)];
            faceManager.GetUserFaceVertices(rightId, ref avModelVertices2);
            p2shoulderpos     = _KinectManager.GetJointPosition(rightId, 4);
            p2shoulderToChin  = Mathf.Abs((((avModelVertices2[4].x) - (p2shoulderpos.x)) * (100f)));
            p2TotalEngagement = (p2shoulderToChin) - (p2shoulderToShoulder);
            Debug.Log("p2TotalEngagement " + p2TotalEngagement);

            /*
             * p2rightShoulderPos=_KinectManager.GetJointPosition(rightid,8);
             * p2leftShoulderPos=_KinectManager.GetJointPosition(rightid,4);
             * p2shoulderToShoulder=((p2rightShoulderPos.z)-(p2leftShoulderPos.z))*20f;
             * avModelVertices2=new Vector3[faceManagerP2.GetFaceModelVertexCount()];
             * faceManagerP2.GetUserFaceVertices(rightid, ref avModelVertices2);
             * p2shoulderpos=_KinectManager.GetJointPosition(rightid,4);
             * p2shoulderToChin=(((avModelVertices2[4].x)-(p2shoulderpos.x))*(100f));
             * p2TotalEngagement =(p2shoulderToChin)-(p2shoulderToShoulder);
             */
            Debug.Log("p1TotalEngagement" + p1TotalEngagement + "p2TotalEngagement" + p2TotalEngagement);


            //Debug.Log("leftshoulder.z=" +p1leftShoulderPos.z+"rightshoulder.z"+p1rightShoulderPos.z);
            //Debug.Log("leftshoulder-rightshoulder");
            //Debug.Log("p1leftshoulder.z=" +p1leftShoulderPos.z+"p1rightshoulder.z"+p1rightShoulderPos.z);
            //Debug.Log("p2leftshoulder.z=" +p2leftShoulderPos.z+"p1rightshoulder.z"+p2rightShoulderPos.z);
            //Debug.Log("p1engagement= "+ ((p1shoulderToChin)+(p1shoulderToShoulder)));
            //Debug.Log("p2engagment="+((p2shoulderToChin)-(p2shoulderToShoulder)));
            //Debug.Log("leftshoulder-rightshoulder"+(rightShoulderPos.z-leftShoulderPos.z ));
            if (p1TotalEngagement >= 20f && p2TotalEngagement <= 15f)
            {
                //faceMat1.material.SetColor("_Color",Color.green);
                //faceMat2.material.SetColor("_Color",Color.green);

                if (_PecCard.pecStarted)
                {
                    combinedPlayerEngagementsecondHalf++;
                }
                else
                {
                    combinedPlayerEngagement++;
                }
                //engagement.SetActive(true);
                //StartCoroutine(wait());
            }
            if (p1TotalEngagement >= 20f && !(p1TotalEngagement >= 20f && p2TotalEngagement <= 15f))
            {
                //faceMat1.material.SetColor("_Color",Color.blue);
                if (_PecCard.pecStarted)
                {
                    leftPlayerEngagementsecondHalf++;;
                }
                else
                {
                    leftPlayerEngagement++;
                }
            }
            if (!(p1TotalEngagement >= 20f) && !(p1TotalEngagement >= 20f && p2TotalEngagement <= 15f))
            {
                //faceMat1.material.SetColor("_Color",Color.gray);
            }

            if (p2TotalEngagement <= 10f && !(p1TotalEngagement >= 20f && p2TotalEngagement <= 10f))
            {
                //faceMat2.material.SetColor("_Color",Color.blue);
                if (_PecCard.pecStarted)               //second phase of level
                {
                    rightPlayerEngagementsecondHalf++;
                }
                else
                {
                    rightPlayerEngagement++;
                }
            }
            if (!(p2TotalEngagement <= 10f) && !(p1TotalEngagement >= 20f && p2TotalEngagement <= 10f))
            {
                //faceMat2.material.SetColor("_Color",Color.gray);
            }

            //return;
        }

        /*
         * if(faceManagerP1.IsTrackingFace()&&_KinectManager.IsUserDetected())
         *      {
         *      //p1rot=faceManagerP1.GetHeadRotation(leftid,false);//shoulderight=8//shoulderleft=4
         *
         *      avModelVertices1=new Vector3[faceManagerP1.GetFaceModelVertexCount()];
         *       faceManagerP1.GetUserFaceVertices(leftid, ref avModelVertices1);
         *       p1shoulderpos=_KinectManager.GetJointPosition(leftid,4);
         *       p1shoulderToChin=(((avModelVertices1[4].x)-(p1shoulderpos.x))*(100f));
         *       p1rightShoulderPos=_KinectManager.GetJointPosition(leftid,8);
         *       p1leftShoulderPos=_KinectManager.GetJointPosition(leftid,4);
         *       p1TotalEngagement=p1shoulderToChin+p1shoulderToShoulder;
         *       p1shoulderToShoulder=(p1rightShoulderPos.z-p1leftShoulderPos.z)*20f;
         *      if((p1TotalEngagement>=25f))
         *              {
         *              faceMat1.material.SetColor("_Color",Color.blue);
         *              leftPlayerEngagement++;
         *              }
         *      else
         *              {
         *              faceMat1.material.SetColor("_Color",Color.gray);
         *              }
         *      }
         *
         *
         *
         *
         * if(faceManagerP2.IsTrackingFace())
         * {
         *      avModelVertices2=new Vector3[faceManagerP2.GetFaceModelVertexCount()];
         *      faceManagerP2.GetUserFaceVertices(leftid, ref avModelVertices2);
         *      p2shoulderpos=_KinectManager.GetJointPosition(rightid,4);
         *      p2shoulderToChin=(((avModelVertices1[4].x)-(p2shoulderpos.x))*(100f));
         *      p2rightShoulderPos=_KinectManager.GetJointPosition(rightid,8);
         *      p2leftShoulderPos=_KinectManager.GetJointPosition(rightid,4);
         *      //Debug.Log("leftshoulder.z=" +leftShoulderPos.z+"rightshoulder.z"+rightShoulderPos.z);
         *      //Debug.Log("leftshoulder-rightshoulder"+(rightShoulderPos.z-leftShoulderPos.z ));
         *  p2shoulderToShoulder=(p2leftShoulderPos.z-p2rightShoulderPos.z)*20f;
         *      if(((p2shoulderToChin)-(p2shoulderToShoulder))<=12f)
         *      {
         *              faceMat2.material.SetColor("_Color",Color.blue);
         *              rightPlayerEngagement++;
         *      }
         *      else
         *      {
         *              faceMat2.material.SetColor("_Color",Color.gray);
         *      }
         *
         * }
         *
         * }
         *
         * IEnumerator wait() {
         *
         * //yield return new WaitForSeconds(3);
         *
         * engagement.SetActive(true);
         * for(int i=0;i<5;i++)
         * {
         *      stars[i].SetBool("isSpinning",true);
         * }
         * yield return new WaitForSeconds(3);	//Wait 3 Secs
         * engagement.SetActive(false);
         * //engagement.SetActive(false);
         *
         */
    }
    void Update()
    {
        // get the face-tracking manager instance
        if (manager == null)
        {
            manager = FacetrackingManager.Instance;
        }

        if (manager && manager.GetFaceTrackingID() != 0)
        {
            // set head position & rotation
            if (HeadTransform != null)
            {
                // head position
                Vector3 newPosition = manager.GetHeadPosition(mirroredHeadMovement);

                // head rotation
                Quaternion newRotation = HeadInitialRotation * manager.GetHeadRotation(mirroredHeadMovement);

                // rotational fix, provided by Richard Borys:
                // The added rotation fixes rotational error that occurs when person is not centered in the middle of the kinect
                Vector3 addedRotation = newPosition.z != 0f ? new Vector3(Mathf.Rad2Deg * (Mathf.Tan(newPosition.y) / newPosition.z),
                                                                          Mathf.Rad2Deg * (Mathf.Tan(newPosition.x) / newPosition.z), 0) : Vector3.zero;

                addedRotation.x = newRotation.eulerAngles.x + addedRotation.x;
                addedRotation.y = newRotation.eulerAngles.y + addedRotation.y;
                addedRotation.z = newRotation.eulerAngles.z + addedRotation.z;

                newRotation = Quaternion.Euler(addedRotation.x, addedRotation.y, addedRotation.z);
                // end of rotational fix

                if (smoothFactor != 0f)
                {
                    HeadTransform.rotation = Quaternion.Slerp(HeadTransform.rotation, newRotation, smoothFactor * Time.deltaTime);
                }
                else
                {
                    HeadTransform.rotation = newRotation;
                }

                // check for head pos overlay
                if (foregroundCamera)
                {
                    // get the background rectangle (use the portrait background, if available)
                    Rect backgroundRect             = foregroundCamera.pixelRect;
                    PortraitBackground portraitBack = PortraitBackground.Instance;

                    if (portraitBack && portraitBack.enabled)
                    {
                        backgroundRect = portraitBack.GetBackgroundRect();
                    }

                    KinectManager kinectManager = KinectManager.Instance;

                    if (kinectManager)
                    {
                        long    userId          = kinectManager.GetUserIdByIndex(manager.playerIndex);
                        Vector3 posColorOverlay = kinectManager.GetJointPosColorOverlay(userId, (int)KinectInterop.JointType.Head, foregroundCamera, backgroundRect);

                        if (posColorOverlay != Vector3.zero)
                        {
                            newPosition = posColorOverlay;

//							if(overlayObj)
//							{
//								overlayObj.position = newPosition;
//							}
                        }
                    }
                }
                else
                {
                    // move around the initial position
                    newPosition += HeadInitialPosition;
                }

                // vertical offet
                if (verticalOffset != 0f)
                {
                    // add the vertical offset
                    Vector3 dirHead = new Vector3(0, verticalOffset, 0);
                    dirHead      = HeadTransform.InverseTransformDirection(dirHead);
                    newPosition += dirHead;
                }

                // set the position
                if (smoothFactor != 0f)
                {
                    HeadTransform.position = Vector3.Lerp(HeadTransform.position, newPosition, smoothFactor * Time.deltaTime);
                }
                else
                {
                    HeadTransform.position = newPosition;
                }

                // scale factor
                if (HeadTransform.localScale.x != modelScaleFactor)
                {
                    HeadTransform.localScale = new Vector3(modelScaleFactor, modelScaleFactor, modelScaleFactor);
                }
            }

            // apply animation units

            // AU0 - Upper Lip Raiser
            // 0=neutral, covering teeth; 1=showing teeth fully; -1=maximal possible pushed down lip
            float fAU0 = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LipPucker);
            SetJointRotation(UpperLipLeft, UpperLipLeftAxis, fAU0, UpperLipLeftNeutral, UpperLipLeftUp);
            SetJointRotation(UpperLipRight, UpperLipRightAxis, fAU0, UpperLipRightNeutral, UpperLipRightUp);

            // AU1 - Jaw Lowerer
            // 0=closed; 1=fully open; -1= closed, like 0
            float fAU1 = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.JawOpen);
            SetJointRotation(Jaw, JawAxis, fAU1, JawNeutral, JawDown);

            // AU2 – Lip Stretcher
            // 0=neutral; 1=fully stretched (joker’s smile); -1=fully rounded (kissing mouth)
            float fAU2_left = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LipStretcherLeft);
            fAU2_left = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU2_left * 2 - 1) : fAU2_left;
            SetJointRotation(LipLeft, LipLeftAxis, fAU2_left, LipLeftNeutral, LipLeftStretched);

            float fAU2_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LipStretcherRight);
            fAU2_right = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU2_right * 2 - 1) : fAU2_right;
            SetJointRotation(LipRight, LipRightAxis, fAU2_right, LipRightNeutral, LipRightStretched);

            // AU3 – Brow Lowerer
            // 0=neutral; -1=raised almost all the way; +1=fully lowered (to the limit of the eyes)
            float fAU3_left = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LefteyebrowLowerer);
            fAU3_left = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU3_left * 2 - 1) : fAU3_left;
            SetJointRotation(EyebrowLeft, EyebrowLeftAxis, fAU3_left, EyebrowLeftNeutral, EyebrowLeftLowered);

            float fAU3_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.RighteyebrowLowerer);
            fAU3_right = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU3_right * 2 - 1) : fAU3_right;
            SetJointRotation(EyebrowRight, EyebrowRightAxis, fAU3_right, EyebrowRightNeutral, EyebrowRightLowered);

            // AU4 – Lip Corner Depressor
            // 0=neutral; -1=very happy smile; +1=very sad frown
            float fAU4_left = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LipCornerDepressorLeft);
            fAU4_left = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU4_left * 2) : fAU4_left;
            SetJointRotation(LipCornerLeft, LipCornerLeftAxis, fAU4_left, LipCornerLeftNeutral, LipCornerLeftDepressed);

            float fAU4_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LipCornerDepressorRight);
            fAU4_right = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU4_right * 2) : fAU4_right;
            SetJointRotation(LipCornerRight, LipCornerRightAxis, fAU4_right, LipCornerRightNeutral, LipCornerRightDepressed);

            // AU6, AU7 – Eyelid closed
            // 0=neutral; -1=raised; +1=fully lowered
            float fAU6_left = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LefteyeClosed);
            fAU6_left = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU6_left * 2 - 1) : fAU6_left;
            SetJointRotation(UpperEyelidLeft, UpperEyelidLeftAxis, fAU6_left, UpperEyelidLeftNeutral, UpperEyelidLeftLowered);
            SetJointRotation(LowerEyelidLeft, LowerEyelidLeftAxis, fAU6_left, LowerEyelidLeftNeutral, LowerEyelidLeftRaised);

            float fAU6_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.RighteyeClosed);
            fAU6_right = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU6_right * 2 - 1) : fAU6_right;
            SetJointRotation(UpperEyelidRight, UpperEyelidRightAxis, fAU6_right, UpperEyelidRightNeutral, UpperEyelidRightLowered);
            SetJointRotation(LowerEyelidRight, LowerEyelidRightAxis, fAU6_right, LowerEyelidRightNeutral, LowerEyelidRightRaised);
        }
        else
        {
            // hide the model behind the camera
            if (HeadTransform && HeadTransform.position.z >= 0f)
            {
                HeadTransform.position = new Vector3(0f, 0f, -10f);
            }
        }
    }