Brandon Eleuterio


Learning Rust – Part 8: Diffuse Materials

Brandon Eleuterio

In Part 7, we smoothed out the edges of our spheres using antialiasing. In this chapter, we start making these spheres look a little more realistic.

Speed Bump

After adding initial diffuse properties to the spheres I noticed a severe slowdown in execution time. I mean we’re talking over 5 minutes to complete the render process! I cracked open my performance monitoring tool of choice (XCode Instruments since I’m on a Mac) and started identifying long-running functions. It turned out all the random ray generation necessary to create a diffuse appearance was the culprit.

In the end, two changes dramatically increased speed:

  1. Increased the optimization level of the Rust dev profile:
    opt-level = 1
  2. Fixing the shadow acne problem where too much speckling occurs by changing the second parameter passed into the world.hit() function from 0.0 to 0.001.
    if world.hit(r, 0.001, INFINITY, &mut rec) {
    This means we ignore more hits and don’t generate as many rays which makes things so much faster!

Three Diffusion Options

We now have three different options for creating diffuse lighting for our spheres:

rejection diffuse
Lambertian diffuse
Alternative diffuse

Notice the subtle differences in shadow, shading, and color. Not sure if any one of these is better than the others, just different. All three of these options could be useful down the road as we render more complex scenes.


Next time, we start making these spheres look metallic! In the meantime, feel free to follow my code and the online book I’m following.


Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top