raymarching

Optical illusion: self-tangling wires (Composition by psonice)

Author: psonice
License: (unknown)
Date: 2011.03.24
Compatibility: 10.6
Categories:
Required plugins:
(none)

Yep, this one is a bit of a mindf**k :) Mouse rotates it (with a trackball).

I was just experimenting a bit with my raytrace/raymarching setup. I wanted to see if I could make a 'weave' (like a basket material), in 3d. Well, I did, using just 2 spheres and some cunning.

Then I wondered, what happens if I modify it with some sin waves? Well, I wasn't quite expecting this :D It looks like a weave that re-weaves itself. Really confusing to watch.

Technical stuff: It's raymarched, using a signed distance field, with ambient occlusion + regular lighting. There's not much to see in the comp, just a shader and a single quad really. The magic happens in the shader.

Try moving the mesh around to move/rotate the camera.

Minor warning: it needs a fast GPU. An intel one definitely won't cut it (you'll be waiting for it to render each frame!) It runs 30fps or so at 640x480 here (recent imac).

Raytracing + raymarching in QC

psonice's picture

I've started to write my own raymarching renderer in GLSL + QC, thought I'd share my output (and some of the code + comps) here.

This is roughly where I'm up to so far:

Moderately complex geometry, lighting + soft shadows. The shadows were hard, I wanted decent quality soft shadows from a point light without too much of a speed hit.

The camera can be moved + rotated in any direction, the light can be moved freely. Adding additional lights with or without shadows is trivial :) Ambient occlusion (true AO, not screenspace) is also trivial, but it seems to look better without it. Animating the object is also trivial.

No .qtz yet, until I've worked out some of the bugs and optimised it a bit. As it stands, the camera can't move outside of a limited space, and repeating objects glitch badly if they're not tightly managed.

Quick rundown of what raymarching is:

  • You render a single polygon, with a GLSL shader (openCL would also work, but without major benefits and with major compatibility issues ;)

  • Inside the shader, you determine camera position and direction for each pixel, and you do a form of raytracing in that direction (this is the 'raymarching' step).

  • To raymarch, you calculate the distance from the current position to the nearest surface. Then, you can 'march forwards' along the ray by that distance, and repeat, until you hit an object.

  • You then calculate the surface angle at that point where the ray hits, so you can do lighting and the rest.

It's also referred to as 'distance fields' because everything is based on the distance from a surface, or 'sphere tracing' because you effectively calculate the intersection of a sphere and the geometry.

Benefits of this kind of rendering:

  • It's "fast" (well, for raytracing!)

  • The scene can be infinitely big and complex. Trivial example: an infinite 3D grid of cubes. This is very quick and easy with raymarching. How many can you do with an iterator? ;)

  • Advanced lighting effects, reflections, refraction, AO, subsurface scattering are all fairly simple. You can use them all at the same time, with a bit of a performance hit.

The downside:

  • It's hard :( Just moving the camera becomes a brain twister.

  • It's slow, compared to normal rendering. 640x480 at 30fps needs a high end GPU.

  • You can't use 3d models and the like. Everything gets built with mathematical formulas.