babylon.baseTexture.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. module BABYLON {
  2. export class BaseTexture {
  3. public delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
  4. public hasAlpha = false;
  5. public level = 1;
  6. public isCube = false;
  7. public _texture: WebGLTexture;
  8. public onDispose: () => void;
  9. private _scene: Scene;
  10. constructor(scene: Scene) {
  11. this._scene = scene;
  12. this._scene.textures.push(this);
  13. }
  14. public getScene(): Scene {
  15. return this._scene;
  16. }
  17. public getInternalTexture(): WebGLTexture {
  18. return this._texture;
  19. }
  20. public isReady(): boolean {
  21. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  22. return true;
  23. }
  24. if (this._texture) {
  25. return this._texture.isReady;
  26. }
  27. return false;
  28. }
  29. public getSize(): ISize {
  30. if (this._texture._width) {
  31. return { width: this._texture._width, height: this._texture._height };
  32. }
  33. if (this._texture._size) {
  34. return { width: this._texture._size, height: this._texture._size };
  35. }
  36. return { width: 0, height: 0 };
  37. }
  38. public getBaseSize(): ISize {
  39. if (!this.isReady())
  40. return { width: 0, height: 0 };
  41. if (this._texture._size) {
  42. return { width: this._texture._size, height: this._texture._size };
  43. }
  44. return { width: this._texture._baseWidth, height: this._texture._baseHeight };
  45. }
  46. public _getFromCache(url: string, noMipmap: boolean): WebGLTexture {
  47. var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
  48. for (var index = 0; index < texturesCache.length; index++) {
  49. var texturesCacheEntry = texturesCache[index];
  50. if (texturesCacheEntry.url === url && texturesCacheEntry.noMipmap === noMipmap) {
  51. texturesCacheEntry.references++;
  52. return texturesCacheEntry;
  53. }
  54. }
  55. return null;
  56. }
  57. public delayLoad(): void {
  58. }
  59. public releaseInternalTexture(): void {
  60. if (!this._texture) {
  61. return;
  62. }
  63. var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
  64. this._texture.references--;
  65. // Final reference ?
  66. if (this._texture.references == 0) {
  67. var index = texturesCache.indexOf(this._texture);
  68. texturesCache.splice(index, 1);
  69. this._scene.getEngine()._releaseTexture(this._texture);
  70. delete this._texture;
  71. }
  72. }
  73. public dispose(): void {
  74. // Remove from scene
  75. var index = this._scene.textures.indexOf(this);
  76. if (index >= 0) {
  77. this._scene.textures.splice(index, 1);
  78. }
  79. if (this._texture === undefined) {
  80. return;
  81. }
  82. this.releaseInternalTexture();
  83. // Callback
  84. if (this.onDispose) {
  85. this.onDispose();
  86. }
  87. }
  88. }
  89. }