Files
FractalInspector/shaders/mandelbrot.frag.glsl

109 lines
2.5 KiB
GLSL

#version 120
#ifdef GL_ES
precision highp float;
#endif
#define PI 3.14159265359
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
uniform vec2 u_translation;
uniform float u_scale;
uniform vec3 u_rgb_1;
uniform vec3 u_rgb_2;
uniform vec3 u_rgb_3;
uniform vec3 u_rgb_4;
#define N 256. // Number of iterations?
#define B 4. // What does B mean?
vec3 pal(in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d) {
return a + b*cos(6.28318*(c*t+d));
}
// The mandelbrot set is a set of complex numbers c for which the function:
// f(z) = z*z + c
// stays bounded between a certain range of values when iterated from z = 0.
#define MAX_ITERATIONS 100.
vec2 square_imaginary(vec2 number) {
return vec2(
pow(number.x, 2) - pow(number.y, 2),
2*number.x*number.y
);
}
float iterate_mandelbrot(vec2 coord) {
vec2 z = vec2(0,0);
for (int i = 0; i < MAX_ITERATIONS; i++) {
z = square_imaginary(z) + coord;
if (length(z) > 2) return i / MAX_ITERATIONS;
}
return MAX_ITERATIONS;
}
float iterate(vec2 p) {
vec2 z = vec2(0), c = p;
float i;
for (i=0.; i < N; i++ ) {
z = mat2(z, -z.y, z.x) * z + c;
if (dot(z, z) > B*B) break;
}
//return i;
// Compute the iteration smoothly, instead of in integers.
if (p.y < 0.f) return i;
return i - log(log(dot(z, z)) / log(B)) / log(2.);;
}
void main() {
vec2 R = u_resolution.xy;
//float translate_h = .4;
//float translate_v = 0.0;
//float scale = 2.;
//vec2 translation = vec2(translate_h, translate_v);
vec2 uv = (u_scale * gl_FragCoord.xy - R - 1.) / R.y - u_translation;
//vec2 z = vec2(0), c = uv;
//float i;
//for (i = 0.; i < N; i++) {
// z = mat2(z, -z.y, z.x) * z + c;
// if (dot(z, z) > B*B) break;
//}
float n = iterate(uv) / N;
vec3 col = pal(fract(n + 0.5), u_rgb_1, u_rgb_2, u_rgb_3, u_rgb_4);
//if (n == 1.) {n = 0; }
gl_FragColor = vec4(n == 1. ? vec3(0) : col, 1.);
//vec2 st = gl_FragCoord.xy/u_resolution.xy;
//vec3 color = vec3(0.0);
//vec3 pct = vec3(st.x);
// pct.r = smoothstep(0.0,1.0, st.x);
// pct.g = sin(st.x*PI);
// pct.b = pow(st.x,0.5);
//color = mix(colorA, colorB, pct);
// Plot transition lines for each channel
// color = mix(color,vec3(1.0,0.0,0.0),plot(st,pct.r));
// color = mix(color,vec3(0.0,1.0,0.0),plot(st,pct.g));
// color = mix(color,vec3(0.0,0.0,1.0),plot(st,pct.b));
// gl_FragColor = vec4(color,1.0);
}