surface xplastic( float Ka = 1; float Kd = 1; float Ks = 0.5; float roughness = 0.1; color specularcolor = 1; string mapName = ""; ) { normal Ni; matrix mv, mp, mv_inv; float xs, xt; point P2; // Query the normal map texture textureinfo (mapName, "viewingmatrix", mv); mv_inv = 1 / mv; textureinfo (mapName, "projectionmatrix", mp); // Transform P from current space to NDC space P2 = transform (mp, P); xs = (1.0 + xcomp (P2)) * 0.5; xt = (1.0 - ycomp (P2)) * 0.5; // Extract the normal Nf from the normal map texture color cn = color texture (mapName, xs, xt, xs, xt, xs, xt, xs, xt, "samples", 1); setxcomp (Ni, comp (cn, 0)); setycomp (Ni, comp (cn, 1)); setzcomp (Ni, comp (cn, 2)); normal Nf = ntransform (mv_inv, Ni); Nf = normalize (Nf); vector V = - normalize (I); Ci = Cs * (Ka * ambient () + Kd * diffuse (Nf)) + Ks * specularcolor * specular (Nf, V, roughness); Oi = Os; Ci *= Oi; }