Discord
Login
Community
DARK THEME

How to port smallpt into microstudio

To be honest, this was just an excuse to try out the new post function :P. However, Around a month ago, I released a raytracer as my first project on microstudio. In the comments, gilles commented that it he would 'like' to see smallpt - https://www.kevinbeason.com/smallpt/ - ported into microstudio some day. So let's see how many features we can get into microstudio : ) !

Currently, I know how to do three things - reflection, soft shadows and DOF in a pathtracer. Please note that a pathtracer is just a raytracer but you take a certain number of repeats to the raytracing process and then average the RGB colour between them.

Reflection is done by tracing another ray from the point of intersection in the direction of the reflection vector, which is :

2*normal*dot(normal,ray)-ray

(so the code would look like this to set up a ray before checking object intersections again and then mixing in the new RGB colour)

centrex=intersectx
centrey=intersecty
centrez=intersectz
dot=rayx*normalx+rayy*normaly+rayz*normalz
rayx=2*normalx*dot-rayx
rayy=2*normaly*dot-rayy
rayz=2*normalz*dot-rayz

Soft shadows are done by randomising the light source between certain values each sample. In my raytracer you would just set lightx,lighty and lightz to a random value between certain points for each sample. But for smallpt, we need to distribute the light source in an unbiased circle. Instead of using monte-carlo distribution, this method is much simpler:

unbiaseddistribution=function(radius)
  direction=360*random.next()
  dist=sqrt(random.next()*radius^2) 
  outputx=sind(direction)*dist 
  outputy=cosd(direction)*dist
end

Last of all, DOF (depth of field) is done by connecting a point on the aperture to a point on the focal plane. The aperture is a pinhole opening on a camera. The higher the aperture, the higher the depth of field. So we again use the unbiased circle distribution with the aperture radius. So the code is (where focal length is the focus distance and FOV is the field of view):

getray=function(x,y,apertureradius) //X and y are the 2d positions on the screen
  unbiaseddistribution(apertureradius)
  rayx=outputx-(focallength*x/FOV) //Connect point on the aperture to point on focal plane
  rayy=outputy-(focallength*y/FOV)
  dist=sqrt(rayx^2+rayy^2+focallength^2) //Normalise for final ray direction
  rayx=rayx/dist
  rayy=rayy/dist
  rayz=focallength/dist
end

Does anyone know how to do global illumination? Also, please note I just edited the code.

You can use triple backquotes to create a code block and preserve formatting, as in the example below:

Some paragraph of text

```
unbiaseddistribution=function(radius) 
  direction=360*random.next() 
  dist=sqrt(random.next()*radius^2) 
  outputx=sind(direction)*dist 
  outputy=cosd(direction)*dist 
end 
```

I will probably post a tip on using Markdown tomorrow :-) time to get some sleep for me.

I have no idea what you are talking about, oh taken one, LOL.
My math horizon is very limited and I just throw everything that looks good together by trial&error :)

Hats off, at least now I know whom to pester when I get stuck evil grin

Oh no... and 'taken one' is my nickname now, oh smith of tinkering? Also, what you don't realise is that I put together these algorithms by trial and error considering MY limited math horizon.

What don't you understand? A pathtracer is just a raytracer but you repeat more trials and average the RGB between them, changing something each time (for example light position for soft shadows, shadows with a nice, accurate, blur).

honestly, it would be great if we could get a render such as on the website working on microstudio. My maths ability is kind of limited too lol.

Does smallpt stand for small pathtracer? As in a small amount of code for rendering a pathtracer. Pathtracing luckily isn't largely more complicated than raytracing, which you already made a project for, as all you do is realistically just randomise the lighting. Reflections would be a bit more difficult though.

I know this was posted a long time ago, but:

Smallpt is written in C++, a language that compiles to WebAssembly. Therefore, it's probably possible to modify the C++ code to interface with the MicroStudio canvas, compile it to WASM, and load it with JavaScript. There's also a JavaScript port of it, but that would be slower. However, it seems that it's not possible to get high-quality images quickly. These renderings of the Cornell box (images from kevinbeason.com) took 13 and 63 seconds respectively, probably in native machine code:

There's also this fast WASM raytracer by Simon Niklaus.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community