PhysicalLightingModel.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
  2. import BRDF_GGX from './BSDF/BRDF_GGX.js';
  3. import BRDF_GGX_Multiscatter from './BSDF/BRDF_GGX_Multiscatter.js';
  4. import DFGApprox from './BSDF/DFGApprox.js';
  5. import EnvironmentBRDF from './BSDF/EnvironmentBRDF.js';
  6. import F_Schlick from './BSDF/F_Schlick.js';
  7. import Schlick_to_F0 from './BSDF/Schlick_to_F0.js';
  8. import BRDF_Sheen from './BSDF/BRDF_Sheen.js';
  9. import { LTC_Evaluate, LTC_Uv } from './BSDF/LTC.js';
  10. import LightingModel from '../core/LightingModel.js';
  11. import { diffuseColor, diffuseContribution, specularColor, specularColorBlended, specularF90, roughness, metalness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, ior, thickness, transmission, attenuationDistance, attenuationColor, dispersion } from '../core/PropertyNode.js';
  12. import { normalView, clearcoatNormalView, normalWorld } from '../accessors/Normal.js';
  13. import { positionViewDirection, positionView, positionWorld } from '../accessors/Position.js';
  14. import { Fn, float, vec2, vec3, vec4, mat3, If } from '../tsl/TSLBase.js';
  15. import { select } from '../math/ConditionalNode.js';
  16. import { mix, normalize, refract, length, clamp, log2, log, exp, smoothstep } from '../math/MathNode.js';
  17. import { div } from '../math/OperatorNode.js';
  18. import { cameraPosition, cameraProjectionMatrix, cameraViewMatrix } from '../accessors/Camera.js';
  19. import { modelWorldMatrix } from '../accessors/ModelNode.js';
  20. import { screenSize } from '../display/ScreenNode.js';
  21. import { viewportMipTexture } from '../display/ViewportTextureNode.js';
  22. import { textureBicubicLevel } from '../accessors/TextureBicubic.js';
  23. import { Loop } from '../utils/LoopNode.js';
  24. import { BackSide } from '../../constants.js';
  25. //
  26. // Transmission
  27. //
  28. const getVolumeTransmissionRay = /*@__PURE__*/ Fn( ( [ n, v, thickness, ior, modelMatrix ] ) => {
  29. // Direction of refracted light.
  30. const refractionVector = vec3( refract( v.negate(), normalize( n ), div( 1.0, ior ) ) );
  31. // Compute rotation-independent scaling of the model matrix.
  32. const modelScale = vec3(
  33. length( modelMatrix[ 0 ].xyz ),
  34. length( modelMatrix[ 1 ].xyz ),
  35. length( modelMatrix[ 2 ].xyz )
  36. );
  37. // The thickness is specified in local space.
  38. return normalize( refractionVector ).mul( thickness.mul( modelScale ) );
  39. } ).setLayout( {
  40. name: 'getVolumeTransmissionRay',
  41. type: 'vec3',
  42. inputs: [
  43. { name: 'n', type: 'vec3' },
  44. { name: 'v', type: 'vec3' },
  45. { name: 'thickness', type: 'float' },
  46. { name: 'ior', type: 'float' },
  47. { name: 'modelMatrix', type: 'mat4' }
  48. ]
  49. } );
  50. const applyIorToRoughness = /*@__PURE__*/ Fn( ( [ roughness, ior ] ) => {
  51. // Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and
  52. // an IOR of 1.5 results in the default amount of microfacet refraction.
  53. return roughness.mul( clamp( ior.mul( 2.0 ).sub( 2.0 ), 0.0, 1.0 ) );
  54. } ).setLayout( {
  55. name: 'applyIorToRoughness',
  56. type: 'float',
  57. inputs: [
  58. { name: 'roughness', type: 'float' },
  59. { name: 'ior', type: 'float' }
  60. ]
  61. } );
  62. const viewportBackSideTexture = /*@__PURE__*/ viewportMipTexture();
  63. const viewportFrontSideTexture = /*@__PURE__*/ viewportMipTexture();
  64. const getTransmissionSample = /*@__PURE__*/ Fn( ( [ fragCoord, roughness, ior ], { material } ) => {
  65. const vTexture = material.side === BackSide ? viewportBackSideTexture : viewportFrontSideTexture;
  66. const transmissionSample = vTexture.sample( fragCoord );
  67. //const transmissionSample = viewportMipTexture( fragCoord );
  68. const lod = log2( screenSize.x ).mul( applyIorToRoughness( roughness, ior ) );
  69. return textureBicubicLevel( transmissionSample, lod );
  70. } );
  71. const volumeAttenuation = /*@__PURE__*/ Fn( ( [ transmissionDistance, attenuationColor, attenuationDistance ] ) => {
  72. If( attenuationDistance.notEqual( 0 ), () => {
  73. // Compute light attenuation using Beer's law.
  74. const attenuationCoefficient = log( attenuationColor ).negate().div( attenuationDistance );
  75. const transmittance = exp( attenuationCoefficient.negate().mul( transmissionDistance ) );
  76. return transmittance;
  77. } );
  78. // Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.
  79. return vec3( 1.0 );
  80. } ).setLayout( {
  81. name: 'volumeAttenuation',
  82. type: 'vec3',
  83. inputs: [
  84. { name: 'transmissionDistance', type: 'float' },
  85. { name: 'attenuationColor', type: 'vec3' },
  86. { name: 'attenuationDistance', type: 'float' }
  87. ]
  88. } );
  89. const getIBLVolumeRefraction = /*@__PURE__*/ Fn( ( [ n, v, roughness, diffuseColor, specularColor, specularF90, position, modelMatrix, viewMatrix, projMatrix, ior, thickness, attenuationColor, attenuationDistance, dispersion ] ) => {
  90. let transmittedLight, transmittance;
  91. if ( dispersion ) {
  92. transmittedLight = vec4().toVar();
  93. transmittance = vec3().toVar();
  94. const halfSpread = ior.sub( 1.0 ).mul( dispersion.mul( 0.025 ) );
  95. const iors = vec3( ior.sub( halfSpread ), ior, ior.add( halfSpread ) );
  96. Loop( { start: 0, end: 3 }, ( { i } ) => {
  97. const ior = iors.element( i );
  98. const transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
  99. const refractedRayExit = position.add( transmissionRay );
  100. // Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
  101. const ndcPos = projMatrix.mul( viewMatrix.mul( vec4( refractedRayExit, 1.0 ) ) );
  102. const refractionCoords = vec2( ndcPos.xy.div( ndcPos.w ) ).toVar();
  103. refractionCoords.addAssign( 1.0 );
  104. refractionCoords.divAssign( 2.0 );
  105. refractionCoords.assign( vec2( refractionCoords.x, refractionCoords.y.oneMinus() ) ); // webgpu
  106. // Sample framebuffer to get pixel the refracted ray hits.
  107. const transmissionSample = getTransmissionSample( refractionCoords, roughness, ior );
  108. transmittedLight.element( i ).assign( transmissionSample.element( i ) );
  109. transmittedLight.a.addAssign( transmissionSample.a );
  110. transmittance.element( i ).assign( diffuseColor.element( i ).mul( volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ).element( i ) ) );
  111. } );
  112. transmittedLight.a.divAssign( 3.0 );
  113. } else {
  114. const transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
  115. const refractedRayExit = position.add( transmissionRay );
  116. // Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
  117. const ndcPos = projMatrix.mul( viewMatrix.mul( vec4( refractedRayExit, 1.0 ) ) );
  118. const refractionCoords = vec2( ndcPos.xy.div( ndcPos.w ) ).toVar();
  119. refractionCoords.addAssign( 1.0 );
  120. refractionCoords.divAssign( 2.0 );
  121. refractionCoords.assign( vec2( refractionCoords.x, refractionCoords.y.oneMinus() ) ); // webgpu
  122. // Sample framebuffer to get pixel the refracted ray hits.
  123. transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );
  124. transmittance = diffuseColor.mul( volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ) );
  125. }
  126. const attenuatedColor = transmittance.rgb.mul( transmittedLight.rgb );
  127. const dotNV = n.dot( v ).clamp();
  128. // Get the specular component.
  129. const F = vec3( EnvironmentBRDF( { // n, v, specularColor, specularF90, roughness
  130. dotNV,
  131. specularColor,
  132. specularF90,
  133. roughness
  134. } ) );
  135. // As less light is transmitted, the opacity should be increased. This simple approximation does a decent job
  136. // of modulating a CSS background, and has no effect when the buffer is opaque, due to a solid object or clear color.
  137. const transmittanceFactor = transmittance.r.add( transmittance.g, transmittance.b ).div( 3.0 );
  138. return vec4( F.oneMinus().mul( attenuatedColor ), transmittedLight.a.oneMinus().mul( transmittanceFactor ).oneMinus() );
  139. } );
  140. //
  141. // Iridescence
  142. //
  143. // XYZ to linear-sRGB color space
  144. const XYZ_TO_REC709 = /*@__PURE__*/ mat3(
  145. 3.2404542, - 0.9692660, 0.0556434,
  146. - 1.5371385, 1.8760108, - 0.2040259,
  147. - 0.4985314, 0.0415560, 1.0572252
  148. );
  149. // Assume air interface for top
  150. // Note: We don't handle the case fresnel0 == 1
  151. const Fresnel0ToIor = ( fresnel0 ) => {
  152. const sqrtF0 = fresnel0.sqrt();
  153. return vec3( 1.0 ).add( sqrtF0 ).div( vec3( 1.0 ).sub( sqrtF0 ) );
  154. };
  155. // ior is a value between 1.0 and 3.0. 1.0 is air interface
  156. const IorToFresnel0 = ( transmittedIor, incidentIor ) => {
  157. return transmittedIor.sub( incidentIor ).div( transmittedIor.add( incidentIor ) ).pow2();
  158. };
  159. // Fresnel equations for dielectric/dielectric interfaces.
  160. // Ref: https://belcour.github.io/blog/research/2017/05/01/brdf-thin-film.html
  161. // Evaluation XYZ sensitivity curves in Fourier space
  162. const evalSensitivity = ( OPD, shift ) => {
  163. const phase = OPD.mul( 2.0 * Math.PI * 1.0e-9 );
  164. const val = vec3( 5.4856e-13, 4.4201e-13, 5.2481e-13 );
  165. const pos = vec3( 1.6810e+06, 1.7953e+06, 2.2084e+06 );
  166. const VAR = vec3( 4.3278e+09, 9.3046e+09, 6.6121e+09 );
  167. 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() );
  168. 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() );
  169. xyz = vec3( xyz.x.add( x ), xyz.y, xyz.z ).div( 1.0685e-7 );
  170. const rgb = XYZ_TO_REC709.mul( xyz );
  171. return rgb;
  172. };
  173. const evalIridescence = /*@__PURE__*/ Fn( ( { outsideIOR, eta2, cosTheta1, thinFilmThickness, baseF0 } ) => {
  174. // Force iridescenceIOR -> outsideIOR when thinFilmThickness -> 0.0
  175. const iridescenceIOR = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) );
  176. // Evaluate the cosTheta on the base layer (Snell law)
  177. const sinTheta2Sq = outsideIOR.div( iridescenceIOR ).pow2().mul( cosTheta1.pow2().oneMinus() );
  178. // Handle TIR:
  179. const cosTheta2Sq = sinTheta2Sq.oneMinus();
  180. If( cosTheta2Sq.lessThan( 0 ), () => {
  181. return vec3( 1.0 );
  182. } );
  183. const cosTheta2 = cosTheta2Sq.sqrt();
  184. // First interface
  185. const R0 = IorToFresnel0( iridescenceIOR, outsideIOR );
  186. const R12 = F_Schlick( { f0: R0, f90: 1.0, dotVH: cosTheta1 } );
  187. //const R21 = R12;
  188. const T121 = R12.oneMinus();
  189. const phi12 = iridescenceIOR.lessThan( outsideIOR ).select( Math.PI, 0.0 );
  190. const phi21 = float( Math.PI ).sub( phi12 );
  191. // Second interface
  192. const baseIOR = Fresnel0ToIor( baseF0.clamp( 0.0, 0.9999 ) ); // guard against 1.0
  193. const R1 = IorToFresnel0( baseIOR, iridescenceIOR.toVec3() );
  194. const R23 = F_Schlick( { f0: R1, f90: 1.0, dotVH: cosTheta2 } );
  195. const phi23 = vec3(
  196. baseIOR.x.lessThan( iridescenceIOR ).select( Math.PI, 0.0 ),
  197. baseIOR.y.lessThan( iridescenceIOR ).select( Math.PI, 0.0 ),
  198. baseIOR.z.lessThan( iridescenceIOR ).select( Math.PI, 0.0 )
  199. );
  200. // Phase shift
  201. const OPD = iridescenceIOR.mul( thinFilmThickness, cosTheta2, 2.0 );
  202. const phi = vec3( phi21 ).add( phi23 );
  203. // Compound terms
  204. const R123 = R12.mul( R23 ).clamp( 1e-5, 0.9999 );
  205. const r123 = R123.sqrt();
  206. const Rs = T121.pow2().mul( R23 ).div( vec3( 1.0 ).sub( R123 ) );
  207. // Reflectance term for m = 0 (DC term amplitude)
  208. const C0 = R12.add( Rs );
  209. const I = C0.toVar();
  210. // Reflectance term for m > 0 (pairs of diracs)
  211. const Cm = Rs.sub( T121 ).toVar();
  212. Loop( { start: 1, end: 2, condition: '<=', name: 'm' }, ( { m } ) => {
  213. Cm.mulAssign( r123 );
  214. const Sm = evalSensitivity( float( m ).mul( OPD ), float( m ).mul( phi ) ).mul( 2.0 );
  215. I.addAssign( Cm.mul( Sm ) );
  216. } );
  217. // Since out of gamut colors might be produced, negative color values are clamped to 0.
  218. return I.max( vec3( 0.0 ) );
  219. } ).setLayout( {
  220. name: 'evalIridescence',
  221. type: 'vec3',
  222. inputs: [
  223. { name: 'outsideIOR', type: 'float' },
  224. { name: 'eta2', type: 'float' },
  225. { name: 'cosTheta1', type: 'float' },
  226. { name: 'thinFilmThickness', type: 'float' },
  227. { name: 'baseF0', type: 'vec3' }
  228. ]
  229. } );
  230. //
  231. // Sheen
  232. //
  233. // This is a curve-fit approximation to the "Charlie sheen" BRDF integrated over the hemisphere from
  234. // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found
  235. // in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
  236. const IBLSheenBRDF = /*@__PURE__*/ Fn( ( { normal, viewDir, roughness } ) => {
  237. const dotNV = normal.dot( viewDir ).saturate();
  238. const r2 = roughness.pow2();
  239. const a = select(
  240. roughness.lessThan( 0.25 ),
  241. float( - 339.2 ).mul( r2 ).add( float( 161.4 ).mul( roughness ) ).sub( 25.9 ),
  242. float( - 8.48 ).mul( r2 ).add( float( 14.3 ).mul( roughness ) ).sub( 9.95 )
  243. );
  244. const b = select(
  245. roughness.lessThan( 0.25 ),
  246. float( 44.0 ).mul( r2 ).sub( float( 23.7 ).mul( roughness ) ).add( 3.26 ),
  247. float( 1.97 ).mul( r2 ).sub( float( 3.27 ).mul( roughness ) ).add( 0.72 )
  248. );
  249. const DG = select( roughness.lessThan( 0.25 ), 0.0, float( 0.1 ).mul( roughness ).sub( 0.025 ) ).add( a.mul( dotNV ).add( b ).exp() );
  250. return DG.mul( 1.0 / Math.PI ).saturate();
  251. } );
  252. const clearcoatF0 = vec3( 0.04 );
  253. const clearcoatF90 = float( 1 );
  254. /**
  255. * Represents the lighting model for a PBR material.
  256. *
  257. * @augments LightingModel
  258. */
  259. class PhysicalLightingModel extends LightingModel {
  260. /**
  261. * Constructs a new physical lighting model.
  262. *
  263. * @param {boolean} [clearcoat=false] - Whether clearcoat is supported or not.
  264. * @param {boolean} [sheen=false] - Whether sheen is supported or not.
  265. * @param {boolean} [iridescence=false] - Whether iridescence is supported or not.
  266. * @param {boolean} [anisotropy=false] - Whether anisotropy is supported or not.
  267. * @param {boolean} [transmission=false] - Whether transmission is supported or not.
  268. * @param {boolean} [dispersion=false] - Whether dispersion is supported or not.
  269. */
  270. constructor( clearcoat = false, sheen = false, iridescence = false, anisotropy = false, transmission = false, dispersion = false ) {
  271. super();
  272. /**
  273. * Whether clearcoat is supported or not.
  274. *
  275. * @type {boolean}
  276. * @default false
  277. */
  278. this.clearcoat = clearcoat;
  279. /**
  280. * Whether sheen is supported or not.
  281. *
  282. * @type {boolean}
  283. * @default false
  284. */
  285. this.sheen = sheen;
  286. /**
  287. * Whether iridescence is supported or not.
  288. *
  289. * @type {boolean}
  290. * @default false
  291. */
  292. this.iridescence = iridescence;
  293. /**
  294. * Whether anisotropy is supported or not.
  295. *
  296. * @type {boolean}
  297. * @default false
  298. */
  299. this.anisotropy = anisotropy;
  300. /**
  301. * Whether transmission is supported or not.
  302. *
  303. * @type {boolean}
  304. * @default false
  305. */
  306. this.transmission = transmission;
  307. /**
  308. * Whether dispersion is supported or not.
  309. *
  310. * @type {boolean}
  311. * @default false
  312. */
  313. this.dispersion = dispersion;
  314. /**
  315. * The clear coat radiance.
  316. *
  317. * @type {?Node}
  318. * @default null
  319. */
  320. this.clearcoatRadiance = null;
  321. /**
  322. * The clear coat specular direct.
  323. *
  324. * @type {?Node}
  325. * @default null
  326. */
  327. this.clearcoatSpecularDirect = null;
  328. /**
  329. * The clear coat specular indirect.
  330. *
  331. * @type {?Node}
  332. * @default null
  333. */
  334. this.clearcoatSpecularIndirect = null;
  335. /**
  336. * The sheen specular direct.
  337. *
  338. * @type {?Node}
  339. * @default null
  340. */
  341. this.sheenSpecularDirect = null;
  342. /**
  343. * The sheen specular indirect.
  344. *
  345. * @type {?Node}
  346. * @default null
  347. */
  348. this.sheenSpecularIndirect = null;
  349. /**
  350. * The iridescence Fresnel.
  351. *
  352. * @type {?Node}
  353. * @default null
  354. */
  355. this.iridescenceFresnel = null;
  356. /**
  357. * The iridescence F0.
  358. *
  359. * @type {?Node}
  360. * @default null
  361. */
  362. this.iridescenceF0 = null;
  363. /**
  364. * The iridescence F0 dielectric.
  365. *
  366. * @type {?Node}
  367. * @default null
  368. */
  369. this.iridescenceF0Dielectric = null;
  370. /**
  371. * The iridescence F0 metallic.
  372. *
  373. * @type {?Node}
  374. * @default null
  375. */
  376. this.iridescenceF0Metallic = null;
  377. }
  378. /**
  379. * Depending on what features are requested, the method prepares certain node variables
  380. * which are later used for lighting computations.
  381. *
  382. * @param {NodeBuilder} builder - The current node builder.
  383. */
  384. start( builder ) {
  385. if ( this.clearcoat === true ) {
  386. this.clearcoatRadiance = vec3().toVar( 'clearcoatRadiance' );
  387. this.clearcoatSpecularDirect = vec3().toVar( 'clearcoatSpecularDirect' );
  388. this.clearcoatSpecularIndirect = vec3().toVar( 'clearcoatSpecularIndirect' );
  389. }
  390. if ( this.sheen === true ) {
  391. this.sheenSpecularDirect = vec3().toVar( 'sheenSpecularDirect' );
  392. this.sheenSpecularIndirect = vec3().toVar( 'sheenSpecularIndirect' );
  393. }
  394. if ( this.iridescence === true ) {
  395. const dotNVi = normalView.dot( positionViewDirection ).clamp();
  396. const iridescenceFresnelDielectric = evalIridescence( {
  397. outsideIOR: float( 1.0 ),
  398. eta2: iridescenceIOR,
  399. cosTheta1: dotNVi,
  400. thinFilmThickness: iridescenceThickness,
  401. baseF0: specularColor
  402. } );
  403. const iridescenceFresnelMetallic = evalIridescence( {
  404. outsideIOR: float( 1.0 ),
  405. eta2: iridescenceIOR,
  406. cosTheta1: dotNVi,
  407. thinFilmThickness: iridescenceThickness,
  408. baseF0: diffuseColor.rgb
  409. } );
  410. this.iridescenceFresnel = mix( iridescenceFresnelDielectric, iridescenceFresnelMetallic, metalness );
  411. this.iridescenceF0Dielectric = Schlick_to_F0( { f: iridescenceFresnelDielectric, f90: 1.0, dotVH: dotNVi } );
  412. this.iridescenceF0Metallic = Schlick_to_F0( { f: iridescenceFresnelMetallic, f90: 1.0, dotVH: dotNVi } );
  413. this.iridescenceF0 = mix( this.iridescenceF0Dielectric, this.iridescenceF0Metallic, metalness );
  414. }
  415. if ( this.transmission === true ) {
  416. const position = positionWorld;
  417. const v = cameraPosition.sub( positionWorld ).normalize(); // TODO: Create Node for this, same issue in MaterialX
  418. const n = normalWorld;
  419. const context = builder.context;
  420. context.backdrop = getIBLVolumeRefraction(
  421. n,
  422. v,
  423. roughness,
  424. diffuseContribution,
  425. specularColorBlended,
  426. specularF90, // specularF90
  427. position, // positionWorld
  428. modelWorldMatrix, // modelMatrix
  429. cameraViewMatrix, // viewMatrix
  430. cameraProjectionMatrix, // projMatrix
  431. ior,
  432. thickness,
  433. attenuationColor,
  434. attenuationDistance,
  435. this.dispersion ? dispersion : null
  436. );
  437. context.backdropAlpha = transmission;
  438. diffuseColor.a.mulAssign( mix( 1, context.backdrop.a, transmission ) );
  439. }
  440. super.start( builder );
  441. }
  442. // Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
  443. // Approximates multi-scattering in order to preserve energy.
  444. // http://www.jcgt.org/published/0008/01/03/
  445. computeMultiscattering( singleScatter, multiScatter, specularF90, f0, iridescenceF0 = null ) {
  446. const dotNV = normalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  447. const fab = DFGApprox( { roughness, dotNV } );
  448. const Fr = iridescenceF0 ? iridescence.mix( f0, iridescenceF0 ) : f0;
  449. const FssEss = Fr.mul( fab.x ).add( specularF90.mul( fab.y ) );
  450. const Ess = fab.x.add( fab.y );
  451. const Ems = Ess.oneMinus();
  452. const Favg = Fr.add( Fr.oneMinus().mul( 0.047619 ) ); // 1/21
  453. const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
  454. singleScatter.addAssign( FssEss );
  455. multiScatter.addAssign( Fms.mul( Ems ) );
  456. }
  457. /**
  458. * Implements the direct light.
  459. *
  460. * @param {Object} lightData - The light data.
  461. * @param {NodeBuilder} builder - The current node builder.
  462. */
  463. direct( { lightDirection, lightColor, reflectedLight }, /* builder */ ) {
  464. const dotNL = normalView.dot( lightDirection ).clamp();
  465. const irradiance = dotNL.mul( lightColor );
  466. if ( this.sheen === true ) {
  467. this.sheenSpecularDirect.addAssign( irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
  468. }
  469. if ( this.clearcoat === true ) {
  470. const dotNLcc = clearcoatNormalView.dot( lightDirection ).clamp();
  471. const ccIrradiance = dotNLcc.mul( lightColor );
  472. this.clearcoatSpecularDirect.addAssign( ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: clearcoatNormalView } ) ) );
  473. }
  474. reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseContribution } ) ) );
  475. reflectedLight.directSpecular.addAssign( irradiance.mul( BRDF_GGX_Multiscatter( { lightDirection, f0: specularColorBlended, f90: 1, roughness, f: this.iridescenceFresnel, USE_IRIDESCENCE: this.iridescence, USE_ANISOTROPY: this.anisotropy } ) ) );
  476. }
  477. /**
  478. * This method is intended for implementing the direct light term for
  479. * rect area light nodes.
  480. *
  481. * @param {Object} input - The input data.
  482. * @param {NodeBuilder} builder - The current node builder.
  483. */
  484. directRectArea( { lightColor, lightPosition, halfWidth, halfHeight, reflectedLight, ltc_1, ltc_2 }, /* builder */ ) {
  485. const p0 = lightPosition.add( halfWidth ).sub( halfHeight ); // counterclockwise; light shines in local neg z direction
  486. const p1 = lightPosition.sub( halfWidth ).sub( halfHeight );
  487. const p2 = lightPosition.sub( halfWidth ).add( halfHeight );
  488. const p3 = lightPosition.add( halfWidth ).add( halfHeight );
  489. const N = normalView;
  490. const V = positionViewDirection;
  491. const P = positionView.toVar();
  492. const uv = LTC_Uv( { N, V, roughness } );
  493. const t1 = ltc_1.sample( uv ).toVar();
  494. const t2 = ltc_2.sample( uv ).toVar();
  495. const mInv = mat3(
  496. vec3( t1.x, 0, t1.y ),
  497. vec3( 0, 1, 0 ),
  498. vec3( t1.z, 0, t1.w )
  499. ).toVar();
  500. // LTC Fresnel Approximation by Stephen Hill
  501. // http://blog.selfshadow.com/publications/s2016-advances/s2016_ltc_fresnel.pdf
  502. const fresnel = specularColorBlended.mul( t2.x ).add( specularColorBlended.oneMinus().mul( t2.y ) ).toVar();
  503. reflectedLight.directSpecular.addAssign( lightColor.mul( fresnel ).mul( LTC_Evaluate( { N, V, P, mInv, p0, p1, p2, p3 } ) ) );
  504. reflectedLight.directDiffuse.addAssign( lightColor.mul( diffuseContribution ).mul( LTC_Evaluate( { N, V, P, mInv: mat3( 1, 0, 0, 0, 1, 0, 0, 0, 1 ), p0, p1, p2, p3 } ) ) );
  505. }
  506. /**
  507. * Implements the indirect lighting.
  508. *
  509. * @param {NodeBuilder} builder - The current node builder.
  510. */
  511. indirect( builder ) {
  512. this.indirectDiffuse( builder );
  513. this.indirectSpecular( builder );
  514. this.ambientOcclusion( builder );
  515. }
  516. /**
  517. * Implements the indirect diffuse term.
  518. *
  519. * @param {NodeBuilder} builder - The current node builder.
  520. */
  521. indirectDiffuse( builder ) {
  522. const { irradiance, reflectedLight } = builder.context;
  523. reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseContribution } ) ) );
  524. }
  525. /**
  526. * Implements the indirect specular term.
  527. *
  528. * @param {NodeBuilder} builder - The current node builder.
  529. */
  530. indirectSpecular( builder ) {
  531. const { radiance, iblIrradiance, reflectedLight } = builder.context;
  532. if ( this.sheen === true ) {
  533. this.sheenSpecularIndirect.addAssign( iblIrradiance.mul(
  534. sheen,
  535. IBLSheenBRDF( {
  536. normal: normalView,
  537. viewDir: positionViewDirection,
  538. roughness: sheenRoughness
  539. } )
  540. ) );
  541. }
  542. if ( this.clearcoat === true ) {
  543. const dotNVcc = clearcoatNormalView.dot( positionViewDirection ).clamp();
  544. const clearcoatEnv = EnvironmentBRDF( {
  545. dotNV: dotNVcc,
  546. specularColor: clearcoatF0,
  547. specularF90: clearcoatF90,
  548. roughness: clearcoatRoughness
  549. } );
  550. this.clearcoatSpecularIndirect.addAssign( this.clearcoatRadiance.mul( clearcoatEnv ) );
  551. }
  552. // Both indirect specular and indirect diffuse light accumulate here
  553. // Compute multiscattering separately for dielectric and metallic, then mix
  554. const singleScatteringDielectric = vec3().toVar( 'singleScatteringDielectric' );
  555. const multiScatteringDielectric = vec3().toVar( 'multiScatteringDielectric' );
  556. const singleScatteringMetallic = vec3().toVar( 'singleScatteringMetallic' );
  557. const multiScatteringMetallic = vec3().toVar( 'multiScatteringMetallic' );
  558. this.computeMultiscattering( singleScatteringDielectric, multiScatteringDielectric, specularF90, specularColor, this.iridescenceF0Dielectric );
  559. this.computeMultiscattering( singleScatteringMetallic, multiScatteringMetallic, specularF90, diffuseColor.rgb, this.iridescenceF0Metallic );
  560. // Mix based on metalness
  561. const singleScattering = mix( singleScatteringDielectric, singleScatteringMetallic, metalness );
  562. const multiScattering = mix( multiScatteringDielectric, multiScatteringMetallic, metalness );
  563. // Diffuse energy conservation uses dielectric path
  564. const totalScatteringDielectric = singleScatteringDielectric.add( multiScatteringDielectric );
  565. const diffuse = diffuseContribution.mul( totalScatteringDielectric.r.max( totalScatteringDielectric.g ).max( totalScatteringDielectric.b ).oneMinus() );
  566. const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
  567. reflectedLight.indirectSpecular.addAssign( radiance.mul( singleScattering ) );
  568. reflectedLight.indirectSpecular.addAssign( multiScattering.mul( cosineWeightedIrradiance ) );
  569. reflectedLight.indirectDiffuse.addAssign( diffuse.mul( cosineWeightedIrradiance ) );
  570. }
  571. /**
  572. * Implements the ambient occlusion term.
  573. *
  574. * @param {NodeBuilder} builder - The current node builder.
  575. */
  576. ambientOcclusion( builder ) {
  577. const { ambientOcclusion, reflectedLight } = builder.context;
  578. const dotNV = normalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
  579. const aoNV = dotNV.add( ambientOcclusion );
  580. const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
  581. const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
  582. if ( this.clearcoat === true ) {
  583. this.clearcoatSpecularIndirect.mulAssign( ambientOcclusion );
  584. }
  585. if ( this.sheen === true ) {
  586. this.sheenSpecularIndirect.mulAssign( ambientOcclusion );
  587. }
  588. reflectedLight.indirectDiffuse.mulAssign( ambientOcclusion );
  589. reflectedLight.indirectSpecular.mulAssign( aoNode );
  590. }
  591. /**
  592. * Used for final lighting accumulations depending on the requested features.
  593. *
  594. * @param {NodeBuilder} builder - The current node builder.
  595. */
  596. finish( { context } ) {
  597. const { outgoingLight } = context;
  598. if ( this.clearcoat === true ) {
  599. const dotNVcc = clearcoatNormalView.dot( positionViewDirection ).clamp();
  600. const Fcc = F_Schlick( {
  601. dotVH: dotNVcc,
  602. f0: clearcoatF0,
  603. f90: clearcoatF90
  604. } );
  605. const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( this.clearcoatSpecularDirect.add( this.clearcoatSpecularIndirect ).mul( clearcoat ) );
  606. outgoingLight.assign( clearcoatLight );
  607. }
  608. if ( this.sheen === true ) {
  609. const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( 0.157 ).oneMinus();
  610. const sheenLight = outgoingLight.mul( sheenEnergyComp ).add( this.sheenSpecularDirect, this.sheenSpecularIndirect );
  611. outgoingLight.assign( sheenLight );
  612. }
  613. }
  614. }
  615. export default PhysicalLightingModel;
粤ICP备19079148号