Skip to content

Instantly share code, notes, and snippets.

@pinkwerks
Last active October 7, 2024 21:48
Show Gist options
  • Save pinkwerks/e2dcf8c1a1e6252ad5e9544abead7e00 to your computer and use it in GitHub Desktop.
Save pinkwerks/e2dcf8c1a1e6252ad5e9544abead7e00 to your computer and use it in GitHub Desktop.
float frequency = .5;
float trianglewave = abs(2 * frac(x * frequency) - 1); // repeating triangle wave /\/\/\...
float squarewave = round(abs(2 * frac(x * frequency + .25) - 1)); // repeating square wave
float FilteredStep(float edge, float x, float fw)
{
return saturate((x + fw / 2 - edge) / fw);
}
// Euclid length because built-in does abs(ddx(x)) + abs(ddy(y))
float widthU = fwidth(u);
float widthV = fwidth(v);
float combinedWidth = length(vec2(widthU, widthV));
float aaStep(float compValue, float gradient)
{
float halfChange = fwidth(gradient) / 2;
// base the range of the inverse lerp on the change over one pixel
float lowerEdge = compValue - halfChange;
float upperEdge = compValue + halfChange;
// inverse lerp
float stepped = (gradient - lowerEdge) / (upperEdge - lowerEdge);
stepped = saturate(stepped);
return stepped;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment