Custom effects need a minimum of two files: a C# and a HLSL source files (note that HLSL gets cross-compiled to GLSL, Metal and others API by Unity so it doesn’t mean it’s restricted to DirectX).
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); } }
Important: this code has to be stored in a file named Grayscale.cs. Because of how serialization works in Unity, you have to make sure that the file is named after your settings class name or it won’t be serialized properly.
这里要注意的是脚本名字要一致。
Setting
The settings class holds the data for our effect. These are all the user-facing fields you’ll see in the volume inspector.