A simplistic Path tracer written in Swift.
SwiftRay is a Path Tracer (a kind of Raytracer) written in Swift and loosely based on the e-book Ray Tracing in One Weekend by Peter Shirley.
There are three books in the series, but SwiftRay is only based on the first two. The original source code is written in C++. Should you wish to know how the path tracer works, buy the book, since it's cheap, and provides nice explanations.
- Antialiasing (by multisampling every pixel)
- Several materials: Lambertian (matte colour), Metal (reflective) and Dielectric (refractive)
- Depth-of-field blur
- Motion blur
- Multi-threaded, to exploit multiple cores
- A Bounding Volume Hierarchy algorithm speeds-up calculations
- There are only two type of object: Spheres and rectangles oriented on the axes.
- The algorithm is interesting but very slow: Path tracing is slow by principle.
It currently only runs on a Mac.
It should be possible to run it on a iOS device, but a little adaptation is needed because it runs on the command line.
Running it on Linux would be more work, since it relies on Core Graphics to save the image to a PNG file. (It also imports Darwin for mathematical functions, but that can be replaced with libc on Linux).
Begin with main.swift. It contains enough comments to understand the structure of the app.
Take a look at Bitmap.swift which does low-level memory access to the bitmap.
Vector.swift also contains overloaded operators. Thanks to Swift, the implementation is simple and elegant.