xplastic.sl 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. surface xplastic(
  2. float Ka = 1;
  3. float Kd = 1;
  4. float Ks = 0.5;
  5. float roughness = 0.1;
  6. color specularcolor = 1;
  7. string mapName = "";
  8. )
  9. {
  10. normal Ni;
  11. matrix mv, mp, mv_inv;
  12. float xs, xt;
  13. point P2;
  14. // Query the normal map texture
  15. textureinfo (mapName, "viewingmatrix", mv);
  16. mv_inv = 1 / mv;
  17. textureinfo (mapName, "projectionmatrix", mp);
  18. // Transform P from current space to NDC space
  19. P2 = transform (mp, P);
  20. xs = (1.0 + xcomp (P2)) * 0.5;
  21. xt = (1.0 - ycomp (P2)) * 0.5;
  22. // Extract the normal Nf from the normal map texture
  23. color cn = color texture (mapName, xs, xt, xs, xt, xs, xt, xs, xt,
  24. "samples", 1);
  25. setxcomp (Ni, comp (cn, 0));
  26. setycomp (Ni, comp (cn, 1));
  27. setzcomp (Ni, comp (cn, 2));
  28. normal Nf = ntransform (mv_inv, Ni);
  29. Nf = normalize (Nf);
  30. vector V = - normalize (I);
  31. Ci = Cs * (Ka * ambient () + Kd * diffuse (Nf)) +
  32. Ks * specularcolor * specular (Nf, V, roughness);
  33. Oi = Os;
  34. Ci *= Oi;
  35. }