1
\$\begingroup\$

Can I make alternate fragments drop(or make them black) based on a checkerboard texture in 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. I would really appreciate some suggestions here!

\$\endgroup\$

bumped to the homepage by Community 15 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • \$\begingroup\$ 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)? \$\endgroup\$ – DMGregory Feb 20 at 17:53
  • \$\begingroup\$ Oh yes, I meant checkerboard. Sorry. Actually my question seems to be wrong. I have edited it now. Please read the question now. \$\endgroup\$ – SuR Feb 20 at 22:26
  • \$\begingroup\$ 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. \$\endgroup\$ – SuR Feb 20 at 22:29
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\$
  • \$\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 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.