123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109 |
- import { __decorate } from "../../tslib.es6.js";
- /* eslint-disable @typescript-eslint/naming-convention */
- import { serializeAsImageProcessingConfiguration, expandToProperty } from "../../Misc/decorators.js";
- import { Logger } from "../../Misc/logger.js";
- import { SmartArray } from "../../Misc/smartArray.js";
- import { GetEnvironmentBRDFTexture } from "../../Misc/brdfTextureTools.js";
- import { Scene } from "../../scene.js";
- import { Vector4 } from "../../Maths/math.vector.js";
- import { VertexBuffer } from "../../Buffers/buffer.js";
- import { PBRBRDFConfiguration } from "./pbrBRDFConfiguration.js";
- import { PrePassConfiguration } from "../prePassConfiguration.js";
- import { Color3, TmpColors } from "../../Maths/math.color.js";
- import { Scalar } from "../../Maths/math.scalar.js";
- import { ImageProcessingConfiguration } from "../../Materials/imageProcessingConfiguration.js";
- import { Material } from "../../Materials/material.js";
- import { MaterialPluginEvent } from "../materialPluginEvent.js";
- import { MaterialDefines } from "../../Materials/materialDefines.js";
- import { PushMaterial } from "../../Materials/pushMaterial.js";
- import { Texture } from "../../Materials/Textures/texture.js";
- import { MaterialFlags } from "../materialFlags.js";
- import "../../Materials/Textures/baseTexture.polynomial.js";
- import "../../Shaders/pbr.fragment.js";
- import "../../Shaders/pbr.vertex.js";
- import { EffectFallbacks } from "../effectFallbacks.js";
- import { PBRClearCoatConfiguration } from "./pbrClearCoatConfiguration.js";
- import { PBRIridescenceConfiguration } from "./pbrIridescenceConfiguration.js";
- import { PBRAnisotropicConfiguration } from "./pbrAnisotropicConfiguration.js";
- import { PBRSheenConfiguration } from "./pbrSheenConfiguration.js";
- import { PBRSubSurfaceConfiguration } from "./pbrSubSurfaceConfiguration.js";
- import { DetailMapConfiguration } from "../material.detailMapConfiguration.js";
- import { addClipPlaneUniforms, bindClipPlane } from "../clipPlaneMaterialHelper.js";
- import { BindBonesParameters, BindFogParameters, BindLights, BindLogDepth, BindMorphTargetParameters, BindTextureMatrix, HandleFallbacksForShadows, PrepareAttributesForBakedVertexAnimation, PrepareAttributesForBones, PrepareAttributesForInstances, PrepareAttributesForMorphTargets, PrepareDefinesForAttributes, PrepareDefinesForFrameBoundValues, PrepareDefinesForLights, PrepareDefinesForMergedUV, PrepareDefinesForMisc, PrepareDefinesForMultiview, PrepareDefinesForOIT, PrepareDefinesForPrePass, PrepareUniformsAndSamplersList, } from "../materialHelper.functions.js";
- const onCreatedEffectParameters = { effect: null, subMesh: null };
- /**
- * Manages the defines for the PBR Material.
- * @internal
- */
- export class PBRMaterialDefines extends MaterialDefines {
- /**
- * Initializes the PBR Material defines.
- * @param externalProperties The external properties
- */
- constructor(externalProperties) {
- super(externalProperties);
- this.PBR = true;
- this.NUM_SAMPLES = "0";
- this.REALTIME_FILTERING = false;
- this.MAINUV1 = false;
- this.MAINUV2 = false;
- this.MAINUV3 = false;
- this.MAINUV4 = false;
- this.MAINUV5 = false;
- this.MAINUV6 = false;
- this.UV1 = false;
- this.UV2 = false;
- this.UV3 = false;
- this.UV4 = false;
- this.UV5 = false;
- this.UV6 = false;
- this.ALBEDO = false;
- this.GAMMAALBEDO = false;
- this.ALBEDODIRECTUV = 0;
- this.VERTEXCOLOR = false;
- this.BAKED_VERTEX_ANIMATION_TEXTURE = false;
- this.AMBIENT = false;
- this.AMBIENTDIRECTUV = 0;
- this.AMBIENTINGRAYSCALE = false;
- this.OPACITY = false;
- this.VERTEXALPHA = false;
- this.OPACITYDIRECTUV = 0;
- this.OPACITYRGB = false;
- this.ALPHATEST = false;
- this.DEPTHPREPASS = false;
- this.ALPHABLEND = false;
- this.ALPHAFROMALBEDO = false;
- this.ALPHATESTVALUE = "0.5";
- this.SPECULAROVERALPHA = false;
- this.RADIANCEOVERALPHA = false;
- this.ALPHAFRESNEL = false;
- this.LINEARALPHAFRESNEL = false;
- this.PREMULTIPLYALPHA = false;
- this.EMISSIVE = false;
- this.EMISSIVEDIRECTUV = 0;
- this.GAMMAEMISSIVE = false;
- this.REFLECTIVITY = false;
- this.REFLECTIVITY_GAMMA = false;
- this.REFLECTIVITYDIRECTUV = 0;
- this.SPECULARTERM = false;
- this.MICROSURFACEFROMREFLECTIVITYMAP = false;
- this.MICROSURFACEAUTOMATIC = false;
- this.LODBASEDMICROSFURACE = false;
- this.MICROSURFACEMAP = false;
- this.MICROSURFACEMAPDIRECTUV = 0;
- this.METALLICWORKFLOW = false;
- this.ROUGHNESSSTOREINMETALMAPALPHA = false;
- this.ROUGHNESSSTOREINMETALMAPGREEN = false;
- this.METALLNESSSTOREINMETALMAPBLUE = false;
- this.AOSTOREINMETALMAPRED = false;
- this.METALLIC_REFLECTANCE = false;
- this.METALLIC_REFLECTANCE_GAMMA = false;
- this.METALLIC_REFLECTANCEDIRECTUV = 0;
- this.METALLIC_REFLECTANCE_USE_ALPHA_ONLY = false;
- this.REFLECTANCE = false;
- this.REFLECTANCE_GAMMA = false;
- this.REFLECTANCEDIRECTUV = 0;
- this.ENVIRONMENTBRDF = false;
- this.ENVIRONMENTBRDF_RGBD = false;
- this.NORMAL = false;
- this.TANGENT = false;
- this.BUMP = false;
- this.BUMPDIRECTUV = 0;
- this.OBJECTSPACE_NORMALMAP = false;
- this.PARALLAX = false;
- this.PARALLAX_RHS = false;
- this.PARALLAXOCCLUSION = false;
- this.NORMALXYSCALE = true;
- this.LIGHTMAP = false;
- this.LIGHTMAPDIRECTUV = 0;
- this.USELIGHTMAPASSHADOWMAP = false;
- this.GAMMALIGHTMAP = false;
- this.RGBDLIGHTMAP = false;
- this.REFLECTION = false;
- this.REFLECTIONMAP_3D = false;
- this.REFLECTIONMAP_SPHERICAL = false;
- this.REFLECTIONMAP_PLANAR = false;
- this.REFLECTIONMAP_CUBIC = false;
- this.USE_LOCAL_REFLECTIONMAP_CUBIC = false;
- this.REFLECTIONMAP_PROJECTION = false;
- this.REFLECTIONMAP_SKYBOX = false;
- this.REFLECTIONMAP_EXPLICIT = false;
- this.REFLECTIONMAP_EQUIRECTANGULAR = false;
- this.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
- this.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
- this.INVERTCUBICMAP = false;
- this.USESPHERICALFROMREFLECTIONMAP = false;
- this.USEIRRADIANCEMAP = false;
- this.USESPHERICALINVERTEX = false;
- this.REFLECTIONMAP_OPPOSITEZ = false;
- this.LODINREFLECTIONALPHA = false;
- this.GAMMAREFLECTION = false;
- this.RGBDREFLECTION = false;
- this.LINEARSPECULARREFLECTION = false;
- this.RADIANCEOCCLUSION = false;
- this.HORIZONOCCLUSION = false;
- this.INSTANCES = false;
- this.THIN_INSTANCES = false;
- this.INSTANCESCOLOR = false;
- this.PREPASS = false;
- this.PREPASS_IRRADIANCE = false;
- this.PREPASS_IRRADIANCE_INDEX = -1;
- this.PREPASS_ALBEDO_SQRT = false;
- this.PREPASS_ALBEDO_SQRT_INDEX = -1;
- this.PREPASS_DEPTH = false;
- this.PREPASS_DEPTH_INDEX = -1;
- this.PREPASS_NORMAL = false;
- this.PREPASS_NORMAL_INDEX = -1;
- this.PREPASS_NORMAL_WORLDSPACE = false;
- this.PREPASS_POSITION = false;
- this.PREPASS_POSITION_INDEX = -1;
- this.PREPASS_VELOCITY = false;
- this.PREPASS_VELOCITY_INDEX = -1;
- this.PREPASS_REFLECTIVITY = false;
- this.PREPASS_REFLECTIVITY_INDEX = -1;
- this.SCENE_MRT_COUNT = 0;
- this.NUM_BONE_INFLUENCERS = 0;
- this.BonesPerMesh = 0;
- this.BONETEXTURE = false;
- this.BONES_VELOCITY_ENABLED = false;
- this.NONUNIFORMSCALING = false;
- this.MORPHTARGETS = false;
- this.MORPHTARGETS_NORMAL = false;
- this.MORPHTARGETS_TANGENT = false;
- this.MORPHTARGETS_UV = false;
- this.NUM_MORPH_INFLUENCERS = 0;
- this.MORPHTARGETS_TEXTURE = false;
- this.IMAGEPROCESSING = false;
- this.VIGNETTE = false;
- this.VIGNETTEBLENDMODEMULTIPLY = false;
- this.VIGNETTEBLENDMODEOPAQUE = false;
- this.TONEMAPPING = false;
- this.TONEMAPPING_ACES = false;
- this.CONTRAST = false;
- this.COLORCURVES = false;
- this.COLORGRADING = false;
- this.COLORGRADING3D = false;
- this.SAMPLER3DGREENDEPTH = false;
- this.SAMPLER3DBGRMAP = false;
- this.DITHER = false;
- this.IMAGEPROCESSINGPOSTPROCESS = false;
- this.SKIPFINALCOLORCLAMP = false;
- this.EXPOSURE = false;
- this.MULTIVIEW = false;
- this.ORDER_INDEPENDENT_TRANSPARENCY = false;
- this.ORDER_INDEPENDENT_TRANSPARENCY_16BITS = false;
- this.USEPHYSICALLIGHTFALLOFF = false;
- this.USEGLTFLIGHTFALLOFF = false;
- this.TWOSIDEDLIGHTING = false;
- this.SHADOWFLOAT = false;
- this.CLIPPLANE = false;
- this.CLIPPLANE2 = false;
- this.CLIPPLANE3 = false;
- this.CLIPPLANE4 = false;
- this.CLIPPLANE5 = false;
- this.CLIPPLANE6 = false;
- this.POINTSIZE = false;
- this.FOG = false;
- this.LOGARITHMICDEPTH = false;
- this.CAMERA_ORTHOGRAPHIC = false;
- this.CAMERA_PERSPECTIVE = false;
- this.FORCENORMALFORWARD = false;
- this.SPECULARAA = false;
- this.UNLIT = false;
- this.DECAL_AFTER_DETAIL = false;
- this.DEBUGMODE = 0;
- this.rebuild();
- }
- /**
- * Resets the PBR Material defines.
- */
- reset() {
- super.reset();
- this.ALPHATESTVALUE = "0.5";
- this.PBR = true;
- this.NORMALXYSCALE = 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/features/featuresDeepDive/materials/using/introToPBR
- */
- export class PBRBaseMaterial extends PushMaterial {
- /**
- * Enables realtime filtering on the texture.
- */
- get realTimeFiltering() {
- return this._realTimeFiltering;
- }
- set realTimeFiltering(b) {
- this._realTimeFiltering = b;
- this.markAsDirty(1);
- }
- /**
- * Quality switch for realtime filtering
- */
- get realTimeFilteringQuality() {
- return this._realTimeFilteringQuality;
- }
- set realTimeFilteringQuality(n) {
- this._realTimeFilteringQuality = n;
- this.markAsDirty(1);
- }
- /**
- * Can this material render to several textures at once
- */
- get canRenderToMRT() {
- return true;
- }
- /**
- * Attaches a new image processing configuration to the PBR Material.
- * @param configuration
- */
- _attachImageProcessingConfiguration(configuration) {
- 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();
- });
- }
- }
- /**
- * Instantiates a new PBRMaterial instance.
- *
- * @param name The material name
- * @param scene The scene the material will be use in.
- */
- constructor(name, scene) {
- super(name, scene);
- /**
- * Intensity of the direct lights e.g. the four lights available in your scene.
- * This impacts both the direct diffuse and specular highlights.
- * @internal
- */
- this._directIntensity = 1.0;
- /**
- * Intensity of the emissive part of the material.
- * This helps controlling the emissive effect without modifying the emissive color.
- * @internal
- */
- this._emissiveIntensity = 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 reflection for shiny ones.
- * @internal
- */
- this._environmentIntensity = 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.
- * @internal
- */
- this._specularIntensity = 1.0;
- /**
- * This stores the direct, emissive, environment, and specular light intensities into a Vector4.
- */
- this._lightingInfos = new Vector4(this._directIntensity, this._emissiveIntensity, this._environmentIntensity, this._specularIntensity);
- /**
- * Debug Control allowing disabling the bump map on this material.
- * @internal
- */
- this._disableBumpMap = false;
- /**
- * AKA Diffuse Texture in standard nomenclature.
- * @internal
- */
- this._albedoTexture = null;
- /**
- * AKA Occlusion Texture in other nomenclature.
- * @internal
- */
- this._ambientTexture = null;
- /**
- * AKA Occlusion Texture Intensity in other nomenclature.
- * @internal
- */
- this._ambientTextureStrength = 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
- * @internal
- */
- this._ambientTextureImpactOnAnalyticalLights = PBRBaseMaterial.DEFAULT_AO_ON_ANALYTICAL_LIGHTS;
- /**
- * Stores the alpha values in a texture.
- * @internal
- */
- this._opacityTexture = null;
- /**
- * Stores the reflection values in a texture.
- * @internal
- */
- this._reflectionTexture = null;
- /**
- * Stores the emissive values in a texture.
- * @internal
- */
- this._emissiveTexture = null;
- /**
- * AKA Specular texture in other nomenclature.
- * @internal
- */
- this._reflectivityTexture = null;
- /**
- * Used to switch from specular/glossiness to metallic/roughness workflow.
- * @internal
- */
- this._metallicTexture = null;
- /**
- * Specifies the metallic scalar of the metallic/roughness workflow.
- * Can also be used to scale the metalness values of the metallic texture.
- * @internal
- */
- this._metallic = null;
- /**
- * Specifies the roughness scalar of the metallic/roughness workflow.
- * Can also be used to scale the roughness values of the metallic texture.
- * @internal
- */
- this._roughness = null;
- /**
- * In metallic workflow, specifies an F0 factor to help configuring the material F0.
- * By default the indexOfrefraction is used to compute F0;
- *
- * This is used as a factor against the default reflectance at normal incidence to tweak it.
- *
- * F0 = defaultF0 * metallicF0Factor * metallicReflectanceColor;
- * F90 = metallicReflectanceColor;
- * @internal
- */
- this._metallicF0Factor = 1;
- /**
- * In metallic workflow, specifies an F0 color.
- * By default the F90 is always 1;
- *
- * Please note that this factor is also used as a factor against the default reflectance at normal incidence.
- *
- * F0 = defaultF0_from_IOR * metallicF0Factor * metallicReflectanceColor
- * F90 = metallicF0Factor;
- * @internal
- */
- this._metallicReflectanceColor = Color3.White();
- /**
- * Specifies that only the A channel from _metallicReflectanceTexture should be used.
- * If false, both RGB and A channels will be used
- * @internal
- */
- this._useOnlyMetallicFromMetallicReflectanceTexture = false;
- /**
- * Defines to store metallicReflectanceColor in RGB and metallicF0Factor in A
- * This is multiply against the scalar values defined in the material.
- * @internal
- */
- this._metallicReflectanceTexture = null;
- /**
- * Defines to store reflectanceColor in RGB
- * This is multiplied against the scalar values defined in the material.
- * If both _reflectanceTexture and _metallicReflectanceTexture textures are provided and _useOnlyMetallicFromMetallicReflectanceTexture
- * is false, _metallicReflectanceTexture takes precedence and _reflectanceTexture is not used
- * @internal
- */
- this._reflectanceTexture = null;
- /**
- * 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.
- * @internal
- */
- this._microSurfaceTexture = null;
- /**
- * Stores surface normal data used to displace a mesh in a texture.
- * @internal
- */
- this._bumpTexture = null;
- /**
- * Stores the pre-calculated light information of a mesh in a texture.
- * @internal
- */
- this._lightmapTexture = null;
- /**
- * The color of a material in ambient lighting.
- * @internal
- */
- this._ambientColor = new Color3(0, 0, 0);
- /**
- * AKA Diffuse Color in other nomenclature.
- * @internal
- */
- this._albedoColor = new Color3(1, 1, 1);
- /**
- * AKA Specular Color in other nomenclature.
- * @internal
- */
- this._reflectivityColor = new Color3(1, 1, 1);
- /**
- * The color applied when light is reflected from a material.
- * @internal
- */
- this._reflectionColor = new Color3(1, 1, 1);
- /**
- * The color applied when light is emitted from a material.
- * @internal
- */
- this._emissiveColor = new Color3(0, 0, 0);
- /**
- * AKA Glossiness in other nomenclature.
- * @internal
- */
- this._microSurface = 0.9;
- /**
- * Specifies that the material will use the light map as a show map.
- * @internal
- */
- this._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).
- * @internal
- */
- this._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.
- * @internal
- */
- this._useRadianceOcclusion = true;
- /**
- * Specifies that the alpha is coming form the albedo channel alpha channel for alpha blending.
- * @internal
- */
- this._useAlphaFromAlbedoTexture = false;
- /**
- * Specifies that the material will keeps the specular highlights over a transparent surface (only the most luminous ones).
- * A car glass is a good example of that. When sun reflects on it you can not see what is behind.
- * @internal
- */
- this._useSpecularOverAlpha = true;
- /**
- * Specifies if the reflectivity texture contains the glossiness information in its alpha channel.
- * @internal
- */
- this._useMicroSurfaceFromReflectivityMapAlpha = false;
- /**
- * Specifies if the metallic texture contains the roughness information in its alpha channel.
- * @internal
- */
- this._useRoughnessFromMetallicTextureAlpha = true;
- /**
- * Specifies if the metallic texture contains the roughness information in its green channel.
- * @internal
- */
- this._useRoughnessFromMetallicTextureGreen = false;
- /**
- * Specifies if the metallic texture contains the metallness information in its blue channel.
- * @internal
- */
- this._useMetallnessFromMetallicTextureBlue = false;
- /**
- * Specifies if the metallic texture contains the ambient occlusion information in its red channel.
- * @internal
- */
- this._useAmbientOcclusionFromMetallicTextureRed = false;
- /**
- * Specifies if the ambient texture contains the ambient occlusion information in its red channel only.
- * @internal
- */
- this._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.
- * @internal
- */
- this._useAutoMicroSurfaceFromReflectivityMap = false;
- /**
- * Defines the falloff type used in this material.
- * It by default is Physical.
- * @internal
- */
- this._lightFalloff = PBRBaseMaterial.LIGHTFALLOFF_PHYSICAL;
- /**
- * Specifies that the material will keeps the reflection highlights over a transparent surface (only the most luminous ones).
- * A car glass is a good example of that. When the street lights reflects on it you can not see what is behind.
- * @internal
- */
- this._useRadianceOverAlpha = true;
- /**
- * Allows using an object space normal map (instead of tangent space).
- * @internal
- */
- this._useObjectSpaceNormalMap = false;
- /**
- * Allows using the bump map in parallax mode.
- * @internal
- */
- this._useParallax = false;
- /**
- * Allows using the bump map in parallax occlusion mode.
- * @internal
- */
- this._useParallaxOcclusion = false;
- /**
- * Controls the scale bias of the parallax mode.
- * @internal
- */
- this._parallaxScaleBias = 0.05;
- /**
- * If sets to true, disables all the lights affecting the material.
- * @internal
- */
- this._disableLighting = false;
- /**
- * Number of Simultaneous lights allowed on the material.
- * @internal
- */
- this._maxSimultaneousLights = 4;
- /**
- * If sets to true, x component of normal map value will be inverted (x = 1.0 - x).
- * @internal
- */
- this._invertNormalMapX = false;
- /**
- * If sets to true, y component of normal map value will be inverted (y = 1.0 - y).
- * @internal
- */
- this._invertNormalMapY = false;
- /**
- * If sets to true and backfaceCulling is false, normals will be flipped on the backside.
- * @internal
- */
- this._twoSidedLighting = false;
- /**
- * Defines the alpha limits in alpha test mode.
- * @internal
- */
- this._alphaCutOff = 0.4;
- /**
- * Enforces alpha test in opaque or blend mode in order to improve the performances of some situations.
- * @internal
- */
- this._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)
- * @internal
- */
- this._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)
- * @internal
- */
- this._useLinearAlphaFresnel = false;
- /**
- * Specifies the environment BRDF texture used to compute the scale and offset roughness values
- * from cos theta and roughness:
- * http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
- * @internal
- */
- this._environmentBRDFTexture = null;
- /**
- * Force the shader to compute irradiance in the fragment shader in order to take bump in account.
- * @internal
- */
- this._forceIrradianceInFragment = false;
- this._realTimeFiltering = false;
- this._realTimeFilteringQuality = 8;
- /**
- * Force normal to face away from face.
- * @internal
- */
- this._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.
- * @internal
- */
- this._enableSpecularAntiAliasing = false;
- /**
- * Keep track of the image processing observer to allow dispose and replace.
- */
- this._imageProcessingObserver = null;
- /**
- * Stores the available render targets.
- */
- this._renderTargets = new SmartArray(16);
- /**
- * Sets the global ambient color for the material used in lighting calculations.
- */
- this._globalAmbientColor = new Color3(0, 0, 0);
- /**
- * If set to true, no lighting calculations will be applied.
- */
- this._unlit = false;
- /**
- * If sets to true, the decal map will be applied after the detail map. Else, it is applied before (default: false)
- */
- this._applyDecalMapAfterDetailMap = false;
- this._debugMode = 0;
- /**
- * @internal
- * This is reserved for the inspector.
- * Defines the material debug mode.
- * It helps seeing only some components of the material while troubleshooting.
- */
- this.debugMode = 0;
- /**
- * @internal
- * 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
- */
- this.debugLimit = -1;
- /**
- * @internal
- * 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.
- */
- this.debugFactor = 1;
- this._cacheHasRenderTargetTextures = false;
- this.brdf = new PBRBRDFConfiguration(this);
- this.clearCoat = new PBRClearCoatConfiguration(this);
- this.iridescence = new PBRIridescenceConfiguration(this);
- this.anisotropy = new PBRAnisotropicConfiguration(this);
- this.sheen = new PBRSheenConfiguration(this);
- this.subSurface = new PBRSubSurfaceConfiguration(this);
- this.detailMap = new DetailMapConfiguration(this);
- // Setup the default processing configuration to the scene.
- this._attachImageProcessingConfiguration(null);
- this.getRenderTargetTextures = () => {
- this._renderTargets.reset();
- if (MaterialFlags.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
- this._renderTargets.push(this._reflectionTexture);
- }
- this._eventInfo.renderTargets = this._renderTargets;
- this._callbackPluginEventFillRenderTargetTextures(this._eventInfo);
- return this._renderTargets;
- };
- this._environmentBRDFTexture = GetEnvironmentBRDFTexture(this.getScene());
- this.prePassConfiguration = new PrePassConfiguration();
- }
- /**
- * Gets a boolean indicating that current material needs to register RTT
- */
- get hasRenderTargetTextures() {
- if (MaterialFlags.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
- return true;
- }
- return this._cacheHasRenderTargetTextures;
- }
- /**
- * Can this material render to prepass
- */
- get isPrePassCapable() {
- return !this.disableDepthWrite;
- }
- /**
- * @returns the name of the material class.
- */
- getClassName() {
- return "PBRBaseMaterial";
- }
- /**
- * Returns true if alpha blending should be disabled.
- */
- get _disableAlphaBlending() {
- return (this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_OPAQUE ||
- this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_ALPHATEST ||
- this.subSurface?.disableAlphaBlending);
- }
- /**
- * @returns whether or not this material should be rendered in alpha blend mode.
- */
- needAlphaBlending() {
- if (this._disableAlphaBlending) {
- return false;
- }
- return this.alpha < 1.0 || this._opacityTexture != null || this._shouldUseAlphaFromAlbedoTexture();
- }
- /**
- * @returns whether or not this material should be rendered in alpha test mode.
- */
- needAlphaTesting() {
- if (this._forceAlphaTest) {
- return true;
- }
- if (this.subSurface?.disableAlphaBlending) {
- return false;
- }
- return this._hasAlphaChannel() && (this._transparencyMode == null || this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_ALPHATEST);
- }
- /**
- * @returns whether or not the alpha value of the albedo texture should be used for alpha blending.
- */
- _shouldUseAlphaFromAlbedoTexture() {
- return this._albedoTexture != null && this._albedoTexture.hasAlpha && this._useAlphaFromAlbedoTexture && this._transparencyMode !== PBRBaseMaterial.PBRMATERIAL_OPAQUE;
- }
- /**
- * @returns whether or not there is a usable alpha channel for transparency.
- */
- _hasAlphaChannel() {
- return (this._albedoTexture != null && this._albedoTexture.hasAlpha) || this._opacityTexture != null;
- }
- /**
- * @returns the texture used for the alpha test.
- */
- getAlphaTestTexture() {
- 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.
- */
- isReadyForSubMesh(mesh, subMesh, useInstances) {
- if (!this._uniformBufferLayoutBuilt) {
- this.buildUniformLayout();
- }
- const drawWrapper = subMesh._drawWrapper;
- if (drawWrapper.effect && this.isFrozen) {
- if (drawWrapper._wasPreviouslyReady && drawWrapper._wasPreviouslyUsingInstances === useInstances) {
- return true;
- }
- }
- if (!subMesh.materialDefines) {
- this._callbackPluginEventGeneric(MaterialPluginEvent.GetDefineNames, this._eventInfo);
- subMesh.materialDefines = new PBRMaterialDefines(this._eventInfo.defineNames);
- }
- const defines = subMesh.materialDefines;
- if (this._isReadyForSubMesh(subMesh)) {
- return true;
- }
- const scene = this.getScene();
- const engine = scene.getEngine();
- if (defines._areTexturesDirty) {
- this._eventInfo.hasRenderTargetTextures = false;
- this._callbackPluginEventHasRenderTargetTextures(this._eventInfo);
- this._cacheHasRenderTargetTextures = this._eventInfo.hasRenderTargetTextures;
- 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;
- }
- }
- const reflectionTexture = this._getReflectionTexture();
- if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
- if (!reflectionTexture.isReadyOrNotBlocking()) {
- return false;
- }
- if (reflectionTexture.irradianceTexture) {
- if (!reflectionTexture.irradianceTexture.isReadyOrNotBlocking()) {
- return false;
- }
- }
- else {
- // Not ready until spherical are ready too.
- if (!reflectionTexture.sphericalPolynomial && reflectionTexture.getInternalTexture()?._sphericalPolynomialPromise) {
- 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._metallicReflectanceTexture) {
- if (!this._metallicReflectanceTexture.isReadyOrNotBlocking()) {
- return false;
- }
- }
- if (this._reflectanceTexture) {
- if (!this._reflectanceTexture.isReadyOrNotBlocking()) {
- return false;
- }
- }
- if (this._microSurfaceTexture) {
- if (!this._microSurfaceTexture.isReadyOrNotBlocking()) {
- return false;
- }
- }
- }
- if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
- // Bump texture cannot be not blocking.
- if (!this._bumpTexture.isReady()) {
- return false;
- }
- }
- if (this._environmentBRDFTexture && MaterialFlags.ReflectionTextureEnabled) {
- // This is blocking.
- if (!this._environmentBRDFTexture.isReady()) {
- return false;
- }
- }
- }
- }
- this._eventInfo.isReadyForSubMesh = true;
- this._eventInfo.defines = defines;
- this._eventInfo.subMesh = subMesh;
- this._callbackPluginEventIsReadyForSubMesh(this._eventInfo);
- if (!this._eventInfo.isReadyForSubMesh) {
- return false;
- }
- if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
- if (!this._imageProcessingConfiguration.isReady()) {
- return false;
- }
- }
- if (!engine.getCaps().standardDerivatives && !mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
- mesh.createNormals(true);
- Logger.Warn("PBRMaterial: Normals have been created for the mesh: " + mesh.name);
- }
- const previousEffect = subMesh.effect;
- const lightDisposed = defines._areLightsDisposed;
- let effect = this._prepareEffect(mesh, defines, this.onCompiled, this.onError, useInstances, null, subMesh.getRenderingMesh().hasThinInstances);
- let forceWasNotReadyPreviously = false;
- if (effect) {
- if (this._onEffectCreatedObservable) {
- onCreatedEffectParameters.effect = effect;
- onCreatedEffectParameters.subMesh = subMesh;
- this._onEffectCreatedObservable.notifyObservers(onCreatedEffectParameters);
- }
- // Use previous effect while new one is compiling
- if (this.allowShaderHotSwapping && previousEffect && !effect.isReady()) {
- effect = previousEffect;
- defines.markAsUnprocessed();
- forceWasNotReadyPreviously = this.isFrozen;
- if (lightDisposed) {
- // re register in case it takes more than one frame.
- defines._areLightsDisposed = true;
- return false;
- }
- }
- else {
- scene.resetCachedMaterial();
- subMesh.setEffect(effect, defines, this._materialContext);
- }
- }
- if (!subMesh.effect || !subMesh.effect.isReady()) {
- return false;
- }
- defines._renderId = scene.getRenderId();
- drawWrapper._wasPreviouslyReady = forceWasNotReadyPreviously ? false : true;
- drawWrapper._wasPreviouslyUsingInstances = !!useInstances;
- this._checkScenePerformancePriority();
- return true;
- }
- /**
- * Specifies if the material uses metallic roughness workflow.
- * @returns boolean specifying if the material uses metallic roughness workflow.
- */
- isMetallicWorkflow() {
- if (this._metallic != null || this._roughness != null || this._metallicTexture) {
- return true;
- }
- return false;
- }
- _prepareEffect(mesh, defines, onCompiled = null, onError = null, useInstances = null, useClipPlane = null, useThinInstances) {
- this._prepareDefines(mesh, defines, useInstances, useClipPlane, useThinInstances);
- if (!defines.isDirty) {
- return null;
- }
- defines.markAsProcessed();
- const scene = this.getScene();
- const engine = scene.getEngine();
- // Fallbacks
- const fallbacks = new EffectFallbacks();
- let fallbackRank = 0;
- if (defines.USESPHERICALINVERTEX) {
- fallbacks.addFallback(fallbackRank++, "USESPHERICALINVERTEX");
- }
- if (defines.FOG) {
- fallbacks.addFallback(fallbackRank, "FOG");
- }
- if (defines.SPECULARAA) {
- fallbacks.addFallback(fallbackRank, "SPECULARAA");
- }
- if (defines.POINTSIZE) {
- fallbacks.addFallback(fallbackRank, "POINTSIZE");
- }
- if (defines.LOGARITHMICDEPTH) {
- fallbacks.addFallback(fallbackRank, "LOGARITHMICDEPTH");
- }
- if (defines.PARALLAX) {
- fallbacks.addFallback(fallbackRank, "PARALLAX");
- }
- if (defines.PARALLAX_RHS) {
- fallbacks.addFallback(fallbackRank, "PARALLAX_RHS");
- }
- if (defines.PARALLAXOCCLUSION) {
- fallbacks.addFallback(fallbackRank++, "PARALLAXOCCLUSION");
- }
- if (defines.ENVIRONMENTBRDF) {
- fallbacks.addFallback(fallbackRank++, "ENVIRONMENTBRDF");
- }
- if (defines.TANGENT) {
- fallbacks.addFallback(fallbackRank++, "TANGENT");
- }
- if (defines.BUMP) {
- fallbacks.addFallback(fallbackRank++, "BUMP");
- }
- fallbackRank = HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights, fallbackRank++);
- if (defines.SPECULARTERM) {
- fallbacks.addFallback(fallbackRank++, "SPECULARTERM");
- }
- if (defines.USESPHERICALFROMREFLECTIONMAP) {
- fallbacks.addFallback(fallbackRank++, "USESPHERICALFROMREFLECTIONMAP");
- }
- if (defines.USEIRRADIANCEMAP) {
- fallbacks.addFallback(fallbackRank++, "USEIRRADIANCEMAP");
- }
- if (defines.LIGHTMAP) {
- fallbacks.addFallback(fallbackRank++, "LIGHTMAP");
- }
- if (defines.NORMAL) {
- fallbacks.addFallback(fallbackRank++, "NORMAL");
- }
- if (defines.AMBIENT) {
- fallbacks.addFallback(fallbackRank++, "AMBIENT");
- }
- if (defines.EMISSIVE) {
- fallbacks.addFallback(fallbackRank++, "EMISSIVE");
- }
- if (defines.VERTEXCOLOR) {
- fallbacks.addFallback(fallbackRank++, "VERTEXCOLOR");
- }
- if (defines.MORPHTARGETS) {
- fallbacks.addFallback(fallbackRank++, "MORPHTARGETS");
- }
- if (defines.MULTIVIEW) {
- fallbacks.addFallback(0, "MULTIVIEW");
- }
- //Attributes
- const attribs = [VertexBuffer.PositionKind];
- if (defines.NORMAL) {
- attribs.push(VertexBuffer.NormalKind);
- }
- if (defines.TANGENT) {
- attribs.push(VertexBuffer.TangentKind);
- }
- for (let i = 1; i <= 6; ++i) {
- if (defines["UV" + i]) {
- attribs.push(`uv${i === 1 ? "" : i}`);
- }
- }
- if (defines.VERTEXCOLOR) {
- attribs.push(VertexBuffer.ColorKind);
- }
- PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
- PrepareAttributesForInstances(attribs, defines);
- PrepareAttributesForMorphTargets(attribs, mesh, defines);
- PrepareAttributesForBakedVertexAnimation(attribs, mesh, defines);
- let shaderName = "pbr";
- const uniforms = [
- "world",
- "view",
- "viewProjection",
- "vEyePosition",
- "vLightsType",
- "vAmbientColor",
- "vAlbedoColor",
- "vReflectivityColor",
- "vMetallicReflectanceFactors",
- "vEmissiveColor",
- "visibility",
- "vReflectionColor",
- "vFogInfos",
- "vFogColor",
- "pointSize",
- "vAlbedoInfos",
- "vAmbientInfos",
- "vOpacityInfos",
- "vReflectionInfos",
- "vReflectionPosition",
- "vReflectionSize",
- "vEmissiveInfos",
- "vReflectivityInfos",
- "vReflectionFilteringInfo",
- "vMetallicReflectanceInfos",
- "vReflectanceInfos",
- "vMicroSurfaceSamplerInfos",
- "vBumpInfos",
- "vLightmapInfos",
- "mBones",
- "albedoMatrix",
- "ambientMatrix",
- "opacityMatrix",
- "reflectionMatrix",
- "emissiveMatrix",
- "reflectivityMatrix",
- "normalMatrix",
- "microSurfaceSamplerMatrix",
- "bumpMatrix",
- "lightmapMatrix",
- "metallicReflectanceMatrix",
- "reflectanceMatrix",
- "vLightingIntensity",
- "logarithmicDepthConstant",
- "vSphericalX",
- "vSphericalY",
- "vSphericalZ",
- "vSphericalXX_ZZ",
- "vSphericalYY_ZZ",
- "vSphericalZZ",
- "vSphericalXY",
- "vSphericalYZ",
- "vSphericalZX",
- "vSphericalL00",
- "vSphericalL1_1",
- "vSphericalL10",
- "vSphericalL11",
- "vSphericalL2_2",
- "vSphericalL2_1",
- "vSphericalL20",
- "vSphericalL21",
- "vSphericalL22",
- "vReflectionMicrosurfaceInfos",
- "vTangentSpaceParams",
- "boneTextureWidth",
- "vDebugMode",
- "morphTargetTextureInfo",
- "morphTargetTextureIndices",
- ];
- const samplers = [
- "albedoSampler",
- "reflectivitySampler",
- "ambientSampler",
- "emissiveSampler",
- "bumpSampler",
- "lightmapSampler",
- "opacitySampler",
- "reflectionSampler",
- "reflectionSamplerLow",
- "reflectionSamplerHigh",
- "irradianceSampler",
- "microSurfaceSampler",
- "environmentBrdfSampler",
- "boneSampler",
- "metallicReflectanceSampler",
- "reflectanceSampler",
- "morphTargets",
- "oitDepthSampler",
- "oitFrontColorSampler",
- ];
- const uniformBuffers = ["Material", "Scene", "Mesh"];
- const indexParameters = { maxSimultaneousLights: this._maxSimultaneousLights, maxSimultaneousMorphTargets: defines.NUM_MORPH_INFLUENCERS };
- this._eventInfo.fallbacks = fallbacks;
- this._eventInfo.fallbackRank = fallbackRank;
- this._eventInfo.defines = defines;
- this._eventInfo.uniforms = uniforms;
- this._eventInfo.attributes = attribs;
- this._eventInfo.samplers = samplers;
- this._eventInfo.uniformBuffersNames = uniformBuffers;
- this._eventInfo.customCode = undefined;
- this._eventInfo.mesh = mesh;
- this._eventInfo.indexParameters = indexParameters;
- this._callbackPluginEventGeneric(MaterialPluginEvent.PrepareEffect, this._eventInfo);
- PrePassConfiguration.AddUniforms(uniforms);
- PrePassConfiguration.AddSamplers(samplers);
- addClipPlaneUniforms(uniforms);
- if (ImageProcessingConfiguration) {
- ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
- ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
- }
- PrepareUniformsAndSamplersList({
- uniformsNames: uniforms,
- uniformBuffersNames: uniformBuffers,
- samplers: samplers,
- defines: defines,
- maxSimultaneousLights: this._maxSimultaneousLights,
- });
- const csnrOptions = {};
- if (this.customShaderNameResolve) {
- shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines, attribs, csnrOptions);
- }
- const join = defines.toString();
- const effect = engine.createEffect(shaderName, {
- attributes: attribs,
- uniformsNames: uniforms,
- uniformBuffersNames: uniformBuffers,
- samplers: samplers,
- defines: join,
- fallbacks: fallbacks,
- onCompiled: onCompiled,
- onError: onError,
- indexParameters,
- processFinalCode: csnrOptions.processFinalCode,
- processCodeAfterIncludes: this._eventInfo.customCode,
- multiTarget: defines.PREPASS,
- }, engine);
- this._eventInfo.customCode = undefined;
- return effect;
- }
- _prepareDefines(mesh, defines, useInstances = null, useClipPlane = null, useThinInstances = false) {
- const scene = this.getScene();
- const engine = scene.getEngine();
- // Lights
- PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
- defines._needNormals = true;
- // Multiview
- PrepareDefinesForMultiview(scene, defines);
- // PrePass
- const oit = this.needAlphaBlendingForMesh(mesh) && this.getScene().useOrderIndependentTransparency;
- PrepareDefinesForPrePass(scene, defines, this.canRenderToMRT && !oit);
- // Order independant transparency
- PrepareDefinesForOIT(scene, defines, oit);
- // Textures
- defines.METALLICWORKFLOW = this.isMetallicWorkflow();
- if (defines._areTexturesDirty) {
- defines._needUVs = false;
- for (let i = 1; i <= 6; ++i) {
- defines["MAINUV" + i] = false;
- }
- if (scene.texturesEnabled) {
- defines.ALBEDODIRECTUV = 0;
- defines.AMBIENTDIRECTUV = 0;
- defines.OPACITYDIRECTUV = 0;
- defines.EMISSIVEDIRECTUV = 0;
- defines.REFLECTIVITYDIRECTUV = 0;
- defines.MICROSURFACEMAPDIRECTUV = 0;
- defines.METALLIC_REFLECTANCEDIRECTUV = 0;
- defines.REFLECTANCEDIRECTUV = 0;
- defines.BUMPDIRECTUV = 0;
- defines.LIGHTMAPDIRECTUV = 0;
- if (engine.getCaps().textureLOD) {
- defines.LODBASEDMICROSFURACE = true;
- }
- if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
- PrepareDefinesForMergedUV(this._albedoTexture, defines, "ALBEDO");
- defines.GAMMAALBEDO = this._albedoTexture.gammaSpace;
- }
- else {
- defines.ALBEDO = false;
- }
- if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
- PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
- defines.AMBIENTINGRAYSCALE = this._useAmbientInGrayScale;
- }
- else {
- defines.AMBIENT = false;
- }
- if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
- PrepareDefinesForMergedUV(this._opacityTexture, defines, "OPACITY");
- defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
- }
- else {
- defines.OPACITY = false;
- }
- const reflectionTexture = this._getReflectionTexture();
- if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
- defines.REFLECTION = true;
- defines.GAMMAREFLECTION = reflectionTexture.gammaSpace;
- defines.RGBDREFLECTION = reflectionTexture.isRGBD;
- defines.LODINREFLECTIONALPHA = reflectionTexture.lodLevelInAlpha;
- defines.LINEARSPECULARREFLECTION = reflectionTexture.linearSpecularLOD;
- if (this.realTimeFiltering && this.realTimeFilteringQuality > 0) {
- defines.NUM_SAMPLES = "" + this.realTimeFilteringQuality;
- if (engine._features.needTypeSuffixInShaderConstants) {
- defines.NUM_SAMPLES = defines.NUM_SAMPLES + "u";
- }
- defines.REALTIME_FILTERING = true;
- }
- else {
- defines.REALTIME_FILTERING = false;
- }
- defines.INVERTCUBICMAP = reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE;
- defines.REFLECTIONMAP_3D = reflectionTexture.isCube;
- defines.REFLECTIONMAP_OPPOSITEZ = defines.REFLECTIONMAP_3D && this.getScene().useRightHandedSystem ? !reflectionTexture.invertZ : reflectionTexture.invertZ;
- defines.REFLECTIONMAP_CUBIC = false;
- defines.REFLECTIONMAP_EXPLICIT = false;
- defines.REFLECTIONMAP_PLANAR = false;
- defines.REFLECTIONMAP_PROJECTION = false;
- defines.REFLECTIONMAP_SKYBOX = false;
- defines.REFLECTIONMAP_SPHERICAL = false;
- defines.REFLECTIONMAP_EQUIRECTANGULAR = false;
- defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
- defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
- switch (reflectionTexture.coordinatesMode) {
- case Texture.EXPLICIT_MODE:
- defines.REFLECTIONMAP_EXPLICIT = true;
- break;
- case Texture.PLANAR_MODE:
- defines.REFLECTIONMAP_PLANAR = true;
- break;
- case Texture.PROJECTION_MODE:
- defines.REFLECTIONMAP_PROJECTION = true;
- break;
- case Texture.SKYBOX_MODE:
- defines.REFLECTIONMAP_SKYBOX = true;
- break;
- case Texture.SPHERICAL_MODE:
- defines.REFLECTIONMAP_SPHERICAL = true;
- break;
- case Texture.EQUIRECTANGULAR_MODE:
- defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
- break;
- case Texture.FIXED_EQUIRECTANGULAR_MODE:
- defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
- break;
- case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
- defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = true;
- break;
- case Texture.CUBIC_MODE:
- case Texture.INVCUBIC_MODE:
- default:
- defines.REFLECTIONMAP_CUBIC = true;
- defines.USE_LOCAL_REFLECTIONMAP_CUBIC = reflectionTexture.boundingBoxSize ? true : false;
- break;
- }
- if (reflectionTexture.coordinatesMode !== Texture.SKYBOX_MODE) {
- if (reflectionTexture.irradianceTexture) {
- defines.USEIRRADIANCEMAP = true;
- defines.USESPHERICALFROMREFLECTIONMAP = false;
- }
- // Assume using spherical polynomial if the reflection texture is a cube map
- else if (reflectionTexture.isCube) {
- defines.USESPHERICALFROMREFLECTIONMAP = true;
- defines.USEIRRADIANCEMAP = false;
- if (this._forceIrradianceInFragment || this.realTimeFiltering || this._twoSidedLighting || engine.getCaps().maxVaryingVectors <= 8) {
- defines.USESPHERICALINVERTEX = false;
- }
- else {
- defines.USESPHERICALINVERTEX = true;
- }
- }
- }
- }
- else {
- defines.REFLECTION = false;
- defines.REFLECTIONMAP_3D = false;
- defines.REFLECTIONMAP_SPHERICAL = false;
- defines.REFLECTIONMAP_PLANAR = false;
- defines.REFLECTIONMAP_CUBIC = false;
- defines.USE_LOCAL_REFLECTIONMAP_CUBIC = false;
- defines.REFLECTIONMAP_PROJECTION = false;
- defines.REFLECTIONMAP_SKYBOX = false;
- defines.REFLECTIONMAP_EXPLICIT = false;
- defines.REFLECTIONMAP_EQUIRECTANGULAR = false;
- defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
- defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
- defines.INVERTCUBICMAP = false;
- defines.USESPHERICALFROMREFLECTIONMAP = false;
- defines.USEIRRADIANCEMAP = false;
- defines.USESPHERICALINVERTEX = false;
- defines.REFLECTIONMAP_OPPOSITEZ = false;
- defines.LODINREFLECTIONALPHA = false;
- defines.GAMMAREFLECTION = false;
- defines.RGBDREFLECTION = false;
- defines.LINEARSPECULARREFLECTION = false;
- }
- if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
- PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
- defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
- defines.GAMMALIGHTMAP = this._lightmapTexture.gammaSpace;
- defines.RGBDLIGHTMAP = this._lightmapTexture.isRGBD;
- }
- else {
- defines.LIGHTMAP = false;
- }
- if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
- PrepareDefinesForMergedUV(this._emissiveTexture, defines, "EMISSIVE");
- defines.GAMMAEMISSIVE = this._emissiveTexture.gammaSpace;
- }
- else {
- defines.EMISSIVE = false;
- }
- if (MaterialFlags.SpecularTextureEnabled) {
- if (this._metallicTexture) {
- PrepareDefinesForMergedUV(this._metallicTexture, defines, "REFLECTIVITY");
- defines.ROUGHNESSSTOREINMETALMAPALPHA = this._useRoughnessFromMetallicTextureAlpha;
- defines.ROUGHNESSSTOREINMETALMAPGREEN = !this._useRoughnessFromMetallicTextureAlpha && this._useRoughnessFromMetallicTextureGreen;
- defines.METALLNESSSTOREINMETALMAPBLUE = this._useMetallnessFromMetallicTextureBlue;
- defines.AOSTOREINMETALMAPRED = this._useAmbientOcclusionFromMetallicTextureRed;
- defines.REFLECTIVITY_GAMMA = false;
- }
- else if (this._reflectivityTexture) {
- PrepareDefinesForMergedUV(this._reflectivityTexture, defines, "REFLECTIVITY");
- defines.MICROSURFACEFROMREFLECTIVITYMAP = this._useMicroSurfaceFromReflectivityMapAlpha;
- defines.MICROSURFACEAUTOMATIC = this._useAutoMicroSurfaceFromReflectivityMap;
- defines.REFLECTIVITY_GAMMA = this._reflectivityTexture.gammaSpace;
- }
- else {
- defines.REFLECTIVITY = false;
- }
- if (this._metallicReflectanceTexture || this._reflectanceTexture) {
- defines.METALLIC_REFLECTANCE_USE_ALPHA_ONLY = this._useOnlyMetallicFromMetallicReflectanceTexture;
- if (this._metallicReflectanceTexture) {
- PrepareDefinesForMergedUV(this._metallicReflectanceTexture, defines, "METALLIC_REFLECTANCE");
- defines.METALLIC_REFLECTANCE_GAMMA = this._metallicReflectanceTexture.gammaSpace;
- }
- else {
- defines.METALLIC_REFLECTANCE = false;
- }
- if (this._reflectanceTexture &&
- (!this._metallicReflectanceTexture || (this._metallicReflectanceTexture && this._useOnlyMetallicFromMetallicReflectanceTexture))) {
- PrepareDefinesForMergedUV(this._reflectanceTexture, defines, "REFLECTANCE");
- defines.REFLECTANCE_GAMMA = this._reflectanceTexture.gammaSpace;
- }
- else {
- defines.REFLECTANCE = false;
- }
- }
- else {
- defines.METALLIC_REFLECTANCE = false;
- defines.REFLECTANCE = false;
- }
- if (this._microSurfaceTexture) {
- PrepareDefinesForMergedUV(this._microSurfaceTexture, defines, "MICROSURFACEMAP");
- }
- else {
- defines.MICROSURFACEMAP = false;
- }
- }
- else {
- defines.REFLECTIVITY = false;
- defines.MICROSURFACEMAP = false;
- }
- if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
- PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
- if (this._useParallax && this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
- defines.PARALLAX = true;
- defines.PARALLAX_RHS = scene.useRightHandedSystem;
- defines.PARALLAXOCCLUSION = !!this._useParallaxOcclusion;
- }
- else {
- defines.PARALLAX = false;
- }
- defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
- }
- else {
- defines.BUMP = false;
- defines.PARALLAX = false;
- defines.PARALLAX_RHS = false;
- defines.PARALLAXOCCLUSION = false;
- defines.OBJECTSPACE_NORMALMAP = false;
- }
- if (this._environmentBRDFTexture && MaterialFlags.ReflectionTextureEnabled) {
- defines.ENVIRONMENTBRDF = true;
- defines.ENVIRONMENTBRDF_RGBD = this._environmentBRDFTexture.isRGBD;
- }
- else {
- defines.ENVIRONMENTBRDF = false;
- defines.ENVIRONMENTBRDF_RGBD = false;
- }
- if (this._shouldUseAlphaFromAlbedoTexture()) {
- defines.ALPHAFROMALBEDO = true;
- }
- else {
- defines.ALPHAFROMALBEDO = false;
- }
- }
- defines.SPECULAROVERALPHA = this._useSpecularOverAlpha;
- if (this._lightFalloff === PBRBaseMaterial.LIGHTFALLOFF_STANDARD) {
- defines.USEPHYSICALLIGHTFALLOFF = false;
- defines.USEGLTFLIGHTFALLOFF = false;
- }
- else if (this._lightFalloff === PBRBaseMaterial.LIGHTFALLOFF_GLTF) {
- defines.USEPHYSICALLIGHTFALLOFF = false;
- defines.USEGLTFLIGHTFALLOFF = true;
- }
- else {
- defines.USEPHYSICALLIGHTFALLOFF = true;
- defines.USEGLTFLIGHTFALLOFF = false;
- }
- defines.RADIANCEOVERALPHA = this._useRadianceOverAlpha;
- if (!this.backFaceCulling && this._twoSidedLighting) {
- defines.TWOSIDEDLIGHTING = true;
- }
- else {
- defines.TWOSIDEDLIGHTING = false;
- }
- defines.SPECULARAA = engine.getCaps().standardDerivatives && this._enableSpecularAntiAliasing;
- }
- if (defines._areTexturesDirty || defines._areMiscDirty) {
- defines.ALPHATESTVALUE = `${this._alphaCutOff}${this._alphaCutOff % 1 === 0 ? "." : ""}`;
- defines.PREMULTIPLYALPHA = this.alphaMode === 7 || this.alphaMode === 8;
- defines.ALPHABLEND = this.needAlphaBlendingForMesh(mesh);
- defines.ALPHAFRESNEL = this._useAlphaFresnel || this._useLinearAlphaFresnel;
- defines.LINEARALPHAFRESNEL = this._useLinearAlphaFresnel;
- }
- if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
- this._imageProcessingConfiguration.prepareDefines(defines);
- }
- defines.FORCENORMALFORWARD = this._forceNormalForward;
- defines.RADIANCEOCCLUSION = this._useRadianceOcclusion;
- defines.HORIZONOCCLUSION = this._useHorizonOcclusion;
- // Misc.
- if (defines._areMiscDirty) {
- PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh) || this._forceAlphaTest, defines, this._applyDecalMapAfterDetailMap);
- defines.UNLIT = this._unlit || ((this.pointsCloud || this.wireframe) && !mesh.isVerticesDataPresent(VertexBuffer.NormalKind));
- defines.DEBUGMODE = this._debugMode;
- }
- // Values that need to be evaluated on every frame
- PrepareDefinesForFrameBoundValues(scene, engine, this, defines, useInstances ? true : false, useClipPlane, useThinInstances);
- // External config
- this._eventInfo.defines = defines;
- this._eventInfo.mesh = mesh;
- this._callbackPluginEventPrepareDefinesBeforeAttributes(this._eventInfo);
- // Attribs
- PrepareDefinesForAttributes(mesh, defines, true, true, true, this._transparencyMode !== PBRBaseMaterial.PBRMATERIAL_OPAQUE);
- // External config
- this._callbackPluginEventPrepareDefines(this._eventInfo);
- }
- /**
- * Force shader compilation
- * @param mesh - Define the mesh we want to force the compilation for
- * @param onCompiled - Define a callback triggered when the compilation completes
- * @param options - Define the options used to create the compilation
- */
- forceCompilation(mesh, onCompiled, options) {
- const localOptions = {
- clipPlane: false,
- useInstances: false,
- ...options,
- };
- if (!this._uniformBufferLayoutBuilt) {
- this.buildUniformLayout();
- }
- this._callbackPluginEventGeneric(MaterialPluginEvent.GetDefineNames, this._eventInfo);
- const defines = new PBRMaterialDefines(this._eventInfo.defineNames);
- const effect = this._prepareEffect(mesh, defines, undefined, undefined, localOptions.useInstances, localOptions.clipPlane, mesh.hasThinInstances);
- if (this._onEffectCreatedObservable) {
- onCreatedEffectParameters.effect = effect;
- onCreatedEffectParameters.subMesh = null;
- this._onEffectCreatedObservable.notifyObservers(onCreatedEffectParameters);
- }
- if (effect.isReady()) {
- if (onCompiled) {
- onCompiled(this);
- }
- }
- else {
- effect.onCompileObservable.add(() => {
- if (onCompiled) {
- onCompiled(this);
- }
- });
- }
- }
- /**
- * Initializes the uniform buffer layout for the shader.
- */
- buildUniformLayout() {
- // Order is important !
- const ubo = this._uniformBuffer;
- ubo.addUniform("vAlbedoInfos", 2);
- ubo.addUniform("vAmbientInfos", 4);
- ubo.addUniform("vOpacityInfos", 2);
- ubo.addUniform("vEmissiveInfos", 2);
- ubo.addUniform("vLightmapInfos", 2);
- ubo.addUniform("vReflectivityInfos", 3);
- ubo.addUniform("vMicroSurfaceSamplerInfos", 2);
- ubo.addUniform("vReflectionInfos", 2);
- ubo.addUniform("vReflectionFilteringInfo", 2);
- ubo.addUniform("vReflectionPosition", 3);
- ubo.addUniform("vReflectionSize", 3);
- ubo.addUniform("vBumpInfos", 3);
- ubo.addUniform("albedoMatrix", 16);
- ubo.addUniform("ambientMatrix", 16);
- ubo.addUniform("opacityMatrix", 16);
- ubo.addUniform("emissiveMatrix", 16);
- ubo.addUniform("lightmapMatrix", 16);
- ubo.addUniform("reflectivityMatrix", 16);
- ubo.addUniform("microSurfaceSamplerMatrix", 16);
- ubo.addUniform("bumpMatrix", 16);
- ubo.addUniform("vTangentSpaceParams", 2);
- ubo.addUniform("reflectionMatrix", 16);
- ubo.addUniform("vReflectionColor", 3);
- ubo.addUniform("vAlbedoColor", 4);
- ubo.addUniform("vLightingIntensity", 4);
- ubo.addUniform("vReflectionMicrosurfaceInfos", 3);
- ubo.addUniform("pointSize", 1);
- ubo.addUniform("vReflectivityColor", 4);
- ubo.addUniform("vEmissiveColor", 3);
- ubo.addUniform("vAmbientColor", 3);
- ubo.addUniform("vDebugMode", 2);
- ubo.addUniform("vMetallicReflectanceFactors", 4);
- ubo.addUniform("vMetallicReflectanceInfos", 2);
- ubo.addUniform("metallicReflectanceMatrix", 16);
- ubo.addUniform("vReflectanceInfos", 2);
- ubo.addUniform("reflectanceMatrix", 16);
- ubo.addUniform("vSphericalL00", 3);
- ubo.addUniform("vSphericalL1_1", 3);
- ubo.addUniform("vSphericalL10", 3);
- ubo.addUniform("vSphericalL11", 3);
- ubo.addUniform("vSphericalL2_2", 3);
- ubo.addUniform("vSphericalL2_1", 3);
- ubo.addUniform("vSphericalL20", 3);
- ubo.addUniform("vSphericalL21", 3);
- ubo.addUniform("vSphericalL22", 3);
- ubo.addUniform("vSphericalX", 3);
- ubo.addUniform("vSphericalY", 3);
- ubo.addUniform("vSphericalZ", 3);
- ubo.addUniform("vSphericalXX_ZZ", 3);
- ubo.addUniform("vSphericalYY_ZZ", 3);
- ubo.addUniform("vSphericalZZ", 3);
- ubo.addUniform("vSphericalXY", 3);
- ubo.addUniform("vSphericalYZ", 3);
- ubo.addUniform("vSphericalZX", 3);
- super.buildUniformLayout();
- }
- /**
- * Binds the submesh data.
- * @param world - The world matrix.
- * @param mesh - The BJS mesh.
- * @param subMesh - A submesh of the BJS mesh.
- */
- bindForSubMesh(world, mesh, subMesh) {
- const scene = this.getScene();
- const defines = subMesh.materialDefines;
- if (!defines) {
- return;
- }
- const effect = subMesh.effect;
- if (!effect) {
- return;
- }
- this._activeEffect = effect;
- // Matrices Mesh.
- mesh.getMeshUniformBuffer().bindToEffect(effect, "Mesh");
- mesh.transferToEffect(world);
- const engine = scene.getEngine();
- // Binding unconditionally
- this._uniformBuffer.bindToEffect(effect, "Material");
- this.prePassConfiguration.bindForSubMesh(this._activeEffect, scene, mesh, world, this.isFrozen);
- this._eventInfo.subMesh = subMesh;
- this._callbackPluginEventHardBindForSubMesh(this._eventInfo);
- // Normal Matrix
- if (defines.OBJECTSPACE_NORMALMAP) {
- world.toNormalMatrix(this._normalMatrix);
- this.bindOnlyNormalMatrix(this._normalMatrix);
- }
- const mustRebind = this._mustRebind(scene, effect, subMesh, mesh.visibility);
- // Bones
- BindBonesParameters(mesh, this._activeEffect, this.prePassConfiguration);
- let reflectionTexture = null;
- const ubo = this._uniformBuffer;
- if (mustRebind) {
- this.bindViewProjection(effect);
- reflectionTexture = this._getReflectionTexture();
- if (!ubo.useUbo || !this.isFrozen || !ubo.isSync || subMesh._drawWrapper._forceRebindOnNextCall) {
- // Texture uniforms
- if (scene.texturesEnabled) {
- if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
- ubo.updateFloat2("vAlbedoInfos", this._albedoTexture.coordinatesIndex, this._albedoTexture.level);
- BindTextureMatrix(this._albedoTexture, ubo, "albedo");
- }
- if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
- ubo.updateFloat4("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level, this._ambientTextureStrength, this._ambientTextureImpactOnAnalyticalLights);
- BindTextureMatrix(this._ambientTexture, ubo, "ambient");
- }
- if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
- ubo.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
- BindTextureMatrix(this._opacityTexture, ubo, "opacity");
- }
- if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
- ubo.updateMatrix("reflectionMatrix", reflectionTexture.getReflectionTextureMatrix());
- ubo.updateFloat2("vReflectionInfos", reflectionTexture.level, 0);
- if (reflectionTexture.boundingBoxSize) {
- const cubeTexture = reflectionTexture;
- ubo.updateVector3("vReflectionPosition", cubeTexture.boundingBoxPosition);
- ubo.updateVector3("vReflectionSize", cubeTexture.boundingBoxSize);
- }
- if (this.realTimeFiltering) {
- const width = reflectionTexture.getSize().width;
- ubo.updateFloat2("vReflectionFilteringInfo", width, Scalar.Log2(width));
- }
- if (!defines.USEIRRADIANCEMAP) {
- const polynomials = reflectionTexture.sphericalPolynomial;
- if (defines.USESPHERICALFROMREFLECTIONMAP && polynomials) {
- if (defines.SPHERICAL_HARMONICS) {
- const preScaledHarmonics = polynomials.preScaledHarmonics;
- ubo.updateVector3("vSphericalL00", preScaledHarmonics.l00);
- ubo.updateVector3("vSphericalL1_1", preScaledHarmonics.l1_1);
- ubo.updateVector3("vSphericalL10", preScaledHarmonics.l10);
- ubo.updateVector3("vSphericalL11", preScaledHarmonics.l11);
- ubo.updateVector3("vSphericalL2_2", preScaledHarmonics.l2_2);
- ubo.updateVector3("vSphericalL2_1", preScaledHarmonics.l2_1);
- ubo.updateVector3("vSphericalL20", preScaledHarmonics.l20);
- ubo.updateVector3("vSphericalL21", preScaledHarmonics.l21);
- ubo.updateVector3("vSphericalL22", preScaledHarmonics.l22);
- }
- else {
- ubo.updateFloat3("vSphericalX", polynomials.x.x, polynomials.x.y, polynomials.x.z);
- ubo.updateFloat3("vSphericalY", polynomials.y.x, polynomials.y.y, polynomials.y.z);
- ubo.updateFloat3("vSphericalZ", polynomials.z.x, polynomials.z.y, polynomials.z.z);
- ubo.updateFloat3("vSphericalXX_ZZ", polynomials.xx.x - polynomials.zz.x, polynomials.xx.y - polynomials.zz.y, polynomials.xx.z - polynomials.zz.z);
- ubo.updateFloat3("vSphericalYY_ZZ", polynomials.yy.x - polynomials.zz.x, polynomials.yy.y - polynomials.zz.y, polynomials.yy.z - polynomials.zz.z);
- ubo.updateFloat3("vSphericalZZ", polynomials.zz.x, polynomials.zz.y, polynomials.zz.z);
- ubo.updateFloat3("vSphericalXY", polynomials.xy.x, polynomials.xy.y, polynomials.xy.z);
- ubo.updateFloat3("vSphericalYZ", polynomials.yz.x, polynomials.yz.y, polynomials.yz.z);
- ubo.updateFloat3("vSphericalZX", polynomials.zx.x, polynomials.zx.y, polynomials.zx.z);
- }
- }
- }
- ubo.updateFloat3("vReflectionMicrosurfaceInfos", reflectionTexture.getSize().width, reflectionTexture.lodGenerationScale, reflectionTexture.lodGenerationOffset);
- }
- if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
- ubo.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
- BindTextureMatrix(this._emissiveTexture, ubo, "emissive");
- }
- if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
- ubo.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
- BindTextureMatrix(this._lightmapTexture, ubo, "lightmap");
- }
- if (MaterialFlags.SpecularTextureEnabled) {
- if (this._metallicTexture) {
- ubo.updateFloat3("vReflectivityInfos", this._metallicTexture.coordinatesIndex, this._metallicTexture.level, this._ambientTextureStrength);
- BindTextureMatrix(this._metallicTexture, ubo, "reflectivity");
- }
- else if (this._reflectivityTexture) {
- ubo.updateFloat3("vReflectivityInfos", this._reflectivityTexture.coordinatesIndex, this._reflectivityTexture.level, 1.0);
- BindTextureMatrix(this._reflectivityTexture, ubo, "reflectivity");
- }
- if (this._metallicReflectanceTexture) {
- ubo.updateFloat2("vMetallicReflectanceInfos", this._metallicReflectanceTexture.coordinatesIndex, this._metallicReflectanceTexture.level);
- BindTextureMatrix(this._metallicReflectanceTexture, ubo, "metallicReflectance");
- }
- if (this._reflectanceTexture && defines.REFLECTANCE) {
- ubo.updateFloat2("vReflectanceInfos", this._reflectanceTexture.coordinatesIndex, this._reflectanceTexture.level);
- BindTextureMatrix(this._reflectanceTexture, ubo, "reflectance");
- }
- if (this._microSurfaceTexture) {
- ubo.updateFloat2("vMicroSurfaceSamplerInfos", this._microSurfaceTexture.coordinatesIndex, this._microSurfaceTexture.level);
- BindTextureMatrix(this._microSurfaceTexture, ubo, "microSurfaceSampler");
- }
- }
- if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
- ubo.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, this._bumpTexture.level, this._parallaxScaleBias);
- BindTextureMatrix(this._bumpTexture, ubo, "bump");
- if (scene._mirroredCameraPosition) {
- ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? 1.0 : -1.0, this._invertNormalMapY ? 1.0 : -1.0);
- }
- else {
- ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? -1.0 : 1.0, this._invertNormalMapY ? -1.0 : 1.0);
- }
- }
- }
- // Point size
- if (this.pointsCloud) {
- ubo.updateFloat("pointSize", this.pointSize);
- }
- // Colors
- if (defines.METALLICWORKFLOW) {
- TmpColors.Color3[0].r = this._metallic === undefined || this._metallic === null ? 1 : this._metallic;
- TmpColors.Color3[0].g = this._roughness === undefined || this._roughness === null ? 1 : this._roughness;
- ubo.updateColor4("vReflectivityColor", TmpColors.Color3[0], 1);
- const ior = this.subSurface?._indexOfRefraction ?? 1.5;
- const outsideIOR = 1; // consider air as clear coat and other layers would remap in the shader.
- // We are here deriving our default reflectance from a common value for none metallic surface.
- // Based of the schlick fresnel approximation model
- // for dielectrics.
- const f0 = Math.pow((ior - outsideIOR) / (ior + outsideIOR), 2);
- // Tweak the default F0 and F90 based on our given setup
- this._metallicReflectanceColor.scaleToRef(f0 * this._metallicF0Factor, TmpColors.Color3[0]);
- const metallicF90 = this._metallicF0Factor;
- ubo.updateColor4("vMetallicReflectanceFactors", TmpColors.Color3[0], metallicF90);
- }
- else {
- ubo.updateColor4("vReflectivityColor", this._reflectivityColor, this._microSurface);
- }
- ubo.updateColor3("vEmissiveColor", MaterialFlags.EmissiveTextureEnabled ? this._emissiveColor : Color3.BlackReadOnly);
- ubo.updateColor3("vReflectionColor", this._reflectionColor);
- if (!defines.SS_REFRACTION && this.subSurface?._linkRefractionWithTransparency) {
- ubo.updateColor4("vAlbedoColor", this._albedoColor, 1);
- }
- else {
- ubo.updateColor4("vAlbedoColor", this._albedoColor, this.alpha);
- }
- // Misc
- this._lightingInfos.x = this._directIntensity;
- this._lightingInfos.y = this._emissiveIntensity;
- this._lightingInfos.z = this._environmentIntensity * scene.environmentIntensity;
- this._lightingInfos.w = this._specularIntensity;
- ubo.updateVector4("vLightingIntensity", this._lightingInfos);
- // Colors
- scene.ambientColor.multiplyToRef(this._ambientColor, this._globalAmbientColor);
- ubo.updateColor3("vAmbientColor", this._globalAmbientColor);
- ubo.updateFloat2("vDebugMode", this.debugLimit, this.debugFactor);
- }
- // Textures
- if (scene.texturesEnabled) {
- if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
- ubo.setTexture("albedoSampler", this._albedoTexture);
- }
- if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
- ubo.setTexture("ambientSampler", this._ambientTexture);
- }
- if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
- ubo.setTexture("opacitySampler", this._opacityTexture);
- }
- if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
- if (defines.LODBASEDMICROSFURACE) {
- ubo.setTexture("reflectionSampler", reflectionTexture);
- }
- else {
- ubo.setTexture("reflectionSampler", reflectionTexture._lodTextureMid || reflectionTexture);
- ubo.setTexture("reflectionSamplerLow", reflectionTexture._lodTextureLow || reflectionTexture);
- ubo.setTexture("reflectionSamplerHigh", reflectionTexture._lodTextureHigh || reflectionTexture);
- }
- if (defines.USEIRRADIANCEMAP) {
- ubo.setTexture("irradianceSampler", reflectionTexture.irradianceTexture);
- }
- }
- if (defines.ENVIRONMENTBRDF) {
- ubo.setTexture("environmentBrdfSampler", this._environmentBRDFTexture);
- }
- if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
- ubo.setTexture("emissiveSampler", this._emissiveTexture);
- }
- if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
- ubo.setTexture("lightmapSampler", this._lightmapTexture);
- }
- if (MaterialFlags.SpecularTextureEnabled) {
- if (this._metallicTexture) {
- ubo.setTexture("reflectivitySampler", this._metallicTexture);
- }
- else if (this._reflectivityTexture) {
- ubo.setTexture("reflectivitySampler", this._reflectivityTexture);
- }
- if (this._metallicReflectanceTexture) {
- ubo.setTexture("metallicReflectanceSampler", this._metallicReflectanceTexture);
- }
- if (this._reflectanceTexture && defines.REFLECTANCE) {
- ubo.setTexture("reflectanceSampler", this._reflectanceTexture);
- }
- if (this._microSurfaceTexture) {
- ubo.setTexture("microSurfaceSampler", this._microSurfaceTexture);
- }
- }
- if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
- ubo.setTexture("bumpSampler", this._bumpTexture);
- }
- }
- // OIT with depth peeling
- if (this.getScene().useOrderIndependentTransparency && this.needAlphaBlendingForMesh(mesh)) {
- this.getScene().depthPeelingRenderer.bind(effect);
- }
- this._eventInfo.subMesh = subMesh;
- this._callbackPluginEventBindForSubMesh(this._eventInfo);
- // Clip plane
- bindClipPlane(this._activeEffect, this, scene);
- this.bindEyePosition(effect);
- }
- else if (scene.getEngine()._features.needToAlwaysBindUniformBuffers) {
- this._needToBindSceneUbo = true;
- }
- if (mustRebind || !this.isFrozen) {
- // Lights
- if (scene.lightsEnabled && !this._disableLighting) {
- BindLights(scene, mesh, this._activeEffect, defines, this._maxSimultaneousLights);
- }
- // View
- if ((scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) ||
- reflectionTexture ||
- this.subSurface.refractionTexture ||
- mesh.receiveShadows ||
- defines.PREPASS) {
- this.bindView(effect);
- }
- // Fog
- BindFogParameters(scene, mesh, this._activeEffect, true);
- // Morph targets
- if (defines.NUM_MORPH_INFLUENCERS) {
- BindMorphTargetParameters(mesh, this._activeEffect);
- }
- if (defines.BAKED_VERTEX_ANIMATION_TEXTURE) {
- mesh.bakedVertexAnimationManager?.bind(effect, defines.INSTANCES);
- }
- // image processing
- this._imageProcessingConfiguration.bind(this._activeEffect);
- // Log. depth
- BindLogDepth(defines, this._activeEffect, scene);
- }
- this._afterBind(mesh, this._activeEffect, subMesh);
- ubo.update();
- }
- /**
- * Returns the animatable textures.
- * If material have animatable metallic texture, then reflectivity texture will not be returned, even if it has animations.
- * @returns - Array of animatable textures.
- */
- getAnimatables() {
- const results = super.getAnimatables();
- if (this._albedoTexture && this._albedoTexture.animations && this._albedoTexture.animations.length > 0) {
- results.push(this._albedoTexture);
- }
- if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
- results.push(this._ambientTexture);
- }
- if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
- results.push(this._opacityTexture);
- }
- if (this._reflectionTexture && this._reflectionTexture.animations && this._reflectionTexture.animations.length > 0) {
- results.push(this._reflectionTexture);
- }
- if (this._emissiveTexture && this._emissiveTexture.animations && this._emissiveTexture.animations.length > 0) {
- results.push(this._emissiveTexture);
- }
- if (this._metallicTexture && this._metallicTexture.animations && this._metallicTexture.animations.length > 0) {
- results.push(this._metallicTexture);
- }
- else if (this._reflectivityTexture && this._reflectivityTexture.animations && this._reflectivityTexture.animations.length > 0) {
- results.push(this._reflectivityTexture);
- }
- if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
- results.push(this._bumpTexture);
- }
- if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
- results.push(this._lightmapTexture);
- }
- if (this._metallicReflectanceTexture && this._metallicReflectanceTexture.animations && this._metallicReflectanceTexture.animations.length > 0) {
- results.push(this._metallicReflectanceTexture);
- }
- if (this._reflectanceTexture && this._reflectanceTexture.animations && this._reflectanceTexture.animations.length > 0) {
- results.push(this._reflectanceTexture);
- }
- if (this._microSurfaceTexture && this._microSurfaceTexture.animations && this._microSurfaceTexture.animations.length > 0) {
- results.push(this._microSurfaceTexture);
- }
- return results;
- }
- /**
- * Returns the texture used for reflections.
- * @returns - Reflection texture if present. Otherwise, returns the environment texture.
- */
- _getReflectionTexture() {
- if (this._reflectionTexture) {
- return this._reflectionTexture;
- }
- return this.getScene().environmentTexture;
- }
- /**
- * Returns an array of the actively used textures.
- * @returns - Array of BaseTextures
- */
- getActiveTextures() {
- const activeTextures = super.getActiveTextures();
- if (this._albedoTexture) {
- activeTextures.push(this._albedoTexture);
- }
- if (this._ambientTexture) {
- activeTextures.push(this._ambientTexture);
- }
- if (this._opacityTexture) {
- activeTextures.push(this._opacityTexture);
- }
- if (this._reflectionTexture) {
- activeTextures.push(this._reflectionTexture);
- }
- if (this._emissiveTexture) {
- activeTextures.push(this._emissiveTexture);
- }
- if (this._reflectivityTexture) {
- activeTextures.push(this._reflectivityTexture);
- }
- if (this._metallicTexture) {
- activeTextures.push(this._metallicTexture);
- }
- if (this._metallicReflectanceTexture) {
- activeTextures.push(this._metallicReflectanceTexture);
- }
- if (this._reflectanceTexture) {
- activeTextures.push(this._reflectanceTexture);
- }
- if (this._microSurfaceTexture) {
- activeTextures.push(this._microSurfaceTexture);
- }
- if (this._bumpTexture) {
- activeTextures.push(this._bumpTexture);
- }
- if (this._lightmapTexture) {
- activeTextures.push(this._lightmapTexture);
- }
- return activeTextures;
- }
- /**
- * Checks to see if a texture is used in the material.
- * @param texture - Base texture to use.
- * @returns - Boolean specifying if a texture is used in the material.
- */
- hasTexture(texture) {
- if (super.hasTexture(texture)) {
- return true;
- }
- if (this._albedoTexture === texture) {
- return true;
- }
- if (this._ambientTexture === texture) {
- return true;
- }
- if (this._opacityTexture === texture) {
- return true;
- }
- if (this._reflectionTexture === texture) {
- return true;
- }
- if (this._emissiveTexture === texture) {
- return true;
- }
- if (this._reflectivityTexture === texture) {
- return true;
- }
- if (this._metallicTexture === texture) {
- return true;
- }
- if (this._metallicReflectanceTexture === texture) {
- return true;
- }
- if (this._reflectanceTexture === texture) {
- return true;
- }
- if (this._microSurfaceTexture === texture) {
- return true;
- }
- if (this._bumpTexture === texture) {
- return true;
- }
- if (this._lightmapTexture === texture) {
- return true;
- }
- return false;
- }
- /**
- * Sets the required values to the prepass renderer.
- * It can't be sets when subsurface scattering of this material is disabled.
- * When scene have ability to enable subsurface prepass effect, it will enable.
- * @returns - If prepass is enabled or not.
- */
- setPrePassRenderer() {
- if (!this.subSurface?.isScatteringEnabled) {
- return false;
- }
- const subSurfaceConfiguration = this.getScene().enableSubSurfaceForPrePass();
- if (subSurfaceConfiguration) {
- subSurfaceConfiguration.enabled = true;
- }
- return true;
- }
- /**
- * Disposes the resources of the material.
- * @param forceDisposeEffect - Forces the disposal of effects.
- * @param forceDisposeTextures - Forces the disposal of all textures.
- */
- dispose(forceDisposeEffect, forceDisposeTextures) {
- if (forceDisposeTextures) {
- if (this._environmentBRDFTexture && this.getScene().environmentBRDFTexture !== this._environmentBRDFTexture) {
- this._environmentBRDFTexture.dispose();
- }
- this._albedoTexture?.dispose();
- this._ambientTexture?.dispose();
- this._opacityTexture?.dispose();
- this._reflectionTexture?.dispose();
- this._emissiveTexture?.dispose();
- this._metallicTexture?.dispose();
- this._reflectivityTexture?.dispose();
- this._bumpTexture?.dispose();
- this._lightmapTexture?.dispose();
- this._metallicReflectanceTexture?.dispose();
- this._reflectanceTexture?.dispose();
- this._microSurfaceTexture?.dispose();
- }
- this._renderTargets.dispose();
- if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
- this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
- }
- super.dispose(forceDisposeEffect, forceDisposeTextures);
- }
- }
- /**
- * PBRMaterialTransparencyMode: No transparency mode, Alpha channel is not use.
- */
- PBRBaseMaterial.PBRMATERIAL_OPAQUE = Material.MATERIAL_OPAQUE;
- /**
- * PBRMaterialTransparencyMode: Alpha Test mode, pixel are discarded below a certain threshold defined by the alpha cutoff value.
- */
- PBRBaseMaterial.PBRMATERIAL_ALPHATEST = Material.MATERIAL_ALPHATEST;
- /**
- * PBRMaterialTransparencyMode: Pixels are blended (according to the alpha mode) with the already drawn pixels in the current frame buffer.
- */
- PBRBaseMaterial.PBRMATERIAL_ALPHABLEND = Material.MATERIAL_ALPHABLEND;
- /**
- * 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.
- */
- PBRBaseMaterial.PBRMATERIAL_ALPHATESTANDBLEND = Material.MATERIAL_ALPHATESTANDBLEND;
- /**
- * Defines the default value of how much AO map is occluding the analytical lights
- * (point spot...).
- */
- PBRBaseMaterial.DEFAULT_AO_ON_ANALYTICAL_LIGHTS = 0;
- /**
- * PBRMaterialLightFalloff Physical: light is falling off following the inverse squared distance law.
- */
- PBRBaseMaterial.LIGHTFALLOFF_PHYSICAL = 0;
- /**
- * PBRMaterialLightFalloff gltf: light is falling off as described in the gltf moving to PBR document
- * to enhance interoperability with other engines.
- */
- PBRBaseMaterial.LIGHTFALLOFF_GLTF = 1;
- /**
- * PBRMaterialLightFalloff Standard: light is falling off like in the standard material
- * to enhance interoperability with other materials.
- */
- PBRBaseMaterial.LIGHTFALLOFF_STANDARD = 2;
- __decorate([
- serializeAsImageProcessingConfiguration()
- ], PBRBaseMaterial.prototype, "_imageProcessingConfiguration", void 0);
- __decorate([
- expandToProperty("_markAllSubMeshesAsMiscDirty")
- ], PBRBaseMaterial.prototype, "debugMode", void 0);
- //# sourceMappingURL=pbrBaseMaterial.js.map
|