I’m currently trying to implement a way to copy a stencil buffer to a render texture in a render feature, and I’m kinda stuck. Online it seems like people have gotten it to work, but other people have said it’s impossible, so I’m a bit confused on what I should do. It seems like people have gotten it to work by drawing a fullscreen quad with a command buffer going through the material passes, and somehow that shows the stencil buffer, but their workflow was hard to follow hence why I’m coming here.
If anyone has ever done this before, or has any ideas on how to do this, any insight would be greatly appreciated
I have tried copying a a stencil buffer to not a render texture as much as a global shader texture. In Unity 2023.2 that didn’t seem to be complicated.
This was just a matter of putting two lines inside the OnCameraSetup function
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
.........
RTHandle rtCameraDepth = renderingData.cameraData.renderer.cameraDepthTargetHandle;
Shader.SetGlobalTexture("_CameraStencil", rtCameraDepth, RenderTextureSubElement.Stencil);
}
Analogically in the RenderGraph mode:
class PassData
{
public TextureHandle depth;
......
}
static void ExecutePass(RasterGraphContext context, PassData data)
{
......
Shader.SetGlobalTexture("_CameraStencil", data.depth, RenderTextureSubElement.Stencil);
Blitter.BlitTexture(context.cmd, new Vector4(1, 1, 0, 0), data.material, 0);
}
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
using (var builder = renderGraph.AddRasterRenderPass<PassData>("ShowStencilsBlit", out var passData))
{
passData.material = m_Material;
......
passData.depth = resourceData.activeDepthTexture;
......
builder.UseTexture(passData.depth, AccessFlags.Read);
builder.SetRenderAttachment(resourceData.activeColorTexture, 0, AccessFlags.Write);
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
{
ExecutePass(context, data);
});
}
}
However I couldn’t get it to work in WebGL.