Raytracing using Go and WebAssembly

This is an experiment in using Golang and WebAssembly to render a simple raytraced scene. The scene is rendered using a Go program that is compiled to WebAssembly and run in the browser.

The Go program is a simple raytracer based on the Ray Tracing in One Weekend book by Peter Shirley. The program defines a simple scene which is then piped to the browser using a Uint8Array. The browser then renders the scene using the ImageData API.

The setup

This is a high-level overview of the different components involved:


I've discovered a main limitation with this approach: Goroutines are not supported in WebAssembly. This means that the Go program can't take advantage of parallelism to speed up the rendering process. Things still work fine but go routines will run sequentially instead of concurrently.

Try it out

Click on the controls located below the canvas to render the scene. The "Low-res preview" button will render a low-resolution version of the scene just to try it out, while the "Full size" button will render the scene at full resolution.

Raytracing can be computationally expensive. Rendering the scene at full resolution with a high number of samples can take a while.