babylon.baseTexture.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.hasAlpha = false;
  12. BABYLON.BaseTexture.prototype.level = 1;
  13. BABYLON.BaseTexture.prototype._texture = null;
  14. BABYLON.BaseTexture.prototype.onDispose = null;
  15. // Properties
  16. BABYLON.BaseTexture.prototype.getInternalTexture = function () {
  17. return this._texture;
  18. };
  19. BABYLON.BaseTexture.prototype.isReady = function () {
  20. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  21. return true;
  22. }
  23. if (this._texture) {
  24. return this._texture.isReady;
  25. }
  26. return false;
  27. };
  28. // Methods
  29. BABYLON.BaseTexture.prototype.getSize = function () {
  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. BABYLON.BaseTexture.prototype.getBaseSize = function () {
  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. BABYLON.BaseTexture.prototype._getFromCache = function (url, noMipmap) {
  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. BABYLON.BaseTexture.prototype.delayLoad = function () {
  58. };
  59. BABYLON.BaseTexture.prototype.releaseInternalTexture = function () {
  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. BABYLON.BaseTexture.prototype.dispose = function () {
  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. })();