surface --- displacement
[BACK]

surface
solid( float Ka = 0.2, Kd = 0.4, Ks = 0.6, roughness = 0.1;
color specularcolor = 1;
float Freq = 2, noiz = 0.2, Iter = 3)

#define udn(x,lo,hi) (smoothstep(.25, .75, noise(x)) * ((hi) - (lo)) + (lo))
#define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - \
smoothstep((b)-(fuzz),(b),(x)))
{
normal Nf;
vector V;
color Csnew;
color Cfg = (1,0,0);
color Cbg = (0,0,1);
point PP;
float x, y, z, xx, yy, zz;
float value, n;
float noiz2 = 0;

/* transform P to shader space, add iterations of noise to it
* and get seperate components*/

PP = transform("shader", P);

for (n=0; n < Iter; n += 1){
PP += vector (udn (PP*Freq*(n*3+1), 0.0, noiz/((n+1)*4)));
}

x = xcomp(PP);
y = ycomp(PP);
z = zcomp(PP);

/* Tile 3D space */

xx = mod(x*Freq,1.0);
yy = mod(y*Freq,1.0);
zz = mod(z*Freq,1.0);

/* create 3D pattern of crossing lines */

value = pulse (0.5, 0.6, 0.1, xx);
value += pulse (0.5, 0.6, 0.1, yy);
value += pulse (0.5, 0.6, 0.1, zz);
value = clamp (value, 0.0, 1.0);

/* add noise to background color & compute new surface color */

for (n=0; n < Iter; n += 1){
noiz2 += udn (PP*Freq*((n+1)*(n+1)), 0.0, 1.0) / (n+1);
}

noiz2 = smoothstep (0.5, 1.0, noiz2);
Cbg = color (noiz2 ,noiz2 ,1);

Csnew = mix (Cbg,Cfg,value);

/* calculate lighting etc, making the line pattern more shiny
* and space inbetween lines a bit transparant */

Nf = faceforward( normalize(N), I );
V = -normalize(I);

Oi = Os * smoothstep(0.0,0.7,(value/2+0.3));
Ci = Cs * Os * ( Csnew * (Ka*ambient() + Kd*diffuse(Nf)) +
specularcolor * Ks*(2*value+1) * specular(Nf,V,roughness) );
}

displacement
soldisp(float Km = 0.1, Freq = 2, noiz = 0.2, Iter = 3)

#define udn(x,lo,hi) (smoothstep(.25, .75, noise(x)) * ((hi) - (lo)) + (lo))
#define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - \
smoothstep((b)-(fuzz),(b),(x)))
{
point PP;
float x, y, z, xx, yy, zz;
float value, n;

/* transform P to shader space, add iterations of noise to it
* and get seperate components*/

PP = transform("shader", P);

for (n=0; n < Iter; n += 1){
PP += vector (udn (PP*Freq*(n*3+1), 0.0, noiz/((n+1)*4)));
}

x = xcomp(PP);
y = ycomp(PP);
z = zcomp(PP);

/* Tile 3D space */

xx = mod(x*Freq,1.0);
yy = mod(y*Freq,1.0);
zz = mod(z*Freq,1.0);

/* create 3D pattern of crossing lines */

value = pulse (0.5, 0.6, 0.1, xx);
value += pulse (0.5, 0.6, 0.1, yy);
value += pulse (0.5, 0.6, 0.1, zz);
value = clamp ((value-0.5), 0.0, 0.2);

/* calculate displacement */

P += Km * value * normalize (N);
N = calculatenormal(P);
}

[BACK]