Code golfing a tiny demo using maths and a pinch of insanity

https://news.ycombinator.com/rss Hits: 14
Summary

A few weeks ago, I made a tiny demo that fits into 464 characters: Red Alp GLSL demo in 464 characters void main(){vec3 c,p,K=vec3(3,1,0);for(float i,a,g=1.,t,h,d,w,k=.15;i++<1e2;d= max(max(d-3.,-d),0.)*k,w=g-g*exp(-mix(h*3e2,d*2.5,a=h<.001?0.:1.)),g-=w*a,c+=w* a*d*9.+(d==0.?K*.01*h:c-c),a=min(p.y+2.,1.),c.r+=w*a*a*.2,t+=min(h*.2,k*=1.015) ,p=normalize(vec3(P+P-R,R.y))*t,p.xz*=mat2(cos(sin(T*.2)+K.zyxz*11.)),p.z+=T*.3 )for(d=p.y,h=d+.5,a=.01;a<1.;a+=a)p.xz*=mat2(8,6,-6,8)*.1,d+=abs(dot(sin((p/a+T )*.3),p/p))*a,h+=abs(dot(sin(p.xz*.6/a),P/P))*a;O=vec4(tanh(c*.5),1);} There is no texture, no mesh, no 3D helper: it's simply a procedural mathematical formula evaluated at each pixel assigning them a color. Code golfing is about making it as short as possible, and thus is part of the art performance. To put things into perspective, the 853x480 JPEG thumbnail of this article is 162x larger than this code. You can watch a larger version on its main dedicated page, or a portage on Shadertoy (508 chars). If your device is not powerful enough (I'm sorry for the lag on this page) or doesn't support WebGL2, a short preview video (of a slightly outdated variant) can be seen on Mastodon. I'm guessing the wizardry of the code has confused many people so we're going to dive through the making-of together. Overall, this demo is a particularly dense and entangled compilation of different techniques, where each aspect could mandate a dedicated article. For that reason, some parts will prefer to link to external resources when the literacy is verbose on the subject. Warning Some demos in this article will start "decaying" over time due to floating point variables getting too large. Reloading the page should fix that. The base template The code is written in GLSL and is executed for each pixel (technically each fragment) on a simple quad geometry (to be accurate it's even a single big triangle). There is no geometry aside from that, it's basically just a fragment shader. The fragment rec...

First seen: 2025-09-29 23:35

Last seen: 2025-09-30 12:37