PhysicalLightingModel.js 12 KB

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