bsdf_toon.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Adapted from Open Shading Language with this license:
  3. *
  4. * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
  5. * All Rights Reserved.
  6. *
  7. * Modifications Copyright 2011, Blender Foundation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * * Neither the name of Sony Pictures Imageworks nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __BSDF_TOON_H__
  33. #define __BSDF_TOON_H__
  34. CCL_NAMESPACE_BEGIN
  35. typedef ccl_addr_space struct ToonBsdf {
  36. SHADER_CLOSURE_BASE;
  37. float size;
  38. float smooth;
  39. } ToonBsdf;
  40. static_assert(sizeof(ShaderClosure) >= sizeof(ToonBsdf), "ToonBsdf is too large!");
  41. /* DIFFUSE TOON */
  42. ccl_device int bsdf_diffuse_toon_setup(ToonBsdf *bsdf)
  43. {
  44. bsdf->type = CLOSURE_BSDF_DIFFUSE_TOON_ID;
  45. bsdf->size = saturate(bsdf->size);
  46. bsdf->smooth = saturate(bsdf->smooth);
  47. return SD_BSDF | SD_BSDF_HAS_EVAL;
  48. }
  49. ccl_device bool bsdf_toon_merge(const ShaderClosure *a, const ShaderClosure *b)
  50. {
  51. const ToonBsdf *bsdf_a = (const ToonBsdf *)a;
  52. const ToonBsdf *bsdf_b = (const ToonBsdf *)b;
  53. return (isequal_float3(bsdf_a->N, bsdf_b->N)) && (bsdf_a->size == bsdf_b->size) &&
  54. (bsdf_a->smooth == bsdf_b->smooth);
  55. }
  56. ccl_device float3 bsdf_toon_get_intensity(float max_angle, float smooth, float angle)
  57. {
  58. float is;
  59. if (angle < max_angle)
  60. is = 1.0f;
  61. else if (angle < (max_angle + smooth) && smooth != 0.0f)
  62. is = (1.0f - (angle - max_angle) / smooth);
  63. else
  64. is = 0.0f;
  65. return make_float3(is, is, is);
  66. }
  67. ccl_device float bsdf_toon_get_sample_angle(float max_angle, float smooth)
  68. {
  69. return fminf(max_angle + smooth, M_PI_2_F);
  70. }
  71. ccl_device float3 bsdf_diffuse_toon_eval_reflect(const ShaderClosure *sc,
  72. const float3 I,
  73. const float3 omega_in,
  74. float *pdf)
  75. {
  76. const ToonBsdf *bsdf = (const ToonBsdf *)sc;
  77. float max_angle = bsdf->size * M_PI_2_F;
  78. float smooth = bsdf->smooth * M_PI_2_F;
  79. float angle = safe_acosf(fmaxf(dot(bsdf->N, omega_in), 0.0f));
  80. float3 eval = bsdf_toon_get_intensity(max_angle, smooth, angle);
  81. if (eval.x > 0.0f) {
  82. float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
  83. *pdf = 0.5f * M_1_PI_F / (1.0f - cosf(sample_angle));
  84. return *pdf * eval;
  85. }
  86. return make_float3(0.0f, 0.0f, 0.0f);
  87. }
  88. ccl_device float3 bsdf_diffuse_toon_eval_transmit(const ShaderClosure *sc,
  89. const float3 I,
  90. const float3 omega_in,
  91. float *pdf)
  92. {
  93. return make_float3(0.0f, 0.0f, 0.0f);
  94. }
  95. ccl_device int bsdf_diffuse_toon_sample(const ShaderClosure *sc,
  96. float3 Ng,
  97. float3 I,
  98. float3 dIdx,
  99. float3 dIdy,
  100. float randu,
  101. float randv,
  102. float3 *eval,
  103. float3 *omega_in,
  104. float3 *domega_in_dx,
  105. float3 *domega_in_dy,
  106. float *pdf)
  107. {
  108. const ToonBsdf *bsdf = (const ToonBsdf *)sc;
  109. float max_angle = bsdf->size * M_PI_2_F;
  110. float smooth = bsdf->smooth * M_PI_2_F;
  111. float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
  112. float angle = sample_angle * randu;
  113. if (sample_angle > 0.0f) {
  114. sample_uniform_cone(bsdf->N, sample_angle, randu, randv, omega_in, pdf);
  115. if (dot(Ng, *omega_in) > 0.0f) {
  116. *eval = *pdf * bsdf_toon_get_intensity(max_angle, smooth, angle);
  117. #ifdef __RAY_DIFFERENTIALS__
  118. // TODO: find a better approximation for the bounce
  119. *domega_in_dx = (2.0f * dot(bsdf->N, dIdx)) * bsdf->N - dIdx;
  120. *domega_in_dy = (2.0f * dot(bsdf->N, dIdy)) * bsdf->N - dIdy;
  121. #endif
  122. }
  123. else
  124. *pdf = 0.0f;
  125. }
  126. return LABEL_REFLECT | LABEL_DIFFUSE;
  127. }
  128. /* GLOSSY TOON */
  129. ccl_device int bsdf_glossy_toon_setup(ToonBsdf *bsdf)
  130. {
  131. bsdf->type = CLOSURE_BSDF_GLOSSY_TOON_ID;
  132. bsdf->size = saturate(bsdf->size);
  133. bsdf->smooth = saturate(bsdf->smooth);
  134. return SD_BSDF | SD_BSDF_HAS_EVAL;
  135. }
  136. ccl_device float3 bsdf_glossy_toon_eval_reflect(const ShaderClosure *sc,
  137. const float3 I,
  138. const float3 omega_in,
  139. float *pdf)
  140. {
  141. const ToonBsdf *bsdf = (const ToonBsdf *)sc;
  142. float max_angle = bsdf->size * M_PI_2_F;
  143. float smooth = bsdf->smooth * M_PI_2_F;
  144. float cosNI = dot(bsdf->N, omega_in);
  145. float cosNO = dot(bsdf->N, I);
  146. if (cosNI > 0 && cosNO > 0) {
  147. /* reflect the view vector */
  148. float3 R = (2 * cosNO) * bsdf->N - I;
  149. float cosRI = dot(R, omega_in);
  150. float angle = safe_acosf(fmaxf(cosRI, 0.0f));
  151. float3 eval = bsdf_toon_get_intensity(max_angle, smooth, angle);
  152. float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
  153. *pdf = 0.5f * M_1_PI_F / (1.0f - cosf(sample_angle));
  154. return *pdf * eval;
  155. }
  156. return make_float3(0.0f, 0.0f, 0.0f);
  157. }
  158. ccl_device float3 bsdf_glossy_toon_eval_transmit(const ShaderClosure *sc,
  159. const float3 I,
  160. const float3 omega_in,
  161. float *pdf)
  162. {
  163. return make_float3(0.0f, 0.0f, 0.0f);
  164. }
  165. ccl_device int bsdf_glossy_toon_sample(const ShaderClosure *sc,
  166. float3 Ng,
  167. float3 I,
  168. float3 dIdx,
  169. float3 dIdy,
  170. float randu,
  171. float randv,
  172. float3 *eval,
  173. float3 *omega_in,
  174. float3 *domega_in_dx,
  175. float3 *domega_in_dy,
  176. float *pdf)
  177. {
  178. const ToonBsdf *bsdf = (const ToonBsdf *)sc;
  179. float max_angle = bsdf->size * M_PI_2_F;
  180. float smooth = bsdf->smooth * M_PI_2_F;
  181. float cosNO = dot(bsdf->N, I);
  182. if (cosNO > 0) {
  183. /* reflect the view vector */
  184. float3 R = (2 * cosNO) * bsdf->N - I;
  185. float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
  186. float angle = sample_angle * randu;
  187. sample_uniform_cone(R, sample_angle, randu, randv, omega_in, pdf);
  188. if (dot(Ng, *omega_in) > 0.0f) {
  189. float cosNI = dot(bsdf->N, *omega_in);
  190. /* make sure the direction we chose is still in the right hemisphere */
  191. if (cosNI > 0) {
  192. *eval = *pdf * bsdf_toon_get_intensity(max_angle, smooth, angle);
  193. #ifdef __RAY_DIFFERENTIALS__
  194. *domega_in_dx = (2 * dot(bsdf->N, dIdx)) * bsdf->N - dIdx;
  195. *domega_in_dy = (2 * dot(bsdf->N, dIdy)) * bsdf->N - dIdy;
  196. #endif
  197. }
  198. else
  199. *pdf = 0.0f;
  200. }
  201. else
  202. *pdf = 0.0f;
  203. }
  204. return LABEL_GLOSSY | LABEL_REFLECT;
  205. }
  206. CCL_NAMESPACE_END
  207. #endif /* __BSDF_TOON_H__ */