From e7685fe44e01eb810d558fd32755ee8283e49b88 Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Tue, 17 Apr 2018 10:58:23 +0800 Subject: [PATCH] optimize dynamic atlas --- .../renderer/utils/dynamic-atlas/atlas.js | 4 ++ .../renderer/utils/dynamic-atlas/manager.js | 60 ++++++++++++++----- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js b/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js index a8eb340ed6..56e5791e11 100644 --- a/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js +++ b/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js @@ -64,6 +64,10 @@ class Atlas { return true; } + + destroy () { + this._texture.destroy(); + } } module.exports = Atlas; diff --git a/cocos2d/core/renderer/utils/dynamic-atlas/manager.js b/cocos2d/core/renderer/utils/dynamic-atlas/manager.js index da5d5ca843..18b132dfa9 100644 --- a/cocos2d/core/renderer/utils/dynamic-atlas/manager.js +++ b/cocos2d/core/renderer/utils/dynamic-atlas/manager.js @@ -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; } };