PhysicalLightingModel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 = false, sheen = false, iridescence = false ) {
  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. start( /*context*/ ) {
  133. if ( this.clearcoat === true ) {
  134. this.clearcoatRadiance = vec3().temp( 'clearcoatRadiance' );
  135. this.clearcoatSpecular = vec3().temp( 'clearcoatSpecular' );
  136. }
  137. if ( this.sheen === true ) {
  138. this.sheenSpecular = vec3().temp( 'sheenSpecular' );
  139. }
  140. if ( this.iridescence === true ) {
  141. const dotNVi = transformedNormalView.dot( positionViewDirection ).clamp();
  142. this.iridescenceFresnel = evalIridescence( float( 1.0 ), iridescenceIOR, dotNVi, iridescenceThickness, specularColor );
  143. this.iridescenceF0 = Schlick_to_F0( { f: this.iridescenceFresnel, f90: 1.0, dotVH: dotNVi } );
  144. }
  145. }
  146. // Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
  147. // Approximates multiscattering in order to preserve energy.
  148. // http://www.jcgt.org/published/0008/01/03/
  149. computeMultiscattering( stack, singleScatter, multiScatter, specularF90 = float( 1 ) ) {
  150. const fab = DFGApprox( { roughness } );
  151. const Fr = this.iridescenceF0 ? iridescence.mix( specularColor, this.iridescenceF0 ) : specularColor;
  152. const FssEss = Fr.mul( fab.x ).add( specularF90.mul( fab.y ) );
  153. const Ess = fab.x.add( fab.y );
  154. const Ems = Ess.oneMinus();
  155. const Favg = specularColor.add( specularColor.oneMinus().mul( 0.047619 ) ); // 1/21
  156. const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
  157. stack.addAssign( singleScatter, FssEss );
  158. stack.addAssign( multiScatter, Fms.mul( Ems ) );
  159. }
  160. direct( { lightDirection, lightColor, reflectedLight }, stack ) {
  161. const dotNL = transformedNormalView.dot( lightDirection ).clamp();
  162. const irradiance = dotNL.mul( lightColor );
  163. if ( this.sheen === true ) {
  164. stack.addAssign( this.sheenSpecular, irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
  165. }
  166. if ( this.clearcoat === true ) {
  167. const dotNLcc = transformedClearcoatNormalView.dot( lightDirection ).clamp();
  168. const ccIrradiance = dotNLcc.mul( lightColor );
  169. stack.addAssign( this.clearcoatSpecular, ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: transformedClearcoatNormalView } ) ) );
  170. }
  171. stack.addAssign( reflectedLight.directDiffuse, irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  172. stack.addAssign( reflectedLight.directSpecular, irradiance.mul( BRDF_GGX( { lightDirection, f0: specularColor, f90: 1, roughness, iridescence: this.iridescence, iridescenceFresnel: this.iridescenceFresnel } ) ) );
  173. }
  174. indirectDiffuse( { irradiance, reflectedLight }, stack ) {
  175. stack.addAssign( reflectedLight.indirectDiffuse, irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
  176. }
  177. indirectSpecular( { radiance, iblIrradiance, reflectedLight }, stack ) {
  178. if ( this.sheen === true ) {
  179. stack.addAssign( this.sheenSpecular, iblIrradiance.mul(
  180. sheen,
  181. IBLSheenBRDF( transformedNormalView, positionViewDirection, sheenRoughness )
  182. ) );
  183. }
  184. if ( this.clearcoat === true ) {
  185. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  186. const clearcoatEnv = EnvironmentBRDF( {
  187. dotNV: dotNVcc,
  188. specularColor: clearcoatF0,
  189. specularF90: clearcoatF90,
  190. roughness: clearcoatRoughness
  191. } );
  192. stack.addAssign( this.clearcoatSpecular, this.clearcoatRadiance.mul( clearcoatEnv ) );
  193. }
  194. // Both indirect specular and indirect diffuse light accumulate here
  195. const singleScattering = vec3().temp( 'singleScattering' );
  196. const multiScattering = vec3().temp( 'multiScattering' );
  197. const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
  198. this.computeMultiscattering( stack, singleScattering, multiScattering );
  199. const totalScattering = singleScattering.add( multiScattering );
  200. const diffuse = diffuseColor.mul( totalScattering.r.max( totalScattering.g ).max( totalScattering.b ).oneMinus() );
  201. stack.addAssign( reflectedLight.indirectSpecular, radiance.mul( singleScattering ) );
  202. stack.addAssign( reflectedLight.indirectSpecular, multiScattering.mul( cosineWeightedIrradiance ) );
  203. stack.addAssign( reflectedLight.indirectDiffuse, diffuse.mul( cosineWeightedIrradiance ) );
  204. }
  205. ambientOcclusion( { ambientOcclusion, reflectedLight }, stack ) {
  206. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  207. const aoNV = dotNV.add( ambientOcclusion );
  208. const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
  209. const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
  210. stack.mulAssign( reflectedLight.indirectDiffuse, ambientOcclusion );
  211. stack.mulAssign( reflectedLight.indirectSpecular, aoNode );
  212. }
  213. finish( context, stack ) {
  214. const { outgoingLight } = context;
  215. if ( this.clearcoat === true ) {
  216. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  217. const Fcc = F_Schlick( {
  218. dotVH: dotNVcc,
  219. f0: clearcoatF0,
  220. f90: clearcoatF90
  221. } );
  222. const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( this.clearcoatSpecular.mul( clearcoat ) );
  223. stack.assign( outgoingLight, clearcoatLight );
  224. }
  225. if ( this.sheen === true ) {
  226. const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( 0.157 ).oneMinus();
  227. const sheenLight = outgoingLight.mul( sheenEnergyComp ).add( this.sheenSpecular );
  228. stack.assign( outgoingLight, sheenLight );
  229. }
  230. }
  231. }
  232. export default PhysicalLightingModel;