예제 #1
0
        private static IEnumerator LoadMaterial(BinaryReader source, string modelFolder, Action <Material> callback)
        {
            string texName     = ReadString(source);
            string texLocation = Path.Combine(modelFolder, texName);
            string matName     = ReadString(source);

            MaterialKey materialKey = new MaterialKey(matName, texLocation);

            float glossiness = 0;

            bool hasMaterialProperties = source.ReadBoolean();

            if (hasMaterialProperties)
            {
                bool hasEmissive = source.ReadBoolean();
                if (hasEmissive)
                {
                    source.ReadSingle();
                    source.ReadSingle();
                    source.ReadSingle();
                    source.ReadSingle();
                }

                bool hasShininess = source.ReadBoolean();
                if (hasShininess)
                {
                    glossiness = source.ReadSingle() / 100;
                    if (glossiness > 1)
                    {
                        glossiness = 0;
                    }
                }

                bool hasSpecular = source.ReadBoolean();
                if (hasSpecular)
                {
                    source.ReadSingle();
                    source.ReadSingle();
                    source.ReadSingle();
                    source.ReadSingle();
                }

                bool hasTransparencyColor = source.ReadBoolean();
                if (hasTransparencyColor)
                {
                    source.ReadSingle();
                    source.ReadSingle();
                    source.ReadSingle();
                    source.ReadSingle();
                }
            }

            if (cachedMaterials.ContainsKey(materialKey))
            {
                Debug.Log("Loading material from cache");
                Material cachedMaterial = cachedMaterials[materialKey];
                callback.Invoke(cachedMaterial);
            }
            else
            {
                Material material = new Material(GraphicsManager.Instance.WomDefaultMaterial);
                material.name = matName;

                TextureReference textureReference = TextureReference.GetTextureReference(texLocation);

                Texture2D texture = null;
                if (textureReference != null)
                {
                    yield return(textureReference.LoadOrGetTexture(loadedTexture => texture = loadedTexture));
                }

                if (texture)
                {
                    material.SetTexture(ShaderPropertyIds.MainTex, texture);
                }
                else
                {
                    material.SetColor(ShaderPropertyIds.Color, new Color(1, 1, 1, 0));
                }

                material.SetFloat(ShaderPropertyIds.Glossiness, glossiness);

                cachedMaterials[materialKey] = material;
                callback.Invoke(material);
            }
        }