aboutsummaryrefslogtreecommitdiff
path: root/sketches/02/newton.frag
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-11-05 15:01:54 +0530
committerAkshay Nair <phenax5@gmail.com>2023-11-05 15:01:54 +0530
commita6c771d3ae775476b6637903ea5c12de89ff3f85 (patch)
treec532586a6105cee217373ca2b7604b8306ae6e8a /sketches/02/newton.frag
parented50aedc08afd67b8d9606460d2d5209239ac88c (diff)
downloadcreative-coding-playground-a6c771d3ae775476b6637903ea5c12de89ff3f85.tar.gz
creative-coding-playground-a6c771d3ae775476b6637903ea5c12de89ff3f85.zip
feat: mandelbrot + newton fractals
Diffstat (limited to 'sketches/02/newton.frag')
-rw-r--r--sketches/02/newton.frag55
1 files changed, 54 insertions, 1 deletions
diff --git a/sketches/02/newton.frag b/sketches/02/newton.frag
index 9cbf92c..ac31d73 100644
--- a/sketches/02/newton.frag
+++ b/sketches/02/newton.frag
@@ -1,5 +1,58 @@
+varying vec4 v_position;
+uniform vec2 u_resolution;
+uniform float u_time;
+uniform float u_mouse;
+
+#define MAX_ITERATIONS 100
+
+vec2 cmul(vec2 a, vec2 b) {
+ return vec2(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
+}
+
+vec2 cdiv(vec2 a, vec2 b) {
+ float denominator = b.x*b.x + b.y*b.y;
+ if (denominator == 0.) return a;
+ return cmul(a, vec2(b.x, -b.y)) / denominator;
+}
+
+vec2 cpow(vec2 z, float n) {
+ float r = length(z);
+ float theta = atan(z.y, z.x);
+ return pow(r, n) * vec2(cos(n * theta), sin(n * theta));
+}
void main() {
- gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+ float time = mod(1.1 + mod(u_time * 1000.0, 100000.0) / 10000.0, 10.);
+ // float time = 2.0;
+
+ float ratio = u_resolution.x / u_resolution.y;
+ vec2 pos = v_position.xy/vec2(1.0, ratio);
+
+ vec2 z = 2.0 * (pos - vec2(0., 0.));
+ int i;
+
+ vec2 prevZ = z;
+ for (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;
+ }
+ }
+
+ gl_FragColor = vec4(0., 0., 0., 1.);
+
+ float diff = length(z - prevZ);
+
+ 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.;
+ }
}