在URP中实现自定义PostProcess
1. OnRenderImage不能用了,需要用RenderFeature自定义Pass,或者使用BeginCameraRendering
2. 这里只分享使用RenderFeature的方式实现后处理,该方式在在URP中实现高度雾和远景雾中有使用过。
class CustomPostProcessRenderFeature 作为一个
ScriptableObject, 其中保存记录该后处理需要的一些参数-
RenderPassEvent是该后处理Pass执行的时机,一般选择AfterRenderingSkyBox
-
Enable Camera Parameter用来开关是否将Camera的矩阵参数等传入Shader,具体缘由见下文中的说明。
-
Shader该后处理需要使用的Shader
-
class PostProcessPass 具体实现后处理的Pass,核心代码:
cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity); cmd.SetViewport(camera.pixelRect); cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_BlitMaterial); cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);该方式是重置
ViewMatrix和ProjectionMatrix之后,直接使用CommandBuffer.DrawMesh API 来绘制一个全屏Mesh,绘制过程就是使用上文提到的Shader,所以该Shader运行环境的ViewMatrix和ProjectionMatrix就不是Camera的矩阵。 为了能正常计算重构世界坐标,所以需要额外将Camera相关的矩阵作为参数传入。