bsdfs.glsl.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. export default /* glsl */`
  2. vec3 BRDF_Lambert( const in vec3 diffuseColor ) {
  3. return RECIPROCAL_PI * diffuseColor;
  4. } // validated
  5. vec3 F_Schlick( const in vec3 f0, const in float f90, const in float dotVH ) {
  6. // Original approximation by Christophe Schlick '94
  7. // float fresnel = pow( 1.0 - dotVH, 5.0 );
  8. // Optimized variant (presented by Epic at SIGGRAPH '13)
  9. // https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf
  10. float fresnel = exp2( ( - 5.55473 * dotVH - 6.98316 ) * dotVH );
  11. return f0 * ( 1.0 - fresnel ) + ( f90 * fresnel );
  12. } // validated
  13. float F_Schlick( const in float f0, const in float f90, const in float dotVH ) {
  14. // Original approximation by Christophe Schlick '94
  15. // float fresnel = pow( 1.0 - dotVH, 5.0 );
  16. // Optimized variant (presented by Epic at SIGGRAPH '13)
  17. // https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf
  18. float fresnel = exp2( ( - 5.55473 * dotVH - 6.98316 ) * dotVH );
  19. return f0 * ( 1.0 - fresnel ) + ( f90 * fresnel );
  20. } // validated
  21. vec3 Schlick_to_F0( const in vec3 f, const in float f90, const in float dotVH ) {
  22. float x = clamp( 1.0 - dotVH, 0.0, 1.0 );
  23. float x2 = x * x;
  24. float x5 = clamp( x * x2 * x2, 0.0, 0.9999 );
  25. return ( f - vec3( f90 ) * x5 ) / ( 1.0 - x5 );
  26. }
  27. // Moving Frostbite to Physically Based Rendering 3.0 - page 12, listing 2
  28. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
  29. float V_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {
  30. float a2 = pow2( alpha );
  31. float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
  32. float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
  33. return 0.5 / max( gv + gl, EPSILON );
  34. }
  35. // Microfacet Models for Refraction through Rough Surfaces - equation (33)
  36. // http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
  37. // alpha is "roughness squared" in Disney’s reparameterization
  38. float D_GGX( const in float alpha, const in float dotNH ) {
  39. float a2 = pow2( alpha );
  40. float denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0; // avoid alpha = 0 with dotNH = 1
  41. return RECIPROCAL_PI * a2 / pow2( denom );
  42. }
  43. // GGX Distribution, Schlick Fresnel, GGX_SmithCorrelated Visibility
  44. vec3 BRDF_GGX( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in vec3 f0, const in float f90, const in float roughness ) {
  45. float alpha = pow2( roughness ); // UE4's roughness
  46. vec3 halfDir = normalize( lightDir + viewDir );
  47. float dotNL = saturate( dot( normal, lightDir ) );
  48. float dotNV = saturate( dot( normal, viewDir ) );
  49. float dotNH = saturate( dot( normal, halfDir ) );
  50. float dotVH = saturate( dot( viewDir, halfDir ) );
  51. vec3 F = F_Schlick( f0, f90, dotVH );
  52. float V = V_GGX_SmithCorrelated( alpha, dotNL, dotNV );
  53. float D = D_GGX( alpha, dotNH );
  54. return F * ( V * D );
  55. }
  56. #ifdef USE_IRIDESCENCE
  57. vec3 BRDF_GGX_Iridescence( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in vec3 f0, const in float f90, const in float iridescence, const in vec3 iridescenceFresnel, const in float roughness ) {
  58. float alpha = pow2( roughness ); // UE4's roughness
  59. vec3 halfDir = normalize( lightDir + viewDir );
  60. float dotNL = saturate( dot( normal, lightDir ) );
  61. float dotNV = saturate( dot( normal, viewDir ) );
  62. float dotNH = saturate( dot( normal, halfDir ) );
  63. float dotVH = saturate( dot( viewDir, halfDir ) );
  64. vec3 F = mix(F_Schlick( f0, f90, dotVH ), iridescenceFresnel, iridescence);
  65. float V = V_GGX_SmithCorrelated( alpha, dotNL, dotNV );
  66. float D = D_GGX( alpha, dotNH );
  67. return F * ( V * D );
  68. }
  69. #endif
  70. // Rect Area Light
  71. // Real-Time Polygonal-Light Shading with Linearly Transformed Cosines
  72. // by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt
  73. // code: https://github.com/selfshadow/ltc_code/
  74. vec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {
  75. const float LUT_SIZE = 64.0;
  76. const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;
  77. const float LUT_BIAS = 0.5 / LUT_SIZE;
  78. float dotNV = saturate( dot( N, V ) );
  79. // texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) )
  80. vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );
  81. uv = uv * LUT_SCALE + LUT_BIAS;
  82. return uv;
  83. }
  84. float LTC_ClippedSphereFormFactor( const in vec3 f ) {
  85. // Real-Time Area Lighting: a Journey from Research to Production (p.102)
  86. // An approximation of the form factor of a horizon-clipped rectangle.
  87. float l = length( f );
  88. return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );
  89. }
  90. vec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {
  91. float x = dot( v1, v2 );
  92. float y = abs( x );
  93. // rational polynomial approximation to theta / sin( theta ) / 2PI
  94. float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;
  95. float b = 3.4175940 + ( 4.1616724 + y ) * y;
  96. float v = a / b;
  97. float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;
  98. return cross( v1, v2 ) * theta_sintheta;
  99. }
  100. vec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {
  101. // bail if point is on back side of plane of light
  102. // assumes ccw winding order of light vertices
  103. vec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];
  104. vec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];
  105. vec3 lightNormal = cross( v1, v2 );
  106. if( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );
  107. // construct orthonormal basis around N
  108. vec3 T1, T2;
  109. T1 = normalize( V - N * dot( V, N ) );
  110. T2 = - cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system
  111. // compute transform
  112. mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );
  113. // transform rect
  114. vec3 coords[ 4 ];
  115. coords[ 0 ] = mat * ( rectCoords[ 0 ] - P );
  116. coords[ 1 ] = mat * ( rectCoords[ 1 ] - P );
  117. coords[ 2 ] = mat * ( rectCoords[ 2 ] - P );
  118. coords[ 3 ] = mat * ( rectCoords[ 3 ] - P );
  119. // project rect onto sphere
  120. coords[ 0 ] = normalize( coords[ 0 ] );
  121. coords[ 1 ] = normalize( coords[ 1 ] );
  122. coords[ 2 ] = normalize( coords[ 2 ] );
  123. coords[ 3 ] = normalize( coords[ 3 ] );
  124. // calculate vector form factor
  125. vec3 vectorFormFactor = vec3( 0.0 );
  126. vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );
  127. vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );
  128. vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );
  129. vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );
  130. // adjust for horizon clipping
  131. float result = LTC_ClippedSphereFormFactor( vectorFormFactor );
  132. /*
  133. // alternate method of adjusting for horizon clipping (see referece)
  134. // refactoring required
  135. float len = length( vectorFormFactor );
  136. float z = vectorFormFactor.z / len;
  137. const float LUT_SIZE = 64.0;
  138. const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;
  139. const float LUT_BIAS = 0.5 / LUT_SIZE;
  140. // tabulated horizon-clipped sphere, apparently...
  141. vec2 uv = vec2( z * 0.5 + 0.5, len );
  142. uv = uv * LUT_SCALE + LUT_BIAS;
  143. float scale = texture2D( ltc_2, uv ).w;
  144. float result = len * scale;
  145. */
  146. return vec3( result );
  147. }
  148. // End Rect Area Light
  149. float G_BlinnPhong_Implicit( /* const in float dotNL, const in float dotNV */ ) {
  150. // geometry term is (n dot l)(n dot v) / 4(n dot l)(n dot v)
  151. return 0.25;
  152. }
  153. float D_BlinnPhong( const in float shininess, const in float dotNH ) {
  154. return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );
  155. }
  156. vec3 BRDF_BlinnPhong( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float shininess ) {
  157. vec3 halfDir = normalize( lightDir + viewDir );
  158. float dotNH = saturate( dot( normal, halfDir ) );
  159. float dotVH = saturate( dot( viewDir, halfDir ) );
  160. vec3 F = F_Schlick( specularColor, 1.0, dotVH );
  161. float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );
  162. float D = D_BlinnPhong( shininess, dotNH );
  163. return F * ( G * D );
  164. } // validated
  165. #if defined( USE_SHEEN )
  166. // https://github.com/google/filament/blob/master/shaders/src/brdf.fs
  167. float D_Charlie( float roughness, float dotNH ) {
  168. float alpha = pow2( roughness );
  169. // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF"
  170. float invAlpha = 1.0 / alpha;
  171. float cos2h = dotNH * dotNH;
  172. float sin2h = max( 1.0 - cos2h, 0.0078125 ); // 2^(-14/2), so sin2h^2 > 0 in fp16
  173. return ( 2.0 + invAlpha ) * pow( sin2h, invAlpha * 0.5 ) / ( 2.0 * PI );
  174. }
  175. // https://github.com/google/filament/blob/master/shaders/src/brdf.fs
  176. float V_Neubelt( float dotNV, float dotNL ) {
  177. // Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
  178. return saturate( 1.0 / ( 4.0 * ( dotNL + dotNV - dotNL * dotNV ) ) );
  179. }
  180. vec3 BRDF_Sheen( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, vec3 sheenColor, const in float sheenRoughness ) {
  181. vec3 halfDir = normalize( lightDir + viewDir );
  182. float dotNL = saturate( dot( normal, lightDir ) );
  183. float dotNV = saturate( dot( normal, viewDir ) );
  184. float dotNH = saturate( dot( normal, halfDir ) );
  185. float D = D_Charlie( sheenRoughness, dotNH );
  186. float V = V_Neubelt( dotNV, dotNL );
  187. return sheenColor * ( D * V );
  188. }
  189. #endif
  190. `;