node_sky_texture.osl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. float sky_angle_between(float thetav, float phiv, float theta, float phi)
  19. {
  20. float cospsi = sin(thetav) * sin(theta) * cos(phi - phiv) + cos(thetav) * cos(theta);
  21. if (cospsi > 1.0)
  22. return 0.0;
  23. if (cospsi < -1.0)
  24. return M_PI;
  25. return acos(cospsi);
  26. }
  27. vector sky_spherical_coordinates(vector dir)
  28. {
  29. return vector(acos(dir[2]), atan2(dir[0], dir[1]), 0);
  30. }
  31. /* Preetham */
  32. float sky_perez_function(float lam[9], float theta, float gamma)
  33. {
  34. float ctheta = cos(theta);
  35. float cgamma = cos(gamma);
  36. return (1.0 + lam[0] * exp(lam[1] / ctheta)) *
  37. (1.0 + lam[2] * exp(lam[3] * gamma) + lam[4] * cgamma * cgamma);
  38. }
  39. color sky_radiance_old(normal dir,
  40. float sunphi,
  41. float suntheta,
  42. color radiance,
  43. float config_x[9],
  44. float config_y[9],
  45. float config_z[9])
  46. {
  47. /* convert vector to spherical coordinates */
  48. vector spherical = sky_spherical_coordinates(dir);
  49. float theta = spherical[0];
  50. float phi = spherical[1];
  51. /* angle between sun direction and dir */
  52. float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
  53. /* clamp theta to horizon */
  54. theta = min(theta, M_PI_2 - 0.001);
  55. /* compute xyY color space values */
  56. float x = radiance[1] * sky_perez_function(config_y, theta, gamma);
  57. float y = radiance[2] * sky_perez_function(config_z, theta, gamma);
  58. float Y = radiance[0] * sky_perez_function(config_x, theta, gamma);
  59. /* convert to RGB */
  60. color xyz = xyY_to_xyz(x, y, Y);
  61. return xyz_to_rgb(xyz[0], xyz[1], xyz[2]);
  62. }
  63. /* Hosek / Wilkie */
  64. float sky_radiance_internal(float config[9], float theta, float gamma)
  65. {
  66. float ctheta = cos(theta);
  67. float cgamma = cos(gamma);
  68. float expM = exp(config[4] * gamma);
  69. float rayM = cgamma * cgamma;
  70. float mieM = (1.0 + rayM) / pow((1.0 + config[8] * config[8] - 2.0 * config[8] * cgamma), 1.5);
  71. float zenith = sqrt(ctheta);
  72. return (1.0 + config[0] * exp(config[1] / (ctheta + 0.01))) *
  73. (config[2] + config[3] * expM + config[5] * rayM + config[6] * mieM + config[7] * zenith);
  74. }
  75. color sky_radiance_new(normal dir,
  76. float sunphi,
  77. float suntheta,
  78. color radiance,
  79. float config_x[9],
  80. float config_y[9],
  81. float config_z[9])
  82. {
  83. /* convert vector to spherical coordinates */
  84. vector spherical = sky_spherical_coordinates(dir);
  85. float theta = spherical[0];
  86. float phi = spherical[1];
  87. /* angle between sun direction and dir */
  88. float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
  89. /* clamp theta to horizon */
  90. theta = min(theta, M_PI_2 - 0.001);
  91. /* compute xyz color space values */
  92. float x = sky_radiance_internal(config_x, theta, gamma) * radiance[0];
  93. float y = sky_radiance_internal(config_y, theta, gamma) * radiance[1];
  94. float z = sky_radiance_internal(config_z, theta, gamma) * radiance[2];
  95. /* convert to RGB and adjust strength */
  96. return xyz_to_rgb(x, y, z) * (M_2PI / 683);
  97. }
  98. shader node_sky_texture(int use_mapping = 0,
  99. matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  100. vector Vector = P,
  101. string type = "hosek_wilkie",
  102. float theta = 0.0,
  103. float phi = 0.0,
  104. color radiance = color(0.0, 0.0, 0.0),
  105. float config_x[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  106. float config_y[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  107. float config_z[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  108. output color Color = color(0.0, 0.0, 0.0))
  109. {
  110. vector p = Vector;
  111. if (use_mapping)
  112. p = transform(mapping, p);
  113. if (type == "hosek_wilkie")
  114. Color = sky_radiance_new(p, phi, theta, radiance, config_x, config_y, config_z);
  115. else
  116. Color = sky_radiance_old(p, phi, theta, radiance, config_x, config_y, config_z);
  117. }