PhysicalLightingModel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 { tslFn, 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 = tslFn( ( { 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. } ).setLayout( {
  95. name: 'evalIridescence',
  96. type: 'vec3',
  97. inputs: [
  98. { name: 'outsideIOR', type: 'float' },
  99. { name: 'eta2', type: 'float' },
  100. { name: 'cosTheta1', type: 'float' },
  101. { name: 'thinFilmThickness', type: 'float' },
  102. { name: 'baseF0', type: 'vec3' }
  103. ]
  104. } );
  105. //
  106. // Sheen
  107. //
  108. // This is a curve-fit approxmation to the "Charlie sheen" BRDF integrated over the hemisphere from
  109. // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found
  110. // in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
  111. const IBLSheenBRDF = tslFn( ( { normal, viewDir, roughness } ) => {
  112. const dotNV = normal.dot( viewDir ).saturate();
  113. const r2 = roughness.pow2();
  114. const a = cond(
  115. roughness.lessThan( 0.25 ),
  116. float( - 339.2 ).mul( r2 ).add( float( 161.4 ).mul( roughness ) ).sub( 25.9 ),
  117. float( - 8.48 ).mul( r2 ).add( float( 14.3 ).mul( roughness ) ).sub( 9.95 )
  118. );
  119. const b = cond(
  120. roughness.lessThan( 0.25 ),
  121. float( 44.0 ).mul( r2 ).sub( float( 23.7 ).mul( roughness ) ).add( 3.26 ),
  122. float( 1.97 ).mul( r2 ).sub( float( 3.27 ).mul( roughness ) ).add( 0.72 )
  123. );
  124. const DG = cond( roughness.lessThan( 0.25 ), 0.0, float( 0.1 ).mul( roughness ).sub( 0.025 ) ).add( a.mul( dotNV ).add( b ).exp() );
  125. return DG.mul( 1.0 / Math.PI ).saturate();
  126. } );
  127. const clearcoatF0 = vec3( 0.04 );
  128. const clearcoatF90 = vec3( 1 );
  129. //
  130. class PhysicalLightingModel extends LightingModel {
  131. constructor( clearcoat = false, sheen = false, iridescence = false ) {
  132. super();
  133. this.clearcoat = clearcoat;
  134. this.sheen = sheen;
  135. this.iridescence = iridescence;
  136. this.clearcoatRadiance = null;
  137. this.clearcoatSpecular = null;
  138. this.sheenSpecular = null;
  139. this.iridescenceFresnel = null;
  140. this.iridescenceF0 = null;
  141. }
  142. start( /*context*/ ) {
  143. if ( this.clearcoat === true ) {
  144. this.clearcoatRadiance = vec3().temp( 'clearcoatRadiance' );
  145. this.clearcoatSpecular = vec3().temp( 'clearcoatSpecular' );
  146. }
  147. if ( this.sheen === true ) {
  148. this.sheenSpecular = vec3().temp( 'sheenSpecular' );
  149. }
  150. if ( this.iridescence === true ) {
  151. const dotNVi = transformedNormalView.dot( positionViewDirection ).clamp();
  152. this.iridescenceFresnel = evalIridescence( {
  153. outsideIOR: float( 1.0 ),
  154. eta2: iridescenceIOR,
  155. cosTheta1: dotNVi,
  156. thinFilmThickness: iridescenceThickness,
  157. baseF0: specularColor
  158. } );
  159. this.iridescenceF0 = Schlick_to_F0( { f: this.iridescenceFresnel, f90: 1.0, dotVH: dotNVi } );
  160. }
  161. }
  162. // Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
  163. // Approximates multiscattering in order to preserve energy.
  164. // http://www.jcgt.org/published/0008/01/03/
  165. computeMultiscattering( stack, singleScatter, multiScatter, specularF90 = float( 1 ) ) {
  166. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  167. const fab = DFGApprox( { roughness, dotNV } );
  168. const Fr = this.iridescenceF0 ? iridescence.mix( specularColor, this.iridescenceF0 ) : specularColor;
  169. const FssEss = Fr.mul( fab.x ).add( specularF90.mul( fab.y ) );
  170. const Ess = fab.x.add( fab.y );
  171. const Ems = Ess.oneMinus();
  172. const Favg = specularColor.add( specularColor.oneMinus().mul( 0.047619 ) ); // 1/21
  173. const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
  174. stack.addAssign( singleScatter, FssEss );
  175. stack.addAssign( multiScatter, Fms.mul( Ems ) );
  176. }
  177. direct( { lightDirection, lightColor, reflectedLight }, stack ) {
  178. const dotNL = transformedNormalView.dot( lightDirection ).clamp();
  179. const irradiance = dotNL.mul( lightColor );
  180. if ( this.sheen === true ) {
  181. stack.addAssign( this.sheenSpecular, irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
  182. }
  183. if ( this.clearcoat === true ) {
  184. const dotNLcc = transformedClearcoatNormalView.dot( lightDirection ).clamp();
  185. const ccIrradiance = dotNLcc.mul( lightColor );
  186. stack.addAssign( this.clearcoatSpecular, ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: transformedClearcoatNormalView } ) ) );
  187. }
  188. stack.addAssign( reflectedLight.directDiffuse, irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  189. stack.addAssign( reflectedLight.directSpecular, irradiance.mul( BRDF_GGX( { lightDirection, f0: specularColor, f90: 1, roughness, iridescence: this.iridescence, iridescenceFresnel: this.iridescenceFresnel } ) ) );
  190. }
  191. indirectDiffuse( { irradiance, reflectedLight }, stack ) {
  192. stack.addAssign( reflectedLight.indirectDiffuse, irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
  193. }
  194. indirectSpecular( { radiance, iblIrradiance, reflectedLight }, stack ) {
  195. if ( this.sheen === true ) {
  196. stack.addAssign( this.sheenSpecular, iblIrradiance.mul(
  197. sheen,
  198. IBLSheenBRDF( {
  199. normal: transformedNormalView,
  200. viewDir: positionViewDirection,
  201. roughness: sheenRoughness
  202. } )
  203. ) );
  204. }
  205. if ( this.clearcoat === true ) {
  206. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  207. const clearcoatEnv = EnvironmentBRDF( {
  208. dotNV: dotNVcc,
  209. specularColor: clearcoatF0,
  210. specularF90: clearcoatF90,
  211. roughness: clearcoatRoughness
  212. } );
  213. stack.addAssign( this.clearcoatSpecular, this.clearcoatRadiance.mul( clearcoatEnv ) );
  214. }
  215. // Both indirect specular and indirect diffuse light accumulate here
  216. const singleScattering = vec3().temp( 'singleScattering' );
  217. const multiScattering = vec3().temp( 'multiScattering' );
  218. const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
  219. this.computeMultiscattering( stack, singleScattering, multiScattering );
  220. const totalScattering = singleScattering.add( multiScattering );
  221. const diffuse = diffuseColor.mul( totalScattering.r.max( totalScattering.g ).max( totalScattering.b ).oneMinus() );
  222. stack.addAssign( reflectedLight.indirectSpecular, radiance.mul( singleScattering ) );
  223. stack.addAssign( reflectedLight.indirectSpecular, multiScattering.mul( cosineWeightedIrradiance ) );
  224. stack.addAssign( reflectedLight.indirectDiffuse, diffuse.mul( cosineWeightedIrradiance ) );
  225. }
  226. ambientOcclusion( { ambientOcclusion, reflectedLight }, stack ) {
  227. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  228. const aoNV = dotNV.add( ambientOcclusion );
  229. const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
  230. const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
  231. stack.mulAssign( reflectedLight.indirectDiffuse, ambientOcclusion );
  232. stack.mulAssign( reflectedLight.indirectSpecular, aoNode );
  233. }
  234. finish( context, stack ) {
  235. const { outgoingLight } = context;
  236. if ( this.clearcoat === true ) {
  237. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  238. const Fcc = F_Schlick( {
  239. dotVH: dotNVcc,
  240. f0: clearcoatF0,
  241. f90: clearcoatF90
  242. } );
  243. const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( this.clearcoatSpecular.mul( clearcoat ) );
  244. stack.assign( outgoingLight, clearcoatLight );
  245. }
  246. if ( this.sheen === true ) {
  247. const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( 0.157 ).oneMinus();
  248. const sheenLight = outgoingLight.mul( sheenEnergyComp ).add( this.sheenSpecular );
  249. stack.assign( outgoingLight, sheenLight );
  250. }
  251. }
  252. }
  253. export default PhysicalLightingModel;