Skip to content

Instantly share code, notes, and snippets.

@kwyntes
Last active February 14, 2024 20:27
Show Gist options
  • Save kwyntes/bc62192489a713808e593d0f58fe1a7b to your computer and use it in GitHub Desktop.
Save kwyntes/bc62192489a713808e593d0f58fe1a7b to your computer and use it in GitHub Desktop.
shadertoy lens distortion transition [unfinished]
// functional, but there's some fuckiness that needs to be worked out.
// (and after that the code has to cleaned up but that's not too much work.)
// adapted from https://www.shadertoy.com/view/4lSGRw
#define PI 3.14159265358979
// (unused as of now)
float someBezierThing(float t /* [0, 1] */)
{
float a = 1.0;
float b = 1.0;
float c = 1.0;
float d = 2.0;
float T = 1.0 - t;
// a (1-t)^3 + 3b (1-t)^2 t + 3c (1-t) t^2 + d t^3 - 1
return a*T*T*T + 3.*b*T*T*t + 3.*c*T*t*t + d*t*t*t - 1.;
}
// the lens distortion + chromatic abberation function
vec2 computeUV(vec2 uv, float k, float kcube, bool yes)
{
// temp, for debugging only
if (!yes) return uv;
vec2 t = uv - 0.5;
float r2 = t.x * t.x + t.y * t.y;
float f = 1.0 + r2 * (k + kcube * sqrt(r2));
vec2 nUV = f * t + .5;
nUV.y = 1. - nUV.y;
return nUV;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
vec2 uv = fragCoord / iResolution.xy;
// Duration idea: pi/2 seconds ** cos(4*pi/2)-1 = 0 which should be nice for this.
// TODO: make this configurable....
float t = (PI/2.0)*(iMouse.x/iResolution.x); //iTime;
float tmod = mod(t, PI/2.0);
// -- Lens distortion + chromatic abberation + zoom --
float intensity = 0.9;
bool doSIN = false; // makes it freeze a little bit (or bouncy for high intensities)
// also limits the intensity of the effect a lot.
//float x = intensity * (cos(4.0 * t) * tmod*tmod - 1.0);//someBezierThing(sin(iTime)) * 2.;
//float w = 1.8; // arbitrary, but lots of values don't work? see https://www.desmos.com/calculator/euovdhkgsw
float x = 0.9 * sin(4.0 * tmod);
float k = 1.0 * (doSIN ? sin(x * 0.9) : x);
float kcube = 0.5 * (doSIN ? sin(x ) : x);
float offset = 0.1 * (doSIN ? sin(x * 0.5) : x);
// Zoom [TODO: in slightly, then out]
// 1.31945... is approx. the y-value of the third peek of this function
//float theta /* greek letters make you sound smarter */ = -sin(6.0 * tmod) * tmod / 1.31945;
// stupid piecewise thing i made up
float w = 1.8; // arbitrary, but lots of values don't work? see https://www.desmos.com/calculator/euovdhkgsw
float theta = tmod <= PI/6.0 ? -sin(6.0 * tmod) * tmod :
tmod <= PI/3.0 ? pow(6.0/PI, w) * pow(tmod - PI/6.0, w) :
/*tmod <= PI/2.0*/ -pow(6.0/PI, 2.0) * pow(tmod - PI/2.0, 2.0); //mix(pow(6.0/PI, w) * pow(tmod - PI/6.0, w), -pow(6.0/PI, 2.0) * pow(tmod - PI/2.0, 2.0), sqrt(tmod/(PI/2.0)));
float zoom = pow(2.0, theta);//pow(2.0, (x+0.5)/4.);//pow(2.0, sin(t)); // This should go from 1.0 to <...> to 2.0 to 1.0
uv = (uv - 0.5) * zoom + 0.5;
// Offset each colour seperately to achieve the chromatic abberation effect
bool doLD = true;
bool doCHROMABB = true;
float r, g, b;
// why is there no nicer way to do this .........
if (tmod <= PI/3.0) {
r = texture(iChannel0, computeUV(uv, k + (doCHROMABB ? offset : 0.0), kcube, doLD)).r;
g = texture(iChannel0, computeUV(uv, k , kcube, doLD)).g;
b = texture(iChannel0, computeUV(uv, k - (doCHROMABB ? offset : 0.0), kcube, doLD)).b;
} else {
r = texture(iChannel1, computeUV(uv, k + (doCHROMABB ? offset : 0.0), kcube, doLD)).r;
g = texture(iChannel1, computeUV(uv, k , kcube, doLD)).g;
b = texture(iChannel1, computeUV(uv, k - (doCHROMABB ? offset : 0.0), kcube, doLD)).b;
}
fragColor = vec4(r, g, b, 1.0);
}
@kwyntes
Copy link
Author

kwyntes commented Oct 10, 2023

https://www.shadertoy.com/view/dsdBR4

also remember to turn off VFlip on any images in shadertoy as the computeUV function for some reason vertically flips them/

@kwyntes
Copy link
Author

kwyntes commented Feb 14, 2024

also remember to turn off VFlip on any images in shadertoy as the computeUV function for some reason vertically flips them

or just comment out line 36 (nUV.y = 1. - nUV.y) because that's where we flip it. idk why it's there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment