baseTexture.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. import { serialize, SerializationHelper, serializeAsTexture, expandToProperty } from "../../Misc/decorators";
  2. import { Observer, Observable } from "../../Misc/observable";
  3. import { Nullable } from "../../types";
  4. import { Scene } from "../../scene";
  5. import { Matrix } from "../../Maths/math.vector";
  6. import { EngineStore } from "../../Engines/engineStore";
  7. import { InternalTexture } from "../../Materials/Textures/internalTexture";
  8. import { Constants } from "../../Engines/constants";
  9. import { IAnimatable } from '../../Animations/animatable.interface';
  10. import { GUID } from '../../Misc/guid';
  11. import { ISize, Size } from '../../Maths/math.size';
  12. import "../../Misc/fileTools";
  13. declare type Animation = import("../../Animations/animation").Animation;
  14. /**
  15. * Base class of all the textures in babylon.
  16. * It groups all the common properties the materials, post process, lights... might need
  17. * in order to make a correct use of the texture.
  18. */
  19. export class BaseTexture implements IAnimatable {
  20. /**
  21. * Default anisotropic filtering level for the application.
  22. * It is set to 4 as a good tradeoff between perf and quality.
  23. */
  24. public static DEFAULT_ANISOTROPIC_FILTERING_LEVEL = 4;
  25. /**
  26. * Gets or sets the unique id of the texture
  27. */
  28. @serialize()
  29. public uniqueId: number;
  30. /**
  31. * Define the name of the texture.
  32. */
  33. @serialize()
  34. public name: string;
  35. /**
  36. * Gets or sets an object used to store user defined information.
  37. */
  38. @serialize()
  39. public metadata: any = null;
  40. /**
  41. * For internal use only. Please do not use.
  42. */
  43. public reservedDataStore: any = null;
  44. @serialize("hasAlpha")
  45. private _hasAlpha = false;
  46. /**
  47. * Define if the texture is having a usable alpha value (can be use for transparency or glossiness for instance).
  48. */
  49. public set hasAlpha(value: boolean) {
  50. if (this._hasAlpha === value) {
  51. return;
  52. }
  53. this._hasAlpha = value;
  54. if (this._scene) {
  55. this._scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag | Constants.MATERIAL_MiscDirtyFlag);
  56. }
  57. }
  58. public get hasAlpha(): boolean {
  59. return this._hasAlpha;
  60. }
  61. /**
  62. * Defines if the alpha value should be determined via the rgb values.
  63. * If true the luminance of the pixel might be used to find the corresponding alpha value.
  64. */
  65. @serialize()
  66. public getAlphaFromRGB = false;
  67. /**
  68. * Intensity or strength of the texture.
  69. * It is commonly used by materials to fine tune the intensity of the texture
  70. */
  71. @serialize()
  72. public level = 1;
  73. /**
  74. * Define the UV chanel to use starting from 0 and defaulting to 0.
  75. * This is part of the texture as textures usually maps to one uv set.
  76. */
  77. @serialize()
  78. public coordinatesIndex = 0;
  79. @serialize("coordinatesMode")
  80. private _coordinatesMode = Constants.TEXTURE_EXPLICIT_MODE;
  81. /**
  82. * How a texture is mapped.
  83. *
  84. * | Value | Type | Description |
  85. * | ----- | ----------------------------------- | ----------- |
  86. * | 0 | EXPLICIT_MODE | |
  87. * | 1 | SPHERICAL_MODE | |
  88. * | 2 | PLANAR_MODE | |
  89. * | 3 | CUBIC_MODE | |
  90. * | 4 | PROJECTION_MODE | |
  91. * | 5 | SKYBOX_MODE | |
  92. * | 6 | INVCUBIC_MODE | |
  93. * | 7 | EQUIRECTANGULAR_MODE | |
  94. * | 8 | FIXED_EQUIRECTANGULAR_MODE | |
  95. * | 9 | FIXED_EQUIRECTANGULAR_MIRRORED_MODE | |
  96. */
  97. public set coordinatesMode(value: number) {
  98. if (this._coordinatesMode === value) {
  99. return;
  100. }
  101. this._coordinatesMode = value;
  102. if (this._scene) {
  103. this._scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
  104. }
  105. }
  106. public get coordinatesMode(): number {
  107. return this._coordinatesMode;
  108. }
  109. /**
  110. * | Value | Type | Description |
  111. * | ----- | ------------------ | ----------- |
  112. * | 0 | CLAMP_ADDRESSMODE | |
  113. * | 1 | WRAP_ADDRESSMODE | |
  114. * | 2 | MIRROR_ADDRESSMODE | |
  115. */
  116. @serialize()
  117. public wrapU = Constants.TEXTURE_WRAP_ADDRESSMODE;
  118. /**
  119. * | Value | Type | Description |
  120. * | ----- | ------------------ | ----------- |
  121. * | 0 | CLAMP_ADDRESSMODE | |
  122. * | 1 | WRAP_ADDRESSMODE | |
  123. * | 2 | MIRROR_ADDRESSMODE | |
  124. */
  125. @serialize()
  126. public wrapV = Constants.TEXTURE_WRAP_ADDRESSMODE;
  127. /**
  128. * | Value | Type | Description |
  129. * | ----- | ------------------ | ----------- |
  130. * | 0 | CLAMP_ADDRESSMODE | |
  131. * | 1 | WRAP_ADDRESSMODE | |
  132. * | 2 | MIRROR_ADDRESSMODE | |
  133. */
  134. @serialize()
  135. public wrapR = Constants.TEXTURE_WRAP_ADDRESSMODE;
  136. /**
  137. * With compliant hardware and browser (supporting anisotropic filtering)
  138. * this defines the level of anisotropic filtering in the texture.
  139. * The higher the better but the slower. This defaults to 4 as it seems to be the best tradeoff.
  140. */
  141. @serialize()
  142. public anisotropicFilteringLevel = BaseTexture.DEFAULT_ANISOTROPIC_FILTERING_LEVEL;
  143. /**
  144. * Define if the texture is a cube texture or if false a 2d texture.
  145. */
  146. @serialize()
  147. public get isCube(): boolean {
  148. if (!this._texture) {
  149. return false;
  150. }
  151. return this._texture.isCube;
  152. }
  153. public set isCube(value: boolean) {
  154. if (!this._texture) {
  155. return;
  156. }
  157. this._texture.isCube = value;
  158. }
  159. /**
  160. * Define if the texture is a 3d texture (webgl 2) or if false a 2d texture.
  161. */
  162. @serialize()
  163. public get is3D(): boolean {
  164. if (!this._texture) {
  165. return false;
  166. }
  167. return this._texture.is3D;
  168. }
  169. public set is3D(value: boolean) {
  170. if (!this._texture) {
  171. return;
  172. }
  173. this._texture.is3D = value;
  174. }
  175. /**
  176. * Define if the texture is a 2d array texture (webgl 2) or if false a 2d texture.
  177. */
  178. @serialize()
  179. public get is2DArray(): boolean {
  180. if (!this._texture) {
  181. return false;
  182. }
  183. return this._texture.is2DArray;
  184. }
  185. public set is2DArray(value: boolean) {
  186. if (!this._texture) {
  187. return;
  188. }
  189. this._texture.is2DArray = value;
  190. }
  191. /**
  192. * Define if the texture contains data in gamma space (most of the png/jpg aside bump).
  193. * HDR texture are usually stored in linear space.
  194. * This only impacts the PBR and Background materials
  195. */
  196. @serialize()
  197. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  198. public gammaSpace = true;
  199. /**
  200. * Gets or sets whether or not the texture contains RGBD data.
  201. */
  202. public get isRGBD(): boolean {
  203. return this._texture != null && this._texture._isRGBD;
  204. }
  205. public set isRGBD(value: boolean) {
  206. if (this._texture) { this._texture._isRGBD = value; }
  207. }
  208. /**
  209. * Is Z inverted in the texture (useful in a cube texture).
  210. */
  211. @serialize()
  212. public invertZ = false;
  213. /**
  214. * Are mip maps generated for this texture or not.
  215. */
  216. public get noMipmap(): boolean {
  217. return false;
  218. }
  219. /**
  220. * @hidden
  221. */
  222. @serialize()
  223. public lodLevelInAlpha = false;
  224. /**
  225. * With prefiltered texture, defined the offset used during the prefiltering steps.
  226. */
  227. @serialize()
  228. public get lodGenerationOffset(): number {
  229. if (this._texture) { return this._texture._lodGenerationOffset; }
  230. return 0.0;
  231. }
  232. public set lodGenerationOffset(value: number) {
  233. if (this._texture) { this._texture._lodGenerationOffset = value; }
  234. }
  235. /**
  236. * With prefiltered texture, defined the scale used during the prefiltering steps.
  237. */
  238. @serialize()
  239. public get lodGenerationScale(): number {
  240. if (this._texture) { return this._texture._lodGenerationScale; }
  241. return 0.0;
  242. }
  243. public set lodGenerationScale(value: number) {
  244. if (this._texture) { this._texture._lodGenerationScale = value; }
  245. }
  246. /**
  247. * With prefiltered texture, defined if the specular generation is based on a linear ramp.
  248. * By default we are using a log2 of the linear roughness helping to keep a better resolution for
  249. * average roughness values.
  250. */
  251. @serialize()
  252. public get linearSpecularLOD(): boolean {
  253. if (this._texture) { return this._texture._linearSpecularLOD; }
  254. return false;
  255. }
  256. public set linearSpecularLOD(value: boolean) {
  257. if (this._texture) { this._texture._linearSpecularLOD = value; }
  258. }
  259. /**
  260. * In case a better definition than spherical harmonics is required for the diffuse part of the environment.
  261. * You can set the irradiance texture to rely on a texture instead of the spherical approach.
  262. * This texture need to have the same characteristics than its parent (Cube vs 2d, coordinates mode, Gamma/Linear, RGBD).
  263. */
  264. @serializeAsTexture()
  265. public get irradianceTexture(): Nullable<BaseTexture> {
  266. if (this._texture) { return this._texture._irradianceTexture; }
  267. return null;
  268. }
  269. public set irradianceTexture(value: Nullable<BaseTexture>) {
  270. if (this._texture) { this._texture._irradianceTexture = value; }
  271. }
  272. /**
  273. * Define if the texture is a render target.
  274. */
  275. @serialize()
  276. public isRenderTarget = false;
  277. /**
  278. * Define the unique id of the texture in the scene.
  279. */
  280. public get uid(): string {
  281. if (!this._uid) {
  282. this._uid = GUID.RandomId();
  283. }
  284. return this._uid;
  285. }
  286. /**
  287. * Return a string representation of the texture.
  288. * @returns the texture as a string
  289. */
  290. public toString(): string {
  291. return this.name;
  292. }
  293. /**
  294. * Get the class name of the texture.
  295. * @returns "BaseTexture"
  296. */
  297. public getClassName(): string {
  298. return "BaseTexture";
  299. }
  300. /**
  301. * Define the list of animation attached to the texture.
  302. */
  303. public animations = new Array<Animation>();
  304. /**
  305. * An event triggered when the texture is disposed.
  306. */
  307. public onDisposeObservable = new Observable<BaseTexture>();
  308. private _onDisposeObserver: Nullable<Observer<BaseTexture>> = null;
  309. /**
  310. * Callback triggered when the texture has been disposed.
  311. * Kept for back compatibility, you can use the onDisposeObservable instead.
  312. */
  313. public set onDispose(callback: () => void) {
  314. if (this._onDisposeObserver) {
  315. this.onDisposeObservable.remove(this._onDisposeObserver);
  316. }
  317. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  318. }
  319. /**
  320. * Define the current state of the loading sequence when in delayed load mode.
  321. */
  322. public delayLoadState = Constants.DELAYLOADSTATE_NONE;
  323. private _scene: Nullable<Scene> = null;
  324. /** @hidden */
  325. public _texture: Nullable<InternalTexture> = null;
  326. private _uid: Nullable<string> = null;
  327. /**
  328. * Define if the texture is preventinga material to render or not.
  329. * If not and the texture is not ready, the engine will use a default black texture instead.
  330. */
  331. public get isBlocking(): boolean {
  332. return true;
  333. }
  334. /**
  335. * Instantiates a new BaseTexture.
  336. * Base class of all the textures in babylon.
  337. * It groups all the common properties the materials, post process, lights... might need
  338. * in order to make a correct use of the texture.
  339. * @param scene Define the scene the texture blongs to
  340. */
  341. constructor(scene: Nullable<Scene>) {
  342. this._scene = scene || EngineStore.LastCreatedScene;
  343. if (this._scene) {
  344. this.uniqueId = this._scene.getUniqueId();
  345. this._scene.addTexture(this);
  346. }
  347. this._uid = null;
  348. }
  349. /**
  350. * Get the scene the texture belongs to.
  351. * @returns the scene or null if undefined
  352. */
  353. public getScene(): Nullable<Scene> {
  354. return this._scene;
  355. }
  356. /**
  357. * Get the texture transform matrix used to offset tile the texture for istance.
  358. * @returns the transformation matrix
  359. */
  360. public getTextureMatrix(): Matrix {
  361. return <Matrix>Matrix.IdentityReadOnly;
  362. }
  363. /**
  364. * Get the texture reflection matrix used to rotate/transform the reflection.
  365. * @returns the reflection matrix
  366. */
  367. public getReflectionTextureMatrix(): Matrix {
  368. return <Matrix>Matrix.IdentityReadOnly;
  369. }
  370. /**
  371. * Get the underlying lower level texture from Babylon.
  372. * @returns the insternal texture
  373. */
  374. public getInternalTexture(): Nullable<InternalTexture> {
  375. return this._texture;
  376. }
  377. /**
  378. * Get if the texture is ready to be consumed (either it is ready or it is not blocking)
  379. * @returns true if ready or not blocking
  380. */
  381. public isReadyOrNotBlocking(): boolean {
  382. return !this.isBlocking || this.isReady();
  383. }
  384. /**
  385. * Get if the texture is ready to be used (downloaded, converted, mip mapped...).
  386. * @returns true if fully ready
  387. */
  388. public isReady(): boolean {
  389. if (this.delayLoadState === Constants.DELAYLOADSTATE_NOTLOADED) {
  390. this.delayLoad();
  391. return false;
  392. }
  393. if (this._texture) {
  394. return this._texture.isReady;
  395. }
  396. return false;
  397. }
  398. private _cachedSize: ISize = Size.Zero();
  399. /**
  400. * Get the size of the texture.
  401. * @returns the texture size.
  402. */
  403. public getSize(): ISize {
  404. if (this._texture) {
  405. if (this._texture.width) {
  406. this._cachedSize.width = this._texture.width;
  407. this._cachedSize.height = this._texture.height;
  408. return this._cachedSize;
  409. }
  410. if (this._texture._size) {
  411. this._cachedSize.width = this._texture._size;
  412. this._cachedSize.height = this._texture._size;
  413. return this._cachedSize;
  414. }
  415. }
  416. return this._cachedSize;
  417. }
  418. /**
  419. * Get the base size of the texture.
  420. * It can be different from the size if the texture has been resized for POT for instance
  421. * @returns the base size
  422. */
  423. public getBaseSize(): ISize {
  424. if (!this.isReady() || !this._texture) {
  425. return Size.Zero();
  426. }
  427. if (this._texture._size) {
  428. return new Size(this._texture._size, this._texture._size);
  429. }
  430. return new Size(this._texture.baseWidth, this._texture.baseHeight);
  431. }
  432. /**
  433. * Update the sampling mode of the texture.
  434. * Default is Trilinear mode.
  435. *
  436. * | Value | Type | Description |
  437. * | ----- | ------------------ | ----------- |
  438. * | 1 | NEAREST_SAMPLINGMODE or NEAREST_NEAREST_MIPLINEAR | Nearest is: mag = nearest, min = nearest, mip = linear |
  439. * | 2 | BILINEAR_SAMPLINGMODE or LINEAR_LINEAR_MIPNEAREST | Bilinear is: mag = linear, min = linear, mip = nearest |
  440. * | 3 | TRILINEAR_SAMPLINGMODE or LINEAR_LINEAR_MIPLINEAR | Trilinear is: mag = linear, min = linear, mip = linear |
  441. * | 4 | NEAREST_NEAREST_MIPNEAREST | |
  442. * | 5 | NEAREST_LINEAR_MIPNEAREST | |
  443. * | 6 | NEAREST_LINEAR_MIPLINEAR | |
  444. * | 7 | NEAREST_LINEAR | |
  445. * | 8 | NEAREST_NEAREST | |
  446. * | 9 | LINEAR_NEAREST_MIPNEAREST | |
  447. * | 10 | LINEAR_NEAREST_MIPLINEAR | |
  448. * | 11 | LINEAR_LINEAR | |
  449. * | 12 | LINEAR_NEAREST | |
  450. *
  451. * > _mag_: magnification filter (close to the viewer)
  452. * > _min_: minification filter (far from the viewer)
  453. * > _mip_: filter used between mip map levels
  454. *@param samplingMode Define the new sampling mode of the texture
  455. */
  456. public updateSamplingMode(samplingMode: number): void {
  457. if (!this._texture) {
  458. return;
  459. }
  460. let scene = this.getScene();
  461. if (!scene) {
  462. return;
  463. }
  464. scene.getEngine().updateTextureSamplingMode(samplingMode, this._texture);
  465. }
  466. /**
  467. * Scales the texture if is `canRescale()`
  468. * @param ratio the resize factor we want to use to rescale
  469. */
  470. public scale(ratio: number): void {
  471. }
  472. /**
  473. * Get if the texture can rescale.
  474. */
  475. public get canRescale(): boolean {
  476. return false;
  477. }
  478. /** @hidden */
  479. public _getFromCache(url: Nullable<string>, noMipmap: boolean, sampling?: number, invertY?: boolean): Nullable<InternalTexture> {
  480. if (!this._scene) {
  481. return null;
  482. }
  483. var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
  484. for (var index = 0; index < texturesCache.length; index++) {
  485. var texturesCacheEntry = texturesCache[index];
  486. if (invertY === undefined || invertY === texturesCacheEntry.invertY) {
  487. if (texturesCacheEntry.url === url && texturesCacheEntry.generateMipMaps === !noMipmap) {
  488. if (!sampling || sampling === texturesCacheEntry.samplingMode) {
  489. texturesCacheEntry.incrementReferences();
  490. return texturesCacheEntry;
  491. }
  492. }
  493. }
  494. }
  495. return null;
  496. }
  497. /** @hidden */
  498. public _rebuild(): void {
  499. }
  500. /**
  501. * Triggers the load sequence in delayed load mode.
  502. */
  503. public delayLoad(): void {
  504. }
  505. /**
  506. * Clones the texture.
  507. * @returns the cloned texture
  508. */
  509. public clone(): Nullable<BaseTexture> {
  510. return null;
  511. }
  512. /**
  513. * Get the texture underlying type (INT, FLOAT...)
  514. */
  515. public get textureType(): number {
  516. if (!this._texture) {
  517. return Constants.TEXTURETYPE_UNSIGNED_INT;
  518. }
  519. return (this._texture.type !== undefined) ? this._texture.type : Constants.TEXTURETYPE_UNSIGNED_INT;
  520. }
  521. /**
  522. * Get the texture underlying format (RGB, RGBA...)
  523. */
  524. public get textureFormat(): number {
  525. if (!this._texture) {
  526. return Constants.TEXTUREFORMAT_RGBA;
  527. }
  528. return (this._texture.format !== undefined) ? this._texture.format : Constants.TEXTUREFORMAT_RGBA;
  529. }
  530. /**
  531. * Indicates that textures need to be re-calculated for all materials
  532. */
  533. protected _markAllSubMeshesAsTexturesDirty() {
  534. let scene = this.getScene();
  535. if (!scene) {
  536. return;
  537. }
  538. scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
  539. }
  540. /**
  541. * Reads the pixels stored in the webgl texture and returns them as an ArrayBuffer.
  542. * This will returns an RGBA array buffer containing either in values (0-255) or
  543. * float values (0-1) depending of the underlying buffer type.
  544. * @param faceIndex defines the face of the texture to read (in case of cube texture)
  545. * @param level defines the LOD level of the texture to read (in case of Mip Maps)
  546. * @param buffer defines a user defined buffer to fill with data (can be null)
  547. * @returns The Array buffer containing the pixels data.
  548. */
  549. public readPixels(faceIndex = 0, level = 0, buffer: Nullable<ArrayBufferView> = null): Nullable<ArrayBufferView> {
  550. if (!this._texture) {
  551. return null;
  552. }
  553. var size = this.getSize();
  554. var width = size.width;
  555. var height = size.height;
  556. let scene = this.getScene();
  557. if (!scene) {
  558. return null;
  559. }
  560. var engine = scene.getEngine();
  561. if (level != 0) {
  562. width = width / Math.pow(2, level);
  563. height = height / Math.pow(2, level);
  564. width = Math.round(width);
  565. height = Math.round(height);
  566. }
  567. if (this._texture.isCube) {
  568. return engine._readTexturePixels(this._texture, width, height, faceIndex, level, buffer);
  569. }
  570. return engine._readTexturePixels(this._texture, width, height, -1, level, buffer);
  571. }
  572. /**
  573. * Release and destroy the underlying lower level texture aka internalTexture.
  574. */
  575. public releaseInternalTexture(): void {
  576. if (this._texture) {
  577. this._texture.dispose();
  578. this._texture = null;
  579. }
  580. }
  581. /** @hidden */
  582. public get _lodTextureHigh(): Nullable<BaseTexture> {
  583. if (this._texture) {
  584. return this._texture._lodTextureHigh;
  585. }
  586. return null;
  587. }
  588. /** @hidden */
  589. public get _lodTextureMid(): Nullable<BaseTexture> {
  590. if (this._texture) {
  591. return this._texture._lodTextureMid;
  592. }
  593. return null;
  594. }
  595. /** @hidden */
  596. public get _lodTextureLow(): Nullable<BaseTexture> {
  597. if (this._texture) {
  598. return this._texture._lodTextureLow;
  599. }
  600. return null;
  601. }
  602. /**
  603. * Dispose the texture and release its associated resources.
  604. */
  605. public dispose(): void {
  606. if (this._scene) {
  607. // Animations
  608. if (this._scene.stopAnimation) {
  609. this._scene.stopAnimation(this);
  610. }
  611. // Remove from scene
  612. this._scene._removePendingData(this);
  613. var index = this._scene.textures.indexOf(this);
  614. if (index >= 0) {
  615. this._scene.textures.splice(index, 1);
  616. }
  617. this._scene.onTextureRemovedObservable.notifyObservers(this);
  618. }
  619. if (this._texture === undefined) {
  620. return;
  621. }
  622. // Release
  623. this.releaseInternalTexture();
  624. // Callback
  625. this.onDisposeObservable.notifyObservers(this);
  626. this.onDisposeObservable.clear();
  627. }
  628. /**
  629. * Serialize the texture into a JSON representation that can be parsed later on.
  630. * @returns the JSON representation of the texture
  631. */
  632. public serialize(): any {
  633. if (!this.name) {
  634. return null;
  635. }
  636. var serializationObject = SerializationHelper.Serialize(this);
  637. // Animations
  638. SerializationHelper.AppendSerializedAnimations(this, serializationObject);
  639. return serializationObject;
  640. }
  641. /**
  642. * Helper function to be called back once a list of texture contains only ready textures.
  643. * @param textures Define the list of textures to wait for
  644. * @param callback Define the callback triggered once the entire list will be ready
  645. */
  646. public static WhenAllReady(textures: BaseTexture[], callback: () => void): void {
  647. let numRemaining = textures.length;
  648. if (numRemaining === 0) {
  649. callback();
  650. return;
  651. }
  652. for (var i = 0; i < textures.length; i++) {
  653. var texture = textures[i];
  654. if (texture.isReady()) {
  655. if (--numRemaining === 0) {
  656. callback();
  657. }
  658. }
  659. else {
  660. var onLoadObservable = (texture as any).onLoadObservable as Observable<BaseTexture>;
  661. if (onLoadObservable) {
  662. let onLoadCallback = () => {
  663. onLoadObservable.removeCallback(onLoadCallback);
  664. if (--numRemaining === 0) {
  665. callback();
  666. }
  667. };
  668. onLoadObservable.add(onLoadCallback);
  669. }
  670. }
  671. }
  672. }
  673. }