Files
Cool-shaders-and-stuff/shaders/mandelbrot.frag
2026-01-13 19:47:59 +00:00

57 lines
1.4 KiB
GLSL

#version 100
precision highp float;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_zoom;
uniform vec2 u_center;
void main() {
// Current pixel coordinate in 0..1
vec2 st = gl_FragCoord.xy / u_resolution.xy;
st.x *= u_resolution.x / u_resolution.y; // Aspect ratio correction
// Map to Mandelbrot space using Zoom and Center
// Default (Zoom 1): -2.5 to 1.5 X, -1.5 to 1.5 Y approx?
// Let's say center is 0,0. Range -2..2.
// st is 0..AspectRatio (approx 0..1.7)
// Center the coords:
vec2 c_uv = (gl_FragCoord.xy - u_resolution.xy * 0.5) / u_resolution.y;
// Apply Zoom and Pan
vec2 c = (c_uv / u_zoom) + u_center;
// Mandelbrot iteration
vec2 z = vec2(0.0);
float iter = 0.0;
float max_iter = 100.0 + log(u_zoom) * 20.0; // Increase details with zoom
for (float i = 0.0; i < 500.0; i++) {
if (i > max_iter) break;
// Z = Z^2 + C
float x = (z.x * z.x - z.y * z.y) + c.x;
float y = (z.y * z.x + z.x * z.y) + c.y;
if ((x * x + y * y) > 4.0) {
iter = i;
break;
}
z.x = x;
z.y = y;
}
// Coloring
float t = iter / max_iter;
if (iter == 0.0) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
} else {
gl_FragColor = vec4(
sqrt(t),
t * t,
sin(t * 3.1415),
1.0
);
}
}