Exemple #1
0
        public override Material GetOrCreate(GetTextureItemFunc getTexture)
        {
            if (getTexture == null)
            {
                getTexture = _ => null;
            }

            var material = CreateMaterial(ShaderName);

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

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

                        // Texture Offset and Scale
                        SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
                    }

                    if (m_src.pbrMetallicRoughness.metallicRoughnessTexture != null && m_src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                    {
                        material.EnableKeyword("_METALLICGLOSSMAP");
                        var texture = getTexture(m_src.pbrMetallicRoughness.metallicRoughnessTexture.index);
                        if (texture != null)
                        {
                            var prop = "_MetallicGlossMap";
                            // Bake roughnessFactor values into a texture.
                            material.SetTexture(prop, texture.ConvertTexture(prop, m_src.pbrMetallicRoughness.roughnessFactor));
                        }

                        material.SetFloat("_Metallic", 1.0f);
                        // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
                        material.SetFloat("_GlossMapScale", 1.0f);

                        // Texture Offset and Scale
                        SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap");
                    }
                    else
                    {
                        material.SetFloat("_Metallic", m_src.pbrMetallicRoughness.metallicFactor);
                        material.SetFloat("_Glossiness", 1.0f - m_src.pbrMetallicRoughness.roughnessFactor);
                    }
                }

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

                    // Texture Offset and Scale
                    SetTextureOffsetAndScale(material, m_src.normalTexture, "_BumpMap");
                }

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

                    // Texture Offset and Scale
                    SetTextureOffsetAndScale(material, m_src.occlusionTexture, "_OcclusionMap");
                }

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

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

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

                        // Texture Offset and Scale
                        SetTextureOffsetAndScale(material, m_src.emissiveTexture, "_EmissionMap");
                    }
                }

                BlendMode blendMode = BlendMode.Opaque;
                // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980
                switch (m_src.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", m_src.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);
        }
Exemple #2
0
        public override Material GetOrCreate(GetTextureItemFunc getTexture)
        {
            var item       = m_vrmMaterial;
            var shaderName = item.shader;
            var shader     = Shader.Find(shaderName);

            if (shader == null)
            {
                //
                // no shader
                //
                if (VRM_SHADER_NAMES.Contains(shaderName))
                {
                    Debug.LogErrorFormat("shader {0} not found. set Assets/VRM/Shaders/VRMShaders to Edit - project setting - Graphics - preloaded shaders", shaderName);
                }
                else
                {
                    Debug.LogWarningFormat("unknown shader {0}.", shaderName);
                }
                return(MaterialFactory.CreateMaterial(m_index, m_src, m_hasVertexColor).GetOrCreate(getTexture));
            }

            //
            // restore VRM material
            //
            var material = new Material(shader);

            material.name        = item.name;
            material.renderQueue = item.renderQueue;

            foreach (var kv in item.floatProperties)
            {
                material.SetFloat(kv.Key, kv.Value);
            }
            foreach (var kv in item.vectorProperties)
            {
                if (item.textureProperties.ContainsKey(kv.Key))
                {
                    // texture offset & scale
                    material.SetTextureOffset(kv.Key, new Vector2(kv.Value[0], kv.Value[1]));
                    material.SetTextureScale(kv.Key, new Vector2(kv.Value[2], kv.Value[3]));
                }
                else
                {
                    // vector4
                    var v = new Vector4(kv.Value[0], kv.Value[1], kv.Value[2], kv.Value[3]);
                    material.SetVector(kv.Key, v);
                }
            }
            foreach (var kv in item.textureProperties)
            {
                var texture = getTexture(kv.Value);
                if (texture != null)
                {
                    var converted = texture.ConvertTexture(kv.Key);
                    if (converted != null)
                    {
                        material.SetTexture(kv.Key, converted);
                    }
                    else
                    {
                        material.SetTexture(kv.Key, texture.Texture);
                    }
                }
            }
            foreach (var kv in item.keywordMap)
            {
                if (kv.Value)
                {
                    material.EnableKeyword(kv.Key);
                }
                else
                {
                    material.DisableKeyword(kv.Key);
                }
            }
            foreach (var kv in item.tagMap)
            {
                material.SetOverrideTag(kv.Key, kv.Value);
            }

            if (shaderName == MToon.Utils.ShaderName)
            {
                // TODO: Material拡張にMToonの項目が追加されたら旧バージョンのshaderPropから変換をかける
                // インポート時にUniVRMに含まれるMToonのバージョンに上書きする
                material.SetFloat(MToon.Utils.PropVersion, MToon.Utils.VersionNumber);
            }

            return(material);
        }
Exemple #3
0
 public abstract Material GetOrCreate(GetTextureItemFunc getTexture);
Exemple #4
0
        public override Material GetOrCreate(GetTextureItemFunc getTexture)
        {
            if (getTexture == null)
            {
                getTexture = _ => null;
            }

            var material = CreateMaterial(ShaderName);

            // texture
            if (m_src.pbrMetallicRoughness.baseColorTexture != null)
            {
                var texture = getTexture(m_src.pbrMetallicRoughness.baseColorTexture.index);
                if (texture != null)
                {
                    material.mainTexture = texture.Texture;
                }

                // Texture Offset and Scale
                SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
            }

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

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

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

            // VColor
            if (m_hasVertexColor)
            {
                UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply);
            }

            UniUnlit.Utils.ValidateProperties(material, true);

            return(material);
        }