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

问题描述 投票:1回答:1

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

对构建质量的更改无效,使用最高质量设置无法显示效果,为Windows x86_64构建。

Grayscale.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);
    }
}

Grayscale.shader

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
        }
    }
}
c# unity3d shader effect post-processing
1个回答
1
投票

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

Project Settings/Graphics/Always Included Shaders

我尝试了两个并且它解决了我的问题,有人建议在你的游戏中创建一个引用隐藏着色器的虚拟对象会更好地工作,因为它让Unity决定它是否需要在一个场景中。无论哪种方式,它为我修复它。

© www.soinside.com 2019 - 2024. All rights reserved.