PhysicalLightingModel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 Schlick_to_F0 from './BSDF/Schlick_to_F0.js';
  7. import BRDF_Sheen from './BSDF/BRDF_Sheen.js';
  8. import LightingModel from '../core/LightingModel.js';
  9. import { diffuseColor, specularColor, roughness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness } from '../core/PropertyNode.js';
  10. import { transformedNormalView, transformedClearcoatNormalView } from '../accessors/NormalNode.js';
  11. import { positionViewDirection } from '../accessors/PositionNode.js';
  12. import { float, vec3, mat3 } from '../shadernode/ShaderNode.js';
  13. import { cond } from '../math/CondNode.js';
  14. import { mix, smoothstep } from '../math/MathNode.js';
  15. //
  16. // Iridescence
  17. //
  18. // XYZ to linear-sRGB color space
  19. const XYZ_TO_REC709 = mat3(
  20. 3.2404542, - 0.9692660, 0.0556434,
  21. - 1.5371385, 1.8760108, - 0.2040259,
  22. - 0.4985314, 0.0415560, 1.0572252
  23. );
  24. // Assume air interface for top
  25. // Note: We don't handle the case fresnel0 == 1
  26. const Fresnel0ToIor = ( fresnel0 ) => {
  27. const sqrtF0 = fresnel0.sqrt();
  28. return vec3( 1.0 ).add( sqrtF0 ).div( vec3( 1.0 ).sub( sqrtF0 ) );
  29. };
  30. // ior is a value between 1.0 and 3.0. 1.0 is air interface
  31. const IorToFresnel0 = ( transmittedIor, incidentIor ) => {
  32. return transmittedIor.sub( incidentIor ).div( transmittedIor.add( incidentIor ) ).pow2();
  33. };
  34. // Fresnel equations for dielectric/dielectric interfaces.
  35. // Ref: https://belcour.github.io/blog/research/2017/05/01/brdf-thin-film.html
  36. // Evaluation XYZ sensitivity curves in Fourier space
  37. const evalSensitivity = ( OPD, shift ) => {
  38. const phase = OPD.mul( 2.0 * Math.PI * 1.0e-9 );
  39. const val = vec3( 5.4856e-13, 4.4201e-13, 5.2481e-13 );
  40. const pos = vec3( 1.6810e+06, 1.7953e+06, 2.2084e+06 );
  41. const VAR = vec3( 4.3278e+09, 9.3046e+09, 6.6121e+09 );
  42. const x = float( 9.7470e-14 * Math.sqrt( 2.0 * Math.PI * 4.5282e+09 ) ).mul( phase.mul( 2.2399e+06 ).add( shift.x ).cos() ).mul( phase.pow2().mul( - 4.5282e+09 ).exp() );
  43. let xyz = val.mul( VAR.mul( 2.0 * Math.PI ).sqrt() ).mul( pos.mul( phase ).add( shift ).cos() ).mul( phase.pow2().negate().mul( VAR ).exp() );
  44. xyz = vec3( xyz.x.add( x ), xyz.y, xyz.z ).div( 1.0685e-7 );
  45. const rgb = XYZ_TO_REC709.mul( xyz );
  46. return rgb;
  47. };
  48. const evalIridescence = ( outsideIOR, eta2, cosTheta1, thinFilmThickness, baseF0 ) => {
  49. // Force iridescenceIOR -> outsideIOR when thinFilmThickness -> 0.0
  50. const iridescenceIOR = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) );
  51. // Evaluate the cosTheta on the base layer (Snell law)
  52. const sinTheta2Sq = outsideIOR.div( iridescenceIOR ).pow2().mul( float( 1 ).sub( cosTheta1.pow2() ) );
  53. // Handle TIR:
  54. const cosTheta2Sq = float( 1 ).sub( sinTheta2Sq );
  55. /*if ( cosTheta2Sq < 0.0 ) {
  56. return vec3( 1.0 );
  57. }*/
  58. const cosTheta2 = cosTheta2Sq.sqrt();
  59. // First interface
  60. const R0 = IorToFresnel0( iridescenceIOR, outsideIOR );
  61. const R12 = F_Schlick( { f0: R0, f90: 1.0, dotVH: cosTheta1 } );
  62. //const R21 = R12;
  63. const T121 = R12.oneMinus();
  64. const phi12 = iridescenceIOR.lessThan( outsideIOR ).cond( Math.PI, 0.0 );
  65. const phi21 = float( Math.PI ).sub( phi12 );
  66. // Second interface
  67. const baseIOR = Fresnel0ToIor( baseF0.clamp( 0.0, 0.9999 ) ); // guard against 1.0
  68. const R1 = IorToFresnel0( baseIOR, iridescenceIOR.vec3() );
  69. const R23 = F_Schlick( { f0: R1, f90: 1.0, dotVH: cosTheta2 } );
  70. const phi23 = vec3(
  71. baseIOR.x.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 ),
  72. baseIOR.y.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 ),
  73. baseIOR.z.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 )
  74. );
  75. // Phase shift
  76. const OPD = iridescenceIOR.mul( thinFilmThickness, cosTheta2, 2.0 );
  77. const phi = vec3( phi21 ).add( phi23 );
  78. // Compound terms
  79. const R123 = R12.mul( R23 ).clamp( 1e-5, 0.9999 );
  80. const r123 = R123.sqrt();
  81. const Rs = T121.pow2().mul( R23 ).div( vec3( 1.0 ).sub( R123 ) );
  82. // Reflectance term for m = 0 (DC term amplitude)
  83. const C0 = R12.add( Rs );
  84. let I = C0;
  85. // Reflectance term for m > 0 (pairs of diracs)
  86. let Cm = Rs.sub( T121 );
  87. for ( let m = 1; m <= 2; ++ m ) {
  88. Cm = Cm.mul( r123 );
  89. const Sm = evalSensitivity( float( m ).mul( OPD ), float( m ).mul( phi ) ).mul( 2.0 );
  90. I = I.add( Cm.mul( Sm ) );
  91. }
  92. // Since out of gamut colors might be produced, negative color values are clamped to 0.
  93. return I.max( vec3( 0.0 ) );
  94. };
  95. //
  96. // Sheen
  97. //
  98. // This is a curve-fit approxmation to the "Charlie sheen" BRDF integrated over the hemisphere from
  99. // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found
  100. // in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
  101. const IBLSheenBRDF = ( normal, viewDir, roughness ) => {
  102. const dotNV = normal.dot( viewDir ).saturate();
  103. const r2 = roughness.pow2();
  104. const a = cond(
  105. roughness.lessThan( 0.25 ),
  106. float( - 339.2 ).mul( r2 ).add( float( 161.4 ).mul( roughness ) ).sub( 25.9 ),
  107. float( - 8.48 ).mul( r2 ).add( float( 14.3 ).mul( roughness ) ).sub( 9.95 )
  108. );
  109. const b = cond(
  110. roughness.lessThan( 0.25 ),
  111. float( 44.0 ).mul( r2 ).sub( float( 23.7 ).mul( roughness ) ).add( 3.26 ),
  112. float( 1.97 ).mul( r2 ).sub( float( 3.27 ).mul( roughness ) ).add( 0.72 )
  113. );
  114. const DG = cond( roughness.lessThan( 0.25 ), 0.0, float( 0.1 ).mul( roughness ).sub( 0.025 ) ).add( a.mul( dotNV ).add( b ).exp() );
  115. return DG.mul( 1.0 / Math.PI ).saturate();
  116. };
  117. const clearcoatF0 = vec3( 0.04 );
  118. const clearcoatF90 = vec3( 1 );
  119. //
  120. class PhysicalLightingModel extends LightingModel {
  121. constructor( clearcoat = true, sheen = true, iridescence = true ) {
  122. super();
  123. this.clearcoat = clearcoat;
  124. this.sheen = sheen;
  125. this.iridescence = iridescence;
  126. this.clearcoatRadiance = null;
  127. this.clearcoatSpecular = null;
  128. this.sheenSpecular = null;
  129. this.iridescenceFresnel = null;
  130. this.iridescenceF0 = null;
  131. }
  132. init( { reflectedLight } ) {
  133. if ( this.clearcoat === true ) {
  134. this.clearcoatRadiance = vec3().temp();
  135. this.clearcoatSpecular = vec3().temp();
  136. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  137. const Fcc = F_Schlick( {
  138. dotVH: dotNVcc,
  139. f0: clearcoatF0,
  140. f90: clearcoatF90
  141. } );
  142. const outgoingLight = reflectedLight.total;
  143. const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( this.clearcoatSpecular.mul( clearcoat ) );
  144. outgoingLight.assign( clearcoatLight );
  145. }
  146. if ( this.sheen === true ) {
  147. this.sheenSpecular = vec3().temp();
  148. const outgoingLight = reflectedLight.total;
  149. const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( 0.157 ).oneMinus();
  150. const sheenLight = outgoingLight.mul( sheenEnergyComp ).add( this.sheenSpecular );
  151. outgoingLight.assign( sheenLight );
  152. }
  153. if ( this.iridescence === true ) {
  154. const dotNVi = transformedNormalView.dot( positionViewDirection ).clamp();
  155. this.iridescenceFresnel = evalIridescence( float( 1.0 ), iridescenceIOR, dotNVi, iridescenceThickness, specularColor );
  156. this.iridescenceF0 = Schlick_to_F0( { f: this.iridescenceFresnel, f90: 1.0, dotVH: dotNVi } );
  157. }
  158. }
  159. // Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
  160. // Approximates multiscattering in order to preserve energy.
  161. // http://www.jcgt.org/published/0008/01/03/
  162. computeMultiscattering( singleScatter, multiScatter, specularF90 = float( 1 ) ) {
  163. const fab = DFGApprox( { roughness } );
  164. const Fr = this.iridescenceF0 ? iridescence.mix( specularColor, this.iridescenceF0 ) : specularColor;
  165. const FssEss = Fr.mul( fab.x ).add( specularF90.mul( fab.y ) );
  166. const Ess = fab.x.add( fab.y );
  167. const Ems = Ess.oneMinus();
  168. const Favg = specularColor.add( specularColor.oneMinus().mul( 0.047619 ) ); // 1/21
  169. const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
  170. singleScatter.addAssign( FssEss );
  171. multiScatter.addAssign( Fms.mul( Ems ) );
  172. }
  173. direct( { lightDirection, lightColor, reflectedLight } ) {
  174. const dotNL = transformedNormalView.dot( lightDirection ).clamp();
  175. const irradiance = dotNL.mul( lightColor );
  176. if ( this.sheen === true ) {
  177. this.sheenSpecular.addAssign( irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
  178. }
  179. if ( this.clearcoat === true ) {
  180. const dotNLcc = transformedClearcoatNormalView.dot( lightDirection ).clamp();
  181. const ccIrradiance = dotNLcc.mul( lightColor );
  182. this.clearcoatSpecular.addAssign( ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: transformedClearcoatNormalView } ) ) );
  183. }
  184. reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  185. reflectedLight.directSpecular.addAssign( irradiance.mul( BRDF_GGX( { lightDirection, f0: specularColor, f90: 1, roughness, iridescence: this.iridescence, iridescenceFresnel: this.iridescenceFresnel } ) ) );
  186. }
  187. indirectDiffuse( { irradiance, reflectedLight } ) {
  188. reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
  189. }
  190. indirectSpecular( { radiance, iblIrradiance, reflectedLight, } ) {
  191. if ( this.sheen === true ) {
  192. this.sheenSpecular.addAssign( iblIrradiance.mul(
  193. sheen,
  194. IBLSheenBRDF( transformedNormalView, positionViewDirection, sheenRoughness )
  195. ) );
  196. }
  197. if ( this.clearcoat === true ) {
  198. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  199. const clearcoatEnv = EnvironmentBRDF( {
  200. dotNV: dotNVcc,
  201. specularColor: clearcoatF0,
  202. specularF90: clearcoatF90,
  203. roughness: clearcoatRoughness
  204. } );
  205. this.clearcoatSpecular.addAssign( this.clearcoatRadiance.mul( clearcoatEnv ) );
  206. }
  207. // Both indirect specular and indirect diffuse light accumulate here
  208. const singleScattering = vec3().temp();
  209. const multiScattering = vec3().temp();
  210. const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
  211. this.computeMultiscattering( singleScattering, multiScattering );
  212. const totalScattering = singleScattering.add( multiScattering );
  213. const diffuse = diffuseColor.mul( totalScattering.r.max( totalScattering.g ).max( totalScattering.b ).oneMinus() );
  214. reflectedLight.indirectSpecular.addAssign( radiance.mul( singleScattering ) );
  215. reflectedLight.indirectSpecular.addAssign( multiScattering.mul( cosineWeightedIrradiance ) );
  216. reflectedLight.indirectDiffuse.addAssign( diffuse.mul( cosineWeightedIrradiance ) );
  217. }
  218. ambientOcclusion( { ambientOcclusion, reflectedLight } ) {
  219. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  220. const aoNV = dotNV.add( ambientOcclusion );
  221. const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
  222. const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
  223. reflectedLight.indirectDiffuse.mulAssign( ambientOcclusion );
  224. reflectedLight.indirectSpecular.mulAssign( aoNode );
  225. }
  226. }
  227. export default PhysicalLightingModel;