pouët.net

Go to bottom

category: code [glöplog]
Under some circumstances the ao version with abs() does not work properly...

This seems to be more correct :)
Code:``` float ao(vec3 p, vec3 n, float d, float i) { float o,v; for (o=1.;i>0.;i--) { o-=(i*d-f(p+n*i*d))/exp2(i); } return o; } float sss(vec3 p, vec3 n, float d, float i) { float o,v; for (o=0.;i>0.;i--) { o+=(i*d+f(p+n*i*d))/exp2(i); } return o; } ```

What's the best way to crunch those two into one function?
added on the 2011-02-11 17:21:15 by las
Okay... tricks
Code:``` float ao(vec3 p, vec3 n, float d, float i) { float o,v; for (o=1.;i>0.;i--) { o-=(i*d-f(p+n*i*abs(d)))/exp2(i); } return o; } ```

sss = ao(p,n,-d,i)-1.;
Works properly.
added on the 2011-02-11 17:38:59 by las
Sorry for spamming...
Code:``` float ao(vec3 p, vec3 n, float d, float i) { float o,s=sign(d); for (o=s*.5+.5;i>0.;i--) { o-=(i*d-f(p+n*i*d*s))/exp2(i); } return o; } ```

negative d -> sss, positive d -> ao
added on the 2011-02-11 17:51:00 by las
I would not mind hosting a wiki related to raymarching.

I just set one up, never did before so I have not set any settings yet. Let me know if there is interest in using this.

http://rmwiki.gammalyrae.com/index.php/Main_Page
added on the 2011-02-12 13:38:43 by Rob
Imho this threads still works pretty well and there's no need for a wiki yet.
added on the 2011-02-12 14:16:11 by las
Quote:
Imho this threads still works pretty well and there's no need for a wiki yet.

You have my vote. I'll cleanup and add the good info to my webpage also but this thread will certainly do and everyone here already have an account to add to the thread.
added on the 2011-02-12 15:18:02 by Xetick
Fine, I will remove it then.
added on the 2011-02-12 16:09:00 by Rob
Decipher's (?) noise used in Muon Baryon and Magnus found here
Code:``` /* Given a position, this function generates a 3D co-ordinates based, * reconstructible static noise. */ float noise(vec3 position) { position.x += position.y * 57. + position.z * 21.; return sin(cos(position.x) * position.x); /* The following is an alternative for the previous line: * return fract(position.x * position.x * .0013) * 2. - 1.; */ } /* Given a position, this function generates a 3D co-ordinates based, * reconstructible linearly interpolated smooth noise. * * This function uses the noise() function above for its underlying * noise texture. */ float smooth_noise(vec3 position) { vec3 integer = floor(position); vec3 fractional = position - integer; return mix(mix(mix(noise(integer), noise(integer + vec3(1, 0, 0)), fractional.x), mix(noise(integer + vec3(0, 1, 0)), noise(integer + vec3(1, 1, 0)), fractional.x), fractional.y), mix(mix(noise(integer + vec3(0, 0, 1)), noise(integer + vec3(1, 0, 1)), fractional.x), mix(noise(integer + vec3(0, 1, 1)), noise(integer + 1.), fractional.x), fractional.y), fractional.z) * .5 + .5; } /* Given a position, this function constructs the oh-so-famous Perlin * noise. */ float perlin(vec3 position) { return smooth_noise(position * .06125) * .5 + smooth_noise(position * .125) * .25 + smooth_noise(position * .25) * .125; } ```
added on the 2011-02-13 17:28:00 by las
Code:``` vec3 fractional = position - integer; ```

you might want to use
Code:``` vec3 fractional = smoothstep(0., 1., position - integer); ```
added on the 2011-02-13 20:06:33 by las
las: Your smooth_noise is rather the famous perlin noise. However its not true perlin noise since you use different permutation values. Not that it matters since this is about speed but just so anyone that uses it knows what they are using.

Your perlin function is a fractal sum
added on the 2011-02-13 20:36:48 by Xetick
Xentic: You're right, last time when I was posting it on pouet I must have commented wrongly. Sorry for my mistake. The second is Perlin while the last one is a very cheap and approximate fractal sum of Perlin. Thanks for the heads up.
added on the 2011-02-13 21:21:19 by decipher
There are several ways to create shadows...
This is what I tried and it seems to work nice for simple objects/scenes.
Code:``` // p pos, l direction to light - normalized, d stepwidth, i number of steps float shadow(vec3 p, vec3 l, float d, float i) { float o; for (o = 0.; i > 0.; i--) { o += f(p+l*i*d); } return clamp(o, 0.0, 1.0); } ```
added on the 2011-02-15 15:41:13 by las
Another way...
Code:``` //p point as usual. l direction to lightsource - normalized, r "some kind of shadow hardness", d stepwidth, i number of steps float shadow(vec3 p, vec3 l, float r, float d, float i) { float o; for (; i>0.; i--) { o=min(f(p+l*i*d),r); } return max(o/r, 0.); } //sample usage shadow(p, normalize(vec3(sin(t)*2.0, 10.0+abs(sin(t))*2.0, 3.0*cos(t))-p), 0.5, 0.5, 7.0); ```
added on the 2011-02-15 16:28:34 by las
Improve this code ;)
Code:``` //repeat around y axis n times void rp(inout vec3 p, float n) { float w = 2.0*pi/n; float a = atan(p.z, p.x); float r = length(p.xz); a = mod(a+pi*.5, w)+pi-pi/n; p.xz = r*vec2(cos(a),sin(a)); } //sample rp(p, 6.0); return box(p+vec3(0.5,0.0,0.0), vec3(0.15, 1.85, 1.85)); ```

added on the 2011-02-17 13:19:14 by las

Code:```// pos = point on surface, l = light position, r = shadow softness factor, f = shadow strength, i = step count float softShadow(vec3 pos, vec3 l, float r, float f, float i) { float d; vec3 p; float o = 1.0, maxI = i, or = r, len; for (; i>1.; i--) { len = (i - 1.) / maxI; p = pos + (l * len); r = or * len; d=ƒ(p); o -= d < r ? (r - d)/(r * f) : 0.; if(o < 0.) break; } return o; }```
added on the 2011-02-17 13:41:01 by psonice
That last one is for point lights btw. It does seem to work ok with a normalised light direction too, and it kind of works for AO if you feed it a normal.
added on the 2011-02-17 13:44:40 by psonice
Ohh damn, I forget that one :)
That trick is indeed worth mentioning
added on the 2011-02-18 12:36:13 by las
Generalized rotation macro:
Code:`#define R(p,a) p=float2(p.x*cos(a)+p.y*sin(a),p.y*cos(a)-p.x*sin(a));`

Usage: R(p.yz,time)
(yes, constructions like p.yz.x actually work in HLSL)
added on the 2011-02-21 14:46:46 by KK
Works in GLSL too.
added on the 2011-02-21 15:57:23 by las
KK: Of course they should. The swizzle operator returns just another vector. And considering the whole thing is implemented with the KISS ideology, swizzling a swizzled vector should function just fine. :)
added on the 2011-02-21 16:01:52 by decipher
Code:``` float sphere(float x, float y, float z, float r) { return sqrt(x * x + y * y + z * z) - r; } ```

Obvious GLSL equivalent (might by faster?)
Code:``` float sphere(vec3 p, float r) { return length(p) - r; } ```
added on the 2011-02-22 15:12:59 by las
Code:``` #define R(p,a) p=cos(a)*p+sin(a)*float2(-p.y,p.x); // hlsl ```

I'm usually using this shorter form for rotation
added on the 2011-02-22 15:42:25 by xoofx