forked from BabylonJS/Babylon.js
/
pbrBaseMaterial.ts
2191 lines (1819 loc) · 87.2 KB
/
pbrBaseMaterial.ts
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { serialize, serializeAsImageProcessingConfiguration, expandToProperty } from "../../Misc/decorators";
import { Observer } from "../../Misc/observable";
import { Logger } from "../../Misc/logger";
import { SmartArray } from "../../Misc/smartArray";
import { BRDFTextureTools } from "../../Misc/brdfTextureTools";
import { Nullable } from "../../types";
import { Camera } from "../../Cameras/camera";
import { Scene } from "../../scene";
import { Matrix, Vector4 } from "../../Maths/math.vector";
import { VertexBuffer } from "../../Meshes/buffer";
import { SubMesh } from "../../Meshes/subMesh";
import { AbstractMesh } from "../../Meshes/abstractMesh";
import { Mesh } from "../../Meshes/mesh";
import { _TimeToken } from "../../Instrumentation/timeToken";
import { IMaterialClearCoatDefines, PBRClearCoatConfiguration } from "./pbrClearCoatConfiguration";
import { IMaterialAnisotropicDefines, PBRAnisotropicConfiguration } from "./pbrAnisotropicConfiguration";
import { IMaterialBRDFDefines, PBRBRDFConfiguration } from "./pbrBRDFConfiguration";
import { IMaterialSheenDefines, PBRSheenConfiguration } from "./pbrSheenConfiguration";
import { IMaterialSubSurfaceDefines, PBRSubSurfaceConfiguration } from "./pbrSubSurfaceConfiguration";
import { Color3, TmpColors } from '../../Maths/math.color';
import { ImageProcessingConfiguration, IImageProcessingConfigurationDefines } from "../../Materials/imageProcessingConfiguration";
import { Effect, IEffectCreationOptions } from "../../Materials/effect";
import { Material, IMaterialCompilationOptions } from "../../Materials/material";
import { MaterialDefines } from "../../Materials/materialDefines";
import { PushMaterial } from "../../Materials/pushMaterial";
import { MaterialHelper } from "../../Materials/materialHelper";
import { BaseTexture } from "../../Materials/Textures/baseTexture";
import { Texture } from "../../Materials/Textures/texture";
import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
import { CubeTexture } from "../../Materials/Textures/cubeTexture";
import { MaterialFlags } from "../materialFlags";
import { Constants } from "../../Engines/constants";
import { IAnimatable } from '../../Animations/animatable.interface';
import "../../Materials/Textures/baseTexture.polynomial";
import "../../Shaders/pbr.fragment";
import "../../Shaders/pbr.vertex";
import { EffectFallbacks } from '../effectFallbacks';
/**
* Manages the defines for the PBR Material.
* @hidden
*/
export class PBRMaterialDefines extends MaterialDefines
implements IImageProcessingConfigurationDefines,
IMaterialClearCoatDefines,
IMaterialAnisotropicDefines,
IMaterialBRDFDefines,
IMaterialSheenDefines,
IMaterialSubSurfaceDefines {
public PBR = true;
public MAINUV1 = false;
public MAINUV2 = false;
public UV1 = false;
public UV2 = false;
public ALBEDO = false;
public ALBEDODIRECTUV = 0;
public VERTEXCOLOR = false;
public AMBIENT = false;
public AMBIENTDIRECTUV = 0;
public AMBIENTINGRAYSCALE = false;
public OPACITY = false;
public VERTEXALPHA = false;
public OPACITYDIRECTUV = 0;
public OPACITYRGB = false;
public ALPHATEST = false;
public DEPTHPREPASS = false;
public ALPHABLEND = false;
public ALPHAFROMALBEDO = false;
public ALPHATESTVALUE = "0.5";
public SPECULAROVERALPHA = false;
public RADIANCEOVERALPHA = false;
public ALPHAFRESNEL = false;
public LINEARALPHAFRESNEL = false;
public PREMULTIPLYALPHA = false;
public EMISSIVE = false;
public EMISSIVEDIRECTUV = 0;
public REFLECTIVITY = false;
public REFLECTIVITYDIRECTUV = 0;
public SPECULARTERM = false;
public MICROSURFACEFROMREFLECTIVITYMAP = false;
public MICROSURFACEAUTOMATIC = false;
public LODBASEDMICROSFURACE = false;
public MICROSURFACEMAP = false;
public MICROSURFACEMAPDIRECTUV = 0;
public METALLICWORKFLOW = false;
public ROUGHNESSSTOREINMETALMAPALPHA = false;
public ROUGHNESSSTOREINMETALMAPGREEN = false;
public METALLNESSSTOREINMETALMAPBLUE = false;
public AOSTOREINMETALMAPRED = false;
public METALLICF0FACTORFROMMETALLICMAP = false;
public ENVIRONMENTBRDF = false;
public ENVIRONMENTBRDF_RGBD = false;
public NORMAL = false;
public TANGENT = false;
public BUMP = false;
public BUMPDIRECTUV = 0;
public OBJECTSPACE_NORMALMAP = false;
public PARALLAX = false;
public PARALLAXOCCLUSION = false;
public NORMALXYSCALE = true;
public LIGHTMAP = false;
public LIGHTMAPDIRECTUV = 0;
public USELIGHTMAPASSHADOWMAP = false;
public GAMMALIGHTMAP = false;
public RGBDLIGHTMAP = false;
public REFLECTION = false;
public REFLECTIONMAP_3D = false;
public REFLECTIONMAP_SPHERICAL = false;
public REFLECTIONMAP_PLANAR = false;
public REFLECTIONMAP_CUBIC = false;
public USE_LOCAL_REFLECTIONMAP_CUBIC = false;
public REFLECTIONMAP_PROJECTION = false;
public REFLECTIONMAP_SKYBOX = false;
public REFLECTIONMAP_SKYBOX_TRANSFORMED = false;
public REFLECTIONMAP_EXPLICIT = false;
public REFLECTIONMAP_EQUIRECTANGULAR = false;
public REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
public REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
public INVERTCUBICMAP = false;
public USESPHERICALFROMREFLECTIONMAP = false;
public USEIRRADIANCEMAP = false;
public SPHERICAL_HARMONICS = false;
public USESPHERICALINVERTEX = false;
public REFLECTIONMAP_OPPOSITEZ = false;
public LODINREFLECTIONALPHA = false;
public GAMMAREFLECTION = false;
public RGBDREFLECTION = false;
public LINEARSPECULARREFLECTION = false;
public RADIANCEOCCLUSION = false;
public HORIZONOCCLUSION = false;
public INSTANCES = false;
public NUM_BONE_INFLUENCERS = 0;
public BonesPerMesh = 0;
public BONETEXTURE = false;
public NONUNIFORMSCALING = false;
public MORPHTARGETS = false;
public MORPHTARGETS_NORMAL = false;
public MORPHTARGETS_TANGENT = false;
public MORPHTARGETS_UV = false;
public NUM_MORPH_INFLUENCERS = 0;
public IMAGEPROCESSING = false;
public VIGNETTE = false;
public VIGNETTEBLENDMODEMULTIPLY = false;
public VIGNETTEBLENDMODEOPAQUE = false;
public TONEMAPPING = false;
public TONEMAPPING_ACES = false;
public CONTRAST = false;
public COLORCURVES = false;
public COLORGRADING = false;
public COLORGRADING3D = false;
public SAMPLER3DGREENDEPTH = false;
public SAMPLER3DBGRMAP = false;
public IMAGEPROCESSINGPOSTPROCESS = false;
public EXPOSURE = false;
public MULTIVIEW = false;
public USEPHYSICALLIGHTFALLOFF = false;
public USEGLTFLIGHTFALLOFF = false;
public TWOSIDEDLIGHTING = false;
public SHADOWFLOAT = false;
public CLIPPLANE = false;
public CLIPPLANE2 = false;
public CLIPPLANE3 = false;
public CLIPPLANE4 = false;
public CLIPPLANE5 = false;
public CLIPPLANE6 = false;
public POINTSIZE = false;
public FOG = false;
public LOGARITHMICDEPTH = false;
public FORCENORMALFORWARD = false;
public SPECULARAA = false;
public CLEARCOAT = false;
public CLEARCOAT_DEFAULTIOR = false;
public CLEARCOAT_TEXTURE = false;
public CLEARCOAT_TEXTUREDIRECTUV = 0;
public CLEARCOAT_BUMP = false;
public CLEARCOAT_BUMPDIRECTUV = 0;
public CLEARCOAT_TINT = false;
public CLEARCOAT_TINT_TEXTURE = false;
public CLEARCOAT_TINT_TEXTUREDIRECTUV = 0;
public ANISOTROPIC = false;
public ANISOTROPIC_TEXTURE = false;
public ANISOTROPIC_TEXTUREDIRECTUV = 0;
public BRDF_V_HEIGHT_CORRELATED = false;
public MS_BRDF_ENERGY_CONSERVATION = false;
public SPECULAR_GLOSSINESS_ENERGY_CONSERVATION = false;
public SHEEN = false;
public SHEEN_TEXTURE = false;
public SHEEN_TEXTUREDIRECTUV = 0;
public SHEEN_LINKWITHALBEDO = false;
public SUBSURFACE = false;
public SS_REFRACTION = false;
public SS_TRANSLUCENCY = false;
public SS_SCATERRING = false;
public SS_THICKNESSANDMASK_TEXTURE = false;
public SS_THICKNESSANDMASK_TEXTUREDIRECTUV = 0;
public SS_REFRACTIONMAP_3D = false;
public SS_REFRACTIONMAP_OPPOSITEZ = false;
public SS_LODINREFRACTIONALPHA = false;
public SS_GAMMAREFRACTION = false;
public SS_RGBDREFRACTION = false;
public SS_LINEARSPECULARREFRACTION = false;
public SS_LINKREFRACTIONTOTRANSPARENCY = false;
public SS_MASK_FROM_THICKNESS_TEXTURE = false;
public UNLIT = false;
public DEBUGMODE = 0;
/**
* Initializes the PBR Material defines.
*/
constructor() {
super();
this.rebuild();
}
/**
* Resets the PBR Material defines.
*/
public reset(): void {
super.reset();
this.ALPHATESTVALUE = "0.5";
this.PBR = true;
}
}
/**
* The Physically based material base class of BJS.
*
* This offers the main features of a standard PBR material.
* For more information, please refer to the documentation :
* https://doc.babylonjs.com/how_to/physically_based_rendering
*/
export abstract class PBRBaseMaterial extends PushMaterial {
/**
* PBRMaterialTransparencyMode: No transparency mode, Alpha channel is not use.
*/
public static readonly PBRMATERIAL_OPAQUE = 0;
/**
* PBRMaterialTransparencyMode: Alpha Test mode, pixel are discarded below a certain threshold defined by the alpha cutoff value.
*/
public static readonly PBRMATERIAL_ALPHATEST = 1;
/**
* PBRMaterialTransparencyMode: Pixels are blended (according to the alpha mode) with the already drawn pixels in the current frame buffer.
*/
public static readonly PBRMATERIAL_ALPHABLEND = 2;
/**
* PBRMaterialTransparencyMode: Pixels are blended (according to the alpha mode) with the already drawn pixels in the current frame buffer.
* They are also discarded below the alpha cutoff threshold to improve performances.
*/
public static readonly PBRMATERIAL_ALPHATESTANDBLEND = 3;
/**
* Defines the default value of how much AO map is occluding the analytical lights
* (point spot...).
*/
public static DEFAULT_AO_ON_ANALYTICAL_LIGHTS = 0;
/**
* PBRMaterialLightFalloff Physical: light is falling off following the inverse squared distance law.
*/
public static readonly LIGHTFALLOFF_PHYSICAL = 0;
/**
* PBRMaterialLightFalloff gltf: light is falling off as described in the gltf moving to PBR document
* to enhance interoperability with other engines.
*/
public static readonly LIGHTFALLOFF_GLTF = 1;
/**
* PBRMaterialLightFalloff Standard: light is falling off like in the standard material
* to enhance interoperability with other materials.
*/
public static readonly LIGHTFALLOFF_STANDARD = 2;
/**
* Intensity of the direct lights e.g. the four lights available in your scene.
* This impacts both the direct diffuse and specular highlights.
*/
protected _directIntensity: number = 1.0;
/**
* Intensity of the emissive part of the material.
* This helps controlling the emissive effect without modifying the emissive color.
*/
protected _emissiveIntensity: number = 1.0;
/**
* Intensity of the environment e.g. how much the environment will light the object
* either through harmonics for rough material or through the refelction for shiny ones.
*/
protected _environmentIntensity: number = 1.0;
/**
* This is a special control allowing the reduction of the specular highlights coming from the
* four lights of the scene. Those highlights may not be needed in full environment lighting.
*/
protected _specularIntensity: number = 1.0;
/**
* This stores the direct, emissive, environment, and specular light intensities into a Vector4.
*/
private _lightingInfos: Vector4 = new Vector4(this._directIntensity, this._emissiveIntensity, this._environmentIntensity, this._specularIntensity);
/**
* Debug Control allowing disabling the bump map on this material.
*/
protected _disableBumpMap: boolean = false;
/**
* AKA Diffuse Texture in standard nomenclature.
*/
protected _albedoTexture: Nullable<BaseTexture> = null;
/**
* AKA Occlusion Texture in other nomenclature.
*/
protected _ambientTexture: Nullable<BaseTexture> = null;
/**
* AKA Occlusion Texture Intensity in other nomenclature.
*/
protected _ambientTextureStrength: number = 1.0;
/**
* Defines how much the AO map is occluding the analytical lights (point spot...).
* 1 means it completely occludes it
* 0 mean it has no impact
*/
protected _ambientTextureImpactOnAnalyticalLights: number = PBRBaseMaterial.DEFAULT_AO_ON_ANALYTICAL_LIGHTS;
/**
* Stores the alpha values in a texture.
*/
protected _opacityTexture: Nullable<BaseTexture> = null;
/**
* Stores the reflection values in a texture.
*/
protected _reflectionTexture: Nullable<BaseTexture> = null;
/**
* Stores the emissive values in a texture.
*/
protected _emissiveTexture: Nullable<BaseTexture> = null;
/**
* AKA Specular texture in other nomenclature.
*/
protected _reflectivityTexture: Nullable<BaseTexture> = null;
/**
* Used to switch from specular/glossiness to metallic/roughness workflow.
*/
protected _metallicTexture: Nullable<BaseTexture> = null;
/**
* Specifies the metallic scalar of the metallic/roughness workflow.
* Can also be used to scale the metalness values of the metallic texture.
*/
protected _metallic: Nullable<number> = null;
/**
* Specifies the roughness scalar of the metallic/roughness workflow.
* Can also be used to scale the roughness values of the metallic texture.
*/
protected _roughness: Nullable<number> = null;
/**
* Specifies the an F0 factor to help configuring the material F0.
* Instead of the default 4%, 8% * factor will be used. As the factor is defaulting
* to 0.5 the previously hard coded value stays the same.
* Can also be used to scale the F0 values of the metallic texture.
*/
protected _metallicF0Factor = 0.5;
/**
* Specifies whether the F0 factor can be fetched from the mettalic texture.
* If set to true, please adapt the metallicF0Factor to ensure it fits with
* your expectation as it multiplies with the texture data.
*/
protected _useMetallicF0FactorFromMetallicTexture = false;
/**
* Used to enable roughness/glossiness fetch from a separate channel depending on the current mode.
* Gray Scale represents roughness in metallic mode and glossiness in specular mode.
*/
protected _microSurfaceTexture: Nullable<BaseTexture> = null;
/**
* Stores surface normal data used to displace a mesh in a texture.
*/
protected _bumpTexture: Nullable<BaseTexture> = null;
/**
* Stores the pre-calculated light information of a mesh in a texture.
*/
protected _lightmapTexture: Nullable<BaseTexture> = null;
/**
* The color of a material in ambient lighting.
*/
protected _ambientColor = new Color3(0, 0, 0);
/**
* AKA Diffuse Color in other nomenclature.
*/
protected _albedoColor = new Color3(1, 1, 1);
/**
* AKA Specular Color in other nomenclature.
*/
protected _reflectivityColor = new Color3(1, 1, 1);
/**
* The color applied when light is reflected from a material.
*/
protected _reflectionColor = new Color3(1, 1, 1);
/**
* The color applied when light is emitted from a material.
*/
protected _emissiveColor = new Color3(0, 0, 0);
/**
* AKA Glossiness in other nomenclature.
*/
protected _microSurface = 0.9;
/**
* Specifies that the material will use the light map as a show map.
*/
protected _useLightmapAsShadowmap = false;
/**
* This parameters will enable/disable Horizon occlusion to prevent normal maps to look shiny when the normal
* makes the reflect vector face the model (under horizon).
*/
protected _useHorizonOcclusion = true;
/**
* This parameters will enable/disable radiance occlusion by preventing the radiance to lit
* too much the area relying on ambient texture to define their ambient occlusion.
*/
protected _useRadianceOcclusion = true;
/**
* Specifies that the alpha is coming form the albedo channel alpha channel for alpha blending.
*/
protected _useAlphaFromAlbedoTexture = false;
/**
* Specifies that the material will keeps the specular highlights over a transparent surface (only the most limunous ones).
* A car glass is a good exemple of that. When sun reflects on it you can not see what is behind.
*/
protected _useSpecularOverAlpha = true;
/**
* Specifies if the reflectivity texture contains the glossiness information in its alpha channel.
*/
protected _useMicroSurfaceFromReflectivityMapAlpha = false;
/**
* Specifies if the metallic texture contains the roughness information in its alpha channel.
*/
protected _useRoughnessFromMetallicTextureAlpha = true;
/**
* Specifies if the metallic texture contains the roughness information in its green channel.
*/
protected _useRoughnessFromMetallicTextureGreen = false;
/**
* Specifies if the metallic texture contains the metallness information in its blue channel.
*/
protected _useMetallnessFromMetallicTextureBlue = false;
/**
* Specifies if the metallic texture contains the ambient occlusion information in its red channel.
*/
protected _useAmbientOcclusionFromMetallicTextureRed = false;
/**
* Specifies if the ambient texture contains the ambient occlusion information in its red channel only.
*/
protected _useAmbientInGrayScale = false;
/**
* In case the reflectivity map does not contain the microsurface information in its alpha channel,
* The material will try to infer what glossiness each pixel should be.
*/
protected _useAutoMicroSurfaceFromReflectivityMap = false;
/**
* Defines the falloff type used in this material.
* It by default is Physical.
*/
protected _lightFalloff = PBRBaseMaterial.LIGHTFALLOFF_PHYSICAL;
/**
* Specifies that the material will keeps the reflection highlights over a transparent surface (only the most limunous ones).
* A car glass is a good exemple of that. When the street lights reflects on it you can not see what is behind.
*/
protected _useRadianceOverAlpha = true;
/**
* Allows using an object space normal map (instead of tangent space).
*/
protected _useObjectSpaceNormalMap = false;
/**
* Allows using the bump map in parallax mode.
*/
protected _useParallax = false;
/**
* Allows using the bump map in parallax occlusion mode.
*/
protected _useParallaxOcclusion = false;
/**
* Controls the scale bias of the parallax mode.
*/
protected _parallaxScaleBias = 0.05;
/**
* If sets to true, disables all the lights affecting the material.
*/
protected _disableLighting = false;
/**
* Number of Simultaneous lights allowed on the material.
*/
protected _maxSimultaneousLights = 4;
/**
* If sets to true, x component of normal map value will be inverted (x = 1.0 - x).
*/
protected _invertNormalMapX = false;
/**
* If sets to true, y component of normal map value will be inverted (y = 1.0 - y).
*/
protected _invertNormalMapY = false;
/**
* If sets to true and backfaceCulling is false, normals will be flipped on the backside.
*/
protected _twoSidedLighting = false;
/**
* Defines the alpha limits in alpha test mode.
*/
protected _alphaCutOff = 0.4;
/**
* Enforces alpha test in opaque or blend mode in order to improve the performances of some situations.
*/
protected _forceAlphaTest = false;
/**
* A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
* And/Or occlude the blended part. (alpha is converted to gamma to compute the fresnel)
*/
protected _useAlphaFresnel = false;
/**
* A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
* And/Or occlude the blended part. (alpha stays linear to compute the fresnel)
*/
protected _useLinearAlphaFresnel = false;
/**
* The transparency mode of the material.
*/
protected _transparencyMode: Nullable<number> = null;
/**
* Specifies the environment BRDF texture used to comput the scale and offset roughness values
* from cos thetav and roughness:
* http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
*/
protected _environmentBRDFTexture: Nullable<BaseTexture> = null;
/**
* Force the shader to compute irradiance in the fragment shader in order to take bump in account.
*/
protected _forceIrradianceInFragment = false;
/**
* Force normal to face away from face.
*/
protected _forceNormalForward = false;
/**
* Enables specular anti aliasing in the PBR shader.
* It will both interacts on the Geometry for analytical and IBL lighting.
* It also prefilter the roughness map based on the bump values.
*/
protected _enableSpecularAntiAliasing = false;
/**
* Default configuration related to image processing available in the PBR Material.
*/
@serializeAsImageProcessingConfiguration()
protected _imageProcessingConfiguration: ImageProcessingConfiguration;
/**
* Keep track of the image processing observer to allow dispose and replace.
*/
private _imageProcessingObserver: Nullable<Observer<ImageProcessingConfiguration>> = null;
/**
* Attaches a new image processing configuration to the PBR Material.
* @param configuration
*/
protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>): void {
if (configuration === this._imageProcessingConfiguration) {
return;
}
// Detaches observer.
if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
}
// Pick the scene configuration if needed.
if (!configuration) {
this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
}
else {
this._imageProcessingConfiguration = configuration;
}
// Attaches observer.
if (this._imageProcessingConfiguration) {
this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(() => {
this._markAllSubMeshesAsImageProcessingDirty();
});
}
}
/**
* Stores the available render targets.
*/
private _renderTargets = new SmartArray<RenderTargetTexture>(16);
/**
* Sets the global ambient color for the material used in lighting calculations.
*/
private _globalAmbientColor = new Color3(0, 0, 0);
/**
* Enables the use of logarithmic depth buffers, which is good for wide depth buffers.
*/
private _useLogarithmicDepth: boolean = false;
/**
* If set to true, no lighting calculations will be applied.
*/
private _unlit = false;
private _debugMode = 0;
/**
* @hidden
* This is reserved for the inspector.
* Defines the material debug mode.
* It helps seeing only some components of the material while troubleshooting.
*/
@expandToProperty("_markAllSubMeshesAsMiscDirty")
public debugMode = 0;
/**
* @hidden
* This is reserved for the inspector.
* Specify from where on screen the debug mode should start.
* The value goes from -1 (full screen) to 1 (not visible)
* It helps with side by side comparison against the final render
* This defaults to -1
*/
private debugLimit = -1;
/**
* @hidden
* This is reserved for the inspector.
* As the default viewing range might not be enough (if the ambient is really small for instance)
* You can use the factor to better multiply the final value.
*/
private debugFactor = 1;
/**
* Defines the clear coat layer parameters for the material.
*/
public readonly clearCoat = new PBRClearCoatConfiguration(this._markAllSubMeshesAsTexturesDirty.bind(this));
/**
* Defines the anisotropic parameters for the material.
*/
public readonly anisotropy = new PBRAnisotropicConfiguration(this._markAllSubMeshesAsTexturesDirty.bind(this));
/**
* Defines the BRDF parameters for the material.
*/
public readonly brdf = new PBRBRDFConfiguration(this._markAllSubMeshesAsMiscDirty.bind(this));
/**
* Defines the Sheen parameters for the material.
*/
public readonly sheen = new PBRSheenConfiguration(this._markAllSubMeshesAsTexturesDirty.bind(this));
/**
* Defines the SubSurface parameters for the material.
*/
public readonly subSurface = new PBRSubSurfaceConfiguration(this._markAllSubMeshesAsTexturesDirty.bind(this));
/**
* Custom callback helping to override the default shader used in the material.
*/
public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: PBRMaterialDefines) => string;
protected _rebuildInParallel = false;
/**
* Instantiates a new PBRMaterial instance.
*
* @param name The material name
* @param scene The scene the material will be use in.
*/
constructor(name: string, scene: Scene) {
super(name, scene);
// Setup the default processing configuration to the scene.
this._attachImageProcessingConfiguration(null);
this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
this._renderTargets.reset();
if (MaterialFlags.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
this._renderTargets.push(<RenderTargetTexture>this._reflectionTexture);
}
this.subSurface.fillRenderTargetTextures(this._renderTargets);
return this._renderTargets;
};
this._environmentBRDFTexture = BRDFTextureTools.GetEnvironmentBRDFTexture(scene);
}
/**
* Gets a boolean indicating that current material needs to register RTT
*/
public get hasRenderTargetTextures(): boolean {
if (MaterialFlags.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
return true;
}
return this.subSurface.hasRenderTargetTextures();
}
/**
* Gets the name of the material class.
*/
public getClassName(): string {
return "PBRBaseMaterial";
}
/**
* Enabled the use of logarithmic depth buffers, which is good for wide depth buffers.
*/
@serialize()
public get useLogarithmicDepth(): boolean {
return this._useLogarithmicDepth;
}
/**
* Enabled the use of logarithmic depth buffers, which is good for wide depth buffers.
*/
public set useLogarithmicDepth(value: boolean) {
this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
}
/**
* Gets the current transparency mode.
*/
@serialize()
public get transparencyMode(): Nullable<number> {
return this._transparencyMode;
}
/**
* Sets the transparency mode of the material.
*
* | Value | Type | Description |
* | ----- | ----------------------------------- | ----------- |
* | 0 | OPAQUE | |
* | 1 | ALPHATEST | |
* | 2 | ALPHABLEND | |
* | 3 | ALPHATESTANDBLEND | |
*
*/
public set transparencyMode(value: Nullable<number>) {
if (this._transparencyMode === value) {
return;
}
this._transparencyMode = value;
this._forceAlphaTest = (value === PBRBaseMaterial.PBRMATERIAL_ALPHATESTANDBLEND);
this._markAllSubMeshesAsTexturesAndMiscDirty();
}
/**
* Returns true if alpha blending should be disabled.
*/
private get _disableAlphaBlending(): boolean {
return (this.subSurface.disableAlphaBlending ||
this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_OPAQUE ||
this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_ALPHATEST);
}
/**
* Specifies whether or not this material should be rendered in alpha blend mode.
*/
public needAlphaBlending(): boolean {
if (this._disableAlphaBlending) {
return false;
}
return (this.alpha < 1.0) || (this._opacityTexture != null) || this._shouldUseAlphaFromAlbedoTexture();
}
/**
* Specifies if the mesh will require alpha blending.
* @param mesh - BJS mesh.
*/
public needAlphaBlendingForMesh(mesh: AbstractMesh): boolean {
if (this._disableAlphaBlending && mesh.visibility >= 1.0) {
return false;
}
return super.needAlphaBlendingForMesh(mesh);
}
/**
* Specifies whether or not this material should be rendered in alpha test mode.
*/
public needAlphaTesting(): boolean {
if (this._forceAlphaTest) {
return true;
}
if (this.subSurface.disableAlphaBlending) {
return false;
}
return this._albedoTexture != null && this._albedoTexture.hasAlpha && (this._transparencyMode == null || this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_ALPHATEST);
}
/**
* Specifies whether or not the alpha value of the albedo texture should be used for alpha blending.
*/
protected _shouldUseAlphaFromAlbedoTexture(): boolean {
return this._albedoTexture != null && this._albedoTexture.hasAlpha && this._useAlphaFromAlbedoTexture && this._transparencyMode !== PBRBaseMaterial.PBRMATERIAL_OPAQUE;
}
/**
* Gets the texture used for the alpha test.
*/
public getAlphaTestTexture(): Nullable<BaseTexture> {
return this._albedoTexture;
}
/**
* Specifies that the submesh is ready to be used.
* @param mesh - BJS mesh.
* @param subMesh - A submesh of the BJS mesh. Used to check if it is ready.
* @param useInstances - Specifies that instances should be used.
* @returns - boolean indicating that the submesh is ready or not.
*/
public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
if (subMesh.effect && this.isFrozen) {
if (this._wasPreviouslyReady) {
return true;
}
}
if (!subMesh._materialDefines) {
subMesh._materialDefines = new PBRMaterialDefines();
}
const defines = <PBRMaterialDefines>subMesh._materialDefines;
if (!this.checkReadyOnEveryCall && subMesh.effect) {
if (defines._renderId === this.getScene().getRenderId()) {
return true;
}
}
const scene = this.getScene();
const engine = scene.getEngine();
if (defines._areTexturesDirty) {
if (scene.texturesEnabled) {
if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
if (!this._albedoTexture.isReadyOrNotBlocking()) {
return false;
}
}
if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
if (!this._ambientTexture.isReadyOrNotBlocking()) {
return false;
}
}
if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
if (!this._opacityTexture.isReadyOrNotBlocking()) {
return false;
}
}
var reflectionTexture = this._getReflectionTexture();
if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
if (!reflectionTexture.isReadyOrNotBlocking()) {
return false;
}
if (reflectionTexture.irradianceTexture && !reflectionTexture.irradianceTexture.isReadyOrNotBlocking()) {
return false;
}
}
if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
if (!this._lightmapTexture.isReadyOrNotBlocking()) {
return false;
}
}
if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
if (!this._emissiveTexture.isReadyOrNotBlocking()) {
return false;
}
}
if (MaterialFlags.SpecularTextureEnabled) {
if (this._metallicTexture) {
if (!this._metallicTexture.isReadyOrNotBlocking()) {
return false;
}
}
else if (this._reflectivityTexture) {
if (!this._reflectivityTexture.isReadyOrNotBlocking()) {
return false;
}
}
if (this._microSurfaceTexture) {
if (!this._microSurfaceTexture.isReadyOrNotBlocking()) {
return false;
}
}
}
if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {