GPU Ray Marching with Distance Fields

First something completely else; I recently got back from No More Sweden 2010, the annual meet-up for Nordic indie developers. This year was less jamming and more awesome presentations & hanging out (discussing game development, indie gossip, etc :) ). Big thanks to Daniel Kaplan and Jens Bergensten for organizing it!

A couple of days ago I stumbled upon legendary demo coder Iñigo Quílez’ site. There I found an article, ‘Rendering Worlds With Two Triangles’, describing the idea of optimizing ray marching (ray tracing done in discrete steps — perfect for scenes where intersections can’t be found analytically) by using distance fields. A distance field supplies the minimum distance to any surface in a scene from any point. A ray can therefore do a look up in the distance field at its position and step at least the returned distance in its direction before a new look up is necessary. The difficulty of course lies in defining a distance field that matches the geometry we want to render. Quílez proposed going the other way around, defining a distance field as a means to model a scene. This pretty much blew my mind!

I decided to implement my own version in Söder Engine (DX10). As opposed to Quílez I wanted to make a scene that was rendered in real time so my screenshots are probably not as exciting as his :) Feel free to download the application and write a better scene though! It’s all defined in the shaders, which can be reloaded by pressing F1.

This method has some amazing properties. For instance, fake (empirical) ambient occlusion and subsurface scattering was a piece of cake to add :)

Download application

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Posted May 14, 2011 at 10:18 | Permalink

    Very nice and well written source also. Specially compared to all size optimized frameworks out there that use distance fields.

  2. Posted June 14, 2011 at 23:26 | Permalink

    Thanks Joakim! I just checked out Plane9 – lovely stuff!

  3. Phil S.
    Posted May 5, 2012 at 08:29 | Permalink

    Lookin’ good! What do you need DistanceToAABB for? Is that axis-aligned bounding box?

  4. Phil S.
    Posted May 5, 2012 at 08:30 | Permalink

    Ah, just a box, got it :)

One Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>