
本文介绍了Unity的HDRP(High Definition Render Pipeline)中如何实现自定义后期效果,使用的是HDRP10.7.0版。
简介
高清渲染管道 (HDRP) 是一种旨在为高端硬件提供高质量图形的管道,而HDRP的后期处理已内置,因此不需要安装任何的其他包。
创建着色器
HDRP为后期效果提供了一个着色器模板,因此可以从资产 → 创建 → 着色器 → HD渲染管线 → 后期处理中创建一个着色器文件。
将文件名保留为示例,以此为基础制作效果,接着咋们制作让屏幕变红的效果。
Shader "Hidden/Shader/Example"
{
HLSLINCLUDE
#pragma target 4.5
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
struct Attributes
{
uint vertexID : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
Varyings Vert(Attributes input)
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
return output;
}
float _Intensity;
TEXTURE2D_X(_InputTexture);
float4 CustomPostProcess(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
uint2 positionSS = input.texcoord * _ScreenSize.xy;
float3 outColor = LOAD_TEXTURE2D_X(_InputTexture, positionSS).xyz;
//just added here
outColor *= lerp(float3(1, 1, 1), float3(1, 0, 0), _Intensity);
return float4(outColor, 1);
}
ENDHLSL
SubShader
{
Pass
{
Name "Example"
ZWrite Off
ZTest Always
Blend Off
Cull Off
HLSLPROGRAM
#pragma fragment CustomPostProcess
#pragma vertex Vert
ENDHLSL
}
}
Fallback Off
}
仅更改了模板中的一行注释。
创建脚本
现在创建必要的脚本,还有一个模板,所以将从资产 → 创建 → 渲染 → C#后期处理体积创建它,与着色器一样,将其命名为Example。
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using System;
[Serializable, VolumeComponentMenu("Post-processing/Custom/Example")]
public sealed class Example : CustomPostProcessVolumeComponent, IPostProcessComponent
{
// Volume Parameters set from the component and passed to the shader
// Use a derived class of VolumeParameter like this
[Tooltip("Controls the intensity of the effect.")]
public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f);
Material m_Material;
// Return a valid state to prevent unnecessary processing
public bool IsActive() => m_Material != null && intensity.value > 0f;
//When to apply this post effect
public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.AfterPostProcess;
// shader name
const string kShaderName = "Hidden/Shader/Example";
public override void Setup()
{
if (Shader.Find(kShaderName) != null)
m_Material = new Material(Shader.Find(kShaderName));
else
Debug.LogError($"Unable to find shader '{kShaderName}'. Post Process Volume Example is unable to load.");
}
public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
{
if (m_Material == null)
return;
m_Material.SetFloat("_Intensity", intensity.value);
m_Material.SetTexture("_InputTexture", source);
HDUtils.DrawFullScreen(cmd, m_Material, destination);
}
public override void Cleanup()
{
CoreUtils.Destroy(m_Material);
}
}
这一次里面不需要改变任何东西。
将其适用于场景
最后,将把这个效果应用到场景中。首先从项目设置 → HDRP默认设置 → 自定义后处理订单按After Post Process(后期处理)的+号按钮Example添加本次创建的效果。

接下来,在场景中设置后处理并从体积组件按钮中选择,如果选中强度并增加滑块的值,可以看到应用了后期效果,如下所示。
目标:添加覆盖后处理 → 自定义 → Example

…
以上是3D天堂网关于如何在Unity的HDRP中实现自定义后期效果的全部内容,如果你有任何反馈,请随时在本页面下方留言。