public static BlenderObject CreateFromByteStream(BinaryReader br)
    {
        var bo = new BlenderObject();

        // name
        bo.name = br.ReadString();
        // mode
        bo.mode = br.ReadString();
        // transform (
        bo.matrix = Matrix4x4.identity;
        foreach (var i in Enumerable.Range(0, 16))
        {
            bo.matrix[i] = br.ReadSingle();
        }

        bool meshDataAvailable = br.ReadByte() > 0;

        if (meshDataAvailable)
        {
            // verts
            int vertsCount = br.ReadInt32();
            bo.verts = new Vector3[vertsCount / 3];
            int vertsIndex = 0;
            for (var i = 0; i < vertsCount; i += 3)
            {
                bo.verts[vertsIndex] = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                vertsIndex++;
            }
            // normals
            int normalsCount = br.ReadInt32();
            bo.normals = new Vector3[normalsCount / 3];
            int normalIndex = 0;
            for (var i = 0; i < normalsCount; i += 3)
            {
                bo.normals[normalIndex] = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                normalIndex++;
            }
            // faces
            int facesCount = br.ReadInt32();
            bo.faces = new int[facesCount];
            int faceIndex = 0;
            for (var i = 0; i < facesCount; i += 1)
            {
                bo.faces[faceIndex] = br.ReadInt32();
                faceIndex++;
            }
            // uvs
            int uvsCount = br.ReadInt32();
            bo.uvs = null;
            if (uvsCount > 0)
            {
                bo.uvs = new Vector2[uvsCount / 2];
                int uvIdex = 0;
                for (var i = 0; i < uvsCount; i += 2)
                {
                    bo.uvs[uvIdex] = new Vector2(br.ReadSingle(), br.ReadSingle());
                    uvIdex++;
                }
            }
            // materials
            int materialsCount = br.ReadInt32();
            bo.materials = null;
            if (materialsCount > 0)
            {
                bo.materials = new BlenderObjectMaterial[materialsCount];
                for (int i = 0; i < materialsCount; i++)
                {
                    bo.materials[i] = BlenderObjectMaterial.CreateFromByteStream(br);
                }
            }
        }

        // chilren
        int childrenCount = br.ReadInt32();

        bo.children = null;
        if (childrenCount > 0)
        {
            bo.children = new BlenderObject[childrenCount];
            for (int i = 0; i < childrenCount; i++)
            {
                bo.children[i] = BlenderObject.CreateFromByteStream(br);
            }
        }

        return(bo);
    }
Esempio n. 2
0
    void ProcessData(byte[] data)
    {
        using (MemoryStream ms = new MemoryStream(data))
        {
            using (BinaryReader br = new BinaryReader(ms))
            {
                byte packetType = br.ReadByte();
                int  packetSize = br.ReadInt32();

                if (packetType == TYPE_OBJECT_UPDATE)
                {
                    Debug.Log("TYPE_OBJECT_UPDATE");

                    BlenderObject bo = null;

                    // metadata
                    string tag            = br.ReadString();
                    float  timestamp      = br.ReadSingle();
                    string updatedObject  = br.ReadString();
                    string updatedMode    = br.ReadString();
                    bool   worldAnchorSet = br.ReadByte() > 0;

                    bo = BlenderObject.CreateFromByteStream(br);
                    bo.worldAnchorSet = worldAnchorSet;

                    // correct matrix
                    bo.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), new Vector3(.1f, .1f, .1f)) * bo.matrix;

                    if (bo != null)
                    {
                        ProcessBlenderObject(bo);
                    }
                }

                else if (packetType == TYPE_MATERIAL_TEXTURE)
                {
                    Debug.Log("TYPE_MATERIAL_TEXTURE");

                    int count = br.ReadByte();
                    for (int i = 0; i < count; i++)
                    {
                        int width  = br.ReadInt32();
                        int height = br.ReadInt32();

                        int    textureLength = br.ReadInt32();
                        byte[] textureData   = br.ReadBytes(textureLength);

                        string textureName = br.ReadString();

                        Debug.LogFormat("TYPE_MATERIAL_TEXTURE {0}", textureName);

                        Texture2D tex = new Texture2D(width, height);
                        if (tex.LoadImage(textureData))
                        {
                            textures[textureName] = tex;
                        }
                        else
                        {
                            Debug.LogWarningFormat("Failed to load texture {0}", textureName);
                        }
                    }

                    for (int i = blenderObjectsToBeProcessed.Count - 1; i >= 0; i--)
                    {
                        BlenderObject bo = blenderObjectsToBeProcessed[i];

                        if (CheckAndRequestTextures(bo) == 0)
                        {
                            blenderObjectsToBeProcessed.RemoveAt(i);
                            ProcessBlenderObject(bo);
                        }
                    }
                }

                else if (packetType == TYPE_OBJECT_REMOVED)
                {
                    Debug.Log("TYPE_OBJECT_REMOVED");

                    string boName = br.ReadString();

                    Debug.LogFormat("Removing BGO {0}", boName);

                    if (blenderGameObjects.ContainsKey(boName))
                    {
                        var bgo = blenderGameObjects[boName];
                        blenderGameObjects.Remove(boName);
                        Destroy(bgo.gameObject);

                        OnBlenderGameObjectDestoryed(boName);
                    }
                }

                else if (packetType == TYPE_OBJECT_WORLDANCHOR)
                {
                    Debug.Log("TYPE_OBJECT_WORLDANCHOR");

                    string boName = br.ReadString();
                    int    worldAnchorDataSize = br.ReadInt32();
                    byte[] worldAnchorData     = br.ReadBytes(worldAnchorDataSize);

                    Debug.LogFormat("Setting world anchor for BGO {0}, count {1}", boName, worldAnchorDataSize);

                    // update existing Blender Object
                    if (blenderGameObjects.ContainsKey(boName))
                    {
                        var bgo = blenderGameObjects[boName];
                        bgo.SetAnchor(worldAnchorData);

                        OnBlenderGameObjectUpdated(bgo);
                    }

                    // update pending BlenderObject
                    for (int i = blenderObjectsToBeProcessed.Count - 1; i >= 0; i--)
                    {
                        BlenderObject bo = blenderObjectsToBeProcessed[i];

                        if (bo.name.Equals(boName, StringComparison.OrdinalIgnoreCase))
                        {
                            blenderObjectsToBeProcessed.RemoveAt(i);
                            bo.worldAnchor = worldAnchorData;
                            ProcessBlenderObject(bo);
                        }
                    }
                }

                else if (packetType == TYPE_OBJECT_OPERATION)
                {
                    Debug.Log("TYPE_OBJECT_OPERATION");
                    // TODO
                }
            }
        }
    }