Hi, i am learning SRP and want to display camera depth target on the screen. ( Just like some kind of image effect ), but i really don’t know how to do it.
I add two pass to my srp, the first one just write depth by
using (var builder = renderGraph.AddRasterRenderPass<SSShadowMapData>("Write SSShadowMap", out var passData,
m_ssShadowMapProfilingSampler))
{
RendererListDesc ssShadowPassDesc =
new RendererListDesc(m_ssShadowMapID, cameraData.cullingResults, cameraData.camera);
ssShadowPassDesc.sortingCriteria = SortingCriteria.CommonOpaque;
ssShadowPassDesc.renderQueueRange = RenderQueueRange.opaque;
passData.m_RendererListHandle = renderGraph.CreateRendererList(ssShadowPassDesc);
builder.UseRendererList(passData.m_RendererListHandle);
if (m_BackbufferDepthHandle.IsValid())
builder.SetRenderAttachmentDepth(m_BackbufferDepthHandle, AccessFlags.Write);
builder.AllowPassCulling(false);
builder.AllowGlobalStateModification(true);
builder.SetRenderFunc<SSShadowMapData>(SSShadowMapRenderFunc);
}
The second one tries to display “m_BackbufferDepthHandle” with
using (var builder = renderGraph.AddRasterRenderPass<TestBlitData>("Test SSShadowMap", out var passData,
m_TestBlitSampler))
{
if (!m_BackbufferDepthHandle.IsValid())
return;
passData.depthTex = m_BackbufferDepthHandle;
if(m_BackbufferColorHandle.IsValid())
builder.SetRenderAttachment(m_BackbufferColorHandle, 0, AccessFlags.Write);
builder.AllowPassCulling(false);
builder.AllowGlobalStateModification(true);
builder.SetRenderFunc<TestBlitData>((data, context) =>
{
context.cmd.SetGlobalTexture("_TestDepthTex", data.depthTex);
context.cmd.DrawProcedural(Matrix4x4.identity, m_BasicUnlitMat, 0, MeshTopology.Triangles, 6);
});
}
And the shader
SubShader
{
Tags
{
"RenderType"="Transparent"
"RenderQueue"="Transparent"
"RenderPipeline" = "LiteRenderPipeline"
}
ZTest Always
ZWrite Off
Cull Off
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
struct Attributes
{
uint vertexID : SV_VertexID; // 输入的顶点 ID
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 positionCS : SV_POSITION; // 裁剪空间中的顶点位置
};
TEXTURE2D(_TestDepthTex);
SAMPLER(sampler_TestDepthTex);
Varyings vert(Attributes input)
{
Varyings output;
float2 uv = float2(
float(input.vertexID == 1 || input.vertexID == 4 || input.vertexID == 5),
float(input.vertexID == 2 || input.vertexID == 3 || input.vertexID == 5)
);
output.positionCS = float4(uv * 2.0 - 1.0, 0, 1);
output.uv = uv;
return output;
}
float4 frag(Varyings input) : SV_Target
{
float depth = SAMPLE_DEPTH_TEXTURE(_TestDepthTex, sample_TestDepthTex, input.uv).r;
#if UNITY_REVERSED_Z
depth = 1.0 - depth;
#endif
return float4(depth / 5, 0, 0, 1); // divide 5 just because i want to see more clear
}
ENDHLSL
}
The result is that it seems do not write anything inside the depth target, please help if somebody know how to do it, if you need more detail, i can provide more, Anybody, please help !