在将PostProcessEffectRenderer的实现添加到Unity后处理堆栈后,该效果在Unity编辑器中可以完美运行,但在内置游戏中不会显示。
对于Windows x86_64进行的构建质量更改无效,使用最高质量设置也不会显示效果。
灰度等级
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将仅包含附着到场景中使用的 Material 的着色器,或仅包含在“始终包含的着色器”阵列的项目设置中添加的着色器。
我尝试了两种方法,都解决了我的问题,建议在游戏中创建一个引用隐藏着色器的虚拟对象会更好,因为它可以让Unity决定是否需要场景中的对象。无论哪种方式,这对我来说都是固定的。
关于c# - Unity后处理PostProcessEffectRenderer在编辑器中显示,但在构建中不显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55660837/