optimize dynamic atlas

This commit is contained in:
yinkaile
2018-04-17 10:58:23 +08:00
parent 0d1d3f7b34
commit e7685fe44e
2 changed files with 50 additions and 14 deletions
@@ -64,6 +64,10 @@ class Atlas {
return true;
}
destroy () {
this._texture.destroy();
}
}
module.exports = Atlas;
@@ -3,35 +3,67 @@ const Atlas = require('./atlas');
let _atlases = [];
let _atlasIndex = -1;
let _textureWidth = 2048;
let _textureHeight = 2048;
let _maxAtlasCount = 5;
let _textureSize = 2048;
let _maxFrameSize = 512;
function newAtlas () {
let atlas = new Atlas(_textureSize, _textureSize);
_atlases.push(atlas);
_atlasIndex++;
return atlas;
}
let _maxFrameWidth = 512;
let _maxFrameHeight = 512;
let dynamicAtlasManager = {
enabled: true,
insertSpriteFrame (spriteFrame) {
if (!this.enabled || !spriteFrame || spriteFrame._oriInfo) return;
get maxAtlasCount () {
return _maxAtlasCount;
},
set maxAtlasCount (value) {
_maxAtlasCount = value;
},
get textureSize () {
return _textureSize;
},
set textureSize (value) {
_textureSize = value;
},
get maxFrameSize () {
return _maxFrameSize;
},
set maxFrameSize (value) {
_maxFrameSize = value;
},
insertSpriteFrame (spriteFrame) {
if (!this.enabled || _atlasIndex === _maxAtlasCount ||
!spriteFrame || spriteFrame._oriInfo) return;
let rect = spriteFrame._rect;
if (rect.width > _maxFrameWidth || rect.height > _maxFrameHeight) {
if (rect.width > _maxFrameSize || rect.height > _maxFrameSize) {
return;
}
let atlas = _atlases[_atlasIndex];
if (!atlas) {
atlas = new Atlas(_textureWidth, _textureHeight);
_atlases.push(atlas);
_atlasIndex++;
atlas = newAtlas();
}
if (!atlas.insertSpriteFrame(spriteFrame)) {
atlas = new Atlas(_textureWidth, _textureHeight);
_atlases.push(atlas);
_atlasIndex++;
if (!atlas.insertSpriteFrame(spriteFrame) && _atlasIndex !== _maxAtlasCount) {
atlas = newAtlas();
atlas.insertSpriteFrame(spriteFrame);
}
},
releaseAll () {
for (let i = 0, l = _atlases.length; i < l; i++) {
_atlases[i].destroy();
}
_atlases.length = 0;
}
};