/* cubes.sl */ #include "rmannotes.sl" surface cubes(float Ka = 0.2, Kd = 0.4, Ks = 0.6, roughness = 0.1, r = 0.5, freq = 6) { color surface_color=1, layer_color, layer_opac; normal Nf = faceforward(normalize(N), I); vector V = -normalize(I); point PP = transform("shader", P); /* transform P to shader space */ PP += vector snoise(PP*10) * 0.1; float x = xcomp(PP), y = ycomp(PP), z = zcomp(PP); float xx = repeat(x,freq); float yy = repeat(y,freq); float zz = repeat(z,freq); float col = whichtile(x,freq); float row = whichtile(y,freq); float pla = whichtile(z,freq); color c = color cellnoise(point(col,row,pla)); /* float d = max(abs(xx)-r, abs(yy)-r, abs(zz)-r); */ float d = (xx-0.5)*(xx-0.5)+(yy-0.5)*(yy-0.5)+(zz-0.5)*(zz-0.5) - r*r; if(d>0) { surface_color = color (1,0,0); } else { surface_color = c; } float ds = distance(point(0.5,0.5,0.5), point(xx, yy, zz)); float max_height = .1; float layer_mag = max_height * clamp(1-ds/r,0,1); float surface_mag = layer_mag; vector Nn = normalize(Nf); P += Nn * (surface_mag / length(vtransform("shader", Nn))); N = calculatenormal(P); Nf = faceforward (normalize(N),I); surface_color = surface_color * (Ka * ambient() + Kd * diffuse(Nf)) + Ks * specular(Nf, V, roughness); /* output */ Oi = Os; Ci = Os * surface_color; }