Ejemplo n.º 1
0
    void Update()
    {
        // start Kinect face tracker as needed
        if (!facetrackingInitialized)
        {
            StartFacetracker();

            if (!facetrackingInitialized)
            {
                Application.Quit();
                return;
            }
        }

        if (facetrackingInitialized)
        {
            // update the face tracker
            int rc = FacetrackingWrapper.UpdateFaceTracking();

            if (rc >= 0)
            {
                // poll the video frame as needed
                if (computeColorMap)
                {
                    if (FacetrackingWrapper.PollVideo(ref videoBuffer, ref colorImage))
                    {
                        usersClrTex.SetPixels32(colorImage);
                        usersClrTex.Apply();
                    }
                }

                // estimate the tracking state
                isTrackingFace = FacetrackingWrapper.IsFaceTracked();
                faceTrackingID = FacetrackingWrapper.GetFaceTrackingID();
                Debug.Log(isTrackingFace);

                // get the facetracking parameters
                if (isTrackingFace)
                {
                    lastFaceTrackedTime = Time.realtimeSinceStartup;

                    // get face rectangle
                    bool bGotFaceRect = FacetrackingWrapper.GetFaceRect(ref faceRect);

                    // get head position and rotation
                    Vector4 vHeadPos = Vector4.zero, vHeadRot = Vector4.zero;
                    if (FacetrackingWrapper.GetHeadPosition(ref vHeadPos))
                    {
                        headPos = (Vector3)vHeadPos;

                        if (!mirroredHeadMovement)
                        {
                            headPos.z = -headPos.z;
                        }
                    }

                    if (FacetrackingWrapper.GetHeadRotation(ref vHeadRot))
                    {
                        if (mirroredHeadMovement)
                        {
                            vHeadRot.x = -vHeadRot.x;
                            vHeadRot.z = -vHeadRot.z;
                        }
                        else
                        {
                            vHeadRot.x = -vHeadRot.x;
                            vHeadRot.y = -vHeadRot.y;
                        }

                        headRot = Quaternion.Euler((Vector3)vHeadRot);
                    }

                    // get the animation units
                    bGotAU = false;
                    int iNumAU = FacetrackingWrapper.GetAnimUnitsCount();

                    if (iNumAU > 0)
                    {
                        if (afAU == null || afAU.Length == 0)
                        {
                            afAU = new float[iNumAU];
                        }

                        if (afAU != null && afAU.Length > 0)
                        {
                            bGotAU = FacetrackingWrapper.GetAnimUnits(ref afAU);
                        }
                    }

                    // get the shape units
                    //isConverged = FacetrackingWrapper.IsShapeConverged();

                    bGotSU = false;
                    int iNumSU = FacetrackingWrapper.GetShapeUnitsCount();

                    if (iNumSU > 0)
                    {
                        if (afSU == null || afSU.Length == 0)
                        {
                            afSU = new float[iNumSU];
                        }

                        if (afSU != null && afSU.Length > 0)
                        {
                            bGotSU = FacetrackingWrapper.GetShapeUnits(ref afSU);
                        }
                    }

                    // get the shape points
                    bGotShapePoints = false;
                    int iNumPoints = FacetrackingWrapper.GetShapePointsCount();

                    if (iNumPoints > 0)
                    {
                        if (avShapePoints == null || avShapePoints.Length == 0)
                        {
                            avShapePoints = new Vector2[iNumPoints];
                        }

                        if (avShapePoints != null || avShapePoints.Length > 0)
                        {
                            bGotShapePoints = FacetrackingWrapper.GetShapePoints(ref avShapePoints);
                        }
                    }

                    // get the 3D model points
                    bGotFaceModelPoints = false;
                    int iFaceModelVertexCount = FacetrackingWrapper.GetFaceModelVerticesCount();

                    if (iFaceModelVertexCount > 0)
                    {
                        if (avFaceModelTriangles == null || avFaceModelTriangles.Length == 0)
                        {
                            int iFaceModelTrianglesCount = FacetrackingWrapper.GetFaceModelTrianglesCount();
                            avFaceModelTriangles = new int[iFaceModelTrianglesCount];

                            FacetrackingWrapper.GetFaceModelTriangles(mirroredHeadMovement, ref avFaceModelTriangles);
                        }

                        if (avFaceModelPoints == null || avFaceModelPoints.Length == 0)
                        {
                            avFaceModelPoints = new Vector3[iFaceModelVertexCount];
                        }

                        if (avFaceModelPoints != null && avFaceModelPoints.Length > 0)
                        {
                            bGotFaceModelPoints = FacetrackingWrapper.GetFaceModelVertices(ref avFaceModelPoints);
                        }
                    }

                    if (computeColorMap)
                    {
                        if (displayFaceRect && bGotFaceRect)
                        {
                            DrawFacetrackerRect(usersClrTex, !visualizeFacetracker);
                        }

                        if (visualizeFacetracker && bGotShapePoints)
                        {
                            DrawFacetrackerLines(usersClrTex, avShapePoints, true);
                        }
                    }
                }
                else if ((Time.realtimeSinceStartup - lastFaceTrackedTime) <= faceTrackingTolerance)
                {
                    // allow tolerance in tracking
                    isTrackingFace = true;
                }
            }
        }
    }