# pouët.net

## Raymarching Toolbox Thread

category: code [glöplog]
Jep. Much better! Nice. Keep it up :)
added on the 2011-02-22 16:33:37 by las
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:
added on the 2011-02-22 18:21:27 by las
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; } ```
added on the 2011-02-22 18:55:30 by raer
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):
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); } ```

added on the 2011-02-26 12:50:22 by las
Even
Code:``` a+=l*i; ```

gives already nice results :)
added on the 2011-02-26 13:09:00 by las
Normals the tetrahedral way - suggested by PauloFalcao here.
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); } ```
added on the 2011-02-27 15:31:02 by las
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; } ```

?
added on the 2011-02-27 16:34:46 by raer
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.
added on the 2011-02-27 18:25:30 by las
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.
added on the 2011-03-06 01:28:53 by las
for obvious reasons :)
added on the 2011-03-06 01:29:32 by las
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))); } ```
added on the 2011-03-06 01:41:50 by las
... and still nobody is able to render fake boobs and ass. :D

well... if this' not motivation. LOL
added on the 2011-03-06 03:40:05 by yumeji
Pouetification fail :)
added on the 2011-03-06 04:03:33 by ferris
added on the 2011-03-06 11:46:43 by las
added on the 2011-03-06 11:53:51 by Puryx
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); } ```
added on the 2011-03-06 18:17:45 by las
added on the 2011-03-20 05:15:45 by las
this' some heavy per pixel compuitation for a simple NOISE. fuck it. worthless. ;D
added on the 2011-03-20 05:39:57 by yumeji
Shut the fuck up. You can discuss that somewhere else. Simplex != simple.
http://en.wikipedia.org/wiki/Simplex
added on the 2011-03-20 11:44:55 by las
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..
added on the 2011-03-22 16:06:40 by toxie
oh, and the taylor series approx of 1/sqrt() sucks, too.. it can produce quiet "nice" artifacts for certain regions..
added on the 2011-03-22 18:15:12 by toxie
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!
added on the 2011-03-22 18:41:22 by iq
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 :(
added on the 2011-03-22 18:48:58 by iq
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
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
added on the 2011-03-22 19:57:35 by Xetick
Link failure correct one
added on the 2011-03-22 19:59:22 by Xetick