node_normal_map.osl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. shader node_normal_map(normal NormalIn = N,
  18. float Strength = 1.0,
  19. color Color = color(0.5, 0.5, 1.0),
  20. string space = "tangent",
  21. string attr_name = "geom:tangent",
  22. string attr_sign_name = "geom:tangent_sign",
  23. output normal Normal = NormalIn)
  24. {
  25. color mcolor = 2.0 * color(Color[0] - 0.5, Color[1] - 0.5, Color[2] - 0.5);
  26. int is_backfacing = backfacing();
  27. if (space == "tangent") {
  28. vector tangent;
  29. vector ninterp;
  30. float tangent_sign;
  31. float is_smooth;
  32. getattribute("geom:is_smooth", is_smooth);
  33. if (!is_smooth) {
  34. ninterp = normalize(transform("world", "object", Ng));
  35. /* the normal is already inverted, which is too soon for the math here */
  36. if (is_backfacing) {
  37. ninterp = -ninterp;
  38. }
  39. }
  40. // get _unnormalized_ interpolated normal and tangent
  41. if (getattribute(attr_name, tangent) && getattribute(attr_sign_name, tangent_sign) &&
  42. (!is_smooth || getattribute("geom:N", ninterp))) {
  43. // apply normal map
  44. vector B = tangent_sign * cross(ninterp, tangent);
  45. Normal = normalize(mcolor[0] * tangent + mcolor[1] * B + mcolor[2] * ninterp);
  46. // transform to world space
  47. Normal = normalize(transform("object", "world", Normal));
  48. }
  49. else {
  50. Normal = normal(0, 0, 0);
  51. }
  52. }
  53. else if (space == "object") {
  54. Normal = normalize(transform("object", "world", vector(mcolor)));
  55. }
  56. else if (space == "world") {
  57. Normal = normalize(vector(mcolor));
  58. }
  59. else if (space == "blender_object") {
  60. /* strange blender convention */
  61. mcolor[1] = -mcolor[1];
  62. mcolor[2] = -mcolor[2];
  63. Normal = normalize(transform("object", "world", vector(mcolor)));
  64. }
  65. else if (space == "blender_world") {
  66. /* strange blender convention */
  67. mcolor[1] = -mcolor[1];
  68. mcolor[2] = -mcolor[2];
  69. Normal = normalize(vector(mcolor));
  70. }
  71. /* invert normal for backfacing polygons */
  72. if (is_backfacing) {
  73. Normal = -Normal;
  74. }
  75. if (Strength != 1.0)
  76. Normal = normalize(NormalIn + (Normal - NormalIn) * max(Strength, 0.0));
  77. Normal = ensure_valid_reflection(Ng, I, Normal);
  78. }