standardMaterial.js 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. import { __decorate } from "../tslib.es6.js";
  2. /* eslint-disable @typescript-eslint/naming-convention */
  3. import { serialize, serializeAsColor3, expandToProperty, serializeAsFresnelParameters, serializeAsTexture } from "../Misc/decorators.js";
  4. import { SmartArray } from "../Misc/smartArray.js";
  5. import { Scene } from "../scene.js";
  6. import { Matrix } from "../Maths/math.vector.js";
  7. import { Color3 } from "../Maths/math.color.js";
  8. import { VertexBuffer } from "../Buffers/buffer.js";
  9. import { PrePassConfiguration } from "./prePassConfiguration.js";
  10. import { ImageProcessingConfiguration } from "./imageProcessingConfiguration.js";
  11. import { Material } from "../Materials/material.js";
  12. import { MaterialPluginEvent } from "./materialPluginEvent.js";
  13. import { MaterialDefines } from "../Materials/materialDefines.js";
  14. import { PushMaterial } from "./pushMaterial.js";
  15. import { Texture } from "../Materials/Textures/texture.js";
  16. import { RegisterClass } from "../Misc/typeStore.js";
  17. import { MaterialFlags } from "./materialFlags.js";
  18. import "../Shaders/default.fragment.js";
  19. import "../Shaders/default.vertex.js";
  20. import { EffectFallbacks } from "./effectFallbacks.js";
  21. import { DetailMapConfiguration } from "./material.detailMapConfiguration.js";
  22. import { addClipPlaneUniforms, bindClipPlane } from "./clipPlaneMaterialHelper.js";
  23. 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";
  24. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  25. const onCreatedEffectParameters = { effect: null, subMesh: null };
  26. /** @internal */
  27. export class StandardMaterialDefines extends MaterialDefines {
  28. /**
  29. * Initializes the Standard Material defines.
  30. * @param externalProperties The external properties
  31. */
  32. constructor(externalProperties) {
  33. super(externalProperties);
  34. this.MAINUV1 = false;
  35. this.MAINUV2 = false;
  36. this.MAINUV3 = false;
  37. this.MAINUV4 = false;
  38. this.MAINUV5 = false;
  39. this.MAINUV6 = false;
  40. this.DIFFUSE = false;
  41. this.DIFFUSEDIRECTUV = 0;
  42. this.BAKED_VERTEX_ANIMATION_TEXTURE = false;
  43. this.AMBIENT = false;
  44. this.AMBIENTDIRECTUV = 0;
  45. this.OPACITY = false;
  46. this.OPACITYDIRECTUV = 0;
  47. this.OPACITYRGB = false;
  48. this.REFLECTION = false;
  49. this.EMISSIVE = false;
  50. this.EMISSIVEDIRECTUV = 0;
  51. this.SPECULAR = false;
  52. this.SPECULARDIRECTUV = 0;
  53. this.BUMP = false;
  54. this.BUMPDIRECTUV = 0;
  55. this.PARALLAX = false;
  56. this.PARALLAX_RHS = false;
  57. this.PARALLAXOCCLUSION = false;
  58. this.SPECULAROVERALPHA = false;
  59. this.CLIPPLANE = false;
  60. this.CLIPPLANE2 = false;
  61. this.CLIPPLANE3 = false;
  62. this.CLIPPLANE4 = false;
  63. this.CLIPPLANE5 = false;
  64. this.CLIPPLANE6 = false;
  65. this.ALPHATEST = false;
  66. this.DEPTHPREPASS = false;
  67. this.ALPHAFROMDIFFUSE = false;
  68. this.POINTSIZE = false;
  69. this.FOG = false;
  70. this.SPECULARTERM = false;
  71. this.DIFFUSEFRESNEL = false;
  72. this.OPACITYFRESNEL = false;
  73. this.REFLECTIONFRESNEL = false;
  74. this.REFRACTIONFRESNEL = false;
  75. this.EMISSIVEFRESNEL = false;
  76. this.FRESNEL = false;
  77. this.NORMAL = false;
  78. this.TANGENT = false;
  79. this.UV1 = false;
  80. this.UV2 = false;
  81. this.UV3 = false;
  82. this.UV4 = false;
  83. this.UV5 = false;
  84. this.UV6 = false;
  85. this.VERTEXCOLOR = false;
  86. this.VERTEXALPHA = false;
  87. this.NUM_BONE_INFLUENCERS = 0;
  88. this.BonesPerMesh = 0;
  89. this.BONETEXTURE = false;
  90. this.BONES_VELOCITY_ENABLED = false;
  91. this.INSTANCES = false;
  92. this.THIN_INSTANCES = false;
  93. this.INSTANCESCOLOR = false;
  94. this.GLOSSINESS = false;
  95. this.ROUGHNESS = false;
  96. this.EMISSIVEASILLUMINATION = false;
  97. this.LINKEMISSIVEWITHDIFFUSE = false;
  98. this.REFLECTIONFRESNELFROMSPECULAR = false;
  99. this.LIGHTMAP = false;
  100. this.LIGHTMAPDIRECTUV = 0;
  101. this.OBJECTSPACE_NORMALMAP = false;
  102. this.USELIGHTMAPASSHADOWMAP = false;
  103. this.REFLECTIONMAP_3D = false;
  104. this.REFLECTIONMAP_SPHERICAL = false;
  105. this.REFLECTIONMAP_PLANAR = false;
  106. this.REFLECTIONMAP_CUBIC = false;
  107. this.USE_LOCAL_REFLECTIONMAP_CUBIC = false;
  108. this.USE_LOCAL_REFRACTIONMAP_CUBIC = false;
  109. this.REFLECTIONMAP_PROJECTION = false;
  110. this.REFLECTIONMAP_SKYBOX = false;
  111. this.REFLECTIONMAP_EXPLICIT = false;
  112. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  113. this.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  114. this.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  115. this.REFLECTIONMAP_OPPOSITEZ = false;
  116. this.INVERTCUBICMAP = false;
  117. this.LOGARITHMICDEPTH = false;
  118. this.REFRACTION = false;
  119. this.REFRACTIONMAP_3D = false;
  120. this.REFLECTIONOVERALPHA = false;
  121. this.TWOSIDEDLIGHTING = false;
  122. this.SHADOWFLOAT = false;
  123. this.MORPHTARGETS = false;
  124. this.MORPHTARGETS_NORMAL = false;
  125. this.MORPHTARGETS_TANGENT = false;
  126. this.MORPHTARGETS_UV = false;
  127. this.NUM_MORPH_INFLUENCERS = 0;
  128. this.MORPHTARGETS_TEXTURE = false;
  129. this.NONUNIFORMSCALING = false; // https://playground.babylonjs.com#V6DWIH
  130. this.PREMULTIPLYALPHA = false; // https://playground.babylonjs.com#LNVJJ7
  131. this.ALPHATEST_AFTERALLALPHACOMPUTATIONS = false;
  132. this.ALPHABLEND = true;
  133. this.PREPASS = false;
  134. this.PREPASS_IRRADIANCE = false;
  135. this.PREPASS_IRRADIANCE_INDEX = -1;
  136. this.PREPASS_ALBEDO_SQRT = false;
  137. this.PREPASS_ALBEDO_SQRT_INDEX = -1;
  138. this.PREPASS_DEPTH = false;
  139. this.PREPASS_DEPTH_INDEX = -1;
  140. this.PREPASS_NORMAL = false;
  141. this.PREPASS_NORMAL_INDEX = -1;
  142. this.PREPASS_NORMAL_WORLDSPACE = false;
  143. this.PREPASS_POSITION = false;
  144. this.PREPASS_POSITION_INDEX = -1;
  145. this.PREPASS_VELOCITY = false;
  146. this.PREPASS_VELOCITY_INDEX = -1;
  147. this.PREPASS_REFLECTIVITY = false;
  148. this.PREPASS_REFLECTIVITY_INDEX = -1;
  149. this.SCENE_MRT_COUNT = 0;
  150. this.RGBDLIGHTMAP = false;
  151. this.RGBDREFLECTION = false;
  152. this.RGBDREFRACTION = false;
  153. this.IMAGEPROCESSING = false;
  154. this.VIGNETTE = false;
  155. this.VIGNETTEBLENDMODEMULTIPLY = false;
  156. this.VIGNETTEBLENDMODEOPAQUE = false;
  157. this.TONEMAPPING = false;
  158. this.TONEMAPPING_ACES = false;
  159. this.CONTRAST = false;
  160. this.COLORCURVES = false;
  161. this.COLORGRADING = false;
  162. this.COLORGRADING3D = false;
  163. this.SAMPLER3DGREENDEPTH = false;
  164. this.SAMPLER3DBGRMAP = false;
  165. this.DITHER = false;
  166. this.IMAGEPROCESSINGPOSTPROCESS = false;
  167. this.SKIPFINALCOLORCLAMP = false;
  168. this.MULTIVIEW = false;
  169. this.ORDER_INDEPENDENT_TRANSPARENCY = false;
  170. this.ORDER_INDEPENDENT_TRANSPARENCY_16BITS = false;
  171. this.CAMERA_ORTHOGRAPHIC = false;
  172. this.CAMERA_PERSPECTIVE = false;
  173. /**
  174. * If the reflection texture on this material is in linear color space
  175. * @internal
  176. */
  177. this.IS_REFLECTION_LINEAR = false;
  178. /**
  179. * If the refraction texture on this material is in linear color space
  180. * @internal
  181. */
  182. this.IS_REFRACTION_LINEAR = false;
  183. this.EXPOSURE = false;
  184. this.DECAL_AFTER_DETAIL = false;
  185. this.rebuild();
  186. }
  187. setReflectionMode(modeToEnable) {
  188. const modes = [
  189. "REFLECTIONMAP_CUBIC",
  190. "REFLECTIONMAP_EXPLICIT",
  191. "REFLECTIONMAP_PLANAR",
  192. "REFLECTIONMAP_PROJECTION",
  193. "REFLECTIONMAP_PROJECTION",
  194. "REFLECTIONMAP_SKYBOX",
  195. "REFLECTIONMAP_SPHERICAL",
  196. "REFLECTIONMAP_EQUIRECTANGULAR",
  197. "REFLECTIONMAP_EQUIRECTANGULAR_FIXED",
  198. "REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED",
  199. ];
  200. for (const mode of modes) {
  201. this[mode] = mode === modeToEnable;
  202. }
  203. }
  204. }
  205. /**
  206. * This is the default material used in Babylon. It is the best trade off between quality
  207. * and performances.
  208. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materials_introduction
  209. */
  210. export class StandardMaterial extends PushMaterial {
  211. /**
  212. * Gets the image processing configuration used either in this material.
  213. */
  214. get imageProcessingConfiguration() {
  215. return this._imageProcessingConfiguration;
  216. }
  217. /**
  218. * Sets the Default image processing configuration used either in the this material.
  219. *
  220. * If sets to null, the scene one is in use.
  221. */
  222. set imageProcessingConfiguration(value) {
  223. this._attachImageProcessingConfiguration(value);
  224. // Ensure the effect will be rebuilt.
  225. this._markAllSubMeshesAsTexturesDirty();
  226. }
  227. /**
  228. * Attaches a new image processing configuration to the Standard Material.
  229. * @param configuration
  230. */
  231. _attachImageProcessingConfiguration(configuration) {
  232. if (configuration === this._imageProcessingConfiguration) {
  233. return;
  234. }
  235. // Detaches observer
  236. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  237. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  238. }
  239. // Pick the scene configuration if needed
  240. if (!configuration) {
  241. this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
  242. }
  243. else {
  244. this._imageProcessingConfiguration = configuration;
  245. }
  246. // Attaches observer
  247. if (this._imageProcessingConfiguration) {
  248. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(() => {
  249. this._markAllSubMeshesAsImageProcessingDirty();
  250. });
  251. }
  252. }
  253. /**
  254. * Can this material render to prepass
  255. */
  256. get isPrePassCapable() {
  257. return !this.disableDepthWrite;
  258. }
  259. /**
  260. * Gets whether the color curves effect is enabled.
  261. */
  262. get cameraColorCurvesEnabled() {
  263. return this.imageProcessingConfiguration.colorCurvesEnabled;
  264. }
  265. /**
  266. * Sets whether the color curves effect is enabled.
  267. */
  268. set cameraColorCurvesEnabled(value) {
  269. this.imageProcessingConfiguration.colorCurvesEnabled = value;
  270. }
  271. /**
  272. * Gets whether the color grading effect is enabled.
  273. */
  274. get cameraColorGradingEnabled() {
  275. return this.imageProcessingConfiguration.colorGradingEnabled;
  276. }
  277. /**
  278. * Gets whether the color grading effect is enabled.
  279. */
  280. set cameraColorGradingEnabled(value) {
  281. this.imageProcessingConfiguration.colorGradingEnabled = value;
  282. }
  283. /**
  284. * Gets whether tonemapping is enabled or not.
  285. */
  286. get cameraToneMappingEnabled() {
  287. return this._imageProcessingConfiguration.toneMappingEnabled;
  288. }
  289. /**
  290. * Sets whether tonemapping is enabled or not
  291. */
  292. set cameraToneMappingEnabled(value) {
  293. this._imageProcessingConfiguration.toneMappingEnabled = value;
  294. }
  295. /**
  296. * The camera exposure used on this material.
  297. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  298. * This corresponds to a photographic exposure.
  299. */
  300. get cameraExposure() {
  301. return this._imageProcessingConfiguration.exposure;
  302. }
  303. /**
  304. * The camera exposure used on this material.
  305. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  306. * This corresponds to a photographic exposure.
  307. */
  308. set cameraExposure(value) {
  309. this._imageProcessingConfiguration.exposure = value;
  310. }
  311. /**
  312. * Gets The camera contrast used on this material.
  313. */
  314. get cameraContrast() {
  315. return this._imageProcessingConfiguration.contrast;
  316. }
  317. /**
  318. * Sets The camera contrast used on this material.
  319. */
  320. set cameraContrast(value) {
  321. this._imageProcessingConfiguration.contrast = value;
  322. }
  323. /**
  324. * Gets the Color Grading 2D Lookup Texture.
  325. */
  326. get cameraColorGradingTexture() {
  327. return this._imageProcessingConfiguration.colorGradingTexture;
  328. }
  329. /**
  330. * Sets the Color Grading 2D Lookup Texture.
  331. */
  332. set cameraColorGradingTexture(value) {
  333. this._imageProcessingConfiguration.colorGradingTexture = value;
  334. }
  335. /**
  336. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  337. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  338. * These are similar to controls found in many professional imaging or colorist software. The global controls are applied to the entire image. For advanced tuning, extra controls are provided to adjust the shadow, midtone and highlight areas of the image;
  339. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  340. */
  341. get cameraColorCurves() {
  342. return this._imageProcessingConfiguration.colorCurves;
  343. }
  344. /**
  345. * The color grading curves provide additional color adjustment that is applied after any color grading transform (3D LUT).
  346. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  347. * These are similar to controls found in many professional imaging or colorist software. The global controls are applied to the entire image. For advanced tuning, extra controls are provided to adjust the shadow, midtone and highlight areas of the image;
  348. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  349. */
  350. set cameraColorCurves(value) {
  351. this._imageProcessingConfiguration.colorCurves = value;
  352. }
  353. /**
  354. * Can this material render to several textures at once
  355. */
  356. get canRenderToMRT() {
  357. return true;
  358. }
  359. /**
  360. * Instantiates a new standard material.
  361. * This is the default material used in Babylon. It is the best trade off between quality
  362. * and performances.
  363. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materials_introduction
  364. * @param name Define the name of the material in the scene
  365. * @param scene Define the scene the material belong to
  366. */
  367. constructor(name, scene) {
  368. super(name, scene);
  369. this._diffuseTexture = null;
  370. this._ambientTexture = null;
  371. this._opacityTexture = null;
  372. this._reflectionTexture = null;
  373. this._emissiveTexture = null;
  374. this._specularTexture = null;
  375. this._bumpTexture = null;
  376. this._lightmapTexture = null;
  377. this._refractionTexture = null;
  378. /**
  379. * The color of the material lit by the environmental background lighting.
  380. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materials_introduction#ambient-color-example
  381. */
  382. this.ambientColor = new Color3(0, 0, 0);
  383. /**
  384. * The basic color of the material as viewed under a light.
  385. */
  386. this.diffuseColor = new Color3(1, 1, 1);
  387. /**
  388. * Define how the color and intensity of the highlight given by the light in the material.
  389. */
  390. this.specularColor = new Color3(1, 1, 1);
  391. /**
  392. * Define the color of the material as if self lit.
  393. * This will be mixed in the final result even in the absence of light.
  394. */
  395. this.emissiveColor = new Color3(0, 0, 0);
  396. /**
  397. * Defines how sharp are the highlights in the material.
  398. * The bigger the value the sharper giving a more glossy feeling to the result.
  399. * Reversely, the smaller the value the blurrier giving a more rough feeling to the result.
  400. */
  401. this.specularPower = 64;
  402. this._useAlphaFromDiffuseTexture = false;
  403. this._useEmissiveAsIllumination = false;
  404. this._linkEmissiveWithDiffuse = false;
  405. this._useSpecularOverAlpha = false;
  406. this._useReflectionOverAlpha = false;
  407. this._disableLighting = false;
  408. this._useObjectSpaceNormalMap = false;
  409. this._useParallax = false;
  410. this._useParallaxOcclusion = false;
  411. /**
  412. * Apply a scaling factor that determine which "depth" the height map should reprensent. A value between 0.05 and 0.1 is reasonnable in Parallax, you can reach 0.2 using Parallax Occlusion.
  413. */
  414. this.parallaxScaleBias = 0.05;
  415. this._roughness = 0;
  416. /**
  417. * In case of refraction, define the value of the index of refraction.
  418. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#how-to-obtain-reflections-and-refractions
  419. */
  420. this.indexOfRefraction = 0.98;
  421. /**
  422. * Invert the refraction texture alongside the y axis.
  423. * It can be useful with procedural textures or probe for instance.
  424. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#how-to-obtain-reflections-and-refractions
  425. */
  426. this.invertRefractionY = true;
  427. /**
  428. * Defines the alpha limits in alpha test mode.
  429. */
  430. this.alphaCutOff = 0.4;
  431. this._useLightmapAsShadowmap = false;
  432. this._useReflectionFresnelFromSpecular = false;
  433. this._useGlossinessFromSpecularMapAlpha = false;
  434. this._maxSimultaneousLights = 4;
  435. this._invertNormalMapX = false;
  436. this._invertNormalMapY = false;
  437. this._twoSidedLighting = false;
  438. this._applyDecalMapAfterDetailMap = false;
  439. this._renderTargets = new SmartArray(16);
  440. this._worldViewProjectionMatrix = Matrix.Zero();
  441. this._globalAmbientColor = new Color3(0, 0, 0);
  442. this._cacheHasRenderTargetTextures = false;
  443. this.detailMap = new DetailMapConfiguration(this);
  444. // Setup the default processing configuration to the scene.
  445. this._attachImageProcessingConfiguration(null);
  446. this.prePassConfiguration = new PrePassConfiguration();
  447. this.getRenderTargetTextures = () => {
  448. this._renderTargets.reset();
  449. if (StandardMaterial.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  450. this._renderTargets.push(this._reflectionTexture);
  451. }
  452. if (StandardMaterial.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  453. this._renderTargets.push(this._refractionTexture);
  454. }
  455. this._eventInfo.renderTargets = this._renderTargets;
  456. this._callbackPluginEventFillRenderTargetTextures(this._eventInfo);
  457. return this._renderTargets;
  458. };
  459. }
  460. /**
  461. * Gets a boolean indicating that current material needs to register RTT
  462. */
  463. get hasRenderTargetTextures() {
  464. if (StandardMaterial.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  465. return true;
  466. }
  467. if (StandardMaterial.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  468. return true;
  469. }
  470. return this._cacheHasRenderTargetTextures;
  471. }
  472. /**
  473. * Gets the current class name of the material e.g. "StandardMaterial"
  474. * Mainly use in serialization.
  475. * @returns the class name
  476. */
  477. getClassName() {
  478. return "StandardMaterial";
  479. }
  480. /**
  481. * Specifies if the material will require alpha blending
  482. * @returns a boolean specifying if alpha blending is needed
  483. */
  484. needAlphaBlending() {
  485. if (this._disableAlphaBlending) {
  486. return false;
  487. }
  488. return (this.alpha < 1.0 ||
  489. this._opacityTexture != null ||
  490. this._shouldUseAlphaFromDiffuseTexture() ||
  491. (this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled));
  492. }
  493. /**
  494. * Specifies if this material should be rendered in alpha test mode
  495. * @returns a boolean specifying if an alpha test is needed.
  496. */
  497. needAlphaTesting() {
  498. if (this._forceAlphaTest) {
  499. return true;
  500. }
  501. return this._hasAlphaChannel() && (this._transparencyMode == null || this._transparencyMode === Material.MATERIAL_ALPHATEST);
  502. }
  503. /**
  504. * @returns whether or not the alpha value of the diffuse texture should be used for alpha blending.
  505. */
  506. _shouldUseAlphaFromDiffuseTexture() {
  507. return this._diffuseTexture != null && this._diffuseTexture.hasAlpha && this._useAlphaFromDiffuseTexture && this._transparencyMode !== Material.MATERIAL_OPAQUE;
  508. }
  509. /**
  510. * @returns whether or not there is a usable alpha channel for transparency.
  511. */
  512. _hasAlphaChannel() {
  513. return (this._diffuseTexture != null && this._diffuseTexture.hasAlpha) || this._opacityTexture != null;
  514. }
  515. /**
  516. * Get the texture used for alpha test purpose.
  517. * @returns the diffuse texture in case of the standard material.
  518. */
  519. getAlphaTestTexture() {
  520. return this._diffuseTexture;
  521. }
  522. /**
  523. * Get if the submesh is ready to be used and all its information available.
  524. * Child classes can use it to update shaders
  525. * @param mesh defines the mesh to check
  526. * @param subMesh defines which submesh to check
  527. * @param useInstances specifies that instances should be used
  528. * @returns a boolean indicating that the submesh is ready or not
  529. */
  530. isReadyForSubMesh(mesh, subMesh, useInstances = false) {
  531. if (!this._uniformBufferLayoutBuilt) {
  532. this.buildUniformLayout();
  533. }
  534. const drawWrapper = subMesh._drawWrapper;
  535. if (drawWrapper.effect && this.isFrozen) {
  536. if (drawWrapper._wasPreviouslyReady && drawWrapper._wasPreviouslyUsingInstances === useInstances) {
  537. return true;
  538. }
  539. }
  540. if (!subMesh.materialDefines) {
  541. this._callbackPluginEventGeneric(MaterialPluginEvent.GetDefineNames, this._eventInfo);
  542. subMesh.materialDefines = new StandardMaterialDefines(this._eventInfo.defineNames);
  543. }
  544. const scene = this.getScene();
  545. const defines = subMesh.materialDefines;
  546. if (this._isReadyForSubMesh(subMesh)) {
  547. return true;
  548. }
  549. const engine = scene.getEngine();
  550. // Lights
  551. defines._needNormals = PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
  552. // Multiview
  553. PrepareDefinesForMultiview(scene, defines);
  554. // PrePass
  555. const oit = this.needAlphaBlendingForMesh(mesh) && this.getScene().useOrderIndependentTransparency;
  556. PrepareDefinesForPrePass(scene, defines, this.canRenderToMRT && !oit);
  557. // Order independant transparency
  558. PrepareDefinesForOIT(scene, defines, oit);
  559. // Textures
  560. if (defines._areTexturesDirty) {
  561. this._eventInfo.hasRenderTargetTextures = false;
  562. this._callbackPluginEventHasRenderTargetTextures(this._eventInfo);
  563. this._cacheHasRenderTargetTextures = this._eventInfo.hasRenderTargetTextures;
  564. defines._needUVs = false;
  565. for (let i = 1; i <= 6; ++i) {
  566. defines["MAINUV" + i] = false;
  567. }
  568. if (scene.texturesEnabled) {
  569. defines.DIFFUSEDIRECTUV = 0;
  570. defines.BUMPDIRECTUV = 0;
  571. defines.AMBIENTDIRECTUV = 0;
  572. defines.OPACITYDIRECTUV = 0;
  573. defines.EMISSIVEDIRECTUV = 0;
  574. defines.SPECULARDIRECTUV = 0;
  575. defines.LIGHTMAPDIRECTUV = 0;
  576. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  577. if (!this._diffuseTexture.isReadyOrNotBlocking()) {
  578. return false;
  579. }
  580. else {
  581. PrepareDefinesForMergedUV(this._diffuseTexture, defines, "DIFFUSE");
  582. }
  583. }
  584. else {
  585. defines.DIFFUSE = false;
  586. }
  587. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  588. if (!this._ambientTexture.isReadyOrNotBlocking()) {
  589. return false;
  590. }
  591. else {
  592. PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
  593. }
  594. }
  595. else {
  596. defines.AMBIENT = false;
  597. }
  598. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  599. if (!this._opacityTexture.isReadyOrNotBlocking()) {
  600. return false;
  601. }
  602. else {
  603. PrepareDefinesForMergedUV(this._opacityTexture, defines, "OPACITY");
  604. defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
  605. }
  606. }
  607. else {
  608. defines.OPACITY = false;
  609. }
  610. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  611. if (!this._reflectionTexture.isReadyOrNotBlocking()) {
  612. return false;
  613. }
  614. else {
  615. defines._needNormals = true;
  616. defines.REFLECTION = true;
  617. defines.ROUGHNESS = this._roughness > 0;
  618. defines.REFLECTIONOVERALPHA = this._useReflectionOverAlpha;
  619. defines.INVERTCUBICMAP = this._reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE;
  620. defines.REFLECTIONMAP_3D = this._reflectionTexture.isCube;
  621. defines.REFLECTIONMAP_OPPOSITEZ =
  622. defines.REFLECTIONMAP_3D && this.getScene().useRightHandedSystem ? !this._reflectionTexture.invertZ : this._reflectionTexture.invertZ;
  623. defines.RGBDREFLECTION = this._reflectionTexture.isRGBD;
  624. switch (this._reflectionTexture.coordinatesMode) {
  625. case Texture.EXPLICIT_MODE:
  626. defines.setReflectionMode("REFLECTIONMAP_EXPLICIT");
  627. break;
  628. case Texture.PLANAR_MODE:
  629. defines.setReflectionMode("REFLECTIONMAP_PLANAR");
  630. break;
  631. case Texture.PROJECTION_MODE:
  632. defines.setReflectionMode("REFLECTIONMAP_PROJECTION");
  633. break;
  634. case Texture.SKYBOX_MODE:
  635. defines.setReflectionMode("REFLECTIONMAP_SKYBOX");
  636. break;
  637. case Texture.SPHERICAL_MODE:
  638. defines.setReflectionMode("REFLECTIONMAP_SPHERICAL");
  639. break;
  640. case Texture.EQUIRECTANGULAR_MODE:
  641. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR");
  642. break;
  643. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  644. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR_FIXED");
  645. break;
  646. case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
  647. defines.setReflectionMode("REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED");
  648. break;
  649. case Texture.CUBIC_MODE:
  650. case Texture.INVCUBIC_MODE:
  651. default:
  652. defines.setReflectionMode("REFLECTIONMAP_CUBIC");
  653. break;
  654. }
  655. defines.USE_LOCAL_REFLECTIONMAP_CUBIC = this._reflectionTexture.boundingBoxSize ? true : false;
  656. }
  657. }
  658. else {
  659. defines.REFLECTION = false;
  660. defines.REFLECTIONMAP_OPPOSITEZ = false;
  661. }
  662. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  663. if (!this._emissiveTexture.isReadyOrNotBlocking()) {
  664. return false;
  665. }
  666. else {
  667. PrepareDefinesForMergedUV(this._emissiveTexture, defines, "EMISSIVE");
  668. }
  669. }
  670. else {
  671. defines.EMISSIVE = false;
  672. }
  673. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  674. if (!this._lightmapTexture.isReadyOrNotBlocking()) {
  675. return false;
  676. }
  677. else {
  678. PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
  679. defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
  680. defines.RGBDLIGHTMAP = this._lightmapTexture.isRGBD;
  681. }
  682. }
  683. else {
  684. defines.LIGHTMAP = false;
  685. }
  686. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  687. if (!this._specularTexture.isReadyOrNotBlocking()) {
  688. return false;
  689. }
  690. else {
  691. PrepareDefinesForMergedUV(this._specularTexture, defines, "SPECULAR");
  692. defines.GLOSSINESS = this._useGlossinessFromSpecularMapAlpha;
  693. }
  694. }
  695. else {
  696. defines.SPECULAR = false;
  697. }
  698. if (scene.getEngine().getCaps().standardDerivatives && this._bumpTexture && StandardMaterial.BumpTextureEnabled) {
  699. // Bump texture can not be not blocking.
  700. if (!this._bumpTexture.isReady()) {
  701. return false;
  702. }
  703. else {
  704. PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
  705. defines.PARALLAX = this._useParallax;
  706. defines.PARALLAX_RHS = scene.useRightHandedSystem;
  707. defines.PARALLAXOCCLUSION = this._useParallaxOcclusion;
  708. }
  709. defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
  710. }
  711. else {
  712. defines.BUMP = false;
  713. defines.PARALLAX = false;
  714. defines.PARALLAX_RHS = false;
  715. defines.PARALLAXOCCLUSION = false;
  716. }
  717. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  718. if (!this._refractionTexture.isReadyOrNotBlocking()) {
  719. return false;
  720. }
  721. else {
  722. defines._needUVs = true;
  723. defines.REFRACTION = true;
  724. defines.REFRACTIONMAP_3D = this._refractionTexture.isCube;
  725. defines.RGBDREFRACTION = this._refractionTexture.isRGBD;
  726. defines.USE_LOCAL_REFRACTIONMAP_CUBIC = this._refractionTexture.boundingBoxSize ? true : false;
  727. }
  728. }
  729. else {
  730. defines.REFRACTION = false;
  731. }
  732. defines.TWOSIDEDLIGHTING = !this._backFaceCulling && this._twoSidedLighting;
  733. }
  734. else {
  735. defines.DIFFUSE = false;
  736. defines.AMBIENT = false;
  737. defines.OPACITY = false;
  738. defines.REFLECTION = false;
  739. defines.EMISSIVE = false;
  740. defines.LIGHTMAP = false;
  741. defines.BUMP = false;
  742. defines.REFRACTION = false;
  743. }
  744. defines.ALPHAFROMDIFFUSE = this._shouldUseAlphaFromDiffuseTexture();
  745. defines.EMISSIVEASILLUMINATION = this._useEmissiveAsIllumination;
  746. defines.LINKEMISSIVEWITHDIFFUSE = this._linkEmissiveWithDiffuse;
  747. defines.SPECULAROVERALPHA = this._useSpecularOverAlpha;
  748. defines.PREMULTIPLYALPHA = this.alphaMode === 7 || this.alphaMode === 8;
  749. defines.ALPHATEST_AFTERALLALPHACOMPUTATIONS = this.transparencyMode !== null;
  750. defines.ALPHABLEND = this.transparencyMode === null || this.needAlphaBlendingForMesh(mesh); // check on null for backward compatibility
  751. }
  752. this._eventInfo.isReadyForSubMesh = true;
  753. this._eventInfo.defines = defines;
  754. this._eventInfo.subMesh = subMesh;
  755. this._callbackPluginEventIsReadyForSubMesh(this._eventInfo);
  756. if (!this._eventInfo.isReadyForSubMesh) {
  757. return false;
  758. }
  759. if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
  760. if (!this._imageProcessingConfiguration.isReady()) {
  761. return false;
  762. }
  763. this._imageProcessingConfiguration.prepareDefines(defines);
  764. defines.IS_REFLECTION_LINEAR = this.reflectionTexture != null && !this.reflectionTexture.gammaSpace;
  765. defines.IS_REFRACTION_LINEAR = this.refractionTexture != null && !this.refractionTexture.gammaSpace;
  766. }
  767. if (defines._areFresnelDirty) {
  768. if (StandardMaterial.FresnelEnabled) {
  769. // Fresnel
  770. if (this._diffuseFresnelParameters ||
  771. this._opacityFresnelParameters ||
  772. this._emissiveFresnelParameters ||
  773. this._refractionFresnelParameters ||
  774. this._reflectionFresnelParameters) {
  775. defines.DIFFUSEFRESNEL = this._diffuseFresnelParameters && this._diffuseFresnelParameters.isEnabled;
  776. defines.OPACITYFRESNEL = this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled;
  777. defines.REFLECTIONFRESNEL = this._reflectionFresnelParameters && this._reflectionFresnelParameters.isEnabled;
  778. defines.REFLECTIONFRESNELFROMSPECULAR = this._useReflectionFresnelFromSpecular;
  779. defines.REFRACTIONFRESNEL = this._refractionFresnelParameters && this._refractionFresnelParameters.isEnabled;
  780. defines.EMISSIVEFRESNEL = this._emissiveFresnelParameters && this._emissiveFresnelParameters.isEnabled;
  781. defines._needNormals = true;
  782. defines.FRESNEL = true;
  783. }
  784. }
  785. else {
  786. defines.FRESNEL = false;
  787. }
  788. }
  789. // Misc.
  790. PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh) || this._forceAlphaTest, defines, this._applyDecalMapAfterDetailMap);
  791. // Values that need to be evaluated on every frame
  792. PrepareDefinesForFrameBoundValues(scene, engine, this, defines, useInstances, null, subMesh.getRenderingMesh().hasThinInstances);
  793. // External config
  794. this._eventInfo.defines = defines;
  795. this._eventInfo.mesh = mesh;
  796. this._callbackPluginEventPrepareDefinesBeforeAttributes(this._eventInfo);
  797. // Attribs
  798. PrepareDefinesForAttributes(mesh, defines, true, true, true);
  799. // External config
  800. this._callbackPluginEventPrepareDefines(this._eventInfo);
  801. // Get correct effect
  802. let forceWasNotReadyPreviously = false;
  803. if (defines.isDirty) {
  804. const lightDisposed = defines._areLightsDisposed;
  805. defines.markAsProcessed();
  806. // Fallbacks
  807. const fallbacks = new EffectFallbacks();
  808. if (defines.REFLECTION) {
  809. fallbacks.addFallback(0, "REFLECTION");
  810. }
  811. if (defines.SPECULAR) {
  812. fallbacks.addFallback(0, "SPECULAR");
  813. }
  814. if (defines.BUMP) {
  815. fallbacks.addFallback(0, "BUMP");
  816. }
  817. if (defines.PARALLAX) {
  818. fallbacks.addFallback(1, "PARALLAX");
  819. }
  820. if (defines.PARALLAX_RHS) {
  821. fallbacks.addFallback(1, "PARALLAX_RHS");
  822. }
  823. if (defines.PARALLAXOCCLUSION) {
  824. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  825. }
  826. if (defines.SPECULAROVERALPHA) {
  827. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  828. }
  829. if (defines.FOG) {
  830. fallbacks.addFallback(1, "FOG");
  831. }
  832. if (defines.POINTSIZE) {
  833. fallbacks.addFallback(0, "POINTSIZE");
  834. }
  835. if (defines.LOGARITHMICDEPTH) {
  836. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  837. }
  838. HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights);
  839. if (defines.SPECULARTERM) {
  840. fallbacks.addFallback(0, "SPECULARTERM");
  841. }
  842. if (defines.DIFFUSEFRESNEL) {
  843. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  844. }
  845. if (defines.OPACITYFRESNEL) {
  846. fallbacks.addFallback(2, "OPACITYFRESNEL");
  847. }
  848. if (defines.REFLECTIONFRESNEL) {
  849. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  850. }
  851. if (defines.EMISSIVEFRESNEL) {
  852. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  853. }
  854. if (defines.FRESNEL) {
  855. fallbacks.addFallback(4, "FRESNEL");
  856. }
  857. if (defines.MULTIVIEW) {
  858. fallbacks.addFallback(0, "MULTIVIEW");
  859. }
  860. //Attributes
  861. const attribs = [VertexBuffer.PositionKind];
  862. if (defines.NORMAL) {
  863. attribs.push(VertexBuffer.NormalKind);
  864. }
  865. if (defines.TANGENT) {
  866. attribs.push(VertexBuffer.TangentKind);
  867. }
  868. for (let i = 1; i <= 6; ++i) {
  869. if (defines["UV" + i]) {
  870. attribs.push(`uv${i === 1 ? "" : i}`);
  871. }
  872. }
  873. if (defines.VERTEXCOLOR) {
  874. attribs.push(VertexBuffer.ColorKind);
  875. }
  876. PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  877. PrepareAttributesForInstances(attribs, defines);
  878. PrepareAttributesForMorphTargets(attribs, mesh, defines);
  879. PrepareAttributesForBakedVertexAnimation(attribs, mesh, defines);
  880. let shaderName = "default";
  881. const uniforms = [
  882. "world",
  883. "view",
  884. "viewProjection",
  885. "vEyePosition",
  886. "vLightsType",
  887. "vAmbientColor",
  888. "vDiffuseColor",
  889. "vSpecularColor",
  890. "vEmissiveColor",
  891. "visibility",
  892. "vFogInfos",
  893. "vFogColor",
  894. "pointSize",
  895. "vDiffuseInfos",
  896. "vAmbientInfos",
  897. "vOpacityInfos",
  898. "vReflectionInfos",
  899. "vEmissiveInfos",
  900. "vSpecularInfos",
  901. "vBumpInfos",
  902. "vLightmapInfos",
  903. "vRefractionInfos",
  904. "mBones",
  905. "diffuseMatrix",
  906. "ambientMatrix",
  907. "opacityMatrix",
  908. "reflectionMatrix",
  909. "emissiveMatrix",
  910. "specularMatrix",
  911. "bumpMatrix",
  912. "normalMatrix",
  913. "lightmapMatrix",
  914. "refractionMatrix",
  915. "diffuseLeftColor",
  916. "diffuseRightColor",
  917. "opacityParts",
  918. "reflectionLeftColor",
  919. "reflectionRightColor",
  920. "emissiveLeftColor",
  921. "emissiveRightColor",
  922. "refractionLeftColor",
  923. "refractionRightColor",
  924. "vReflectionPosition",
  925. "vReflectionSize",
  926. "vRefractionPosition",
  927. "vRefractionSize",
  928. "logarithmicDepthConstant",
  929. "vTangentSpaceParams",
  930. "alphaCutOff",
  931. "boneTextureWidth",
  932. "morphTargetTextureInfo",
  933. "morphTargetTextureIndices",
  934. ];
  935. const samplers = [
  936. "diffuseSampler",
  937. "ambientSampler",
  938. "opacitySampler",
  939. "reflectionCubeSampler",
  940. "reflection2DSampler",
  941. "emissiveSampler",
  942. "specularSampler",
  943. "bumpSampler",
  944. "lightmapSampler",
  945. "refractionCubeSampler",
  946. "refraction2DSampler",
  947. "boneSampler",
  948. "morphTargets",
  949. "oitDepthSampler",
  950. "oitFrontColorSampler",
  951. ];
  952. const uniformBuffers = ["Material", "Scene", "Mesh"];
  953. const indexParameters = { maxSimultaneousLights: this._maxSimultaneousLights, maxSimultaneousMorphTargets: defines.NUM_MORPH_INFLUENCERS };
  954. this._eventInfo.fallbacks = fallbacks;
  955. this._eventInfo.fallbackRank = 0;
  956. this._eventInfo.defines = defines;
  957. this._eventInfo.uniforms = uniforms;
  958. this._eventInfo.attributes = attribs;
  959. this._eventInfo.samplers = samplers;
  960. this._eventInfo.uniformBuffersNames = uniformBuffers;
  961. this._eventInfo.customCode = undefined;
  962. this._eventInfo.mesh = mesh;
  963. this._eventInfo.indexParameters = indexParameters;
  964. this._callbackPluginEventGeneric(MaterialPluginEvent.PrepareEffect, this._eventInfo);
  965. PrePassConfiguration.AddUniforms(uniforms);
  966. PrePassConfiguration.AddSamplers(samplers);
  967. if (ImageProcessingConfiguration) {
  968. ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
  969. ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
  970. }
  971. PrepareUniformsAndSamplersList({
  972. uniformsNames: uniforms,
  973. uniformBuffersNames: uniformBuffers,
  974. samplers: samplers,
  975. defines: defines,
  976. maxSimultaneousLights: this._maxSimultaneousLights,
  977. });
  978. addClipPlaneUniforms(uniforms);
  979. const csnrOptions = {};
  980. if (this.customShaderNameResolve) {
  981. shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines, attribs, csnrOptions);
  982. }
  983. const join = defines.toString();
  984. const previousEffect = subMesh.effect;
  985. let effect = scene.getEngine().createEffect(shaderName, {
  986. attributes: attribs,
  987. uniformsNames: uniforms,
  988. uniformBuffersNames: uniformBuffers,
  989. samplers: samplers,
  990. defines: join,
  991. fallbacks: fallbacks,
  992. onCompiled: this.onCompiled,
  993. onError: this.onError,
  994. indexParameters,
  995. processFinalCode: csnrOptions.processFinalCode,
  996. processCodeAfterIncludes: this._eventInfo.customCode,
  997. multiTarget: defines.PREPASS,
  998. }, engine);
  999. this._eventInfo.customCode = undefined;
  1000. if (effect) {
  1001. if (this._onEffectCreatedObservable) {
  1002. onCreatedEffectParameters.effect = effect;
  1003. onCreatedEffectParameters.subMesh = subMesh;
  1004. this._onEffectCreatedObservable.notifyObservers(onCreatedEffectParameters);
  1005. }
  1006. // Use previous effect while new one is compiling
  1007. if (this.allowShaderHotSwapping && previousEffect && !effect.isReady()) {
  1008. effect = previousEffect;
  1009. defines.markAsUnprocessed();
  1010. forceWasNotReadyPreviously = this.isFrozen;
  1011. if (lightDisposed) {
  1012. // re register in case it takes more than one frame.
  1013. defines._areLightsDisposed = true;
  1014. return false;
  1015. }
  1016. }
  1017. else {
  1018. scene.resetCachedMaterial();
  1019. subMesh.setEffect(effect, defines, this._materialContext);
  1020. }
  1021. }
  1022. }
  1023. if (!subMesh.effect || !subMesh.effect.isReady()) {
  1024. return false;
  1025. }
  1026. defines._renderId = scene.getRenderId();
  1027. drawWrapper._wasPreviouslyReady = forceWasNotReadyPreviously ? false : true;
  1028. drawWrapper._wasPreviouslyUsingInstances = useInstances;
  1029. this._checkScenePerformancePriority();
  1030. return true;
  1031. }
  1032. /**
  1033. * Builds the material UBO layouts.
  1034. * Used internally during the effect preparation.
  1035. */
  1036. buildUniformLayout() {
  1037. // Order is important !
  1038. const ubo = this._uniformBuffer;
  1039. ubo.addUniform("diffuseLeftColor", 4);
  1040. ubo.addUniform("diffuseRightColor", 4);
  1041. ubo.addUniform("opacityParts", 4);
  1042. ubo.addUniform("reflectionLeftColor", 4);
  1043. ubo.addUniform("reflectionRightColor", 4);
  1044. ubo.addUniform("refractionLeftColor", 4);
  1045. ubo.addUniform("refractionRightColor", 4);
  1046. ubo.addUniform("emissiveLeftColor", 4);
  1047. ubo.addUniform("emissiveRightColor", 4);
  1048. ubo.addUniform("vDiffuseInfos", 2);
  1049. ubo.addUniform("vAmbientInfos", 2);
  1050. ubo.addUniform("vOpacityInfos", 2);
  1051. ubo.addUniform("vReflectionInfos", 2);
  1052. ubo.addUniform("vReflectionPosition", 3);
  1053. ubo.addUniform("vReflectionSize", 3);
  1054. ubo.addUniform("vEmissiveInfos", 2);
  1055. ubo.addUniform("vLightmapInfos", 2);
  1056. ubo.addUniform("vSpecularInfos", 2);
  1057. ubo.addUniform("vBumpInfos", 3);
  1058. ubo.addUniform("diffuseMatrix", 16);
  1059. ubo.addUniform("ambientMatrix", 16);
  1060. ubo.addUniform("opacityMatrix", 16);
  1061. ubo.addUniform("reflectionMatrix", 16);
  1062. ubo.addUniform("emissiveMatrix", 16);
  1063. ubo.addUniform("lightmapMatrix", 16);
  1064. ubo.addUniform("specularMatrix", 16);
  1065. ubo.addUniform("bumpMatrix", 16);
  1066. ubo.addUniform("vTangentSpaceParams", 2);
  1067. ubo.addUniform("pointSize", 1);
  1068. ubo.addUniform("alphaCutOff", 1);
  1069. ubo.addUniform("refractionMatrix", 16);
  1070. ubo.addUniform("vRefractionInfos", 4);
  1071. ubo.addUniform("vRefractionPosition", 3);
  1072. ubo.addUniform("vRefractionSize", 3);
  1073. ubo.addUniform("vSpecularColor", 4);
  1074. ubo.addUniform("vEmissiveColor", 3);
  1075. ubo.addUniform("vDiffuseColor", 4);
  1076. ubo.addUniform("vAmbientColor", 3);
  1077. super.buildUniformLayout();
  1078. }
  1079. /**
  1080. * Binds the submesh to this material by preparing the effect and shader to draw
  1081. * @param world defines the world transformation matrix
  1082. * @param mesh defines the mesh containing the submesh
  1083. * @param subMesh defines the submesh to bind the material to
  1084. */
  1085. bindForSubMesh(world, mesh, subMesh) {
  1086. const scene = this.getScene();
  1087. const defines = subMesh.materialDefines;
  1088. if (!defines) {
  1089. return;
  1090. }
  1091. const effect = subMesh.effect;
  1092. if (!effect) {
  1093. return;
  1094. }
  1095. this._activeEffect = effect;
  1096. // Matrices Mesh.
  1097. mesh.getMeshUniformBuffer().bindToEffect(effect, "Mesh");
  1098. mesh.transferToEffect(world);
  1099. // Binding unconditionally
  1100. this._uniformBuffer.bindToEffect(effect, "Material");
  1101. this.prePassConfiguration.bindForSubMesh(this._activeEffect, scene, mesh, world, this.isFrozen);
  1102. this._eventInfo.subMesh = subMesh;
  1103. this._callbackPluginEventHardBindForSubMesh(this._eventInfo);
  1104. // Normal Matrix
  1105. if (defines.OBJECTSPACE_NORMALMAP) {
  1106. world.toNormalMatrix(this._normalMatrix);
  1107. this.bindOnlyNormalMatrix(this._normalMatrix);
  1108. }
  1109. const mustRebind = this._mustRebind(scene, effect, subMesh, mesh.visibility);
  1110. // Bones
  1111. BindBonesParameters(mesh, effect);
  1112. const ubo = this._uniformBuffer;
  1113. if (mustRebind) {
  1114. this.bindViewProjection(effect);
  1115. if (!ubo.useUbo || !this.isFrozen || !ubo.isSync || subMesh._drawWrapper._forceRebindOnNextCall) {
  1116. if (StandardMaterial.FresnelEnabled && defines.FRESNEL) {
  1117. // Fresnel
  1118. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  1119. ubo.updateColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  1120. ubo.updateColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  1121. }
  1122. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  1123. ubo.updateColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  1124. }
  1125. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  1126. ubo.updateColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  1127. ubo.updateColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  1128. }
  1129. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  1130. ubo.updateColor4("refractionLeftColor", this.refractionFresnelParameters.leftColor, this.refractionFresnelParameters.power);
  1131. ubo.updateColor4("refractionRightColor", this.refractionFresnelParameters.rightColor, this.refractionFresnelParameters.bias);
  1132. }
  1133. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  1134. ubo.updateColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  1135. ubo.updateColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  1136. }
  1137. }
  1138. // Textures
  1139. if (scene.texturesEnabled) {
  1140. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  1141. ubo.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  1142. BindTextureMatrix(this._diffuseTexture, ubo, "diffuse");
  1143. }
  1144. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  1145. ubo.updateFloat2("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level);
  1146. BindTextureMatrix(this._ambientTexture, ubo, "ambient");
  1147. }
  1148. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  1149. ubo.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
  1150. BindTextureMatrix(this._opacityTexture, ubo, "opacity");
  1151. }
  1152. if (this._hasAlphaChannel()) {
  1153. ubo.updateFloat("alphaCutOff", this.alphaCutOff);
  1154. }
  1155. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  1156. ubo.updateFloat2("vReflectionInfos", this._reflectionTexture.level, this.roughness);
  1157. ubo.updateMatrix("reflectionMatrix", this._reflectionTexture.getReflectionTextureMatrix());
  1158. if (this._reflectionTexture.boundingBoxSize) {
  1159. const cubeTexture = this._reflectionTexture;
  1160. ubo.updateVector3("vReflectionPosition", cubeTexture.boundingBoxPosition);
  1161. ubo.updateVector3("vReflectionSize", cubeTexture.boundingBoxSize);
  1162. }
  1163. }
  1164. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  1165. ubo.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
  1166. BindTextureMatrix(this._emissiveTexture, ubo, "emissive");
  1167. }
  1168. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  1169. ubo.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
  1170. BindTextureMatrix(this._lightmapTexture, ubo, "lightmap");
  1171. }
  1172. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  1173. ubo.updateFloat2("vSpecularInfos", this._specularTexture.coordinatesIndex, this._specularTexture.level);
  1174. BindTextureMatrix(this._specularTexture, ubo, "specular");
  1175. }
  1176. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  1177. ubo.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, 1.0 / this._bumpTexture.level, this.parallaxScaleBias);
  1178. BindTextureMatrix(this._bumpTexture, ubo, "bump");
  1179. if (scene._mirroredCameraPosition) {
  1180. ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? 1.0 : -1.0, this._invertNormalMapY ? 1.0 : -1.0);
  1181. }
  1182. else {
  1183. ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? -1.0 : 1.0, this._invertNormalMapY ? -1.0 : 1.0);
  1184. }
  1185. }
  1186. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  1187. let depth = 1.0;
  1188. if (!this._refractionTexture.isCube) {
  1189. ubo.updateMatrix("refractionMatrix", this._refractionTexture.getReflectionTextureMatrix());
  1190. if (this._refractionTexture.depth) {
  1191. depth = this._refractionTexture.depth;
  1192. }
  1193. }
  1194. ubo.updateFloat4("vRefractionInfos", this._refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  1195. if (this._refractionTexture.boundingBoxSize) {
  1196. const cubeTexture = this._refractionTexture;
  1197. ubo.updateVector3("vRefractionPosition", cubeTexture.boundingBoxPosition);
  1198. ubo.updateVector3("vRefractionSize", cubeTexture.boundingBoxSize);
  1199. }
  1200. }
  1201. }
  1202. // Point size
  1203. if (this.pointsCloud) {
  1204. ubo.updateFloat("pointSize", this.pointSize);
  1205. }
  1206. if (defines.SPECULARTERM) {
  1207. ubo.updateColor4("vSpecularColor", this.specularColor, this.specularPower);
  1208. }
  1209. ubo.updateColor3("vEmissiveColor", StandardMaterial.EmissiveTextureEnabled ? this.emissiveColor : Color3.BlackReadOnly);
  1210. ubo.updateColor4("vDiffuseColor", this.diffuseColor, this.alpha);
  1211. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  1212. ubo.updateColor3("vAmbientColor", this._globalAmbientColor);
  1213. }
  1214. // Textures
  1215. if (scene.texturesEnabled) {
  1216. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  1217. effect.setTexture("diffuseSampler", this._diffuseTexture);
  1218. }
  1219. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  1220. effect.setTexture("ambientSampler", this._ambientTexture);
  1221. }
  1222. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  1223. effect.setTexture("opacitySampler", this._opacityTexture);
  1224. }
  1225. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  1226. if (this._reflectionTexture.isCube) {
  1227. effect.setTexture("reflectionCubeSampler", this._reflectionTexture);
  1228. }
  1229. else {
  1230. effect.setTexture("reflection2DSampler", this._reflectionTexture);
  1231. }
  1232. }
  1233. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  1234. effect.setTexture("emissiveSampler", this._emissiveTexture);
  1235. }
  1236. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  1237. effect.setTexture("lightmapSampler", this._lightmapTexture);
  1238. }
  1239. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  1240. effect.setTexture("specularSampler", this._specularTexture);
  1241. }
  1242. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  1243. effect.setTexture("bumpSampler", this._bumpTexture);
  1244. }
  1245. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  1246. if (this._refractionTexture.isCube) {
  1247. effect.setTexture("refractionCubeSampler", this._refractionTexture);
  1248. }
  1249. else {
  1250. effect.setTexture("refraction2DSampler", this._refractionTexture);
  1251. }
  1252. }
  1253. }
  1254. // OIT with depth peeling
  1255. if (this.getScene().useOrderIndependentTransparency && this.needAlphaBlendingForMesh(mesh)) {
  1256. this.getScene().depthPeelingRenderer.bind(effect);
  1257. }
  1258. this._eventInfo.subMesh = subMesh;
  1259. this._callbackPluginEventBindForSubMesh(this._eventInfo);
  1260. // Clip plane
  1261. bindClipPlane(effect, this, scene);
  1262. // Colors
  1263. this.bindEyePosition(effect);
  1264. }
  1265. else if (scene.getEngine()._features.needToAlwaysBindUniformBuffers) {
  1266. this._needToBindSceneUbo = true;
  1267. }
  1268. if (mustRebind || !this.isFrozen) {
  1269. // Lights
  1270. if (scene.lightsEnabled && !this._disableLighting) {
  1271. BindLights(scene, mesh, effect, defines, this._maxSimultaneousLights);
  1272. }
  1273. // View
  1274. if ((scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) ||
  1275. this._reflectionTexture ||
  1276. this._refractionTexture ||
  1277. mesh.receiveShadows ||
  1278. defines.PREPASS) {
  1279. this.bindView(effect);
  1280. }
  1281. // Fog
  1282. BindFogParameters(scene, mesh, effect);
  1283. // Morph targets
  1284. if (defines.NUM_MORPH_INFLUENCERS) {
  1285. BindMorphTargetParameters(mesh, effect);
  1286. }
  1287. if (defines.BAKED_VERTEX_ANIMATION_TEXTURE) {
  1288. mesh.bakedVertexAnimationManager?.bind(effect, defines.INSTANCES);
  1289. }
  1290. // Log. depth
  1291. if (this.useLogarithmicDepth) {
  1292. BindLogDepth(defines, effect, scene);
  1293. }
  1294. // image processing
  1295. if (this._imageProcessingConfiguration && !this._imageProcessingConfiguration.applyByPostProcess) {
  1296. this._imageProcessingConfiguration.bind(this._activeEffect);
  1297. }
  1298. }
  1299. this._afterBind(mesh, this._activeEffect, subMesh);
  1300. ubo.update();
  1301. }
  1302. /**
  1303. * Get the list of animatables in the material.
  1304. * @returns the list of animatables object used in the material
  1305. */
  1306. getAnimatables() {
  1307. const results = super.getAnimatables();
  1308. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  1309. results.push(this._diffuseTexture);
  1310. }
  1311. if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
  1312. results.push(this._ambientTexture);
  1313. }
  1314. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  1315. results.push(this._opacityTexture);
  1316. }
  1317. if (this._reflectionTexture && this._reflectionTexture.animations && this._reflectionTexture.animations.length > 0) {
  1318. results.push(this._reflectionTexture);
  1319. }
  1320. if (this._emissiveTexture && this._emissiveTexture.animations && this._emissiveTexture.animations.length > 0) {
  1321. results.push(this._emissiveTexture);
  1322. }
  1323. if (this._specularTexture && this._specularTexture.animations && this._specularTexture.animations.length > 0) {
  1324. results.push(this._specularTexture);
  1325. }
  1326. if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
  1327. results.push(this._bumpTexture);
  1328. }
  1329. if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
  1330. results.push(this._lightmapTexture);
  1331. }
  1332. if (this._refractionTexture && this._refractionTexture.animations && this._refractionTexture.animations.length > 0) {
  1333. results.push(this._refractionTexture);
  1334. }
  1335. return results;
  1336. }
  1337. /**
  1338. * Gets the active textures from the material
  1339. * @returns an array of textures
  1340. */
  1341. getActiveTextures() {
  1342. const activeTextures = super.getActiveTextures();
  1343. if (this._diffuseTexture) {
  1344. activeTextures.push(this._diffuseTexture);
  1345. }
  1346. if (this._ambientTexture) {
  1347. activeTextures.push(this._ambientTexture);
  1348. }
  1349. if (this._opacityTexture) {
  1350. activeTextures.push(this._opacityTexture);
  1351. }
  1352. if (this._reflectionTexture) {
  1353. activeTextures.push(this._reflectionTexture);
  1354. }
  1355. if (this._emissiveTexture) {
  1356. activeTextures.push(this._emissiveTexture);
  1357. }
  1358. if (this._specularTexture) {
  1359. activeTextures.push(this._specularTexture);
  1360. }
  1361. if (this._bumpTexture) {
  1362. activeTextures.push(this._bumpTexture);
  1363. }
  1364. if (this._lightmapTexture) {
  1365. activeTextures.push(this._lightmapTexture);
  1366. }
  1367. if (this._refractionTexture) {
  1368. activeTextures.push(this._refractionTexture);
  1369. }
  1370. return activeTextures;
  1371. }
  1372. /**
  1373. * Specifies if the material uses a texture
  1374. * @param texture defines the texture to check against the material
  1375. * @returns a boolean specifying if the material uses the texture
  1376. */
  1377. hasTexture(texture) {
  1378. if (super.hasTexture(texture)) {
  1379. return true;
  1380. }
  1381. if (this._diffuseTexture === texture) {
  1382. return true;
  1383. }
  1384. if (this._ambientTexture === texture) {
  1385. return true;
  1386. }
  1387. if (this._opacityTexture === texture) {
  1388. return true;
  1389. }
  1390. if (this._reflectionTexture === texture) {
  1391. return true;
  1392. }
  1393. if (this._emissiveTexture === texture) {
  1394. return true;
  1395. }
  1396. if (this._specularTexture === texture) {
  1397. return true;
  1398. }
  1399. if (this._bumpTexture === texture) {
  1400. return true;
  1401. }
  1402. if (this._lightmapTexture === texture) {
  1403. return true;
  1404. }
  1405. if (this._refractionTexture === texture) {
  1406. return true;
  1407. }
  1408. return false;
  1409. }
  1410. /**
  1411. * Disposes the material
  1412. * @param forceDisposeEffect specifies if effects should be forcefully disposed
  1413. * @param forceDisposeTextures specifies if textures should be forcefully disposed
  1414. */
  1415. dispose(forceDisposeEffect, forceDisposeTextures) {
  1416. if (forceDisposeTextures) {
  1417. this._diffuseTexture?.dispose();
  1418. this._ambientTexture?.dispose();
  1419. this._opacityTexture?.dispose();
  1420. this._reflectionTexture?.dispose();
  1421. this._emissiveTexture?.dispose();
  1422. this._specularTexture?.dispose();
  1423. this._bumpTexture?.dispose();
  1424. this._lightmapTexture?.dispose();
  1425. this._refractionTexture?.dispose();
  1426. }
  1427. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  1428. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  1429. }
  1430. super.dispose(forceDisposeEffect, forceDisposeTextures);
  1431. }
  1432. /**
  1433. * Makes a duplicate of the material, and gives it a new name
  1434. * @param name defines the new name for the duplicated material
  1435. * @param cloneTexturesOnlyOnce - if a texture is used in more than one channel (e.g diffuse and opacity), only clone it once and reuse it on the other channels. Default false.
  1436. * @param rootUrl defines the root URL to use to load textures
  1437. * @returns the cloned material
  1438. */
  1439. clone(name, cloneTexturesOnlyOnce = true, rootUrl = "") {
  1440. const result = SerializationHelper.Clone(() => new StandardMaterial(name, this.getScene()), this, { cloneTexturesOnlyOnce });
  1441. result.name = name;
  1442. result.id = name;
  1443. this.stencil.copyTo(result.stencil);
  1444. this._clonePlugins(result, rootUrl);
  1445. return result;
  1446. }
  1447. /**
  1448. * Creates a standard material from parsed material data
  1449. * @param source defines the JSON representation of the material
  1450. * @param scene defines the hosting scene
  1451. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  1452. * @returns a new standard material
  1453. */
  1454. static Parse(source, scene, rootUrl) {
  1455. const material = SerializationHelper.Parse(() => new StandardMaterial(source.name, scene), source, scene, rootUrl);
  1456. if (source.stencil) {
  1457. material.stencil.parse(source.stencil, scene, rootUrl);
  1458. }
  1459. Material._ParsePlugins(source, material, scene, rootUrl);
  1460. return material;
  1461. }
  1462. // Flags used to enable or disable a type of texture for all Standard Materials
  1463. /**
  1464. * Are diffuse textures enabled in the application.
  1465. */
  1466. static get DiffuseTextureEnabled() {
  1467. return MaterialFlags.DiffuseTextureEnabled;
  1468. }
  1469. static set DiffuseTextureEnabled(value) {
  1470. MaterialFlags.DiffuseTextureEnabled = value;
  1471. }
  1472. /**
  1473. * Are detail textures enabled in the application.
  1474. */
  1475. static get DetailTextureEnabled() {
  1476. return MaterialFlags.DetailTextureEnabled;
  1477. }
  1478. static set DetailTextureEnabled(value) {
  1479. MaterialFlags.DetailTextureEnabled = value;
  1480. }
  1481. /**
  1482. * Are ambient textures enabled in the application.
  1483. */
  1484. static get AmbientTextureEnabled() {
  1485. return MaterialFlags.AmbientTextureEnabled;
  1486. }
  1487. static set AmbientTextureEnabled(value) {
  1488. MaterialFlags.AmbientTextureEnabled = value;
  1489. }
  1490. /**
  1491. * Are opacity textures enabled in the application.
  1492. */
  1493. static get OpacityTextureEnabled() {
  1494. return MaterialFlags.OpacityTextureEnabled;
  1495. }
  1496. static set OpacityTextureEnabled(value) {
  1497. MaterialFlags.OpacityTextureEnabled = value;
  1498. }
  1499. /**
  1500. * Are reflection textures enabled in the application.
  1501. */
  1502. static get ReflectionTextureEnabled() {
  1503. return MaterialFlags.ReflectionTextureEnabled;
  1504. }
  1505. static set ReflectionTextureEnabled(value) {
  1506. MaterialFlags.ReflectionTextureEnabled = value;
  1507. }
  1508. /**
  1509. * Are emissive textures enabled in the application.
  1510. */
  1511. static get EmissiveTextureEnabled() {
  1512. return MaterialFlags.EmissiveTextureEnabled;
  1513. }
  1514. static set EmissiveTextureEnabled(value) {
  1515. MaterialFlags.EmissiveTextureEnabled = value;
  1516. }
  1517. /**
  1518. * Are specular textures enabled in the application.
  1519. */
  1520. static get SpecularTextureEnabled() {
  1521. return MaterialFlags.SpecularTextureEnabled;
  1522. }
  1523. static set SpecularTextureEnabled(value) {
  1524. MaterialFlags.SpecularTextureEnabled = value;
  1525. }
  1526. /**
  1527. * Are bump textures enabled in the application.
  1528. */
  1529. static get BumpTextureEnabled() {
  1530. return MaterialFlags.BumpTextureEnabled;
  1531. }
  1532. static set BumpTextureEnabled(value) {
  1533. MaterialFlags.BumpTextureEnabled = value;
  1534. }
  1535. /**
  1536. * Are lightmap textures enabled in the application.
  1537. */
  1538. static get LightmapTextureEnabled() {
  1539. return MaterialFlags.LightmapTextureEnabled;
  1540. }
  1541. static set LightmapTextureEnabled(value) {
  1542. MaterialFlags.LightmapTextureEnabled = value;
  1543. }
  1544. /**
  1545. * Are refraction textures enabled in the application.
  1546. */
  1547. static get RefractionTextureEnabled() {
  1548. return MaterialFlags.RefractionTextureEnabled;
  1549. }
  1550. static set RefractionTextureEnabled(value) {
  1551. MaterialFlags.RefractionTextureEnabled = value;
  1552. }
  1553. /**
  1554. * Are color grading textures enabled in the application.
  1555. */
  1556. static get ColorGradingTextureEnabled() {
  1557. return MaterialFlags.ColorGradingTextureEnabled;
  1558. }
  1559. static set ColorGradingTextureEnabled(value) {
  1560. MaterialFlags.ColorGradingTextureEnabled = value;
  1561. }
  1562. /**
  1563. * Are fresnels enabled in the application.
  1564. */
  1565. static get FresnelEnabled() {
  1566. return MaterialFlags.FresnelEnabled;
  1567. }
  1568. static set FresnelEnabled(value) {
  1569. MaterialFlags.FresnelEnabled = value;
  1570. }
  1571. }
  1572. __decorate([
  1573. serializeAsTexture("diffuseTexture")
  1574. ], StandardMaterial.prototype, "_diffuseTexture", void 0);
  1575. __decorate([
  1576. expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  1577. ], StandardMaterial.prototype, "diffuseTexture", void 0);
  1578. __decorate([
  1579. serializeAsTexture("ambientTexture")
  1580. ], StandardMaterial.prototype, "_ambientTexture", void 0);
  1581. __decorate([
  1582. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1583. ], StandardMaterial.prototype, "ambientTexture", void 0);
  1584. __decorate([
  1585. serializeAsTexture("opacityTexture")
  1586. ], StandardMaterial.prototype, "_opacityTexture", void 0);
  1587. __decorate([
  1588. expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  1589. ], StandardMaterial.prototype, "opacityTexture", void 0);
  1590. __decorate([
  1591. serializeAsTexture("reflectionTexture")
  1592. ], StandardMaterial.prototype, "_reflectionTexture", void 0);
  1593. __decorate([
  1594. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1595. ], StandardMaterial.prototype, "reflectionTexture", void 0);
  1596. __decorate([
  1597. serializeAsTexture("emissiveTexture")
  1598. ], StandardMaterial.prototype, "_emissiveTexture", void 0);
  1599. __decorate([
  1600. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1601. ], StandardMaterial.prototype, "emissiveTexture", void 0);
  1602. __decorate([
  1603. serializeAsTexture("specularTexture")
  1604. ], StandardMaterial.prototype, "_specularTexture", void 0);
  1605. __decorate([
  1606. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1607. ], StandardMaterial.prototype, "specularTexture", void 0);
  1608. __decorate([
  1609. serializeAsTexture("bumpTexture")
  1610. ], StandardMaterial.prototype, "_bumpTexture", void 0);
  1611. __decorate([
  1612. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1613. ], StandardMaterial.prototype, "bumpTexture", void 0);
  1614. __decorate([
  1615. serializeAsTexture("lightmapTexture")
  1616. ], StandardMaterial.prototype, "_lightmapTexture", void 0);
  1617. __decorate([
  1618. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1619. ], StandardMaterial.prototype, "lightmapTexture", void 0);
  1620. __decorate([
  1621. serializeAsTexture("refractionTexture")
  1622. ], StandardMaterial.prototype, "_refractionTexture", void 0);
  1623. __decorate([
  1624. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1625. ], StandardMaterial.prototype, "refractionTexture", void 0);
  1626. __decorate([
  1627. serializeAsColor3("ambient")
  1628. ], StandardMaterial.prototype, "ambientColor", void 0);
  1629. __decorate([
  1630. serializeAsColor3("diffuse")
  1631. ], StandardMaterial.prototype, "diffuseColor", void 0);
  1632. __decorate([
  1633. serializeAsColor3("specular")
  1634. ], StandardMaterial.prototype, "specularColor", void 0);
  1635. __decorate([
  1636. serializeAsColor3("emissive")
  1637. ], StandardMaterial.prototype, "emissiveColor", void 0);
  1638. __decorate([
  1639. serialize()
  1640. ], StandardMaterial.prototype, "specularPower", void 0);
  1641. __decorate([
  1642. serialize("useAlphaFromDiffuseTexture")
  1643. ], StandardMaterial.prototype, "_useAlphaFromDiffuseTexture", void 0);
  1644. __decorate([
  1645. expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  1646. ], StandardMaterial.prototype, "useAlphaFromDiffuseTexture", void 0);
  1647. __decorate([
  1648. serialize("useEmissiveAsIllumination")
  1649. ], StandardMaterial.prototype, "_useEmissiveAsIllumination", void 0);
  1650. __decorate([
  1651. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1652. ], StandardMaterial.prototype, "useEmissiveAsIllumination", void 0);
  1653. __decorate([
  1654. serialize("linkEmissiveWithDiffuse")
  1655. ], StandardMaterial.prototype, "_linkEmissiveWithDiffuse", void 0);
  1656. __decorate([
  1657. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1658. ], StandardMaterial.prototype, "linkEmissiveWithDiffuse", void 0);
  1659. __decorate([
  1660. serialize("useSpecularOverAlpha")
  1661. ], StandardMaterial.prototype, "_useSpecularOverAlpha", void 0);
  1662. __decorate([
  1663. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1664. ], StandardMaterial.prototype, "useSpecularOverAlpha", void 0);
  1665. __decorate([
  1666. serialize("useReflectionOverAlpha")
  1667. ], StandardMaterial.prototype, "_useReflectionOverAlpha", void 0);
  1668. __decorate([
  1669. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1670. ], StandardMaterial.prototype, "useReflectionOverAlpha", void 0);
  1671. __decorate([
  1672. serialize("disableLighting")
  1673. ], StandardMaterial.prototype, "_disableLighting", void 0);
  1674. __decorate([
  1675. expandToProperty("_markAllSubMeshesAsLightsDirty")
  1676. ], StandardMaterial.prototype, "disableLighting", void 0);
  1677. __decorate([
  1678. serialize("useObjectSpaceNormalMap")
  1679. ], StandardMaterial.prototype, "_useObjectSpaceNormalMap", void 0);
  1680. __decorate([
  1681. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1682. ], StandardMaterial.prototype, "useObjectSpaceNormalMap", void 0);
  1683. __decorate([
  1684. serialize("useParallax")
  1685. ], StandardMaterial.prototype, "_useParallax", void 0);
  1686. __decorate([
  1687. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1688. ], StandardMaterial.prototype, "useParallax", void 0);
  1689. __decorate([
  1690. serialize("useParallaxOcclusion")
  1691. ], StandardMaterial.prototype, "_useParallaxOcclusion", void 0);
  1692. __decorate([
  1693. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1694. ], StandardMaterial.prototype, "useParallaxOcclusion", void 0);
  1695. __decorate([
  1696. serialize()
  1697. ], StandardMaterial.prototype, "parallaxScaleBias", void 0);
  1698. __decorate([
  1699. serialize("roughness")
  1700. ], StandardMaterial.prototype, "_roughness", void 0);
  1701. __decorate([
  1702. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1703. ], StandardMaterial.prototype, "roughness", void 0);
  1704. __decorate([
  1705. serialize()
  1706. ], StandardMaterial.prototype, "indexOfRefraction", void 0);
  1707. __decorate([
  1708. serialize()
  1709. ], StandardMaterial.prototype, "invertRefractionY", void 0);
  1710. __decorate([
  1711. serialize()
  1712. ], StandardMaterial.prototype, "alphaCutOff", void 0);
  1713. __decorate([
  1714. serialize("useLightmapAsShadowmap")
  1715. ], StandardMaterial.prototype, "_useLightmapAsShadowmap", void 0);
  1716. __decorate([
  1717. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1718. ], StandardMaterial.prototype, "useLightmapAsShadowmap", void 0);
  1719. __decorate([
  1720. serializeAsFresnelParameters("diffuseFresnelParameters")
  1721. ], StandardMaterial.prototype, "_diffuseFresnelParameters", void 0);
  1722. __decorate([
  1723. expandToProperty("_markAllSubMeshesAsFresnelDirty")
  1724. ], StandardMaterial.prototype, "diffuseFresnelParameters", void 0);
  1725. __decorate([
  1726. serializeAsFresnelParameters("opacityFresnelParameters")
  1727. ], StandardMaterial.prototype, "_opacityFresnelParameters", void 0);
  1728. __decorate([
  1729. expandToProperty("_markAllSubMeshesAsFresnelAndMiscDirty")
  1730. ], StandardMaterial.prototype, "opacityFresnelParameters", void 0);
  1731. __decorate([
  1732. serializeAsFresnelParameters("reflectionFresnelParameters")
  1733. ], StandardMaterial.prototype, "_reflectionFresnelParameters", void 0);
  1734. __decorate([
  1735. expandToProperty("_markAllSubMeshesAsFresnelDirty")
  1736. ], StandardMaterial.prototype, "reflectionFresnelParameters", void 0);
  1737. __decorate([
  1738. serializeAsFresnelParameters("refractionFresnelParameters")
  1739. ], StandardMaterial.prototype, "_refractionFresnelParameters", void 0);
  1740. __decorate([
  1741. expandToProperty("_markAllSubMeshesAsFresnelDirty")
  1742. ], StandardMaterial.prototype, "refractionFresnelParameters", void 0);
  1743. __decorate([
  1744. serializeAsFresnelParameters("emissiveFresnelParameters")
  1745. ], StandardMaterial.prototype, "_emissiveFresnelParameters", void 0);
  1746. __decorate([
  1747. expandToProperty("_markAllSubMeshesAsFresnelDirty")
  1748. ], StandardMaterial.prototype, "emissiveFresnelParameters", void 0);
  1749. __decorate([
  1750. serialize("useReflectionFresnelFromSpecular")
  1751. ], StandardMaterial.prototype, "_useReflectionFresnelFromSpecular", void 0);
  1752. __decorate([
  1753. expandToProperty("_markAllSubMeshesAsFresnelDirty")
  1754. ], StandardMaterial.prototype, "useReflectionFresnelFromSpecular", void 0);
  1755. __decorate([
  1756. serialize("useGlossinessFromSpecularMapAlpha")
  1757. ], StandardMaterial.prototype, "_useGlossinessFromSpecularMapAlpha", void 0);
  1758. __decorate([
  1759. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1760. ], StandardMaterial.prototype, "useGlossinessFromSpecularMapAlpha", void 0);
  1761. __decorate([
  1762. serialize("maxSimultaneousLights")
  1763. ], StandardMaterial.prototype, "_maxSimultaneousLights", void 0);
  1764. __decorate([
  1765. expandToProperty("_markAllSubMeshesAsLightsDirty")
  1766. ], StandardMaterial.prototype, "maxSimultaneousLights", void 0);
  1767. __decorate([
  1768. serialize("invertNormalMapX")
  1769. ], StandardMaterial.prototype, "_invertNormalMapX", void 0);
  1770. __decorate([
  1771. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1772. ], StandardMaterial.prototype, "invertNormalMapX", void 0);
  1773. __decorate([
  1774. serialize("invertNormalMapY")
  1775. ], StandardMaterial.prototype, "_invertNormalMapY", void 0);
  1776. __decorate([
  1777. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1778. ], StandardMaterial.prototype, "invertNormalMapY", void 0);
  1779. __decorate([
  1780. serialize("twoSidedLighting")
  1781. ], StandardMaterial.prototype, "_twoSidedLighting", void 0);
  1782. __decorate([
  1783. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  1784. ], StandardMaterial.prototype, "twoSidedLighting", void 0);
  1785. __decorate([
  1786. serialize("applyDecalMapAfterDetailMap")
  1787. ], StandardMaterial.prototype, "_applyDecalMapAfterDetailMap", void 0);
  1788. __decorate([
  1789. expandToProperty("_markAllSubMeshesAsMiscDirty")
  1790. ], StandardMaterial.prototype, "applyDecalMapAfterDetailMap", void 0);
  1791. RegisterClass("BABYLON.StandardMaterial", StandardMaterial);
  1792. Scene.DefaultMaterialFactory = (scene) => {
  1793. return new StandardMaterial("default material", scene);
  1794. };
  1795. //# sourceMappingURL=standardMaterial.js.map