Example #1
0
    public void DataReceivedAsReceiver(byte[] data, ConnectionId id)
    {
        byte comm = data[0];

        if (comm == 0x02 || comm == 0x03 || comm == 0x04)
        {
            //int objId = data[1];
            int    packCnt = data[2];
            int    current = data[3];
            byte[] buf     = new byte[data.Length - 5];
            Buffer.BlockCopy(data, 5, buf, 0, buf.Length);
            tempBuffer.AddRange(buf);
            if (current == packCnt)
            {
                if (comm == 0x02)
                {
                    GetMeshFromByteArray(tempBuffer.ToArray());
                }
                if (comm == 0x03)
                {
                    GetMaterialFromByteArray(tempBuffer.ToArray());
                }
                if (comm == 0x04)
                {
                    GetTextureFromByteArray(tempBuffer.ToArray());
                }
            }
        }

        if (comm == 0x0F || comm == 0x0E)
        {
            if (verticesReceived != null)
            {
                verticesReceived(data);
            }
        }


        if (comm == 0x12 || comm == 0x13 || comm == 0x14)
        {
            DataReceivedAsSender(data, id);
        }

        if (comm == 0x22 || comm == 0x23 || comm == 0x24)
        {
            //byte[] header = new byte[5];
            //header[0] = 0x11;
            //webRTCManager.RequestData(header, true);
            if (lastRequest != null)
            {
                webRTCManager.RequestData(lastRequest, true);
            }
            else
            {
                Debug.LogError("Some data occurs an unexpected error.");
            }
        }
    }
Example #2
0
    public void VerticesReceived(byte[] data)
    {
        if (isRequestComplete)
        {
            byte[] res = { 0x31, 0x00, 0x00, 0x00, 0x00 };
            webRtcManager.RequestData(res, false);

            lastDataType = data[0];

            if (_interpolate)
            {
                for (int i = 0; i < vertsBuf.Length; i++)
                {
                    vertsBuf[i].CopyTo(vertsBuf_old[i], 0);
                }
                position_old = position;
            }

            //Debug.Log("Received bytes: " + data.Length);

            //byte[] stampBuf = { data[1], data[2], data[3], data[4] };
            //uint stamp = BitConverter.ToUInt32(stampBuf, 0);

            int  packages     = data[7] * 65536 + data[6] * 256 + data[5];
            bool isCompressed = data[8] == 0x01 ? true : false;

            byte[][] byteVec = new byte[3][];
            for (int i = 0; i < 3; i++)
            {
                byteVec[i] = new byte[4];
                for (int j = 0; j < 4; j++)
                {
                    byteVec[i][j] = data[i * 4 + j + 9];
                }
            }
            position = new Vector3(
                BitConverter.ToSingle(byteVec[0], 0),
                BitConverter.ToSingle(byteVec[1], 0),
                BitConverter.ToSingle(byteVec[2], 0)
                );

            int offset = 21;

            byte[] buf = null;
#if UNITY_WEBGL || BROTLI_NO_COMPRESS
            buf = data;
#else
            if (isCompressed)
            {
                int    bufSize = data.Length - 21;
                byte[] rawbuf  = new byte[bufSize];
                Buffer.BlockCopy(data, 21, rawbuf, 0, bufSize);
                if (!brotli.decompressBuffer(rawbuf, ref buf))
                {
                    Debug.Log("decompress failed!");
                    return;
                }
                offset = 0;
            }
            else
            {
                buf = data;
            }
#endif

            if (data[0] == 0x0F)
            {
                linedIndices.Clear();

                for (int i = 0; i < packages; i++)
                {
                    VertexPack vp = new VertexPack();
                    vp.tx    = buf[offset];
                    vp.ty    = buf[offset + 1];
                    vp.tz    = buf[offset + 2];
                    vp.poly1 = buf[offset + 3];
                    vp.poly2 = buf[offset + 4];
                    vp.poly3 = buf[offset + 5];

                    offset += 6;

                    int hk = packageSize / 2;
                    int qk = hk / areaRange;

                    int vertCount = vp.poly3 * 65536 + vp.poly2 * 256 + vp.poly1;
                    for (int j = 0; j < vertCount; j++)
                    {
                        ByteCoord v = new ByteCoord();
                        v.p1 = buf[offset + j * 5];
                        v.p2 = buf[offset + j * 5 + 1];
                        v.p3 = buf[offset + j * 5 + 2];
                        int compress = 0;
                        compress += buf[offset + j * 5 + 3];
                        compress += (ushort)(buf[offset + j * 5 + 4] << 8);

                        v.x = (byte)(compress & 0x1F);
                        v.y = (byte)((compress >> 5) & 0x1F);
                        v.z = (byte)((compress >> 10) & 0x1F);

                        float x = ((int)vp.tx - hk) / (float)qk;
                        float y = ((int)vp.ty - hk) / (float)qk;
                        float z = ((int)vp.tz - hk) / (float)qk;
                        x += (float)v.x / (32 * (float)qk);
                        y += (float)v.y / (32 * (float)qk);
                        z += (float)v.z / (32 * (float)qk);

                        int vertIdx = v.p2 * 256 + v.p1;
                        int meshIdx = v.p3;

                        Vector3 vert = new Vector3(x, y, z);
                        vertsBuf[meshIdx][vertIdx] = vert;
                        linedIndices.Add(meshIdx * 0x10000 + vertIdx);
                    }
                    offset += (vertCount * 5);
                }
                if (getLag && _interpolate)
                {
                    for (int i = 0; i < vertsBuf.Length; i++)
                    {
                        vertsBuf[i].CopyTo(vertsBuf_old[i], 0);
                    }
                    position_old = position;
                }
                getLag       = false;
                _interpolate = interpolateVertices;
            }
            else if (data[0] == 0x0E && !getLag)
            {
                for (int i = 0; i < linedIndices.Count; i++)
                {
                    int   meshIdx = (linedIndices[i] >> 16) & 0xFF;
                    int   vertIdx = linedIndices[i] & 0xFFFF;
                    int   ix      = buf[i * 3 + offset];
                    int   iy      = buf[i * 3 + offset + 1];
                    int   iz      = buf[i * 3 + offset + 2];
                    float dx      = ((float)ix - 128f) / 128f;
                    float dy      = ((float)iy - 128f) / 128f;
                    float dz      = ((float)iz - 128f) / 128f;
                    float x       = Mathf.Sign(dx) * Mathf.Pow(Mathf.Abs(dx), 2f);
                    float y       = Mathf.Sign(dy) * Mathf.Pow(Mathf.Abs(dy), 2f);
                    float z       = Mathf.Sign(dz) * Mathf.Pow(Mathf.Abs(dz), 2f);

                    Vector3 vec = vertsBuf[meshIdx][vertIdx];
                    vec = vec + new Vector3(x, y, z);
                    vertsBuf[meshIdx][vertIdx] = vec;
                }
            }
            //currentTimeStamp = stamp;
        }
        if (!_interpolate)
        {
            if (lagTime >= 0.1f && lastDataType == 0x0E)
            {
                getLag = true;
                UpdateVerts();
                return;
            }
            lagTime = 0.0f;
            UpdateVerts();
        }
        else
        {
            timeWeight = 0.0f;
        }
    }