node_environment_texture.osl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "node_color.h"
  18. vector environment_texture_direction_to_equirectangular(vector dir)
  19. {
  20. float u = -atan2(dir[1], dir[0]) / (M_2PI) + 0.5;
  21. float v = atan2(dir[2], hypot(dir[0], dir[1])) / M_PI + 0.5;
  22. return vector(u, v, 0.0);
  23. }
  24. vector environment_texture_direction_to_mirrorball(vector idir)
  25. {
  26. vector dir = idir;
  27. dir[1] -= 1.0;
  28. float div = 2.0 * sqrt(max(-0.5 * dir[1], 0.0));
  29. if (div > 0.0)
  30. dir /= div;
  31. float u = 0.5 * (dir[0] + 1.0);
  32. float v = 0.5 * (dir[2] + 1.0);
  33. return vector(u, v, 0.0);
  34. }
  35. shader node_environment_texture(
  36. int use_mapping = 0,
  37. matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  38. vector Vector = P,
  39. string filename = "",
  40. string projection = "equirectangular",
  41. string interpolation = "linear",
  42. int compress_as_srgb = 0,
  43. int ignore_alpha = 0,
  44. int unassociate_alpha = 0,
  45. int is_float = 1,
  46. output color Color = 0.0,
  47. output float Alpha = 1.0)
  48. {
  49. vector p = Vector;
  50. if (use_mapping)
  51. p = transform(mapping, p);
  52. p = normalize(p);
  53. if (projection == "equirectangular")
  54. p = environment_texture_direction_to_equirectangular(p);
  55. else
  56. p = environment_texture_direction_to_mirrorball(p);
  57. /* todo: use environment for better texture filtering of equirectangular */
  58. Color = (color)texture(
  59. filename, p[0], 1.0 - p[1], "wrap", "periodic", "interp", interpolation, "alpha", Alpha);
  60. if (ignore_alpha) {
  61. Alpha = 1.0;
  62. }
  63. else if (unassociate_alpha) {
  64. Color = color_unpremultiply(Color, Alpha);
  65. if (!is_float)
  66. Color = min(Color, 1.0);
  67. }
  68. if (compress_as_srgb)
  69. Color = color_srgb_to_scene_linear(Color);
  70. }