pbrBaseMaterial.js 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  1. import { __decorate } from "../../tslib.es6.js";
  2. /* eslint-disable @typescript-eslint/naming-convention */
  3. import { serializeAsImageProcessingConfiguration, expandToProperty } from "../../Misc/decorators.js";
  4. import { Logger } from "../../Misc/logger.js";
  5. import { SmartArray } from "../../Misc/smartArray.js";
  6. import { GetEnvironmentBRDFTexture } from "../../Misc/brdfTextureTools.js";
  7. import { Scene } from "../../scene.js";
  8. import { Vector4 } from "../../Maths/math.vector.js";
  9. import { VertexBuffer } from "../../Buffers/buffer.js";
  10. import { PBRBRDFConfiguration } from "./pbrBRDFConfiguration.js";
  11. import { PrePassConfiguration } from "../prePassConfiguration.js";
  12. import { Color3, TmpColors } from "../../Maths/math.color.js";
  13. import { Scalar } from "../../Maths/math.scalar.js";
  14. import { ImageProcessingConfiguration } from "../../Materials/imageProcessingConfiguration.js";
  15. import { Material } from "../../Materials/material.js";
  16. import { MaterialPluginEvent } from "../materialPluginEvent.js";
  17. import { MaterialDefines } from "../../Materials/materialDefines.js";
  18. import { PushMaterial } from "../../Materials/pushMaterial.js";
  19. import { Texture } from "../../Materials/Textures/texture.js";
  20. import { MaterialFlags } from "../materialFlags.js";
  21. import "../../Materials/Textures/baseTexture.polynomial.js";
  22. import "../../Shaders/pbr.fragment.js";
  23. import "../../Shaders/pbr.vertex.js";
  24. import { EffectFallbacks } from "../effectFallbacks.js";
  25. import { PBRClearCoatConfiguration } from "./pbrClearCoatConfiguration.js";
  26. import { PBRIridescenceConfiguration } from "./pbrIridescenceConfiguration.js";
  27. import { PBRAnisotropicConfiguration } from "./pbrAnisotropicConfiguration.js";
  28. import { PBRSheenConfiguration } from "./pbrSheenConfiguration.js";
  29. import { PBRSubSurfaceConfiguration } from "./pbrSubSurfaceConfiguration.js";
  30. import { DetailMapConfiguration } from "../material.detailMapConfiguration.js";
  31. import { addClipPlaneUniforms, bindClipPlane } from "../clipPlaneMaterialHelper.js";
  32. 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";
  33. const onCreatedEffectParameters = { effect: null, subMesh: null };
  34. /**
  35. * Manages the defines for the PBR Material.
  36. * @internal
  37. */
  38. export class PBRMaterialDefines extends MaterialDefines {
  39. /**
  40. * Initializes the PBR Material defines.
  41. * @param externalProperties The external properties
  42. */
  43. constructor(externalProperties) {
  44. super(externalProperties);
  45. this.PBR = true;
  46. this.NUM_SAMPLES = "0";
  47. this.REALTIME_FILTERING = false;
  48. this.MAINUV1 = false;
  49. this.MAINUV2 = false;
  50. this.MAINUV3 = false;
  51. this.MAINUV4 = false;
  52. this.MAINUV5 = false;
  53. this.MAINUV6 = false;
  54. this.UV1 = false;
  55. this.UV2 = false;
  56. this.UV3 = false;
  57. this.UV4 = false;
  58. this.UV5 = false;
  59. this.UV6 = false;
  60. this.ALBEDO = false;
  61. this.GAMMAALBEDO = false;
  62. this.ALBEDODIRECTUV = 0;
  63. this.VERTEXCOLOR = false;
  64. this.BAKED_VERTEX_ANIMATION_TEXTURE = false;
  65. this.AMBIENT = false;
  66. this.AMBIENTDIRECTUV = 0;
  67. this.AMBIENTINGRAYSCALE = false;
  68. this.OPACITY = false;
  69. this.VERTEXALPHA = false;
  70. this.OPACITYDIRECTUV = 0;
  71. this.OPACITYRGB = false;
  72. this.ALPHATEST = false;
  73. this.DEPTHPREPASS = false;
  74. this.ALPHABLEND = false;
  75. this.ALPHAFROMALBEDO = false;
  76. this.ALPHATESTVALUE = "0.5";
  77. this.SPECULAROVERALPHA = false;
  78. this.RADIANCEOVERALPHA = false;
  79. this.ALPHAFRESNEL = false;
  80. this.LINEARALPHAFRESNEL = false;
  81. this.PREMULTIPLYALPHA = false;
  82. this.EMISSIVE = false;
  83. this.EMISSIVEDIRECTUV = 0;
  84. this.GAMMAEMISSIVE = false;
  85. this.REFLECTIVITY = false;
  86. this.REFLECTIVITY_GAMMA = false;
  87. this.REFLECTIVITYDIRECTUV = 0;
  88. this.SPECULARTERM = false;
  89. this.MICROSURFACEFROMREFLECTIVITYMAP = false;
  90. this.MICROSURFACEAUTOMATIC = false;
  91. this.LODBASEDMICROSFURACE = false;
  92. this.MICROSURFACEMAP = false;
  93. this.MICROSURFACEMAPDIRECTUV = 0;
  94. this.METALLICWORKFLOW = false;
  95. this.ROUGHNESSSTOREINMETALMAPALPHA = false;
  96. this.ROUGHNESSSTOREINMETALMAPGREEN = false;
  97. this.METALLNESSSTOREINMETALMAPBLUE = false;
  98. this.AOSTOREINMETALMAPRED = false;
  99. this.METALLIC_REFLECTANCE = false;
  100. this.METALLIC_REFLECTANCE_GAMMA = false;
  101. this.METALLIC_REFLECTANCEDIRECTUV = 0;
  102. this.METALLIC_REFLECTANCE_USE_ALPHA_ONLY = false;
  103. this.REFLECTANCE = false;
  104. this.REFLECTANCE_GAMMA = false;
  105. this.REFLECTANCEDIRECTUV = 0;
  106. this.ENVIRONMENTBRDF = false;
  107. this.ENVIRONMENTBRDF_RGBD = false;
  108. this.NORMAL = false;
  109. this.TANGENT = false;
  110. this.BUMP = false;
  111. this.BUMPDIRECTUV = 0;
  112. this.OBJECTSPACE_NORMALMAP = false;
  113. this.PARALLAX = false;
  114. this.PARALLAX_RHS = false;
  115. this.PARALLAXOCCLUSION = false;
  116. this.NORMALXYSCALE = true;
  117. this.LIGHTMAP = false;
  118. this.LIGHTMAPDIRECTUV = 0;
  119. this.USELIGHTMAPASSHADOWMAP = false;
  120. this.GAMMALIGHTMAP = false;
  121. this.RGBDLIGHTMAP = false;
  122. this.REFLECTION = false;
  123. this.REFLECTIONMAP_3D = false;
  124. this.REFLECTIONMAP_SPHERICAL = false;
  125. this.REFLECTIONMAP_PLANAR = false;
  126. this.REFLECTIONMAP_CUBIC = false;
  127. this.USE_LOCAL_REFLECTIONMAP_CUBIC = false;
  128. this.REFLECTIONMAP_PROJECTION = false;
  129. this.REFLECTIONMAP_SKYBOX = false;
  130. this.REFLECTIONMAP_EXPLICIT = false;
  131. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  132. this.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  133. this.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  134. this.INVERTCUBICMAP = false;
  135. this.USESPHERICALFROMREFLECTIONMAP = false;
  136. this.USEIRRADIANCEMAP = false;
  137. this.USESPHERICALINVERTEX = false;
  138. this.REFLECTIONMAP_OPPOSITEZ = false;
  139. this.LODINREFLECTIONALPHA = false;
  140. this.GAMMAREFLECTION = false;
  141. this.RGBDREFLECTION = false;
  142. this.LINEARSPECULARREFLECTION = false;
  143. this.RADIANCEOCCLUSION = false;
  144. this.HORIZONOCCLUSION = false;
  145. this.INSTANCES = false;
  146. this.THIN_INSTANCES = false;
  147. this.INSTANCESCOLOR = false;
  148. this.PREPASS = false;
  149. this.PREPASS_IRRADIANCE = false;
  150. this.PREPASS_IRRADIANCE_INDEX = -1;
  151. this.PREPASS_ALBEDO_SQRT = false;
  152. this.PREPASS_ALBEDO_SQRT_INDEX = -1;
  153. this.PREPASS_DEPTH = false;
  154. this.PREPASS_DEPTH_INDEX = -1;
  155. this.PREPASS_NORMAL = false;
  156. this.PREPASS_NORMAL_INDEX = -1;
  157. this.PREPASS_NORMAL_WORLDSPACE = false;
  158. this.PREPASS_POSITION = false;
  159. this.PREPASS_POSITION_INDEX = -1;
  160. this.PREPASS_VELOCITY = false;
  161. this.PREPASS_VELOCITY_INDEX = -1;
  162. this.PREPASS_REFLECTIVITY = false;
  163. this.PREPASS_REFLECTIVITY_INDEX = -1;
  164. this.SCENE_MRT_COUNT = 0;
  165. this.NUM_BONE_INFLUENCERS = 0;
  166. this.BonesPerMesh = 0;
  167. this.BONETEXTURE = false;
  168. this.BONES_VELOCITY_ENABLED = false;
  169. this.NONUNIFORMSCALING = false;
  170. this.MORPHTARGETS = false;
  171. this.MORPHTARGETS_NORMAL = false;
  172. this.MORPHTARGETS_TANGENT = false;
  173. this.MORPHTARGETS_UV = false;
  174. this.NUM_MORPH_INFLUENCERS = 0;
  175. this.MORPHTARGETS_TEXTURE = false;
  176. this.IMAGEPROCESSING = false;
  177. this.VIGNETTE = false;
  178. this.VIGNETTEBLENDMODEMULTIPLY = false;
  179. this.VIGNETTEBLENDMODEOPAQUE = false;
  180. this.TONEMAPPING = false;
  181. this.TONEMAPPING_ACES = false;
  182. this.CONTRAST = false;
  183. this.COLORCURVES = false;
  184. this.COLORGRADING = false;
  185. this.COLORGRADING3D = false;
  186. this.SAMPLER3DGREENDEPTH = false;
  187. this.SAMPLER3DBGRMAP = false;
  188. this.DITHER = false;
  189. this.IMAGEPROCESSINGPOSTPROCESS = false;
  190. this.SKIPFINALCOLORCLAMP = false;
  191. this.EXPOSURE = false;
  192. this.MULTIVIEW = false;
  193. this.ORDER_INDEPENDENT_TRANSPARENCY = false;
  194. this.ORDER_INDEPENDENT_TRANSPARENCY_16BITS = false;
  195. this.USEPHYSICALLIGHTFALLOFF = false;
  196. this.USEGLTFLIGHTFALLOFF = false;
  197. this.TWOSIDEDLIGHTING = false;
  198. this.SHADOWFLOAT = false;
  199. this.CLIPPLANE = false;
  200. this.CLIPPLANE2 = false;
  201. this.CLIPPLANE3 = false;
  202. this.CLIPPLANE4 = false;
  203. this.CLIPPLANE5 = false;
  204. this.CLIPPLANE6 = false;
  205. this.POINTSIZE = false;
  206. this.FOG = false;
  207. this.LOGARITHMICDEPTH = false;
  208. this.CAMERA_ORTHOGRAPHIC = false;
  209. this.CAMERA_PERSPECTIVE = false;
  210. this.FORCENORMALFORWARD = false;
  211. this.SPECULARAA = false;
  212. this.UNLIT = false;
  213. this.DECAL_AFTER_DETAIL = false;
  214. this.DEBUGMODE = 0;
  215. this.rebuild();
  216. }
  217. /**
  218. * Resets the PBR Material defines.
  219. */
  220. reset() {
  221. super.reset();
  222. this.ALPHATESTVALUE = "0.5";
  223. this.PBR = true;
  224. this.NORMALXYSCALE = true;
  225. }
  226. }
  227. /**
  228. * The Physically based material base class of BJS.
  229. *
  230. * This offers the main features of a standard PBR material.
  231. * For more information, please refer to the documentation :
  232. * https://doc.babylonjs.com/features/featuresDeepDive/materials/using/introToPBR
  233. */
  234. export class PBRBaseMaterial extends PushMaterial {
  235. /**
  236. * Enables realtime filtering on the texture.
  237. */
  238. get realTimeFiltering() {
  239. return this._realTimeFiltering;
  240. }
  241. set realTimeFiltering(b) {
  242. this._realTimeFiltering = b;
  243. this.markAsDirty(1);
  244. }
  245. /**
  246. * Quality switch for realtime filtering
  247. */
  248. get realTimeFilteringQuality() {
  249. return this._realTimeFilteringQuality;
  250. }
  251. set realTimeFilteringQuality(n) {
  252. this._realTimeFilteringQuality = n;
  253. this.markAsDirty(1);
  254. }
  255. /**
  256. * Can this material render to several textures at once
  257. */
  258. get canRenderToMRT() {
  259. return true;
  260. }
  261. /**
  262. * Attaches a new image processing configuration to the PBR Material.
  263. * @param configuration
  264. */
  265. _attachImageProcessingConfiguration(configuration) {
  266. if (configuration === this._imageProcessingConfiguration) {
  267. return;
  268. }
  269. // Detaches observer.
  270. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  271. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  272. }
  273. // Pick the scene configuration if needed.
  274. if (!configuration) {
  275. this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
  276. }
  277. else {
  278. this._imageProcessingConfiguration = configuration;
  279. }
  280. // Attaches observer.
  281. if (this._imageProcessingConfiguration) {
  282. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(() => {
  283. this._markAllSubMeshesAsImageProcessingDirty();
  284. });
  285. }
  286. }
  287. /**
  288. * Instantiates a new PBRMaterial instance.
  289. *
  290. * @param name The material name
  291. * @param scene The scene the material will be use in.
  292. */
  293. constructor(name, scene) {
  294. super(name, scene);
  295. /**
  296. * Intensity of the direct lights e.g. the four lights available in your scene.
  297. * This impacts both the direct diffuse and specular highlights.
  298. * @internal
  299. */
  300. this._directIntensity = 1.0;
  301. /**
  302. * Intensity of the emissive part of the material.
  303. * This helps controlling the emissive effect without modifying the emissive color.
  304. * @internal
  305. */
  306. this._emissiveIntensity = 1.0;
  307. /**
  308. * Intensity of the environment e.g. how much the environment will light the object
  309. * either through harmonics for rough material or through the reflection for shiny ones.
  310. * @internal
  311. */
  312. this._environmentIntensity = 1.0;
  313. /**
  314. * This is a special control allowing the reduction of the specular highlights coming from the
  315. * four lights of the scene. Those highlights may not be needed in full environment lighting.
  316. * @internal
  317. */
  318. this._specularIntensity = 1.0;
  319. /**
  320. * This stores the direct, emissive, environment, and specular light intensities into a Vector4.
  321. */
  322. this._lightingInfos = new Vector4(this._directIntensity, this._emissiveIntensity, this._environmentIntensity, this._specularIntensity);
  323. /**
  324. * Debug Control allowing disabling the bump map on this material.
  325. * @internal
  326. */
  327. this._disableBumpMap = false;
  328. /**
  329. * AKA Diffuse Texture in standard nomenclature.
  330. * @internal
  331. */
  332. this._albedoTexture = null;
  333. /**
  334. * AKA Occlusion Texture in other nomenclature.
  335. * @internal
  336. */
  337. this._ambientTexture = null;
  338. /**
  339. * AKA Occlusion Texture Intensity in other nomenclature.
  340. * @internal
  341. */
  342. this._ambientTextureStrength = 1.0;
  343. /**
  344. * Defines how much the AO map is occluding the analytical lights (point spot...).
  345. * 1 means it completely occludes it
  346. * 0 mean it has no impact
  347. * @internal
  348. */
  349. this._ambientTextureImpactOnAnalyticalLights = PBRBaseMaterial.DEFAULT_AO_ON_ANALYTICAL_LIGHTS;
  350. /**
  351. * Stores the alpha values in a texture.
  352. * @internal
  353. */
  354. this._opacityTexture = null;
  355. /**
  356. * Stores the reflection values in a texture.
  357. * @internal
  358. */
  359. this._reflectionTexture = null;
  360. /**
  361. * Stores the emissive values in a texture.
  362. * @internal
  363. */
  364. this._emissiveTexture = null;
  365. /**
  366. * AKA Specular texture in other nomenclature.
  367. * @internal
  368. */
  369. this._reflectivityTexture = null;
  370. /**
  371. * Used to switch from specular/glossiness to metallic/roughness workflow.
  372. * @internal
  373. */
  374. this._metallicTexture = null;
  375. /**
  376. * Specifies the metallic scalar of the metallic/roughness workflow.
  377. * Can also be used to scale the metalness values of the metallic texture.
  378. * @internal
  379. */
  380. this._metallic = null;
  381. /**
  382. * Specifies the roughness scalar of the metallic/roughness workflow.
  383. * Can also be used to scale the roughness values of the metallic texture.
  384. * @internal
  385. */
  386. this._roughness = null;
  387. /**
  388. * In metallic workflow, specifies an F0 factor to help configuring the material F0.
  389. * By default the indexOfrefraction is used to compute F0;
  390. *
  391. * This is used as a factor against the default reflectance at normal incidence to tweak it.
  392. *
  393. * F0 = defaultF0 * metallicF0Factor * metallicReflectanceColor;
  394. * F90 = metallicReflectanceColor;
  395. * @internal
  396. */
  397. this._metallicF0Factor = 1;
  398. /**
  399. * In metallic workflow, specifies an F0 color.
  400. * By default the F90 is always 1;
  401. *
  402. * Please note that this factor is also used as a factor against the default reflectance at normal incidence.
  403. *
  404. * F0 = defaultF0_from_IOR * metallicF0Factor * metallicReflectanceColor
  405. * F90 = metallicF0Factor;
  406. * @internal
  407. */
  408. this._metallicReflectanceColor = Color3.White();
  409. /**
  410. * Specifies that only the A channel from _metallicReflectanceTexture should be used.
  411. * If false, both RGB and A channels will be used
  412. * @internal
  413. */
  414. this._useOnlyMetallicFromMetallicReflectanceTexture = false;
  415. /**
  416. * Defines to store metallicReflectanceColor in RGB and metallicF0Factor in A
  417. * This is multiply against the scalar values defined in the material.
  418. * @internal
  419. */
  420. this._metallicReflectanceTexture = null;
  421. /**
  422. * Defines to store reflectanceColor in RGB
  423. * This is multiplied against the scalar values defined in the material.
  424. * If both _reflectanceTexture and _metallicReflectanceTexture textures are provided and _useOnlyMetallicFromMetallicReflectanceTexture
  425. * is false, _metallicReflectanceTexture takes precedence and _reflectanceTexture is not used
  426. * @internal
  427. */
  428. this._reflectanceTexture = null;
  429. /**
  430. * Used to enable roughness/glossiness fetch from a separate channel depending on the current mode.
  431. * Gray Scale represents roughness in metallic mode and glossiness in specular mode.
  432. * @internal
  433. */
  434. this._microSurfaceTexture = null;
  435. /**
  436. * Stores surface normal data used to displace a mesh in a texture.
  437. * @internal
  438. */
  439. this._bumpTexture = null;
  440. /**
  441. * Stores the pre-calculated light information of a mesh in a texture.
  442. * @internal
  443. */
  444. this._lightmapTexture = null;
  445. /**
  446. * The color of a material in ambient lighting.
  447. * @internal
  448. */
  449. this._ambientColor = new Color3(0, 0, 0);
  450. /**
  451. * AKA Diffuse Color in other nomenclature.
  452. * @internal
  453. */
  454. this._albedoColor = new Color3(1, 1, 1);
  455. /**
  456. * AKA Specular Color in other nomenclature.
  457. * @internal
  458. */
  459. this._reflectivityColor = new Color3(1, 1, 1);
  460. /**
  461. * The color applied when light is reflected from a material.
  462. * @internal
  463. */
  464. this._reflectionColor = new Color3(1, 1, 1);
  465. /**
  466. * The color applied when light is emitted from a material.
  467. * @internal
  468. */
  469. this._emissiveColor = new Color3(0, 0, 0);
  470. /**
  471. * AKA Glossiness in other nomenclature.
  472. * @internal
  473. */
  474. this._microSurface = 0.9;
  475. /**
  476. * Specifies that the material will use the light map as a show map.
  477. * @internal
  478. */
  479. this._useLightmapAsShadowmap = false;
  480. /**
  481. * This parameters will enable/disable Horizon occlusion to prevent normal maps to look shiny when the normal
  482. * makes the reflect vector face the model (under horizon).
  483. * @internal
  484. */
  485. this._useHorizonOcclusion = true;
  486. /**
  487. * This parameters will enable/disable radiance occlusion by preventing the radiance to lit
  488. * too much the area relying on ambient texture to define their ambient occlusion.
  489. * @internal
  490. */
  491. this._useRadianceOcclusion = true;
  492. /**
  493. * Specifies that the alpha is coming form the albedo channel alpha channel for alpha blending.
  494. * @internal
  495. */
  496. this._useAlphaFromAlbedoTexture = false;
  497. /**
  498. * Specifies that the material will keeps the specular highlights over a transparent surface (only the most luminous ones).
  499. * A car glass is a good example of that. When sun reflects on it you can not see what is behind.
  500. * @internal
  501. */
  502. this._useSpecularOverAlpha = true;
  503. /**
  504. * Specifies if the reflectivity texture contains the glossiness information in its alpha channel.
  505. * @internal
  506. */
  507. this._useMicroSurfaceFromReflectivityMapAlpha = false;
  508. /**
  509. * Specifies if the metallic texture contains the roughness information in its alpha channel.
  510. * @internal
  511. */
  512. this._useRoughnessFromMetallicTextureAlpha = true;
  513. /**
  514. * Specifies if the metallic texture contains the roughness information in its green channel.
  515. * @internal
  516. */
  517. this._useRoughnessFromMetallicTextureGreen = false;
  518. /**
  519. * Specifies if the metallic texture contains the metallness information in its blue channel.
  520. * @internal
  521. */
  522. this._useMetallnessFromMetallicTextureBlue = false;
  523. /**
  524. * Specifies if the metallic texture contains the ambient occlusion information in its red channel.
  525. * @internal
  526. */
  527. this._useAmbientOcclusionFromMetallicTextureRed = false;
  528. /**
  529. * Specifies if the ambient texture contains the ambient occlusion information in its red channel only.
  530. * @internal
  531. */
  532. this._useAmbientInGrayScale = false;
  533. /**
  534. * In case the reflectivity map does not contain the microsurface information in its alpha channel,
  535. * The material will try to infer what glossiness each pixel should be.
  536. * @internal
  537. */
  538. this._useAutoMicroSurfaceFromReflectivityMap = false;
  539. /**
  540. * Defines the falloff type used in this material.
  541. * It by default is Physical.
  542. * @internal
  543. */
  544. this._lightFalloff = PBRBaseMaterial.LIGHTFALLOFF_PHYSICAL;
  545. /**
  546. * Specifies that the material will keeps the reflection highlights over a transparent surface (only the most luminous ones).
  547. * A car glass is a good example of that. When the street lights reflects on it you can not see what is behind.
  548. * @internal
  549. */
  550. this._useRadianceOverAlpha = true;
  551. /**
  552. * Allows using an object space normal map (instead of tangent space).
  553. * @internal
  554. */
  555. this._useObjectSpaceNormalMap = false;
  556. /**
  557. * Allows using the bump map in parallax mode.
  558. * @internal
  559. */
  560. this._useParallax = false;
  561. /**
  562. * Allows using the bump map in parallax occlusion mode.
  563. * @internal
  564. */
  565. this._useParallaxOcclusion = false;
  566. /**
  567. * Controls the scale bias of the parallax mode.
  568. * @internal
  569. */
  570. this._parallaxScaleBias = 0.05;
  571. /**
  572. * If sets to true, disables all the lights affecting the material.
  573. * @internal
  574. */
  575. this._disableLighting = false;
  576. /**
  577. * Number of Simultaneous lights allowed on the material.
  578. * @internal
  579. */
  580. this._maxSimultaneousLights = 4;
  581. /**
  582. * If sets to true, x component of normal map value will be inverted (x = 1.0 - x).
  583. * @internal
  584. */
  585. this._invertNormalMapX = false;
  586. /**
  587. * If sets to true, y component of normal map value will be inverted (y = 1.0 - y).
  588. * @internal
  589. */
  590. this._invertNormalMapY = false;
  591. /**
  592. * If sets to true and backfaceCulling is false, normals will be flipped on the backside.
  593. * @internal
  594. */
  595. this._twoSidedLighting = false;
  596. /**
  597. * Defines the alpha limits in alpha test mode.
  598. * @internal
  599. */
  600. this._alphaCutOff = 0.4;
  601. /**
  602. * Enforces alpha test in opaque or blend mode in order to improve the performances of some situations.
  603. * @internal
  604. */
  605. this._forceAlphaTest = false;
  606. /**
  607. * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
  608. * And/Or occlude the blended part. (alpha is converted to gamma to compute the fresnel)
  609. * @internal
  610. */
  611. this._useAlphaFresnel = false;
  612. /**
  613. * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
  614. * And/Or occlude the blended part. (alpha stays linear to compute the fresnel)
  615. * @internal
  616. */
  617. this._useLinearAlphaFresnel = false;
  618. /**
  619. * Specifies the environment BRDF texture used to compute the scale and offset roughness values
  620. * from cos theta and roughness:
  621. * http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  622. * @internal
  623. */
  624. this._environmentBRDFTexture = null;
  625. /**
  626. * Force the shader to compute irradiance in the fragment shader in order to take bump in account.
  627. * @internal
  628. */
  629. this._forceIrradianceInFragment = false;
  630. this._realTimeFiltering = false;
  631. this._realTimeFilteringQuality = 8;
  632. /**
  633. * Force normal to face away from face.
  634. * @internal
  635. */
  636. this._forceNormalForward = false;
  637. /**
  638. * Enables specular anti aliasing in the PBR shader.
  639. * It will both interacts on the Geometry for analytical and IBL lighting.
  640. * It also prefilter the roughness map based on the bump values.
  641. * @internal
  642. */
  643. this._enableSpecularAntiAliasing = false;
  644. /**
  645. * Keep track of the image processing observer to allow dispose and replace.
  646. */
  647. this._imageProcessingObserver = null;
  648. /**
  649. * Stores the available render targets.
  650. */
  651. this._renderTargets = new SmartArray(16);
  652. /**
  653. * Sets the global ambient color for the material used in lighting calculations.
  654. */
  655. this._globalAmbientColor = new Color3(0, 0, 0);
  656. /**
  657. * If set to true, no lighting calculations will be applied.
  658. */
  659. this._unlit = false;
  660. /**
  661. * If sets to true, the decal map will be applied after the detail map. Else, it is applied before (default: false)
  662. */
  663. this._applyDecalMapAfterDetailMap = false;
  664. this._debugMode = 0;
  665. /**
  666. * @internal
  667. * This is reserved for the inspector.
  668. * Defines the material debug mode.
  669. * It helps seeing only some components of the material while troubleshooting.
  670. */
  671. this.debugMode = 0;
  672. /**
  673. * @internal
  674. * This is reserved for the inspector.
  675. * Specify from where on screen the debug mode should start.
  676. * The value goes from -1 (full screen) to 1 (not visible)
  677. * It helps with side by side comparison against the final render
  678. * This defaults to -1
  679. */
  680. this.debugLimit = -1;
  681. /**
  682. * @internal
  683. * This is reserved for the inspector.
  684. * As the default viewing range might not be enough (if the ambient is really small for instance)
  685. * You can use the factor to better multiply the final value.
  686. */
  687. this.debugFactor = 1;
  688. this._cacheHasRenderTargetTextures = false;
  689. this.brdf = new PBRBRDFConfiguration(this);
  690. this.clearCoat = new PBRClearCoatConfiguration(this);
  691. this.iridescence = new PBRIridescenceConfiguration(this);
  692. this.anisotropy = new PBRAnisotropicConfiguration(this);
  693. this.sheen = new PBRSheenConfiguration(this);
  694. this.subSurface = new PBRSubSurfaceConfiguration(this);
  695. this.detailMap = new DetailMapConfiguration(this);
  696. // Setup the default processing configuration to the scene.
  697. this._attachImageProcessingConfiguration(null);
  698. this.getRenderTargetTextures = () => {
  699. this._renderTargets.reset();
  700. if (MaterialFlags.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  701. this._renderTargets.push(this._reflectionTexture);
  702. }
  703. this._eventInfo.renderTargets = this._renderTargets;
  704. this._callbackPluginEventFillRenderTargetTextures(this._eventInfo);
  705. return this._renderTargets;
  706. };
  707. this._environmentBRDFTexture = GetEnvironmentBRDFTexture(this.getScene());
  708. this.prePassConfiguration = new PrePassConfiguration();
  709. }
  710. /**
  711. * Gets a boolean indicating that current material needs to register RTT
  712. */
  713. get hasRenderTargetTextures() {
  714. if (MaterialFlags.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  715. return true;
  716. }
  717. return this._cacheHasRenderTargetTextures;
  718. }
  719. /**
  720. * Can this material render to prepass
  721. */
  722. get isPrePassCapable() {
  723. return !this.disableDepthWrite;
  724. }
  725. /**
  726. * @returns the name of the material class.
  727. */
  728. getClassName() {
  729. return "PBRBaseMaterial";
  730. }
  731. /**
  732. * Returns true if alpha blending should be disabled.
  733. */
  734. get _disableAlphaBlending() {
  735. return (this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_OPAQUE ||
  736. this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_ALPHATEST ||
  737. this.subSurface?.disableAlphaBlending);
  738. }
  739. /**
  740. * @returns whether or not this material should be rendered in alpha blend mode.
  741. */
  742. needAlphaBlending() {
  743. if (this._disableAlphaBlending) {
  744. return false;
  745. }
  746. return this.alpha < 1.0 || this._opacityTexture != null || this._shouldUseAlphaFromAlbedoTexture();
  747. }
  748. /**
  749. * @returns whether or not this material should be rendered in alpha test mode.
  750. */
  751. needAlphaTesting() {
  752. if (this._forceAlphaTest) {
  753. return true;
  754. }
  755. if (this.subSurface?.disableAlphaBlending) {
  756. return false;
  757. }
  758. return this._hasAlphaChannel() && (this._transparencyMode == null || this._transparencyMode === PBRBaseMaterial.PBRMATERIAL_ALPHATEST);
  759. }
  760. /**
  761. * @returns whether or not the alpha value of the albedo texture should be used for alpha blending.
  762. */
  763. _shouldUseAlphaFromAlbedoTexture() {
  764. return this._albedoTexture != null && this._albedoTexture.hasAlpha && this._useAlphaFromAlbedoTexture && this._transparencyMode !== PBRBaseMaterial.PBRMATERIAL_OPAQUE;
  765. }
  766. /**
  767. * @returns whether or not there is a usable alpha channel for transparency.
  768. */
  769. _hasAlphaChannel() {
  770. return (this._albedoTexture != null && this._albedoTexture.hasAlpha) || this._opacityTexture != null;
  771. }
  772. /**
  773. * @returns the texture used for the alpha test.
  774. */
  775. getAlphaTestTexture() {
  776. return this._albedoTexture;
  777. }
  778. /**
  779. * Specifies that the submesh is ready to be used.
  780. * @param mesh - BJS mesh.
  781. * @param subMesh - A submesh of the BJS mesh. Used to check if it is ready.
  782. * @param useInstances - Specifies that instances should be used.
  783. * @returns - boolean indicating that the submesh is ready or not.
  784. */
  785. isReadyForSubMesh(mesh, subMesh, useInstances) {
  786. if (!this._uniformBufferLayoutBuilt) {
  787. this.buildUniformLayout();
  788. }
  789. const drawWrapper = subMesh._drawWrapper;
  790. if (drawWrapper.effect && this.isFrozen) {
  791. if (drawWrapper._wasPreviouslyReady && drawWrapper._wasPreviouslyUsingInstances === useInstances) {
  792. return true;
  793. }
  794. }
  795. if (!subMesh.materialDefines) {
  796. this._callbackPluginEventGeneric(MaterialPluginEvent.GetDefineNames, this._eventInfo);
  797. subMesh.materialDefines = new PBRMaterialDefines(this._eventInfo.defineNames);
  798. }
  799. const defines = subMesh.materialDefines;
  800. if (this._isReadyForSubMesh(subMesh)) {
  801. return true;
  802. }
  803. const scene = this.getScene();
  804. const engine = scene.getEngine();
  805. if (defines._areTexturesDirty) {
  806. this._eventInfo.hasRenderTargetTextures = false;
  807. this._callbackPluginEventHasRenderTargetTextures(this._eventInfo);
  808. this._cacheHasRenderTargetTextures = this._eventInfo.hasRenderTargetTextures;
  809. if (scene.texturesEnabled) {
  810. if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
  811. if (!this._albedoTexture.isReadyOrNotBlocking()) {
  812. return false;
  813. }
  814. }
  815. if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
  816. if (!this._ambientTexture.isReadyOrNotBlocking()) {
  817. return false;
  818. }
  819. }
  820. if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
  821. if (!this._opacityTexture.isReadyOrNotBlocking()) {
  822. return false;
  823. }
  824. }
  825. const reflectionTexture = this._getReflectionTexture();
  826. if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
  827. if (!reflectionTexture.isReadyOrNotBlocking()) {
  828. return false;
  829. }
  830. if (reflectionTexture.irradianceTexture) {
  831. if (!reflectionTexture.irradianceTexture.isReadyOrNotBlocking()) {
  832. return false;
  833. }
  834. }
  835. else {
  836. // Not ready until spherical are ready too.
  837. if (!reflectionTexture.sphericalPolynomial && reflectionTexture.getInternalTexture()?._sphericalPolynomialPromise) {
  838. return false;
  839. }
  840. }
  841. }
  842. if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
  843. if (!this._lightmapTexture.isReadyOrNotBlocking()) {
  844. return false;
  845. }
  846. }
  847. if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
  848. if (!this._emissiveTexture.isReadyOrNotBlocking()) {
  849. return false;
  850. }
  851. }
  852. if (MaterialFlags.SpecularTextureEnabled) {
  853. if (this._metallicTexture) {
  854. if (!this._metallicTexture.isReadyOrNotBlocking()) {
  855. return false;
  856. }
  857. }
  858. else if (this._reflectivityTexture) {
  859. if (!this._reflectivityTexture.isReadyOrNotBlocking()) {
  860. return false;
  861. }
  862. }
  863. if (this._metallicReflectanceTexture) {
  864. if (!this._metallicReflectanceTexture.isReadyOrNotBlocking()) {
  865. return false;
  866. }
  867. }
  868. if (this._reflectanceTexture) {
  869. if (!this._reflectanceTexture.isReadyOrNotBlocking()) {
  870. return false;
  871. }
  872. }
  873. if (this._microSurfaceTexture) {
  874. if (!this._microSurfaceTexture.isReadyOrNotBlocking()) {
  875. return false;
  876. }
  877. }
  878. }
  879. if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
  880. // Bump texture cannot be not blocking.
  881. if (!this._bumpTexture.isReady()) {
  882. return false;
  883. }
  884. }
  885. if (this._environmentBRDFTexture && MaterialFlags.ReflectionTextureEnabled) {
  886. // This is blocking.
  887. if (!this._environmentBRDFTexture.isReady()) {
  888. return false;
  889. }
  890. }
  891. }
  892. }
  893. this._eventInfo.isReadyForSubMesh = true;
  894. this._eventInfo.defines = defines;
  895. this._eventInfo.subMesh = subMesh;
  896. this._callbackPluginEventIsReadyForSubMesh(this._eventInfo);
  897. if (!this._eventInfo.isReadyForSubMesh) {
  898. return false;
  899. }
  900. if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
  901. if (!this._imageProcessingConfiguration.isReady()) {
  902. return false;
  903. }
  904. }
  905. if (!engine.getCaps().standardDerivatives && !mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  906. mesh.createNormals(true);
  907. Logger.Warn("PBRMaterial: Normals have been created for the mesh: " + mesh.name);
  908. }
  909. const previousEffect = subMesh.effect;
  910. const lightDisposed = defines._areLightsDisposed;
  911. let effect = this._prepareEffect(mesh, defines, this.onCompiled, this.onError, useInstances, null, subMesh.getRenderingMesh().hasThinInstances);
  912. let forceWasNotReadyPreviously = false;
  913. if (effect) {
  914. if (this._onEffectCreatedObservable) {
  915. onCreatedEffectParameters.effect = effect;
  916. onCreatedEffectParameters.subMesh = subMesh;
  917. this._onEffectCreatedObservable.notifyObservers(onCreatedEffectParameters);
  918. }
  919. // Use previous effect while new one is compiling
  920. if (this.allowShaderHotSwapping && previousEffect && !effect.isReady()) {
  921. effect = previousEffect;
  922. defines.markAsUnprocessed();
  923. forceWasNotReadyPreviously = this.isFrozen;
  924. if (lightDisposed) {
  925. // re register in case it takes more than one frame.
  926. defines._areLightsDisposed = true;
  927. return false;
  928. }
  929. }
  930. else {
  931. scene.resetCachedMaterial();
  932. subMesh.setEffect(effect, defines, this._materialContext);
  933. }
  934. }
  935. if (!subMesh.effect || !subMesh.effect.isReady()) {
  936. return false;
  937. }
  938. defines._renderId = scene.getRenderId();
  939. drawWrapper._wasPreviouslyReady = forceWasNotReadyPreviously ? false : true;
  940. drawWrapper._wasPreviouslyUsingInstances = !!useInstances;
  941. this._checkScenePerformancePriority();
  942. return true;
  943. }
  944. /**
  945. * Specifies if the material uses metallic roughness workflow.
  946. * @returns boolean specifying if the material uses metallic roughness workflow.
  947. */
  948. isMetallicWorkflow() {
  949. if (this._metallic != null || this._roughness != null || this._metallicTexture) {
  950. return true;
  951. }
  952. return false;
  953. }
  954. _prepareEffect(mesh, defines, onCompiled = null, onError = null, useInstances = null, useClipPlane = null, useThinInstances) {
  955. this._prepareDefines(mesh, defines, useInstances, useClipPlane, useThinInstances);
  956. if (!defines.isDirty) {
  957. return null;
  958. }
  959. defines.markAsProcessed();
  960. const scene = this.getScene();
  961. const engine = scene.getEngine();
  962. // Fallbacks
  963. const fallbacks = new EffectFallbacks();
  964. let fallbackRank = 0;
  965. if (defines.USESPHERICALINVERTEX) {
  966. fallbacks.addFallback(fallbackRank++, "USESPHERICALINVERTEX");
  967. }
  968. if (defines.FOG) {
  969. fallbacks.addFallback(fallbackRank, "FOG");
  970. }
  971. if (defines.SPECULARAA) {
  972. fallbacks.addFallback(fallbackRank, "SPECULARAA");
  973. }
  974. if (defines.POINTSIZE) {
  975. fallbacks.addFallback(fallbackRank, "POINTSIZE");
  976. }
  977. if (defines.LOGARITHMICDEPTH) {
  978. fallbacks.addFallback(fallbackRank, "LOGARITHMICDEPTH");
  979. }
  980. if (defines.PARALLAX) {
  981. fallbacks.addFallback(fallbackRank, "PARALLAX");
  982. }
  983. if (defines.PARALLAX_RHS) {
  984. fallbacks.addFallback(fallbackRank, "PARALLAX_RHS");
  985. }
  986. if (defines.PARALLAXOCCLUSION) {
  987. fallbacks.addFallback(fallbackRank++, "PARALLAXOCCLUSION");
  988. }
  989. if (defines.ENVIRONMENTBRDF) {
  990. fallbacks.addFallback(fallbackRank++, "ENVIRONMENTBRDF");
  991. }
  992. if (defines.TANGENT) {
  993. fallbacks.addFallback(fallbackRank++, "TANGENT");
  994. }
  995. if (defines.BUMP) {
  996. fallbacks.addFallback(fallbackRank++, "BUMP");
  997. }
  998. fallbackRank = HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights, fallbackRank++);
  999. if (defines.SPECULARTERM) {
  1000. fallbacks.addFallback(fallbackRank++, "SPECULARTERM");
  1001. }
  1002. if (defines.USESPHERICALFROMREFLECTIONMAP) {
  1003. fallbacks.addFallback(fallbackRank++, "USESPHERICALFROMREFLECTIONMAP");
  1004. }
  1005. if (defines.USEIRRADIANCEMAP) {
  1006. fallbacks.addFallback(fallbackRank++, "USEIRRADIANCEMAP");
  1007. }
  1008. if (defines.LIGHTMAP) {
  1009. fallbacks.addFallback(fallbackRank++, "LIGHTMAP");
  1010. }
  1011. if (defines.NORMAL) {
  1012. fallbacks.addFallback(fallbackRank++, "NORMAL");
  1013. }
  1014. if (defines.AMBIENT) {
  1015. fallbacks.addFallback(fallbackRank++, "AMBIENT");
  1016. }
  1017. if (defines.EMISSIVE) {
  1018. fallbacks.addFallback(fallbackRank++, "EMISSIVE");
  1019. }
  1020. if (defines.VERTEXCOLOR) {
  1021. fallbacks.addFallback(fallbackRank++, "VERTEXCOLOR");
  1022. }
  1023. if (defines.MORPHTARGETS) {
  1024. fallbacks.addFallback(fallbackRank++, "MORPHTARGETS");
  1025. }
  1026. if (defines.MULTIVIEW) {
  1027. fallbacks.addFallback(0, "MULTIVIEW");
  1028. }
  1029. //Attributes
  1030. const attribs = [VertexBuffer.PositionKind];
  1031. if (defines.NORMAL) {
  1032. attribs.push(VertexBuffer.NormalKind);
  1033. }
  1034. if (defines.TANGENT) {
  1035. attribs.push(VertexBuffer.TangentKind);
  1036. }
  1037. for (let i = 1; i <= 6; ++i) {
  1038. if (defines["UV" + i]) {
  1039. attribs.push(`uv${i === 1 ? "" : i}`);
  1040. }
  1041. }
  1042. if (defines.VERTEXCOLOR) {
  1043. attribs.push(VertexBuffer.ColorKind);
  1044. }
  1045. PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  1046. PrepareAttributesForInstances(attribs, defines);
  1047. PrepareAttributesForMorphTargets(attribs, mesh, defines);
  1048. PrepareAttributesForBakedVertexAnimation(attribs, mesh, defines);
  1049. let shaderName = "pbr";
  1050. const uniforms = [
  1051. "world",
  1052. "view",
  1053. "viewProjection",
  1054. "vEyePosition",
  1055. "vLightsType",
  1056. "vAmbientColor",
  1057. "vAlbedoColor",
  1058. "vReflectivityColor",
  1059. "vMetallicReflectanceFactors",
  1060. "vEmissiveColor",
  1061. "visibility",
  1062. "vReflectionColor",
  1063. "vFogInfos",
  1064. "vFogColor",
  1065. "pointSize",
  1066. "vAlbedoInfos",
  1067. "vAmbientInfos",
  1068. "vOpacityInfos",
  1069. "vReflectionInfos",
  1070. "vReflectionPosition",
  1071. "vReflectionSize",
  1072. "vEmissiveInfos",
  1073. "vReflectivityInfos",
  1074. "vReflectionFilteringInfo",
  1075. "vMetallicReflectanceInfos",
  1076. "vReflectanceInfos",
  1077. "vMicroSurfaceSamplerInfos",
  1078. "vBumpInfos",
  1079. "vLightmapInfos",
  1080. "mBones",
  1081. "albedoMatrix",
  1082. "ambientMatrix",
  1083. "opacityMatrix",
  1084. "reflectionMatrix",
  1085. "emissiveMatrix",
  1086. "reflectivityMatrix",
  1087. "normalMatrix",
  1088. "microSurfaceSamplerMatrix",
  1089. "bumpMatrix",
  1090. "lightmapMatrix",
  1091. "metallicReflectanceMatrix",
  1092. "reflectanceMatrix",
  1093. "vLightingIntensity",
  1094. "logarithmicDepthConstant",
  1095. "vSphericalX",
  1096. "vSphericalY",
  1097. "vSphericalZ",
  1098. "vSphericalXX_ZZ",
  1099. "vSphericalYY_ZZ",
  1100. "vSphericalZZ",
  1101. "vSphericalXY",
  1102. "vSphericalYZ",
  1103. "vSphericalZX",
  1104. "vSphericalL00",
  1105. "vSphericalL1_1",
  1106. "vSphericalL10",
  1107. "vSphericalL11",
  1108. "vSphericalL2_2",
  1109. "vSphericalL2_1",
  1110. "vSphericalL20",
  1111. "vSphericalL21",
  1112. "vSphericalL22",
  1113. "vReflectionMicrosurfaceInfos",
  1114. "vTangentSpaceParams",
  1115. "boneTextureWidth",
  1116. "vDebugMode",
  1117. "morphTargetTextureInfo",
  1118. "morphTargetTextureIndices",
  1119. ];
  1120. const samplers = [
  1121. "albedoSampler",
  1122. "reflectivitySampler",
  1123. "ambientSampler",
  1124. "emissiveSampler",
  1125. "bumpSampler",
  1126. "lightmapSampler",
  1127. "opacitySampler",
  1128. "reflectionSampler",
  1129. "reflectionSamplerLow",
  1130. "reflectionSamplerHigh",
  1131. "irradianceSampler",
  1132. "microSurfaceSampler",
  1133. "environmentBrdfSampler",
  1134. "boneSampler",
  1135. "metallicReflectanceSampler",
  1136. "reflectanceSampler",
  1137. "morphTargets",
  1138. "oitDepthSampler",
  1139. "oitFrontColorSampler",
  1140. ];
  1141. const uniformBuffers = ["Material", "Scene", "Mesh"];
  1142. const indexParameters = { maxSimultaneousLights: this._maxSimultaneousLights, maxSimultaneousMorphTargets: defines.NUM_MORPH_INFLUENCERS };
  1143. this._eventInfo.fallbacks = fallbacks;
  1144. this._eventInfo.fallbackRank = fallbackRank;
  1145. this._eventInfo.defines = defines;
  1146. this._eventInfo.uniforms = uniforms;
  1147. this._eventInfo.attributes = attribs;
  1148. this._eventInfo.samplers = samplers;
  1149. this._eventInfo.uniformBuffersNames = uniformBuffers;
  1150. this._eventInfo.customCode = undefined;
  1151. this._eventInfo.mesh = mesh;
  1152. this._eventInfo.indexParameters = indexParameters;
  1153. this._callbackPluginEventGeneric(MaterialPluginEvent.PrepareEffect, this._eventInfo);
  1154. PrePassConfiguration.AddUniforms(uniforms);
  1155. PrePassConfiguration.AddSamplers(samplers);
  1156. addClipPlaneUniforms(uniforms);
  1157. if (ImageProcessingConfiguration) {
  1158. ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
  1159. ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
  1160. }
  1161. PrepareUniformsAndSamplersList({
  1162. uniformsNames: uniforms,
  1163. uniformBuffersNames: uniformBuffers,
  1164. samplers: samplers,
  1165. defines: defines,
  1166. maxSimultaneousLights: this._maxSimultaneousLights,
  1167. });
  1168. const csnrOptions = {};
  1169. if (this.customShaderNameResolve) {
  1170. shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines, attribs, csnrOptions);
  1171. }
  1172. const join = defines.toString();
  1173. const effect = engine.createEffect(shaderName, {
  1174. attributes: attribs,
  1175. uniformsNames: uniforms,
  1176. uniformBuffersNames: uniformBuffers,
  1177. samplers: samplers,
  1178. defines: join,
  1179. fallbacks: fallbacks,
  1180. onCompiled: onCompiled,
  1181. onError: onError,
  1182. indexParameters,
  1183. processFinalCode: csnrOptions.processFinalCode,
  1184. processCodeAfterIncludes: this._eventInfo.customCode,
  1185. multiTarget: defines.PREPASS,
  1186. }, engine);
  1187. this._eventInfo.customCode = undefined;
  1188. return effect;
  1189. }
  1190. _prepareDefines(mesh, defines, useInstances = null, useClipPlane = null, useThinInstances = false) {
  1191. const scene = this.getScene();
  1192. const engine = scene.getEngine();
  1193. // Lights
  1194. PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
  1195. defines._needNormals = true;
  1196. // Multiview
  1197. PrepareDefinesForMultiview(scene, defines);
  1198. // PrePass
  1199. const oit = this.needAlphaBlendingForMesh(mesh) && this.getScene().useOrderIndependentTransparency;
  1200. PrepareDefinesForPrePass(scene, defines, this.canRenderToMRT && !oit);
  1201. // Order independant transparency
  1202. PrepareDefinesForOIT(scene, defines, oit);
  1203. // Textures
  1204. defines.METALLICWORKFLOW = this.isMetallicWorkflow();
  1205. if (defines._areTexturesDirty) {
  1206. defines._needUVs = false;
  1207. for (let i = 1; i <= 6; ++i) {
  1208. defines["MAINUV" + i] = false;
  1209. }
  1210. if (scene.texturesEnabled) {
  1211. defines.ALBEDODIRECTUV = 0;
  1212. defines.AMBIENTDIRECTUV = 0;
  1213. defines.OPACITYDIRECTUV = 0;
  1214. defines.EMISSIVEDIRECTUV = 0;
  1215. defines.REFLECTIVITYDIRECTUV = 0;
  1216. defines.MICROSURFACEMAPDIRECTUV = 0;
  1217. defines.METALLIC_REFLECTANCEDIRECTUV = 0;
  1218. defines.REFLECTANCEDIRECTUV = 0;
  1219. defines.BUMPDIRECTUV = 0;
  1220. defines.LIGHTMAPDIRECTUV = 0;
  1221. if (engine.getCaps().textureLOD) {
  1222. defines.LODBASEDMICROSFURACE = true;
  1223. }
  1224. if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
  1225. PrepareDefinesForMergedUV(this._albedoTexture, defines, "ALBEDO");
  1226. defines.GAMMAALBEDO = this._albedoTexture.gammaSpace;
  1227. }
  1228. else {
  1229. defines.ALBEDO = false;
  1230. }
  1231. if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
  1232. PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
  1233. defines.AMBIENTINGRAYSCALE = this._useAmbientInGrayScale;
  1234. }
  1235. else {
  1236. defines.AMBIENT = false;
  1237. }
  1238. if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
  1239. PrepareDefinesForMergedUV(this._opacityTexture, defines, "OPACITY");
  1240. defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
  1241. }
  1242. else {
  1243. defines.OPACITY = false;
  1244. }
  1245. const reflectionTexture = this._getReflectionTexture();
  1246. if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
  1247. defines.REFLECTION = true;
  1248. defines.GAMMAREFLECTION = reflectionTexture.gammaSpace;
  1249. defines.RGBDREFLECTION = reflectionTexture.isRGBD;
  1250. defines.LODINREFLECTIONALPHA = reflectionTexture.lodLevelInAlpha;
  1251. defines.LINEARSPECULARREFLECTION = reflectionTexture.linearSpecularLOD;
  1252. if (this.realTimeFiltering && this.realTimeFilteringQuality > 0) {
  1253. defines.NUM_SAMPLES = "" + this.realTimeFilteringQuality;
  1254. if (engine._features.needTypeSuffixInShaderConstants) {
  1255. defines.NUM_SAMPLES = defines.NUM_SAMPLES + "u";
  1256. }
  1257. defines.REALTIME_FILTERING = true;
  1258. }
  1259. else {
  1260. defines.REALTIME_FILTERING = false;
  1261. }
  1262. defines.INVERTCUBICMAP = reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE;
  1263. defines.REFLECTIONMAP_3D = reflectionTexture.isCube;
  1264. defines.REFLECTIONMAP_OPPOSITEZ = defines.REFLECTIONMAP_3D && this.getScene().useRightHandedSystem ? !reflectionTexture.invertZ : reflectionTexture.invertZ;
  1265. defines.REFLECTIONMAP_CUBIC = false;
  1266. defines.REFLECTIONMAP_EXPLICIT = false;
  1267. defines.REFLECTIONMAP_PLANAR = false;
  1268. defines.REFLECTIONMAP_PROJECTION = false;
  1269. defines.REFLECTIONMAP_SKYBOX = false;
  1270. defines.REFLECTIONMAP_SPHERICAL = false;
  1271. defines.REFLECTIONMAP_EQUIRECTANGULAR = false;
  1272. defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  1273. defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  1274. switch (reflectionTexture.coordinatesMode) {
  1275. case Texture.EXPLICIT_MODE:
  1276. defines.REFLECTIONMAP_EXPLICIT = true;
  1277. break;
  1278. case Texture.PLANAR_MODE:
  1279. defines.REFLECTIONMAP_PLANAR = true;
  1280. break;
  1281. case Texture.PROJECTION_MODE:
  1282. defines.REFLECTIONMAP_PROJECTION = true;
  1283. break;
  1284. case Texture.SKYBOX_MODE:
  1285. defines.REFLECTIONMAP_SKYBOX = true;
  1286. break;
  1287. case Texture.SPHERICAL_MODE:
  1288. defines.REFLECTIONMAP_SPHERICAL = true;
  1289. break;
  1290. case Texture.EQUIRECTANGULAR_MODE:
  1291. defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  1292. break;
  1293. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  1294. defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
  1295. break;
  1296. case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
  1297. defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = true;
  1298. break;
  1299. case Texture.CUBIC_MODE:
  1300. case Texture.INVCUBIC_MODE:
  1301. default:
  1302. defines.REFLECTIONMAP_CUBIC = true;
  1303. defines.USE_LOCAL_REFLECTIONMAP_CUBIC = reflectionTexture.boundingBoxSize ? true : false;
  1304. break;
  1305. }
  1306. if (reflectionTexture.coordinatesMode !== Texture.SKYBOX_MODE) {
  1307. if (reflectionTexture.irradianceTexture) {
  1308. defines.USEIRRADIANCEMAP = true;
  1309. defines.USESPHERICALFROMREFLECTIONMAP = false;
  1310. }
  1311. // Assume using spherical polynomial if the reflection texture is a cube map
  1312. else if (reflectionTexture.isCube) {
  1313. defines.USESPHERICALFROMREFLECTIONMAP = true;
  1314. defines.USEIRRADIANCEMAP = false;
  1315. if (this._forceIrradianceInFragment || this.realTimeFiltering || this._twoSidedLighting || engine.getCaps().maxVaryingVectors <= 8) {
  1316. defines.USESPHERICALINVERTEX = false;
  1317. }
  1318. else {
  1319. defines.USESPHERICALINVERTEX = true;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. else {
  1325. defines.REFLECTION = false;
  1326. defines.REFLECTIONMAP_3D = false;
  1327. defines.REFLECTIONMAP_SPHERICAL = false;
  1328. defines.REFLECTIONMAP_PLANAR = false;
  1329. defines.REFLECTIONMAP_CUBIC = false;
  1330. defines.USE_LOCAL_REFLECTIONMAP_CUBIC = false;
  1331. defines.REFLECTIONMAP_PROJECTION = false;
  1332. defines.REFLECTIONMAP_SKYBOX = false;
  1333. defines.REFLECTIONMAP_EXPLICIT = false;
  1334. defines.REFLECTIONMAP_EQUIRECTANGULAR = false;
  1335. defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  1336. defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  1337. defines.INVERTCUBICMAP = false;
  1338. defines.USESPHERICALFROMREFLECTIONMAP = false;
  1339. defines.USEIRRADIANCEMAP = false;
  1340. defines.USESPHERICALINVERTEX = false;
  1341. defines.REFLECTIONMAP_OPPOSITEZ = false;
  1342. defines.LODINREFLECTIONALPHA = false;
  1343. defines.GAMMAREFLECTION = false;
  1344. defines.RGBDREFLECTION = false;
  1345. defines.LINEARSPECULARREFLECTION = false;
  1346. }
  1347. if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
  1348. PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
  1349. defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
  1350. defines.GAMMALIGHTMAP = this._lightmapTexture.gammaSpace;
  1351. defines.RGBDLIGHTMAP = this._lightmapTexture.isRGBD;
  1352. }
  1353. else {
  1354. defines.LIGHTMAP = false;
  1355. }
  1356. if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
  1357. PrepareDefinesForMergedUV(this._emissiveTexture, defines, "EMISSIVE");
  1358. defines.GAMMAEMISSIVE = this._emissiveTexture.gammaSpace;
  1359. }
  1360. else {
  1361. defines.EMISSIVE = false;
  1362. }
  1363. if (MaterialFlags.SpecularTextureEnabled) {
  1364. if (this._metallicTexture) {
  1365. PrepareDefinesForMergedUV(this._metallicTexture, defines, "REFLECTIVITY");
  1366. defines.ROUGHNESSSTOREINMETALMAPALPHA = this._useRoughnessFromMetallicTextureAlpha;
  1367. defines.ROUGHNESSSTOREINMETALMAPGREEN = !this._useRoughnessFromMetallicTextureAlpha && this._useRoughnessFromMetallicTextureGreen;
  1368. defines.METALLNESSSTOREINMETALMAPBLUE = this._useMetallnessFromMetallicTextureBlue;
  1369. defines.AOSTOREINMETALMAPRED = this._useAmbientOcclusionFromMetallicTextureRed;
  1370. defines.REFLECTIVITY_GAMMA = false;
  1371. }
  1372. else if (this._reflectivityTexture) {
  1373. PrepareDefinesForMergedUV(this._reflectivityTexture, defines, "REFLECTIVITY");
  1374. defines.MICROSURFACEFROMREFLECTIVITYMAP = this._useMicroSurfaceFromReflectivityMapAlpha;
  1375. defines.MICROSURFACEAUTOMATIC = this._useAutoMicroSurfaceFromReflectivityMap;
  1376. defines.REFLECTIVITY_GAMMA = this._reflectivityTexture.gammaSpace;
  1377. }
  1378. else {
  1379. defines.REFLECTIVITY = false;
  1380. }
  1381. if (this._metallicReflectanceTexture || this._reflectanceTexture) {
  1382. defines.METALLIC_REFLECTANCE_USE_ALPHA_ONLY = this._useOnlyMetallicFromMetallicReflectanceTexture;
  1383. if (this._metallicReflectanceTexture) {
  1384. PrepareDefinesForMergedUV(this._metallicReflectanceTexture, defines, "METALLIC_REFLECTANCE");
  1385. defines.METALLIC_REFLECTANCE_GAMMA = this._metallicReflectanceTexture.gammaSpace;
  1386. }
  1387. else {
  1388. defines.METALLIC_REFLECTANCE = false;
  1389. }
  1390. if (this._reflectanceTexture &&
  1391. (!this._metallicReflectanceTexture || (this._metallicReflectanceTexture && this._useOnlyMetallicFromMetallicReflectanceTexture))) {
  1392. PrepareDefinesForMergedUV(this._reflectanceTexture, defines, "REFLECTANCE");
  1393. defines.REFLECTANCE_GAMMA = this._reflectanceTexture.gammaSpace;
  1394. }
  1395. else {
  1396. defines.REFLECTANCE = false;
  1397. }
  1398. }
  1399. else {
  1400. defines.METALLIC_REFLECTANCE = false;
  1401. defines.REFLECTANCE = false;
  1402. }
  1403. if (this._microSurfaceTexture) {
  1404. PrepareDefinesForMergedUV(this._microSurfaceTexture, defines, "MICROSURFACEMAP");
  1405. }
  1406. else {
  1407. defines.MICROSURFACEMAP = false;
  1408. }
  1409. }
  1410. else {
  1411. defines.REFLECTIVITY = false;
  1412. defines.MICROSURFACEMAP = false;
  1413. }
  1414. if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
  1415. PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
  1416. if (this._useParallax && this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
  1417. defines.PARALLAX = true;
  1418. defines.PARALLAX_RHS = scene.useRightHandedSystem;
  1419. defines.PARALLAXOCCLUSION = !!this._useParallaxOcclusion;
  1420. }
  1421. else {
  1422. defines.PARALLAX = false;
  1423. }
  1424. defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
  1425. }
  1426. else {
  1427. defines.BUMP = false;
  1428. defines.PARALLAX = false;
  1429. defines.PARALLAX_RHS = false;
  1430. defines.PARALLAXOCCLUSION = false;
  1431. defines.OBJECTSPACE_NORMALMAP = false;
  1432. }
  1433. if (this._environmentBRDFTexture && MaterialFlags.ReflectionTextureEnabled) {
  1434. defines.ENVIRONMENTBRDF = true;
  1435. defines.ENVIRONMENTBRDF_RGBD = this._environmentBRDFTexture.isRGBD;
  1436. }
  1437. else {
  1438. defines.ENVIRONMENTBRDF = false;
  1439. defines.ENVIRONMENTBRDF_RGBD = false;
  1440. }
  1441. if (this._shouldUseAlphaFromAlbedoTexture()) {
  1442. defines.ALPHAFROMALBEDO = true;
  1443. }
  1444. else {
  1445. defines.ALPHAFROMALBEDO = false;
  1446. }
  1447. }
  1448. defines.SPECULAROVERALPHA = this._useSpecularOverAlpha;
  1449. if (this._lightFalloff === PBRBaseMaterial.LIGHTFALLOFF_STANDARD) {
  1450. defines.USEPHYSICALLIGHTFALLOFF = false;
  1451. defines.USEGLTFLIGHTFALLOFF = false;
  1452. }
  1453. else if (this._lightFalloff === PBRBaseMaterial.LIGHTFALLOFF_GLTF) {
  1454. defines.USEPHYSICALLIGHTFALLOFF = false;
  1455. defines.USEGLTFLIGHTFALLOFF = true;
  1456. }
  1457. else {
  1458. defines.USEPHYSICALLIGHTFALLOFF = true;
  1459. defines.USEGLTFLIGHTFALLOFF = false;
  1460. }
  1461. defines.RADIANCEOVERALPHA = this._useRadianceOverAlpha;
  1462. if (!this.backFaceCulling && this._twoSidedLighting) {
  1463. defines.TWOSIDEDLIGHTING = true;
  1464. }
  1465. else {
  1466. defines.TWOSIDEDLIGHTING = false;
  1467. }
  1468. defines.SPECULARAA = engine.getCaps().standardDerivatives && this._enableSpecularAntiAliasing;
  1469. }
  1470. if (defines._areTexturesDirty || defines._areMiscDirty) {
  1471. defines.ALPHATESTVALUE = `${this._alphaCutOff}${this._alphaCutOff % 1 === 0 ? "." : ""}`;
  1472. defines.PREMULTIPLYALPHA = this.alphaMode === 7 || this.alphaMode === 8;
  1473. defines.ALPHABLEND = this.needAlphaBlendingForMesh(mesh);
  1474. defines.ALPHAFRESNEL = this._useAlphaFresnel || this._useLinearAlphaFresnel;
  1475. defines.LINEARALPHAFRESNEL = this._useLinearAlphaFresnel;
  1476. }
  1477. if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
  1478. this._imageProcessingConfiguration.prepareDefines(defines);
  1479. }
  1480. defines.FORCENORMALFORWARD = this._forceNormalForward;
  1481. defines.RADIANCEOCCLUSION = this._useRadianceOcclusion;
  1482. defines.HORIZONOCCLUSION = this._useHorizonOcclusion;
  1483. // Misc.
  1484. if (defines._areMiscDirty) {
  1485. PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh) || this._forceAlphaTest, defines, this._applyDecalMapAfterDetailMap);
  1486. defines.UNLIT = this._unlit || ((this.pointsCloud || this.wireframe) && !mesh.isVerticesDataPresent(VertexBuffer.NormalKind));
  1487. defines.DEBUGMODE = this._debugMode;
  1488. }
  1489. // Values that need to be evaluated on every frame
  1490. PrepareDefinesForFrameBoundValues(scene, engine, this, defines, useInstances ? true : false, useClipPlane, useThinInstances);
  1491. // External config
  1492. this._eventInfo.defines = defines;
  1493. this._eventInfo.mesh = mesh;
  1494. this._callbackPluginEventPrepareDefinesBeforeAttributes(this._eventInfo);
  1495. // Attribs
  1496. PrepareDefinesForAttributes(mesh, defines, true, true, true, this._transparencyMode !== PBRBaseMaterial.PBRMATERIAL_OPAQUE);
  1497. // External config
  1498. this._callbackPluginEventPrepareDefines(this._eventInfo);
  1499. }
  1500. /**
  1501. * Force shader compilation
  1502. * @param mesh - Define the mesh we want to force the compilation for
  1503. * @param onCompiled - Define a callback triggered when the compilation completes
  1504. * @param options - Define the options used to create the compilation
  1505. */
  1506. forceCompilation(mesh, onCompiled, options) {
  1507. const localOptions = {
  1508. clipPlane: false,
  1509. useInstances: false,
  1510. ...options,
  1511. };
  1512. if (!this._uniformBufferLayoutBuilt) {
  1513. this.buildUniformLayout();
  1514. }
  1515. this._callbackPluginEventGeneric(MaterialPluginEvent.GetDefineNames, this._eventInfo);
  1516. const defines = new PBRMaterialDefines(this._eventInfo.defineNames);
  1517. const effect = this._prepareEffect(mesh, defines, undefined, undefined, localOptions.useInstances, localOptions.clipPlane, mesh.hasThinInstances);
  1518. if (this._onEffectCreatedObservable) {
  1519. onCreatedEffectParameters.effect = effect;
  1520. onCreatedEffectParameters.subMesh = null;
  1521. this._onEffectCreatedObservable.notifyObservers(onCreatedEffectParameters);
  1522. }
  1523. if (effect.isReady()) {
  1524. if (onCompiled) {
  1525. onCompiled(this);
  1526. }
  1527. }
  1528. else {
  1529. effect.onCompileObservable.add(() => {
  1530. if (onCompiled) {
  1531. onCompiled(this);
  1532. }
  1533. });
  1534. }
  1535. }
  1536. /**
  1537. * Initializes the uniform buffer layout for the shader.
  1538. */
  1539. buildUniformLayout() {
  1540. // Order is important !
  1541. const ubo = this._uniformBuffer;
  1542. ubo.addUniform("vAlbedoInfos", 2);
  1543. ubo.addUniform("vAmbientInfos", 4);
  1544. ubo.addUniform("vOpacityInfos", 2);
  1545. ubo.addUniform("vEmissiveInfos", 2);
  1546. ubo.addUniform("vLightmapInfos", 2);
  1547. ubo.addUniform("vReflectivityInfos", 3);
  1548. ubo.addUniform("vMicroSurfaceSamplerInfos", 2);
  1549. ubo.addUniform("vReflectionInfos", 2);
  1550. ubo.addUniform("vReflectionFilteringInfo", 2);
  1551. ubo.addUniform("vReflectionPosition", 3);
  1552. ubo.addUniform("vReflectionSize", 3);
  1553. ubo.addUniform("vBumpInfos", 3);
  1554. ubo.addUniform("albedoMatrix", 16);
  1555. ubo.addUniform("ambientMatrix", 16);
  1556. ubo.addUniform("opacityMatrix", 16);
  1557. ubo.addUniform("emissiveMatrix", 16);
  1558. ubo.addUniform("lightmapMatrix", 16);
  1559. ubo.addUniform("reflectivityMatrix", 16);
  1560. ubo.addUniform("microSurfaceSamplerMatrix", 16);
  1561. ubo.addUniform("bumpMatrix", 16);
  1562. ubo.addUniform("vTangentSpaceParams", 2);
  1563. ubo.addUniform("reflectionMatrix", 16);
  1564. ubo.addUniform("vReflectionColor", 3);
  1565. ubo.addUniform("vAlbedoColor", 4);
  1566. ubo.addUniform("vLightingIntensity", 4);
  1567. ubo.addUniform("vReflectionMicrosurfaceInfos", 3);
  1568. ubo.addUniform("pointSize", 1);
  1569. ubo.addUniform("vReflectivityColor", 4);
  1570. ubo.addUniform("vEmissiveColor", 3);
  1571. ubo.addUniform("vAmbientColor", 3);
  1572. ubo.addUniform("vDebugMode", 2);
  1573. ubo.addUniform("vMetallicReflectanceFactors", 4);
  1574. ubo.addUniform("vMetallicReflectanceInfos", 2);
  1575. ubo.addUniform("metallicReflectanceMatrix", 16);
  1576. ubo.addUniform("vReflectanceInfos", 2);
  1577. ubo.addUniform("reflectanceMatrix", 16);
  1578. ubo.addUniform("vSphericalL00", 3);
  1579. ubo.addUniform("vSphericalL1_1", 3);
  1580. ubo.addUniform("vSphericalL10", 3);
  1581. ubo.addUniform("vSphericalL11", 3);
  1582. ubo.addUniform("vSphericalL2_2", 3);
  1583. ubo.addUniform("vSphericalL2_1", 3);
  1584. ubo.addUniform("vSphericalL20", 3);
  1585. ubo.addUniform("vSphericalL21", 3);
  1586. ubo.addUniform("vSphericalL22", 3);
  1587. ubo.addUniform("vSphericalX", 3);
  1588. ubo.addUniform("vSphericalY", 3);
  1589. ubo.addUniform("vSphericalZ", 3);
  1590. ubo.addUniform("vSphericalXX_ZZ", 3);
  1591. ubo.addUniform("vSphericalYY_ZZ", 3);
  1592. ubo.addUniform("vSphericalZZ", 3);
  1593. ubo.addUniform("vSphericalXY", 3);
  1594. ubo.addUniform("vSphericalYZ", 3);
  1595. ubo.addUniform("vSphericalZX", 3);
  1596. super.buildUniformLayout();
  1597. }
  1598. /**
  1599. * Binds the submesh data.
  1600. * @param world - The world matrix.
  1601. * @param mesh - The BJS mesh.
  1602. * @param subMesh - A submesh of the BJS mesh.
  1603. */
  1604. bindForSubMesh(world, mesh, subMesh) {
  1605. const scene = this.getScene();
  1606. const defines = subMesh.materialDefines;
  1607. if (!defines) {
  1608. return;
  1609. }
  1610. const effect = subMesh.effect;
  1611. if (!effect) {
  1612. return;
  1613. }
  1614. this._activeEffect = effect;
  1615. // Matrices Mesh.
  1616. mesh.getMeshUniformBuffer().bindToEffect(effect, "Mesh");
  1617. mesh.transferToEffect(world);
  1618. const engine = scene.getEngine();
  1619. // Binding unconditionally
  1620. this._uniformBuffer.bindToEffect(effect, "Material");
  1621. this.prePassConfiguration.bindForSubMesh(this._activeEffect, scene, mesh, world, this.isFrozen);
  1622. this._eventInfo.subMesh = subMesh;
  1623. this._callbackPluginEventHardBindForSubMesh(this._eventInfo);
  1624. // Normal Matrix
  1625. if (defines.OBJECTSPACE_NORMALMAP) {
  1626. world.toNormalMatrix(this._normalMatrix);
  1627. this.bindOnlyNormalMatrix(this._normalMatrix);
  1628. }
  1629. const mustRebind = this._mustRebind(scene, effect, subMesh, mesh.visibility);
  1630. // Bones
  1631. BindBonesParameters(mesh, this._activeEffect, this.prePassConfiguration);
  1632. let reflectionTexture = null;
  1633. const ubo = this._uniformBuffer;
  1634. if (mustRebind) {
  1635. this.bindViewProjection(effect);
  1636. reflectionTexture = this._getReflectionTexture();
  1637. if (!ubo.useUbo || !this.isFrozen || !ubo.isSync || subMesh._drawWrapper._forceRebindOnNextCall) {
  1638. // Texture uniforms
  1639. if (scene.texturesEnabled) {
  1640. if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
  1641. ubo.updateFloat2("vAlbedoInfos", this._albedoTexture.coordinatesIndex, this._albedoTexture.level);
  1642. BindTextureMatrix(this._albedoTexture, ubo, "albedo");
  1643. }
  1644. if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
  1645. ubo.updateFloat4("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level, this._ambientTextureStrength, this._ambientTextureImpactOnAnalyticalLights);
  1646. BindTextureMatrix(this._ambientTexture, ubo, "ambient");
  1647. }
  1648. if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
  1649. ubo.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
  1650. BindTextureMatrix(this._opacityTexture, ubo, "opacity");
  1651. }
  1652. if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
  1653. ubo.updateMatrix("reflectionMatrix", reflectionTexture.getReflectionTextureMatrix());
  1654. ubo.updateFloat2("vReflectionInfos", reflectionTexture.level, 0);
  1655. if (reflectionTexture.boundingBoxSize) {
  1656. const cubeTexture = reflectionTexture;
  1657. ubo.updateVector3("vReflectionPosition", cubeTexture.boundingBoxPosition);
  1658. ubo.updateVector3("vReflectionSize", cubeTexture.boundingBoxSize);
  1659. }
  1660. if (this.realTimeFiltering) {
  1661. const width = reflectionTexture.getSize().width;
  1662. ubo.updateFloat2("vReflectionFilteringInfo", width, Scalar.Log2(width));
  1663. }
  1664. if (!defines.USEIRRADIANCEMAP) {
  1665. const polynomials = reflectionTexture.sphericalPolynomial;
  1666. if (defines.USESPHERICALFROMREFLECTIONMAP && polynomials) {
  1667. if (defines.SPHERICAL_HARMONICS) {
  1668. const preScaledHarmonics = polynomials.preScaledHarmonics;
  1669. ubo.updateVector3("vSphericalL00", preScaledHarmonics.l00);
  1670. ubo.updateVector3("vSphericalL1_1", preScaledHarmonics.l1_1);
  1671. ubo.updateVector3("vSphericalL10", preScaledHarmonics.l10);
  1672. ubo.updateVector3("vSphericalL11", preScaledHarmonics.l11);
  1673. ubo.updateVector3("vSphericalL2_2", preScaledHarmonics.l2_2);
  1674. ubo.updateVector3("vSphericalL2_1", preScaledHarmonics.l2_1);
  1675. ubo.updateVector3("vSphericalL20", preScaledHarmonics.l20);
  1676. ubo.updateVector3("vSphericalL21", preScaledHarmonics.l21);
  1677. ubo.updateVector3("vSphericalL22", preScaledHarmonics.l22);
  1678. }
  1679. else {
  1680. ubo.updateFloat3("vSphericalX", polynomials.x.x, polynomials.x.y, polynomials.x.z);
  1681. ubo.updateFloat3("vSphericalY", polynomials.y.x, polynomials.y.y, polynomials.y.z);
  1682. ubo.updateFloat3("vSphericalZ", polynomials.z.x, polynomials.z.y, polynomials.z.z);
  1683. ubo.updateFloat3("vSphericalXX_ZZ", polynomials.xx.x - polynomials.zz.x, polynomials.xx.y - polynomials.zz.y, polynomials.xx.z - polynomials.zz.z);
  1684. ubo.updateFloat3("vSphericalYY_ZZ", polynomials.yy.x - polynomials.zz.x, polynomials.yy.y - polynomials.zz.y, polynomials.yy.z - polynomials.zz.z);
  1685. ubo.updateFloat3("vSphericalZZ", polynomials.zz.x, polynomials.zz.y, polynomials.zz.z);
  1686. ubo.updateFloat3("vSphericalXY", polynomials.xy.x, polynomials.xy.y, polynomials.xy.z);
  1687. ubo.updateFloat3("vSphericalYZ", polynomials.yz.x, polynomials.yz.y, polynomials.yz.z);
  1688. ubo.updateFloat3("vSphericalZX", polynomials.zx.x, polynomials.zx.y, polynomials.zx.z);
  1689. }
  1690. }
  1691. }
  1692. ubo.updateFloat3("vReflectionMicrosurfaceInfos", reflectionTexture.getSize().width, reflectionTexture.lodGenerationScale, reflectionTexture.lodGenerationOffset);
  1693. }
  1694. if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
  1695. ubo.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
  1696. BindTextureMatrix(this._emissiveTexture, ubo, "emissive");
  1697. }
  1698. if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
  1699. ubo.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
  1700. BindTextureMatrix(this._lightmapTexture, ubo, "lightmap");
  1701. }
  1702. if (MaterialFlags.SpecularTextureEnabled) {
  1703. if (this._metallicTexture) {
  1704. ubo.updateFloat3("vReflectivityInfos", this._metallicTexture.coordinatesIndex, this._metallicTexture.level, this._ambientTextureStrength);
  1705. BindTextureMatrix(this._metallicTexture, ubo, "reflectivity");
  1706. }
  1707. else if (this._reflectivityTexture) {
  1708. ubo.updateFloat3("vReflectivityInfos", this._reflectivityTexture.coordinatesIndex, this._reflectivityTexture.level, 1.0);
  1709. BindTextureMatrix(this._reflectivityTexture, ubo, "reflectivity");
  1710. }
  1711. if (this._metallicReflectanceTexture) {
  1712. ubo.updateFloat2("vMetallicReflectanceInfos", this._metallicReflectanceTexture.coordinatesIndex, this._metallicReflectanceTexture.level);
  1713. BindTextureMatrix(this._metallicReflectanceTexture, ubo, "metallicReflectance");
  1714. }
  1715. if (this._reflectanceTexture && defines.REFLECTANCE) {
  1716. ubo.updateFloat2("vReflectanceInfos", this._reflectanceTexture.coordinatesIndex, this._reflectanceTexture.level);
  1717. BindTextureMatrix(this._reflectanceTexture, ubo, "reflectance");
  1718. }
  1719. if (this._microSurfaceTexture) {
  1720. ubo.updateFloat2("vMicroSurfaceSamplerInfos", this._microSurfaceTexture.coordinatesIndex, this._microSurfaceTexture.level);
  1721. BindTextureMatrix(this._microSurfaceTexture, ubo, "microSurfaceSampler");
  1722. }
  1723. }
  1724. if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
  1725. ubo.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, this._bumpTexture.level, this._parallaxScaleBias);
  1726. BindTextureMatrix(this._bumpTexture, ubo, "bump");
  1727. if (scene._mirroredCameraPosition) {
  1728. ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? 1.0 : -1.0, this._invertNormalMapY ? 1.0 : -1.0);
  1729. }
  1730. else {
  1731. ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? -1.0 : 1.0, this._invertNormalMapY ? -1.0 : 1.0);
  1732. }
  1733. }
  1734. }
  1735. // Point size
  1736. if (this.pointsCloud) {
  1737. ubo.updateFloat("pointSize", this.pointSize);
  1738. }
  1739. // Colors
  1740. if (defines.METALLICWORKFLOW) {
  1741. TmpColors.Color3[0].r = this._metallic === undefined || this._metallic === null ? 1 : this._metallic;
  1742. TmpColors.Color3[0].g = this._roughness === undefined || this._roughness === null ? 1 : this._roughness;
  1743. ubo.updateColor4("vReflectivityColor", TmpColors.Color3[0], 1);
  1744. const ior = this.subSurface?._indexOfRefraction ?? 1.5;
  1745. const outsideIOR = 1; // consider air as clear coat and other layers would remap in the shader.
  1746. // We are here deriving our default reflectance from a common value for none metallic surface.
  1747. // Based of the schlick fresnel approximation model
  1748. // for dielectrics.
  1749. const f0 = Math.pow((ior - outsideIOR) / (ior + outsideIOR), 2);
  1750. // Tweak the default F0 and F90 based on our given setup
  1751. this._metallicReflectanceColor.scaleToRef(f0 * this._metallicF0Factor, TmpColors.Color3[0]);
  1752. const metallicF90 = this._metallicF0Factor;
  1753. ubo.updateColor4("vMetallicReflectanceFactors", TmpColors.Color3[0], metallicF90);
  1754. }
  1755. else {
  1756. ubo.updateColor4("vReflectivityColor", this._reflectivityColor, this._microSurface);
  1757. }
  1758. ubo.updateColor3("vEmissiveColor", MaterialFlags.EmissiveTextureEnabled ? this._emissiveColor : Color3.BlackReadOnly);
  1759. ubo.updateColor3("vReflectionColor", this._reflectionColor);
  1760. if (!defines.SS_REFRACTION && this.subSurface?._linkRefractionWithTransparency) {
  1761. ubo.updateColor4("vAlbedoColor", this._albedoColor, 1);
  1762. }
  1763. else {
  1764. ubo.updateColor4("vAlbedoColor", this._albedoColor, this.alpha);
  1765. }
  1766. // Misc
  1767. this._lightingInfos.x = this._directIntensity;
  1768. this._lightingInfos.y = this._emissiveIntensity;
  1769. this._lightingInfos.z = this._environmentIntensity * scene.environmentIntensity;
  1770. this._lightingInfos.w = this._specularIntensity;
  1771. ubo.updateVector4("vLightingIntensity", this._lightingInfos);
  1772. // Colors
  1773. scene.ambientColor.multiplyToRef(this._ambientColor, this._globalAmbientColor);
  1774. ubo.updateColor3("vAmbientColor", this._globalAmbientColor);
  1775. ubo.updateFloat2("vDebugMode", this.debugLimit, this.debugFactor);
  1776. }
  1777. // Textures
  1778. if (scene.texturesEnabled) {
  1779. if (this._albedoTexture && MaterialFlags.DiffuseTextureEnabled) {
  1780. ubo.setTexture("albedoSampler", this._albedoTexture);
  1781. }
  1782. if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
  1783. ubo.setTexture("ambientSampler", this._ambientTexture);
  1784. }
  1785. if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
  1786. ubo.setTexture("opacitySampler", this._opacityTexture);
  1787. }
  1788. if (reflectionTexture && MaterialFlags.ReflectionTextureEnabled) {
  1789. if (defines.LODBASEDMICROSFURACE) {
  1790. ubo.setTexture("reflectionSampler", reflectionTexture);
  1791. }
  1792. else {
  1793. ubo.setTexture("reflectionSampler", reflectionTexture._lodTextureMid || reflectionTexture);
  1794. ubo.setTexture("reflectionSamplerLow", reflectionTexture._lodTextureLow || reflectionTexture);
  1795. ubo.setTexture("reflectionSamplerHigh", reflectionTexture._lodTextureHigh || reflectionTexture);
  1796. }
  1797. if (defines.USEIRRADIANCEMAP) {
  1798. ubo.setTexture("irradianceSampler", reflectionTexture.irradianceTexture);
  1799. }
  1800. }
  1801. if (defines.ENVIRONMENTBRDF) {
  1802. ubo.setTexture("environmentBrdfSampler", this._environmentBRDFTexture);
  1803. }
  1804. if (this._emissiveTexture && MaterialFlags.EmissiveTextureEnabled) {
  1805. ubo.setTexture("emissiveSampler", this._emissiveTexture);
  1806. }
  1807. if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
  1808. ubo.setTexture("lightmapSampler", this._lightmapTexture);
  1809. }
  1810. if (MaterialFlags.SpecularTextureEnabled) {
  1811. if (this._metallicTexture) {
  1812. ubo.setTexture("reflectivitySampler", this._metallicTexture);
  1813. }
  1814. else if (this._reflectivityTexture) {
  1815. ubo.setTexture("reflectivitySampler", this._reflectivityTexture);
  1816. }
  1817. if (this._metallicReflectanceTexture) {
  1818. ubo.setTexture("metallicReflectanceSampler", this._metallicReflectanceTexture);
  1819. }
  1820. if (this._reflectanceTexture && defines.REFLECTANCE) {
  1821. ubo.setTexture("reflectanceSampler", this._reflectanceTexture);
  1822. }
  1823. if (this._microSurfaceTexture) {
  1824. ubo.setTexture("microSurfaceSampler", this._microSurfaceTexture);
  1825. }
  1826. }
  1827. if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
  1828. ubo.setTexture("bumpSampler", this._bumpTexture);
  1829. }
  1830. }
  1831. // OIT with depth peeling
  1832. if (this.getScene().useOrderIndependentTransparency && this.needAlphaBlendingForMesh(mesh)) {
  1833. this.getScene().depthPeelingRenderer.bind(effect);
  1834. }
  1835. this._eventInfo.subMesh = subMesh;
  1836. this._callbackPluginEventBindForSubMesh(this._eventInfo);
  1837. // Clip plane
  1838. bindClipPlane(this._activeEffect, this, scene);
  1839. this.bindEyePosition(effect);
  1840. }
  1841. else if (scene.getEngine()._features.needToAlwaysBindUniformBuffers) {
  1842. this._needToBindSceneUbo = true;
  1843. }
  1844. if (mustRebind || !this.isFrozen) {
  1845. // Lights
  1846. if (scene.lightsEnabled && !this._disableLighting) {
  1847. BindLights(scene, mesh, this._activeEffect, defines, this._maxSimultaneousLights);
  1848. }
  1849. // View
  1850. if ((scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) ||
  1851. reflectionTexture ||
  1852. this.subSurface.refractionTexture ||
  1853. mesh.receiveShadows ||
  1854. defines.PREPASS) {
  1855. this.bindView(effect);
  1856. }
  1857. // Fog
  1858. BindFogParameters(scene, mesh, this._activeEffect, true);
  1859. // Morph targets
  1860. if (defines.NUM_MORPH_INFLUENCERS) {
  1861. BindMorphTargetParameters(mesh, this._activeEffect);
  1862. }
  1863. if (defines.BAKED_VERTEX_ANIMATION_TEXTURE) {
  1864. mesh.bakedVertexAnimationManager?.bind(effect, defines.INSTANCES);
  1865. }
  1866. // image processing
  1867. this._imageProcessingConfiguration.bind(this._activeEffect);
  1868. // Log. depth
  1869. BindLogDepth(defines, this._activeEffect, scene);
  1870. }
  1871. this._afterBind(mesh, this._activeEffect, subMesh);
  1872. ubo.update();
  1873. }
  1874. /**
  1875. * Returns the animatable textures.
  1876. * If material have animatable metallic texture, then reflectivity texture will not be returned, even if it has animations.
  1877. * @returns - Array of animatable textures.
  1878. */
  1879. getAnimatables() {
  1880. const results = super.getAnimatables();
  1881. if (this._albedoTexture && this._albedoTexture.animations && this._albedoTexture.animations.length > 0) {
  1882. results.push(this._albedoTexture);
  1883. }
  1884. if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
  1885. results.push(this._ambientTexture);
  1886. }
  1887. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  1888. results.push(this._opacityTexture);
  1889. }
  1890. if (this._reflectionTexture && this._reflectionTexture.animations && this._reflectionTexture.animations.length > 0) {
  1891. results.push(this._reflectionTexture);
  1892. }
  1893. if (this._emissiveTexture && this._emissiveTexture.animations && this._emissiveTexture.animations.length > 0) {
  1894. results.push(this._emissiveTexture);
  1895. }
  1896. if (this._metallicTexture && this._metallicTexture.animations && this._metallicTexture.animations.length > 0) {
  1897. results.push(this._metallicTexture);
  1898. }
  1899. else if (this._reflectivityTexture && this._reflectivityTexture.animations && this._reflectivityTexture.animations.length > 0) {
  1900. results.push(this._reflectivityTexture);
  1901. }
  1902. if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
  1903. results.push(this._bumpTexture);
  1904. }
  1905. if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
  1906. results.push(this._lightmapTexture);
  1907. }
  1908. if (this._metallicReflectanceTexture && this._metallicReflectanceTexture.animations && this._metallicReflectanceTexture.animations.length > 0) {
  1909. results.push(this._metallicReflectanceTexture);
  1910. }
  1911. if (this._reflectanceTexture && this._reflectanceTexture.animations && this._reflectanceTexture.animations.length > 0) {
  1912. results.push(this._reflectanceTexture);
  1913. }
  1914. if (this._microSurfaceTexture && this._microSurfaceTexture.animations && this._microSurfaceTexture.animations.length > 0) {
  1915. results.push(this._microSurfaceTexture);
  1916. }
  1917. return results;
  1918. }
  1919. /**
  1920. * Returns the texture used for reflections.
  1921. * @returns - Reflection texture if present. Otherwise, returns the environment texture.
  1922. */
  1923. _getReflectionTexture() {
  1924. if (this._reflectionTexture) {
  1925. return this._reflectionTexture;
  1926. }
  1927. return this.getScene().environmentTexture;
  1928. }
  1929. /**
  1930. * Returns an array of the actively used textures.
  1931. * @returns - Array of BaseTextures
  1932. */
  1933. getActiveTextures() {
  1934. const activeTextures = super.getActiveTextures();
  1935. if (this._albedoTexture) {
  1936. activeTextures.push(this._albedoTexture);
  1937. }
  1938. if (this._ambientTexture) {
  1939. activeTextures.push(this._ambientTexture);
  1940. }
  1941. if (this._opacityTexture) {
  1942. activeTextures.push(this._opacityTexture);
  1943. }
  1944. if (this._reflectionTexture) {
  1945. activeTextures.push(this._reflectionTexture);
  1946. }
  1947. if (this._emissiveTexture) {
  1948. activeTextures.push(this._emissiveTexture);
  1949. }
  1950. if (this._reflectivityTexture) {
  1951. activeTextures.push(this._reflectivityTexture);
  1952. }
  1953. if (this._metallicTexture) {
  1954. activeTextures.push(this._metallicTexture);
  1955. }
  1956. if (this._metallicReflectanceTexture) {
  1957. activeTextures.push(this._metallicReflectanceTexture);
  1958. }
  1959. if (this._reflectanceTexture) {
  1960. activeTextures.push(this._reflectanceTexture);
  1961. }
  1962. if (this._microSurfaceTexture) {
  1963. activeTextures.push(this._microSurfaceTexture);
  1964. }
  1965. if (this._bumpTexture) {
  1966. activeTextures.push(this._bumpTexture);
  1967. }
  1968. if (this._lightmapTexture) {
  1969. activeTextures.push(this._lightmapTexture);
  1970. }
  1971. return activeTextures;
  1972. }
  1973. /**
  1974. * Checks to see if a texture is used in the material.
  1975. * @param texture - Base texture to use.
  1976. * @returns - Boolean specifying if a texture is used in the material.
  1977. */
  1978. hasTexture(texture) {
  1979. if (super.hasTexture(texture)) {
  1980. return true;
  1981. }
  1982. if (this._albedoTexture === texture) {
  1983. return true;
  1984. }
  1985. if (this._ambientTexture === texture) {
  1986. return true;
  1987. }
  1988. if (this._opacityTexture === texture) {
  1989. return true;
  1990. }
  1991. if (this._reflectionTexture === texture) {
  1992. return true;
  1993. }
  1994. if (this._emissiveTexture === texture) {
  1995. return true;
  1996. }
  1997. if (this._reflectivityTexture === texture) {
  1998. return true;
  1999. }
  2000. if (this._metallicTexture === texture) {
  2001. return true;
  2002. }
  2003. if (this._metallicReflectanceTexture === texture) {
  2004. return true;
  2005. }
  2006. if (this._reflectanceTexture === texture) {
  2007. return true;
  2008. }
  2009. if (this._microSurfaceTexture === texture) {
  2010. return true;
  2011. }
  2012. if (this._bumpTexture === texture) {
  2013. return true;
  2014. }
  2015. if (this._lightmapTexture === texture) {
  2016. return true;
  2017. }
  2018. return false;
  2019. }
  2020. /**
  2021. * Sets the required values to the prepass renderer.
  2022. * It can't be sets when subsurface scattering of this material is disabled.
  2023. * When scene have ability to enable subsurface prepass effect, it will enable.
  2024. * @returns - If prepass is enabled or not.
  2025. */
  2026. setPrePassRenderer() {
  2027. if (!this.subSurface?.isScatteringEnabled) {
  2028. return false;
  2029. }
  2030. const subSurfaceConfiguration = this.getScene().enableSubSurfaceForPrePass();
  2031. if (subSurfaceConfiguration) {
  2032. subSurfaceConfiguration.enabled = true;
  2033. }
  2034. return true;
  2035. }
  2036. /**
  2037. * Disposes the resources of the material.
  2038. * @param forceDisposeEffect - Forces the disposal of effects.
  2039. * @param forceDisposeTextures - Forces the disposal of all textures.
  2040. */
  2041. dispose(forceDisposeEffect, forceDisposeTextures) {
  2042. if (forceDisposeTextures) {
  2043. if (this._environmentBRDFTexture && this.getScene().environmentBRDFTexture !== this._environmentBRDFTexture) {
  2044. this._environmentBRDFTexture.dispose();
  2045. }
  2046. this._albedoTexture?.dispose();
  2047. this._ambientTexture?.dispose();
  2048. this._opacityTexture?.dispose();
  2049. this._reflectionTexture?.dispose();
  2050. this._emissiveTexture?.dispose();
  2051. this._metallicTexture?.dispose();
  2052. this._reflectivityTexture?.dispose();
  2053. this._bumpTexture?.dispose();
  2054. this._lightmapTexture?.dispose();
  2055. this._metallicReflectanceTexture?.dispose();
  2056. this._reflectanceTexture?.dispose();
  2057. this._microSurfaceTexture?.dispose();
  2058. }
  2059. this._renderTargets.dispose();
  2060. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  2061. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  2062. }
  2063. super.dispose(forceDisposeEffect, forceDisposeTextures);
  2064. }
  2065. }
  2066. /**
  2067. * PBRMaterialTransparencyMode: No transparency mode, Alpha channel is not use.
  2068. */
  2069. PBRBaseMaterial.PBRMATERIAL_OPAQUE = Material.MATERIAL_OPAQUE;
  2070. /**
  2071. * PBRMaterialTransparencyMode: Alpha Test mode, pixel are discarded below a certain threshold defined by the alpha cutoff value.
  2072. */
  2073. PBRBaseMaterial.PBRMATERIAL_ALPHATEST = Material.MATERIAL_ALPHATEST;
  2074. /**
  2075. * PBRMaterialTransparencyMode: Pixels are blended (according to the alpha mode) with the already drawn pixels in the current frame buffer.
  2076. */
  2077. PBRBaseMaterial.PBRMATERIAL_ALPHABLEND = Material.MATERIAL_ALPHABLEND;
  2078. /**
  2079. * PBRMaterialTransparencyMode: Pixels are blended (according to the alpha mode) with the already drawn pixels in the current frame buffer.
  2080. * They are also discarded below the alpha cutoff threshold to improve performances.
  2081. */
  2082. PBRBaseMaterial.PBRMATERIAL_ALPHATESTANDBLEND = Material.MATERIAL_ALPHATESTANDBLEND;
  2083. /**
  2084. * Defines the default value of how much AO map is occluding the analytical lights
  2085. * (point spot...).
  2086. */
  2087. PBRBaseMaterial.DEFAULT_AO_ON_ANALYTICAL_LIGHTS = 0;
  2088. /**
  2089. * PBRMaterialLightFalloff Physical: light is falling off following the inverse squared distance law.
  2090. */
  2091. PBRBaseMaterial.LIGHTFALLOFF_PHYSICAL = 0;
  2092. /**
  2093. * PBRMaterialLightFalloff gltf: light is falling off as described in the gltf moving to PBR document
  2094. * to enhance interoperability with other engines.
  2095. */
  2096. PBRBaseMaterial.LIGHTFALLOFF_GLTF = 1;
  2097. /**
  2098. * PBRMaterialLightFalloff Standard: light is falling off like in the standard material
  2099. * to enhance interoperability with other materials.
  2100. */
  2101. PBRBaseMaterial.LIGHTFALLOFF_STANDARD = 2;
  2102. __decorate([
  2103. serializeAsImageProcessingConfiguration()
  2104. ], PBRBaseMaterial.prototype, "_imageProcessingConfiguration", void 0);
  2105. __decorate([
  2106. expandToProperty("_markAllSubMeshesAsMiscDirty")
  2107. ], PBRBaseMaterial.prototype, "debugMode", void 0);
  2108. //# sourceMappingURL=pbrBaseMaterial.js.map