1. Home
  2. Explore
  3. Computer Science
  4. Ray Marching Distance Functions

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Ray Marching Distance Functions

πŸš€ Ray Marching: Exploring 3D Graphics and SDFs

Ray marching is a powerful technique in computer graphics that allows for rendering complex 3D scenes by taking incremental steps along a ray until an intersection with a surface is found. Unlike traditional ray tracing which requires explicit intersection calculations for each shape, ray marching leverages Signed Distance Functions (SDFs) to efficiently determine distances and detect collisions.

πŸ“ Calculating Distances with Signed Distance Functions (SDFs)

At the core of ray marching are distance functions that calculate the shortest distance from a given point to a surface. These functions are crucial for determining how far a ray can safely travel before potentially hitting an object.

πŸ“ Basic Distance Calculations

  • Distance between two points: This fundamental calculation is based on the Pythagorean theorem.

  • Distance from a point to a circle:

    • Calculated as the distance between the point and the circle's center minus the radius of the circle.

    • This is an example of a Signed Distance Function (SDF).

πŸ“‰ Signed Distance Functions (SDFs)

  • An SDF provides a signed distance from a point to a surface.

  • If the point is outside the surface, the distance is positive.

  • If the point is inside the surface, the distance is negative.

  • If the point is on the surface, the distance is zero.

  • This negative distance for points inside is particularly useful for advanced rendering techniques, such as boolean operations and shape inversion.

  • Another example of an SDF is the distance from a point to the edge of a box.

🌍 Scene Distance Function

  • To find the distance to the nearest surface in a scene composed of multiple shapes, you can:

    1. Loop over all shapes in the scene.

    2. Calculate the SDF for each shape.

    3. Return the smallest (minimum) of these distance values. This minimum distance represents the closest surface to the point.

πŸšΆβ€β™‚οΈ How Ray Marching Works

Ray marching simulates casting rays from a viewpoint (an "eye") into a 3D scene to determine what surfaces they intersect.

✨ The Ray Marching Process

  1. Cast a ray from the "eye" into the scene.

  2. Calculate the distance to the scene (the smallest SDF value) from the ray's current position.

  3. This distance provides a safe radius within which the ray can move along its direction without passing through any objects.

  4. Advance the ray by this calculated distance.

  5. Repeat steps 2-4 until the calculated distance becomes very small (approaching zero), indicating a collision with a surface.

Ray marching essentially involves moving a ray along in steps, with the step size determined by the distance to the nearest surface, until an intersection is found.

🌟 Sphere Tracing

  • A common type of ray marching is sphere tracing.

  • It optimizes the process by using the radius of the sphere (or circle in 2D) calculated by the SDF as the step size. This allows for larger, more efficient steps compared to fixed-size steps.

  • When extended to three dimensions, sphere tracing can render a wide variety of shapes, including cubes, spheres, and any other object for which an SDF can be defined.

🎨 Creative Applications with SDFs

The ability to define shapes using SDFs opens up a world of creative possibilities beyond simple rendering.

🧱 Boolean Operations (Combining Shapes)

  • SDFs can be combined using simple mathematical operations to create complex shapes from simpler ones.

  • Union (Min Function):

    • Using the min() function between two SDFs (e.g., min(distanceToCube, distanceToSphere)) creates a union of the shapes.

    • The resulting shape's surface is where the distance is zero, effectively merging the two objects.

  • Intersection (Max Function):

    • Using the max() function between two SDFs (e.g., max(distanceToCube, distanceToSphere)) creates an intersection of the shapes.

    • The surface of the new shape exists only where both original SDFs are zero or less, meaning it forms the common volume shared by both objects.

    • This demonstrates why negative distances (points inside) are crucial for SDFs.

βœ‚οΈ Shape Inversion and Subtraction

  • Multiplying an SDF by -1 effectively inverts the shape.

    • The inside becomes the outside, and vice-versa.

    • For example, max(distanceToCube, -distanceToSphere) would create a shape that is the cube with the sphere cut out of it.

    • This is a powerful technique for subtracting one shape from another.

🌊 Smooth Blending (Smooth Min Function)

  • Instead of a sharp min() function for unions, a smooth min function can be used.

  • This function blends objects together where they are close, creating a seamless, organic connection between surfaces.

  • The degree of smoothing can often be adjusted to achieve different visual effects.

πŸ’‘ Glowing Outlines

  • As a ray approaches a surface, the number of ray march steps dramatically increases because the step size (SDF value) becomes very small.

  • This data (number of steps) can be used to generate a glowing outline effect around objects, essentially "for free" as a byproduct of the ray marching process.

🌌 Advanced Explorations

Ray marching can lead to fascinating visual effects and complex procedural generation.

πŸŒ€ Procedural Worlds

  • By creating custom, often abstract, distance functions that blend various mathematical operations (e.g., sign() function, noise), designers can generate repetitive, glitchy, or organic worlds that are fun to explore.

  • Tweaking parameters in these functions can dramatically alter the generated environment.

🍎 Mandelbulb Fractal

  • Ray marching is particularly well-suited for rendering fractals defined by distance functions.

  • The Mandelbulb is a 3D fractal that can be rendered by plugging its complex distance function into a ray marching algorithm.

  • The results are often visually mesmerizing and demonstrate the power of SDFs for rendering infinitely detailed structures.


Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent