Search Unity

How to combine clip() with unity SSAO

Discussion in 'Shaders' started by PatHightree, Feb 6, 2011.

  1. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    297
    Hi there,
    I'm trying to use the unity SSAO shader in combination with a surface shader that uses clip() to discard pixels.
    Unfortunately the SSAO shader ignores the clip and renders as if the full quad was drawn.
    I'm hoping that a certain tags combination will convince the unity pipeline that my shader wants to be treated specially.
    I tried copying stuff from the unity Transparent/Cutout/Diffuse shader, but that uses a fixed cutoff value.

    I've attached a sample scene and here is the source :
    Code (csharp):
    1. Shader "Clip Shader" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags {"IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    7.         LOD 200
    8.         Cull off
    9.        
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert
    12.        
    13.         sampler2D _MainTex;
    14.  
    15.         struct Input {
    16.             float2 uv_MainTex;
    17.             float3 worldPos;
    18.         };
    19.  
    20.         void surf (Input IN, inout SurfaceOutput o) {
    21.             clip (-length(IN.worldPos) + 1);
    22.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    23.             o.Albedo = c.rgb;
    24.             o.Alpha = c.a;
    25.         }
    26.         ENDCG
    27.     }
    28.     FallBack "Transparent/Cutout/Diffuse"
    29. }
     

    Attached Files:

  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Doesn't the SSAO shader use a composite upsample shader/material to blit the result to the screen?

    If so, couldn't you just modify that compositing shader and do any masking you want in there?

    I haven't looked at how SSAO works, but that's the bit I'd be hunting around for if I wanted to apple a texture mask to the results.
     
  3. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    297
    No, its more fundamental than masking the output.
    The textured geometry that is clipped away, changes the form of the geometry on which the ssao should be performed.
    So actually the question isn't really whether the ssao shader works with clipped pixels, but whether the rendering of the depth+normals take the clip operations into account.

    I did some testing and unity's Transparent/Cutout/Diffuse shader works fine with ssao.
    But as soon as I change the output of the alpha component from just a sampled texture to something depending on worldspace coordinates, the effect breaks.
    The render output is what I clip, but the ssao is still derived from the texture alpha.
    As you can see below, the ssao output of both shaders is identical.

    Code (csharp):
    1. void surf (Input IN, inout SurfaceOutput o) {
    2.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    3.     o.Albedo = c.rgb;
    4.     o.Alpha = c.a;
    5. }
    Renders :


    Code (csharp):
    1. void surf (Input IN, inout SurfaceOutput o) {
    2.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    3.     o.Albedo = c.rgb;
    4.     o.Alpha = -length(IN.worldPos) + 1 + 3.5;
    5. }
    Renders :
     

    Attached Files:

  4. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    297
    Actually, its quite logical.
    Unity uses an override shader to render the depth+ normal texture.
    There's no way that this shader is going to know what I'm up to with my clipping.
    So the only way I see around this is to render my own depth+ normals.
    Is there any docs or code around to help me get started with this ?
     
  5. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    You might have luck using a copy of Camera-DepthNormalTexture as a replacement shader. It's one of the built-in shaders, and you should be able to add a subshader with a tag that matches your fancy clipping shader's tag.
     
    Last edited: Feb 8, 2011
  7. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    297
    Ah great, I totally forgot to check the builtin shaders archive.
    This means I don't have to reverse-engineer the encoding etc.
    Thanks a lot !
     
  8. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    @danial-Brauer: Sorry for reopening an old thread but is this still somehow possible in Unity 5.5? The shader seems to be called "Hidden/Internal-DepthNormalsTexture" now but I can't figure out how to add a custom version of this shader to unity.
     
  9. ferretnt

    ferretnt

    Joined:
    Apr 10, 2012
    Posts:
    412
    Same question as above. I can't figure this out how to add my custom shader with extra passes to GraphicsSettings so Unity actually uses it in 5.5 either.
     
  10. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    You need to enable the debug mode in Unity in order to see the missing buttons in the GraphicsSettings.