/// <summary>
        /// Serializes the current <see cref="BodyFrame"/>.
        /// </summary>
        /// <param name="frame">The frame to serialize.</param>
        /// <returns>A JSON representation of the current frame.</returns>
        public static string Serialize(this BodyFrame frame)
        {
            StringBuilder json = new StringBuilder();

            if (frame != null)
            {
                json.Append("{");
                json.Append("\"Bodies\":");

                json.Append(frame.Bodies().Serialize());
                json.Append("}");
            }

            return(json.ToString());
        }
Esempio n. 2
0
    protected override void OnBodyFrameReceived(BodyFrame frame)
    {
        Body body = frame.Bodies().Closest();

        if (body != null && this.body == null)
        {
            this.body = new BodyWrapper();
        }
        else if (body == null && this.body != null)
        {
            this.body = null;
        }

        if (this.body != null)
        {
            this.body.Set(body, KinectSensor.CoordinateMapper, visualization);
        }
    }
Esempio n. 3
0
    private void IdentifyAllBodies(BodyFrame frame)
    {
        // frame.GetAndRefreshBodyData
        allBodies = new Body[KinectSensor.BodyFrameSource.BodyCount];
        System.Collections.Generic.IEnumerator <Body> e = frame.Bodies().GetEnumerator();
        int   i        = 0;
        float bestDist = float.MaxValue;

        playerBody = -1;
        while (e.MoveNext())
        {
            Body thisBody = e.Current;
            allBodies[i] = thisBody;
            if (thisBody.IsTracked)
            {
                // Calculates that player head is exactly inside the playing hexagon
                CameraSpacePoint csp  = thisBody.Joints[Windows.Kinect.JointType.Head].Position;
                float            x    = csp.X;
                float            z    = csp.Z;
                bool             isIn = (z > 1.285f) && (z < 2.314f) &&
                                        (x > -0.603f + (1.8f - z) * 0.1f / 0.514f) &&
                                        (x > -0.603f + (z - 1.8f) * 0.1f / 0.514f) &&
                                        (x < 0.603f - (1.8f - z) * 0.1f / 0.514f) &&
                                        (x < 0.603f - (z - 1.8f) * 0.1f / 0.514f);
                if ((isIn) && (z < bestDist))
                {
                    bestDist   = z;
                    playerBody = i;
                }
            }
            i += 1;
        }

        /* if (playerBody >= 0)
         * {
         *  Debug.Log("Dist: " + bestDist.ToString());
         * } */
    }
Esempio n. 4
0
    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);
                            }
                        }
                    }
                }
            }
        }
    }