Search Unity

clip/discard instruction in shader

Discussion in 'Shaders' started by sebastien lagarde, Jul 22, 2010.

  1. sebastien lagarde

    sebastien lagarde

    Joined:
    Jul 22, 2010
    Posts:
    15
    Hi,

    I try to do my own custom shader and wanted to used clip/discard instead of fixed function pipeline Alpha testing.

    I force my shader to compile in SM 3.0 but
    when I test my custom shader with clip instruction, the pixel still be writted.
    And it is written with a wrong value (generaly a pixel from a texture appled on it) instead of being kill.

    You can reproduce the problem just with the following shader:

    float4 frag (v2f i) : COLOR
    {
    clip(-1);

    return 1;
    }

    You can observe that pixel are not kill.

    Note that my shader begin with this:

    Category {
    Tags { "RenderType"="Opaque" }
    LOD 200
    Fog { Color [_AddFog] }

    // Pixel lights
    Pass {
    Name "PPL"
    Tags { "LightMode" = "Pixel" }
    CGPROGRAM
    #pragma target 3.0
    #pragma vertex vert
    #pragma fragment frag
    #pragma multi_compile_builtin_noshadows
    #pragma fragmentoption ARB_fog_exp2
    #pragma fragmentoption ARB_precision_hint_fastest

    #include "UnityCG.cginc"
    #include "AutoLight.cginc"

    (...)

    Any though about this problem or how to solve it ?

    Thanks you.
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Try this:

    float4 frag (v2f i) : COLOR
    {
    clip(-1.0);

    return 1.0;
    }

    If you posted your whole shader code I'd have a look at if for you, but really having a clip in there really should 'just work'.
     
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Here is one I have used before:

    Code (csharp):
    1.  
    2.  Pass {
    3.             ZTest Always
    4.             Cull Off
    5.             ZWrite Off
    6.             Fog { Mode off }
    7.             Blend Off
    8.        
    9.             CGPROGRAM
    10.             #pragma vertex vert_img
    11.             #pragma fragment frag
    12.             #pragma fragmentoption ARB_precision_hint_fastest
    13.             #include "UnityCG.cginc"
    14.            
    15.             float4 frag (v2f_img i) : COLOR
    16.             {  
    17.                 //Never render the proxy shader in scene
    18.                 //Just use it for shader replace
    19.                 clip( -1.0 );
    20.                 return float4( 1.0, 0.0, 0.0, 1.0 );
    21.             }
    22.             ENDCG
    23.         }
    24.  
    25.  
     
  4. sebastien lagarde

    sebastien lagarde

    Joined:
    Jul 22, 2010
    Posts:
    15
    Hi,

    Thanks for your help. I tried your shader alone, and in fact its working.

    So after some test I discover that adding the following pass to my shader, will have side effect of a not right clip() in the shader.

    // Vertex lights
    Pass {
    Name "BASE"
    Tags {"LightMode" = "Vertex"}
    Lighting On
    Material {
    Diffuse [_Color]
    Emission [_PPLAmbient]
    }
    SetTexture [_AlbedoTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    }

    Dont know exactly why, and I am not using vertex lighting at all. But now without this pass all works fine. Thanks