babylon.baseTexture.ts 3.9 KB

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