## Raymarching Beginners' Thread

**category:**code [glöplog]

rudi: Raymaching is a method to compute the intersection of a ray and some object defined by an inside-outside function, and is often used in raytracers to compute intersections with iso-surfaces.

Lately, the demoscene has become obsessed with using procedural distance-guided raymarching to raytrace scenes in 4k-intros, both to save space on the object definitions and to easily get shadows, reflections and other typical raytracing-features.

And this is cool as long as there's something novel about it. But I thow up a little bit in my mouth each time I see a trivial low-resolution, low-framerate rubbervector, which is waaay too often these days. I guess it's a bit of the same thing as happened with grid-expanders in the mid/late 90s; everyone did by-the-book FD-tunnels over and over again.

Lately, the demoscene has become obsessed with using procedural distance-guided raymarching to raytrace scenes in 4k-intros, both to save space on the object definitions and to easily get shadows, reflections and other typical raytracing-features.

And this is cool as long as there's something novel about it. But I thow up a little bit in my mouth each time I see a trivial low-resolution, low-framerate rubbervector, which is waaay too often these days. I guess it's a bit of the same thing as happened with grid-expanders in the mid/late 90s; everyone did by-the-book FD-tunnels over and over again.

Traditional voxel landscapes/tunnels/twisters are mostly forward rendering, not raytracing/marching.

Psycho: A classic voxel landscape march 2D rays in a height-map, and splat visible line-segments as they encounter them. So I agree it's not a raytracer, but IMO it IS raymarching :)

Has anyone tried producing a 3d modeller for distance fields? It would seem to be whats needed for a breakthrough.

**Quote:**

Psycho: A classic voxel landscape march 2D rays in a height-map, and splat visible line-segments as they encounter them. So I agree it's not a raytracer, but IMO it IS raymarching :)

i look at raycasting as some ray that does not trace anything, it just evaluates the point it hits (if it is a heightmap, it evaluates the height). so in voxel landscape (or whatever) the ray discards points along the way if the next point is lower than the previous one. that's how i look at it and how i implemented my voxel landscape several years ago. (of course there's room for different implementations of this, but that's another story). so if kusma says what it is, then i probably know what raymarching is in a sense.

My maths sucks miserably, but there's something I want to try that would work well with ray marching. So I've started, but right at the beginning:

1. 2d, plot a circle in the shader. No marching, but it helps me get my head round it one step at a time.

2. 3d, plot a sphere. Marching with fixed step size to keep it simple. No lighting, just a straight hit test.

All OK so far, and this way I understand it. Next will have to be perspective, lighting and texture, and then more complex/efficient marching, and more complex geometry (what I want to do just needs a sphere, so this can wait :)

1. 2d, plot a circle in the shader. No marching, but it helps me get my head round it one step at a time.

2. 3d, plot a sphere. Marching with fixed step size to keep it simple. No lighting, just a straight hit test.

All OK so far, and this way I understand it. Next will have to be perspective, lighting and texture, and then more complex/efficient marching, and more complex geometry (what I want to do just needs a sphere, so this can wait :)

psonice: go for it! and give us some shots.

Navis, ACK.

vibrator: so far, it's a white circle on a black background. I guess you can imagine that so I'll save the bandwidth ;)

I'll post screenshots when it looks as good as navis' video. Or when I have a lit, textured sphere at least.

I'll post screenshots when it looks as good as navis' video. Or when I have a lit, textured sphere at least.

^ daaaaaaaaaaaaaaaaamn

**Quote:**

auld: Has anyone tried producing a 3d modeller for distance fields?

I thought about it and finally decided that it is much more practical to simply write formulas for that :) Of course it would be nice to have some tool allowing to create simlpe shapes and then bend/blend/join/cut those shapes. But resulting formula will be

**really**slow. Also do not forget about possibility to find something interesting if you make mistake in formula ;)

Raymarching through volumes? How does that work, is it a volume of distances based off the data-set?

The screenshot is just fake sss (sss=1-ao(d,p)) gone wild.

I'm also curious about the Distance Fields Modeling tool. Some operators to combine different functions into code but still be able to control and animate the parameters. A lot of codes must have thought about this already.

@chaos: Do you think this would be possible with operator stacking in W4?

@chaos: Do you think this would be possible with operator stacking in W4?

**Quote:**

But resulting formula will be really slow.

That's exactly the main problem with distfield-modelling tools - Even simple scenes will result in a pretty damn slow formula as long you don't do any optimizations (i.e. domain repetition).

Maybe that's why raymarching is so cool for us coders...

Finally we are not just doing the damn tools for the lazy artists again. :D

Imho the best way is to have a simple editor where you just have your RM-shader code, a viewer window, a pause and reload button + some value sliders (maybe GNU Rocket :)).

And if you want to have that in a "more artist usable way" - just teach your artist a minimal set of GLSL/HLSL instructions (mix, min, max, smoothstep, edge, mod, sin, cos...) + maybe integrate something in the editor that the artist can only modify stuff inside functions he's meant to edit. :D

An artist generating distance fields should - in a way - know what he's doing - especially why things go slow.

An artist generating distance fields should - in a way - know what he's doing - especially why things go slow.

Another question...

How to do fake soft shadows with RM? :)

From RWWTT - iq

Currently only hard shadows here... Used all the magic powder up for the sss yesterday.

How to do fake soft shadows with RM? :)

From RWWTT - iq

**Quote:**

Recipe: take n points on the line from the surface to the light and evaluate

the distance to the closest geometry. Find a magic formula to blend the n

distances to obtain a shadow factor.

Currently only hard shadows here... Used all the magic powder up for the sss yesterday.

Making your blending process a function of distance to the closest geometry

In essence, the whole trick is to use the same AO code I had posted in a clever way. Instead of sampling back on the normal, you sample back to the light source:

You can also apply a variety of it just like rrrola and iq mentioned before. Also, as a simple optimisation you can merge this with the AO procedure to avoid having two loops in the fragment shader. :)

Enjoy!

**on the way to the light source**would give you a smooth shadow. You can also weight your values with the same method to get some sort of a fake high-dynamic feeling. :)In essence, the whole trick is to use the same AO code I had posted in a clever way. Instead of sampling back on the normal, you sample back to the light source:

**Code:**

```
float shadowFactor = 0.;
vec3 q = normalize(lightSource - p);
for (float i = 0.; i < 5.; ++i) {
shadowFactor += vec3(i * k - ƒ(p + q * i * k)) / pow(2., i);
}
```

You can also apply a variety of it just like rrrola and iq mentioned before. Also, as a simple optimisation you can merge this with the AO procedure to avoid having two loops in the fragment shader. :)

Enjoy!

umm there's some typo in the code. well it's quite pseudo anyway :) you can't implicitly convert from vec3 to float, but since that vec3 there represents a "greyscale colour" you can just remove the 'vec3(' and ')' around it and have your shadow factor as a single float.

sorry I am quite sick and my nose is full, so I can't concentrate well…

sorry I am quite sick and my nose is full, so I can't concentrate well…

Okay that was to simple ;)

But faking everything with the ao routine... ;)

Come on - there must be another magic formula to blend the n distances!

But faking everything with the ao routine... ;)

Come on - there must be another magic formula to blend the n distances!

Thanks + get well soon!

Actually it's not really faking, a shadow is actually the obstruction of a light source's effect on a certain point. :) That's exactly what the formula calculates. It might not be 100% scientifically correct, however it's very close to the mathematical implementation of the actual definition of a shadow. :)

And you're welcome. :)

And you're welcome. :)

pixtur: I'm not exactly Chaos but I know the Wz4 well enough that I can say: Yes, perfectly. Somebody just has to code it :D