# How can I make alternate fragments drop (or make them black) based on a checkerboard texture?

How can I make alternate fragments drop(or make them black) based on a checkerboard texture in a Unity fragment shader?

I am using forward path rendering in my shader passes for lighting and shadows for every object in the scene.

Now,I need a shader (or something) that drops alternate pixels from the overall screen space after everything is rendered. How can I achieve this effect?

• You might want to show us your shader code so we can see what we're working with. Also, do you mean "checkerboard" (an alternating pattern of white & black squares), not "checkbox" (a UI control consisting of a box that can be toggled between checked/true and not-checked/false states)? – DMGregory Feb 20 '19 at 17:53
• Oh yes, I meant checkerboard. Sorry. Actually my question seems to be wrong. I have edited it now. Please read the question now. – SuR Feb 20 '19 at 22:26
• Do I need to use Command Buffer for this? I don't know how to use it though, couldn't find any tutorial to learn it's implementation. – SuR Feb 20 '19 at 22:29

Generally, you can check the fragment coordinates (assuming they're integers going from (0,0) to (width, height)) and if both are even or both are odd keep the fragment, otherwise reject it. (Or the reverse if you prefer.)

In pseudocode it would look like this:

bool rowIsEven = (fragCoord.x % 2) == 0;
bool colIsEven = (fragCoord.y % 2) == 0;
if (rowIsEven == colIsEven)
{
// keep the fragment
}
else
{
// discard the fragment
}


If your coordinates are normalized, then you can multiply them by the width and height before checking the result. You'll need to pass the width and height in as a uniform, probably.

• In Unity ShaderLab specifically, you can do this with the VPOS semantic as described here (on shader model 3.0 plus), or manually by passing the projected position in from the vertex shader and scaling it with the _ScreenParams built-in uniform variable. Also note you can save one modulo operation by just adding the row & column first and taking the parity of the result (even + even / odd + odd both make an even) – DMGregory Feb 22 '19 at 17:51