PhysicalLightingModel.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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, specularF90, roughness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, ior, thickness, transmission, attenuationDistance, attenuationColor, dispersion } from '../core/PropertyNode.js';
  10. import { transformedNormalView, transformedClearcoatNormalView, transformedNormalWorld } from '../accessors/NormalNode.js';
  11. import { positionViewDirection, positionWorld } from '../accessors/PositionNode.js';
  12. import { tslFn, float, vec2, vec3, vec4, mat3, If } from '../shadernode/ShaderNode.js';
  13. import { cond } from '../math/CondNode.js';
  14. import { mix, normalize, refract, length, clamp, log2, log, exp, smoothstep } from '../math/MathNode.js';
  15. import { div } from '../math/OperatorNode.js';
  16. import { cameraPosition, cameraProjectionMatrix, cameraViewMatrix } from '../accessors/CameraNode.js';
  17. import { modelWorldMatrix } from '../accessors/ModelNode.js';
  18. import { viewportResolution } from '../display/ViewportNode.js';
  19. import { viewportMipTexture } from '../display/ViewportTextureNode.js';
  20. import { loop } from '../utils/LoopNode.js';
  21. //
  22. // Transmission
  23. //
  24. const getVolumeTransmissionRay = tslFn( ( [ n, v, thickness, ior, modelMatrix ] ) => {
  25. // Direction of refracted light.
  26. const refractionVector = vec3( refract( v.negate(), normalize( n ), div( 1.0, ior ) ) );
  27. // Compute rotation-independant scaling of the model matrix.
  28. const modelScale = vec3(
  29. length( modelMatrix[ 0 ].xyz ),
  30. length( modelMatrix[ 1 ].xyz ),
  31. length( modelMatrix[ 2 ].xyz )
  32. );
  33. // The thickness is specified in local space.
  34. return normalize( refractionVector ).mul( thickness.mul( modelScale ) );
  35. } ).setLayout( {
  36. name: 'getVolumeTransmissionRay',
  37. type: 'vec3',
  38. inputs: [
  39. { name: 'n', type: 'vec3' },
  40. { name: 'v', type: 'vec3' },
  41. { name: 'thickness', type: 'float' },
  42. { name: 'ior', type: 'float' },
  43. { name: 'modelMatrix', type: 'mat4' }
  44. ]
  45. } );
  46. const applyIorToRoughness = tslFn( ( [ roughness, ior ] ) => {
  47. // Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and
  48. // an IOR of 1.5 results in the default amount of microfacet refraction.
  49. return roughness.mul( clamp( ior.mul( 2.0 ).sub( 2.0 ), 0.0, 1.0 ) );
  50. } ).setLayout( {
  51. name: 'applyIorToRoughness',
  52. type: 'float',
  53. inputs: [
  54. { name: 'roughness', type: 'float' },
  55. { name: 'ior', type: 'float' }
  56. ]
  57. } );
  58. const singleViewportMipTexture = viewportMipTexture();
  59. const getTransmissionSample = tslFn( ( [ fragCoord, roughness, ior ] ) => {
  60. const transmissionSample = singleViewportMipTexture.uv( fragCoord );
  61. //const transmissionSample = viewportMipTexture( fragCoord );
  62. const lod = log2( float( viewportResolution.x ) ).mul( applyIorToRoughness( roughness, ior ) );
  63. return transmissionSample.bicubic( lod );
  64. } );
  65. const volumeAttenuation = tslFn( ( [ transmissionDistance, attenuationColor, attenuationDistance ] ) => {
  66. If( attenuationDistance.notEqual( 0 ), () => {
  67. // Compute light attenuation using Beer's law.
  68. const attenuationCoefficient = log( attenuationColor ).negate().div( attenuationDistance );
  69. const transmittance = exp( attenuationCoefficient.negate().mul( transmissionDistance ) );
  70. return transmittance;
  71. } );
  72. // Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.
  73. return vec3( 1.0 );
  74. } ).setLayout( {
  75. name: 'volumeAttenuation',
  76. type: 'vec3',
  77. inputs: [
  78. { name: 'transmissionDistance', type: 'float' },
  79. { name: 'attenuationColor', type: 'vec3' },
  80. { name: 'attenuationDistance', type: 'float' }
  81. ]
  82. } );
  83. const getIBLVolumeRefraction = tslFn( ( [ n, v, roughness, diffuseColor, specularColor, specularF90, position, modelMatrix, viewMatrix, projMatrix, ior, thickness, attenuationColor, attenuationDistance, dispersion ] ) => {
  84. let transmittedLight, transmittance;
  85. if ( dispersion ) {
  86. transmittedLight = vec4().toVar();
  87. transmittance = vec3().toVar();
  88. const halfSpread = ior.sub( 1.0 ).mul( dispersion.mul( 0.025 ) );
  89. const iors = vec3( ior.sub( halfSpread ), ior, ior.add( halfSpread ) );
  90. loop( { start: 0, end: 3 }, ( { i } ) => {
  91. const ior = iors.element( i );
  92. const transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
  93. const refractedRayExit = position.add( transmissionRay );
  94. // Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
  95. const ndcPos = projMatrix.mul( viewMatrix.mul( vec4( refractedRayExit, 1.0 ) ) );
  96. const refractionCoords = vec2( ndcPos.xy.div( ndcPos.w ) ).toVar();
  97. refractionCoords.addAssign( 1.0 );
  98. refractionCoords.divAssign( 2.0 );
  99. refractionCoords.assign( vec2( refractionCoords.x, refractionCoords.y.oneMinus() ) ); // webgpu
  100. // Sample framebuffer to get pixel the refracted ray hits.
  101. const transmissionSample = getTransmissionSample( refractionCoords, roughness, ior );
  102. transmittedLight.element( i ).assign( transmissionSample.element( i ) );
  103. transmittedLight.a.addAssign( transmissionSample.a );
  104. transmittance.element( i ).assign( diffuseColor.element( i ).mul( volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ).element( i ) ) );
  105. } );
  106. transmittedLight.a.divAssign( 3.0 );
  107. } else {
  108. const transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
  109. const refractedRayExit = position.add( transmissionRay );
  110. // Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
  111. const ndcPos = projMatrix.mul( viewMatrix.mul( vec4( refractedRayExit, 1.0 ) ) );
  112. const refractionCoords = vec2( ndcPos.xy.div( ndcPos.w ) ).toVar();
  113. refractionCoords.addAssign( 1.0 );
  114. refractionCoords.divAssign( 2.0 );
  115. refractionCoords.assign( vec2( refractionCoords.x, refractionCoords.y.oneMinus() ) ); // webgpu
  116. // Sample framebuffer to get pixel the refracted ray hits.
  117. transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );
  118. transmittance = diffuseColor.mul( volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ) );
  119. }
  120. const attenuatedColor = transmittance.rgb.mul( transmittedLight.rgb );
  121. const dotNV = n.dot( v ).clamp();
  122. // Get the specular component.
  123. const F = vec3( EnvironmentBRDF( { // n, v, specularColor, specularF90, roughness
  124. dotNV,
  125. specularColor,
  126. specularF90,
  127. roughness
  128. } ) );
  129. // As less light is transmitted, the opacity should be increased. This simple approximation does a decent job
  130. // of modulating a CSS background, and has no effect when the buffer is opaque, due to a solid object or clear color.
  131. const transmittanceFactor = transmittance.r.add( transmittance.g, transmittance.b ).div( 3.0 );
  132. return vec4( F.oneMinus().mul( attenuatedColor ), transmittedLight.a.oneMinus().mul( transmittanceFactor ).oneMinus() );
  133. } );
  134. //
  135. // Iridescence
  136. //
  137. // XYZ to linear-sRGB color space
  138. const XYZ_TO_REC709 = mat3(
  139. 3.2404542, - 0.9692660, 0.0556434,
  140. - 1.5371385, 1.8760108, - 0.2040259,
  141. - 0.4985314, 0.0415560, 1.0572252
  142. );
  143. // Assume air interface for top
  144. // Note: We don't handle the case fresnel0 == 1
  145. const Fresnel0ToIor = ( fresnel0 ) => {
  146. const sqrtF0 = fresnel0.sqrt();
  147. return vec3( 1.0 ).add( sqrtF0 ).div( vec3( 1.0 ).sub( sqrtF0 ) );
  148. };
  149. // ior is a value between 1.0 and 3.0. 1.0 is air interface
  150. const IorToFresnel0 = ( transmittedIor, incidentIor ) => {
  151. return transmittedIor.sub( incidentIor ).div( transmittedIor.add( incidentIor ) ).pow2();
  152. };
  153. // Fresnel equations for dielectric/dielectric interfaces.
  154. // Ref: https://belcour.github.io/blog/research/2017/05/01/brdf-thin-film.html
  155. // Evaluation XYZ sensitivity curves in Fourier space
  156. const evalSensitivity = ( OPD, shift ) => {
  157. const phase = OPD.mul( 2.0 * Math.PI * 1.0e-9 );
  158. const val = vec3( 5.4856e-13, 4.4201e-13, 5.2481e-13 );
  159. const pos = vec3( 1.6810e+06, 1.7953e+06, 2.2084e+06 );
  160. const VAR = vec3( 4.3278e+09, 9.3046e+09, 6.6121e+09 );
  161. 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() );
  162. 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() );
  163. xyz = vec3( xyz.x.add( x ), xyz.y, xyz.z ).div( 1.0685e-7 );
  164. const rgb = XYZ_TO_REC709.mul( xyz );
  165. return rgb;
  166. };
  167. const evalIridescence = tslFn( ( { outsideIOR, eta2, cosTheta1, thinFilmThickness, baseF0 } ) => {
  168. // Force iridescenceIOR -> outsideIOR when thinFilmThickness -> 0.0
  169. const iridescenceIOR = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) );
  170. // Evaluate the cosTheta on the base layer (Snell law)
  171. const sinTheta2Sq = outsideIOR.div( iridescenceIOR ).pow2().mul( float( 1 ).sub( cosTheta1.pow2() ) );
  172. // Handle TIR:
  173. const cosTheta2Sq = float( 1 ).sub( sinTheta2Sq );
  174. /*if ( cosTheta2Sq < 0.0 ) {
  175. return vec3( 1.0 );
  176. }*/
  177. const cosTheta2 = cosTheta2Sq.sqrt();
  178. // First interface
  179. const R0 = IorToFresnel0( iridescenceIOR, outsideIOR );
  180. const R12 = F_Schlick( { f0: R0, f90: 1.0, dotVH: cosTheta1 } );
  181. //const R21 = R12;
  182. const T121 = R12.oneMinus();
  183. const phi12 = iridescenceIOR.lessThan( outsideIOR ).cond( Math.PI, 0.0 );
  184. const phi21 = float( Math.PI ).sub( phi12 );
  185. // Second interface
  186. const baseIOR = Fresnel0ToIor( baseF0.clamp( 0.0, 0.9999 ) ); // guard against 1.0
  187. const R1 = IorToFresnel0( baseIOR, iridescenceIOR.toVec3() );
  188. const R23 = F_Schlick( { f0: R1, f90: 1.0, dotVH: cosTheta2 } );
  189. const phi23 = vec3(
  190. baseIOR.x.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 ),
  191. baseIOR.y.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 ),
  192. baseIOR.z.lessThan( iridescenceIOR ).cond( Math.PI, 0.0 )
  193. );
  194. // Phase shift
  195. const OPD = iridescenceIOR.mul( thinFilmThickness, cosTheta2, 2.0 );
  196. const phi = vec3( phi21 ).add( phi23 );
  197. // Compound terms
  198. const R123 = R12.mul( R23 ).clamp( 1e-5, 0.9999 );
  199. const r123 = R123.sqrt();
  200. const Rs = T121.pow2().mul( R23 ).div( vec3( 1.0 ).sub( R123 ) );
  201. // Reflectance term for m = 0 (DC term amplitude)
  202. const C0 = R12.add( Rs );
  203. let I = C0;
  204. // Reflectance term for m > 0 (pairs of diracs)
  205. let Cm = Rs.sub( T121 );
  206. for ( let m = 1; m <= 2; ++ m ) {
  207. Cm = Cm.mul( r123 );
  208. const Sm = evalSensitivity( float( m ).mul( OPD ), float( m ).mul( phi ) ).mul( 2.0 );
  209. I = I.add( Cm.mul( Sm ) );
  210. }
  211. // Since out of gamut colors might be produced, negative color values are clamped to 0.
  212. return I.max( vec3( 0.0 ) );
  213. } ).setLayout( {
  214. name: 'evalIridescence',
  215. type: 'vec3',
  216. inputs: [
  217. { name: 'outsideIOR', type: 'float' },
  218. { name: 'eta2', type: 'float' },
  219. { name: 'cosTheta1', type: 'float' },
  220. { name: 'thinFilmThickness', type: 'float' },
  221. { name: 'baseF0', type: 'vec3' }
  222. ]
  223. } );
  224. //
  225. // Sheen
  226. //
  227. // This is a curve-fit approxmation to the "Charlie sheen" BRDF integrated over the hemisphere from
  228. // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found
  229. // in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
  230. const IBLSheenBRDF = tslFn( ( { normal, viewDir, roughness } ) => {
  231. const dotNV = normal.dot( viewDir ).saturate();
  232. const r2 = roughness.pow2();
  233. const a = cond(
  234. roughness.lessThan( 0.25 ),
  235. float( - 339.2 ).mul( r2 ).add( float( 161.4 ).mul( roughness ) ).sub( 25.9 ),
  236. float( - 8.48 ).mul( r2 ).add( float( 14.3 ).mul( roughness ) ).sub( 9.95 )
  237. );
  238. const b = cond(
  239. roughness.lessThan( 0.25 ),
  240. float( 44.0 ).mul( r2 ).sub( float( 23.7 ).mul( roughness ) ).add( 3.26 ),
  241. float( 1.97 ).mul( r2 ).sub( float( 3.27 ).mul( roughness ) ).add( 0.72 )
  242. );
  243. const DG = cond( roughness.lessThan( 0.25 ), 0.0, float( 0.1 ).mul( roughness ).sub( 0.025 ) ).add( a.mul( dotNV ).add( b ).exp() );
  244. return DG.mul( 1.0 / Math.PI ).saturate();
  245. } );
  246. const clearcoatF0 = vec3( 0.04 );
  247. const clearcoatF90 = float( 1 );
  248. //
  249. class PhysicalLightingModel extends LightingModel {
  250. constructor( clearcoat = false, sheen = false, iridescence = false, anisotropy = false, transmission = false, dispersion = false ) {
  251. super();
  252. this.clearcoat = clearcoat;
  253. this.sheen = sheen;
  254. this.iridescence = iridescence;
  255. this.anisotropy = anisotropy;
  256. this.transmission = transmission;
  257. this.dispersion = dispersion;
  258. this.clearcoatRadiance = null;
  259. this.clearcoatSpecularDirect = null;
  260. this.clearcoatSpecularIndirect = null;
  261. this.sheenSpecularDirect = null;
  262. this.sheenSpecularIndirect = null;
  263. this.iridescenceFresnel = null;
  264. this.iridescenceF0 = null;
  265. }
  266. start( context ) {
  267. if ( this.clearcoat === true ) {
  268. this.clearcoatRadiance = vec3().temp( 'clearcoatRadiance' );
  269. this.clearcoatSpecularDirect = vec3().temp( 'clearcoatSpecularDirect' );
  270. this.clearcoatSpecularIndirect = vec3().temp( 'clearcoatSpecularIndirect' );
  271. }
  272. if ( this.sheen === true ) {
  273. this.sheenSpecularDirect = vec3().temp( 'sheenSpecularDirect' );
  274. this.sheenSpecularIndirect = vec3().temp( 'sheenSpecularIndirect' );
  275. }
  276. if ( this.iridescence === true ) {
  277. const dotNVi = transformedNormalView.dot( positionViewDirection ).clamp();
  278. this.iridescenceFresnel = evalIridescence( {
  279. outsideIOR: float( 1.0 ),
  280. eta2: iridescenceIOR,
  281. cosTheta1: dotNVi,
  282. thinFilmThickness: iridescenceThickness,
  283. baseF0: specularColor
  284. } );
  285. this.iridescenceF0 = Schlick_to_F0( { f: this.iridescenceFresnel, f90: 1.0, dotVH: dotNVi } );
  286. }
  287. if ( this.transmission === true ) {
  288. const position = positionWorld;
  289. const v = cameraPosition.sub( positionWorld ).normalize(); // TODO: Create Node for this, same issue in MaterialX
  290. const n = transformedNormalWorld;
  291. context.backdrop = getIBLVolumeRefraction(
  292. n,
  293. v,
  294. roughness,
  295. diffuseColor,
  296. specularColor,
  297. specularF90, // specularF90
  298. position, // positionWorld
  299. modelWorldMatrix, // modelMatrix
  300. cameraViewMatrix, // viewMatrix
  301. cameraProjectionMatrix, // projMatrix
  302. ior,
  303. thickness,
  304. attenuationColor,
  305. attenuationDistance,
  306. this.dispersion ? dispersion : null
  307. );
  308. context.backdropAlpha = transmission;
  309. diffuseColor.a.mulAssign( mix( 1, context.backdrop.a, transmission ) );
  310. }
  311. }
  312. // Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
  313. // Approximates multiscattering in order to preserve energy.
  314. // http://www.jcgt.org/published/0008/01/03/
  315. computeMultiscattering( singleScatter, multiScatter, specularF90 ) {
  316. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  317. const fab = DFGApprox( { roughness, dotNV } );
  318. const Fr = this.iridescenceF0 ? iridescence.mix( specularColor, this.iridescenceF0 ) : specularColor;
  319. const FssEss = Fr.mul( fab.x ).add( specularF90.mul( fab.y ) );
  320. const Ess = fab.x.add( fab.y );
  321. const Ems = Ess.oneMinus();
  322. const Favg = specularColor.add( specularColor.oneMinus().mul( 0.047619 ) ); // 1/21
  323. const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
  324. singleScatter.addAssign( FssEss );
  325. multiScatter.addAssign( Fms.mul( Ems ) );
  326. }
  327. direct( { lightDirection, lightColor, reflectedLight } ) {
  328. const dotNL = transformedNormalView.dot( lightDirection ).clamp();
  329. const irradiance = dotNL.mul( lightColor );
  330. if ( this.sheen === true ) {
  331. this.sheenSpecularDirect.addAssign( irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
  332. }
  333. if ( this.clearcoat === true ) {
  334. const dotNLcc = transformedClearcoatNormalView.dot( lightDirection ).clamp();
  335. const ccIrradiance = dotNLcc.mul( lightColor );
  336. this.clearcoatSpecularDirect.addAssign( ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: transformedClearcoatNormalView } ) ) );
  337. }
  338. reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  339. reflectedLight.directSpecular.addAssign( irradiance.mul( BRDF_GGX( { lightDirection, f0: specularColor, f90: 1, roughness, iridescence: this.iridescence, f: this.iridescenceFresnel, USE_IRIDESCENCE: this.iridescence, USE_ANISOTROPY: this.anisotropy } ) ) );
  340. }
  341. indirectDiffuse( { irradiance, reflectedLight } ) {
  342. reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
  343. }
  344. indirectSpecular( { radiance, iblIrradiance, reflectedLight } ) {
  345. if ( this.sheen === true ) {
  346. this.sheenSpecularIndirect.addAssign( iblIrradiance.mul(
  347. sheen,
  348. IBLSheenBRDF( {
  349. normal: transformedNormalView,
  350. viewDir: positionViewDirection,
  351. roughness: sheenRoughness
  352. } )
  353. ) );
  354. }
  355. if ( this.clearcoat === true ) {
  356. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  357. const clearcoatEnv = EnvironmentBRDF( {
  358. dotNV: dotNVcc,
  359. specularColor: clearcoatF0,
  360. specularF90: clearcoatF90,
  361. roughness: clearcoatRoughness
  362. } );
  363. this.clearcoatSpecularIndirect.addAssign( this.clearcoatRadiance.mul( clearcoatEnv ) );
  364. }
  365. // Both indirect specular and indirect diffuse light accumulate here
  366. const singleScattering = vec3().temp( 'singleScattering' );
  367. const multiScattering = vec3().temp( 'multiScattering' );
  368. const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
  369. this.computeMultiscattering( singleScattering, multiScattering, specularF90 );
  370. const totalScattering = singleScattering.add( multiScattering );
  371. const diffuse = diffuseColor.mul( totalScattering.r.max( totalScattering.g ).max( totalScattering.b ).oneMinus() );
  372. reflectedLight.indirectSpecular.addAssign( radiance.mul( singleScattering ) );
  373. reflectedLight.indirectSpecular.addAssign( multiScattering.mul( cosineWeightedIrradiance ) );
  374. reflectedLight.indirectDiffuse.addAssign( diffuse.mul( cosineWeightedIrradiance ) );
  375. }
  376. ambientOcclusion( { ambientOcclusion, reflectedLight } ) {
  377. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  378. const aoNV = dotNV.add( ambientOcclusion );
  379. const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
  380. const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
  381. if ( this.clearcoat === true ) {
  382. this.clearcoatSpecularIndirect.mulAssign( ambientOcclusion );
  383. }
  384. if ( this.sheen === true ) {
  385. this.sheenSpecularIndirect.mulAssign( ambientOcclusion );
  386. }
  387. reflectedLight.indirectDiffuse.mulAssign( ambientOcclusion );
  388. reflectedLight.indirectSpecular.mulAssign( aoNode );
  389. }
  390. finish( context ) {
  391. const { outgoingLight } = context;
  392. if ( this.clearcoat === true ) {
  393. const dotNVcc = transformedClearcoatNormalView.dot( positionViewDirection ).clamp();
  394. const Fcc = F_Schlick( {
  395. dotVH: dotNVcc,
  396. f0: clearcoatF0,
  397. f90: clearcoatF90
  398. } );
  399. const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( this.clearcoatSpecularDirect.add( this.clearcoatSpecularIndirect ).mul( clearcoat ) );
  400. outgoingLight.assign( clearcoatLight );
  401. }
  402. if ( this.sheen === true ) {
  403. const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( 0.157 ).oneMinus();
  404. const sheenLight = outgoingLight.mul( sheenEnergyComp ).add( this.sheenSpecularDirect, this.sheenSpecularIndirect );
  405. outgoingLight.assign( sheenLight );
  406. }
  407. }
  408. }
  409. export default PhysicalLightingModel;