|
Message Passing
get
displacement file
get
surface file
Snow Cropped Mountains
In a surface shader we can grab values output from a displacement
shader and have those values effect how we shade the surface of
our object.
|
Using the height of the displacement we effect the change
of the color over the surface. This is not a realstic placement
of snow, because snow would also fall in the valleys of the
surface. |
|
Using the dot product of the undisplaced normal
and the displaced normal we can get the cosine of the two
vectors when normalized. Using this value in the surface shader
we can effect where we change the surface color.
RIB file Settings:
Surface "messagein" "Kd" .7
"snowcap" 1
"smoothness" .06
"groundcolor" [0.376 0.313 0.196]
"snowcolor" [1 1 1]
"height" 2
Displacement
"messageout"
"Km" -.35
"freq" 2
"detail" 8
"amp" 1.3
Attribute "bound" "displacement" [.4]
|
Code for Surface Shader
/* Surface shader that uses output from a displacementfile to gauge where to change surface color. Using a mountain displacent map we can
create snow covered mountains*/
surface
messagein(float Kd = 1,
snowcap = 0,
smoothness = .02,
height = 0;
color snowcolor = 1,
groundcolor = color(.4, .8, .4))
{
vector n = normalize(N);
vector nf = faceforward(n, I);
color diffusecolor, surfcolor = 1;
float angle = 0;
Oi = Os;
/*if displacement*/
if(displacement("dp", angle) == 1)
{
/*taking the value that comes out of the do t product into the color value,
so basically add color according t o the rate of change of the hump*/
float blend = smoothstep(snowcap - smoothnes s, snowcap + smoothness, angle);
surfcolor = mix(groundcolor, snowcolo r, blend);
}
diffusecolor = Kd * diffuse(nf);
Ci = Oi * Cs * surfcolor * diffusecolor;
}
Code for Displacement Shader
/* Creates a mountain and outputs the dot product of the undisplaced normal and the
displaced normal for use in a surface shader */
displacement
messageout(float Km = 0.1,
freq = 4,
detail = 3,
amp = 1;
output varying float hump = 0;
output varying vector surfaceP = 0;
output varying vector n = 0;
output varying float dp = 0; )
{
n = normalize(N);
vector n1 = normalize(N);
/*Generates variable height with adjustable detail*/
float turbulence(point p; float detail, freq, amp)
{
float i, f = freq, a = amp, turb = 0;
for(i = 0; i < detail; i = i + 1)
{
turb = turb + noise(p * f) * a;
f = f * 2;
a = a / 2;
}
return turb;
}
/*Apply the turbulence to the height of the surface for displacement*/
hump += turbulence(P,detail,freq, amp);
hump -= .5;
P = P - n * hump * Km;
surfaceP = P;
N = calculatenormal(P);
n = normalize(N);
/*Calculate the dot product of the undisplaced normal
and the displaced normal*/
dp = n.n1;
}
|