PhysicalLightingModel.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
  2. import BRDF_GGX from './BSDF/BRDF_GGX.js';
  3. import DFGApprox from './BSDF/DFGApprox.js';
  4. import EnvironmentBRDF from './BSDF/EnvironmentBRDF.js';
  5. import F_Schlick from './BSDF/F_Schlick.js';
  6. import BRDF_Sheen from './BSDF/BRDF_Sheen.js';
  7. import { lightingModel } from '../core/LightingModel.js';
  8. import { diffuseColor, specularColor, roughness, clearcoat, clearcoatRoughness, sheen, sheenRoughness } from '../core/PropertyNode.js';
  9. import { transformedNormalView, transformedClearcoatNormalView } from '../accessors/NormalNode.js';
  10. import { positionViewDirection } from '../accessors/PositionNode.js';
  11. import { tslFn, float, vec3 } from '../shadernode/ShaderNode.js';
  12. import { cond } from '../math/CondNode.js';
  13. const clearcoatF0 = vec3( 0.04 );
  14. const clearcoatF90 = vec3( 1 );
  15. // This is a curve-fit approxmation to the "Charlie sheen" BRDF integrated over the hemisphere from
  16. // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found
  17. // in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
  18. const IBLSheenBRDF = ( normal, viewDir, roughness ) => {
  19. const dotNV = normal.dot( viewDir ).saturate();
  20. const r2 = roughness.pow2();
  21. const a = cond(
  22. roughness.lessThan( 0.25 ),
  23. float( - 339.2 ).mul( r2 ).add( float( 161.4 ).mul( roughness ) ).sub( 25.9 ),
  24. float( - 8.48 ).mul( r2 ).add( float( 14.3 ).mul( roughness ) ).sub( 9.95 )
  25. );
  26. const b = cond(
  27. roughness.lessThan( 0.25 ),
  28. float( 44.0 ).mul( r2 ).sub( float( 23.7 ).mul( roughness ) ).add( 3.26 ),
  29. float( 1.97 ).mul( r2 ).sub( float( 3.27 ).mul( roughness ) ).add( 0.72 )
  30. );
  31. const DG = cond( roughness.lessThan( 0.25 ), 0.0, float( 0.1 ).mul( roughness ).sub( 0.025 ) ).add( a.mul( dotNV ).add( b ).exp() );
  32. return DG.mul( 1.0 / Math.PI ).saturate();
  33. };
  34. // Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
  35. // Approximates multiscattering in order to preserve energy.
  36. // http://www.jcgt.org/published/0008/01/03/
  37. const computeMultiscattering = ( singleScatter, multiScatter, specularF90 = float( 1 ) ) => {
  38. const fab = DFGApprox( { roughness } );
  39. const FssEss = specularColor.mul( fab.x ).add( specularF90.mul( fab.y ) );
  40. const Ess = fab.x.add( fab.y );
  41. const Ems = Ess.oneMinus();
  42. const Favg = specularColor.add( specularColor.oneMinus().mul( 0.047619 ) ); // 1/21
  43. const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
  44. singleScatter.addAssign( FssEss );
  45. multiScatter.addAssign( Fms.mul( Ems ) );
  46. };
  47. const LM_Init = tslFn( ( context, stack, builder ) => {
  48. if ( builder.includes( clearcoat ) ) {
  49. context.clearcoatRadiance = vec3().temp();
  50. context.reflectedLight.clearcoatSpecular = vec3().temp();
  51. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  52. const Fcc = F_Schlick( {
  53. dotVH: dotNVcc,
  54. f0: clearcoatF0,
  55. f90: clearcoatF90
  56. } );
  57. const outgoingLight = context.reflectedLight.total;
  58. const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( context.reflectedLight.clearcoatSpecular.mul( clearcoat ) );
  59. outgoingLight.assign( clearcoatLight );
  60. }
  61. if ( builder.includes( sheen ) ) {
  62. context.reflectedLight.sheenSpecular = vec3().temp();
  63. const outgoingLight = context.reflectedLight.total;
  64. const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( 0.157 ).oneMinus();
  65. const sheenLight = outgoingLight.mul( sheenEnergyComp ).add( context.reflectedLight.sheenSpecular );
  66. outgoingLight.assign( sheenLight );
  67. }
  68. } );
  69. const RE_IndirectSpecular_Physical = tslFn( ( context ) => {
  70. const { radiance, iblIrradiance, reflectedLight } = context;
  71. if ( reflectedLight.sheenSpecular ) {
  72. reflectedLight.sheenSpecular.addAssign( iblIrradiance.mul(
  73. sheen,
  74. IBLSheenBRDF( transformedNormalView, positionViewDirection, sheenRoughness )
  75. ) );
  76. }
  77. if ( reflectedLight.clearcoatSpecular ) {
  78. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  79. const clearcoatEnv = EnvironmentBRDF( {
  80. dotNV: dotNVcc,
  81. specularColor: clearcoatF0,
  82. specularF90: clearcoatF90,
  83. roughness: clearcoatRoughness
  84. } );
  85. reflectedLight.clearcoatSpecular.addAssign( context.clearcoatRadiance.mul( clearcoatEnv ) );
  86. }
  87. // Both indirect specular and indirect diffuse light accumulate here
  88. const singleScattering = vec3().temp();
  89. const multiScattering = vec3().temp();
  90. const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
  91. computeMultiscattering( singleScattering, multiScattering );
  92. const totalScattering = singleScattering.add( multiScattering );
  93. const diffuse = diffuseColor.mul( totalScattering.r.max( totalScattering.g ).max( totalScattering.b ).oneMinus() );
  94. reflectedLight.indirectSpecular.addAssign( radiance.mul( singleScattering ) );
  95. reflectedLight.indirectSpecular.addAssign( multiScattering.mul( cosineWeightedIrradiance ) );
  96. reflectedLight.indirectDiffuse.addAssign( diffuse.mul( cosineWeightedIrradiance ) );
  97. } );
  98. const RE_IndirectDiffuse_Physical = tslFn( ( context ) => {
  99. const { irradiance, reflectedLight } = context;
  100. reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
  101. } );
  102. const RE_Direct_Physical = tslFn( ( inputs ) => {
  103. const { lightDirection, lightColor, reflectedLight } = inputs;
  104. const dotNL = transformedNormalView.dot( lightDirection ).clamp();
  105. const irradiance = dotNL.mul( lightColor );
  106. if ( reflectedLight.sheenSpecular ) {
  107. reflectedLight.sheenSpecular.addAssign( irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
  108. }
  109. if ( reflectedLight.clearcoatSpecular ) {
  110. const dotNLcc = transformedClearcoatNormalView.dot( lightDirection ).clamp();
  111. const ccIrradiance = dotNLcc.mul( lightColor );
  112. reflectedLight.clearcoatSpecular.addAssign( ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: transformedClearcoatNormalView } ) ) );
  113. }
  114. reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  115. reflectedLight.directSpecular.addAssign( irradiance.mul( BRDF_GGX( { lightDirection, f0: specularColor, f90: 1, roughness } ) ) );
  116. } );
  117. const RE_AmbientOcclusion_Physical = tslFn( ( context ) => {
  118. const { ambientOcclusion, reflectedLight } = context;
  119. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  120. const aoNV = dotNV.add( ambientOcclusion );
  121. const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
  122. const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
  123. reflectedLight.indirectDiffuse.mulAssign( ambientOcclusion );
  124. reflectedLight.indirectSpecular.mulAssign( aoNode );
  125. } );
  126. const physicalLightingModel = lightingModel( LM_Init, RE_Direct_Physical, RE_IndirectDiffuse_Physical, RE_IndirectSpecular_Physical, RE_AmbientOcclusion_Physical );
  127. export default physicalLightingModel;