material.ts 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. import { serialize, SerializationHelper } from "../Misc/decorators";
  2. import { Tools } from "../Misc/tools";
  3. import { IAnimatable } from '../Animations/animatable.interface';
  4. import { SmartArray } from "../Misc/smartArray";
  5. import { Observer, Observable } from "../Misc/observable";
  6. import { Nullable } from "../types";
  7. import { Scene } from "../scene";
  8. import { Matrix } from "../Maths/math.vector";
  9. import { EngineStore } from "../Engines/engineStore";
  10. import { SubMesh } from "../Meshes/subMesh";
  11. import { Geometry } from "../Meshes/geometry";
  12. import { AbstractMesh } from "../Meshes/abstractMesh";
  13. import { UniformBuffer } from "./uniformBuffer";
  14. import { Effect } from "./effect";
  15. import { BaseTexture } from "../Materials/Textures/baseTexture";
  16. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  17. import { MaterialDefines } from "./materialDefines";
  18. import { Constants } from "../Engines/constants";
  19. import { Logger } from "../Misc/logger";
  20. import { IInspectable } from '../Misc/iInspectable';
  21. import { Plane } from '../Maths/math.plane';
  22. import { ShadowDepthWrapper } from './shadowDepthWrapper';
  23. declare type Mesh = import("../Meshes/mesh").Mesh;
  24. declare type Animation = import("../Animations/animation").Animation;
  25. declare type InstancedMesh = import('../Meshes/instancedMesh').InstancedMesh;
  26. declare var BABYLON: any;
  27. /**
  28. * Options for compiling materials.
  29. */
  30. export interface IMaterialCompilationOptions {
  31. /**
  32. * Defines whether clip planes are enabled.
  33. */
  34. clipPlane: boolean;
  35. /**
  36. * Defines whether instances are enabled.
  37. */
  38. useInstances: boolean;
  39. }
  40. /**
  41. * Options passed when calling customShaderNameResolve
  42. */
  43. export interface ICustomShaderNameResolveOptions {
  44. /**
  45. * If provided, will be called two times with the vertex and fragment code so that this code can be updated before it is compiled by the GPU
  46. */
  47. processFinalCode?: Nullable<(shaderType: string, code: string) => string>;
  48. }
  49. /**
  50. * Base class for the main features of a material in Babylon.js
  51. */
  52. export class Material implements IAnimatable {
  53. /**
  54. * Returns the triangle fill mode
  55. */
  56. public static readonly TriangleFillMode = Constants.MATERIAL_TriangleFillMode;
  57. /**
  58. * Returns the wireframe mode
  59. */
  60. public static readonly WireFrameFillMode = Constants.MATERIAL_WireFrameFillMode;
  61. /**
  62. * Returns the point fill mode
  63. */
  64. public static readonly PointFillMode = Constants.MATERIAL_PointFillMode;
  65. /**
  66. * Returns the point list draw mode
  67. */
  68. public static readonly PointListDrawMode = Constants.MATERIAL_PointListDrawMode;
  69. /**
  70. * Returns the line list draw mode
  71. */
  72. public static readonly LineListDrawMode = Constants.MATERIAL_LineListDrawMode;
  73. /**
  74. * Returns the line loop draw mode
  75. */
  76. public static readonly LineLoopDrawMode = Constants.MATERIAL_LineLoopDrawMode;
  77. /**
  78. * Returns the line strip draw mode
  79. */
  80. public static readonly LineStripDrawMode = Constants.MATERIAL_LineStripDrawMode;
  81. /**
  82. * Returns the triangle strip draw mode
  83. */
  84. public static readonly TriangleStripDrawMode = Constants.MATERIAL_TriangleStripDrawMode;
  85. /**
  86. * Returns the triangle fan draw mode
  87. */
  88. public static readonly TriangleFanDrawMode = Constants.MATERIAL_TriangleFanDrawMode;
  89. /**
  90. * Stores the clock-wise side orientation
  91. */
  92. public static readonly ClockWiseSideOrientation = Constants.MATERIAL_ClockWiseSideOrientation;
  93. /**
  94. * Stores the counter clock-wise side orientation
  95. */
  96. public static readonly CounterClockWiseSideOrientation = Constants.MATERIAL_CounterClockWiseSideOrientation;
  97. /**
  98. * The dirty texture flag value
  99. */
  100. public static readonly TextureDirtyFlag = Constants.MATERIAL_TextureDirtyFlag;
  101. /**
  102. * The dirty light flag value
  103. */
  104. public static readonly LightDirtyFlag = Constants.MATERIAL_LightDirtyFlag;
  105. /**
  106. * The dirty fresnel flag value
  107. */
  108. public static readonly FresnelDirtyFlag = Constants.MATERIAL_FresnelDirtyFlag;
  109. /**
  110. * The dirty attribute flag value
  111. */
  112. public static readonly AttributesDirtyFlag = Constants.MATERIAL_AttributesDirtyFlag;
  113. /**
  114. * The dirty misc flag value
  115. */
  116. public static readonly MiscDirtyFlag = Constants.MATERIAL_MiscDirtyFlag;
  117. /**
  118. * The all dirty flag value
  119. */
  120. public static readonly AllDirtyFlag = Constants.MATERIAL_AllDirtyFlag;
  121. /**
  122. * MaterialTransparencyMode: No transparency mode, Alpha channel is not use.
  123. */
  124. public static readonly MATERIAL_OPAQUE = 0;
  125. /**
  126. * MaterialTransparencyMode: Alpha Test mode, pixel are discarded below a certain threshold defined by the alpha cutoff value.
  127. */
  128. public static readonly MATERIAL_ALPHATEST = 1;
  129. /**
  130. * MaterialTransparencyMode: Pixels are blended (according to the alpha mode) with the already drawn pixels in the current frame buffer.
  131. */
  132. public static readonly MATERIAL_ALPHABLEND = 2;
  133. /**
  134. * MaterialTransparencyMode: Pixels are blended (according to the alpha mode) with the already drawn pixels in the current frame buffer.
  135. * They are also discarded below the alpha cutoff threshold to improve performances.
  136. */
  137. public static readonly MATERIAL_ALPHATESTANDBLEND = 3;
  138. /**
  139. * The Whiteout method is used to blend normals.
  140. * Details of the algorithm can be found here: https://blog.selfshadow.com/publications/blending-in-detail/
  141. */
  142. public static readonly MATERIAL_NORMALBLENDMETHOD_WHITEOUT = 0;
  143. /**
  144. * The Reoriented Normal Mapping method is used to blend normals.
  145. * Details of the algorithm can be found here: https://blog.selfshadow.com/publications/blending-in-detail/
  146. */
  147. public static readonly MATERIAL_NORMALBLENDMETHOD_RNM = 1;
  148. /**
  149. * Custom callback helping to override the default shader used in the material.
  150. */
  151. public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: MaterialDefines | string[], attributes?: string[], options?: ICustomShaderNameResolveOptions) => string;
  152. /**
  153. * Custom shadow depth material to use for shadow rendering instead of the in-built one
  154. */
  155. public shadowDepthWrapper: Nullable<ShadowDepthWrapper> = null;
  156. /**
  157. * Gets or sets a boolean indicating that the material is allowed (if supported) to do shader hot swapping.
  158. * This means that the material can keep using a previous shader while a new one is being compiled.
  159. * This is mostly used when shader parallel compilation is supported (true by default)
  160. */
  161. public allowShaderHotSwapping = true;
  162. /**
  163. * The ID of the material
  164. */
  165. @serialize()
  166. public id: string;
  167. /**
  168. * Gets or sets the unique id of the material
  169. */
  170. @serialize()
  171. public uniqueId: number;
  172. /**
  173. * The name of the material
  174. */
  175. @serialize()
  176. public name: string;
  177. /**
  178. * Gets or sets user defined metadata
  179. */
  180. public metadata: any = null;
  181. /**
  182. * For internal use only. Please do not use.
  183. */
  184. public reservedDataStore: any = null;
  185. /**
  186. * Specifies if the ready state should be checked on each call
  187. */
  188. @serialize()
  189. public checkReadyOnEveryCall = false;
  190. /**
  191. * Specifies if the ready state should be checked once
  192. */
  193. @serialize()
  194. public checkReadyOnlyOnce = false;
  195. /**
  196. * The state of the material
  197. */
  198. @serialize()
  199. public state = "";
  200. /**
  201. * If the material should be rendered to several textures with MRT extension
  202. */
  203. public get shouldRenderToMRT() : boolean {
  204. // By default, shaders are not compatible with MRTs
  205. // Base classes should override that if their shader supports MRT
  206. return false;
  207. }
  208. /**
  209. * The alpha value of the material
  210. */
  211. @serialize("alpha")
  212. protected _alpha = 1.0;
  213. /**
  214. * List of inspectable custom properties (used by the Inspector)
  215. * @see https://doc.babylonjs.com/how_to/debug_layer#extensibility
  216. */
  217. public inspectableCustomProperties: IInspectable[];
  218. /**
  219. * Sets the alpha value of the material
  220. */
  221. public set alpha(value: number) {
  222. if (this._alpha === value) {
  223. return;
  224. }
  225. this._alpha = value;
  226. this.markAsDirty(Material.MiscDirtyFlag);
  227. }
  228. /**
  229. * Gets the alpha value of the material
  230. */
  231. public get alpha(): number {
  232. return this._alpha;
  233. }
  234. /**
  235. * Specifies if back face culling is enabled
  236. */
  237. @serialize("backFaceCulling")
  238. protected _backFaceCulling = true;
  239. /**
  240. * Sets the back-face culling state
  241. */
  242. public set backFaceCulling(value: boolean) {
  243. if (this._backFaceCulling === value) {
  244. return;
  245. }
  246. this._backFaceCulling = value;
  247. this.markAsDirty(Material.TextureDirtyFlag);
  248. }
  249. /**
  250. * Gets the back-face culling state
  251. */
  252. public get backFaceCulling(): boolean {
  253. return this._backFaceCulling;
  254. }
  255. /**
  256. * Stores the value for side orientation
  257. */
  258. @serialize()
  259. public sideOrientation: number;
  260. /**
  261. * Callback triggered when the material is compiled
  262. */
  263. public onCompiled: Nullable<(effect: Effect) => void> = null;
  264. /**
  265. * Callback triggered when an error occurs
  266. */
  267. public onError: Nullable<(effect: Effect, errors: string) => void> = null;
  268. /**
  269. * Callback triggered to get the render target textures
  270. */
  271. public getRenderTargetTextures: Nullable<() => SmartArray<RenderTargetTexture>> = null;
  272. /**
  273. * Gets a boolean indicating that current material needs to register RTT
  274. */
  275. public get hasRenderTargetTextures(): boolean {
  276. return false;
  277. }
  278. /**
  279. * Specifies if the material should be serialized
  280. */
  281. public doNotSerialize = false;
  282. /**
  283. * @hidden
  284. */
  285. public _storeEffectOnSubMeshes = false;
  286. /**
  287. * Stores the animations for the material
  288. */
  289. public animations: Nullable<Array<Animation>> = null;
  290. /**
  291. * An event triggered when the material is disposed
  292. */
  293. public onDisposeObservable = new Observable<Material>();
  294. /**
  295. * An observer which watches for dispose events
  296. */
  297. private _onDisposeObserver: Nullable<Observer<Material>> = null;
  298. private _onUnBindObservable: Nullable<Observable<Material>> = null;
  299. /**
  300. * Called during a dispose event
  301. */
  302. public set onDispose(callback: () => void) {
  303. if (this._onDisposeObserver) {
  304. this.onDisposeObservable.remove(this._onDisposeObserver);
  305. }
  306. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  307. }
  308. private _onBindObservable: Nullable<Observable<AbstractMesh>>;
  309. /**
  310. * An event triggered when the material is bound
  311. */
  312. public get onBindObservable(): Observable<AbstractMesh> {
  313. if (!this._onBindObservable) {
  314. this._onBindObservable = new Observable<AbstractMesh>();
  315. }
  316. return this._onBindObservable;
  317. }
  318. /**
  319. * An observer which watches for bind events
  320. */
  321. private _onBindObserver: Nullable<Observer<AbstractMesh>> = null;
  322. /**
  323. * Called during a bind event
  324. */
  325. public set onBind(callback: (Mesh: AbstractMesh) => void) {
  326. if (this._onBindObserver) {
  327. this.onBindObservable.remove(this._onBindObserver);
  328. }
  329. this._onBindObserver = this.onBindObservable.add(callback);
  330. }
  331. /**
  332. * An event triggered when the material is unbound
  333. */
  334. public get onUnBindObservable(): Observable<Material> {
  335. if (!this._onUnBindObservable) {
  336. this._onUnBindObservable = new Observable<Material>();
  337. }
  338. return this._onUnBindObservable;
  339. }
  340. protected _onEffectCreatedObservable: Nullable<Observable<{ effect: Effect, subMesh: Nullable<SubMesh>}>>;
  341. /**
  342. * An event triggered when the effect is (re)created
  343. */
  344. public get onEffectCreatedObservable(): Observable<{ effect: Effect, subMesh: Nullable<SubMesh>}> {
  345. if (!this._onEffectCreatedObservable) {
  346. this._onEffectCreatedObservable = new Observable<{effect: Effect, subMesh: Nullable<SubMesh>}>();
  347. }
  348. return this._onEffectCreatedObservable;
  349. }
  350. /**
  351. * Stores the value of the alpha mode
  352. */
  353. @serialize("alphaMode")
  354. private _alphaMode: number = Constants.ALPHA_COMBINE;
  355. /**
  356. * Sets the value of the alpha mode.
  357. *
  358. * | Value | Type | Description |
  359. * | --- | --- | --- |
  360. * | 0 | ALPHA_DISABLE | |
  361. * | 1 | ALPHA_ADD | |
  362. * | 2 | ALPHA_COMBINE | |
  363. * | 3 | ALPHA_SUBTRACT | |
  364. * | 4 | ALPHA_MULTIPLY | |
  365. * | 5 | ALPHA_MAXIMIZED | |
  366. * | 6 | ALPHA_ONEONE | |
  367. * | 7 | ALPHA_PREMULTIPLIED | |
  368. * | 8 | ALPHA_PREMULTIPLIED_PORTERDUFF | |
  369. * | 9 | ALPHA_INTERPOLATE | |
  370. * | 10 | ALPHA_SCREENMODE | |
  371. *
  372. */
  373. public set alphaMode(value: number) {
  374. if (this._alphaMode === value) {
  375. return;
  376. }
  377. this._alphaMode = value;
  378. this.markAsDirty(Material.TextureDirtyFlag);
  379. }
  380. /**
  381. * Gets the value of the alpha mode
  382. */
  383. public get alphaMode(): number {
  384. return this._alphaMode;
  385. }
  386. /**
  387. * Stores the state of the need depth pre-pass value
  388. */
  389. @serialize()
  390. private _needDepthPrePass = false;
  391. /**
  392. * Sets the need depth pre-pass value
  393. */
  394. public set needDepthPrePass(value: boolean) {
  395. if (this._needDepthPrePass === value) {
  396. return;
  397. }
  398. this._needDepthPrePass = value;
  399. if (this._needDepthPrePass) {
  400. this.checkReadyOnEveryCall = true;
  401. }
  402. }
  403. /**
  404. * Gets the depth pre-pass value
  405. */
  406. public get needDepthPrePass(): boolean {
  407. return this._needDepthPrePass;
  408. }
  409. /**
  410. * Specifies if depth writing should be disabled
  411. */
  412. @serialize()
  413. public disableDepthWrite = false;
  414. /**
  415. * Specifies if color writing should be disabled
  416. */
  417. @serialize()
  418. public disableColorWrite = false;
  419. /**
  420. * Specifies if depth writing should be forced
  421. */
  422. @serialize()
  423. public forceDepthWrite = false;
  424. /**
  425. * Specifies the depth function that should be used. 0 means the default engine function
  426. */
  427. @serialize()
  428. public depthFunction = 0;
  429. /**
  430. * Specifies if there should be a separate pass for culling
  431. */
  432. @serialize()
  433. public separateCullingPass = false;
  434. /**
  435. * Stores the state specifing if fog should be enabled
  436. */
  437. @serialize("fogEnabled")
  438. private _fogEnabled = true;
  439. /**
  440. * Sets the state for enabling fog
  441. */
  442. public set fogEnabled(value: boolean) {
  443. if (this._fogEnabled === value) {
  444. return;
  445. }
  446. this._fogEnabled = value;
  447. this.markAsDirty(Material.MiscDirtyFlag);
  448. }
  449. /**
  450. * Gets the value of the fog enabled state
  451. */
  452. public get fogEnabled(): boolean {
  453. return this._fogEnabled;
  454. }
  455. /**
  456. * Stores the size of points
  457. */
  458. @serialize()
  459. public pointSize = 1.0;
  460. /**
  461. * Stores the z offset value
  462. */
  463. @serialize()
  464. public zOffset = 0;
  465. public get wireframe(): boolean {
  466. switch (this._fillMode) {
  467. case Material.WireFrameFillMode:
  468. case Material.LineListDrawMode:
  469. case Material.LineLoopDrawMode:
  470. case Material.LineStripDrawMode:
  471. return true;
  472. }
  473. return this._scene.forceWireframe;
  474. }
  475. /**
  476. * Sets the state of wireframe mode
  477. */
  478. public set wireframe(value: boolean) {
  479. this.fillMode = (value ? Material.WireFrameFillMode : Material.TriangleFillMode);
  480. }
  481. /**
  482. * Gets the value specifying if point clouds are enabled
  483. */
  484. @serialize()
  485. public get pointsCloud(): boolean {
  486. switch (this._fillMode) {
  487. case Material.PointFillMode:
  488. case Material.PointListDrawMode:
  489. return true;
  490. }
  491. return this._scene.forcePointsCloud;
  492. }
  493. /**
  494. * Sets the state of point cloud mode
  495. */
  496. public set pointsCloud(value: boolean) {
  497. this.fillMode = (value ? Material.PointFillMode : Material.TriangleFillMode);
  498. }
  499. /**
  500. * Gets the material fill mode
  501. */
  502. @serialize()
  503. public get fillMode(): number {
  504. return this._fillMode;
  505. }
  506. /**
  507. * Sets the material fill mode
  508. */
  509. public set fillMode(value: number) {
  510. if (this._fillMode === value) {
  511. return;
  512. }
  513. this._fillMode = value;
  514. this.markAsDirty(Material.MiscDirtyFlag);
  515. }
  516. /**
  517. * @hidden
  518. * Stores the effects for the material
  519. */
  520. public _effect: Nullable<Effect> = null;
  521. /**
  522. * Specifies if uniform buffers should be used
  523. */
  524. private _useUBO: boolean = false;
  525. /**
  526. * Stores a reference to the scene
  527. */
  528. private _scene: Scene;
  529. /**
  530. * Stores the fill mode state
  531. */
  532. private _fillMode = Material.TriangleFillMode;
  533. /**
  534. * Specifies if the depth write state should be cached
  535. */
  536. private _cachedDepthWriteState: boolean = false;
  537. /**
  538. * Specifies if the color write state should be cached
  539. */
  540. private _cachedColorWriteState: boolean = false;
  541. /**
  542. * Specifies if the depth function state should be cached
  543. */
  544. private _cachedDepthFunctionState: number = 0;
  545. /**
  546. * Stores the uniform buffer
  547. */
  548. protected _uniformBuffer: UniformBuffer;
  549. /** @hidden */
  550. public _indexInSceneMaterialArray = -1;
  551. /** @hidden */
  552. public meshMap: Nullable<{ [id: string]: AbstractMesh | undefined }> = null;
  553. /**
  554. * Creates a material instance
  555. * @param name defines the name of the material
  556. * @param scene defines the scene to reference
  557. * @param doNotAdd specifies if the material should be added to the scene
  558. */
  559. constructor(name: string, scene: Scene, doNotAdd?: boolean) {
  560. this.name = name;
  561. this.id = name || Tools.RandomId();
  562. this._scene = scene || EngineStore.LastCreatedScene;
  563. this.uniqueId = this._scene.getUniqueId();
  564. if (this._scene.useRightHandedSystem) {
  565. this.sideOrientation = Material.ClockWiseSideOrientation;
  566. } else {
  567. this.sideOrientation = Material.CounterClockWiseSideOrientation;
  568. }
  569. this._uniformBuffer = new UniformBuffer(this._scene.getEngine());
  570. this._useUBO = this.getScene().getEngine().supportsUniformBuffers;
  571. if (!doNotAdd) {
  572. this._scene.addMaterial(this);
  573. }
  574. if (this._scene.useMaterialMeshMap) {
  575. this.meshMap = {};
  576. }
  577. }
  578. /**
  579. * Returns a string representation of the current material
  580. * @param fullDetails defines a boolean indicating which levels of logging is desired
  581. * @returns a string with material information
  582. */
  583. public toString(fullDetails?: boolean): string {
  584. var ret = "Name: " + this.name;
  585. if (fullDetails) {
  586. }
  587. return ret;
  588. }
  589. /**
  590. * Gets the class name of the material
  591. * @returns a string with the class name of the material
  592. */
  593. public getClassName(): string {
  594. return "Material";
  595. }
  596. /**
  597. * Specifies if updates for the material been locked
  598. */
  599. public get isFrozen(): boolean {
  600. return this.checkReadyOnlyOnce;
  601. }
  602. /**
  603. * Locks updates for the material
  604. */
  605. public freeze(): void {
  606. this.markDirty();
  607. this.checkReadyOnlyOnce = true;
  608. }
  609. /**
  610. * Unlocks updates for the material
  611. */
  612. public unfreeze(): void {
  613. this.markDirty();
  614. this.checkReadyOnlyOnce = false;
  615. }
  616. /**
  617. * Specifies if the material is ready to be used
  618. * @param mesh defines the mesh to check
  619. * @param useInstances specifies if instances should be used
  620. * @returns a boolean indicating if the material is ready to be used
  621. */
  622. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  623. return true;
  624. }
  625. /**
  626. * Specifies that the submesh is ready to be used
  627. * @param mesh defines the mesh to check
  628. * @param subMesh defines which submesh to check
  629. * @param useInstances specifies that instances should be used
  630. * @returns a boolean indicating that the submesh is ready or not
  631. */
  632. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  633. return false;
  634. }
  635. /**
  636. * Returns the material effect
  637. * @returns the effect associated with the material
  638. */
  639. public getEffect(): Nullable<Effect> {
  640. return this._effect;
  641. }
  642. /**
  643. * Returns the current scene
  644. * @returns a Scene
  645. */
  646. public getScene(): Scene {
  647. return this._scene;
  648. }
  649. /**
  650. * Enforces alpha test in opaque or blend mode in order to improve the performances of some situations.
  651. */
  652. protected _forceAlphaTest = false;
  653. /**
  654. * The transparency mode of the material.
  655. */
  656. protected _transparencyMode: Nullable<number> = null;
  657. /**
  658. * Gets the current transparency mode.
  659. */
  660. @serialize()
  661. public get transparencyMode(): Nullable<number> {
  662. return this._transparencyMode;
  663. }
  664. /**
  665. * Sets the transparency mode of the material.
  666. *
  667. * | Value | Type | Description |
  668. * | ----- | ----------------------------------- | ----------- |
  669. * | 0 | OPAQUE | |
  670. * | 1 | ALPHATEST | |
  671. * | 2 | ALPHABLEND | |
  672. * | 3 | ALPHATESTANDBLEND | |
  673. *
  674. */
  675. public set transparencyMode(value: Nullable<number>) {
  676. if (this._transparencyMode === value) {
  677. return;
  678. }
  679. this._transparencyMode = value;
  680. this._forceAlphaTest = (value === Material.MATERIAL_ALPHATESTANDBLEND);
  681. this._markAllSubMeshesAsTexturesAndMiscDirty();
  682. }
  683. /**
  684. * Returns true if alpha blending should be disabled.
  685. */
  686. protected get _disableAlphaBlending(): boolean {
  687. return (this._transparencyMode === Material.MATERIAL_OPAQUE ||
  688. this._transparencyMode === Material.MATERIAL_ALPHATEST);
  689. }
  690. /**
  691. * Specifies whether or not this material should be rendered in alpha blend mode.
  692. * @returns a boolean specifying if alpha blending is needed
  693. */
  694. public needAlphaBlending(): boolean {
  695. if (this._disableAlphaBlending) {
  696. return false;
  697. }
  698. return (this.alpha < 1.0);
  699. }
  700. /**
  701. * Specifies if the mesh will require alpha blending
  702. * @param mesh defines the mesh to check
  703. * @returns a boolean specifying if alpha blending is needed for the mesh
  704. */
  705. public needAlphaBlendingForMesh(mesh: AbstractMesh): boolean {
  706. if (this._disableAlphaBlending && mesh.visibility >= 1.0) {
  707. return false;
  708. }
  709. return this.needAlphaBlending() || (mesh.visibility < 1.0) || mesh.hasVertexAlpha;
  710. }
  711. /**
  712. * Specifies whether or not this material should be rendered in alpha test mode.
  713. * @returns a boolean specifying if an alpha test is needed.
  714. */
  715. public needAlphaTesting(): boolean {
  716. if (this._forceAlphaTest) {
  717. return true;
  718. }
  719. return false;
  720. }
  721. /**
  722. * Specifies if material alpha testing should be turned on for the mesh
  723. * @param mesh defines the mesh to check
  724. */
  725. protected _shouldTurnAlphaTestOn(mesh: AbstractMesh): boolean {
  726. return (!this.needAlphaBlendingForMesh(mesh) && this.needAlphaTesting());
  727. }
  728. /**
  729. * Gets the texture used for the alpha test
  730. * @returns the texture to use for alpha testing
  731. */
  732. public getAlphaTestTexture(): Nullable<BaseTexture> {
  733. return null;
  734. }
  735. /**
  736. * Marks the material to indicate that it needs to be re-calculated
  737. */
  738. public markDirty(): void {
  739. const meshes = this.getScene().meshes;
  740. for (var mesh of meshes) {
  741. if (!mesh.subMeshes) {
  742. continue;
  743. }
  744. for (var subMesh of mesh.subMeshes) {
  745. if (subMesh.getMaterial() !== this) {
  746. continue;
  747. }
  748. if (!subMesh.effect) {
  749. continue;
  750. }
  751. subMesh.effect._wasPreviouslyReady = false;
  752. }
  753. }
  754. }
  755. /** @hidden */
  756. public _preBind(effect?: Effect, overrideOrientation: Nullable<number> = null): boolean {
  757. var engine = this._scene.getEngine();
  758. var orientation = (overrideOrientation == null) ? this.sideOrientation : overrideOrientation;
  759. var reverse = orientation === Material.ClockWiseSideOrientation;
  760. engine.enableEffect(effect ? effect : this._effect);
  761. engine.setState(this.backFaceCulling, this.zOffset, false, reverse);
  762. return reverse;
  763. }
  764. /**
  765. * Binds the material to the mesh
  766. * @param world defines the world transformation matrix
  767. * @param mesh defines the mesh to bind the material to
  768. */
  769. public bind(world: Matrix, mesh?: Mesh): void {
  770. }
  771. /**
  772. * Binds the submesh to the material
  773. * @param world defines the world transformation matrix
  774. * @param mesh defines the mesh containing the submesh
  775. * @param subMesh defines the submesh to bind the material to
  776. */
  777. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  778. }
  779. /**
  780. * Binds the world matrix to the material
  781. * @param world defines the world transformation matrix
  782. */
  783. public bindOnlyWorldMatrix(world: Matrix): void {
  784. }
  785. /**
  786. * Binds the scene's uniform buffer to the effect.
  787. * @param effect defines the effect to bind to the scene uniform buffer
  788. * @param sceneUbo defines the uniform buffer storing scene data
  789. */
  790. public bindSceneUniformBuffer(effect: Effect, sceneUbo: UniformBuffer): void {
  791. sceneUbo.bindToEffect(effect, "Scene");
  792. }
  793. /**
  794. * Binds the view matrix to the effect
  795. * @param effect defines the effect to bind the view matrix to
  796. */
  797. public bindView(effect: Effect): void {
  798. if (!this._useUBO) {
  799. effect.setMatrix("view", this.getScene().getViewMatrix());
  800. } else {
  801. this.bindSceneUniformBuffer(effect, this.getScene().getSceneUniformBuffer());
  802. }
  803. }
  804. /**
  805. * Binds the view projection matrix to the effect
  806. * @param effect defines the effect to bind the view projection matrix to
  807. */
  808. public bindViewProjection(effect: Effect): void {
  809. if (!this._useUBO) {
  810. effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
  811. } else {
  812. this.bindSceneUniformBuffer(effect, this.getScene().getSceneUniformBuffer());
  813. }
  814. }
  815. /**
  816. * Processes to execute after binding the material to a mesh
  817. * @param mesh defines the rendered mesh
  818. */
  819. protected _afterBind(mesh?: Mesh): void {
  820. this._scene._cachedMaterial = this;
  821. if (mesh) {
  822. this._scene._cachedVisibility = mesh.visibility;
  823. } else {
  824. this._scene._cachedVisibility = 1;
  825. }
  826. if (this._onBindObservable && mesh) {
  827. this._onBindObservable.notifyObservers(mesh);
  828. }
  829. if (this.disableDepthWrite) {
  830. var engine = this._scene.getEngine();
  831. this._cachedDepthWriteState = engine.getDepthWrite();
  832. engine.setDepthWrite(false);
  833. }
  834. if (this.disableColorWrite) {
  835. var engine = this._scene.getEngine();
  836. this._cachedColorWriteState = engine.getColorWrite();
  837. engine.setColorWrite(false);
  838. }
  839. if (this.depthFunction !== 0) {
  840. var engine = this._scene.getEngine();
  841. this._cachedDepthFunctionState = engine.getDepthFunction() || 0;
  842. engine.setDepthFunction(this.depthFunction);
  843. }
  844. }
  845. /**
  846. * Unbinds the material from the mesh
  847. */
  848. public unbind(): void {
  849. if (this._onUnBindObservable) {
  850. this._onUnBindObservable.notifyObservers(this);
  851. }
  852. if (this.depthFunction !== 0) {
  853. var engine = this._scene.getEngine();
  854. engine.setDepthFunction(this._cachedDepthFunctionState);
  855. }
  856. if (this.disableDepthWrite) {
  857. var engine = this._scene.getEngine();
  858. engine.setDepthWrite(this._cachedDepthWriteState);
  859. }
  860. if (this.disableColorWrite) {
  861. var engine = this._scene.getEngine();
  862. engine.setColorWrite(this._cachedColorWriteState);
  863. }
  864. }
  865. /**
  866. * Gets the active textures from the material
  867. * @returns an array of textures
  868. */
  869. public getActiveTextures(): BaseTexture[] {
  870. return [];
  871. }
  872. /**
  873. * Specifies if the material uses a texture
  874. * @param texture defines the texture to check against the material
  875. * @returns a boolean specifying if the material uses the texture
  876. */
  877. public hasTexture(texture: BaseTexture): boolean {
  878. return false;
  879. }
  880. /**
  881. * Makes a duplicate of the material, and gives it a new name
  882. * @param name defines the new name for the duplicated material
  883. * @returns the cloned material
  884. */
  885. public clone(name: string): Nullable<Material> {
  886. return null;
  887. }
  888. /**
  889. * Gets the meshes bound to the material
  890. * @returns an array of meshes bound to the material
  891. */
  892. public getBindedMeshes(): AbstractMesh[] {
  893. if (this.meshMap) {
  894. var result = new Array<AbstractMesh>();
  895. for (let meshId in this.meshMap) {
  896. const mesh = this.meshMap[meshId];
  897. if (mesh) {
  898. result.push(mesh);
  899. }
  900. }
  901. return result;
  902. }
  903. else {
  904. const meshes = this._scene.meshes;
  905. return meshes.filter((mesh) => mesh.material === this);
  906. }
  907. }
  908. /**
  909. * Force shader compilation
  910. * @param mesh defines the mesh associated with this material
  911. * @param onCompiled defines a function to execute once the material is compiled
  912. * @param options defines the options to configure the compilation
  913. * @param onError defines a function to execute if the material fails compiling
  914. */
  915. public forceCompilation(mesh: AbstractMesh, onCompiled?: (material: Material) => void, options?: Partial<IMaterialCompilationOptions>, onError?: (reason: string) => void): void {
  916. let localOptions = {
  917. clipPlane: false,
  918. useInstances: false,
  919. ...options
  920. };
  921. var scene = this.getScene();
  922. let currentHotSwapingState = this.allowShaderHotSwapping;
  923. this.allowShaderHotSwapping = false; // Turned off to let us evaluate the real compilation state
  924. var checkReady = () => {
  925. if (!this._scene || !this._scene.getEngine()) {
  926. return;
  927. }
  928. var clipPlaneState = scene.clipPlane;
  929. if (localOptions.clipPlane) {
  930. scene.clipPlane = new Plane(0, 0, 0, 1);
  931. }
  932. if (this._storeEffectOnSubMeshes) {
  933. var allDone = true, lastError = null;
  934. if (mesh.subMeshes) {
  935. let tempSubMesh = new SubMesh(0, 0, 0, 0, 0, mesh, undefined, false, false);
  936. if (tempSubMesh._materialDefines) {
  937. tempSubMesh._materialDefines._renderId = -1;
  938. }
  939. if (!this.isReadyForSubMesh(mesh, tempSubMesh, localOptions.useInstances)) {
  940. if (tempSubMesh.effect && tempSubMesh.effect.getCompilationError() && tempSubMesh.effect.allFallbacksProcessed()) {
  941. lastError = tempSubMesh.effect.getCompilationError();
  942. } else {
  943. allDone = false;
  944. setTimeout(checkReady, 16);
  945. }
  946. }
  947. }
  948. if (allDone) {
  949. this.allowShaderHotSwapping = currentHotSwapingState;
  950. if (lastError) {
  951. if (onError) {
  952. onError(lastError);
  953. }
  954. }
  955. if (onCompiled) {
  956. onCompiled(this);
  957. }
  958. }
  959. } else {
  960. if (this.isReady()) {
  961. this.allowShaderHotSwapping = currentHotSwapingState;
  962. if (onCompiled) {
  963. onCompiled(this);
  964. }
  965. }
  966. else {
  967. setTimeout(checkReady, 16);
  968. }
  969. }
  970. if (localOptions.clipPlane) {
  971. scene.clipPlane = clipPlaneState;
  972. }
  973. };
  974. checkReady();
  975. }
  976. /**
  977. * Force shader compilation
  978. * @param mesh defines the mesh that will use this material
  979. * @param options defines additional options for compiling the shaders
  980. * @returns a promise that resolves when the compilation completes
  981. */
  982. public forceCompilationAsync(mesh: AbstractMesh, options?: Partial<IMaterialCompilationOptions>): Promise<void> {
  983. return new Promise((resolve, reject) => {
  984. this.forceCompilation(mesh, () => {
  985. resolve();
  986. }, options, (reason) => {
  987. reject(reason);
  988. });
  989. });
  990. }
  991. private static readonly _AllDirtyCallBack = (defines: MaterialDefines) => defines.markAllAsDirty();
  992. private static readonly _ImageProcessingDirtyCallBack = (defines: MaterialDefines) => defines.markAsImageProcessingDirty();
  993. private static readonly _TextureDirtyCallBack = (defines: MaterialDefines) => defines.markAsTexturesDirty();
  994. private static readonly _FresnelDirtyCallBack = (defines: MaterialDefines) => defines.markAsFresnelDirty();
  995. private static readonly _MiscDirtyCallBack = (defines: MaterialDefines) => defines.markAsMiscDirty();
  996. private static readonly _LightsDirtyCallBack = (defines: MaterialDefines) => defines.markAsLightDirty();
  997. private static readonly _AttributeDirtyCallBack = (defines: MaterialDefines) => defines.markAsAttributesDirty();
  998. private static _FresnelAndMiscDirtyCallBack = (defines: MaterialDefines) => {
  999. Material._FresnelDirtyCallBack(defines);
  1000. Material._MiscDirtyCallBack(defines);
  1001. }
  1002. private static _TextureAndMiscDirtyCallBack = (defines: MaterialDefines) => {
  1003. Material._TextureDirtyCallBack(defines);
  1004. Material._MiscDirtyCallBack(defines);
  1005. }
  1006. private static readonly _DirtyCallbackArray: Array<(defines: MaterialDefines) => void> = [];
  1007. private static readonly _RunDirtyCallBacks = (defines: MaterialDefines) => {
  1008. for (const cb of Material._DirtyCallbackArray) {
  1009. cb(defines);
  1010. }
  1011. }
  1012. /**
  1013. * Marks a define in the material to indicate that it needs to be re-computed
  1014. * @param flag defines a flag used to determine which parts of the material have to be marked as dirty
  1015. */
  1016. public markAsDirty(flag: number): void {
  1017. if (this.getScene().blockMaterialDirtyMechanism) {
  1018. return;
  1019. }
  1020. Material._DirtyCallbackArray.length = 0;
  1021. if (flag & Material.TextureDirtyFlag) {
  1022. Material._DirtyCallbackArray.push(Material._TextureDirtyCallBack);
  1023. }
  1024. if (flag & Material.LightDirtyFlag) {
  1025. Material._DirtyCallbackArray.push(Material._LightsDirtyCallBack);
  1026. }
  1027. if (flag & Material.FresnelDirtyFlag) {
  1028. Material._DirtyCallbackArray.push(Material._FresnelDirtyCallBack);
  1029. }
  1030. if (flag & Material.AttributesDirtyFlag) {
  1031. Material._DirtyCallbackArray.push(Material._AttributeDirtyCallBack);
  1032. }
  1033. if (flag & Material.MiscDirtyFlag) {
  1034. Material._DirtyCallbackArray.push(Material._MiscDirtyCallBack);
  1035. }
  1036. if (Material._DirtyCallbackArray.length) {
  1037. this._markAllSubMeshesAsDirty(Material._RunDirtyCallBacks);
  1038. }
  1039. this.getScene().resetCachedMaterial();
  1040. }
  1041. /**
  1042. * Marks all submeshes of a material to indicate that their material defines need to be re-calculated
  1043. * @param func defines a function which checks material defines against the submeshes
  1044. */
  1045. protected _markAllSubMeshesAsDirty(func: (defines: MaterialDefines) => void) {
  1046. if (this.getScene().blockMaterialDirtyMechanism) {
  1047. return;
  1048. }
  1049. const meshes = this.getScene().meshes;
  1050. for (var mesh of meshes) {
  1051. if (!mesh.subMeshes) {
  1052. continue;
  1053. }
  1054. for (var subMesh of mesh.subMeshes) {
  1055. if (subMesh.getMaterial() !== this) {
  1056. continue;
  1057. }
  1058. if (!subMesh._materialDefines) {
  1059. continue;
  1060. }
  1061. func(subMesh._materialDefines);
  1062. }
  1063. }
  1064. }
  1065. protected _markSceneDeferredDirty() {
  1066. if (this.getScene().blockMaterialDirtyMechanism) {
  1067. return;
  1068. }
  1069. const prePassRenderer = this.getScene().enablePrePassRenderer();
  1070. if (prePassRenderer) {
  1071. prePassRenderer.markAsDirty();
  1072. }
  1073. }
  1074. /**
  1075. * Indicates that we need to re-calculated for all submeshes
  1076. */
  1077. protected _markAllSubMeshesAsAllDirty() {
  1078. this._markAllSubMeshesAsDirty(Material._AllDirtyCallBack);
  1079. }
  1080. /**
  1081. * Indicates that image processing needs to be re-calculated for all submeshes
  1082. */
  1083. protected _markAllSubMeshesAsImageProcessingDirty() {
  1084. this._markAllSubMeshesAsDirty(Material._ImageProcessingDirtyCallBack);
  1085. }
  1086. /**
  1087. * Indicates that textures need to be re-calculated for all submeshes
  1088. */
  1089. protected _markAllSubMeshesAsTexturesDirty() {
  1090. this._markAllSubMeshesAsDirty(Material._TextureDirtyCallBack);
  1091. }
  1092. /**
  1093. * Indicates that fresnel needs to be re-calculated for all submeshes
  1094. */
  1095. protected _markAllSubMeshesAsFresnelDirty() {
  1096. this._markAllSubMeshesAsDirty(Material._FresnelDirtyCallBack);
  1097. }
  1098. /**
  1099. * Indicates that fresnel and misc need to be re-calculated for all submeshes
  1100. */
  1101. protected _markAllSubMeshesAsFresnelAndMiscDirty() {
  1102. this._markAllSubMeshesAsDirty(Material._FresnelAndMiscDirtyCallBack);
  1103. }
  1104. /**
  1105. * Indicates that lights need to be re-calculated for all submeshes
  1106. */
  1107. protected _markAllSubMeshesAsLightsDirty() {
  1108. this._markAllSubMeshesAsDirty(Material._LightsDirtyCallBack);
  1109. }
  1110. /**
  1111. * Indicates that attributes need to be re-calculated for all submeshes
  1112. */
  1113. protected _markAllSubMeshesAsAttributesDirty() {
  1114. this._markAllSubMeshesAsDirty(Material._AttributeDirtyCallBack);
  1115. }
  1116. /**
  1117. * Indicates that misc needs to be re-calculated for all submeshes
  1118. */
  1119. protected _markAllSubMeshesAsMiscDirty() {
  1120. this._markAllSubMeshesAsDirty(Material._MiscDirtyCallBack);
  1121. }
  1122. /**
  1123. * Indicates that textures and misc need to be re-calculated for all submeshes
  1124. */
  1125. protected _markAllSubMeshesAsTexturesAndMiscDirty() {
  1126. this._markAllSubMeshesAsDirty(Material._TextureAndMiscDirtyCallBack);
  1127. }
  1128. /**
  1129. * Disposes the material
  1130. * @param forceDisposeEffect specifies if effects should be forcefully disposed
  1131. * @param forceDisposeTextures specifies if textures should be forcefully disposed
  1132. * @param notBoundToMesh specifies if the material that is being disposed is known to be not bound to any mesh
  1133. */
  1134. public dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean, notBoundToMesh?: boolean): void {
  1135. const scene = this.getScene();
  1136. // Animations
  1137. scene.stopAnimation(this);
  1138. scene.freeProcessedMaterials();
  1139. // Remove from scene
  1140. scene.removeMaterial(this);
  1141. if (notBoundToMesh !== true) {
  1142. // Remove from meshes
  1143. if (this.meshMap) {
  1144. for (let meshId in this.meshMap) {
  1145. const mesh = this.meshMap[meshId];
  1146. if (mesh) {
  1147. mesh.material = null; // will set the entry in the map to undefined
  1148. this.releaseVertexArrayObject(mesh, forceDisposeEffect);
  1149. }
  1150. }
  1151. }
  1152. else {
  1153. const meshes = scene.meshes;
  1154. for (let mesh of meshes) {
  1155. if (mesh.material === this && !(mesh as InstancedMesh).sourceMesh) {
  1156. mesh.material = null;
  1157. this.releaseVertexArrayObject(mesh, forceDisposeEffect);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. this._uniformBuffer.dispose();
  1163. // Shader are kept in cache for further use but we can get rid of this by using forceDisposeEffect
  1164. if (forceDisposeEffect && this._effect) {
  1165. if (!this._storeEffectOnSubMeshes) {
  1166. this._effect.dispose();
  1167. }
  1168. this._effect = null;
  1169. }
  1170. // Callback
  1171. this.onDisposeObservable.notifyObservers(this);
  1172. this.onDisposeObservable.clear();
  1173. if (this._onBindObservable) {
  1174. this._onBindObservable.clear();
  1175. }
  1176. if (this._onUnBindObservable) {
  1177. this._onUnBindObservable.clear();
  1178. }
  1179. if (this._onEffectCreatedObservable) {
  1180. this._onEffectCreatedObservable.clear();
  1181. }
  1182. }
  1183. /** @hidden */
  1184. private releaseVertexArrayObject(mesh: AbstractMesh, forceDisposeEffect?: boolean) {
  1185. if ((<Mesh>mesh).geometry) {
  1186. var geometry = <Geometry>((<Mesh>mesh).geometry);
  1187. if (this._storeEffectOnSubMeshes) {
  1188. for (var subMesh of mesh.subMeshes) {
  1189. geometry._releaseVertexArrayObject(subMesh._materialEffect);
  1190. if (forceDisposeEffect && subMesh._materialEffect) {
  1191. subMesh._materialEffect.dispose();
  1192. }
  1193. }
  1194. } else {
  1195. geometry._releaseVertexArrayObject(this._effect);
  1196. }
  1197. }
  1198. }
  1199. /**
  1200. * Serializes this material
  1201. * @returns the serialized material object
  1202. */
  1203. public serialize(): any {
  1204. return SerializationHelper.Serialize(this);
  1205. }
  1206. /**
  1207. * Creates a material from parsed material data
  1208. * @param parsedMaterial defines parsed material data
  1209. * @param scene defines the hosting scene
  1210. * @param rootUrl defines the root URL to use to load textures
  1211. * @returns a new material
  1212. */
  1213. public static Parse(parsedMaterial: any, scene: Scene, rootUrl: string): Nullable<Material> {
  1214. if (!parsedMaterial.customType) {
  1215. parsedMaterial.customType = "BABYLON.StandardMaterial";
  1216. }
  1217. else if (parsedMaterial.customType === "BABYLON.PBRMaterial" && parsedMaterial.overloadedAlbedo) {
  1218. parsedMaterial.customType = "BABYLON.LegacyPBRMaterial";
  1219. if (!BABYLON.LegacyPBRMaterial) {
  1220. Logger.Error("Your scene is trying to load a legacy version of the PBRMaterial, please, include it from the materials library.");
  1221. return null;
  1222. }
  1223. }
  1224. var materialType = Tools.Instantiate(parsedMaterial.customType);
  1225. return materialType.Parse(parsedMaterial, scene, rootUrl);
  1226. }
  1227. }