- Home /
How to properly use RTHandles in HDRP for post effects
I've been trying to write a post-process volume component for HDRP (Unity 2019.4.1, HDRP 7.3.1) to replicate the Clear Flags: None effect (setting the background type to "none" on the camera doesn't seem to work the same way in HDRP). I'd like to achieve the effect of having "trails" after the image, where every frame is drawn on top of the previous.
The method I'm hoping to use for this is a separate camera with a custom volume component added to it that takes the output of the previous frame and uses a shader to add the current frame to it. However, I can't quite seem to get RTHandles to work with this.
What I'm trying to replicate Reference 1 and 2
Relevant volume component code:
private GraphicsFormat RTFormat;
private RTHandle rth;
public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
{
//Validate
if (m_Material == null) return;
if (rth == null) rth = CreateRTH(camera);
m_Material.SetTexture("_MainTex", source);
m_Material.SetTexture("_PrevFrame", rth);
//Draw the frame itself
HDUtils.DrawFullScreen(cmd, m_Material, destination);
HDUtils.DrawFullScreen(cmd, m_Material, rth);
}
private RTHandle CreateRTH(HDCamera camera)
{
return RTHandles.Alloc(camera.actualWidth, camera.actualHeight, 2, DepthBits.Depth16, colorFormat: RTFormat, FilterMode.Bilinear, TextureWrapMode.Clamp, TextureDimension.Tex2D, false, false, false, false, 2, 0, MSAASamples.None, true, true, RenderTextureMemoryless.None, "rth");
}
Shader function:
float _Intensity;
TEXTURE2D_X(_MainTex);
TEXTURE2D_X(_PrevFrame);
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(_MainTex, positionSS).xyz;
outColor.xy = 0;
float3 acc = LOAD_TEXTURE2D_X(_PrevFrame, positionSS).xyz;
acc.yz = 0;
outColor += acc;
return float4(outColor, 1);
}
However, I can't seem to get the generated RTHandle and RenderTexture to output anything other than black. I've researched this as thoroughly as possible and my method seems to be correct from what I understand. Any ideas?