I'm currently trying to create a custom render pass in Unity, and I've encountered a problem where the scene appears to be flipped upside down when using the effect I'm trying to achieve, even though I want it to be right-side up. After doing some research, I learned that render textures can be flipped when blitting to them temporarily, so I've created a function to flip the temporary render texture after the initial blit, before blitting it back to the camera. However, this causes the effect to disappear completely and only outputs what the camera sees. Here is the function:
public static void FlipRT(CommandBuffer cmd, RenderTargetHandle target)
{
var temp = new RenderTargetHandle();
cmd.Blit(target.Identifier(), temp.Identifier(), new Vector2(1, -1), new Vector2(0, 1));
cmd.Blit(temp.Identifier(), target.Identifier());
cmd.ReleaseTemporaryRT(temp.id);
}
And here is the relevant code within my custom render pass:
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cmd = CommandBufferPool.Get(profilerTag);
using (new ProfilingScope(cmd, new ProfilingSampler(profilerTag)))
{
cmd.Blit(colorBuffer, tempBuffer.Identifier());
FlipRT(cmd, tempBuffer);
cmd.Blit(tempBuffer.Identifier(), colorBuffer, materialToBlit, materialPassIndex);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
How can I modify my function or custom render pass to ensure that my effect is displayed correctly and both the camera and the effect are right-side up?
I've tried:
cmd.Blit(colorBuffer, tempBuffer.Identifier(), materialToBlit, materialPassIndex);
cmd.Blit(tempBuffer.Identifier(), colorBuffer);
Result: Image effect is right side up, rest of the camera is upside down.
cmd.Blit(colorBuffer, tempBuffer.Identifier());
cmd.Blit(tempBuffer.Identifier(), colorBuffer, materialToBlit, materialPassIndex);
Result: Image effect is right side up, only the image effect is displayed with the original camera output being black.
cmd.Blit(colorBuffer, tempBuffer.Identifier(), materialToBlit, materialPassIndex);
FlipRT(cmd, tempBuffer);
cmd.Blit(tempBuffer.Identifier(), colorBuffer);
Result: Only displays camera output, performance decreases.
I've also made tried modifying the shader using its _ProjectionParams and _MainTex_TexelSize to flip the UVs on the y-axis to no avail.