Unity 后处理 PostProcessEffectRenderer 在编辑器中显示,但在构建中不显示

2024-04-22

将 PostProcessEffectRenderer 的实现添加到 Unity 后处理堆栈后,效果在 Unity 编辑器中完美运行,但不会在构建的游戏中显示。

对构建质量的更改没有效果,使用针对 Windows x86_64 构建的最高质量设置不会显示效果。

灰度.cs

using System;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

[Serializable]
[PostProcess(typeof(GrayscaleRenderer), PostProcessEvent.AfterStack, "Custom/Grayscale")]
public sealed class Grayscale : PostProcessEffectSettings
{
    [Range(0f, 1f), Tooltip("Grayscale effect intensity.")]
    public FloatParameter blend = new FloatParameter { value = 0.5f };
}

public sealed class GrayscaleRenderer : PostProcessEffectRenderer<Grayscale>
{
    public override void Render(PostProcessRenderContext context)
    {
        var sheet = context.propertySheets.Get(Shader.Find("Hidden/Custom/Grayscale"));
        sheet.properties.SetFloat("_Blend", settings.blend);
        context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    }
}

灰度着色器

Shader "Hidden/Custom/Grayscale"
{
    HLSLINCLUDE

        #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"

        TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
        float _Blend;

        float4 Frag(VaryingsDefault i) : SV_Target
        {
            float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
            float luminance = dot(color.rgb, float3(0.2126729, 0.7151522, 0.0721750));
            color.rgb = lerp(color.rgb, luminance.xxx, _Blend.xxx);
            return color;
        }

    ENDHLSL

    SubShader
    {
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            HLSLPROGRAM

                #pragma vertex VertDefault
                #pragma fragment Frag

            ENDHLSL
        }
    }
}

经过多次尝试和错误后,我意识到这是由 Unity 排除隐藏着色器造成的,因为它在构建时缺乏对游戏中任何内容的引用。在构建时,Unity 将仅包含附加到场景中使用的材质的着色器或添加到“始终包含的着色器”数组中的项目设置中的着色器。

我尝试了这两种方法,它解决了我的问题,有人建议在游戏中创建一个引用隐藏着色器的虚拟对象会更好,因为它让 Unity 来决定场景中是否需要它。不管怎样,这对我来说已经解决了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity 后处理 PostProcessEffectRenderer 在编辑器中显示,但在构建中不显示 的相关文章

随机推荐