/*Solid surface shader: luckystone * Final Assignment: shader with turbulence, solid texture. * Tom Suchan * This shader simulates a marble like stone with colored veins.*/ #include "rmannotes.sl" surface luckystone(float Ka = 0.2, Kd = 0.4, Ks = 0.6, roughness = 0.1) { color paper, ink; float paper_opac, ink_opac; float fuzz = 0.05; point Nf, V; point PP; float x, y, z; float noifreq = 5; float noiscale = 0.8; float noi, noi2; float pixelsize, twice; float scale, turb = 0; float turbfreq = 2; float H, L; float col; /* layer one: create turbulance, paper */ PP = transform("shader", P) * turbfreq; pixelsize = sqrt(area(PP)); twice = pixelsize * 2; for(scale = 1; scale > twice; scale /= 2) turb += abs(noise(PP/scale) - 0.5) * scale; turb = turb * 2; paper = spline(turb, color(1,0,0), color(1,0,0), color(1,1,1), color(0,0,1), color(0,0,1)); paper_opac = 1; Nf = faceforward(normalize(N), I); V = -normalize(I); /* transform P to shader space*/ PP = transform("shader", P); PP += noise(PP) * 0.5; x = xcomp(P); y = ycomp(P); z = zcomp(P); noi = noise(y * noifreq, x * noifreq); /* perturb y, set noise*/ rotate2d(y, x, radians(45), .5, .5, y, x); col = whichtile(y, 3); y = repeat(y, 3); y = y + snoise(noi + 3752) * noiscale; H = udn(noi * 4319, .2, .4); L = udn(noi * 597, .4, .7); noi2 = noise(col * noifreq); /* veins, generate %, choose color */ if(udn(noi2 * 452, 0, 1) > 0.3){ if(even(col)){ ink = spline(turb, color(1,1,1), color(1,1,1), color(1,1,1), color(0,0,0), color(0,0,0)); ink_opac = pulse(H, L, fuzz, y); } if(odd(col)){ ink_opac = pulse(L, H, fuzz, y); ink = spline(turb, color(0,0,0), color(1,0,0), color(1,0,0), color(0,0,0), color(0,0,0)); } /* else{ ink_opac = pulse(L, H, fuzz, y); ink = spline(turb, color(0,0,0), color(0,0,0), color(0,0,0), color(0,0,1), color(0,0,1)); }*/ paper = mix(paper, ink, ink_opac); } /* plastic illum */ paper = paper * Ka * ambient() + paper * Kd * diffuse(Nf) + Ks * specular(Nf, V, roughness); /* output */ Oi = paper_opac; Ci = paper_opac * paper; }