## Raymarching Beginners' Thread

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

(hmmm that modelling tool looks really cool :))

I know it's obvious stuff and an old approach, but I hadn't actually seen it done in a proper tool before. There's a difference between "Hey, that's easy to do," and then actually doing it. A lot of great ideas have been shattered by shitty implementations.

iq: long-belated reply; yes, that's how Himalaya was done :)

Okay. I'm currently working on that nice simplex noise :)

It's really cool all - but I suggest some optimizations:

1. "step" is faster than lessThanEqual (vec3(lessThanEqual(A,B)) == step(A,B)) and greaterThan (== 1. - step(A,B))

2. the permutation can be done faster by moving the floor out of the permutation and some inlining

3. working on it.

=> it's already a bit faster than the original one and i'm still getting the same noise.

It's really cool all - but I suggest some optimizations:

1. "step" is faster than lessThanEqual (vec3(lessThanEqual(A,B)) == step(A,B)) and greaterThan (== 1. - step(A,B))

2. the permutation can be done faster by moving the floor out of the permutation and some inlining

3. working on it.

=> it's already a bit faster than the original one and i'm still getting the same noise.

las - why are you telling us? Go make some noise on the ogl.org

las ...contact info is in the paper :) Also, the guy who started the thread is the one who wrote the paper.

hornet: too late - btw. that's not the same guy who did the implementation - already posted a link to changed the code - I didn't care to read the license file - so I hope it will be ok :D

I was reading through Smash's smashing blog http://directtovideo.wordpress.com/ and came across this "All the hype about distance fields made me get around to writing a proper mesh to signed distance field conversion routine for some effect or other" :O It says he'll may be writing an article about it soon. Can't wait!

The guys who are tutoring me on my bachelor project also did a paper on the subject.

Yeah, that WOULD be interesting. I'm really struggling to think how that could work. Using the actual mesh within the fragment shader is probably out of the question.

Pre-computing it? Storing the signed distance field as a 3d texture? That could work, although accuracy would depend on resolution and I'm not sure if animation would be workable. Actually, he's talking about collisions with particles, and he's talking about a fixed scene. It might be accurate enough for that, but you probably wouldn't want to use it for lighting for a really complex object.

Pre-computing it? Storing the signed distance field as a 3d texture? That could work, although accuracy would depend on resolution and I'm not sure if animation would be workable. Actually, he's talking about collisions with particles, and he's talking about a fixed scene. It might be accurate enough for that, but you probably wouldn't want to use it for lighting for a really complex object.

once youve got the distance fields you find all manner of uses for them. :)

graga: i actually used something like the weighted normal method for signs they describe.

graga: i actually used something like the weighted normal method for signs they describe.

For the "voxelized"-spikeball in Regus Ademordna I computed the signed distance field by rasterizing the object to a 3d grid on the GPU with the stencil buffer (similar to stencil shadows), and performing an O(n) exact distance transform (based on some parabola intersections, don't remember the exact paper) on it. Worked out well, since it was an offline process.

PRECOMPUTED?! OMG!

las, being anal retentive, I did read the license a while back. I remember as long as you provide changes back as open source you are free to redistribute - in other words what you are doing ...

I has been making the rays bounce.

Reflections are fun :)

Reflections are fun :)

psonice: back to the topic... and nice. :)

I'm quite happy with that. The way it moves is pretty funky: http://www.youtube.com/watch?v=jU2M4HEOYyQ

Things are starting to come together for an intro i think :)

Things are starting to come together for an intro i think :)

nice! :)

psonice: I want that for my EP cover. :)

that would rule :D Want a really high quality version?

psonice: yes please :) thankyou.

4mat: drop me an email. Just did a quick test render :) psonice@gmail.com

My first raymarcher:

Inverted Malevich Black Square using orthogonal raymarcher

Inverted Malevich Black Square using orthogonal raymarcher

**Code:**

```
// orthogonal raymarcher using GLSL 1.0, WebGL and ShaderToy
#ifdef GL_ES
precision highp float;
#endif
uniform float time;
uniform vec2 resolution;
uniform vec4 mouse;
// SDF cube
float cube(float x,float y,float z,float size)
{
return max(max(abs(x)-size,abs(y)-size),abs(z)-size);
}
float f(vec3 p)
{
float sdf;
// only one object in the scene currently
sdf = cube(p.x, p.y, p.z, 0.5);
return sdf;
}
void main(void)
{
vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
vec3 color = vec3(0.0,0.0,0.0);
// orthogonal ray cast
vec3 Ro = vec3(p.x,p.y,-1.0); // ray origin
vec3 Rd = vec3(0.0,0.0,1.0); // ray direction
float t = 0.0;
// WebGL uses OpenGL ES which GLSL 1.0 have no support
// neither for "while" loop, neither for "for" loop with float counter
// only integer counter loops allowed
const int maxsteps = 75;
for(int steps = 0; steps < maxsteps; steps++)
{
float d = f(Ro+t*Rd);
const float eps = 0.001;
if(d < eps) // are we too close to some object's surface
{
// WE HIT SOMETHING!!!
color = vec3(1.0,1.0,1.0);
break;
}
t = t + d;
}
gl_FragColor = vec4(color,1.0);
}
```

oh my god, I just understood that

is it better to use?

it uniform outside, but non-uniform inside = 0

maybe this is best cube SDF? author is iq as I remember

**max()**functions of that cube's SDF is non-uniform along spherical coordinates, which can bring serious artefacts, everything must be expressed through geometrical distance i.e. GLSL length() functionis it better to use?

**Code:**

```
float unsigned_sdf_cube( in vec3 p, in float size )
{
vec3 di = max(abs(p)-size,0.0);
return sqrt(dot(di,di));
}
```

it uniform outside, but non-uniform inside = 0

maybe this is best cube SDF? author is iq as I remember

**Code:**

```
float cube( in vec3 p, in float b )
{
const vec3 di = abs(p) - b;
return min( maxcomp(di), length(max(di,0.0)) );
}
```