r/creativecoding • • 2d ago • Bravo 1

1kb WebGL demo (2 lines of code)

<canvas id=c style=position:fixed;inset:0><script>c.width=W=innerWidth;c.height=H=innerHeight;with(c.getContext`webgl2`){P=createProgram();S=(T,s)=>attachShader(P,x=createShader(T),shaderSource(x,`#version 300 es

precision highp float;`+s),compileShader(x));S(35633,`void main(){int i=gl_VertexID;gl_Position=vec4(i%24-1,i/24-1,0,1);}`);S(35632,`uniform vec3 r;out vec4 o;float s;float M(vec3 p){p.xy=mat2(cos(p.z.2+vec4(0,33,11,0)));float k;s=1.;for(int j=0;j++<8;)p=2.fract(p.5+.5)-1.,k=1.3/dot(p,p),p=k,s=k;return abs(p.y)/s*.2;}void main(){float t=r.z,z=0.,d=1.,i=0.,q;vec3 D=normalize(vec3(gl_FragCoord.xy2.-r.xy,r.y)),c=vec3(0,.7,t.3),p;for(;i++<99.&&d>z/1e3;)p=c+z*D,z+=d=M(p);q=log(s);vec2 e=vec2(z/1e3,0);t=abs(dot(normalize(vec3(M(p+e.xyy),M(p+e.yxy),M(p+e.yyx))-d),D));o=vec4(sqrt((1.+cos(q+r.z+vec3(0,1,2)))t(1.-i/99.)+pow(t,60.))/exp(z*.15),1);}`);useProgram(P,linkProgram(P));F=t=>requestAnimationFrame(F,uniform3f(getUniformLocation(P,`r`),W,H,t/1e3),drawArrays(4,0,3));F(0)}</script>

138 Upvotes

9 comments sorted by

14

u/Varsoviadog 2d ago

I can make it on 1

2

u/ILoveMyDurdle 2d ago

It even has a light source! how does this magic work?
is this actually a 3D space instead of simulated trippy lines that fake depth and stuff?

3

u/FlippByte 1d ago

It's done trough "sphere tracing", a kind of raymarching. And yes, it's actually 3D, although not in the usual "triangles"-way. Check out glslsandbox, or shadertoy, they are full of these goodnesses..

1

u/CircularLogic555 20h ago

I feel like the code is a rick-roll

1

u/capfsb 11h ago

When you just remove breaklines and replaces if with ternary it's not making it less strings, it's make more complexity

1

u/Flimsy-Importance313 2d ago

What base inspiration did you use?

-22

u/ElcuartodeGustavo 2d ago

Packing a raymarched fractal shader, with scope tricks, and full-screen setup into ~1 KB is seriously impressive. Using 35633 and 35632 for VERTEX_SHADER and FRAGMENT_SHADER instead of the constants is a classic size-coding move.