brdf_cost_function.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2020 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: darius.rueckert@fau.de (Darius Rueckert)
  30. //
  31. //
  32. #ifndef CERES_INTERNAL_AUTODIFF_BENCHMARK_BRDF_COST_FUNCTION_H_
  33. #define CERES_INTERNAL_AUTODIFF_BENCHMARK_BRDF_COST_FUNCTION_H_
  34. #include <Eigen/Core>
  35. #include <cmath>
  36. #include "ceres/constants.h"
  37. namespace ceres {
  38. // The brdf is based on:
  39. // Burley, Brent, and Walt Disney Animation Studios. "Physically-based shading
  40. // at disney." ACM SIGGRAPH. Vol. 2012. 2012.
  41. //
  42. // The implementation is based on:
  43. // https://github.com/wdas/brdf/blob/master/src/brdfs/disney.brdf
  44. struct Brdf {
  45. public:
  46. template <typename T>
  47. inline bool operator()(const T* const material,
  48. const T* const c_ptr,
  49. const T* const n_ptr,
  50. const T* const v_ptr,
  51. const T* const l_ptr,
  52. const T* const x_ptr,
  53. const T* const y_ptr,
  54. T* residual) const {
  55. using Vec3 = Eigen::Matrix<T, 3, 1>;
  56. T metallic = material[0];
  57. T subsurface = material[1];
  58. T specular = material[2];
  59. T roughness = material[3];
  60. T specular_tint = material[4];
  61. T anisotropic = material[5];
  62. T sheen = material[6];
  63. T sheen_tint = material[7];
  64. T clearcoat = material[8];
  65. T clearcoat_gloss = material[9];
  66. Eigen::Map<const Vec3> c(c_ptr);
  67. Eigen::Map<const Vec3> n(n_ptr);
  68. Eigen::Map<const Vec3> v(v_ptr);
  69. Eigen::Map<const Vec3> l(l_ptr);
  70. Eigen::Map<const Vec3> x(x_ptr);
  71. Eigen::Map<const Vec3> y(y_ptr);
  72. const T n_dot_l = n.dot(l);
  73. const T n_dot_v = n.dot(v);
  74. const Vec3 l_p_v = l + v;
  75. const Vec3 h = l_p_v / l_p_v.norm();
  76. const T n_dot_h = n.dot(h);
  77. const T l_dot_h = l.dot(h);
  78. const T h_dot_x = h.dot(x);
  79. const T h_dot_y = h.dot(y);
  80. const T c_dlum = T(0.3) * c[0] + T(0.6) * c[1] + T(0.1) * c[2];
  81. const Vec3 c_tint = c / c_dlum;
  82. const Vec3 c_spec0 =
  83. Lerp(specular * T(0.08) *
  84. Lerp(Vec3(T(1), T(1), T(1)), c_tint, specular_tint),
  85. c,
  86. metallic);
  87. const Vec3 c_sheen = Lerp(Vec3(T(1), T(1), T(1)), c_tint, sheen_tint);
  88. // Diffuse fresnel - go from 1 at normal incidence to .5 at grazing
  89. // and mix in diffuse retro-reflection based on roughness
  90. const T fl = SchlickFresnel(n_dot_l);
  91. const T fv = SchlickFresnel(n_dot_v);
  92. const T fd_90 = T(0.5) + T(2) * l_dot_h * l_dot_h * roughness;
  93. const T fd = Lerp(T(1), fd_90, fl) * Lerp(T(1), fd_90, fv);
  94. // Based on Hanrahan-Krueger brdf approximation of isotropic bssrdf
  95. // 1.25 scale is used to (roughly) preserve albedo
  96. // Fss90 used to "flatten" retroreflection based on roughness
  97. const T fss_90 = l_dot_h * l_dot_h * roughness;
  98. const T fss = Lerp(T(1), fss_90, fl) * Lerp(T(1), fss_90, fv);
  99. const T ss =
  100. T(1.25) * (fss * (T(1) / (n_dot_l + n_dot_v) - T(0.5)) + T(0.5));
  101. // specular
  102. const T eps = T(0.001);
  103. const T aspct = Aspect(anisotropic);
  104. const T ax_temp = Square(roughness) / aspct;
  105. const T ay_temp = Square(roughness) * aspct;
  106. const T ax = (ax_temp < eps ? eps : ax_temp);
  107. const T ay = (ay_temp < eps ? eps : ay_temp);
  108. const T ds = GTR2Aniso(n_dot_h, h_dot_x, h_dot_y, ax, ay);
  109. const T fh = SchlickFresnel(l_dot_h);
  110. const Vec3 fs = Lerp(c_spec0, Vec3(T(1), T(1), T(1)), fh);
  111. const T roughg = Square(roughness * T(0.5) + T(0.5));
  112. const T ggxn_dot_l = SmithG_GGX(n_dot_l, roughg);
  113. const T ggxn_dot_v = SmithG_GGX(n_dot_v, roughg);
  114. const T gs = ggxn_dot_l * ggxn_dot_v;
  115. // sheen
  116. const Vec3 f_sheen = fh * sheen * c_sheen;
  117. // clearcoat (ior = 1.5 -> F0 = 0.04)
  118. const T a = Lerp(T(0.1), T(0.001), clearcoat_gloss);
  119. const T dr = GTR1(n_dot_h, a);
  120. const T fr = Lerp(T(0.04), T(1), fh);
  121. const T cggxn_dot_l = SmithG_GGX(n_dot_l, T(0.25));
  122. const T cggxn_dot_v = SmithG_GGX(n_dot_v, T(0.25));
  123. const T gr = cggxn_dot_l * cggxn_dot_v;
  124. const Vec3 result_no_cosine =
  125. (T(1.0 / constants::pi) * Lerp(fd, ss, subsurface) * c + f_sheen) *
  126. (T(1) - metallic) +
  127. gs * fs * ds +
  128. Vec3(T(0.25), T(0.25), T(0.25)) * clearcoat * gr * fr * dr;
  129. const Vec3 result = n_dot_l * result_no_cosine;
  130. residual[0] = result(0);
  131. residual[1] = result(1);
  132. residual[2] = result(2);
  133. return true;
  134. }
  135. template <typename T>
  136. inline T SchlickFresnel(const T& u) const {
  137. T m = T(1) - u;
  138. const T m2 = m * m;
  139. return m2 * m2 * m; // (1-u)^5
  140. }
  141. template <typename T>
  142. inline T Aspect(const T& anisotropic) const {
  143. return T(sqrt(T(1) - anisotropic * T(0.9)));
  144. }
  145. template <typename T>
  146. inline T SmithG_GGX(const T& n_dot_v, const T& alpha_g) const {
  147. const T a = alpha_g * alpha_g;
  148. const T b = n_dot_v * n_dot_v;
  149. return T(1) / (n_dot_v + T(sqrt(a + b - a * b)));
  150. }
  151. // Generalized-Trowbridge-Reitz (GTR) Microfacet Distribution
  152. // See paper, Appendix B
  153. template <typename T>
  154. inline T GTR1(const T& n_dot_h, const T& a) const {
  155. T result = T(0);
  156. if (a >= T(1)) {
  157. result = T(1 / constants::pi);
  158. } else {
  159. const T a2 = a * a;
  160. const T t = T(1) + (a2 - T(1)) * n_dot_h * n_dot_h;
  161. result = (a2 - T(1)) / (T(constants::pi) * T(log(a2) * t));
  162. }
  163. return result;
  164. }
  165. template <typename T>
  166. inline T GTR2Aniso(const T& n_dot_h,
  167. const T& h_dot_x,
  168. const T& h_dot_y,
  169. const T& ax,
  170. const T& ay) const {
  171. return T(1) / (T(constants::pi) * ax * ay *
  172. Square(Square(h_dot_x / ax) + Square(h_dot_y / ay) +
  173. n_dot_h * n_dot_h));
  174. }
  175. template <typename T>
  176. inline T Lerp(const T& a, const T& b, const T& u) const {
  177. return a + u * (b - a);
  178. }
  179. template <typename Derived1, typename Derived2>
  180. inline typename Derived1::PlainObject Lerp(
  181. const Eigen::MatrixBase<Derived1>& a,
  182. const Eigen::MatrixBase<Derived2>& b,
  183. typename Derived1::Scalar alpha) const {
  184. return (typename Derived1::Scalar(1) - alpha) * a + alpha * b;
  185. }
  186. template <typename T>
  187. inline T Square(const T& x) const {
  188. return x * x;
  189. }
  190. };
  191. } // namespace ceres
  192. #endif // CERES_INTERNAL_AUTODIFF_BENCHMARK_BRDF_COST_FUNCTION_H_