1
\$\begingroup\$

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?

\$\endgroup\$
0
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ – DMGregory Feb 22 '19 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.