/* * backlit_triangles.sl * * Lawson Wade * * A surface shader that simulates a translucent * plastic surface by allowing itself to be lit * from behind. The surface has backgammon like * triangles of alternating colors. */ #include "rmannotes.sl" #include "colors.sl" surface backlit_triangles( color triangle_color1 = Red3, triangle_color2 = SlateBlue4; float numtriangles = 16, baseline = 0; float Ka = 1, Kd = 0.5, Ks = 0.5, roughness = 0.1; color specularcolor = 1; ) { point Nf, V; point Nin, Vmirrored; color surface_color, layer_color; color surface_opac; float count; float layer_opac; float ss, tt; float col; /* resolve triangle layer */ ss = repeat(s, numtriangles); tt = t; col = whichtile(s, numtriangles); layer_color = Cs; if ((1 - baseline) * abs(2 * ss - 1) < tt) { if (even(col)) layer_color = triangle_color1; else layer_color = triangle_color2; } /* illumination vectors */ Nf = faceforward(normalize(N), I); V = normalize(-I); /*** background layer ***/ surface_opac = Os; surface_color = 0; /*** ambient light ***/ surface_color += Os * layer_color * Ka * ambient(); /*** diffuse light from front ***/ surface_color += Os * layer_color * Kd * diffuse(Nf); /*** diffuse light from behind ***/ layer_color = 0; Nin = -Nf; illuminance(P, Nin, 0.5*PI) layer_color += Cl * (normalize(L) . Nin); surface_color += Os * layer_color; /*** specular light from front ***/ surface_color += specularcolor * Ks * specular(Nf, V, roughness); /*** specular light from behind ***/ Vmirrored = faceforward(reflect(V, Nf), Nin); surface_color += specularcolor * Ks * specular(Nin, Vmirrored, 1.0); /* output */ Oi = surface_opac; Ci = surface_color; }