Exemple #1
0
        public static Mesh DeserializeFromBinary(byte[] data, int offsetBytes, int dataSize, int containerSize, out List <string> refMaterials)
        {
            byte[] buffer = new byte[dataSize];
            Buffer.BlockCopy(data, offsetBytes, buffer, 0, dataSize);
            MeshInfo meshInfo = InfoConverter.Deserialize <MeshInfo>(buffer);

            Mesh mesh = new Mesh();

            mesh.name = meshInfo.name + "_stm";

            Vector3[] verts = new Vector3[meshInfo.vertexCount];
            mesh.SetVertices(new List <Vector3>(verts));
            mesh.bounds = new Bounds(
                Vector3.zero, new Vector3(containerSize / 2.0f, containerSize, containerSize / 2.0f)
                );
            List <int> multiIndices = meshInfo.indices;
            int        offset       = 0;

            mesh.subMeshCount = meshInfo.subMeshCount;
            for (int i = 0; i < meshInfo.subMeshCount; i++)
            {
                int        indicesCnt = meshInfo.indicesCounts[i];
                List <int> indices    = multiIndices.GetRange(offset, indicesCnt);
                offset += indicesCnt;
                mesh.SetIndices(indices.ToArray(), MeshTopology.Triangles, i);
            }
            refMaterials = meshInfo.materialNames;

            mesh.uv  = meshInfo.uv;
            mesh.uv2 = meshInfo.uv2;
            mesh.uv3 = meshInfo.uv3;
            mesh.uv4 = meshInfo.uv4;

            return(mesh);
        }
        public static Material DeserializeFromBinary(
            byte[] data, int offsetBytes, int dataSize,
            ShaderTable shaderTable, Shader defaultShader, Dictionary <string, Texture2D> textures
            )
        {
            byte[] buffer = new byte[dataSize];
            Buffer.BlockCopy(data, offsetBytes, buffer, 0, dataSize);
            MaterialInfo materialInfo = InfoConverter.Deserialize <MaterialInfo>(buffer);
            string       name         = materialInfo.name;

            Shader   shader   = null;
            Material material = new Material(
                shaderTable.GetTable().TryGetValue(name.TrimEnd('\0'), out shader) ? shader : defaultShader
                );

            material.name = name;

            foreach (MaterialPropertyInfo info in materialInfo.properties)
            {
                switch (info.type)
                {
                case 0://ShaderUtil.ShaderPropertyType.Color:
                    Color col = JsonUtility.FromJson <Color>(info.value);
                    material.SetColor(info.name, col);
                    break;

                case 1://ShaderUtil.ShaderPropertyType.Vector:
                    Vector4 vec = JsonUtility.FromJson <Vector4>(info.value);
                    material.SetVector(info.name, vec);
                    break;

                case 2://ShaderUtil.ShaderPropertyType.Float:
                    float fValue = JsonUtility.FromJson <float>(info.value);
                    material.SetFloat(info.name, fValue);
                    break;

                case 3://ShaderUtil.ShaderPropertyType.Range:
                    float rValue = JsonUtility.FromJson <float>(info.value);
                    material.SetFloat(info.name, rValue);
                    break;

                case 4://ShaderUtil.ShaderPropertyType.TexEnv:
                    Texture2D texture = null;
                    if (textures.TryGetValue(info.value, out texture))
                    {
                        material.SetTexture(info.name, texture);
                    }
                    break;
                }
            }

            return(material);
        }