node_principled_hair_bsdf.osl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2018 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. color log3(color a)
  18. {
  19. return color(log(a[0]), log(a[1]), log(a[2]));
  20. }
  21. color sigma_from_concentration(float eumelanin, float pheomelanin)
  22. {
  23. return eumelanin * color(0.506, 0.841, 1.653) + pheomelanin * color(0.343, 0.733, 1.924);
  24. }
  25. color sigma_from_reflectance(color c, float azimuthal_roughness)
  26. {
  27. float x = azimuthal_roughness;
  28. float roughness_fac = (((((0.245 * x) + 5.574) * x - 10.73) * x + 2.532) * x - 0.215) * x +
  29. 5.969;
  30. color sigma = log3(c) / roughness_fac;
  31. return sigma * sigma;
  32. }
  33. shader node_principled_hair_bsdf(color Color = color(0.017513, 0.005763, 0.002059),
  34. float Melanin = 0.8,
  35. float MelaninRedness = 1.0,
  36. float RandomColor = 0.0,
  37. color Tint = 1.0,
  38. color AbsorptionCoefficient = color(0.245531, 0.52, 1.365),
  39. normal Normal = Ng,
  40. string parametrization = "Absorption coefficient",
  41. float Offset = radians(2),
  42. float Roughness = 0.3,
  43. float RadialRoughness = 0.3,
  44. float RandomRoughness = 0.0,
  45. float Coat = 0.0,
  46. float IOR = 1.55,
  47. string AttrRandom = "geom:curve_random",
  48. float Random = 0.0,
  49. output closure color BSDF = 0)
  50. {
  51. /* Get random value from curve in none is specified. */
  52. float random_value = 0.0;
  53. if (isconnected(Random)) {
  54. random_value = Random;
  55. }
  56. else {
  57. getattribute(AttrRandom, random_value);
  58. }
  59. /* Compute roughness. */
  60. float factor_random_roughness = 1.0 + 2.0 * (random_value - 0.5) * RandomRoughness;
  61. float m0_roughness = 1.0 - clamp(Coat, 0.0, 1.0);
  62. float roughness = Roughness * factor_random_roughness;
  63. float radial_roughness = RadialRoughness * factor_random_roughness;
  64. /* Compute absorption. */
  65. color sigma;
  66. if (parametrization == "Absorption coefficient") {
  67. sigma = AbsorptionCoefficient;
  68. }
  69. else if (parametrization == "Melanin concentration") {
  70. /* Randomize melanin. */
  71. float factor_random_color = 1.0 + 2.0 * (random_value - 0.5) * RandomColor;
  72. float melanin = Melanin * factor_random_color;
  73. /* Map melanin 0..inf from more perceptually linear 0..1. */
  74. melanin = -log(max(1.0 - melanin, 0.0001));
  75. /* Benedikt Bitterli's melanin ratio remapping. */
  76. float eumelanin = melanin * (1.0 - MelaninRedness);
  77. float pheomelanin = melanin * MelaninRedness;
  78. color melanin_sigma = sigma_from_concentration(eumelanin, pheomelanin);
  79. /* Optional tint. */
  80. color tint_sigma = sigma_from_reflectance(Tint, radial_roughness);
  81. sigma = melanin_sigma + tint_sigma;
  82. }
  83. else if (parametrization == "Direct coloring") {
  84. sigma = sigma_from_reflectance(Color, radial_roughness);
  85. }
  86. else {
  87. /* Fallback to brownish hair, same as defaults for melanin. */
  88. sigma = sigma_from_concentration(0.0, 0.8054375);
  89. }
  90. BSDF = principled_hair(Normal, sigma, roughness, radial_roughness, m0_roughness, Offset, IOR);
  91. }