node_bump.osl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "stdosl.h"
  17. /* "Bump Mapping Unparameterized Surfaces on the GPU"
  18. * Morten S. Mikkelsen, 2010 */
  19. surface node_bump(int invert = 0,
  20. int use_object_space = 0,
  21. normal NormalIn = N,
  22. float Strength = 0.1,
  23. float Distance = 1.0,
  24. float SampleCenter = 0.0,
  25. float SampleX = 0.0,
  26. float SampleY = 0.0,
  27. output normal NormalOut = N)
  28. {
  29. point Ptmp = P;
  30. normal Normal = NormalIn;
  31. if (use_object_space) {
  32. Ptmp = transform("object", Ptmp);
  33. Normal = normalize(transform("object", Normal));
  34. }
  35. /* get surface tangents from normal */
  36. vector dPdx = Dx(Ptmp);
  37. vector dPdy = Dy(Ptmp);
  38. vector Rx = cross(dPdy, Normal);
  39. vector Ry = cross(Normal, dPdx);
  40. /* compute surface gradient and determinant */
  41. float det = dot(dPdx, Rx);
  42. vector surfgrad = (SampleX - SampleCenter) * Rx + (SampleY - SampleCenter) * Ry;
  43. float absdet = fabs(det);
  44. float strength = max(Strength, 0.0);
  45. float dist = Distance;
  46. if (invert)
  47. dist *= -1.0;
  48. /* compute and output perturbed normal */
  49. NormalOut = normalize(absdet * Normal - dist * sign(det) * surfgrad);
  50. NormalOut = normalize(strength * NormalOut + (1.0 - strength) * Normal);
  51. if (use_object_space) {
  52. NormalOut = normalize(transform("object", "world", NormalOut));
  53. }
  54. NormalOut = ensure_valid_reflection(Ng, I, NormalOut);
  55. }