-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathPhysicalLightingModel.js
More file actions
936 lines (680 loc) · 29.9 KB
/
Copy pathPhysicalLightingModel.js
File metadata and controls
936 lines (680 loc) · 29.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
import BRDF_GGX from './BSDF/BRDF_GGX.js';
import DFGLUT from './BSDF/DFGLUT.js';
import EnvironmentBRDF from './BSDF/EnvironmentBRDF.js';
import F_Schlick from './BSDF/F_Schlick.js';
import Schlick_to_F0 from './BSDF/Schlick_to_F0.js';
import BRDF_Sheen from './BSDF/BRDF_Sheen.js';
import { LTC_Evaluate, LTC_Uv } from './BSDF/LTC.js';
import LightingModel from '../core/LightingModel.js';
import { diffuseColor, diffuseContribution, specularColor, specularColorBlended, specularF90, roughness, metalness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, ior, thickness, transmission, attenuationDistance, attenuationColor, dispersion, retroreflectivity } from '../core/PropertyNode.js';
import { normalView, clearcoatNormalView, normalWorld } from '../accessors/Normal.js';
import { positionViewDirection, positionView, positionWorld } from '../accessors/Position.js';
import { Fn, float, vec2, vec3, vec4, mat3, If } from '../tsl/TSLBase.js';
import { mix, normalize, refract, length, clamp, log2, log, exp, smoothstep } from '../math/MathNode.js';
import { div } from '../math/OperatorNode.js';
import { cameraPosition, cameraProjectionMatrix, cameraViewMatrix } from '../accessors/Camera.js';
import { modelWorldMatrix } from '../accessors/ModelNode.js';
import { screenSize } from '../display/ScreenNode.js';
import { viewportMipTexture, viewportOpaqueMipTexture } from '../display/ViewportTextureNode.js';
import { textureBicubicLevel } from '../accessors/TextureBicubic.js';
import { Loop } from '../utils/LoopNode.js';
import { BackSide } from '../../constants.js';
//
// Transmission
//
const getVolumeTransmissionRay = /*@__PURE__*/ Fn( ( [ n, v, thickness, ior, modelMatrix ] ) => {
// Direction of refracted light.
const refractionVector = vec3( refract( v.negate(), normalize( n ), div( 1.0, ior ) ) );
// Compute rotation-independent scaling of the model matrix.
const modelScale = vec3(
length( modelMatrix[ 0 ].xyz ),
length( modelMatrix[ 1 ].xyz ),
length( modelMatrix[ 2 ].xyz )
);
// The thickness is specified in local space.
return normalize( refractionVector ).mul( thickness.mul( modelScale ) );
} ).setLayout( {
name: 'getVolumeTransmissionRay',
type: 'vec3',
inputs: [
{ name: 'n', type: 'vec3' },
{ name: 'v', type: 'vec3' },
{ name: 'thickness', type: 'float' },
{ name: 'ior', type: 'float' },
{ name: 'modelMatrix', type: 'mat4' }
]
} );
const applyIorToRoughness = /*@__PURE__*/ Fn( ( [ roughness, ior ] ) => {
// Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and
// an IOR of 1.5 results in the default amount of microfacet refraction.
return roughness.mul( clamp( ior.mul( 2.0 ).sub( 2.0 ), 0.0, 1.0 ) );
} ).setLayout( {
name: 'applyIorToRoughness',
type: 'float',
inputs: [
{ name: 'roughness', type: 'float' },
{ name: 'ior', type: 'float' }
]
} );
const viewportBackSideTexture = /*@__PURE__*/ viewportMipTexture();
const viewportFrontSideTexture = /*@__PURE__*/ viewportOpaqueMipTexture();
const getTransmissionSample = /*@__PURE__*/ Fn( ( [ fragCoord, roughness, ior ], { materialSide } ) => {
const vTexture = materialSide === BackSide ? viewportBackSideTexture : viewportFrontSideTexture;
const transmissionSample = vTexture.sample( fragCoord );
//const transmissionSample = viewportMipTexture( fragCoord );
const lod = log2( screenSize.x ).mul( applyIorToRoughness( roughness, ior ) );
return textureBicubicLevel( transmissionSample, lod );
} );
const volumeAttenuation = /*@__PURE__*/ Fn( ( [ transmissionDistance, attenuationColor, attenuationDistance ] ) => {
If( attenuationDistance.notEqual( 0 ), () => {
// Compute light attenuation using Beer's law.
const attenuationCoefficient = log( attenuationColor ).negate().div( attenuationDistance );
const transmittance = exp( attenuationCoefficient.negate().mul( transmissionDistance ) );
return transmittance;
} );
// Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.
return vec3( 1.0 );
} ).setLayout( {
name: 'volumeAttenuation',
type: 'vec3',
inputs: [
{ name: 'transmissionDistance', type: 'float' },
{ name: 'attenuationColor', type: 'vec3' },
{ name: 'attenuationDistance', type: 'float' }
]
} );
const getIBLVolumeRefraction = /*@__PURE__*/ Fn( ( [ n, v, roughness, diffuseColor, specularColor, specularF90, position, modelMatrix, viewMatrix, projMatrix, ior, thickness, attenuationColor, attenuationDistance, dispersion ] ) => {
let transmittedLight, transmittance;
if ( dispersion ) {
transmittedLight = vec4().toVar();
transmittance = vec3().toVar();
const halfSpread = ior.sub( 1.0 ).mul( dispersion.mul( 0.025 ) );
const iors = vec3( ior.sub( halfSpread ), ior, ior.add( halfSpread ) );
Loop( { start: 0, end: 3 }, ( { i } ) => {
const ior = iors.element( i );
const transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
const refractedRayExit = position.add( transmissionRay );
// Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
const ndcPos = projMatrix.mul( viewMatrix.mul( vec4( refractedRayExit, 1.0 ) ) );
const refractionCoords = vec2( ndcPos.xy.div( ndcPos.w ) ).toVar();
refractionCoords.addAssign( 1.0 );
refractionCoords.divAssign( 2.0 );
refractionCoords.assign( vec2( refractionCoords.x, refractionCoords.y.oneMinus() ) ); // webgpu
// Sample framebuffer to get pixel the refracted ray hits.
const transmissionSample = getTransmissionSample( refractionCoords, roughness, ior );
transmittedLight.element( i ).assign( transmissionSample.element( i ) );
transmittedLight.a.addAssign( transmissionSample.a );
transmittance.element( i ).assign( diffuseColor.element( i ).mul( volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ).element( i ) ) );
} );
transmittedLight.a.divAssign( 3.0 );
} else {
const transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
const refractedRayExit = position.add( transmissionRay );
// Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
const ndcPos = projMatrix.mul( viewMatrix.mul( vec4( refractedRayExit, 1.0 ) ) );
const refractionCoords = vec2( ndcPos.xy.div( ndcPos.w ) ).toVar();
refractionCoords.addAssign( 1.0 );
refractionCoords.divAssign( 2.0 );
refractionCoords.assign( vec2( refractionCoords.x, refractionCoords.y.oneMinus() ) ); // webgpu
// Sample framebuffer to get pixel the refracted ray hits.
transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );
transmittance = diffuseColor.mul( volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ) );
}
const attenuatedColor = transmittance.rgb.mul( transmittedLight.rgb );
const dotNV = n.dot( v ).clamp();
// Get the specular component.
const F = vec3( EnvironmentBRDF( { // n, v, specularColor, specularF90, roughness
dotNV,
specularColor,
specularF90,
roughness
} ) );
// As less light is transmitted, the opacity should be increased. This simple approximation does a decent job
// of modulating a CSS background, and has no effect when the buffer is opaque, due to a solid object or clear color.
const transmittanceFactor = transmittance.r.add( transmittance.g, transmittance.b ).div( 3.0 );
return vec4( F.oneMinus().mul( attenuatedColor ), transmittedLight.a.oneMinus().mul( transmittanceFactor ).oneMinus() );
} );
//
// Iridescence
//
// XYZ to linear-sRGB color space
const XYZ_TO_REC709 = /*@__PURE__*/ mat3(
3.2404542, - 1.5371385, - 0.4985314,
- 0.9692660, 1.8760108, 0.0415560,
0.0556434, - 0.2040259, 1.0572252
);
// Assume air interface for top
// Note: We don't handle the case fresnel0 == 1
const Fresnel0ToIor = ( fresnel0 ) => {
const sqrtF0 = fresnel0.sqrt();
return vec3( 1.0 ).add( sqrtF0 ).div( vec3( 1.0 ).sub( sqrtF0 ) );
};
// ior is a value between 1.0 and 3.0. 1.0 is air interface
const IorToFresnel0 = ( transmittedIor, incidentIor ) => {
return transmittedIor.sub( incidentIor ).div( transmittedIor.add( incidentIor ) ).pow2();
};
// Fresnel equations for dielectric/dielectric interfaces.
// Ref: https://belcour.github.io/blog/research/2017/05/01/brdf-thin-film.html
// Evaluation XYZ sensitivity curves in Fourier space
const evalSensitivity = ( OPD, shift ) => {
const phase = OPD.mul( 2.0 * Math.PI * 1.0e-9 );
const val = vec3( 5.4856e-13, 4.4201e-13, 5.2481e-13 );
const pos = vec3( 1.6810e+06, 1.7953e+06, 2.2084e+06 );
const VAR = vec3( 4.3278e+09, 9.3046e+09, 6.6121e+09 );
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() );
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() );
xyz = vec3( xyz.x.add( x ), xyz.y, xyz.z ).div( 1.0685e-7 );
const rgb = XYZ_TO_REC709.mul( xyz );
return rgb;
};
const evalIridescence = /*@__PURE__*/ Fn( ( { outsideIOR, eta2, cosTheta1, thinFilmThickness, baseF0 } ) => {
// Force iridescenceIOR -> outsideIOR when thinFilmThickness -> 0.0
const iridescenceIOR = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) );
// Evaluate the cosTheta on the base layer (Snell law)
const sinTheta2Sq = outsideIOR.div( iridescenceIOR ).pow2().mul( cosTheta1.pow2().oneMinus() );
// Handle TIR:
const cosTheta2Sq = sinTheta2Sq.oneMinus();
If( cosTheta2Sq.lessThan( 0 ), () => {
return vec3( 1.0 );
} );
const cosTheta2 = cosTheta2Sq.sqrt();
// First interface
const R0 = IorToFresnel0( iridescenceIOR, outsideIOR );
const R12 = F_Schlick( { f0: R0, f90: 1.0, dotVH: cosTheta1 } );
//const R21 = R12;
const T121 = R12.oneMinus();
const phi12 = iridescenceIOR.lessThan( outsideIOR ).select( Math.PI, 0.0 );
const phi21 = float( Math.PI ).sub( phi12 );
// Second interface
const baseIOR = Fresnel0ToIor( baseF0.clamp( 0.0, 0.9999 ) ); // guard against 1.0
const R1 = IorToFresnel0( baseIOR, iridescenceIOR.toVec3() );
const R23 = F_Schlick( { f0: R1, f90: 1.0, dotVH: cosTheta2 } );
const phi23 = vec3(
baseIOR.x.lessThan( iridescenceIOR ).select( Math.PI, 0.0 ),
baseIOR.y.lessThan( iridescenceIOR ).select( Math.PI, 0.0 ),
baseIOR.z.lessThan( iridescenceIOR ).select( Math.PI, 0.0 )
);
// Phase shift
const OPD = iridescenceIOR.mul( thinFilmThickness, cosTheta2, 2.0 );
const phi = vec3( phi21 ).add( phi23 );
// Compound terms
const R123 = R12.mul( R23 ).clamp( 1e-5, 0.9999 );
const r123 = R123.sqrt();
const Rs = T121.pow2().mul( R23 ).div( vec3( 1.0 ).sub( R123 ) );
// Reflectance term for m = 0 (DC term amplitude)
const C0 = R12.add( Rs );
const I = C0.toVar();
// Reflectance term for m > 0 (pairs of diracs)
const Cm = Rs.sub( T121 ).toVar();
Loop( { start: 1, end: 2, condition: '<=', name: 'm' }, ( { m } ) => {
Cm.mulAssign( r123 );
const Sm = evalSensitivity( float( m ).mul( OPD ), float( m ).mul( phi ) ).mul( 2.0 );
I.addAssign( Cm.mul( Sm ) );
} );
// Since out of gamut colors might be produced, negative color values are clamped to 0.
return I.max( vec3( 0.0 ) );
} ).setLayout( {
name: 'evalIridescence',
type: 'vec3',
inputs: [
{ name: 'outsideIOR', type: 'float' },
{ name: 'eta2', type: 'float' },
{ name: 'cosTheta1', type: 'float' },
{ name: 'thinFilmThickness', type: 'float' },
{ name: 'baseF0', type: 'vec3' }
]
} );
//
// Sheen
//
// This is a curve-fit approximation to the "Charlie sheen" BRDF integrated over the hemisphere from
// Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF".
const IBLSheenBRDF = /*@__PURE__*/ Fn( ( { normal, viewDir, roughness } ) => {
const dotNV = normal.dot( viewDir ).saturate();
const r2 = roughness.mul( roughness );
const rInv = roughness.add( 0.1 ).reciprocal();
const a = float( - 1.9362 ).add( roughness.mul( 1.0678 ) ).add( r2.mul( 0.4573 ) ).sub( rInv.mul( 0.8469 ) );
const b = float( - 0.6014 ).add( roughness.mul( 0.5538 ) ).sub( r2.mul( 0.4670 ) ).sub( rInv.mul( 0.1255 ) );
const DG = a.mul( dotNV ).add( b ).exp();
return DG.saturate();
} );
const clearcoatF0 = vec3( 0.04 );
const clearcoatF90 = float( 1 );
/**
* Represents the lighting model for a PBR material.
*
* @augments LightingModel
*/
class PhysicalLightingModel extends LightingModel {
/**
* Constructs a new physical lighting model.
*
* @param {boolean} [clearcoat=false] - Whether clearcoat is supported or not.
* @param {boolean} [sheen=false] - Whether sheen is supported or not.
* @param {boolean} [iridescence=false] - Whether iridescence is supported or not.
* @param {boolean} [anisotropy=false] - Whether anisotropy is supported or not.
* @param {boolean} [transmission=false] - Whether transmission is supported or not.
* @param {boolean} [dispersion=false] - Whether dispersion is supported or not.
* @param {boolean} [retroreflection=false] - Whether retroreflection is supported or not.
*/
constructor( clearcoat = false, sheen = false, iridescence = false, anisotropy = false, transmission = false, dispersion = false, retroreflection = false ) {
super();
/**
* Whether clearcoat is supported or not.
*
* @type {boolean}
* @default false
*/
this.clearcoat = clearcoat;
/**
* Whether sheen is supported or not.
*
* @type {boolean}
* @default false
*/
this.sheen = sheen;
/**
* Whether iridescence is supported or not.
*
* @type {boolean}
* @default false
*/
this.iridescence = iridescence;
/**
* Whether anisotropy is supported or not.
*
* @type {boolean}
* @default false
*/
this.anisotropy = anisotropy;
/**
* Whether transmission is supported or not.
*
* @type {boolean}
* @default false
*/
this.transmission = transmission;
/**
* Whether dispersion is supported or not.
*
* @type {boolean}
* @default false
*/
this.dispersion = dispersion;
/**
* Whether retroreflection is supported or not.
*
* @type {boolean}
* @default false
*/
this.retroreflection = retroreflection;
/**
* The clear coat radiance.
*
* @type {?Node}
* @default null
*/
this.clearcoatRadiance = null;
/**
* The clear coat specular direct.
*
* @type {?Node}
* @default null
*/
this.clearcoatSpecularDirect = null;
/**
* The clear coat specular indirect.
*
* @type {?Node}
* @default null
*/
this.clearcoatSpecularIndirect = null;
/**
* The sheen specular direct.
*
* @type {?Node}
* @default null
*/
this.sheenSpecularDirect = null;
/**
* The sheen specular indirect.
*
* @type {?Node}
* @default null
*/
this.sheenSpecularIndirect = null;
/**
* The iridescence Fresnel.
*
* @type {?Node}
* @default null
*/
this.iridescenceFresnel = null;
/**
* The iridescence F0 dielectric.
*
* @type {?Node}
* @default null
*/
this.iridescenceF0Dielectric = null;
/**
* The iridescence F0 metallic.
*
* @type {?Node}
* @default null
*/
this.iridescenceF0Metallic = null;
/**
* The sampled DFG LUT value, shared by the direct and indirect lighting paths.
*
* @type {?Node}
* @default null
*/
this.dfg = null;
/**
* The multi-scattering energy compensation for direct lighting.
*
* @type {?Node}
* @default null
*/
this.multiScatteringCompensation = null;
}
/**
* Depending on what features are requested, the method prepares certain node variables
* which are later used for lighting computations.
*
* @param {NodeBuilder} builder - The current node builder.
*/
start( builder ) {
if ( this.clearcoat === true ) {
this.clearcoatRadiance = vec3().toVar( 'clearcoatRadiance' );
this.clearcoatSpecularDirect = vec3().toVar( 'clearcoatSpecularDirect' );
this.clearcoatSpecularIndirect = vec3().toVar( 'clearcoatSpecularIndirect' );
}
if ( this.sheen === true ) {
this.sheenSpecularDirect = vec3().toVar( 'sheenSpecularDirect' );
this.sheenSpecularIndirect = vec3().toVar( 'sheenSpecularIndirect' );
}
if ( this.iridescence === true ) {
const dotNVi = normalView.dot( positionViewDirection ).clamp();
const iridescenceFresnelDielectric = evalIridescence( {
outsideIOR: float( 1.0 ),
eta2: iridescenceIOR,
cosTheta1: dotNVi,
thinFilmThickness: iridescenceThickness,
baseF0: specularColor
} );
const iridescenceFresnelMetallic = evalIridescence( {
outsideIOR: float( 1.0 ),
eta2: iridescenceIOR,
cosTheta1: dotNVi,
thinFilmThickness: iridescenceThickness,
baseF0: diffuseColor.rgb
} );
this.iridescenceFresnel = mix( iridescenceFresnelDielectric, iridescenceFresnelMetallic, metalness );
this.iridescenceF0Dielectric = Schlick_to_F0( { f: iridescenceFresnelDielectric, f90: 1.0, dotVH: dotNVi } );
this.iridescenceF0Metallic = Schlick_to_F0( { f: iridescenceFresnelMetallic, f90: 1.0, dotVH: dotNVi } );
}
if ( this.transmission === true ) {
const position = positionWorld;
const v = cameraPosition.sub( positionWorld ).normalize(); // TODO: Create Node for this, same issue in MaterialX
const n = normalWorld;
const context = builder.context;
context.backdrop = getIBLVolumeRefraction(
n,
v,
roughness,
diffuseContribution,
specularColorBlended,
specularF90, // specularF90
position, // positionWorld
modelWorldMatrix, // modelMatrix
cameraViewMatrix, // viewMatrix
cameraProjectionMatrix, // projMatrix
ior,
thickness,
attenuationColor,
attenuationDistance,
this.dispersion ? dispersion : null
);
context.backdropAlpha = transmission;
diffuseColor.a.mulAssign( mix( 1, context.backdrop.a, transmission ) );
}
//
const dotNV = normalView.dot( positionViewDirection ).clamp();
this.dfg = DFGLUT( { roughness, dotNV } ).toConst( 'dfg' );
// Multi-scattering energy compensation for direct lighting
// Based on "Practical Multiple Scattering Compensation for Microfacet Models"
// https://blog.selfshadow.com/publications/turquin/ms_comp_final.pdf
// Energy of the single-scattering lobe in a white furnace ( F0 = F90 = 1 )
const Ess = this.dfg.x.add( this.dfg.y );
// Compensate for the energy lost to multiple scattering, tinting the added term by F0 ( equation 16 )
this.multiScatteringCompensation = specularColorBlended.mul( Ess.reciprocal().sub( 1.0 ) ).add( 1.0 ).toConst( 'multiScatteringCompensation' );
super.start( builder );
}
// Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting"
// Approximates multi-scattering in order to preserve energy.
// http://www.jcgt.org/published/0008/01/03/
computeMultiscattering( singleScatter, multiScatter, specularF90, f0, iridescenceF0 = null ) {
const fab = this.dfg;
const Fr = iridescenceF0 ? iridescence.mix( f0, iridescenceF0 ) : f0;
const FssEss = Fr.mul( fab.x ).add( specularF90.mul( fab.y ) );
const Ess = fab.x.add( fab.y );
const Ems = Ess.oneMinus();
const Favg = Fr.add( Fr.oneMinus().mul( 0.047619 ) ); // 1/21
const Fms = FssEss.mul( Favg ).div( Ems.mul( Favg ).oneMinus() );
singleScatter.addAssign( FssEss );
multiScatter.addAssign( Fms.mul( Ems ) );
}
/**
* Implements the direct light.
*
* @param {Object} lightData - The light data.
* @param {NodeBuilder} builder - The current node builder.
*/
direct( { lightDirection, lightColor, reflectedLight }, /* builder */ ) {
const dotNL = normalView.dot( lightDirection ).clamp();
const irradiance = dotNL.mul( lightColor ).toVar();
if ( this.sheen === true ) {
this.sheenSpecularDirect.addAssign( irradiance.mul( BRDF_Sheen( { lightDirection } ) ) );
const sheenAlbedoV = IBLSheenBRDF( { normal: normalView, viewDir: positionViewDirection, roughness: sheenRoughness } );
const sheenAlbedoL = IBLSheenBRDF( { normal: normalView, viewDir: lightDirection, roughness: sheenRoughness } );
const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( sheenAlbedoV.max( sheenAlbedoL ) ).oneMinus();
irradiance.mulAssign( sheenEnergyComp );
}
if ( this.clearcoat === true ) {
const dotNLcc = clearcoatNormalView.dot( lightDirection ).clamp();
const ccIrradiance = dotNLcc.mul( lightColor );
this.clearcoatSpecularDirect.addAssign( ccIrradiance.mul( BRDF_GGX( { lightDirection, f0: clearcoatF0, f90: clearcoatF90, roughness: clearcoatRoughness, normalView: clearcoatNormalView } ) ) );
}
// Light reflected by the specular interface is not available to the diffuse layer ( glTF fresnel_mix )
const halfDir = lightDirection.add( positionViewDirection ).normalize();
const dotVH = positionViewDirection.dot( halfDir ).clamp();
let F = F_Schlick( { f0: specularColor, f90: specularF90, dotVH } );
let specularBRDF = BRDF_GGX( { lightDirection, f0: specularColorBlended, f90: 1, roughness, f: this.iridescenceFresnel, USE_IRIDESCENCE: this.iridescence, USE_ANISOTROPY: this.anisotropy } );
if ( this.retroreflection === true ) {
// Minimal Retroreflective Microfacet Model:
// https://jcgt.org/published/0015/01/04/
const retroViewDirection = positionViewDirection.negate().reflect( normalView );
const retroHalfDir = lightDirection.add( retroViewDirection ).normalize();
const dotRetroVH = retroViewDirection.dot( retroHalfDir ).clamp();
const retroF = F_Schlick( { f0: specularColor, f90: specularF90, dotVH: dotRetroVH } );
const retroSpecularBRDF = BRDF_GGX( { lightDirection, viewDirection: retroViewDirection, f0: specularColorBlended, f90: 1, roughness, f: this.iridescenceFresnel, USE_IRIDESCENCE: this.iridescence, USE_ANISOTROPY: this.anisotropy } );
F = mix( F, retroF, retroreflectivity.clamp() );
specularBRDF = mix( specularBRDF, retroSpecularBRDF, retroreflectivity.clamp() );
}
reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseContribution } ) ).mul( F.oneMinus() ) );
reflectedLight.directSpecular.addAssign( irradiance.mul( specularBRDF ).mul( this.multiScatteringCompensation ) );
}
/**
* This method is intended for implementing the direct light term for
* rect area light nodes.
*
* @param {Object} input - The input data.
* @param {NodeBuilder} builder - The current node builder.
*/
directRectArea( { lightColor, lightPosition, halfWidth, halfHeight, reflectedLight, ltc_1, ltc_2 }, /* builder */ ) {
const p0 = lightPosition.add( halfWidth ).sub( halfHeight ); // counterclockwise; light shines in local neg z direction
const p1 = lightPosition.sub( halfWidth ).sub( halfHeight );
const p2 = lightPosition.sub( halfWidth ).add( halfHeight );
const p3 = lightPosition.add( halfWidth ).add( halfHeight );
const N = normalView;
const V = positionViewDirection;
const P = positionView.toVar();
const uv = LTC_Uv( { N, V, roughness } );
const t1 = ltc_1.sample( uv ).toVar();
const t2 = ltc_2.sample( uv ).toVar();
const mInv = mat3(
vec3( t1.x, 0, t1.y ),
vec3( 0, 1, 0 ),
vec3( t1.z, 0, t1.w )
).toVar();
// LTC Fresnel Approximation by Stephen Hill
// http://blog.selfshadow.com/publications/s2016-advances/s2016_ltc_fresnel.pdf
const fresnel = specularColorBlended.mul( t2.x ).add( specularF90.sub( specularColorBlended ).mul( t2.y ) ).toVar();
reflectedLight.directSpecular.addAssign( lightColor.mul( fresnel ).mul( LTC_Evaluate( { N, V, P, mInv, p0, p1, p2, p3 } ) ) );
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 } ) ) );
if ( this.clearcoat === true ) {
const Ncc = clearcoatNormalView;
const uvClearcoat = LTC_Uv( { N: Ncc, V, roughness: clearcoatRoughness } );
const t1Clearcoat = ltc_1.sample( uvClearcoat );
const t2Clearcoat = ltc_2.sample( uvClearcoat );
const mInvClearcoat = mat3(
vec3( t1Clearcoat.x, 0, t1Clearcoat.y ),
vec3( 0, 1, 0 ),
vec3( t1Clearcoat.z, 0, t1Clearcoat.w )
);
// LTC Fresnel Approximation for clearcoat
const fresnelClearcoat = clearcoatF0.mul( t2Clearcoat.x ).add( clearcoatF90.sub( clearcoatF0 ).mul( t2Clearcoat.y ) );
this.clearcoatSpecularDirect.addAssign( lightColor.mul( fresnelClearcoat ).mul( LTC_Evaluate( { N: Ncc, V, P, mInv: mInvClearcoat, p0, p1, p2, p3 } ) ) );
}
}
/**
* Implements the indirect lighting.
*
* @param {NodeBuilder} builder - The current node builder.
*/
indirect( builder ) {
this.indirectDiffuse( builder );
this.indirectSpecular( builder );
this.ambientOcclusion( builder );
}
/**
* Implements the indirect diffuse term.
*
* @param {NodeBuilder} builder - The current node builder.
*/
indirectDiffuse( builder ) {
const { irradiance, reflectedLight } = builder.context;
// Energy reflected by the specular lobe is not available to the diffuse layer
const singleScattering = vec3().toVar();
const multiScattering = vec3().toVar();
this.computeMultiscattering( singleScattering, multiScattering, specularF90, specularColor, this.iridescenceF0Dielectric );
const diffuse = irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseContribution } ) ).mul( singleScattering.add( multiScattering ).oneMinus() ).toVar();
if ( this.sheen === true ) {
const sheenAlbedo = IBLSheenBRDF( { normal: normalView, viewDir: positionViewDirection, roughness: sheenRoughness } );
this.sheenSpecularIndirect.addAssign( irradiance.mul( sheen, sheenAlbedo, 1 / Math.PI ) );
const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( sheenAlbedo ).oneMinus();
diffuse.mulAssign( sheenEnergyComp );
}
reflectedLight.indirectDiffuse.addAssign( diffuse );
}
/**
* Implements the indirect specular term.
*
* @param {NodeBuilder} builder - The current node builder.
*/
indirectSpecular( builder ) {
const { radiance, iblIrradiance, reflectedLight } = builder.context;
if ( this.sheen === true ) {
this.sheenSpecularIndirect.addAssign( iblIrradiance.mul(
sheen,
IBLSheenBRDF( {
normal: normalView,
viewDir: positionViewDirection,
roughness: sheenRoughness
} ),
1 / Math.PI
) );
}
if ( this.clearcoat === true ) {
const dotNVcc = clearcoatNormalView.dot( positionViewDirection ).clamp();
const clearcoatEnv = EnvironmentBRDF( {
dotNV: dotNVcc,
specularColor: clearcoatF0,
specularF90: clearcoatF90,
roughness: clearcoatRoughness
} );
this.clearcoatSpecularIndirect.addAssign( this.clearcoatRadiance.mul( clearcoatEnv ) );
}
// Both indirect specular and indirect diffuse light accumulate here
// Compute multiscattering separately for dielectric and metallic, then mix
const singleScatteringDielectric = vec3().toVar( 'singleScatteringDielectric' );
const multiScatteringDielectric = vec3().toVar( 'multiScatteringDielectric' );
const singleScatteringMetallic = vec3().toVar( 'singleScatteringMetallic' );
const multiScatteringMetallic = vec3().toVar( 'multiScatteringMetallic' );
this.computeMultiscattering( singleScatteringDielectric, multiScatteringDielectric, specularF90, specularColor, this.iridescenceF0Dielectric );
this.computeMultiscattering( singleScatteringMetallic, multiScatteringMetallic, specularF90, diffuseColor.rgb, this.iridescenceF0Metallic );
// Mix based on metalness
const singleScattering = mix( singleScatteringDielectric, singleScatteringMetallic, metalness );
const multiScattering = mix( multiScatteringDielectric, multiScatteringMetallic, metalness );
// Diffuse energy conservation uses dielectric path
const totalScatteringDielectric = singleScatteringDielectric.add( multiScatteringDielectric );
const diffuse = diffuseContribution.mul( totalScatteringDielectric.oneMinus() );
const cosineWeightedIrradiance = iblIrradiance.mul( 1 / Math.PI );
const indirectSpecular = radiance.mul( singleScattering ).add( multiScattering.mul( cosineWeightedIrradiance ) ).toVar();
const indirectDiffuse = diffuse.mul( cosineWeightedIrradiance ).toVar();
if ( this.sheen === true ) {
const sheenAlbedo = IBLSheenBRDF( { normal: normalView, viewDir: positionViewDirection, roughness: sheenRoughness } );
const sheenEnergyComp = sheen.r.max( sheen.g ).max( sheen.b ).mul( sheenAlbedo ).oneMinus();
indirectSpecular.mulAssign( sheenEnergyComp );
indirectDiffuse.mulAssign( sheenEnergyComp );
}
reflectedLight.indirectSpecular.addAssign( indirectSpecular );
reflectedLight.indirectDiffuse.addAssign( indirectDiffuse );
}
/**
* Implements the ambient occlusion term.
*
* @param {NodeBuilder} builder - The current node builder.
*/
ambientOcclusion( builder ) {
const { ambientOcclusion, reflectedLight } = builder.context;
const dotNV = normalView.dot( positionViewDirection ).clamp(); // @ TODO: Move to core dotNV
const aoNV = dotNV.add( ambientOcclusion );
const aoExp = roughness.mul( - 16.0 ).oneMinus().negate().exp2();
const aoNode = ambientOcclusion.sub( aoNV.pow( aoExp ).oneMinus() ).clamp();
if ( this.clearcoat === true ) {
this.clearcoatSpecularIndirect.mulAssign( ambientOcclusion );
}
if ( this.sheen === true ) {
this.sheenSpecularIndirect.mulAssign( ambientOcclusion );
}
reflectedLight.indirectDiffuse.mulAssign( ambientOcclusion );
reflectedLight.indirectSpecular.mulAssign( aoNode );
}
/**
* Used for final lighting accumulations depending on the requested features.
*
* @param {NodeBuilder} builder - The current node builder.
*/
finish( { context } ) {
const { outgoingLight } = context;
if ( this.clearcoat === true ) {
const dotNVcc = clearcoatNormalView.dot( positionViewDirection ).clamp();
const Fcc = F_Schlick( {
dotVH: dotNVcc,
f0: clearcoatF0,
f90: clearcoatF90
} );
const clearcoatLight = outgoingLight.mul( clearcoat.mul( Fcc ).oneMinus() ).add( this.clearcoatSpecularDirect.add( this.clearcoatSpecularIndirect ).mul( clearcoat ) );
outgoingLight.assign( clearcoatLight );
}
if ( this.sheen === true ) {
const sheenLight = outgoingLight.add( this.sheenSpecularDirect, this.sheenSpecularIndirect );
outgoingLight.assign( sheenLight );
}
}
}
export default PhysicalLightingModel;