Exemple #1
0
    void Update()
    {
        if (facetrackingManager == null)
        {
            facetrackingManager = FacetrackingManager.Instance;
        }

        if (facetrackingManager && facetrackingManager.IsTrackingFace())
        {
            // set head position & rotation
            if (head != null)
            {
                //Vector3 newPosition = HeadInitialPosition + manager.GetHeadPosition();
                //HeadTransform.localPosition = Vector3.Lerp(HeadTransform.localPosition, newPosition, 3 * Time.deltaTime);

                //Quaternion newRotation = HeadInitialRotation * facetrackingManager.GetHeadRotation();
                //head.localRotation = Quaternion.Slerp(head.localRotation, newRotation, 3 * Time.deltaTime);
                //head.localRotation = HeadInitialRotation * facetrackingManager.GetHeadRotation();
                Vector3 targetOrientation = Vector3.zero;
                targetOrientation.x = facetrackingManager.GetHeadRotation().eulerAngles.x;
                targetOrientation.y = facetrackingManager.GetHeadRotation().eulerAngles.y;

                head.localRotation = Quaternion.Euler(targetOrientation);
            }
        }
    }
Exemple #2
0
    public static float GetNeckAngle(FacetrackingManager facetrackingManager, JointName joint)
    {
        float angle = 0;

        if (facetrackingManager == null)
        {
            facetrackingManager = FacetrackingManager.Instance;
        }

        if (facetrackingManager && facetrackingManager.IsTrackingFace())
        {
            switch (joint)
            {
            case JointName.pan:
                angle = facetrackingManager.GetHeadRotation().eulerAngles.y;
                if (angle > 180f)
                {
                    angle = angle - 360f;                   //180도를 넘을리는 없지만 각의 범위를 -180~180로 쓰겠다는 의미
                }
                break;

            case JointName.tilt:
                angle = facetrackingManager.GetHeadRotation().eulerAngles.x;
                if (angle > 180f)
                {
                    angle = angle - 360f;
                }
                break;

            default: break;
            }
        }

        return(angle);
    }
    void Update()
    {
        if (!isOn)
        {
            return;
        }

        headRotation = faceTrackingManager.GetHeadRotation(true).eulerAngles;
        //invierte:
        headRotation.x = headRotation.x * -2;
        headRotation.z = headRotation.z * -1;
        headRotation.y = headRotation.y * 2;

        faceInteractions.transform.localEulerAngles = headRotation;
        //el 22 es la punta de arriba de la boca:
        Vector3 vert_pos_mouth_1 = faceTrackingManager.GetFaceModelVertex(22);
        //el 10 es la punta de abajo de la boca:
        Vector3 vert_pos_mouth_2 = faceTrackingManager.GetFaceModelVertex(10);



        mouthPosition = vert_pos_mouth_1;//Vector3.Lerp(vert_pos_mouth_1, vert_pos_mouth_2, 0.5f);

        float mouthValue = Vector3.Distance(vert_pos_mouth_1, vert_pos_mouth_2);

        smoothMouthValue = Mathf.Lerp(mouthValue, smoothMouthValue, mouthSmoothFilter);

        SetMouthState();

        faceInteractions.transform.position = Vector3.Lerp(vert_pos_mouth_1 + offsetMomuth, faceInteractions.transform.position, 0.1f);

        // DrawPoints();
    }
Exemple #4
0
        private Quaternion RotateBone(OverlayObject oo, KinectManager kinectManager, FacetrackingManager facetrackingManager, long key)
        {
            int        iJointIndex = (int)oo.TrackedJointType;
            Quaternion qRotObject  = Quaternion.identity;

            if (iJointIndex == 3)        // if it's the head
            {
                if (facetrackingManager) // && facetrackingManager.IsFaceTrackingInitialized())
                {
                    qRotObject *= facetrackingManager.GetHeadRotation(key, bMirroredMovement: !RearProjection);

                    if (oo.BoneGameObject.FindChild("Jaw") != null)
                    {
                        Transform Jaw;
                        if (facetrackingManager.bGotAU && oo.BoneGameObject.FindChild("Jaw") != null)
                        {
                            Jaw = oo.BoneGameObject.FindChild("Jaw");
                            //  print("Jaw angle : " + facetrackingManager.dictAU[0] * JAWROTATEMULIPLIER);
                            Jaw.localRotation = Quaternion.AngleAxis(facetrackingManager.dictAU[0] * JAWROTATEMULIPLIER, Vector3.right);
                        }
                    }
                }
            }
            else // all joints not the head
            {
                qRotObject = kinectManager.GetJointOrientation(key, iJointIndex, flip: RearProjection);
                Vector3 rotAngles = qRotObject.eulerAngles - oo.InitialRotation.eulerAngles;
                qRotObject = Quaternion.Euler(rotAngles);
            }
            return(Quaternion.Slerp(oo.BoneGameObject.transform.rotation, qRotObject, smoothFactor * Time.deltaTime));
        }
	void Update () 
	{
		// get the face-tracking manager instance
		if(faceManager == null)
		{
			kinectManager = KinectManager.Instance;
			faceManager = FacetrackingManager.Instance;
		}
		
		if (kinectManager && faceManager && faceManager.IsTrackingFace ()) {
			// get user-id by user-index
			long userId = kinectManager.GetUserIdByIndex (playerIndex);
			if (userId == 0)
				return;

			// head rotation
			Quaternion newRotation = initialRotation * faceManager.GetHeadRotation (userId, true);
			
			if (smoothFactor != 0f)
				transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, smoothFactor * Time.deltaTime);
			else
				transform.rotation = newRotation;

			// head position
			Vector3 newPosition = faceManager.GetHeadPosition (userId, true);
			//Debug.Log(newPosition.ToString());

			if (verticalOffset != 0f) {
				Vector3 dirHead = new Vector3 (0, verticalOffset, 0);
				dirHead = transform.InverseTransformDirection (dirHead);
				newPosition += dirHead;
			}
			
//			if(smoothFactor != 0f)
//				transform.position = Vector3.Lerp(transform.position, newPosition, smoothFactor * Time.deltaTime);
//			else

			//<----------------------------------------------correct modal position --------------------------------------------->
			//Debug.Log ("new Positionx   "  + newPosition.x.ToString());

			if(newPosition.x > 0f)
				newPosition.x = newPosition.x * 0.8f - deltaX; 
			else if(newPosition.x < 0f)
				newPosition.x = newPosition.x * 0.8f + deltaX;
			//<------------------------------------------------------------------------------------------------------------------>


			transform.position = newPosition;

			//Debug.Log ("transform.position   " + transform.position.x.ToString());
			//Debug.Log(newPosition.ToString());
		} else if (kinectManager && faceManager && !faceManager.IsTrackingFace ()) {
		
			transform.position = -Vector3.one;
		}

	}
    void Update()
    {
        if (manager && manager.IsTracking())
        {
            // set head position & rotation
            if (Head != null)
            {
                Vector3 newPosition = HeadInitialPosition + manager.GetHeadPosition();
                Head.localPosition = Vector3.Lerp(Head.localPosition, newPosition, 3 * Time.deltaTime);

                Quaternion newRotation = HeadInitialRotation * manager.GetHeadRotation();
                Head.localRotation = Quaternion.Slerp(Head.localRotation, newRotation, 3 * Time.deltaTime);
            }

            // get animation units
            int iAUCount = manager.GetAnimUnitsCount();

            if (iAUCount >= 6)
            {
                // AU0 - Upper Lip Raiser
                // 0=neutral, covering teeth; 1=showing teeth fully; -1=maximal possible pushed down lip
                float fAU0 = manager.GetAnimUnit(0);
                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(1);
                SetJointRotation(Jaw, JawAxis, fAU1, JawNeutral, JawDown);

                // AU2 – Lip Stretcher
                // 0=neutral; 1=fully stretched (joker’s smile); -1=fully rounded (kissing mouth)
                float fAU2 = manager.GetAnimUnit(2);
                SetJointRotation(LipLeft, LipLeftAxis, fAU2, LipLeftNeutral, LipLeftStretched);
                SetJointRotation(LipRight, LipRightAxis, fAU2, LipRightNeutral, LipRightStretched);

                // AU3 – Brow Lowerer
                // 0=neutral; -1=raised almost all the way; +1=fully lowered (to the limit of the eyes)
                float fAU3 = manager.GetAnimUnit(3);
                SetJointRotation(EyebrowLeft, EyebrowLeftAxis, fAU3, EyebrowLeftNeutral, EyebrowLeftLowered);
                SetJointRotation(EyebrowRight, EyebrowRightAxis, fAU3, EyebrowRightNeutral, EyebrowRightLowered);

                // AU4 – Lip Corner Depressor
                // 0=neutral; -1=very happy smile; +1=very sad frown
                float fAU4 = manager.GetAnimUnit(4);
                SetJointRotation(LipCornerLeft, LipCornerLeftAxis, fAU4, LipCornerLeftNeutral, LipCornerLeftDepressed);
                SetJointRotation(LipCornerRight, LipCornerRightAxis, fAU4, LipCornerRightNeutral, LipCornerRightDepressed);

                // AU5 – Outer Brow Raiser
                // 0=neutral; -1=fully lowered as a very sad face; +1=raised as in an expression of deep surprise
                float fAU5 = manager.GetAnimUnit(5);
                SetJointRotation(OuterBrowLeft, OuterBrowLeftAxis, fAU5, OuterBrowLeftNeutral, OuterBrowLeftRaised);
                SetJointRotation(OuterBrowRight, OuterBrowRightAxis, fAU5, OuterBrowRightNeutral, OuterBrowRightRaised);
            }
        }
    }
    void Update()
    {
        // get the face-tracking manager instance
        if (faceManager == null)
        {
            kinectManager = KinectManager.Instance;
            faceManager   = FacetrackingManager.Instance;
        }

        if (kinectManager && faceManager && faceManager.IsTrackingFace())
        {
            // get user-id by user-index
            long userId = kinectManager.GetUserIdByIndex(playerIndex);
            if (userId == 0)
            {
                return;
            }

            // head rotation
            Quaternion newRotation = initialRotation * faceManager.GetHeadRotation(userId, true);

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

            // head position
            Vector3 newPosition = faceManager.GetHeadPosition(userId, true);

            if (verticalOffset != 0f)
            {
                Vector3 dirHead = new Vector3(horizontalOffset, verticalOffset, zOffset);
                dirHead      = transform.InverseTransformDirection(dirHead);
                newPosition += dirHead;
            }

//			if(smoothFactor != 0f)
//				transform.position = Vector3.Lerp(transform.position, newPosition, smoothFactor * Time.deltaTime);
//			else
            transform.position = newPosition;
        }
    }
Exemple #8
0
    void Update()
    {
        // get the face-tracking manager instance
        if (faceManager == null)
        {
            faceManager = FacetrackingManager.Instance;
        }

        if (faceManager && faceManager.IsTrackingFace())
        {
            // head rotation
            Quaternion newRotation = initialRotation * faceManager.GetHeadRotation(true);

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

            // head position
            Vector3 newPosition = faceManager.GetHeadPosition(true);

            if (verticalOffset != 0f)
            {
                Vector3 dirHead = new Vector3(0, verticalOffset, 0);
                dirHead      = transform.InverseTransformDirection(dirHead);
                newPosition += dirHead;
            }

//			if(smoothFactor != 0f)
//				transform.position = Vector3.Lerp(transform.position, newPosition, smoothFactor * Time.deltaTime);
//			else
            transform.position = newPosition;
        }
    }
    void Update()
    {
        // get the face-tracking manager instance
        if (manager == null)
        {
            manager = FacetrackingManager.Instance;
        }

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

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

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

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

            // 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, UpperEyelidRightNeutral, LowerEyelidRightRaised);
        }
    }
Exemple #10
0
    void Update()
    {
        // get the face-tracking manager instance
        if (faceManager == null)
        {
            kinectManager = KinectManager.Instance;
            faceManager   = FacetrackingManager.Instance;
        }

        // get user-id by user-index
        long userId = kinectManager ? kinectManager.GetUserIdByIndex(playerIndex) : 0;

        if (kinectManager && kinectManager.IsInitialized() && userId != 0 &&
            faceManager && faceManager.IsTrackingFace(userId) && foregroundCamera)
        {
            // get head position
            Vector3 newPosition = faceManager.GetHeadPosition(userId, true);

            // get head rotation
            Quaternion newRotation = initialRotation * faceManager.GetHeadRotation(userId, true);

            // 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 (smoothFactorRotation != 0f)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, smoothFactorRotation * Time.deltaTime);
            }
            else
            {
                transform.rotation = newRotation;
            }

            // 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();
            }

            // model position
            newPosition = kinectManager.GetJointPosColorOverlay(userId, (int)KinectInterop.JointType.Head, foregroundCamera, backgroundRect);
            if (newPosition == Vector3.zero)
            {
                // hide the model behind the camera
                newPosition.z = -10f;
            }

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

            // go to the new position
            if (smoothFactorMovement != 0f && transform.position.z >= 0f)
            {
                transform.position = Vector3.Lerp(transform.position, newPosition, smoothFactorMovement * Time.deltaTime);
            }
            else
            {
                transform.position = newPosition;
            }

            // scale the model if needed
            if (transform.localScale.x != modelScaleFactor)
            {
                transform.localScale = new Vector3(modelScaleFactor, modelScaleFactor, modelScaleFactor);
            }
        }
        else
        {
            // hide the model behind the camera
            if (transform.position.z >= 0f)
            {
                transform.position = new Vector3(0f, 0f, -10f);
            }
        }
    }
    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);
            }
        }
    }
    // moves the transform according to the user's head position & rotation
    private void MoveHeadTransform(long userId)
    {
        // head position
        Vector3 newPosition = faceManager.GetHeadPosition(userId, mirroredHeadMovement);

        // head rotation
        Quaternion newRotation = headInitialRotation * faceManager.GetHeadRotation(userId, 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 (moveSmoothFactor != 0f)
        {
            headTransform.rotation = Quaternion.Slerp(headTransform.rotation, newRotation, moveSmoothFactor * 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)
            {
                Vector3 posColorOverlay = kinectManager.GetJointPosColorOverlay(userId, (int)KinectInterop.JointType.Head, foregroundCamera, backgroundRect);

                if (posColorOverlay != Vector3.zero)
                {
                    newPosition = posColorOverlay;
                }
            }
        }
        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(moveSmoothFactor != 0f)
//			headTransform.position = Vector3.Lerp(headTransform.position, newPosition, moveSmoothFactor * Time.deltaTime);
//		else
        headTransform.position = newPosition;

        // scale factor
        if (headTransform.localScale.x != modelScaleFactor)
        {
            headTransform.localScale = new Vector3(modelScaleFactor, modelScaleFactor, modelScaleFactor);
        }
    }
Exemple #13
0
    void Update()
    {
        // get the face-tracking manager instance
        if (faceManager == null)
        {
            kinectManager = KinectManager.Instance;
            faceManager   = FacetrackingManager.Instance;
        }

        // get user-id by user-index
        long userId = kinectManager ? kinectManager.GetUserIdByIndex(playerIndex) : 0;

        if (kinectManager && kinectManager.IsInitialized() && userId != 0 &&
            faceManager && faceManager.IsTrackingFace(userId) && foregroundCamera)
        {
            // get head position
            Vector3 newPosition = faceManager.GetHeadPosition(userId, true);

            // get head rotation
            Quaternion newRotation = initialRotation * faceManager.GetHeadRotation(userId, true);

            // 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 (smoothFactorRotation != 0f)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, smoothFactorRotation * Time.deltaTime);
            }
            else
            {
                transform.rotation = newRotation;
            }

            // 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();
            }

            // model position
            newPosition = kinectManager.GetJointPosColorOverlay(userId, (int)KinectInterop.JointType.Head, foregroundCamera, backgroundRect);
            if (newPosition == Vector3.zero)
            {
                // hide the model behind the camera
                newPosition.z = -10f;
            }

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

            // go to the new position
            if (smoothFactorMovement != 0f && transform.position.z >= 0f)
            {
                transform.position = Vector3.Lerp(transform.position, newPosition, smoothFactorMovement * Time.deltaTime);
            }
            else
            {
                transform.position = newPosition;
            }

            // scale the model if needed
            if (transform.localScale.x != modelScaleFactor)
            {
                transform.localScale = new Vector3(modelScaleFactor, modelScaleFactor, modelScaleFactor);
            }

            ///Main Routine

            int angle = (int)(transform.eulerAngles.x + KinectManager.Instance.sensorAngle);
            if (angle < 0)
            {
                angle += 360;
            }

            //Face Up/Down
            if (angle > 90)
            {
                int i = 360 - (int)angle;

                if (i > iDeltaUpDown)
                {
                    DriverManager.instance.OutObjList[5].GetComponent <Text>().text = "Face : Up " + i.ToString() + " degrees";
                    DriverManager.instance.OutObjList[5].SetActive(true);
                }
                else
                {
                    DriverManager.instance.OutObjList[5].SetActive(false);
                }
            }
            else
            {
                if (angle > iDeltaUpDown)
                {
                    DriverManager.instance.OutObjList[5].GetComponent <Text>().text = "Face : Down " + angle.ToString() + " degrees";
                    DriverManager.instance.OutObjList[5].SetActive(true);
                }
                else
                {
                    DriverManager.instance.OutObjList[5].SetActive(false);
                }
            }
            ////
            //Face Left/Right

            angle = (int)(transform.eulerAngles.y);

            if (angle >= 180)
            {
                int i = (int)angle - 180;

                if (i > iDeltaLeftRight)
                {
                    DriverManager.instance.OutObjList[6].GetComponent <Text>().text = "Face : Left " + i.ToString() + " degrees";
                    DriverManager.instance.OutObjList[6].SetActive(true);
                }
                else
                {
                    DriverManager.instance.OutObjList[6].SetActive(false);
                }
            }
            else
            {
                int i = 180 - (int)angle;

                if (i > iDeltaLeftRight)
                {
                    DriverManager.instance.OutObjList[6].GetComponent <Text>().text = "Face : Right " + i.ToString() + " degrees";
                    DriverManager.instance.OutObjList[6].SetActive(true);
                }
                else
                {
                    DriverManager.instance.OutObjList[6].SetActive(false);
                }
            }
        }
        else
        {
            DriverManager.instance.OutObjList[5].SetActive(false);
            DriverManager.instance.OutObjList[6].SetActive(false);

            // hide the model behind the camera
            if (transform.position.z >= 0f)
            {
                transform.position = new Vector3(0f, 0f, -10f);
            }
        }
    }
    void FixedUpdate()
    {      // float translation = Time.deltaTime * 10;
        // get the face-tracking manager instance
        if (manager == null)
        {
            manager = FacetrackingManager.Instance;
        }

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

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

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

                if (smoothFactor != 0f)
                {
                    HeadTransform.localRotation = Quaternion.Slerp(HeadTransform.localRotation, newRotation, smoothFactor * Time.deltaTime);
                }
                else
                {
                    HeadTransform.localRotation = newRotation;
                }
            }
            if (LeftEyeTransform != null)
            {
                // head position
                Vector3 newPosition = LeftEyeInitialPosition + manager.GetHeadPosition(mirroredHeadMovement);

                if (smoothFactor != 0f)
                {
                    LeftEyeTransform.localPosition = Vector3.Lerp(HeadTransform.localPosition, newPosition, smoothFactor * Time.deltaTime);
                }
                else
                {
                    LeftEyeTransform.localPosition = newPosition;
                }

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

                if (smoothFactor != 0f)
                {
                    LeftEyeTransform.localRotation = Quaternion.Slerp(LeftEyeTransform.localRotation, newRotation, smoothFactor * Time.deltaTime);
                }
                else
                {
                    LeftEyeTransform.localRotation = newRotation;
                }
            }
            if (RightEyeTransform != null)
            {
                // head position
                Vector3 newPosition = RightEyeInitialPosition + manager.GetHeadPosition(mirroredHeadMovement);

                if (smoothFactor != 0f)
                {
                    RightEyeTransform.localPosition = Vector3.Lerp(HeadTransform.localPosition, newPosition, smoothFactor * Time.deltaTime);
                }
                else
                {
                    RightEyeTransform.localPosition = newPosition;
                }

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

                if (smoothFactor != 0f)
                {
                    RightEyeTransform.localRotation = Quaternion.Slerp(RightEyeTransform.localRotation, newRotation, smoothFactor * Time.deltaTime);
                }
                else
                {
                    RightEyeTransform.localRotation = newRotation;
                }
            }            // 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);
            if (fAU0 > Value_Lippucker && isSadMouth == false && isSmillingMouth == false && isOpenMouth == false)
            {
                setBlendShape(100, 4);
                isKissingMouth = true;
            }
            else
            {
                setBlendShape(0, 4);
                isKissingMouth = false;
            }

            //	Debug.Log (fAU0 .ToString ());
            //	setBlendShape1 (fAU0, 5);
            // AU1 - Jaw Lowerer
            // 0=closed; 1=fully open; -1= closed, like 0
            float fAU1 = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.JawOpen);
            if (fAU1 > Value_JawOpen && isSadMouth == false && isSmillingMouth == false && isKissingMouth == false)
            {
                setBlendShape(100, 8);
                isOpenMouth = true;
            }
            else
            {
                setBlendShape(0, 8);
                isOpenMouth = false;
            }
            //	Debug.Log (fAU1 .ToString ());
            // 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;

            float fAU2_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LipStretcherRight);
            //	fAU2_right = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU2_right * 2 - 1) : fAU2_right;



            if (fAU2_left > Value_Lipstretcher && isSadMouth == false && isKissingMouth == false && isOpenMouth == false)
            {
                setBlendShape(100, 1);
                isSmillingMouth = true;
            }
            else
            {
                //setBlendShape (0, 4);
                setBlendShape(0, 1);
                isSmillingMouth = false;
            }


            //	Debug.Log (fAU2_left .ToString ());
            // 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;


            float fAU3_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.RighteyebrowLowerer);
            //fAU3_right = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU3_right * 2 - 1) : fAU3_right;


            if (fAU3_left < -0.1)
            {
                setBlendShape(100f, 3);
            }
            else if (fAU3_left > 0.1)
            {
                setBlendShape(100f, 7);
            }
            else
            {
                setBlendShape(0, 7);
                setBlendShape(0, 3);
            }



            // 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-1) : fAU4_left;

            float fAU4_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.LipCornerDepressorRight);
            //	Debug.Log (fAU4_left.ToString ());

            if (fAU1 < 0.6 && fAU4_left > Value_LipCornerDepressor && isSmillingMouth == false && isKissingMouth == false)
            {
                setBlendShape(100f, 2);
                isSadMouth = true;
            }
            else
            {
                setBlendShape(0f, 2);
                isSadMouth = false;
            }

            Debug.Log(fAU4_left.ToString());
            // 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;


            float fAU6_right = manager.GetAnimUnit(KinectInterop.FaceShapeAnimations.RighteyeClosed);
            fAU6_right = (platform == KinectInterop.DepthSensorPlatform.KinectSDKv2) ? (fAU6_right * 2 - 1) : fAU6_right;

            /*if (blendShapeCount > 2) {
             *
             * if (blendOne<<100f &&fAU2_left>0f) {
             *      blendOne=1000*fAU2_left;
             *      skinnedMeshRenderer.SetBlendShapeWeight (1, blendOne);
             *      //blendOne += blendSpeed;
             * } else {
             *      //blendOneFinished = true;
             * }
             *
             * if (  blendTwo < 100f) {
             *      skinnedMeshRenderer.SetBlendShapeWeight (2, blendTwo);
             *      blendTwo += blendSpeed;
             * }
             *
             * }*/
        }
    }