File under: javascript, raymarcher, 3d, development, online

Javascript Ray Marcher v0.5

And now... Shadows!

Shadows are one of the best parts of writing a ray-tracer (or ray-marcher, in this case). Raymarchers get perfect shadows at the cost of a couple of lines of code. Unfortunately, they really slow down the render.

To calculate shadows I start with the same procedure as previous versions. When it comes time to perform the lighting step, I calculate the hit point for a ray as usual, but before colouring it, I fire a ray from the point where hit towards the sun. If the new ray hits the sun, I colour the pixel like normal. If the ray hits anything else, I don't colour the pixel.

If there was more than one light source, I would fire a ray towards each light source, then add the results to decide how brightly I should colour the pixel. I would only decline to colour it if I couldn't hit any of the lights.

No pre-rendered pictures for this one (yet), so I went through the code used to generate this one, looking at the commented-out code. Most of the comments were old versions that didn't work, and reading through them is a fascinating look at what I was thinking at the time. And a little bit scary.

Most of the commented out code was in the shading routine, and it shows that I clearly had no idea what I was doing. That's the joy of research!

Here I try to get some shadows to appear on the screen. Intensity is the light "strength".

//    intensity=intensity+1;  <- what the...?
//    intensity=intensity/2;  <- why?

//  intensity = intensity * 256; <- Could be useful, I guess
//  intensity = 256-intensity; <- how would that make things look better?

//  if ( intensity<50 ) { ret = rgb(50,50,50)};  <- I guess that's one way to get rid of shadows

My only defense is that I usually do this late at night.