5 ms·
After quickly skimming this article, I got sucked down the rabbit hole of "ray marching" - in concept, and implementation (mainly as a shader). The idea and how
by cr0sh 9y ago
After quickly skimming this article, I got sucked down the rabbit hole of "ray marching" - in concept, and implementation (mainly as a shader). The idea and how it works was fascinating, if a little above my head mathematically. I got the general gist of the idea, though; it's kinda a combination of ray casting and ray tracing, where you send out the rays and step along them, but in ray marching you don't cast secondary or tertiary (or more rays) like you do in ray tracing (thus limiting the processing, but also the level of realism) and instead continue to "step" (ie - "march") along the ray past the intersection, to gather more information (and thus you can do volumetric stuff with transparency - smoke, water, clouds, marble, skin, etc).
Stepping is done using something called "sphere tracing" - where for each step along the ray, you compute a sphere, and where it intersects the scene, and only step that far; as you get closer to surfaces, the spheres become smaller (and so do the steps and more computations) - this keeps you from sampling along the ray where there is no scene, and stepping along the ray in larger "jumps" to keep the computational reqs down (it has limitations, particularly along edges of objects, and complex scenes).
For 4k comps (and in general, I think) objects of the scene are described algorithmically (ie - you don't create a model of a sphere or torus or cube, but rather the mathematical representation of it) - so intersects with the ray become a bit trivial. At the same time, complex scenes can become tedious to lay out, so you typically see tessellations and repetitions of scene objects, or other mathematical constructs are used (fractals and the like) that can generate complexity from simple representations (thus, procedural rendering and representational concepts come into play of course).
There was a ton more involved (found interesting shadertoy demos from known authors, going over the concepts of how raymarching and related techniques are used - especially in the context of GPU shader rendering, where it is frequently used in 4k compos - the idea of "two triangle demos", doing everything in the shaders) - and I probably got a lot of this wrong or over-simplified (as I said, I felt a lot went over my head in the math arena).
But I think I got the gist. Regardless, it was a fascinating exploration, and taught me some new things I didn't know about until then; I haven't kept up much with the demoscene since I stopped playing around with my Amigas a long time ago (aside from the occasional amazing demo or whatnot here and there - especially those authors who like to push their Craft - hint-hint - using microcontrollers). So it was nice to see what "state of the art" was. Although - milkytracker is still being used as well - which is nice to see.
- westoncb 9y agoI think that's a pretty nice overview you have. One thing I would clarify is about how the objects in the scene are represented mathematically: it's done using 'signed distance functions' so every object you have in the scene is actually created through one of these functions which takes a point in 3D space and returns the distance to the nearest point on the object's surface to that point—which does, as you say, make the object/ray intersection testing trivial (the return value of this function is the radius of the sphere used to step along the ray). The other basic components when creating scenes to be raymarched are distance functions which combine other distance functions together. The simplest cases are just union and intersection, but it's easy to get more interesting things like smoothly blending objects together. This page is a nice reference of these 'combining' functions as well as distance functions for geometric primitives: http://mercury.sexy/hg_sdf/ http://mercury.sexy/hg_sdf/ I think that combining power is one of the main reasons to use raymarching/distance functions: building procedural content this way is faaar easier than trying to knit triangle meshes together. Additionally, if anyone wants to go a little further on the subject of building up sophisticated content by raymarching distance functions, this is an excellent talk on the subject: https://www.youtube.com/watch?v=s8nFqwOho-s https://www.youtube.com/watch?v=s8nFqwOho-s
- userbinator 9y agoHere is another page on distance functions... http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm http://www.iquilezles.org/www/articles/distfunctions/distfun... ...written by one of the authors of this amazing 4k (four kilobytes!) demo: https://www.youtube.com/watch?v=jB0vBmiTr6o https://www.youtube.com/watch?v=jB0vBmiTr6o
- geon 9y ago> but in ray marching you don't cast secondary or tertiary (or more rays) like you do in ray tracing (thus limiting the processing, but also the level of realism) Not necessarily. The marching is used to sample a volume to find intersections (or density), so you can absolutely march secondary rays for shadows, reflections etc. http://9bitscience.blogspot.se/2013/07/raymarching-distance-fields_14.html http://9bitscience.blogspot.se/2013/07/raymarching-distance-...