babylon.baseTexture.js 3.0 KB

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