void clientHandler(object o)
    {
        int          SIZEHELLO = 200;
        TcpClient    client    = (TcpClient)o;
        KinectStream kstream   = new KinectStream(client);

        _kinectStreams.Add(kstream);

        using (NetworkStream ns = client.GetStream())
        {
            byte[] message   = new byte[SIZEHELLO];
            int    bytesRead = 0;
            byte[] buffer    = new byte[4341760];
            try
            {
                bytesRead = ns.Read(message, 0, SIZEHELLO);
            }
            catch
            {
                Debug.Log("Connection Lost from " + kstream.name);
                client.Close();
                _kinectStreams.Remove(kstream);;
            }

            if (bytesRead == 0)
            {
                Debug.Log("Connection Lost from " + kstream.name);
                client.Close();
                _kinectStreams.Remove(kstream);;
            }

            //Login
            string   s = System.Text.Encoding.Default.GetString(message);
            string[] l = s.Split('/');

            if (l.Length == 3 && l[0] == "k")
            {
                kstream.name = l[1];
                Debug.Log("New stream from " + l[1]);
            }



            while (_running)
            {
                try
                {
                    bytesRead = ns.Read(message, 0, 8);
                }
                catch (Exception e)
                {
                    Debug.Log(e.Message);
                    _running = false;
                    break;
                }
                if (bytesRead == 0)
                {
                    _running = false;
                    break;
                }

                byte[] idb = { message[0], message[1], message[2], message[3] };
                uint   id  = BitConverter.ToUInt32(idb, 0);
                kstream.lastID = id;
                byte[] sizeb = { message[4], message[5], message[6], message[7] };
                int    size  = BitConverter.ToInt32(sizeb, 0);
                kstream.size = size;

                while (size > 0)
                {
                    try
                    {
                        bytesRead = ns.Read(buffer, 0, size);
                    }catch (Exception e)
                    {
                        Debug.Log(e.Message);
                        _running = false;
                        break;
                    }
                    if (bytesRead == 0)
                    {
                        _running = false;
                        break;
                    }
                    //save because can't update from outside main thread

                    Array.Copy(buffer, 0, kstream.data, kstream.size - size, bytesRead);
                    size -= bytesRead;
                }
                kstream.dirty = true;
            }
        }
    }
    void clientHandler2(object o)
    {
        TcpClient client = (TcpClient)o;

        KinectStream kstream = new KinectStream(client);

        _kinectStreams.Add(kstream);

        using (NetworkStream ns = client.GetStream())
        {
            bool login = false;

            byte[] message = new byte[BUFFER];

            byte[]      loginMessage = new byte[BUFFER];
            List <byte> received     = new List <byte>();

            int bytesRead;

            byte[] rcvSizeInBytes = new byte[2];
            int    rcvSize        = 0;

            int rcvBuffer = 0;

            while (_running)
            {
                if (!login)
                {
                    try
                    {
                        bytesRead             = ns.Read(loginMessage, 0, loginMessage.Length);
                        kstream.bytesReceived = bytesRead;
                    }
                    catch
                    {
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        break;
                    }

                    string   s = System.Text.Encoding.Default.GetString(loginMessage);
                    string[] l = s.Split('/');
                    if (l.Length == 3 && l[0] == "k")
                    {
                        kstream.name = l[1];
                        login        = true;
                        Debug.Log("New stream from " + l[1]);
                    }
                }
                else
                {
                    try
                    {
                        bytesRead = ns.Read(rcvSizeInBytes, 0, rcvSizeInBytes.Length);
                    }
                    catch
                    {
                        break;
                    }

                    if (bytesRead > 0)
                    {
                        rcvSize = BitConverter.ToInt16(rcvSizeInBytes, 0);

                        Debug.Log(rcvSize);

                        continue;
                        try
                        {
                            message               = new byte[rcvSize];
                            bytesRead             = ns.Read(message, 0, message.Length);
                            kstream.bytesReceived = bytesRead;

                            message = received.ToArray();

                            kstream.Ready = false;

                            Color  background = new Color(0, 0, 0, 0);
                            Color  foreground = Color.white;
                            int    index;
                            ushort depth;
                            int    ptr        = 0;
                            byte[] byteValues = new byte[2];

                            //message
                            int currentLine = 0;

                            continue;

                            for (int i = 0; i < kstream.texture.Length; i++)
                            {
                                byteValues[0] = message[ptr];
                                byteValues[1] = message[ptr + 1];
                                index         = BitConverter.ToInt16(byteValues, 0);

                                if (index == 5000)
                                {
                                    currentLine++;
                                    ptr += 2;
                                    continue;
                                }
                                else if (i - (currentLine * 512) == index)
                                {
                                    foreground.b       = ((float)message[ptr + 2]) / 255;
                                    foreground.g       = ((float)message[ptr + 3]) / 255;
                                    foreground.r       = ((float)message[ptr + 4]) / 255;
                                    kstream.texture[i] = foreground;

                                    byteValues[0]        = message[ptr + 5];
                                    byteValues[1]        = message[ptr + 6];
                                    depth                = BitConverter.ToUInt16(byteValues, 0);
                                    kstream.depthData[i] = depth;

                                    ptr += 7;
                                }
                                else
                                {
                                    kstream.texture[i]   = background;
                                    kstream.depthData[i] = 0;
                                }
                            }

                            kstream.Ready = true;
                        }
                        catch (Exception e)
                        {
                            Debug.LogError(e.Message);
                            Debug.LogError(e.StackTrace);
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        Debug.Log("Connection Lost from " + kstream.name);
        client.Close();
        _kinectStreams.Remove(kstream);
    }
Beispiel #3
0
    void Start()
    {
        jointPositions = new Dictionary <Joint, Vector3>();
        foreach (var j in Joints)
        {
            jointPositions[j] = Vector3.zero;
        }

        filteredJointPositions = new Dictionary <Joint, Vector3[]>();
        foreach (Joint joint in Joints)
        {
            filteredJointPositions[joint] = new Vector3[FilterWindow];
        }
        filterCoefficients = new float[] { 0.197413f, 0.174666f, 0.120978f, 0.065591f, 0.027835f, 0.009245f, 0.002403f, 0.000489f, 0.000078f, 0.00001f, 0.000001f, };
        for (int i = 0; i < filterCoefficients.Length; ++i)
        {
            filterCoefficients[i] *= 5;
        }

        /*filterCoefficients[0] = 1f;
         * for (int i = 1; i < FilterWindow; ++i) {
         * filterCoefficients[i] = 0.5f / Mathf.Pow(2, i);
         * }*/

        kinectStream = kinectReference.GetComponent <KinectStream>();

        animator = GetComponent <Animator>();

        Hips          = animator.GetBoneTransform(HumanBodyBones.Hips);
        LeftUpperLeg  = animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg);
        RightUpperLeg = animator.GetBoneTransform(HumanBodyBones.RightUpperLeg);
        LeftLowerLeg  = animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg);
        RightLowerLeg = animator.GetBoneTransform(HumanBodyBones.RightLowerLeg);
        LeftFoot      = animator.GetBoneTransform(HumanBodyBones.LeftFoot);
        RightFoot     = animator.GetBoneTransform(HumanBodyBones.RightFoot);
        LeftToes      = animator.GetBoneTransform(HumanBodyBones.LeftToes);
        RightToes     = animator.GetBoneTransform(HumanBodyBones.RightToes);
        Spine         = animator.GetBoneTransform(HumanBodyBones.Spine);
        Chest         = animator.GetBoneTransform(HumanBodyBones.Chest);
        UpperChest    = animator.GetBoneTransform(HumanBodyBones.UpperChest);
        Neck          = animator.GetBoneTransform(HumanBodyBones.Neck);
        Head          = animator.GetBoneTransform(HumanBodyBones.Head);
        LeftShoulder  = animator.GetBoneTransform(HumanBodyBones.LeftShoulder);
        RightShoulder = animator.GetBoneTransform(HumanBodyBones.RightShoulder);
        LeftUpperArm  = animator.GetBoneTransform(HumanBodyBones.LeftUpperArm);
        RightUpperArm = animator.GetBoneTransform(HumanBodyBones.RightUpperArm);
        LeftLowerArm  = animator.GetBoneTransform(HumanBodyBones.LeftLowerArm);
        RightLowerArm = animator.GetBoneTransform(HumanBodyBones.RightLowerArm);
        LeftHand      = animator.GetBoneTransform(HumanBodyBones.LeftHand);
        RightHand     = animator.GetBoneTransform(HumanBodyBones.RightHand);

        modelBoneLengths = new Dictionary <Joint, float>();

        modelBoneLengths[Joint.Neck]          = (Neck.position - Hips.position).magnitude;
        modelBoneLengths[Joint.ShoulderLeft]  = (Neck.position - LeftUpperArm.position).magnitude;
        modelBoneLengths[Joint.ElbowLeft]     = (LeftUpperArm.position - LeftLowerArm.position).magnitude;
        modelBoneLengths[Joint.WristLeft]     = (LeftLowerArm.position - LeftHand.position).magnitude;
        modelBoneLengths[Joint.ShoulderRight] = (Neck.position - RightUpperArm.position).magnitude;
        modelBoneLengths[Joint.ElbowRight]    = (RightUpperArm.position - RightLowerArm.position).magnitude;
        modelBoneLengths[Joint.WristRight]    = (RightLowerArm.position - RightHand.position).magnitude;
        modelBoneLengths[Joint.HipLeft]       = (Hips.position - LeftUpperLeg.position).magnitude;
        modelBoneLengths[Joint.KneeLeft]      = (LeftUpperLeg.position - LeftLowerLeg.position).magnitude;
        modelBoneLengths[Joint.AnkleLeft]     = (LeftLowerLeg.position - LeftFoot.position).magnitude;
        modelBoneLengths[Joint.HipRight]      = (Hips.position - RightUpperLeg.position).magnitude;
        modelBoneLengths[Joint.KneeRight]     = (RightUpperLeg.position - RightLowerLeg.position).magnitude;
        modelBoneLengths[Joint.AnkleRight]    = (RightLowerLeg.position - RightFoot.position).magnitude;

        HipOffset = Hips.position - transform.position;
    }
    void clientHandler(object o)
    {
        int SIZEHELLO = 200;

        TcpClient    client  = (TcpClient)o;
        KinectStream kstream = new KinectStream(client);

        _kinectStreams.Add(kstream);

        bool login = false;

        using (NetworkStream ns = client.GetStream())
        {
            byte [] message   = new byte[SIZEHELLO];
            int     bytesRead = 0;
            byte[]  buffer    = new byte[BUFFER];

            try
            {
                bytesRead = ns.Read(message, 0, SIZEHELLO);
            }
            catch
            {
                Debug.Log("Connection Lost from " + kstream.name);
                client.Close();
                _kinectStreams.Remove(kstream);
            }

            if (bytesRead == 0)
            {
                Debug.Log("Connection Lost from " + kstream.name);
                client.Close();
                _kinectStreams.Remove(kstream);
            }

            // login
            string   s = System.Text.Encoding.Default.GetString(message);
            string[] l = s.Split('/');
            if (l.Length == 3 && l[0] == "k")
            {
                kstream.name = l[1];
                Debug.Log("New stream from " + l[1]);
                login = true;
            }
            else
            {
                Debug.Log("Invalid Login data from: " + kstream.name);
                client.Close();
                _kinectStreams.Remove(kstream);
            }

            while (login && _running)
            {
                try
                {
                    bytesRead = ns.Read(message, 0, 4);
                }
                catch (Exception e)
                {
                    Debug.Log(e.Message);
                    login = false;
                    break;
                }
                if (bytesRead == 0)
                {
                    login = false;
                    break;
                }

                byte[] sizeb = { message[0], message[1], message[2], message[3] };
                int    size  = BitConverter.ToInt32(sizeb, 0);
                kstream.size = size;

                kstream.bytesReceived = size;

                string ccc = "X";

                while (size > 0)
                {
                    try
                    {
                        bytesRead = ns.Read(buffer, 0, size);
                        ccc      += "Y";
                    }
                    catch (Exception e)
                    {
                        Debug.Log(e.Message);
                        login = false;
                        break;
                    }
                    if (bytesRead == 0)
                    {
                        login = false;
                        break;
                    }

                    Array.Copy(buffer, 0, kstream.data, kstream.size - size, bytesRead);
                    size -= bytesRead;
                }

                kstream.Ready = false;


                Color  background = new Color(0, 0, 0, 0);
                Color  foreground = Color.white;
                int    index;
                ushort depth;
                int    ptr        = 0;
                byte[] byteValues = new byte[2];

                int currentLine = 0;

                for (int i = 0; i < kstream.texture.Length; i++)
                {
                    byteValues[0] = kstream.data[ptr];
                    byteValues[1] = kstream.data[ptr + 1];
                    index         = BitConverter.ToInt16(byteValues, 0);

                    if (index == 5000)
                    {
                        currentLine++;
                        ptr += 2;
                        continue;
                    }
                    else if (i - (currentLine * 512) == index)
                    {
                        foreground.b       = ((float)kstream.data[ptr + 2]) / 255;
                        foreground.g       = ((float)kstream.data[ptr + 3]) / 255;
                        foreground.r       = ((float)kstream.data[ptr + 4]) / 255;
                        kstream.texture[i] = foreground;

                        byteValues[0]        = kstream.data[ptr + 5];
                        byteValues[1]        = kstream.data[ptr + 6];
                        depth                = BitConverter.ToUInt16(byteValues, 0);
                        kstream.depthData[i] = depth;

                        ptr += 7;
                    }
                    else
                    {
                        kstream.texture[i]   = background;
                        kstream.depthData[i] = 0;
                    }
                }

                kstream.Ready = true;
            }
            Debug.Log("Connection Lost from " + kstream.name);
            client.Close();
            _kinectStreamsToDestroy.Add(kstream);
            _kinectStreams.Remove(kstream);
        }
    }