Skip to content

Instantly share code, notes, and snippets.

@exoad
Created April 7, 2025 21:44
Show Gist options
  • Select an option

  • Save exoad/2a441b41ed346995416043fbeba5e18b to your computer and use it in GitHub Desktop.

Select an option

Save exoad/2a441b41ed346995416043fbeba5e18b to your computer and use it in GitHub Desktop.
pincushion shadertoy
// a fragment shader that applies a pincushion distortion
// jiaming meng 4.7.2025
// !!TUNED VALUE!!
// value for changing the distortion amount by their individual components
#define DISTORT vec2(5.4)
// !!TUNED VALUE!!
// normalization factor to control additional distortion factors
// + normalizes the distortion and thus is equatable to zooming in subtracting the distortion
// - removes the normalization and thus is equatable to zooming in adding the distortion
#define NORMALIZE .88
// precalculated constants
#define NORMALIZE_2 (NORMALIZE*NORMALIZE)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// shift uv to the center
vec2 uv=fragCoord/iResolution.xy-vec2(.5);
// find the center of the uv
float center = length(uv);
center=center*center;
// apply the transformation to the uv (we will also need to reshift the
// uv back) to account for the distortion
uv=uv*(DISTORT+NORMALIZE_2)/(DISTORT+center)+vec2(.5);
// checks if the coordinate here is out of bounds
// meaning it outside of the distortion uv
// in this case we show a black texture ie vec4(0.)
if(uv.x<0.||uv.x>1.||uv.y<0.||uv.y>1.)
fragColor=vec4(0.);
else
// show the actual texture for any uvs that are actually in the distortion uv
fragColor=texture(iChannel0,uv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment