Raymarching Beginners' Thread
category: code [glöplog]
A simple cloudy spikeball with a technique adapted from my code is pron.
Code:
uniform float t;
uniform vec2 wh;
#define pi 3.14159265
#define R(p, a) p=cos(a)*p+sin(a)*vec2(p.y, -p.x)
#define hsv(h,s,v) mix(vec3(1.), clamp((abs(fract(h+vec3(3., 2., 1.)/3.)*6.-3.)-1.), 0., 1.), s)*v
float pn(vec3 p) {
vec3 i = floor(p);
vec4 a = dot(i, vec3(1., 57., 21.)) + vec4(0., 57., 21., 78.);
vec3 f = cos((p-i)*pi)*(-.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);
}
float fpn(vec3 p) {
return pn(p*.06125)*.5 + pn(p*.125)*.25 + pn(p*.25)*.125;
}
vec3 n1 = vec3(1.000,0.000,0.000);
vec3 n2 = vec3(0.000,1.000,0.000);
vec3 n3 = vec3(0.000,0.000,1.000);
vec3 n4 = vec3(0.577,0.577,0.577);
vec3 n5 = vec3(-0.577,0.577,0.577);
vec3 n6 = vec3(0.577,-0.577,0.577);
vec3 n7 = vec3(0.577,0.577,-0.577);
vec3 n8 = vec3(0.000,0.357,0.934);
vec3 n9 = vec3(0.000,-0.357,0.934);
vec3 n10 = vec3(0.934,0.000,0.357);
vec3 n11 = vec3(-0.934,0.000,0.357);
vec3 n12 = vec3(0.357,0.934,0.000);
vec3 n13 = vec3(-0.357,0.934,0.000);
vec3 n14 = vec3(0.000,0.851,0.526);
vec3 n15 = vec3(0.000,-0.851,0.526);
vec3 n16 = vec3(0.526,0.000,0.851);
vec3 n17 = vec3(-0.526,0.000,0.851);
vec3 n18 = vec3(0.851,0.526,0.000);
vec3 n19 = vec3(-0.851,0.526,0.000);
float spikeball(vec3 p) {
vec3 q=p;
p = normalize(p);
vec4 b = max(max(max(
abs(vec4(dot(p,n16), dot(p,n17),dot(p, n18), dot(p,n19))),
abs(vec4(dot(p,n12), dot(p,n13), dot(p, n14), dot(p,n15)))),
abs(vec4(dot(p,n8), dot(p,n9), dot(p, n10), dot(p,n11)))),
abs(vec4(dot(p,n4), dot(p,n5), dot(p, n6), dot(p,n7))));
b.xy = max(b.xy, b.zw);
b.x = pow(max(b.x, b.y), 140.);
return length(q)-2.5*pow(1.5,b.x*(1.-mix(.3, 1., sin(t*2.)*.5+.5)*b.x));
}
float f(vec3 p) {
p.z += 6.;
R(p.xy, t);
R(p.xz, t);
return spikeball(p) + fpn(p*50.+t*15.) * 0.45;
}
vec3 g(vec3 p) {
vec2 e = vec2(.0001, .0);
return normalize(vec3(f(p+e.xyy) - f(p-e.xyy),f(p+e.yxy) - f(p-e.yxy),f(p+e.yyx) - f(p-e.yyx)));
}
float as(vec3 p, vec3 n, float d, float i) {
float s = sign(d);
float o = s*.5+.5;
for (; i > 0.; --i) {
o -= (i*d - f(p+n*i*d*s)) / exp2(i);
}
return o;
}
void main(void)
{
// p: position on the ray
// d: direction of the ray
vec3 p = vec3(0.,0.,2.);
vec3 d = vec3((gl_FragCoord.xy/(0.5*wh)-1.)*vec2(wh.x/wh.y,1.0), 0.) - p;
d = normalize(d);
// ld, td: local, total density
// w: weighting factor
float ld, td= 0.;
float w;
// total color
vec3 tc = vec3(0.);
// i: 0 <= i <= 1.
// r: length of the ray
// l: distance function
float i, r, l, b=0.;
// rm loop
for (i=r=l=0.; i<1. && l>=0.001*r && r < 50. && td < .95; i+=1./64.) {
// evaluate distance function
l = f(p) * 0.5;
// check whether we are close enough
if (l < .05) {
// compute local density and weighting factor
ld = 0.05 - l;
w = (1. - td) * ld;
// accumulate color and density
tc += w; //* hsv(w, 1., 1.);
td += w;
}
td += 1./200.;
// enforce minimum stepsize
l = max(l, 0.03);
// step forward
p += l*d;
r += l;
}
gl_FragColor = vec4(tc, 1.0);
}
That's just awesome! Looks like cotton candy!
For my gf :)
But it seems that the distance function for the heart is pretty bad. Is it even an SDF?
(ignore the banding)
nice dustball las. could need some more crisp details and some more transparent fiber like texture. wish I could do it myself but my rig won't compile rm shaders. should do it in classic fake rendering? tsh ... can't focus on coding today anyway. -.-
what do you mean by a fiber like texture?
(credit to chock for the technique of course)
I now implemented it in a completely branchless manner.
...
branchless:
(credit to chock for the technique of course)
I now implemented it in a completely branchless manner.
...
Code:
// check whether we are close enough
if (l < .05) {
// compute local density and weighting factor
ld = 0.05 - l;
w = (1. - td) * ld;
// accumulate color and density
tc += w; //* hsv(w, 1., 1.);
td += w;
}
branchless:
Code:
// check whether we are close enough (step)
// compute local density and weighting factor
const float h = .05;
ld = (h - l) * step(l, h);
w = (1. - td) * ld;
// accumulate color and density
tc += w;// * hsv(w*3.-0.5, 1.-w*20., 1.);
td += w;
That cottonwool-spikeball looks good! I'll have to play with that code when i get some time!
Quote:
what do you mean by a fiber like texture?
Probably another noise octave.
I shouldn't talk big but: You don't know or can't translate what a fiber is? My dictionary said it's right. If this is meant to be cottonwool you should know what I mean with that kinda details. and WTF... add more noize. -.- random lines maybe.
Anyway... I'll do some code tommorow when I'm fit again and see what I can do. I even wanna see the old jelly spikeballs with sugar. I'm gonna murder that RM stuff.
Anyway... I'll do some code tommorow when I'm fit again and see what I can do. I even wanna see the old jelly spikeballs with sugar. I'm gonna murder that RM stuff.
My breakfast lacks fiber.
So you want it to look like fucking "Zuckerwatte"?
It would work las. so "zuckerwatte" or some other wool. that thing looks something like the ambient only portion of it. just not that detailed. I pictured a lil more in there. somewhat fibers, specular flakes and more contrast to it and it's cool.
can't help it. I like decent spec(tac)ular shading. ;)
can't help it. I like decent spec(tac)ular shading. ;)
It doesn't look like cotton wool because it's supposed to look like cloud ;)
Las: would your SSS work with this? I think clouds look darker at the bottom, because there's more cloud between the surface + the light source there. Not sure if it'll work, but if it's lit from the top, with SSS that bends towards the light source from the surface, you might get good looking clouds
Las: would your SSS work with this? I think clouds look darker at the bottom, because there's more cloud between the surface + the light source there. Not sure if it'll work, but if it's lit from the top, with SSS that bends towards the light source from the surface, you might get good looking clouds
That is awkward. It's supposed to be like a cloud in the sky? Ok. Well then the background and ambient is the wrong color to generate a feel like looking into the sky.
I can adapt and play. but... -.-
I can adapt and play. but... -.-
psonice sss isn't that good looking with the technique... but i did some other strange experiments.
0.0
Noice.
Noice.
that last one is awesome.. looks at bit like cast iron..
las: the middle one looks killer. The last one looks too over-the-top, ruining the subtlety of the effect.
i'm sceptical -- the stills look great indeed, but how does that texture behave with respect to changes in view position? (I.e. post a video where the thing is rotating!)
hyde: not tried it, but.. it's not a screenspace dependent effect, so it should rotate nicely. It's actual surface noise, so I guess the only problem with rotation will be if the details are so small they're aliasing as it rotates, and I don't think that will happen here (especially if it's animated :)
Las: I'm with gloom. The middle one is killer. Animate it, make lightning ripple through it, win compo.
Las: I'm with gloom. The middle one is killer. Animate it, make lightning ripple through it, win compo.
Quote:
Las: I'm with gloom. The middle one is killer. Animate it, make lightning ripple through it, win compo.
Don't forget the killer fecal blur overlay.
I like the middle one too ;)
I wont take a video - but I've got something else:
(You have to trust me - that's a nice twister!)
I wont take a video - but I've got something else:
(You have to trust me - that's a nice twister!)
that looks kinda cool now las. tons of "details" probably made of noize.
Yep, that looks hot (but I hope you're keeping your best stuff secret as a surprise in your next demo ;)
Actually I shared all the current stuff, it's still far away from what I want.
From now on I'll stop posting screenshots here, it's your turn now! :)
From now on I'll stop posting screenshots here, it's your turn now! :)