Anti Aliasing for Depth Texture

I’m trying to render the depth texture of a camera and apply anti aliasing, but I can’t seem to get it to anti-alias. I always end up with jagged aliased edges. My code is below, any ideas where I’m going wrong?

Thanks!
Brad

My Shader:

Shader "Custom/DepthCameraShader"
{
    SubShader
    {
        Tags{ "RenderType" = "Opaque" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            sampler2D _CameraDepthTexture;

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 scrPos:TEXCOORD1;
            };

            //Vertex Shader
            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.scrPos = ComputeScreenPos(o.pos);
                return o;
            }

            //Fragment Shader
            half4 frag(v2f i) : COLOR
            {
                float depthValue = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r);
                half4 depth;

                depth.r = depthValue;
                depth.g = depthValue;
                depth.b = depthValue;
                depth.a = 1;

                return depth;
            }
            ENDCG
        }
    }
        FallBack "Diffuse"
}

My Image Effect Script:

using UnityEngine;
using System.Collections;

public class DepthCameraEffect : MonoBehaviour
{
    public Material depthCameraMaterial;
    private Camera cameraComponent;
    private RenderTexture tempRenderTexture;

    void Start()
    {
        cameraComponent = GetComponent<Camera>();
        cameraComponent.depthTextureMode = DepthTextureMode.Depth;
    }

    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        tempRenderTexture = RenderTexture.GetTemporary(source.width, source.height, 0);
        tempRenderTexture.antiAliasing = 8;
        Graphics.Blit(tempRenderTexture, destination, depthCameraMaterial);
        RenderTexture.ReleaseTemporary(tempRenderTexture);
    }
}

Oh yeah, that’s not going to work at all.

What antiAliasing does on a render texture is set the msaa you are applying to what is rendered into that texture. For this to work you need a depth buffer, and you need to be rendering things INTO that depth buffer. In this case neither of those things are happening.

What do you mean when you say you want antialiasing on the depth buffer? That doesn’t really make a lot of sense because you can’t USE that buffer for anything really…

If you want the buffer to have AA because you are drawing it to the screen and don’t like the edges you can run a screen space AA pass on it. Check out the MSAA and FXAA in the image effects package.

3 Likes

I am drawing it to the screen and would like the depth AA to match the color AA. I’m using the depth buffer for compositing in After Effects.

I’m new to image effects and shaders, can you provide some reference examples for how to do this? I’m guessing I need to set a camera render texture target somewhere along the way?

Take a look at some of the supplied image effects and how they work first, simple ones like vignette or color correction are a good place to start.

Here, @bgolus has implemented the depth texture AA in this project.

Cheers

The depth texture is not anti-aliased in itself in my setup. Rather when the anti-aliased forward pass is done I test multiple points in the depth texture to find the pixel in the depth that’s the closest match.

Thanks for the clarification,
I stopped reading your code where you tap, assuming it’s for blur purposes, now I see it indeed does more.
To the op: Once you get the depth texture inside your shader, you can tap it to perform a blur.

we are using _CameraDepthTexture to determine alpha value in uberPost.shader(for example, eliminate camera backgrounds color), that’s very convenient. and also we want the _CameraDepthTexture have handled by SMAA, the the final output have smooth edges.