Skip to main content
GameDev.net gamedev.net
🔒 Locked

Grid shader aliasing

Started by Alundra Feb 7, 2015 at 3:56 AM 0 replies 2.6k views
Original Post
Alundra
Alundra

Hi all,

I have a grid shader using frac for each line drawing like that :


float StepDistance = LineWidth / StepDistance;
if( frac( PosWSCenter.x / GridParams.x ) <= StepDistance )
{
  return LineColor;
}

I do an alpha based on the distance of the line to smooth transition of each line :


float Alpha = smoothstep( 1.0f, 0.75f, abs( 2.0f * ( LineFrac / StepDistance ) - 1.0f ) );

All works fine but one important problem is there : aliasing.That causes moire effect.

I read ddx and ddy could be used to remove aliasing but I don't know how does that.

How change this little stuff to be anti-aliased using ddx/ddy or something else ?

Thanks for the help

macnihilist
macnihilist
That is usually called 'analytic prefiltering'. The idea is to use ddx/y to get an estimate of the area the target pixel covers in texture space, and then to convolve the procedural texture with a pixel reconstruction filter analytically. Slightly simplified you need to find the average value of the procedural texture over the projected pixel. As you can imagine this quickly gets compicated, but for box filters and step functions it's often doable.

I can't tell you how exactly it would work in your case, but I think with the smoothstep function you are on the right track, because it can be seen as the convolution of a step function with a quadratic filter. (Well, that depends on the smoothstep, but at least for the cubic smoothstep it's true). If you have access to 'Texturing and Modeling - A Procedural Approach', you could take a look at the chapters about AA; this book also contains some numerical techniques you can use when the analytical stuff fails (or gets too complicated, rather).


EDIT: Depending on your speed and quality requirements you may want to save yourself the hassle of analytic prefiltering and jump directly to numerical integration. If your function is simple (which it is), you can just brute force supersample it by evaluating it at a few points inside the projected pixel and averaging. If you want to do something a little fancier you could use Simpson's rule, but I doubt this will be an advantage here, because your function is not smooth enough.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.