Ejemplo n.º 1
0
        /// StandardShader vaiables
        ///
        /// _Color
        /// _MainTex
        /// _Cutoff
        /// _Glossiness
        /// _Metallic
        /// _MetallicGlossMap
        /// _BumpScale
        /// _BumpMap
        /// _Parallax
        /// _ParallaxMap
        /// _OcclusionStrength
        /// _OcclusionMap
        /// _EmissionColor
        /// _EmissionMap
        /// _DetailMask
        /// _DetailAlbedoMap
        /// _DetailNormalMapScale
        /// _DetailNormalMap
        /// _UVSec
        /// _EmissionScaleUI
        /// _EmissionColorUI
        /// _Mode
        /// _SrcBlend
        /// _DstBlend
        /// _ZWrite
        public virtual Material CreateMaterial(int i, glTFMaterial x)
        {
            var shader = m_shaderStore.GetShader(x);
            //Debug.LogFormat("[{0}]{1}", i, shader.name);
            var material = new Material(shader);

#if UNITY_EDITOR
            // textureImporter.SaveAndReimport(); may destory this material
            material.hideFlags = HideFlags.DontUnloadUnusedAsset;
#endif

            material.name = (x == null || string.IsNullOrEmpty(x.name))
                ? string.Format("material_{0:00}", i)
                : x.name
            ;

            if (x == null)
            {
                Debug.LogWarning("glTFMaterial is empty");
                return(material);
            }

            // unlit material
            if (x.extensions != null && x.extensions.KHR_materials_unlit != null)
            {
                // texture
                if (x.pbrMetallicRoughness.baseColorTexture != null)
                {
                    var texture = m_context.GetTexture(x.pbrMetallicRoughness.baseColorTexture.index);
                    if (texture != null)
                    {
                        material.mainTexture = texture.Texture;
                    }
                }

                // color
                if (x.pbrMetallicRoughness.baseColorFactor != null && x.pbrMetallicRoughness.baseColorFactor.Length == 4)
                {
                    var color = x.pbrMetallicRoughness.baseColorFactor;
                    material.color = new Color(color[0], color[1], color[2], color[3]);
                }

                //renderMode
                if (x.alphaMode == "OPAQUE")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
                }
                else if (x.alphaMode == "BLEND")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent);
                }
                else if (x.alphaMode == "MASK")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout);
                }
                else
                {
                    // default OPAQUE
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
                }

                // culling
                if (x.doubleSided)
                {
                    UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off);
                }
                else
                {
                    UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back);
                }

                UniUnlit.Utils.ValidateProperties(material, true);

                return(material);
            }

            // PBR material
            if (x.pbrMetallicRoughness != null)
            {
                if (x.pbrMetallicRoughness.baseColorFactor != null && x.pbrMetallicRoughness.baseColorFactor.Length == 4)
                {
                    var color = x.pbrMetallicRoughness.baseColorFactor;
                    material.color = new Color(color[0], color[1], color[2], color[3]);
                }

                if (x.pbrMetallicRoughness.baseColorTexture != null && x.pbrMetallicRoughness.baseColorTexture.index != -1)
                {
                    var texture = m_context.GetTexture(x.pbrMetallicRoughness.baseColorTexture.index);
                    if (texture != null)
                    {
                        material.mainTexture = texture.Texture;
                    }
                }

                if (x.pbrMetallicRoughness.metallicRoughnessTexture != null && x.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                {
                    material.EnableKeyword("_METALLICGLOSSMAP");
                    var texture = Context.GetTexture(x.pbrMetallicRoughness.metallicRoughnessTexture.index);
                    if (texture != null)
                    {
                        var prop = "_MetallicGlossMap";
                        material.SetTexture(prop, texture.ConvertTexture(prop));
                    }

                    material.SetFloat("_Metallic", 1.0f);
                    material.SetFloat("_GlossMapScale", 1.0f - x.pbrMetallicRoughness.roughnessFactor);
                }
                else
                {
                    material.SetFloat("_Metallic", x.pbrMetallicRoughness.metallicFactor);
                    material.SetFloat("_Glossiness", 1.0f - x.pbrMetallicRoughness.roughnessFactor);
                }
            }

            if (x.normalTexture != null && x.normalTexture.index != -1)
            {
                material.EnableKeyword("_NORMALMAP");
                var texture = Context.GetTexture(x.normalTexture.index);
                if (texture != null)
                {
                    var prop = "_BumpMap";
                    material.SetTexture(prop, texture.ConvertTexture(prop));
                    material.SetFloat("_BumpScale", x.normalTexture.scale);
                }
            }

            if (x.occlusionTexture != null && x.occlusionTexture.index != -1)
            {
                var texture = Context.GetTexture(x.occlusionTexture.index);
                if (texture != null)
                {
                    var prop = "_OcclusionMap";
                    material.SetTexture(prop, texture.ConvertTexture(prop));
                    material.SetFloat("_OcclusionStrength", x.occlusionTexture.strength);
                }
            }

            if (x.emissiveFactor != null ||
                (x.emissiveTexture != null && x.emissiveTexture.index != -1))
            {
                material.EnableKeyword("_EMISSION");
                material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;

                if (x.emissiveFactor != null && x.emissiveFactor.Length == 3)
                {
                    material.SetColor("_EmissionColor", new Color(x.emissiveFactor[0], x.emissiveFactor[1], x.emissiveFactor[2]));
                }

                if (x.emissiveTexture != null && x.emissiveTexture.index != -1)
                {
                    var texture = Context.GetTexture(x.emissiveTexture.index);
                    if (texture != null)
                    {
                        material.SetTexture("_EmissionMap", texture.Texture);
                    }
                }
            }

            BlendMode blendMode = BlendMode.Opaque;
            // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980
            switch (x.alphaMode)
            {
            case "BLEND":
                blendMode = BlendMode.Fade;
                material.SetOverrideTag("RenderType", "Transparent");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.EnableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 3000;
                break;

            case "MASK":
                blendMode = BlendMode.Cutout;
                material.SetOverrideTag("RenderType", "TransparentCutout");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.SetFloat("_Cutoff", x.alphaCutoff);
                material.EnableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 2450;

                break;

            default:     // OPAQUE
                blendMode = BlendMode.Opaque;
                material.SetOverrideTag("RenderType", "");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.DisableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = -1;
                break;
            }

            material.SetFloat("_Mode", (float)blendMode);
            return(material);
        }
Ejemplo n.º 2
0
        /// StandardShader vaiables
        /// 
        /// _Color
        /// _MainTex
        /// _Cutoff
        /// _Glossiness
        /// _Metallic
        /// _MetallicGlossMap
        /// _BumpScale
        /// _BumpMap
        /// _Parallax
        /// _ParallaxMap
        /// _OcclusionStrength
        /// _OcclusionMap
        /// _EmissionColor
        /// _EmissionMap
        /// _DetailMask
        /// _DetailAlbedoMap
        /// _DetailNormalMapScale
        /// _DetailNormalMap
        /// _UVSec
        /// _EmissionScaleUI
        /// _EmissionColorUI
        /// _Mode
        /// _SrcBlend
        /// _DstBlend
        /// _ZWrite
        public static CreateMaterialFunc CreateMaterialFuncFromShader(IShaderStore shaderStore)
        {
            if (shaderStore == null) return null;

            return (ctx, i) =>
            {
                 var material = new Material(shaderStore.GetShader(ctx, i));
                 material.name = string.Format("material_{0:00}", i);

                 if (i >= 0 && i < ctx.GLTF.materials.Count)
                 {
                     var x = ctx.GLTF.materials[i];
                     if (x != null)
                     {
                         if (!string.IsNullOrEmpty(x.name))
                         {
                             material.name = ctx.GLTF.GetUniqueMaterialName(i);
                         }
                         //Debug.LogFormat("{0}: {1}", i, material.name);

                         if (x.pbrMetallicRoughness != null)
                         {
                             if (x.pbrMetallicRoughness.baseColorFactor != null)
                             {
                                 var color = x.pbrMetallicRoughness.baseColorFactor;
                                 material.color = new Color(color[0], color[1], color[2], color[3]);
                             }

                             if (x.pbrMetallicRoughness.baseColorTexture.index != -1)
                             {
                                 var texture = ctx.Textures[x.pbrMetallicRoughness.baseColorTexture.index];
                                 material.mainTexture = texture.Texture;
                             }

                             if (x.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                             {
                                 material.EnableKeyword("_METALLICGLOSSMAP");
                                 var texture = ctx.Textures[x.pbrMetallicRoughness.metallicRoughnessTexture.index];
                                 material.SetTexture("_MetallicGlossMap", texture.GetMetallicRoughnessOcclusionConverted());
                             }
                         }

                         if (x.normalTexture.index != -1)
                         {
                             material.EnableKeyword("_NORMALMAP");
                             var texture = ctx.Textures[x.normalTexture.index];
#if UNITY_EDITOR
                             var textureAssetPath = AssetDatabase.GetAssetPath(texture.Texture);
                             if (!string.IsNullOrEmpty(textureAssetPath))
                             {
                                 MarkTextureAssetAsNormalMap(textureAssetPath);
                             }
#endif
                             material.SetTexture("_BumpMap", texture.Texture);
                         }

                         if (x.occlusionTexture.index != -1)
                         {
                             var texture = ctx.Textures[x.occlusionTexture.index];
                             material.SetTexture("_OcclusionMap", texture.GetMetallicRoughnessOcclusionConverted());
                         }

                         if (x.emissiveFactor != null
                             || x.emissiveTexture.index != -1)
                         {
                             material.EnableKeyword("_EMISSION");
                             material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;

                             if (x.emissiveFactor != null)
                             {
                                 material.SetColor("_EmissionColor", new Color(x.emissiveFactor[0], x.emissiveFactor[1], x.emissiveFactor[2]));
                             }

                             if (x.emissiveTexture.index != -1)
                             {
                                 var texture = ctx.Textures[x.emissiveTexture.index];
                                 material.SetTexture("_EmissionMap", texture.Texture);
                             }
                         }
                     }
                 }

                 return material;
            };
        }
Ejemplo n.º 3
0
        /// StandardShader vaiables
        ///
        /// _Color
        /// _MainTex
        /// _Cutoff
        /// _Glossiness
        /// _Metallic
        /// _MetallicGlossMap
        /// _BumpScale
        /// _BumpMap
        /// _Parallax
        /// _ParallaxMap
        /// _OcclusionStrength
        /// _OcclusionMap
        /// _EmissionColor
        /// _EmissionMap
        /// _DetailMask
        /// _DetailAlbedoMap
        /// _DetailNormalMapScale
        /// _DetailNormalMap
        /// _UVSec
        /// _EmissionScaleUI
        /// _EmissionColorUI
        /// _Mode
        /// _SrcBlend
        /// _DstBlend
        /// _ZWrite
        public virtual Material CreateMaterial(int i, glTFMaterial x)
        {
            var shader = m_shaderStore.GetShader(x);

            Debug.LogFormat("[{0}]{1}", i, shader.name);
            var material = new Material(shader);

            material.name = (x == null || string.IsNullOrEmpty(x.name))
                ? string.Format("material_{0:00}", i)
                : x.name
            ;

            if (x != null)
            {
                if (x.pbrMetallicRoughness != null)
                {
                    if (x.pbrMetallicRoughness.baseColorFactor != null && x.pbrMetallicRoughness.baseColorFactor.Length == 4)
                    {
                        var color = x.pbrMetallicRoughness.baseColorFactor;
                        material.color = new Color(color[0], color[1], color[2], color[3]);
                    }

                    if (x.pbrMetallicRoughness.baseColorTexture != null && x.pbrMetallicRoughness.baseColorTexture.index != -1)
                    {
                        var texture = m_context.GetTexture(x.pbrMetallicRoughness.baseColorTexture.index);
                        if (texture != null)
                        {
                            material.mainTexture = texture.Texture;
                        }
                    }

                    if (x.pbrMetallicRoughness.metallicRoughnessTexture != null && x.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                    {
                        material.EnableKeyword("_METALLICGLOSSMAP");
                        var texture = Context.GetTexture(x.pbrMetallicRoughness.metallicRoughnessTexture.index);
                        if (texture != null)
                        {
                            texture.Converted = (new MetallicRoughnessConverter()).GetImportTexture(texture.Texture);
                            material.SetTexture("_MetallicGlossMap", texture.Converted);
                        }
                    }

                    material.SetFloat("_Metallic", x.pbrMetallicRoughness.metallicFactor);
                    material.SetFloat("_Glossiness", 1.0f - x.pbrMetallicRoughness.roughnessFactor);
                }

                if (x.normalTexture != null && x.normalTexture.index != -1)
                {
                    material.EnableKeyword("_NORMALMAP");
                    var texture = Context.GetTexture(x.normalTexture.index);
                    if (texture != null)
                    {
                        if (Application.isPlaying)
                        {
                            texture.Converted = (new NormalConverter()).GetImportTexture(texture.Texture);
                            material.SetTexture("_BumpMap", texture.Converted);
                        }
                        else
                        {
#if UNITY_EDITOR
                            var textureAssetPath = AssetDatabase.GetAssetPath(texture.Texture);
                            if (!string.IsNullOrEmpty(textureAssetPath))
                            {
                                TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath);
                            }
                            else
                            {
                                Debug.LogWarningFormat("no asset for {0}", texture.Texture);
                            }
                            material.SetTexture("_BumpMap", texture.Texture);
#endif
                        }

                        material.SetFloat("_BumpScale", x.normalTexture.scale);
                    }
                }

                if (x.occlusionTexture != null && x.occlusionTexture.index != -1)
                {
                    var texture = Context.GetTexture(x.occlusionTexture.index);
                    if (texture != null)
                    {
                        texture.Converted = (new OcclusionConverter()).GetImportTexture(texture.Texture);
                        material.SetTexture("_OcclusionMap", texture.Converted);
                        material.SetFloat("_OcclusionStrength", x.occlusionTexture.strength);
                    }
                }

                if (x.emissiveFactor != null ||
                    (x.emissiveTexture != null && x.emissiveTexture.index != -1))
                {
                    material.EnableKeyword("_EMISSION");
                    material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;

                    if (x.emissiveFactor != null && x.emissiveFactor.Length == 3)
                    {
                        material.SetColor("_EmissionColor", new Color(x.emissiveFactor[0], x.emissiveFactor[1], x.emissiveFactor[2]));
                    }

                    if (x.emissiveTexture.index != -1)
                    {
                        var texture = Context.GetTexture(x.emissiveTexture.index);
                        if (texture != null)
                        {
                            material.SetTexture("_EmissionMap", texture.Texture);
                        }
                    }
                }

                BlendMode blendMode = BlendMode.Opaque;
                // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980
                switch (x.alphaMode)
                {
                case "BLEND":
                    blendMode = BlendMode.Fade;
                    material.SetOverrideTag("RenderType", "Transparent");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    material.SetInt("_ZWrite", 0);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.EnableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = 3000;
                    break;

                case "MASK":
                    blendMode = BlendMode.Cutout;
                    material.SetOverrideTag("RenderType", "TransparentCutout");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.EnableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = 2450;

                    break;

                default:     // OPAQUE
                    blendMode = BlendMode.Opaque;
                    material.SetOverrideTag("RenderType", "");
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = -1;
                    break;
                }

                material.SetFloat("_Mode", (float)blendMode);
            }
            return(material);
        }
Ejemplo n.º 4
0
        /// StandardShader vaiables
        ///
        /// _Color
        /// _MainTex
        /// _Cutoff
        /// _Glossiness
        /// _Metallic
        /// _MetallicGlossMap
        /// _BumpScale
        /// _BumpMap
        /// _Parallax
        /// _ParallaxMap
        /// _OcclusionStrength
        /// _OcclusionMap
        /// _EmissionColor
        /// _EmissionMap
        /// _DetailMask
        /// _DetailAlbedoMap
        /// _DetailNormalMapScale
        /// _DetailNormalMap
        /// _UVSec
        /// _EmissionScaleUI
        /// _EmissionColorUI
        /// _Mode
        /// _SrcBlend
        /// _DstBlend
        /// _ZWrite
        public virtual Material CreateMaterial(int i, glTFMaterial x)
        {
            var shader = m_shaderStore.GetShader(x);

            Debug.LogFormat("[{0}]{1}", i, shader.name);
            var material = new Material(shader);

            material.name = (x == null || string.IsNullOrEmpty(x.name))
                ? string.Format("material_{0:00}", i)
                : x.name
            ;

            if (x != null)
            {
                if (x.pbrMetallicRoughness != null)
                {
                    if (x.pbrMetallicRoughness.baseColorFactor != null)
                    {
                        var color = x.pbrMetallicRoughness.baseColorFactor;
                        material.color = new Color(color[0], color[1], color[2], color[3]);
                    }

                    if (x.pbrMetallicRoughness.baseColorTexture != null && x.pbrMetallicRoughness.baseColorTexture.index != -1)
                    {
                        var texture = m_context.GetTexture(x.pbrMetallicRoughness.baseColorTexture.index);
                        if (texture != null)
                        {
                            material.mainTexture = texture.Texture;
                        }
                    }

                    if (x.pbrMetallicRoughness.metallicRoughnessTexture != null && x.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                    {
                        material.EnableKeyword("_METALLICGLOSSMAP");
                        var texture = Context.GetTexture(x.pbrMetallicRoughness.metallicRoughnessTexture.index);
                        if (texture != null)
                        {
                            material.SetTexture("_MetallicGlossMap", texture.GetMetallicRoughnessOcclusionConverted());
                        }
                    }
                }

                if (x.normalTexture != null && x.normalTexture.index != -1)
                {
                    material.EnableKeyword("_NORMALMAP");
                    var texture = Context.GetTexture(x.normalTexture.index);
                    if (texture != null)
                    {
#if UNITY_EDITOR
                        var textureAssetPath = AssetDatabase.GetAssetPath(texture.Texture);
                        if (!string.IsNullOrEmpty(textureAssetPath))
                        {
                            TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath);
                        }
#endif
                        material.SetTexture("_BumpMap", texture.Texture);
                    }
                }

                if (x.occlusionTexture != null && x.occlusionTexture.index != -1)
                {
                    var texture = Context.GetTexture(x.occlusionTexture.index);
                    if (texture != null)
                    {
                        material.SetTexture("_OcclusionMap", texture.GetMetallicRoughnessOcclusionConverted());
                    }
                }

                if (x.emissiveFactor != null ||
                    (x.emissiveTexture != null && x.emissiveTexture.index != -1))
                {
                    material.EnableKeyword("_EMISSION");
                    material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;

                    if (x.emissiveFactor != null)
                    {
                        material.SetColor("_EmissionColor", new Color(x.emissiveFactor[0], x.emissiveFactor[1], x.emissiveFactor[2]));
                    }

                    if (x.emissiveTexture.index != -1)
                    {
                        var texture = Context.GetTexture(x.emissiveTexture.index);
                        if (texture != null)
                        {
                            material.SetTexture("_EmissionMap", texture.Texture);
                        }
                    }
                }
            }

            return(material);
        }