RenderPaths and depth texture

I’m trying to show the depth. Am I doing this correctly?

SharedPtr<Viewport> viewport(new Viewport(context_, m_scene, camera));
viewport->SetRenderPath(cache->GetResource<XMLFile>("RenderPaths/DeferredHWDepth.xml"));
viewport->GetRenderPath()->Append(cache->GetResource<XMLFile>("Post/ShowDepth.xml"));

auto renderer = context_->GetSubsystem<Renderer>();
renderer->SetViewport(0, viewport);

ShowDepth.xml

<renderpath>
    <command type="quad" vs="MyDepthTexture" ps="MyDepthTexture" output="viewport">
        <texture unit="depth" name="depth" />
    </command>
</renderpath>
// MyDepthTexture.glsl
void VS()
{
    mat4 modelMatrix = iModelMatrix;
    vec3 worldPos = GetWorldPos(modelMatrix);
    gl_Position = GetClipPos(worldPos);
    vScreenPos = GetScreenPosPreDiv(gl_Position);
}

void PS()
{
    float depth = ReconstructDepth(texture2D(sDepthBuffer, vScreenPos).r);
    vec3 color = vec3(depth, depth, depth);
    gl_FragColor = vec4(color, 1.0);
}

Result:

This is correct depth texture with different depth values

2 Likes

Yep, looks like depth after some curve tweaking

1 Like

I just solved this a while ago. It was because the default camera far clip, at least in the editor where I created the scene, was 1000. It was too high for the different values to be visible.

I cant believe it took me days :angry: