fuckingtech/public/assets/glsl/lygia/math/aafract.msl
Elias Rhouzlane bf76c6b086 fix: code
2025-04-06 17:23:52 +02:00

33 lines
No EOL
873 B
Text

#include "map.msl"
/*
contributors: dahart (https://www.shadertoy.com/user/dahart)
description: |
Anti-aliasing fract function. It clamp except for a 2-pixel wide gradient along the edge
Based on this example https://www.shadertoy.com/view/4l2BRD
use: <float> aafract(<float> x)
option:
AA_EDGE: the absence of derivatives you can specify the antialiasing factor
*/
#ifndef FNC_AAFRACT
#define FNC_AAFRACT
float aafract(float x) {
#if defined(AA_EDGE)
float afwidth = AA_EDGE;
float fx = fract(x);
float idx = 1. - afwidth;
return (fx < idx) ? fx : map(fx, idx, 1., fx, 0.);
#else
float afwidth = 2.0 * length(float2(dfdx(x), dfdy(x)));
float fx = fract(x);
float idx = 1. - afwidth;
return (fx < idx) ? fx : map(fx, idx, 1., fx, 0.);
#endif
}
float2 aafract(float2 v) { return float2(aafract(v.x), aafract(v.y)); }
#endif