Newbie problem with frag shader

Hello!
I am starting to learn shaders using the book Cg programming in Unity. I am trying to run a shader that will “cut” half of the object. it uses discard, because I am experimenting with it. I got the following errors:
Shader error in ‘Cg shader nusing discard’: GLSL vertex shader: ERROR: 0:33: ‘discard’ : supported in fragment shaders only at line 5
Shader error in ‘Cg shader nusing discard’: Shader program had errors at line 6

I don’t know how to solve this. Here is the code:

Shader “Cg shader using discard” {
SubShader {
Pass {
Cull Off
CGPROGRAM
#pragma vertex vert

#pragma fragment frag

struct vertexInput
{
float4 vertex : POSITION;
};

struct vertexOutput
{
float4 pos : SV_POSITION;
float4 posInObjectCoords: TEXCOORD0;
};

vertexOutput vert(vertexInput input)
{
vertexOutput output;

output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
output.posInObjectCoords = input.vertex;

return output;
}
float4 frag(vertexOutput input) : COLOR
{
if (input.posInObjectCoords.y > 0.0)
{
discard; // drop the fragment if y coordinate > 0
}
return float4(0.0, 1.0, 0.0, 1.0); // green
}
ENDCG
}
}
}

Thank in advance for any help :slight_smile:

I solved it using this:

float4 frag(vertexOutput input) : COLOR
{
//if (input.posInObjectCoords.y > 0.0)
//{
// discard; // drop the fragment if y coordinate > 0
//}

clip(-input.posInObjectCoords.y);

return float4(0.0, 1.0, 0.0, 1.0); // green
}

Could anyone explain me why clip worked and my original code didn’t?

Because discard is from a different programming language.

Thanks :slight_smile:

It’s definitely part of Cg, but it isn’t supported by all profiles. I’m not sure if it works with Unity in any situation, although this post hints that it does at least sometimes.

You learn something new every dan.

On which platform (operating system or GPU) does discard not work?

I am sure discard works with Unity cg shaders and on windows.
Although clipping is a better idea.