From 61b7b7484e141319479df532c185961eef8edf0c Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 3 Dec 2023 22:57:03 +0530 Subject: improved newton's fractal coloring --- sketches/02/newton.frag | 30 ++++++++++++------------------ sketches/02/screenshots/1.jpg | Bin 66810 -> 242241 bytes sketches/02/screenshots/2.jpg | Bin 147734 -> 230782 bytes 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/sketches/02/newton.frag b/sketches/02/newton.frag index ac31d73..b905d1b 100644 --- a/sketches/02/newton.frag +++ b/sketches/02/newton.frag @@ -21,38 +21,32 @@ vec2 cpow(vec2 z, float n) { return pow(r, n) * vec2(cos(n * theta), sin(n * theta)); } +float fabs(float a) { return a > 0. ? a : -a; } + void main() { - float time = mod(1.1 + mod(u_time * 1000.0, 100000.0) / 10000.0, 10.); - // float time = 2.0; + float time = mod(0.5 + mod(u_time * 1000.0, 100000.0) / 10000.0, 2.); float ratio = u_resolution.x / u_resolution.y; vec2 pos = v_position.xy/vec2(1.0, ratio); + vec2 prevZ; vec2 z = 2.0 * (pos - vec2(0., 0.)); - int i; - vec2 prevZ = z; - for (i = 0; i < MAX_ITERATIONS; i++) { + int iterations = 0; + for (int i = 0; i < MAX_ITERATIONS; i++) { prevZ = z; vec2 value = cpow(z, 3.0) + vec2(1., 0.); vec2 value_der = 3.0 * cpow(z, 2.0); z = z - time * cdiv(value, value_der); - if (length(z - prevZ) > 30.0) { - break; - } + iterations++; + if (fabs(z.x - prevZ.x) < 0.001) break; } - gl_FragColor = vec4(0., 0., 0., 1.); - - float diff = length(z - prevZ); + float val = float(iterations) * 10.0 / float(MAX_ITERATIONS); + float g = val * 0.5; + float b = val * 0.8; - if (diff > 10.0) { - gl_FragColor = vec4(97., 42., 191., 255.0) / 255.; - } else if (diff > 1.0) { - gl_FragColor = vec4(150., 191., 69., 255.0) / 255.; - } else if (diff > 0.0) { - gl_FragColor = vec4(27., 14., 50., 255.0) / 255.; - } + gl_FragColor = vec4(0.0, g, b, val); } diff --git a/sketches/02/screenshots/1.jpg b/sketches/02/screenshots/1.jpg index 67a2266..1fb1721 100644 Binary files a/sketches/02/screenshots/1.jpg and b/sketches/02/screenshots/1.jpg differ diff --git a/sketches/02/screenshots/2.jpg b/sketches/02/screenshots/2.jpg index da745cb..fd4d4be 100644 Binary files a/sketches/02/screenshots/2.jpg and b/sketches/02/screenshots/2.jpg differ -- cgit v1.3.1