protected override void OnBodyFrameReceived(BodyFrame frame)
    {
        // Each user controls an avatar separately
        if (useSeparateUsers)
        {
            Body[] users     = frame.Bodies().Where(b => b.IsTracked).ToArray();
            int    userCount = users.Length;

            for (int i = 0; i < userCount; i++)
            {
                if (models[i].updateModel)
                {
                    Avateering.Update(models[i], users[i]);
                }

                if (showStickmen)
                {
                    if (!stickmen[i].gameObject.activeSelf)
                    {
                        stickmen[i].gameObject.SetActive(true);
                    }
                }
                else if (stickmen[i].gameObject.activeSelf)
                {
                    stickmen[i].gameObject.SetActive(false);
                }

                stickmen[i].UpdateBody(users[i], frameView, KinectSensor.CoordinateMapper, visualization);
            }

            if (faceFrameReader != null && users.Length > 0)
            {
                if (!faceFrameSource.IsTrackingIdValid)
                {
                    faceFrameSource.TrackingId = users[0].TrackingId;
                }

                using (var faceFrame = faceFrameReader.AcquireLatestFrame())
                {
                    if (faceFrame != null)
                    {
                        Face face = faceFrame.Face();

                        if (face != null)
                        {
                            if (!detailedFace.gameObject.activeSelf)
                            {
                                detailedFace.gameObject.SetActive(true);
                            }

                            detailedFace.UpdateFace(face, frameView, visualization, KinectSensor.CoordinateMapper);
                        }
                        else if (detailedFace.gameObject.activeSelf)
                        {
                            detailedFace.gameObject.SetActive(false);
                        }
                    }
                }
            }

            for (int i = userCount; i < models.Length; i++)
            {
                if (stickmen[i].gameObject.activeSelf)
                {
                    stickmen[i].gameObject.SetActive(false);
                }
            }
        }
        // The first assigned user controls all avatars
        else
        {
            //   Body user = frame.Bodies().First();//Closest();
            // Debug.Log(user);
            if (user == null)
            {
                user = frame.Bodies().Closest();
            }
            else
            {
                // user = frame.Bodies().First();
            }

            if (user != null)
            {
                for (int i = 0; i < models.Length; i++)
                {
                    if (models[i].updateModel)
                    {
                        Avateering.Update(models[i], user);
                    }
                }

                if (showStickmen)
                {
                    if (!stickmen[0].gameObject.activeSelf)
                    {
                        stickmen[0].gameObject.SetActive(true);
                    }
                }
                else if (stickmen[0].gameObject.activeSelf)
                {
                    stickmen[0].gameObject.SetActive(false);
                }

                stickmen[0].UpdateBody(user, frameView, KinectSensor.CoordinateMapper, visualization);

                if (faceFrameReader != null)
                {
                    if (!faceFrameSource.IsTrackingIdValid)
                    {
                        faceFrameSource.TrackingId = user.TrackingId;
                    }

                    using (HighDefinitionFaceFrame faceFrame = faceFrameReader.AcquireLatestFrame())
                    {
                        if (faceFrame != null)
                        {
                            Face face = faceFrame.Face();

                            if (face != null)
                            {
                                if (!detailedFace.gameObject.activeSelf)
                                {
                                    detailedFace.gameObject.SetActive(true);
                                }

                                detailedFace.UpdateFace(face, frameView, visualization, KinectSensor.CoordinateMapper);
                            }
                            else if (detailedFace.gameObject.activeSelf)
                            {
                                detailedFace.gameObject.SetActive(false);
                            }
                        }
                    }
                }
            }
        }
    }
Exemple #2
0
    void RefreshFrame(BodyWrapper body)
    {
        stickman.gameObject.SetActive(body != null);
        frameViewArc.gameObject.SetActive(body != null);
        modelArc.gameObject.SetActive(body != null);

        if (body != null)
        {
            if (model.updateModel)
            {
                Avateering.Update(model, body);
            }

            stickman.UpdateBody(body, frameView);

            Joint   startJoint;
            Joint   centerJoint;
            Joint   endJoint;
            Vector2 centerJointPosition;
            Vector2 startJointDir;
            Vector2 endJointDir;

            #region JointPeaks

            for (int i = 0; i < jointPeaks.Length; i++)
            {
                startJoint  = body.Joints[jointPeaks[i].start];
                centerJoint = body.Joints[jointPeaks[i].center];
                endJoint    = body.Joints[jointPeaks[i].end];

                centerJointPosition = stickman.controller.GetJointPosition(jointPeaks[i].center);
                startJointDir       = ((Vector2)stickman.controller.GetJointPosition(jointPeaks[i].start) - centerJointPosition).normalized;
                endJointDir         = ((Vector2)stickman.controller.GetJointPosition(jointPeaks[i].end) - centerJointPosition).normalized;

                jointPeaks[i].arc.Angle = Vector2.Angle(startJointDir, endJointDir);

                jointPeaks[i].arc.transform.position = centerJointPosition;
                jointPeaks[i].arc.transform.up       = Quaternion.Euler(0, 0, jointPeaks[i].arc.Angle) *
                                                       (Vector2.Dot(Quaternion.Euler(0, 0, 90) * startJointDir, endJointDir) > 0 ? startJointDir : endJointDir);

                jointPeaks[i].jointAngle = (float)centerJoint.Angle(startJoint, endJoint);
            }

            #endregion

            startJoint  = body.Joints[model.start];
            centerJoint = body.Joints[model.center];
            endJoint    = body.Joints[model.end];

            #region FrameView Arc

            if (vitruviusVideo.videoPlayer.IsPlaying)
            {
                startJointDir       = body.Map2D[model.start].ToVector2();
                centerJointPosition = body.Map2D[model.center].ToVector2();
                endJointDir         = body.Map2D[model.end].ToVector2();
            }
            else
            {
                startJointDir       = startJoint.Position.ToPoint(visualization, KinectSensor.CoordinateMapper);
                centerJointPosition = centerJoint.Position.ToPoint(visualization, KinectSensor.CoordinateMapper);
                endJointDir         = endJoint.Position.ToPoint(visualization, KinectSensor.CoordinateMapper);
            }

            frameView.SetPositionOnFrame(ref startJointDir);
            frameView.SetPositionOnFrame(ref centerJointPosition);
            frameView.SetPositionOnFrame(ref endJointDir);

            startJointDir = (startJointDir - centerJointPosition).normalized;
            endJointDir   = (endJointDir - centerJointPosition).normalized;

            frameViewArc.Angle = Vector2.Angle(startJointDir, endJointDir);

            frameViewArc.transform.position = centerJointPosition;
            frameViewArc.transform.up       = Quaternion.Euler(0, 0, frameViewArc.Angle) *
                                              (Vector2.Dot(Quaternion.Euler(0, 0, 90) * startJointDir, endJointDir) > 0 ? startJointDir : endJointDir);

            #endregion

            #region Model Arc

            Vector3 arcPosition = model.GetBone(model.center).Transform.position;
            arcPosition.z -= 2;

            centerJointPosition = centerJoint.Position.ToVector3();
            startJointDir       = ((Vector2)startJoint.Position.ToVector3() - centerJointPosition).normalized;
            endJointDir         = ((Vector2)endJoint.Position.ToVector3() - centerJointPosition).normalized;

            modelArc.Angle = Vector2.Angle(startJointDir, endJointDir);

            modelArc.transform.position = arcPosition;
            modelArc.transform.up       = Quaternion.Euler(0, 0, modelArc.Angle) *
                                          (Vector2.Dot(Quaternion.Euler(0, 0, 90) * startJointDir, endJointDir) > 0 ? startJointDir : endJointDir);

            #endregion
        }
    }