Beispiel #1
0
        Material GetUnlitMaterial(Schema.Material gltfMaterial)
        {
            Shader shader;

#if UNITY_SHADER_GRAPH_12_OR_NEWER
            if (s_UnlitShader == null)
            {
                s_UnlitShader = FindShader(SHADER_UNLIT);
            }
            shader = s_UnlitShader;
#else
            var features = GetUnlitShaderFeatures(gltfMaterial);
            if (!unlitShaders.TryGetValue(features, out shader))
            {
                var shaderName = GetUnlitShaderName(features);
                shader = FindShader(shaderName);
                unlitShaders[features] = shader;
            }
#endif
            if (shader == null)
            {
                return(null);
            }
            var mat = new Material(shader);
#if UNITY_EDITOR
            mat.doubleSidedGI = gltfMaterial.doubleSided;
#endif
            return(mat);
        }
Beispiel #2
0
        static Material GetUnlitMaterial(Schema.Material gltfMaterial)
        {
            var    features    = GetUnlitShaderFeatures(gltfMaterial);
            bool   doubleSided = (features & UnlitShaderFeatures.DoubleSided) != 0;
            Shader shader      = null;

            if (!unlitShaders.TryGetValue(features, out shader))
            {
                bool alphaBlend = (features & UnlitShaderFeatures.AlphaBlend) != 0;
                var  shaderName = string.Format(
                    "{0}{1}{2}",
                    SHADER_UNLIT,
                    alphaBlend ? "-Blend" : "-Opaque",
                    doubleSided ? "-double" : ""
                    );
                shader = FindShader(shaderName);
                unlitShaders[features] = shader;
            }
            if (shader == null)
            {
                return(null);
            }
            var mat = new Material(shader);

#if UNITY_EDITOR
            mat.doubleSidedGI = gltfMaterial.doubleSided;
#endif
            return(mat);
        }
Beispiel #3
0
        protected virtual ShaderMode?ApplyTransmissionShaderFeatures(Schema.Material gltfMaterial)
        {
            // Makeshift approximation
            Color      baseColorLinear = Color.white;
            var        premul          = TransmissionWorkaroundShaderMode(gltfMaterial.extensions.KHR_materials_transmission, ref baseColorLinear);
            ShaderMode?sm = premul ? ShaderMode.Premultiply : ShaderMode.Blend;

            return(sm);
        }
Beispiel #4
0
        protected virtual void SetAlphaModeMask(Schema.Material gltfMaterial, Material material)
        {
            material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff);
#if USING_HDRP_10_OR_NEWER || USING_URP_12_OR_NEWER
            material.EnableKeyword(KW_ALPHATEST_ON);
            material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_CUTOUT);
            material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal); //3
#endif
        }
 protected override ShaderMode?ApplyTransmissionShaderFeatures(Schema.Material gltfMaterial)
 {
     if (!supportsCameraOpaqueTexture)
     {
         // Fall back to makeshift approximation via premultiply or blend
         return(base.ApplyTransmissionShaderFeatures(gltfMaterial));
     }
     // No explicitly change in shader features
     return(null);
 }
        Material GetUnlitMaterial(Schema.Material gltfMaterial)
        {
            Shader shader = GetUnlitShader(gltfMaterial);

            if (shader == null)
            {
                return(null);
            }
            var mat = new Material(shader);

#if UNITY_EDITOR
            mat.doubleSidedGI = gltfMaterial.doubleSided;
#endif
            return(mat);
        }
Beispiel #7
0
        static SpecularShaderFeatures GetSpecularShaderFeatures(Schema.Material gltfMaterial)
        {
            var feature = SpecularShaderFeatures.Default;

            if (gltfMaterial.doubleSided)
            {
                feature |= SpecularShaderFeatures.DoubleSided;
            }

            if (gltfMaterial.alphaModeEnum != AlphaMode.OPAQUE)
            {
                feature |= SpecularShaderFeatures.AlphaBlend;
            }
            return(feature);
        }
        protected override ShaderMode?ApplyTransmissionShaderFeatures(Schema.Material gltfMaterial)
        {
            if (!supportsCameraOpaqueTexture)
            {
                // Fall back to makeshift approximation via premultiply or blend
                return(base.ApplyTransmissionShaderFeatures(gltfMaterial));
            }

            if (gltfMaterial?.extensions?.KHR_materials_transmission != null &&
                gltfMaterial.extensions.KHR_materials_transmission.transmissionFactor > 0f)
            {
                return(ShaderMode.Blend);
            }

            // No explicitly change in shader features
            return(null);
        }
Beispiel #9
0
 protected override void SetShaderModeBlend(Schema.Material gltfMaterial, Material material)
 {
     material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_TRANSPARENT);
     material.EnableKeyword(KW_SURFACE_TYPE_TRANSPARENT);
     material.EnableKeyword(KW_DISABLE_SSR_TRANSPARENT);
     material.EnableKeyword(KW_ENABLE_FOG_ON_TRANSPARENT);
     material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPrepass, false);
     material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPostpass, false);
     material.SetShaderPassEnabled(k_ShaderPassTransparentBackface, false);
     material.SetShaderPassEnabled(k_ShaderPassRayTracingPrepass, false);
     material.SetShaderPassEnabled(k_ShaderPassDepthOnlyPass, false);
     material.SetFloat(srcBlendPropId, (int)BlendMode.SrcAlpha);                //5
     material.SetFloat(dstBlendPropId, (int)BlendMode.OneMinusSrcAlpha);        //10
     material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal);       //3
     material.SetFloat(k_AlphaDstBlendPropId, (int)BlendMode.OneMinusSrcAlpha); //10
     material.SetFloat(k_Surface, 1);
     material.SetFloat(zWritePropId, 0);
 }
        Shader GetUnlitShader(Schema.Material gltfMaterial)
        {
            Shader shader;

#if UNITY_SHADER_GRAPH_12_OR_NEWER
            if (s_UnlitShader == null)
            {
                s_UnlitShader = LoadShaderByName(SHADER_UNLIT);
            }
            shader = s_UnlitShader;
#else
            var features = GetUnlitShaderFeatures(gltfMaterial);
            if (!unlitShaders.TryGetValue(features, out shader) || shader == null)
            {
                shader = LoadShaderByName(GetUnlitShaderName(features));
                unlitShaders[features] = shader;
            }
#endif
            return(shader);
        }
Beispiel #11
0
        protected MetallicShaderFeatures GetMetallicShaderFeatures(Schema.Material gltfMaterial)
        {
            var        feature = MetallicShaderFeatures.Default;
            ShaderMode?sm      = null;

            if (gltfMaterial.extensions != null)
            {
                if (gltfMaterial.extensions.KHR_materials_clearcoat != null &&
                    gltfMaterial.extensions.KHR_materials_clearcoat.clearcoatFactor > 0)
                {
                    feature |= MetallicShaderFeatures.ClearCoat;
                }
                if (gltfMaterial.extensions.KHR_materials_sheen != null &&
                    gltfMaterial.extensions.KHR_materials_sheen.sheenColor.maxColorComponent > 0)
                {
                    feature |= MetallicShaderFeatures.Sheen;
                }

                if (
                    gltfMaterial.extensions.KHR_materials_transmission != null &&
                    gltfMaterial.extensions.KHR_materials_transmission.transmissionFactor > 0
                    )
                {
                    sm = ApplyTransmissionShaderFeatures(gltfMaterial);
                }
            }

            if (gltfMaterial.doubleSided)
            {
                feature |= MetallicShaderFeatures.DoubleSided;
            }

            if (!sm.HasValue)
            {
                sm = gltfMaterial.alphaModeEnum != AlphaMode.OPAQUE ? ShaderMode.Blend : ShaderMode.Opaque;
            }

            feature |= (MetallicShaderFeatures)sm;

            return(feature);
        }
Beispiel #12
0
        static Material GetUnlitMaterial(Schema.Material gltfMaterial)
        {
            int index = gltfMaterial.doubleSided ? 0 : 1;

            if (unlitShaders[index] == null)
            {
                var mode       = gltfMaterial.alphaModeEnum != AlphaMode.OPAQUE ? ShaderMode.Blend : ShaderMode.Opaque;
                var shaderName = string.Format("{0}-{1}{2}",
                                               SHADER_UNLIT,
                                               mode,
                                               gltfMaterial.doubleSided ? "-double" : "");
                unlitShaders[index] = FindShader(shaderName);
            }
            if (unlitShaders[index] == null)
            {
                return(null);
            }
            var mat = new Material(unlitShaders[index]);

#if UNITY_EDITOR
            mat.doubleSidedGI = gltfMaterial.doubleSided;
#endif
            return(mat);
        }
 protected override void SetDoubleSided(Schema.Material gltfMaterial, Material material)
 {
     base.SetDoubleSided(gltfMaterial, material);
     material.SetFloat(cullPropId, (int)CullMode.Off);
 }
        /// <inheritdoc />
        public override Material GenerateMaterial(
            Schema.Material gltfMaterial,
            IGltfReadable gltf
            )
        {
            Material material;

            if (gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness != null)
            {
                material = GetPbrSpecularGlossinessMaterial(gltfMaterial.doubleSided);
            }
            else
            if (gltfMaterial.extensions?.KHR_materials_unlit != null)
            {
                material = GetUnlitMaterial(gltfMaterial.doubleSided);
            }
            else
            {
                material = GetPbrMetallicRoughnessMaterial(gltfMaterial.doubleSided);
            }

            if (material == null)
            {
                return(null);
            }

            material.name = gltfMaterial.name;

            StandardShaderMode shaderMode = StandardShaderMode.Opaque;
            Color baseColorLinear         = Color.white;

            if (gltfMaterial.alphaModeEnum == AlphaMode.MASK)
            {
                material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff);
                shaderMode = StandardShaderMode.Cutout;
            }
            else if (gltfMaterial.alphaModeEnum == AlphaMode.BLEND)
            {
                SetAlphaModeBlend(material);
                shaderMode = StandardShaderMode.Fade;
            }

            if (gltfMaterial.extensions != null)
            {
                // Specular glossiness
                Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness;
                if (specGloss != null)
                {
                    baseColorLinear = specGloss.diffuseColor;
                    material.SetVector(specColorPropId, specGloss.specularColor);
                    material.SetFloat(glossinessPropId, specGloss.glossinessFactor);

                    TrySetTexture(
                        specGloss.diffuseTexture,
                        material,
                        gltf,
                        mainTexPropId,
                        mainTexScaleTransform,
                        mainTexRotation,
                        mainTexUVChannelPropId
                        );

                    if (TrySetTexture(
                            specGloss.specularGlossinessTexture,
                            material,
                            gltf,
                            specGlossMapPropId,
                            specGlossScaleTransformMapPropId,
                            specGlossMapRotationPropId,
                            specGlossMapUVChannelPropId
                            ))
                    {
                        material.EnableKeyword(KW_SPEC_GLOSS_MAP);
                    }
                }
            }

            if (gltfMaterial.pbrMetallicRoughness != null
                // If there's a specular-glossiness extension, ignore metallic-roughness
                // (according to extension specification)
                && gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness == null)
            {
                baseColorLinear = gltfMaterial.pbrMetallicRoughness.baseColor;
                material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor);
                material.SetFloat(roughnessPropId, gltfMaterial.pbrMetallicRoughness.roughnessFactor);

                TrySetTexture(
                    gltfMaterial.pbrMetallicRoughness.baseColorTexture,
                    material,
                    gltf,
                    mainTexPropId,
                    mainTexScaleTransform,
                    mainTexRotation,
                    mainTexUVChannelPropId
                    );

                if (TrySetTexture(
                        gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture,
                        material,
                        gltf,
                        metallicGlossMapPropId,
                        metallicRoughnessMapScaleTransformPropId,
                        metallicRoughnessMapRotationPropId,
                        metallicRoughnessMapUVChannelPropId
                        ))
                {
                    material.EnableKeyword(KW_METALLIC_ROUGNESS_MAP);
                }
            }

            if (TrySetTexture(
                    gltfMaterial.normalTexture,
                    material,
                    gltf,
                    bumpMapPropId,
                    bumpMapScaleTransformPropId,
                    bumpMapRotationPropId,
                    bumpMapUVChannelPropId
                    ))
            {
                material.EnableKeyword(Constants.kwNormalMap);
                material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale);
            }

            if (TrySetTexture(
                    gltfMaterial.occlusionTexture,
                    material,
                    gltf,
                    occlusionMapPropId,
                    occlusionMapScaleTransformPropId,
                    occlusionMapRotationPropId,
                    occlusionMapUVChannelPropId
                    ))
            {
                material.EnableKeyword(KW_OCCLUSION);
                material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength);
            }

            if (TrySetTexture(
                    gltfMaterial.emissiveTexture,
                    material,
                    gltf,
                    emissionMapPropId,
                    emissionMapScaleTransformPropId,
                    emissionMapRotationPropId,
                    emissionMapUVChannelPropId
                    ))
            {
                material.EnableKeyword(KW_EMISSION);
            }

            if (gltfMaterial.extensions != null)
            {
                // Transmission - Approximation
                var transmission = gltfMaterial.extensions.KHR_materials_transmission;
                if (transmission != null)
                {
#if UNITY_EDITOR
                    logger?.Warning(LogCode.MaterialTransmissionApprox);
#endif
                    // Correct transmission is not supported in Built-In renderer
                    // This is an approximation for some corner cases
                    if (transmission.transmissionFactor > 0f && transmission.transmissionTexture.index < 0)
                    {
                        var premul = TransmissionWorkaroundShaderMode(transmission, ref baseColorLinear);
                        shaderMode = premul ? StandardShaderMode.Transparent : StandardShaderMode.Fade;
                    }
                }
            }

            switch (shaderMode)
            {
            case StandardShaderMode.Cutout:
                SetAlphaModeMask(material, gltfMaterial);
                break;

            case StandardShaderMode.Fade:
                SetAlphaModeBlend(material);
                break;

            case StandardShaderMode.Transparent:
                SetAlphaModeTransparent(material);
                break;

            default:
                SetOpaqueMode(material);
                break;
            }

            material.color = baseColorLinear.gamma;

            if (gltfMaterial.emissive != Color.black)
            {
                material.SetColor(emissionColorPropId, gltfMaterial.emissive.gamma);
                material.EnableKeyword(KW_EMISSION);
            }

            return(material);
        }
Beispiel #15
0
        public override Material GenerateMaterial(Schema.Material gltfMaterial, IGltfReadable gltf)
        {
            Material material;

            MaterialType?materialType = null;
            ShaderMode   shaderMode   = ShaderMode.Opaque;

            if (gltfMaterial.extensions?.KHR_materials_unlit != null)
            {
                material     = GetUnlitMaterial(gltfMaterial);
                materialType = MaterialType.Unlit;
                shaderMode   = gltfMaterial.alphaModeEnum == AlphaMode.BLEND ? ShaderMode.Blend : ShaderMode.Opaque;
            }
            else
            {
                bool isMetallicRoughness = gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness == null;
                if (isMetallicRoughness)
                {
                    materialType = MaterialType.MetallicRoughness;
                    var metallicShaderFeatures = GetMetallicShaderFeatures(gltfMaterial);
                    material   = GetMetallicMaterial(metallicShaderFeatures);
                    shaderMode = (ShaderMode)(metallicShaderFeatures & MetallicShaderFeatures.ModeMask);
                }
                else
                {
                    materialType = MaterialType.SpecularGlossiness;
                    var specularShaderFeatures = GetSpecularShaderFeatures(gltfMaterial);
                    material = GetSpecularMaterial(specularShaderFeatures);
                    if ((specularShaderFeatures & SpecularShaderFeatures.AlphaBlend) != 0)
                    {
                        shaderMode = ShaderMode.Blend;
                    }
                }
            }

            if (material == null)
            {
                return(null);
            }

            material.name = gltfMaterial.name;

            Color       baseColorLinear = Color.white;
            RenderQueue?renderQueue     = null;

            //added support for KHR_materials_pbrSpecularGlossiness
            if (gltfMaterial.extensions != null)
            {
                Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness;
                if (specGloss != null)
                {
                    baseColorLinear = specGloss.diffuseColor;
#if UNITY_SHADER_GRAPH_12_OR_NEWER
                    material.SetVector(specularFactorPropId, specGloss.specularColor);
#else
                    material.SetVector(specColorPropId, specGloss.specularColor);
#endif
                    material.SetFloat(smoothnessPropId, specGloss.glossinessFactor);

                    TrySetTexture(
                        specGloss.diffuseTexture,
                        material,
                        gltf,
                        baseMapPropId,
                        baseMapScaleTransformPropId,
                        baseMapRotationPropId,
                        baseMapUVChannelPropId
                        );

                    if (TrySetTexture(
                            specGloss.specularGlossinessTexture,
                            material,
                            gltf,
                            specGlossMapPropId,
                            specGlossScaleTransformMapPropId,
                            specGlossMapRotationPropId,
                            specGlossMapUVChannelPropId
                            ))
                    {
                        // material.EnableKeyword();
                    }
                }
            }

            if (gltfMaterial.pbrMetallicRoughness != null
                // If there's a specular-glossiness extension, ignore metallic-roughness
                // (according to extension specification)
                && gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness == null)
            {
                baseColorLinear = gltfMaterial.pbrMetallicRoughness.baseColor;

                if (materialType != MaterialType.SpecularGlossiness)
                {
                    // baseColorTexture can be used by both MetallicRoughness AND Unlit materials
                    TrySetTexture(
                        gltfMaterial.pbrMetallicRoughness.baseColorTexture,
                        material,
                        gltf,
                        baseMapPropId,
                        baseMapScaleTransformPropId,
                        baseMapRotationPropId,
                        baseMapUVChannelPropId
                        );
                }

                if (materialType == MaterialType.MetallicRoughness)
                {
                    material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor);
                    material.SetFloat(smoothnessPropId, 1 - gltfMaterial.pbrMetallicRoughness.roughnessFactor);

                    if (TrySetTexture(
                            gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture,
                            material,
                            gltf,
                            metallicRoughnessMapPropId,
                            metallicRoughnessMapScaleTransformPropId,
                            metallicRoughnessMapRotationPropId,
                            metallicRoughnessMapUVChannelPropId
                            ))
                    {
                        // material.EnableKeyword(KW_METALLIC_ROUGHNESS_MAP);
                    }

                    // TODO: When the occlusionTexture equals the metallicRoughnessTexture, we could sample just once instead of twice.
                    // if (!DifferentIndex(gltfMaterial.occlusionTexture,gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture)) {
                    //    ...
                    // }
                }
            }

            if (TrySetTexture(
                    gltfMaterial.normalTexture,
                    material,
                    gltf,
                    bumpMapPropId,
                    bumpMapScaleTransformPropId,
                    bumpMapRotationPropId,
                    bumpMapUVChannelPropId
                    ))
            {
                // material.EnableKeyword(Constants.kwNormalMap);
                material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale);
            }

            if (TrySetTexture(
                    gltfMaterial.occlusionTexture,
                    material,
                    gltf,
                    occlusionMapPropId,
                    occlusionMapScaleTransformPropId,
                    occlusionMapRotationPropId,
                    occlusionMapUVChannelPropId
                    ))
            {
                material.EnableKeyword(KW_OCCLUSION);
                material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength);
            }

            if (TrySetTexture(
                    gltfMaterial.emissiveTexture,
                    material,
                    gltf,
                    emissionMapPropId,
                    emissionMapScaleTransformPropId,
                    emissionMapRotationPropId,
                    emissionMapUVChannelPropId
                    ))
            {
                material.EnableKeyword(KW_EMISSION);
            }

            if (gltfMaterial.extensions != null)
            {
                // Transmission - Approximation
                var transmission = gltfMaterial.extensions.KHR_materials_transmission;
                if (transmission != null)
                {
                    renderQueue = ApplyTransmission(ref baseColorLinear, gltf, transmission, material, renderQueue);
                }
            }

            if (gltfMaterial.alphaModeEnum == AlphaMode.MASK)
            {
                SetAlphaModeMask(gltfMaterial, material);
#if USING_HDRP
                if (gltfMaterial.extensions?.KHR_materials_unlit != null)
                {
                    renderQueue = RenderQueue.Transparent;
                }
                else
#endif
                renderQueue = RenderQueue.AlphaTest;
            }
            else
            {
                material.SetFloat(cutoffPropId, 0);
                // double sided opaque would make errors in HDRP 7.3 otherwise
                material.SetOverrideTag(TAG_MOTION_VECTOR, TAG_MOTION_VECTOR_USER);
                material.SetShaderPassEnabled(k_MotionVectorsPass, false);
            }
            if (!renderQueue.HasValue)
            {
                if (shaderMode == ShaderMode.Opaque)
                {
                    renderQueue = gltfMaterial.alphaModeEnum == AlphaMode.MASK
                        ? RenderQueue.AlphaTest
                        : RenderQueue.Geometry;
                }
                else
                {
                    renderQueue = RenderQueue.Transparent;
                }
            }

            material.renderQueue = (int)renderQueue.Value;

            if (gltfMaterial.doubleSided)
            {
                SetDoubleSided(gltfMaterial, material);
            }

            switch (shaderMode)
            {
            case ShaderMode.Opaque:
                SetShaderModeOpaque(gltfMaterial, material);
                break;

            case ShaderMode.Blend:
                SetShaderModeBlend(gltfMaterial, material);
                break;

            case ShaderMode.Premultiply:
                SetShaderModePremultiply(gltfMaterial, material);
                break;
            }

            material.SetVector(baseColorPropId, baseColorLinear);

            if (gltfMaterial.emissive != Color.black)
            {
                material.SetColor(emissionColorPropId, gltfMaterial.emissive);
                material.EnableKeyword(KW_EMISSION);
            }

            return(material);
        }
Beispiel #16
0
        public override Material GenerateMaterial(
            Schema.Material gltfMaterial,
            ref Schema.Texture[] textures,
            ref Schema.Image[] schemaImages,
            ref Dictionary <int, Texture2D>[] imageVariants
            )
        {
            Material material;

            MaterialType?materialType = null;
            ShaderMode   shaderMode   = ShaderMode.Opaque;

            if (gltfMaterial.extensions?.KHR_materials_unlit != null)
            {
                material     = GetUnlitMaterial(gltfMaterial);
                materialType = MaterialType.Unlit;
                shaderMode   = gltfMaterial.alphaModeEnum != AlphaMode.OPAQUE ? ShaderMode.Blend : ShaderMode.Opaque;
            }
            else
            {
                bool isMetallicRoughness = gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness == null;
                if (isMetallicRoughness)
                {
                    materialType = MaterialType.MetallicRoughness;
                    var metallicShaderFeatures = GetMetallicShaderFeatures(gltfMaterial);
                    material   = GetMetallicMaterial(metallicShaderFeatures);
                    shaderMode = (ShaderMode)(metallicShaderFeatures & MetallicShaderFeatures.ModeMask);
                }
                else
                {
                    materialType = MaterialType.SpecularGlossiness;
                    var specularShaderFeatures = GetSpecularShaderFeatures(gltfMaterial);
                    material = GetSpecularMaterial(specularShaderFeatures);
                    if ((specularShaderFeatures & SpecularShaderFeatures.AlphaBlend) != 0)
                    {
                        shaderMode = ShaderMode.Blend;
                    }
                }
            }

            if (material == null)
            {
                return(null);
            }

            material.name = gltfMaterial.name;

            Color       baseColorLinear = Color.white;
            RenderQueue?renderQueue     = null;

            //added support for KHR_materials_pbrSpecularGlossiness
            if (gltfMaterial.extensions != null)
            {
                Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness;
                if (specGloss != null)
                {
                    baseColorLinear = specGloss.diffuseColor;
                    material.SetVector(specColorPropId, specGloss.specularColor);
                    material.SetFloat(smoothnessPropId, specGloss.glossinessFactor);

                    TrySetTexture(specGloss.diffuseTexture, material, baseMapPropId, ref textures, ref schemaImages, ref imageVariants);

                    if (TrySetTexture(specGloss.specularGlossinessTexture, material, specGlossMapPropId, ref textures, ref schemaImages, ref imageVariants))
                    {
                        // material.EnableKeyword();
                    }
                }
            }

            if (gltfMaterial.pbrMetallicRoughness != null)
            {
                baseColorLinear = gltfMaterial.pbrMetallicRoughness.baseColor;

                if (materialType != MaterialType.SpecularGlossiness)
                {
                    // baseColorTexture can be used by both MetallicRoughness AND Unlit materials
                    TrySetTexture(
                        gltfMaterial.pbrMetallicRoughness.baseColorTexture,
                        material,
                        baseMapPropId,
                        ref textures,
                        ref schemaImages,
                        ref imageVariants
                        );
                }

                if (materialType == MaterialType.MetallicRoughness)
                {
                    material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor);
                    material.SetFloat(smoothnessPropId, 1 - gltfMaterial.pbrMetallicRoughness.roughnessFactor);

                    if (TrySetTexture(gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture, material, metallicRoughnessTexturePropId, ref textures, ref schemaImages, ref imageVariants))
                    {
                        // material.EnableKeyword(KW_METALLIC_ROUGHNESS_MAP);
                    }

                    // TODO: When the occlusionTexture equals the metallicRoughnessTexture, we could sample just once instead of twice.
                    // if (!DifferentIndex(gltfMaterial.occlusionTexture,gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture)) {
                    //    ...
                    // }
                }
            }

            if (TrySetTexture(gltfMaterial.normalTexture, material, bumpMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                // material.EnableKeyword(KW_NORMALMAP);
                material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale);
            }

            if (TrySetTexture(gltfMaterial.occlusionTexture, material, occlusionMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                material.EnableKeyword(KW_OCCLUSION);
                material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength);
            }

            if (TrySetTexture(gltfMaterial.emissiveTexture, material, emissionMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                material.EnableKeyword(KW_EMISSION);
            }

            if (gltfMaterial.extensions != null)
            {
                // Transmission - Approximation
                var transmission = gltfMaterial.extensions.KHR_materials_transmission;
                if (transmission != null)
                {
                    renderQueue = ApplyTransmission(ref baseColorLinear, ref textures, ref schemaImages, ref imageVariants, transmission, material, renderQueue);
                }
            }

            if (gltfMaterial.alphaModeEnum == AlphaMode.MASK)
            {
                material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff);
            }
            else
            {
                material.SetFloat(cutoffPropId, 0);
                // double sided opaque would make errors in HDRP 7.3 otherwise
                material.SetOverrideTag("MotionVector", "User");
                material.SetShaderPassEnabled("MOTIONVECTORS", false);
            }
            if (!renderQueue.HasValue)
            {
                if (shaderMode == ShaderMode.Opaque)
                {
                    renderQueue = gltfMaterial.alphaModeEnum == AlphaMode.MASK
                        ? RenderQueue.AlphaTest
                        : RenderQueue.Geometry;
                }
                else
                {
                    renderQueue = RenderQueue.Transparent;
                }
            }

            material.renderQueue = (int)renderQueue.Value;

            material.SetVector(baseColorPropId, baseColorLinear);

            if (gltfMaterial.emissive != Color.black)
            {
                material.SetColor(emissionColorPropId, gltfMaterial.emissive);
                material.EnableKeyword(KW_EMISSION);
            }

            return(material);
        }
Beispiel #17
0
 protected override void SetAlphaModeMask(Schema.Material gltfMaterial, Material material)
 {
     base.SetAlphaModeMask(gltfMaterial, material);
     material.SetFloat(k_AlphaClip, 1);
 }
 public static void SetAlphaModeMask(UnityEngine.Material material, Schema.Material gltfMaterial)
 {
     SetAlphaModeMask(material, gltfMaterial.alphaCutoff);
 }
        public override Material GenerateMaterial(
            Schema.Material gltfMaterial,
            ref Schema.Texture[] textures,
            ref Schema.Image[] schemaImages,
            ref Dictionary <int, Texture2D>[] imageVariants
            )
        {
            Material material;

            if (gltfMaterial.extensions?.KHR_materials_pbrSpecularGlossiness != null)
            {
                material = GetPbrSpecularGlossinessMaterial(gltfMaterial.doubleSided);
            }
            else
            if (gltfMaterial.extensions?.KHR_materials_unlit != null)
            {
                material = GetUnlitMaterial(gltfMaterial.doubleSided);
            }
            else
            {
                material = GetPbrMetallicRoughnessMaterial(gltfMaterial.doubleSided);
            }

            if (material == null)
            {
                return(null);
            }

            material.name = gltfMaterial.name;

            StandardShaderMode shaderMode = StandardShaderMode.Opaque;
            Color baseColorLinear         = Color.white;

            if (gltfMaterial.alphaModeEnum == AlphaMode.MASK)
            {
                material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff);
                shaderMode = StandardShaderMode.Cutout;
            }
            else if (gltfMaterial.alphaModeEnum == AlphaMode.BLEND)
            {
                SetAlphaModeBlend(material);
                shaderMode = StandardShaderMode.Fade;
            }

            if (gltfMaterial.extensions != null)
            {
                // Specular glossiness
                Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness;
                if (specGloss != null)
                {
                    baseColorLinear = specGloss.diffuseColor;
                    material.SetVector(specColorPropId, specGloss.specularColor);
                    material.SetFloat(glossinessPropId, specGloss.glossinessFactor);

                    TrySetTexture(specGloss.diffuseTexture, material, mainTexPropId, ref textures, ref schemaImages, ref imageVariants);

                    if (TrySetTexture(specGloss.specularGlossinessTexture, material, specGlossMapPropId, ref textures, ref schemaImages, ref imageVariants))
                    {
                        material.EnableKeyword(KW_SPEC_GLOSS_MAP);
                    }
                }
            }

            if (gltfMaterial.pbrMetallicRoughness != null)
            {
                baseColorLinear = gltfMaterial.pbrMetallicRoughness.baseColor;
                material.SetFloat(metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor);
                material.SetFloat(roughnessPropId, gltfMaterial.pbrMetallicRoughness.roughnessFactor);

                TrySetTexture(
                    gltfMaterial.pbrMetallicRoughness.baseColorTexture,
                    material,
                    mainTexPropId,
                    ref textures,
                    ref schemaImages,
                    ref imageVariants
                    );

                if (TrySetTexture(gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture, material, metallicGlossMapPropId, ref textures, ref schemaImages, ref imageVariants))
                {
                    material.EnableKeyword(KW_METALLIC_ROUGNESS_MAP);
                }
            }

            if (TrySetTexture(gltfMaterial.normalTexture, material, bumpMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                material.EnableKeyword(KW_NORMALMAP);
                material.SetFloat(bumpScalePropId, gltfMaterial.normalTexture.scale);
            }

            if (TrySetTexture(gltfMaterial.occlusionTexture, material, occlusionMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                material.EnableKeyword(KW_OCCLUSION);
                material.SetFloat(occlusionStrengthPropId, gltfMaterial.occlusionTexture.strength);
            }

            if (TrySetTexture(gltfMaterial.emissiveTexture, material, emissionMapPropId, ref textures, ref schemaImages, ref imageVariants))
            {
                material.EnableKeyword(KW_EMISSION);
            }

            if (gltfMaterial.extensions != null)
            {
                // Transmission - Approximation
                var transmission = gltfMaterial.extensions.KHR_materials_transmission;
                if (transmission != null)
                {
#if UNITY_EDITOR
                    Debug.LogWarning("Chance of incorrect materials! glTF transmission is approximated when using built-in render pipeline!");
#endif
                    // Correct transmission is not supported in Built-In renderer
                    // This is an approximation for some corner cases
                    if (transmission.transmissionFactor > 0f && transmission.transmissionTexture.index < 0)
                    {
                        var premul = TransmissionWorkaroundShaderMode(transmission, ref baseColorLinear);
                        shaderMode = premul ? StandardShaderMode.Transparent : StandardShaderMode.Fade;
                    }
                }
            }

            switch (shaderMode)
            {
            case StandardShaderMode.Cutout:
                SetAlphaModeMask(material, gltfMaterial);
                break;

            case StandardShaderMode.Fade:
                SetAlphaModeBlend(material);
                break;

            case StandardShaderMode.Transparent:
                SetAlphaModeTransparent(material);
                break;

            default:
                SetOpaqueMode(material);
                break;
            }

            material.color = baseColorLinear.gamma;

            if (gltfMaterial.emissive != Color.black)
            {
                material.SetColor(emissionColorPropId, gltfMaterial.emissive.gamma);
                material.EnableKeyword(KW_EMISSION);
            }

            return(material);
        }
Beispiel #20
0
 protected virtual void SetShaderModePremultiply(Schema.Material gltfMaterial, Material material)
 {
 }
Beispiel #21
0
 protected virtual void SetShaderModeBlend(Schema.Material gltfMaterial, Material material)
 {
 }
Beispiel #22
0
 protected virtual void SetDoubleSided(Schema.Material gltfMaterial, Material material)
 {
     material.doubleSidedGI = true;
 }