clip/discard instruction in shader

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.

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’.

Here is one I have used before:

 Pass {
            ZTest Always 
    		Cull Off 
    		ZWrite Off
			Fog { Mode off }
			Blend Off
		
			CGPROGRAM
			#pragma vertex vert_img
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest 
			#include "UnityCG.cginc"
			
			float4 frag (v2f_img i) : COLOR
			{	
				//Never render the proxy shader in scene
				//Just use it for shader replace
				clip( -1.0 );
				return float4( 1.0, 0.0, 0.0, 1.0 );
			}
			ENDCG
        }

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