babylon.baseTexture.js 3.3 KB

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