mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
optimize dynamic atlas
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user