Rust Raytracer

Rust Raytracer is an implementation of a simple raytracer based on "Ray Tracing in One Weekend" by Peter Shirley. Developed to learn Rust from scratch, the project might not be perfectly idiomatic but it produces visually appealing results. Additional features beyond Shirley's course include texture mapping, lighting, parallel rendering, reading scene data from JSON, and rendering a sky texture.

Project Introduction

The Rust Raytracer project was created to explore the fundamentals of ray tracing while learning the Rust programming language. It extends the basic implementation provided in Peter Shirley's "Ray Tracing in One Weekend" with several advanced features to enhance rendering capabilities.

Features

  • Texture Mapping: Apply textures to objects, such as earth and moon textures.
  • Lighting: Simulate realistic lighting effects in the rendered scenes.
  • Parallel Rendering: Utilize all CPU cores for improved performance.
  • JSON Scene Data: Read scene configurations from a JSON file.
  • Sky Texture Rendering: Render a sky texture for more immersive visuals.

Example Output

Latest output Latest output

https://user-images.githubusercontent.com/237355/147687883-4e9ca4fc-7c3b-4adb-85d7-6b08d1bc69f7.mp4

Example Usage

To build and run the raytracer, use the following commands:

$ cargo build --release
   Compiling raytracer v0.1.0 (/path/to/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

Texture Mapping

cover_alt cover_alt

Lighting

lighting-recast-final lighting-recast-final

Parallel Rendering

Parallel rendering significantly improves performance by utilizing multiple CPU cores.

Original

Rendering anim/frame_000.png
............................................................
Frame time: 21s

Using rayon

Rendering anim/frame_000.png
Frame time: 2573ms

Render a Sky Texture

sky_textures sky_textures

Read Scene Data from JSON File

Example

{
  "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
        }
      }
    }
  ]
}

Make Animation

To create an animation from rendered frames, use:

ffmpeg -f image2 -framerate 15 -i anim/frame_%03d.png -loop -0 anim.gif

Credits

Extreme Lighting Example

147705264-c6f439df-f61b-4bcf-b5e6-c2c755b35b1c 147705264-c6f439df-f61b-4bcf-b5e6-c2c755b35b1c

Progressive Max Depth Animation

max_depth_anim max_depth_anim

Similar Projects

Rustfinity.com

Links

  1. Home
  2. Learn Rust
  3. Get Started
  4. Practice Rust
  5. Challenges
  6. Tutorials
  7. Blog
  8. Open source
  9. Learn Gleam

Socials

  1. GitHub
  2. X

Legal

  1. Privacy Policy
  2. Terms of Service