node_vector_displacement.osl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_vector_displacement(color Vector = color(0.0, 0.0, 0.0),
  18. float Midlevel = 0.0,
  19. float Scale = 1.0,
  20. string space = "tangent",
  21. string attr_name = "geom:tangent",
  22. string attr_sign_name = "geom:tangent_sign",
  23. output vector Displacement = vector(0.0, 0.0, 0.0))
  24. {
  25. vector offset = (Vector - vector(Midlevel)) * Scale;
  26. if (space == "tangent") {
  27. /* Tangent space. */
  28. vector N_object = normalize(transform("world", "object", N));
  29. vector T_object;
  30. if (getattribute(attr_name, T_object)) {
  31. T_object = normalize(T_object);
  32. }
  33. else {
  34. T_object = normalize(dPdu);
  35. }
  36. vector B_object = normalize(cross(N_object, T_object));
  37. float tangent_sign;
  38. if (getattribute(attr_sign_name, tangent_sign)) {
  39. B_object *= tangent_sign;
  40. }
  41. Displacement = T_object * offset[0] + N_object * offset[1] + B_object * offset[2];
  42. }
  43. else {
  44. /* Object or world space. */
  45. Displacement = offset;
  46. }
  47. if (space != "world") {
  48. /* Tangent or object space. */
  49. Displacement = transform("object", "world", Displacement);
  50. }
  51. }