An implementation of a very simple raytracer based on Ray Tracing in One Weekend by Peter Shirley in Rust. I used this project to learn Rust from scratch - the code may not be perfectly idiomatic, or even good, but it does make pretty pictures.
Additional features beyond Shirley's course:
- Texture mapping (e.g. earth and moon textures below)
- Lighting
- Parallel rendering - will use all CPU cores for best performance
- Read scene data from JSON file
- Render a sky texture
anim.mp4
$ cargo build --release
Compiling raytracer v0.1.0 (/Users/dps/proj/rust-raytracer/raytracer)
Finished release [optimized] target(s) in 2.57s
$ ./target/release/raytracer data/test_scene.json out.png
Rendering out.png
Frame time: 2840ms
$ ./target/release/raytracer data/cover_scene.json cover.png
Rendering cover.png
Frame time: 27146ms
🚀 ./target/release/raytracer anim/frame
Compiling raytracer v0.1.0 (/Users/dps/proj/rust-raytracer/raytracer)
Finished release [optimized] target(s) in 2.21s
Rendering anim/frame_000.png
............................................................
Frame time: 21s
Rendering anim/frame_000.png
Frame time: 2573ms
{
"width": 800,
"height": 600,
"samples_per_pixel": 128,
"max_depth": 50,
"sky": {
"texture":"data/beach.jpg"
},
"camera": {
"look_from": { "x": -2.0, "y": 0.5, "z": 1.0 },
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
"vup": { "x": 0.0, "y": 1.0, "z": 0.0 },
"vfov": 50.0,
"aspect": 1.3333333333333333
},
"objects": [
{
"center": { "x": 0.0, "y": 0.0, "z": -1.0 },
"radius": 0.5,
"material": {
"Texture": {
"albedo": [
1.0,
1.0,
1.0
],
"pixels": "data/earth.jpg",
"width": 2048,
"height": 1024,
"h_offset": 0.75
}
}
}
]
}
🚀 ffmpeg -f image2 -framerate 15 -i anim/frame_%03d.png -loop -0 anim.gif
Earth and moon textures from https://www.solarsystemscope.com/textures/