BSDFs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import FunctionNode from '../core/FunctionNode.js';
  2. export const F_Schlick = new FunctionNode( `
  3. vec3 F_Schlick( const in vec3 f0, const in vec3 f90, const in float dotVH ) {
  4. // Original approximation by Christophe Schlick '94
  5. // float fresnel = pow( 1.0 - dotVH, 5.0 );
  6. // Optimized variant (presented by Epic at SIGGRAPH '13)
  7. // https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf
  8. float fresnel = exp2( ( -5.55473 * dotVH - 6.98316 ) * dotVH );
  9. return ( f90 - f0 ) * fresnel + f0;
  10. }` ); // validated
  11. export const G_BlinnPhong_Implicit = new FunctionNode( `
  12. float G_BlinnPhong_Implicit() {
  13. // ( const in float dotNL, const in float dotNV )
  14. // geometry term is (n dot l)(n dot v) / 4(n dot l)(n dot v)
  15. return 0.25;
  16. }` ); // validated
  17. export const D_BlinnPhong = new FunctionNode( `
  18. float D_BlinnPhong( const in float shininess, const in float dotNH ) {
  19. return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );
  20. }` ); // validated
  21. export const BRDF_Lambert = new FunctionNode( `
  22. vec3 BRDF_Lambert( const in vec3 diffuseColor ) {
  23. return RECIPROCAL_PI * diffuseColor;
  24. }` ); // validated
  25. export const BRDF_BlinnPhong = new FunctionNode( `
  26. vec3 BRDF_BlinnPhong( vec3 lightDirection, vec3 specularColor, float shininess ) {
  27. vec3 halfDir = normalize( lightDirection + PositionViewDirection );
  28. float dotNH = saturate( dot( NormalView, halfDir ) );
  29. float dotVH = saturate( dot( PositionViewDirection, halfDir ) );
  30. vec3 F = F_Schlick( specularColor, vec3( 1.0 ), dotVH );
  31. float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );
  32. float D = D_BlinnPhong( shininess, dotNH );
  33. return F * ( G * D );
  34. }` ).setIncludes( [ F_Schlick, G_BlinnPhong_Implicit, D_BlinnPhong ] ); // validated
  35. export const punctualLightIntensityToIrradianceFactor = new FunctionNode( `
  36. float punctualLightIntensityToIrradianceFactor( float lightDistance, float cutoffDistance, float decayExponent ) {
  37. #if defined ( PHYSICALLY_CORRECT_LIGHTS )
  38. // based upon Frostbite 3 Moving to Physically-based Rendering
  39. // page 32, equation 26: E[window1]
  40. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
  41. // this is intended to be used on spot and point lights who are represented as luminous intensity
  42. // but who must be converted to luminous irradiance for surface lighting calculation
  43. float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );
  44. if( cutoffDistance > 0.0 ) {
  45. distanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );
  46. }
  47. return distanceFalloff;
  48. #else
  49. if( cutoffDistance > 0.0 && decayExponent > 0.0 ) {
  50. return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );
  51. }
  52. return 1.0;
  53. #endif
  54. }` );
  55. export const RE_Direct_BlinnPhong = new FunctionNode( `
  56. void RE_Direct_BlinnPhong( vec3 lightDirection, vec3 lightColor ) {
  57. float dotNL = saturate( dot( NormalView, lightDirection ) );
  58. vec3 irradiance = dotNL * lightColor;
  59. ReflectedLightDirectDiffuse += irradiance * BRDF_Lambert( MaterialDiffuseColor.rgb );
  60. ReflectedLightDirectSpecular += irradiance * BRDF_BlinnPhong( lightDirection, MaterialSpecularColor, MaterialSpecularShininess );
  61. }` ).setIncludes( [ BRDF_Lambert, BRDF_BlinnPhong ] );
  62. export const RE_IndirectDiffuse_BlinnPhong = new FunctionNode( `
  63. void RE_IndirectDiffuse_BlinnPhong( ) {
  64. ReflectedLightIndirectDiffuse += Irradiance * BRDF_Lambert( MaterialDiffuseColor.rgb );
  65. }` ).setIncludes( [ BRDF_Lambert ] );