baseTexture.ts 25 KB

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