protected override RenderQueue?ApplyTransmission(
            ref Color baseColorLinear,
            IGltfReadable gltf,
            Transmission transmission,
            Material material,
            RenderQueue?renderQueue
            )
        {
            if (supportsCameraOpaqueTexture)
            {
                if (transmission.transmissionFactor > 0f)
                {
                    material.EnableKeyword("TRANSMISSION");
                    material.SetFloat(transmissionFactorPropId, transmission.transmissionFactor);
                    renderQueue = RenderQueue.Transparent;
                    if (TrySetTexture(transmission.transmissionTexture, material, transmissionTexturePropId, gltf))
                    {
                    }
                }
                return(renderQueue);
            }

            return(base.ApplyTransmission(
                       ref baseColorLinear,
                       gltf,
                       transmission,
                       material,
                       renderQueue
                       ));
        }
Example #2
0
        protected override RenderQueue?ApplyTransmission(
            ref Color baseColorLinear,
            ref Texture[] textures,
            ref Image[] schemaImages,
            ref Dictionary <int, Texture2D>[] imageVariants,
            Transmission transmission,
            Material material,
            RenderQueue?renderQueue
            )
        {
            if (supportsCameraOpaqueTexture)
            {
                if (transmission.transmissionFactor > 0f)
                {
                    material.EnableKeyword("TRANSMISSION");
                    material.SetFloat(transmissionFactorPropId, transmission.transmissionFactor);
                    renderQueue = RenderQueue.Transparent;
                    if (TrySetTexture(transmission.transmissionTexture, material, transmissionTexturePropId, ref textures, ref schemaImages, ref imageVariants))
                    {
                    }
                }
                return(renderQueue);
            }

            return(base.ApplyTransmission(
                       ref baseColorLinear,
                       ref textures,
                       ref schemaImages,
                       ref imageVariants,
                       transmission,
                       material,
                       renderQueue
                       ));
        }
Example #3
0
        protected virtual RenderQueue?ApplyTransmission(
            ref Color baseColorLinear,
            ref Texture[] textures,
            ref Image[] schemaImages,
            ref Dictionary <int, Texture2D>[] imageVariants,
            Transmission transmission,
            Material material,
            RenderQueue?renderQueue
            )
        {
#if UNITY_EDITOR
            // ReSharper disable once Unity.PerformanceCriticalCodeInvocation
            Debug.LogWarning(
                "Chance of incorrect materials! glTF transmission"
                + " is approximated. Enable Opaque Texture access in Universal 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);
            }
            return(renderQueue);
        }
        protected override void DisposeManaged()
        {
            Flush();

            m_queue?.Dispose();
            m_queue = null;

            m_rectMesh.Dispose();
            m_resources.Dispose();
        }
        public void BeginFrame()
        {
            m_transform  = Transform.Identity;
            m_scissor    = null;
            m_drawColor  = Vector4.One;
            m_imageColor = Vector4.One;
            m_textAlign  = Anchor.TopLeft;

            SetFontSize(16);

            Vector2 viewportSize = m_viewport ?? new Vector2(Window.Width, Window.Height);

            m_queue = new RenderQueue(new RenderState
            {
                ProjectionMatrix = (Transform)Matrix4x4.CreateOrthographicOffCenter(0, viewportSize.X, viewportSize.Y, 0, -10, 10),
                CameraMatrix     = Transform.Identity,
                Viewport         = (0, 0, (int)viewportSize.X, (int)viewportSize.Y),
            });
Example #6
0
        protected virtual RenderQueue?ApplyTransmission(
            ref Color baseColorLinear,
            IGltfReadable gltf,
            Transmission transmission,
            Material material,
            RenderQueue?renderQueue
            )
        {
#if UNITY_EDITOR
            // ReSharper disable once Unity.PerformanceCriticalCodeInvocation
            logger.Warning(LogCode.MaterialTransmissionApproxURP);
#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);
            }
            return(renderQueue);
        }
Example #7
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);
        }
Example #8
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);
        }