## Raymarching Toolbox Thread

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

Jep. Much better! Nice. Keep it up :)

**Code:**

```
// vec3 Color float Contrast Saturation Brightness
#define csb(f,c,s,b) mix(vec3(.5), mix(vec3(dot(vec3(.2125, .7154, .0721), f*b)), f*b, s), c)
```

More here:

http://mouaif.wordpress.com/2009/01/05/photoshop-math-with-glsl-shaders/

As stated in the lighting for 1k intros thread field(p) = 0 when at the surface, thus the gradient calculation can be simplified to:

**Code:**

```
vec3 grad(vec3 p) {
vec2 e = vec2(0.001, 0.0);
return vec3(field(p+e.xyy), field(p+e.yxy), field(p+e.yyx)) / e.x;
}
```

unc just proposed a nice way to get smooth iteration glow without banding when using only a low number of iteration steps.

It works by accumulating "something with the current distance" and some magic... You have to play around with the values anyways but the accumulation could look something like this (Gives me a nice glow with 32 iteration steps without banding):

It works by accumulating "something with the current distance" and some magic... You have to play around with the values anyways but the accumulation could look something like this (Gives me a nice glow with 32 iteration steps without banding):

**Code:**

```
// a is the accumulated glow value
vec3 rm(inout vec3 p, inout vec3 d, float s, float thr, float n) {
float l, i, m, a;
for (a=l=i=m=0.; i<1. && l>=thr*m; i+=n) {
l = abs(f(p))*s;
a += smoothstep(0.,0.2,l)*i*i; // play around here.
m += l;
p += l*d;
}
return vec3(i, m, a);
}
```

Even

gives already nice results :)

**Code:**

```
a+=l*i;
```

gives already nice results :)

Normals the tetrahedral way - suggested by PauloFalcao here.

Implemented crunched by applying swizzling :)

Implemented crunched by applying swizzling :)

**Code:**

```
vec3 g(vec3 p) {
vec2 e = vec2(.0001, -.0001);
vec4 o = vec4(f(p+e.xyy), f(p+e.yyx), f(p+e.yxy), f(p+e.xxx));
return (o.wzy+o.xww-o.zxz-o.yyx)/(4.*e.x);
}
```

Makes no difference to here. Why is that better than

?

**Code:**

```
vec3 g(vec3 p) {
vec2 e = vec2(0.001, 0.0);
return vec3(f(p+e.xyy), f(p+e.yxy), f(p+e.yyx)) / e.x;
}
```

?

It should be more "symmetric" or something like that I guess.

Also "makes no difference" depends on your distance field and the epsilons of the gradients.

It's just an alternative way to do it - as you said it makes no visible difference for you - it might be good to have several different implementations especially if you are going to compress your shaders.

Also "makes no difference" depends on your distance field and the epsilons of the gradients.

It's just an alternative way to do it - as you said it makes no visible difference for you - it might be good to have several different implementations especially if you are going to compress your shaders.

**Code:**

```
// iq's cellular noise from leizex shadertoy source - modified to use deciphers noise (=n)
vec2 cn(vec3 p) {
vec3 i = floor(p);
vec3 f = p-i;
// vec3 f = smoothstep(0., 1., p-i);
vec2 r = vec2(1.0);
for (float z = -1.; z <= 1.; ++z) {
for (float y = -1.; y <= 1.; ++y) {
for (float x = -1.; x <= 1.; ++x) {
vec3 c = (n((i+vec3(x, y, z)))*.5+.5) + vec3(x, y, z) - f;
float d = dot(c, c);
if (d < r.x) {
r.y = r.x;
r.x = d;
} else if (d < r.y) {
r.y = d;
}
}
}
}
return sqrt(r);
}
```

It's very slow.

for obvious reasons :)

Yet another simple primitive build from cylinder and two half spheres (and not two spheres - using two spheres results in ugly results while using sss or other similar techniques).

**Code:**

```
float capsule(vec3 p, float r, float c) {
return mix(length(p.xz)-r, length(vec3(p.x,abs(p.y)-c,p.z))-r, step(c,abs(p.y)));
}
```

... and still nobody is able to render fake boobs and ass. :D

well... if this' not motivation. LOL

well... if this' not motivation. LOL

Pouetification fail :)

**Code:**

```
float perlin(vec3 p) {
vec3 i = floor(p);
vec4 a = dot(i, vec3(1., 57., 21.)) + vec4(0., 57., 21., 78.);
vec3 f = cos((p-i)*acos(-1.))*(-.5)+.5;
a = mix(sin(cos(a)*a),sin(cos(1.+a)*(1.+a)), f.x);
a.xy = mix(a.xz, a.yw, f.y);
return mix(a.x, a.y, f.z);
}
```

this' some heavy per pixel compuitation for a simple NOISE. fuck it. worthless. ;D

Shut the fuck up. You can discuss that somewhere else. Simplex != simple.

http://en.wikipedia.org/wiki/Simplex

http://en.wikipedia.org/wiki/Simplex

hmmm.. the glsl code in the link produces rather "regular" simplex noise.. also i don't really see the benefit of it over using the original simplex implementation unless one -really- has to go without a LUT or texture..

oh, and the taylor series approx of 1/sqrt() sucks, too.. it can produce quiet "nice" artifacts for certain regions..

las, decipher, psycho, unc, @lx, ferris, mentor, xTr1m, kusma, ryg, smash, everybody that i'm forgetting (so long i don't check latest prods) who has done or has something to say about raymarching, distance fields, maths, noise, gradients, lighting, volumetric scattering, GI... could you send me an email please? iquilezles at the hotest email system dot com. thx!

forgot of course gopher, blueberry, etc. well , you know who you are.

change topic, about noise, the LUT version is still the fastest nowadays. I used it in Elevated. It computes value noise, and it's derivatives at the same time. value noise is usually crappy, but good enough if you combine several octaves. still, nothing as beautiful as gradient noise :(

change topic, about noise, the LUT version is still the fastest nowadays. I used it in Elevated. It computes value noise, and it's derivatives at the same time. value noise is usually crappy, but good enough if you combine several octaves. still, nothing as beautiful as gradient noise :(

For a relatively fast but good looking noise I can recommend perlins own gpu approximation from gpugems.

Its just to generate a 3d texture with a normal perlin noise then sample using

Used on its own it shows linear artifacts but when combined in a turbulence/fbm they wont be as noticeable.

Simple speed test 120 noise textures with 5 octaves of 3d turbulence using the perlin noise above that las shows 45fps but shows serious artifacts. This noise 118 fps. But in a raymarching scenario the performance is probably quite different

Its just to generate a 3d texture with a normal perlin noise then sample using

**Code:**

```
float2 hi = tex3D(tex1, p).rg*2.0-1.0;
float lo = tex3D(tex1, p/9.0).r*2.0-1.0;
float s, c;
sincos(lo*PI2, s, c);
return hi.r * c + hi.g * s;
```

Used on its own it shows linear artifacts but when combined in a turbulence/fbm they wont be as noticeable.

Simple speed test 120 noise textures with 5 octaves of 3d turbulence using the perlin noise above that las shows 45fps but shows serious artifacts. This noise 118 fps. But in a raymarching scenario the performance is probably quite different

Link failure correct one