top | item 45413588

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

107 points| ux | 5 months ago |blog.pkh.me

22 comments

order

recursive|5 months ago

I don't know much about 3d stuff or shaders or this language, but I know golf, and I think I found a byte to save.

    w = g-g*exp(-mix(h*3e2,d*2.5,a=h<.001?0.:1.))
    w = g-g/exp(mix(h*3e2,d*2.5,a=h<.001?0.:1.))

lifthrasiir|5 months ago

You can also premultiply w with a, as every occurrence of `w` looks like `w*a`. This should shave three more bytes:

  w=g-g*exp(...),g-=w*a,c+=w*a*d*9.+...,c.r+=w*a*a*2,...
  w=g-g*exp(...),g-=w*=a,c+=w*d*9.+...,c.r+=w*a*2,...
(I've tested this with the Shadertoy version. YMMV.)

ux|5 months ago

Hey, thank you, that's a nice one. I added the change to the Shadertoy version with your credit. I will add a note/update on the blog and demo page later. Thanks again :)

vintermann|5 months ago

Using maths, insanity, and a ton of calculation horsepower... That made my graphic card fan turn on instantly!

ForHackernews|5 months ago

If anyone is interested in learning more about this kind of coding https://thebookofshaders.com/ is a fantastic intro.

ux|5 months ago

Is this tutorial kind of abandoned? There are many teased chapters that never seem to have materialized somehow.

bangaladore|5 months ago

The top demo doesn't function on Edge or Chrome on Windows due to too many webgl contexts.

> WARNING: Too many active WebGL contexts. Oldest context will be lost.

ux|5 months ago

Ah, someone reported this to me today, but I must admit I have no idea how to address the issue. Currently the canvas animations are stopped when out of context, but yeah they have to be loaded.

The code on the blog is pretty simple and naive (I'm not a webdev): https://github.com/ubitux/scripts/blob/main/share/blog/shade...

Any suggestion on how to address the issue is welcome.

Note: I don't have any Windows machine to test with

user1093298|5 months ago

Works fine with Firefox on Windows.

What am I saying? Fine? It's freaking awesome.

samwhiteUK|5 months ago

Unbelievably cool, well done!

mkbosmans|5 months ago

Another two bytes found (I think)

  (d==0.?K*.01*h:c-c)
could become

  (d>0.?.0:.01)*K*h

ux|5 months ago

Ah nice for noticing d!=0 is d>0. Not sure how I missed the multiplication to get rid of the vector form; I guess I was too obsessed with the x-x trick...

I added your changes to the Shadertoy version with your HN nickname. I'll integrate it to the original later.

Thanks!

sgarland|5 months ago

This is utterly bonkers to me, well done.

mielioort|5 months ago

Needs dragons flying sunwards