예제 #1
0
        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);
        }
예제 #2
0
        void OnMaterialInfoReceived(string name, MaterialInfo matInfo)
        {
            Material mat;
            Shader   refShader = null;
            bool     result    = customShaders.GetTable().TryGetValue(name.TrimEnd('\0'), out refShader);

            if (result)
            {
                mat = new Material(refShader);
            }
            else
            {
                mat = new Material(defaultShader);
            }

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

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

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

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

                    case 4://ShaderUtil.ShaderPropertyType.TexEnv:
                        foreach (KeyValuePair <string, Texture2D> pair in m_textureList)
                        {
                            if (pair.Key == info.value)
                            {
                                Texture2D texture = pair.Value;
                                mat.SetTexture(info.name, pair.Value);
                            }
                        }
                        break;
                    }
                }
                        //end of foreach
                          
            }
            else
            {
                Debug.LogError("Not found ANY shader!");
            }

            KeyValuePair <string, Material> matPair = new KeyValuePair <string, Material>(matInfo.name, mat);

            m_materialList.Add(matPair);
        }
예제 #3
0
        void OnMaterialInfoReceived(string name, MaterialInfo info)
        {
            Material mat;
            Shader   refShader = null;
            bool     result    = customShaders.GetTable().TryGetValue(name.TrimEnd('\0'), out refShader);

            if (result)
            {
                if (refShader != null)
                {
                    mat = new Material(refShader);
                }
                else
                {
                    mat = new Material(defaultShader);
                }
            }
            else
            {
                mat = new Material(defaultShader);
            }
            if (mat != null)
            {
                foreach (MaterialPropertyInfo tinfo in info.properties)
                {
                    switch (tinfo.type)
                    {
                    case 0:                    //ShaderUtil.ShaderPropertyType.Color:
                    {
                        Color col = JsonUtility.FromJson <Color>(tinfo.value);
                        mat.SetColor(tinfo.name, col);
                    }
                    break;

                    case 1:                    //ShaderUtil.ShaderPropertyType.Vector:
                    {
                        Vector4 vec = JsonUtility.FromJson <Vector4>(tinfo.value);
                        mat.SetVector(tinfo.name, vec);
                    }
                    break;

                    case 2:                    //ShaderUtil.ShaderPropertyType.Float:
                    {
                        float value = JsonUtility.FromJson <float>(tinfo.value);
                        mat.SetFloat(tinfo.name, value);
                    }
                    break;

                    case 3:                    //ShaderUtil.ShaderPropertyType.Range:
                    {
                        float value = JsonUtility.FromJson <float>(tinfo.value);
                        mat.SetFloat(tinfo.name, value);
                    }
                    break;

                    case 4:                    //ShaderUtil.ShaderPropertyType.TexEnv:
                    {
                        foreach (KeyValuePair <string, Texture2D> pair in textureList)
                        {
                            if (pair.Key == tinfo.value)
                            {
                                Texture2D texture = pair.Value;
                                mat.SetTexture(tinfo.name, pair.Value);
                            }
                        }
                    }
                    break;
                    }
                }
                //end of foreach
            }

            KeyValuePair <string, Material> matPair = new KeyValuePair <string, Material>(info.name, mat);

            materialList.Add(matPair);
            requestQueue--;
        }