Skip to content

Instantly share code, notes, and snippets.

@keijiro
Last active November 6, 2024 22:32
Show Gist options
  • Save keijiro/b53975f195e4ab6d63bbd9e0cc786be7 to your computer and use it in GitHub Desktop.
Save keijiro/b53975f195e4ab6d63bbd9e0cc786be7 to your computer and use it in GitHub Desktop.
KodeLife fragment shader sketch
#version 150
uniform float time;
uniform sampler2D prevFrame;
uniform sampler2D texture0;
uniform vec2 resolution;
uniform vec3 spectrum;
out vec4 fragColor;
vec2 fade(vec2 x) { return x * x * x * (x * (x * 6 - 15) + 10); }
vec2 phash(vec2 p)
{
p = fract(mat2(1.2989833, 7.8233198, 6.7598192, 3.4857334) * p);
p = ((2384.2345 * p - 1324.3438) * p + 3884.2243) * p - 4921.2354;
return normalize(fract(p) * 2 - 1);
}
float cnoise(vec2 p)
{
vec2 ip = floor(p);
vec2 fp = fract(p);
float d00 = dot(phash(ip), fp);
float d01 = dot(phash(ip + vec2(0, 1)), fp - vec2(0, 1));
float d10 = dot(phash(ip + vec2(1, 0)), fp - vec2(1, 0));
float d11 = dot(phash(ip + vec2(1, 1)), fp - vec2(1, 1));
fp = fade(fp);
return mix(mix(d00, d01, fp.y), mix(d10, d11, fp.y), fp.x);
}
void main(void)
{
vec2 uv = gl_FragCoord.xy / resolution;
float aspect = resolution.x / resolution.y;
float amp = spectrum.x;
// eye texture
float tex = texture(texture0, uv * vec2(1, -1)).r;
// polar coordinate
vec2 p = (uv - 0.5) * vec2(aspect, 1);
p = vec2(atan(p.y, p.x), length(p));
// displacement
p.x += cnoise(vec2(p.y * 20, time)) * 0.08 / (p.y + 0.1);
// bring back to uv space
uv = vec2(cos(p.x), sin(p.x)) * p.y;
uv = uv / vec2(aspect, 1) + 0.5;
// feedback
float fb = texture(prevFrame, uv).r * 0.998;
// blending
float c = mix(fb, tex, smoothstep(0.4, 1, amp));
// color gradient
float t = time + amp;
float c1 = 0.5 + sin(c * 12.12 + t * 1.11) * 0.5;
float c2 = 0.5 + sin(c * 11.33 + t * 1.73) * 0.5;
fragColor = vec4(1, c1, c2, 0) * c;
}
@crcdng
Copy link

crcdng commented Nov 6, 2024

It does not run with copy/pasting the code alone because this shader requires an eye texture (an image file) in variable texture0 (line 38), prevFrame (line 52) and an audio input with spectrum split input (line 35). After adding these inputs, it still doesn't show anything.

The reason is that in the last line fragColor = vec4(1, c1, c2, 0) * c; the parameter for alpha should be 1.0, not 0. After fixing these issues, the shader shows something.

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