mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
support deferred load texture in runtime
This commit is contained in:
@@ -37,7 +37,7 @@ function isSceneObj (json) {
|
||||
);
|
||||
}
|
||||
|
||||
function parseDepends (loader, item, asset, tdInfo, deferredLoadRawAssetsInRuntime) {
|
||||
function parseDepends (item, asset, tdInfo, deferredLoadRawAssetsInRuntime) {
|
||||
var uuidList = tdInfo.uuidList;
|
||||
var objList = tdInfo.uuidObjList;
|
||||
var propList = tdInfo.uuidPropList;
|
||||
@@ -77,6 +77,7 @@ function parseDepends (loader, item, asset, tdInfo, deferredLoadRawAssetsInRunti
|
||||
}
|
||||
else {
|
||||
depends = new Array(uuidList.length);
|
||||
|
||||
// declare depends assets
|
||||
for (i = 0; i < uuidList.length; i++) {
|
||||
dependUuid = uuidList[i];
|
||||
@@ -88,17 +89,17 @@ function parseDepends (loader, item, asset, tdInfo, deferredLoadRawAssetsInRunti
|
||||
_stillUseUrl: stillUseUrl[i]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// parse native
|
||||
if (asset._native && !asset.constructor.preventPreloadNativeObject) {
|
||||
depends.push({
|
||||
url: asset.nativeUrl,
|
||||
// For image, we should skip loader otherwise it will load a new texture
|
||||
skips: [ loader.id ],
|
||||
_owner: asset,
|
||||
_ownerProp: '_nativeAsset',
|
||||
});
|
||||
// parse native
|
||||
if (asset._native && !asset.constructor.preventPreloadNativeObject) {
|
||||
depends.push({
|
||||
url: asset.nativeUrl,
|
||||
// For image, we should skip loader otherwise it will load a new texture
|
||||
skips: [ 'Loader' ],
|
||||
_owner: asset,
|
||||
_ownerProp: '_nativeAsset',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return depends;
|
||||
@@ -268,7 +269,7 @@ function loadUuid (item, callback) {
|
||||
}
|
||||
|
||||
var deferredLoad = canDeferredLoad(asset, item, isScene);
|
||||
var depends = parseDepends(this, item, asset, tdInfo, deferredLoad);
|
||||
var depends = parseDepends(item, asset, tdInfo, deferredLoad);
|
||||
|
||||
cc.deserialize.Details.pool.put(tdInfo);
|
||||
|
||||
|
||||
@@ -66,7 +66,9 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
this._texture = texture;
|
||||
return;
|
||||
}
|
||||
this._refreshTexture(texture);
|
||||
if (this._texture !== texture) {
|
||||
this._refreshTexture(texture);
|
||||
}
|
||||
this._textureFilename = texture.url;
|
||||
}
|
||||
}
|
||||
@@ -261,65 +263,55 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
},
|
||||
|
||||
/*
|
||||
* !#en Sets the texture of the frame, the texture is retained automatically.
|
||||
* !#zh 设置使用的纹理实例,会被 retain。
|
||||
* !#en Sets the texture of the frame.
|
||||
* !#zh 设置使用的纹理实例。
|
||||
* @method _refreshTexture
|
||||
* @param {Texture2D} texture
|
||||
*/
|
||||
_refreshTexture: function (texture) {
|
||||
var self = this;
|
||||
if (self._texture !== texture) {
|
||||
var locLoaded = texture.loaded;
|
||||
this._texture = texture;
|
||||
function textureLoadedCallback () {
|
||||
if (!self._texture) {
|
||||
// clearTexture called while loading texture...
|
||||
return;
|
||||
}
|
||||
var w = texture.width, h = texture.height;
|
||||
this._texture = texture;
|
||||
|
||||
if (self._rotated && cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
|
||||
self._texture = _ccsg.Sprite.CanvasRenderCmd._createRotatedTexture(texture, self.getRect());
|
||||
self._rotated = false;
|
||||
w = self._texture.width;
|
||||
h = self._texture.height;
|
||||
self.setRect(cc.rect(0, 0, w, h));
|
||||
}
|
||||
function textureLoadedCallback () {
|
||||
if (!self._texture) {
|
||||
// clearTexture called while loading texture...
|
||||
return;
|
||||
}
|
||||
var w = texture.width, h = texture.height;
|
||||
|
||||
if (self._rect) {
|
||||
self._checkRect(self._texture);
|
||||
}
|
||||
else {
|
||||
self.setRect(cc.rect(0, 0, w, h));
|
||||
}
|
||||
|
||||
if (!self._originalSize) {
|
||||
self.setOriginalSize(cc.size(w, h));
|
||||
}
|
||||
|
||||
if (!self._offset) {
|
||||
self.setOffset(cc.v2(0, 0));
|
||||
}
|
||||
|
||||
// dispatch 'load' event of cc.SpriteFrame
|
||||
self.emit("load");
|
||||
if (self._rotated && cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
|
||||
self._texture = _ccsg.Sprite.CanvasRenderCmd._createRotatedTexture(texture, self.getRect());
|
||||
self._rotated = false;
|
||||
w = self._texture.width;
|
||||
h = self._texture.height;
|
||||
self.setRect(cc.rect(0, 0, w, h));
|
||||
}
|
||||
|
||||
if (locLoaded) {
|
||||
textureLoadedCallback();
|
||||
if (self._rect) {
|
||||
self._checkRect(self._texture);
|
||||
}
|
||||
else {
|
||||
texture.once("load", textureLoadedCallback);
|
||||
self.setRect(cc.rect(0, 0, w, h));
|
||||
}
|
||||
|
||||
if (!self._originalSize) {
|
||||
self.setOriginalSize(cc.size(w, h));
|
||||
}
|
||||
|
||||
if (!self._offset) {
|
||||
self.setOffset(cc.v2(0, 0));
|
||||
}
|
||||
|
||||
// dispatch 'load' event of cc.SpriteFrame
|
||||
self.emit("load");
|
||||
}
|
||||
|
||||
if (texture.loaded) {
|
||||
textureLoadedCallback();
|
||||
}
|
||||
else {
|
||||
texture.once("load", textureLoadedCallback);
|
||||
}
|
||||
//if (texture && texture.url && texture.isLoaded()) {
|
||||
// if (self._rect) {
|
||||
// self._checkRect(texture);
|
||||
// }
|
||||
// else {
|
||||
// self.setRect(cc.rect(0, 0, texture.width, texture.height));
|
||||
// }
|
||||
//}
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -392,20 +384,13 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
if (typeof texture === 'string' && texture) {
|
||||
texture = cc.textureCache.addImage(texture);
|
||||
}
|
||||
if (texture instanceof cc.Texture2D) {
|
||||
if (texture instanceof cc.Texture2D && this._texture !== texture) {
|
||||
this._refreshTexture(texture);
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
_loadTexture: function () {
|
||||
if (this._textureFilename) {
|
||||
var texture = cc.textureCache.addImage(this._textureFilename);
|
||||
this._refreshTexture(texture);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en If a loading scene (or prefab) is marked as `asyncLoadAssets`, all the textures of the SpriteFrame which
|
||||
* associated by user's custom Components in the scene, will not preload automatically.
|
||||
@@ -425,11 +410,17 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
* }
|
||||
*/
|
||||
ensureLoadTexture: function () {
|
||||
if (!this._texture) {
|
||||
this._loadTexture();
|
||||
if (this._texture) {
|
||||
if (!this._texture.isLoaded()) {
|
||||
// load exists texture
|
||||
this._refreshTexture(this._texture);
|
||||
this._texture.load();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// TODO - load native texture
|
||||
else if (this._textureFilename) {
|
||||
// load new texture
|
||||
var texture = cc.textureCache.addImage(this._textureFilename);
|
||||
this._refreshTexture(texture);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -410,6 +410,35 @@ var Texture2D = cc.Class({
|
||||
return this._image;
|
||||
},
|
||||
|
||||
// load a texture
|
||||
load (callback) {
|
||||
if (this.loaded) {
|
||||
callback && callback();
|
||||
return;
|
||||
}
|
||||
if (!this.url) {
|
||||
callback && callback();
|
||||
return;
|
||||
}
|
||||
// load image
|
||||
var self = this;
|
||||
cc.loader.load({
|
||||
url: this.url,
|
||||
// For image, we should skip loader otherwise it will load a new texture
|
||||
skips: [ 'Loader' ],
|
||||
}, function (err, image) {
|
||||
if (image) {
|
||||
if (CC_DEBUG && image instanceof cc.Texture2D) {
|
||||
return cc.error('internal error: loader handle pipe must be skipped');
|
||||
}
|
||||
if (!self.loaded) {
|
||||
self._nativeAsset = image;
|
||||
}
|
||||
}
|
||||
callback && callback(err);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Check whether texture is loaded.
|
||||
* @method isLoaded
|
||||
@@ -423,7 +452,6 @@ var Texture2D = cc.Class({
|
||||
/**
|
||||
* Handler of texture loaded event.
|
||||
* @method handleLoadedTexture
|
||||
* @param {Boolean} [premultiplied]
|
||||
*/
|
||||
handleLoadedTexture: function () {
|
||||
if (!this._image || !this._image.width || !this._image.height)
|
||||
@@ -948,8 +976,12 @@ JS.get(_p, "pixelHeight", _p.getPixelHeight);
|
||||
return true;
|
||||
};
|
||||
|
||||
// [premultiplied=false]
|
||||
_p.handleLoadedTexture = function (premultiplied) {
|
||||
_p.handleLoadedTexture = function () {
|
||||
// TODO: remove AUTO_PREMULTIPLIED_ALPHA_FOR_PNG
|
||||
// if (this.premultiplied === undefined) {
|
||||
// this.premultiplied = cc.macro.AUTO_PREMULTIPLIED_ALPHA_FOR_PNG &&
|
||||
// (this.url && this.url.endsWith(".png"));
|
||||
// }
|
||||
if (!this._image || !this._image.width || !this._image.height) {
|
||||
return;
|
||||
}
|
||||
@@ -959,7 +991,6 @@ JS.get(_p, "pixelHeight", _p.getPixelHeight);
|
||||
opts.format = PixelFormat.RGBA8888;
|
||||
opts.width = this._image.width;
|
||||
opts.height = this._image.height;
|
||||
opts.premultiplyAlpha = !!premultiplied;
|
||||
opts.minFilter = cc.view._antiAliasEnabled ? Filter.LINEAR : Filter.NEAREST;
|
||||
this.update(opts);
|
||||
|
||||
|
||||
@@ -197,27 +197,26 @@ var textureCache = /** @lends cc.textureCache# */{
|
||||
if (tex) {
|
||||
if(tex.isLoaded()) {
|
||||
cb && cb.call(target, tex);
|
||||
return tex;
|
||||
}
|
||||
else {
|
||||
tex.once("load", function(){
|
||||
cb && cb.call(target, tex);
|
||||
}, target);
|
||||
return tex;
|
||||
}
|
||||
}
|
||||
else {
|
||||
tex = locTexs[url] = new Texture2D();
|
||||
tex.url = url;
|
||||
cc.loader.load(url, function (err, texture) {
|
||||
if (err) {
|
||||
return cb && cb.call(target, err || new Error('Unknown error'));
|
||||
}
|
||||
|
||||
tex = locTexs[url] = new Texture2D();
|
||||
tex.url = url;
|
||||
cc.loader.load(url, function (err, texture) {
|
||||
if (err) {
|
||||
return cb && cb.call(target, err || new Error('Unknown error'));
|
||||
}
|
||||
textureCache.handleLoadedTexture(url);
|
||||
|
||||
textureCache.handleLoadedTexture(url);
|
||||
|
||||
cb && cb.call(target, tex);
|
||||
});
|
||||
cb && cb.call(target, tex);
|
||||
});
|
||||
}
|
||||
|
||||
return tex;
|
||||
},
|
||||
|
||||
+30
-39
@@ -178,20 +178,18 @@ cc.SpriteFrame.$super = cc.Asset;
|
||||
|
||||
prototype = cc.SpriteFrame.prototype;
|
||||
prototype._setTexture = prototype.setTexture;
|
||||
prototype._initWithTexture = prototype.initWithTexture;
|
||||
|
||||
cc.js.mixin(prototype, cc.EventTarget.prototype);
|
||||
|
||||
prototype._ctor = function (filename, rect, rotated, offset, originalSize) {
|
||||
this._name = '';
|
||||
this.insetTop = 0;
|
||||
this.insetBottom = 0;
|
||||
this.insetLeft = 0;
|
||||
this.insetRight = 0;
|
||||
this._name = '';
|
||||
this._texture = null;
|
||||
if (filename !== undefined) {
|
||||
this.initWithTexture(filename, rect, rotated, offset, originalSize);
|
||||
} else {
|
||||
//todo log Error
|
||||
}
|
||||
};
|
||||
|
||||
@@ -217,7 +215,7 @@ prototype.setTexture = function (textureOrTextureFile, rect, rotated, offset, or
|
||||
if (typeof texture === 'string' && texture) {
|
||||
texture = cc.textureCache.addImage(texture);
|
||||
}
|
||||
if (texture instanceof cc.Texture2D) {
|
||||
if (texture instanceof cc.Texture2D && this.getTexture() !== texture) {
|
||||
this._refreshTexture(texture);
|
||||
}
|
||||
|
||||
@@ -226,52 +224,45 @@ prototype.setTexture = function (textureOrTextureFile, rect, rotated, offset, or
|
||||
|
||||
prototype.initWithTexture = prototype.setTexture;
|
||||
|
||||
prototype._loadTexture = function () {
|
||||
if (this._textureFilename) {
|
||||
prototype.ensureLoadTexture = function () {
|
||||
if (this._texture) {
|
||||
if (!this._texture.isLoaded()) {
|
||||
// load exists texture
|
||||
this._refreshTexture(this._texture);
|
||||
this._texture.load();
|
||||
}
|
||||
}
|
||||
else if (this._textureFilename) {
|
||||
// load new texture
|
||||
var texture = cc.textureCache.addImage(this._textureFilename);
|
||||
this._refreshTexture(texture);
|
||||
}
|
||||
};
|
||||
|
||||
prototype.ensureLoadTexture = function () {
|
||||
if (!this._texture) {
|
||||
this._loadTexture();
|
||||
}
|
||||
else {
|
||||
// TODO - load native texture
|
||||
}
|
||||
};
|
||||
|
||||
prototype.clearTexture = function () {
|
||||
this._setTexture(null);
|
||||
};
|
||||
|
||||
prototype._refreshTexture = function (texture) {
|
||||
var w = texture.width, h = texture.height;
|
||||
|
||||
if (this.getTexture() !== texture) {
|
||||
|
||||
var w = texture.width, h = texture.height;
|
||||
|
||||
var rect = this.getRect();
|
||||
if (rect.width === 0 || rect.height === 0) {
|
||||
rect = cc.rect(0, 0, w, h);
|
||||
}
|
||||
else {
|
||||
this._checkRect(texture);
|
||||
}
|
||||
|
||||
var originalSize = this.getOriginalSize();
|
||||
if (originalSize.width === 0 || originalSize.height === 0) {
|
||||
originalSize = cc.size(w, h);
|
||||
}
|
||||
var offset = this.getOffset();
|
||||
var rotated = this.isRotated();
|
||||
|
||||
this._initWithTexture(texture, rect, rotated, offset, originalSize);
|
||||
|
||||
//dispatch 'load' event of cc.SpriteFrame
|
||||
this.emit("load");
|
||||
var rect = this.getRect();
|
||||
if (rect.width === 0 || rect.height === 0) {
|
||||
this.setRect(cc.rect(0, 0, w, h));
|
||||
}
|
||||
else {
|
||||
this._checkRect(texture);
|
||||
}
|
||||
|
||||
var originalSize = this.getOriginalSize();
|
||||
if (originalSize.width === 0 || originalSize.height === 0) {
|
||||
this.setOriginalSize(cc.size(w, h));
|
||||
}
|
||||
|
||||
this._setTexture(texture);
|
||||
|
||||
//dispatch 'load' event of cc.SpriteFrame
|
||||
this.emit("load");
|
||||
};
|
||||
|
||||
prototype._deserialize = function (data, handle) {
|
||||
|
||||
Reference in New Issue
Block a user