Search Unity

Anti Aliasing for Depth Texture

Discussion in 'Image Effects' started by Brad-Newman, Apr 25, 2016.

  1. Brad-Newman

    Brad-Newman

    Joined:
    Feb 7, 2013
    Posts:
    185
    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:
    Code (CSharp):
    1. Shader "Custom/DepthCameraShader"
    2. {
    3.     SubShader
    4.     {
    5.         Tags{ "RenderType" = "Opaque" }
    6.  
    7.         Pass
    8.         {
    9.             CGPROGRAM
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.             #include "UnityCG.cginc"
    13.  
    14.             sampler2D _CameraDepthTexture;
    15.  
    16.             struct v2f
    17.             {
    18.                 float4 pos : SV_POSITION;
    19.                 float4 scrPos:TEXCOORD1;
    20.             };
    21.  
    22.             //Vertex Shader
    23.             v2f vert(appdata_base v)
    24.             {
    25.                 v2f o;
    26.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    27.                 o.scrPos = ComputeScreenPos(o.pos);
    28.                 return o;
    29.             }
    30.  
    31.             //Fragment Shader
    32.             half4 frag(v2f i) : COLOR
    33.             {
    34.                 float depthValue = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r);
    35.                 half4 depth;
    36.  
    37.                 depth.r = depthValue;
    38.                 depth.g = depthValue;
    39.                 depth.b = depthValue;
    40.                 depth.a = 1;
    41.  
    42.                 return depth;
    43.             }
    44.             ENDCG
    45.         }
    46.     }
    47.         FallBack "Diffuse"
    48. }
    My Image Effect Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DepthCameraEffect : MonoBehaviour
    5. {
    6.     public Material depthCameraMaterial;
    7.     private Camera cameraComponent;
    8.     private RenderTexture tempRenderTexture;
    9.  
    10.     void Start()
    11.     {
    12.         cameraComponent = GetComponent<Camera>();
    13.         cameraComponent.depthTextureMode = DepthTextureMode.Depth;
    14.     }
    15.  
    16.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    17.     {
    18.         tempRenderTexture = RenderTexture.GetTemporary(source.width, source.height, 0);
    19.         tempRenderTexture.antiAliasing = 8;
    20.         Graphics.Blit(tempRenderTexture, destination, depthCameraMaterial);
    21.         RenderTexture.ReleaseTemporary(tempRenderTexture);
    22.     }
    23. }
     
    Last edited: Apr 25, 2016
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    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.
     
    FurySven, yonng666 and hippocoder like this.
  3. Brad-Newman

    Brad-Newman

    Joined:
    Feb 7, 2013
    Posts:
    185
    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.
     
  4. Brad-Newman

    Brad-Newman

    Joined:
    Feb 7, 2013
    Posts:
    185
    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?
     
  5. GoGoGadget

    GoGoGadget

    Joined:
    Sep 23, 2013
    Posts:
    864
    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.
     
  6. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    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.
     
  8. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    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.
     
  9. ocean_cx

    ocean_cx

    Joined:
    May 19, 2020
    Posts:
    5
    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.