@galacean/engine-spine 4.2.3 → 4.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -5016,19 +5016,18 @@ function _async_to_generator(fn) {
5016
5016
  }
5017
5017
 
5018
5018
  var AssetManagerBase = /*#__PURE__*/ function() {
5019
- function AssetManagerBase(textureLoader, pathPrefix, downloader) {
5019
+ function AssetManagerBase(textureLoader, pathPrefix, downloader, cache) {
5020
5020
  if (pathPrefix === void 0) pathPrefix = "";
5021
5021
  if (downloader === void 0) downloader = new Downloader();
5022
+ if (cache === void 0) cache = new AssetCache();
5022
5023
  this.pathPrefix = "";
5023
- this.assets = {};
5024
- this.assetsRefCount = {};
5025
- this.assetsLoaded = {};
5026
5024
  this.errors = {};
5027
5025
  this.toLoad = 0;
5028
5026
  this.loaded = 0;
5029
5027
  this.textureLoader = textureLoader;
5030
5028
  this.pathPrefix = pathPrefix;
5031
5029
  this.downloader = downloader;
5030
+ this.cache = cache;
5032
5031
  }
5033
5032
  var _proto = AssetManagerBase.prototype;
5034
5033
  _proto.start = function start(path) {
@@ -5038,8 +5037,8 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5038
5037
  _proto.success = function success(callback, path, asset) {
5039
5038
  this.toLoad--;
5040
5039
  this.loaded++;
5041
- this.assets[path] = asset;
5042
- this.assetsRefCount[path] = (this.assetsRefCount[path] || 0) + 1;
5040
+ this.cache.assets[path] = asset;
5041
+ this.cache.assetsRefCount[path] = (this.cache.assetsRefCount[path] || 0) + 1;
5043
5042
  if (callback) callback(path, asset);
5044
5043
  };
5045
5044
  _proto.error = function error(callback, path, message) {
@@ -5072,7 +5071,7 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5072
5071
  if (error === void 0) error = function() {};
5073
5072
  path = this.start(path);
5074
5073
  if (this.reuseAssets(path, success, error)) return;
5075
- this.assetsLoaded[path] = new Promise(function(resolve, reject) {
5074
+ this.cache.assetsLoaded[path] = new Promise(function(resolve, reject) {
5076
5075
  _this.downloader.downloadBinary(path, function(data) {
5077
5076
  _this.success(success, path, data);
5078
5077
  resolve(data);
@@ -5100,7 +5099,7 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5100
5099
  if (error === void 0) error = function() {};
5101
5100
  path = this.start(path);
5102
5101
  if (this.reuseAssets(path, success, error)) return;
5103
- this.assetsLoaded[path] = new Promise(function(resolve, reject) {
5102
+ this.cache.assetsLoaded[path] = new Promise(function(resolve, reject) {
5104
5103
  _this.downloader.downloadJson(path, function(data) {
5105
5104
  _this.success(success, path, data);
5106
5105
  resolve(data);
@@ -5115,11 +5114,15 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5115
5114
  var _this = this;
5116
5115
  if (success === void 0) success = function() {};
5117
5116
  if (error === void 0) error = function() {};
5118
- var loadedStatus = this.assetsLoaded[path];
5117
+ var loadedStatus = this.cache.assetsLoaded[path];
5119
5118
  var alreadyExistsOrLoading = loadedStatus !== undefined;
5120
5119
  if (alreadyExistsOrLoading) {
5121
- loadedStatus.then(function(data) {
5122
- return _this.success(success, path, data);
5120
+ this.cache.assetsLoaded[path] = loadedStatus.then(function(data) {
5121
+ // necessary when user preloads an image into the cache.
5122
+ // texture loader is not avaiable in the cache, so we transform in GLTexture at first use
5123
+ data = _instanceof1(data, Image) || _instanceof1(data, ImageBitmap) ? _this.textureLoader(data) : data;
5124
+ _this.success(success, path, data);
5125
+ return data;
5123
5126
  }).catch(function(errorMsg) {
5124
5127
  return _this.error(error, path, errorMsg);
5125
5128
  });
@@ -5132,7 +5135,7 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5132
5135
  if (error === void 0) error = function() {};
5133
5136
  path = this.start(path);
5134
5137
  if (this.reuseAssets(path, success, error)) return;
5135
- this.assetsLoaded[path] = new Promise(function(resolve, reject) {
5138
+ this.cache.assetsLoaded[path] = new Promise(function(resolve, reject) {
5136
5139
  var isBrowser = !!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document);
5137
5140
  var isWebWorker = !isBrowser; // && typeof importScripts !== 'undefined';
5138
5141
  if (isWebWorker) {
@@ -5181,7 +5184,7 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5181
5184
  var parent = index >= 0 ? path.substring(0, index + 1) : "";
5182
5185
  path = this.start(path);
5183
5186
  if (this.reuseAssets(path, success, error)) return;
5184
- this.assetsLoaded[path] = new Promise(function(resolve, reject) {
5187
+ this.cache.assetsLoaded[path] = new Promise(function(resolve, reject) {
5185
5188
  _this.downloader.downloadText(path, function(atlasText) {
5186
5189
  try {
5187
5190
  var _loop = function() {
@@ -5196,7 +5199,7 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5196
5199
  }
5197
5200
  }, function(imagePath, message) {
5198
5201
  if (!abort) {
5199
- var errorMsg = "Couldn't load texture atlas " + path + " page image: " + imagePath;
5202
+ var errorMsg = "Couldn't load texture " + path + " page image: " + imagePath;
5200
5203
  _this.error(error, path, errorMsg);
5201
5204
  reject(errorMsg);
5202
5205
  }
@@ -5224,7 +5227,7 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5224
5227
  if (error === void 0) error = function() {};
5225
5228
  path = this.start(path);
5226
5229
  if (this.reuseAssets(path, success, error)) return;
5227
- this.assetsLoaded[path] = new Promise(function(resolve, reject) {
5230
+ this.cache.assetsLoaded[path] = new Promise(function(resolve, reject) {
5228
5231
  _this.downloader.downloadText(path, function(atlasText) {
5229
5232
  try {
5230
5233
  var atlas = new TextureAtlas(atlasText);
@@ -5333,33 +5336,36 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5333
5336
  });
5334
5337
  }).call(this);
5335
5338
  };
5339
+ _proto.setCache = function setCache(cache) {
5340
+ this.cache = cache;
5341
+ };
5336
5342
  _proto.get = function get(path) {
5337
- return this.assets[this.pathPrefix + path];
5343
+ return this.cache.assets[this.pathPrefix + path];
5338
5344
  };
5339
5345
  _proto.require = function require(path) {
5340
5346
  path = this.pathPrefix + path;
5341
- var asset = this.assets[path];
5347
+ var asset = this.cache.assets[path];
5342
5348
  if (asset) return asset;
5343
5349
  var error = this.errors[path];
5344
5350
  throw Error("Asset not found: " + path + (error ? "\n" + error : ""));
5345
5351
  };
5346
5352
  _proto.remove = function remove(path) {
5347
5353
  path = this.pathPrefix + path;
5348
- var asset = this.assets[path];
5354
+ var asset = this.cache.assets[path];
5349
5355
  if (asset.dispose) asset.dispose();
5350
- delete this.assets[path];
5351
- delete this.assetsRefCount[path];
5352
- delete this.assetsLoaded[path];
5356
+ delete this.cache.assets[path];
5357
+ delete this.cache.assetsRefCount[path];
5358
+ delete this.cache.assetsLoaded[path];
5353
5359
  return asset;
5354
5360
  };
5355
5361
  _proto.removeAll = function removeAll() {
5356
- for(var path in this.assets){
5357
- var asset = this.assets[path];
5362
+ for(var path in this.cache.assets){
5363
+ var asset = this.cache.assets[path];
5358
5364
  if (asset.dispose) asset.dispose();
5359
5365
  }
5360
- this.assets = {};
5361
- this.assetsLoaded = {};
5362
- this.assetsRefCount = {};
5366
+ this.cache.assets = {};
5367
+ this.cache.assetsLoaded = {};
5368
+ this.cache.assetsRefCount = {};
5363
5369
  };
5364
5370
  _proto.isLoadingComplete = function isLoadingComplete() {
5365
5371
  return this.toLoad == 0;
@@ -5375,7 +5381,7 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5375
5381
  };
5376
5382
  // dispose asset only if it's not used by others
5377
5383
  _proto.disposeAsset = function disposeAsset(path) {
5378
- if (--this.assetsRefCount[path] === 0) {
5384
+ if (--this.cache.assetsRefCount[path] === 0) {
5379
5385
  this.remove(path);
5380
5386
  }
5381
5387
  };
@@ -5387,6 +5393,44 @@ var AssetManagerBase = /*#__PURE__*/ function() {
5387
5393
  };
5388
5394
  return AssetManagerBase;
5389
5395
  }();
5396
+ var AssetCache = /*#__PURE__*/ function() {
5397
+ function AssetCache() {
5398
+ this.assets = {};
5399
+ this.assetsRefCount = {};
5400
+ this.assetsLoaded = {};
5401
+ }
5402
+ var _proto = AssetCache.prototype;
5403
+ _proto.addAsset = function addAsset(path, asset) {
5404
+ return _async_to_generator(function() {
5405
+ var _;
5406
+ return __generator(this, function(_state) {
5407
+ switch(_state.label){
5408
+ case 0:
5409
+ this.assetsLoaded[path] = Promise.resolve(asset);
5410
+ _ = this.assets;
5411
+ return [
5412
+ 4,
5413
+ asset
5414
+ ];
5415
+ case 1:
5416
+ _[path] = _state.sent();
5417
+ return [
5418
+ 2
5419
+ ];
5420
+ }
5421
+ });
5422
+ }).call(this);
5423
+ };
5424
+ AssetCache.getCache = function getCache(id) {
5425
+ var cache = AssetCache.AVAILABLE_CACHES.get(id);
5426
+ if (cache) return cache;
5427
+ var newCache = new AssetCache();
5428
+ AssetCache.AVAILABLE_CACHES.set(id, newCache);
5429
+ return newCache;
5430
+ };
5431
+ return AssetCache;
5432
+ }();
5433
+ AssetCache.AVAILABLE_CACHES = new Map();
5390
5434
  var Downloader = /*#__PURE__*/ function() {
5391
5435
  function Downloader() {
5392
5436
  this.callbacks = {};
@@ -11363,6 +11407,7 @@ SpineMaterial._spineFS = "\n #include <common>\n uniform sampler2D materia
11363
11407
  */ _this.defaultConfig = new SpineAnimationDefaultConfig(), /** @internal */ _this._subPrimitives = [], /** @internal */ _this._vertices = new Float32Array(), /** @internal */ _this._indices = new Uint16Array(), /** @internal */ _this._needResizeBuffer = false, /** @internal */ _this._vertexCount = 0, /** @internal */ _this._localBounds = new engine.BoundingBox(new engine.Vector3(Infinity, Infinity, Infinity), new engine.Vector3(-Infinity, -Infinity, -Infinity));
11364
11408
  var primitive = new engine.Primitive(_this._engine);
11365
11409
  _this._primitive = primitive;
11410
+ _this._primitive._addReferCount(1);
11366
11411
  primitive.addVertexElement(SpineAnimationRenderer._positionVertexElement);
11367
11412
  primitive.addVertexElement(SpineAnimationRenderer._lightColorVertexElement);
11368
11413
  primitive.addVertexElement(SpineAnimationRenderer._uvVertexElement);
@@ -11448,8 +11493,12 @@ SpineMaterial._spineFS = "\n #include <common>\n uniform sampler2D materia
11448
11493
  */ _proto._onDestroy = function _onDestroy() {
11449
11494
  this._clearMaterialCache();
11450
11495
  this._subPrimitives.length = 0;
11451
- this._primitive && this._primitive.destroy();
11452
- this._primitive = null;
11496
+ var primitive = this._primitive;
11497
+ if (primitive) {
11498
+ primitive._addReferCount(-1);
11499
+ primitive._destroy();
11500
+ this._primitive = null;
11501
+ }
11453
11502
  this._resource = null;
11454
11503
  this._skeleton = null;
11455
11504
  this._state = null;
@@ -12010,7 +12059,7 @@ for(var key in RendererObject){
12010
12059
  for(var key1 in LoaderObject){
12011
12060
  engine.Loader.registerClass(key1, LoaderObject[key1]);
12012
12061
  }
12013
- var version = "4.2.3";
12062
+ var version = "4.2.4";
12014
12063
  console.log("Galacean spine version: " + version);
12015
12064
 
12016
12065
  exports.AlphaTimeline = AlphaTimeline;
@@ -12018,6 +12067,7 @@ exports.Animation = Animation;
12018
12067
  exports.AnimationState = AnimationState;
12019
12068
  exports.AnimationStateAdapter = AnimationStateAdapter;
12020
12069
  exports.AnimationStateData = AnimationStateData;
12070
+ exports.AssetCache = AssetCache;
12021
12071
  exports.AssetManagerBase = AssetManagerBase;
12022
12072
  exports.AtlasAttachmentLoader = AtlasAttachmentLoader;
12023
12073
  exports.Attachment = Attachment;