Jam*_*uld 6 c# shader effect unity-game-engine post-processing
将 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);
}
}
Run Code Online (Sandbox Code Playgroud)
灰度着色器
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
经过多次尝试和错误后,我意识到这是由 Unity 排除隐藏着色器造成的,因为它在构建时缺乏对游戏中任何内容的引用。在构建时,Unity 将仅包含附加到场景中使用的材质的着色器或添加到“始终包含的着色器”数组中的项目设置中的着色器。
我尝试了这两种方法,它解决了我的问题,有人建议在游戏中创建一个引用隐藏着色器的虚拟对象会更好,因为它让 Unity 来决定场景中是否需要它。不管怎样,这对我来说已经解决了。
| 归档时间: |
|
| 查看次数: |
4265 次 |
| 最近记录: |