mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
optimize dynamic atlas
This commit is contained in:
@@ -56,6 +56,26 @@ let RenderTexture = cc.Class({
|
||||
this.emit("load");
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Draw a texture to the specified position
|
||||
* !#zh 将指定的图片渲染到指定的位置上
|
||||
* @param {Texture2D} texture
|
||||
* @param {Number} x
|
||||
* @param {Number} y
|
||||
*/
|
||||
drawTextureAt (texture, x, y) {
|
||||
if (!texture._image) return;
|
||||
|
||||
this._texture.updateSubImage({
|
||||
x, y,
|
||||
image: texture._image,
|
||||
width: texture.width,
|
||||
height: texture.height,
|
||||
level: 0,
|
||||
flipY: false
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Get pixels from render texture, the pixels data stores in a RGBA Uint8Array.
|
||||
|
||||
@@ -158,6 +158,9 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
this._texture = null;
|
||||
this._textureFilename = '';
|
||||
|
||||
// store original info before packed to dynamic atlas
|
||||
this._oriInfo = null;
|
||||
|
||||
if (CC_EDITOR) {
|
||||
// Atlas asset uuid
|
||||
this._atlasUuid = '';
|
||||
|
||||
@@ -383,6 +383,7 @@ var Label = cc.Class({
|
||||
this._fontAtlas = null;
|
||||
this._material = null;
|
||||
this._updateAssembler();
|
||||
this._activateMaterial();
|
||||
this._updateRenderData();
|
||||
},
|
||||
type: cc.Font,
|
||||
@@ -469,6 +470,7 @@ var Label = cc.Class({
|
||||
this._super();
|
||||
|
||||
this._updateAssembler();
|
||||
this._activateMaterial();
|
||||
},
|
||||
|
||||
onDestroy () {
|
||||
|
||||
@@ -159,6 +159,7 @@ var MotionStreak = cc.Class({
|
||||
set: function (value) {
|
||||
this._texture = value;
|
||||
this._material = null;
|
||||
this._activateMaterial();
|
||||
},
|
||||
type: cc.Texture2D,
|
||||
animatable: false,
|
||||
|
||||
@@ -176,6 +176,7 @@ let Graphics = cc.Class({
|
||||
|
||||
onEnable () {
|
||||
this._super();
|
||||
this._activateMaterial();
|
||||
},
|
||||
|
||||
onDestroy () {
|
||||
|
||||
@@ -27,10 +27,14 @@ const Sprite = require('../../../components/CCSprite');
|
||||
const FillType = Sprite.FillType;
|
||||
|
||||
const simpleRenderUtil = require('./simple');
|
||||
const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
|
||||
|
||||
module.exports = {
|
||||
useModel: false,
|
||||
updateRenderData (sprite) {
|
||||
dynamicAtlasManager.insertSpriteFrame(sprite.spriteFrame);
|
||||
sprite._activateMaterial();
|
||||
|
||||
let renderData = sprite._renderData;
|
||||
if (renderData && sprite._material) {
|
||||
let uvDirty = renderData.uvDirty,
|
||||
@@ -179,7 +183,6 @@ module.exports = {
|
||||
renderData.vertDirty = false;
|
||||
},
|
||||
|
||||
packAtlas: simpleRenderUtil.packAtlas,
|
||||
updateWorldVerts: simpleRenderUtil.updateWorldVerts,
|
||||
createData: simpleRenderUtil.createData,
|
||||
fillBuffers: simpleRenderUtil.fillBuffers
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
const Sprite = require('../../../components/CCSprite');
|
||||
const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
|
||||
|
||||
const SpriteType = Sprite.Type;
|
||||
const FillType = Sprite.FillType;
|
||||
|
||||
@@ -23,9 +23,9 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const simpleRenderUtil = require('./simple');
|
||||
const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
|
||||
|
||||
module.exports = {
|
||||
module.exports = {
|
||||
useModel: false,
|
||||
|
||||
createData (sprite) {
|
||||
@@ -33,6 +33,9 @@ module.exports = {
|
||||
},
|
||||
|
||||
updateRenderData (sprite) {
|
||||
dynamicAtlasManager.insertSpriteFrame(sprite.spriteFrame);
|
||||
sprite._activateMaterial();
|
||||
|
||||
let renderData = sprite._renderData;
|
||||
let frame = sprite.spriteFrame;
|
||||
if (renderData && frame && sprite._material) {
|
||||
@@ -171,6 +174,4 @@ module.exports = {
|
||||
ibuf[indiceOffset++] = vertexId + triangles[i];
|
||||
}
|
||||
},
|
||||
|
||||
packAtlas: simpleRenderUtil.packAtlas,
|
||||
};
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const simpleRenderUtil = require('./simple');
|
||||
const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
|
||||
|
||||
const PI_2 = Math.PI * 2;
|
||||
|
||||
@@ -43,6 +43,9 @@ module.exports = {
|
||||
},
|
||||
|
||||
updateRenderData (sprite) {
|
||||
dynamicAtlasManager.insertSpriteFrame(sprite.spriteFrame);
|
||||
sprite._activateMaterial();
|
||||
|
||||
let renderData = sprite._renderData;
|
||||
let spriteFrame = sprite._spriteFrame;
|
||||
if (renderData && frame && sprite._material) {
|
||||
@@ -345,6 +348,4 @@ module.exports = {
|
||||
ibuf[indiceOffset+i] = vertexId+i;
|
||||
}
|
||||
},
|
||||
|
||||
packAtlas: simpleRenderUtil.packAtlas,
|
||||
};
|
||||
|
||||
@@ -29,11 +29,10 @@ const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
|
||||
module.exports = {
|
||||
useModel: false,
|
||||
|
||||
packAtlas (comp) {
|
||||
dynamicAtlasManager.insertSpriteFrame(comp.spriteFrame);
|
||||
},
|
||||
|
||||
updateRenderData (sprite) {
|
||||
dynamicAtlasManager.insertSpriteFrame(sprite.spriteFrame);
|
||||
sprite._activateMaterial();
|
||||
|
||||
let renderData = sprite._renderData;
|
||||
if (renderData && sprite._material) {
|
||||
if (renderData.uvDirty) {
|
||||
|
||||
@@ -24,10 +24,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
const Sprite = require('../../../components/CCSprite');
|
||||
const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
|
||||
const FillType = Sprite.FillType;
|
||||
|
||||
const simpleRenderUtil = require('./simple');
|
||||
|
||||
module.exports = {
|
||||
useModel: false,
|
||||
|
||||
@@ -42,7 +41,10 @@ module.exports = {
|
||||
return renderData;
|
||||
},
|
||||
|
||||
updateRenderData (sprite) {
|
||||
updateRenderData (sprite, batchData) {
|
||||
dynamicAtlasManager.insertSpriteFrame(sprite.spriteFrame);
|
||||
sprite._activateMaterial();
|
||||
|
||||
let renderData = sprite._renderData;
|
||||
if (renderData && sprite._material) {
|
||||
if (renderData.uvDirty) {
|
||||
@@ -192,6 +194,4 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
packAtlas: simpleRenderUtil.packAtlas,
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const simpleRenderUtil = require('./simple');
|
||||
const dynamicAtlasManager = require('../../utils/dynamic-atlas/manager');
|
||||
|
||||
module.exports = {
|
||||
useModel: false,
|
||||
@@ -33,6 +33,9 @@ module.exports = {
|
||||
},
|
||||
|
||||
updateRenderData (sprite) {
|
||||
dynamicAtlasManager.insertSpriteFrame(sprite.spriteFrame);
|
||||
sprite._activateMaterial();
|
||||
|
||||
let renderData = sprite._renderData;
|
||||
let material = sprite.getMaterial();
|
||||
if (renderData && material) {
|
||||
@@ -209,6 +212,4 @@ module.exports = {
|
||||
vertexId += 4;
|
||||
}
|
||||
},
|
||||
|
||||
packAtlas: simpleRenderUtil.packAtlas,
|
||||
};
|
||||
@@ -28,6 +28,7 @@ const renderEngine = require('./render-engine');
|
||||
const defaultVertexFormat = require('./vertex-format');
|
||||
const StencilManager = require('./stencil-manager');
|
||||
const hierarchyChain = require('./utils/hierarchy-chain');
|
||||
const atlasManager = require('./utils/dynamic-atlas/manager');
|
||||
|
||||
const gfx = renderEngine.gfx;
|
||||
const RecyclePool = renderEngine.RecyclePool;
|
||||
@@ -255,13 +256,6 @@ RenderComponentWalker.prototype = {
|
||||
broken = false,
|
||||
iaData = false;
|
||||
|
||||
if (assembler.packAtlas) {
|
||||
assembler.packAtlas(comp);
|
||||
}
|
||||
if (comp._activateMaterial) {
|
||||
comp._activateMaterial();
|
||||
}
|
||||
|
||||
// Update render data
|
||||
datas = assembler.updateRenderData(comp, batchData);
|
||||
|
||||
@@ -373,6 +367,7 @@ RenderComponentWalker.prototype = {
|
||||
this._batchData.worldMatUpdated = false;
|
||||
}
|
||||
|
||||
atlasManager.update();
|
||||
this._flush();
|
||||
this._uploadData();
|
||||
}
|
||||
|
||||
@@ -2,21 +2,11 @@ const space = 1;
|
||||
|
||||
class Atlas {
|
||||
constructor (width, height) {
|
||||
let canvas = document.createElement("canvas");
|
||||
canvas.style.width = canvas.width = width;
|
||||
canvas.style.height = canvas.height = height;
|
||||
|
||||
// comment out this to show atlas
|
||||
// document.body.appendChild(canvas)
|
||||
|
||||
let ctx = canvas.getContext('2d');
|
||||
|
||||
let texture = new cc.Texture2D();
|
||||
texture.initWithElement(canvas);
|
||||
let texture = new cc.RenderTexture();
|
||||
texture.initWithSize(width, height);
|
||||
texture.handleLoadedTexture();
|
||||
|
||||
|
||||
this._texture = texture;
|
||||
this._ctx = ctx;
|
||||
|
||||
this._x = space;
|
||||
this._y = space;
|
||||
@@ -24,48 +14,94 @@ class Atlas {
|
||||
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
|
||||
this._innerTextureInfos = {};
|
||||
this._innerSpriteFrames = [];
|
||||
}
|
||||
|
||||
insertSpriteFrame (spriteFrame) {
|
||||
let rect = spriteFrame._rect;
|
||||
let sx = rect.x, sy = rect.y, width = rect.width, height = rect.height;
|
||||
let rect = spriteFrame._rect,
|
||||
texture = spriteFrame._texture,
|
||||
info = this._innerTextureInfos[texture.__instanceId];
|
||||
|
||||
if ((this._x + width) > this._width) {
|
||||
this._x = space;
|
||||
this._y = this._nexty;
|
||||
let sx = rect.x, sy = rect.y;
|
||||
|
||||
if (info) {
|
||||
rect.x += info.x;
|
||||
rect.y += info.y;
|
||||
}
|
||||
else {
|
||||
let width = texture.width, height = texture.height;
|
||||
|
||||
if ((this._y + height) > this._nexty) {
|
||||
this._nexty = this._y + height;
|
||||
if ((this._x + width) > this._width) {
|
||||
this._x = space;
|
||||
this._y = this._nexty;
|
||||
}
|
||||
|
||||
if ((this._y + height) > this._nexty) {
|
||||
this._nexty = this._y + height;
|
||||
}
|
||||
|
||||
if (this._nexty > this._height) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._texture.drawTextureAt(texture, this._x, this._y);
|
||||
|
||||
this._innerTextureInfos[texture.__instanceId] = {
|
||||
x: this._x,
|
||||
y: this._y,
|
||||
texture: texture
|
||||
};
|
||||
|
||||
rect.x += this._x;
|
||||
rect.y += this._y;
|
||||
|
||||
this._x += width;
|
||||
|
||||
this._dirty = true;
|
||||
this._texture.handleLoadedTexture();
|
||||
}
|
||||
|
||||
if (this._nexty > this._height) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._ctx.drawImage(spriteFrame._texture._image,
|
||||
sx, sy, rect.width, rect.height,
|
||||
this._x, this._y, rect.width, rect.height,
|
||||
);
|
||||
|
||||
spriteFrame._oriInfo = {
|
||||
x: sx,
|
||||
y: sy
|
||||
y: sy,
|
||||
texture: spriteFrame._texture
|
||||
}
|
||||
|
||||
rect.x = this._x;
|
||||
rect.y = this._y;
|
||||
|
||||
this._x += width;
|
||||
|
||||
spriteFrame._texture = this._texture;
|
||||
|
||||
this._texture.handleLoadedTexture();
|
||||
this._innerSpriteFrames.push(spriteFrame);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
update () {
|
||||
if (!this._dirty) return;
|
||||
this._texture.handleLoadedTexture();
|
||||
this._dirty = false;
|
||||
}
|
||||
|
||||
reset () {
|
||||
this._x = space;
|
||||
this._y = space;
|
||||
this._nexty = space;
|
||||
|
||||
let frames = this._innerSpriteFrames;
|
||||
for (let i = 0, l = frames.length; i < l; i++) {
|
||||
let frame = frames[i];
|
||||
let oriInfo = frame._oriInfo;
|
||||
frame._rect.x = oriInfo.x;
|
||||
frame._rect.y = oriInfo.y;
|
||||
frame._texture = oriInfo.texture;
|
||||
frame._oriInfo = null;
|
||||
}
|
||||
this._innerSpriteFrames.length = 0;
|
||||
this._innerTextureInfos = {};
|
||||
}
|
||||
|
||||
destroy () {
|
||||
this.reset();
|
||||
this._texture.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@ let _maxAtlasCount = 5;
|
||||
let _textureSize = 2048;
|
||||
let _maxFrameSize = 512;
|
||||
|
||||
let _enabled = false;
|
||||
|
||||
let _debugNode = null;
|
||||
|
||||
function newAtlas () {
|
||||
let atlas = new Atlas(_textureSize, _textureSize);
|
||||
_atlases.push(atlas);
|
||||
@@ -15,9 +19,37 @@ function newAtlas () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* !#en Manager the dynamic atlas.
|
||||
* !#zh 管理动态图集。
|
||||
* @class DynamicAtlasManager
|
||||
*/
|
||||
let dynamicAtlasManager = {
|
||||
enabled: true,
|
||||
|
||||
/**
|
||||
* !#en Enabled or Disabled dynamic atlas.
|
||||
* !#zh 开启或者关闭动态图集。
|
||||
* @property enabled
|
||||
* @type {Boolean}
|
||||
*/
|
||||
get enabled () {
|
||||
return _enabled;
|
||||
},
|
||||
set enabled (value) {
|
||||
if (_enabled === value) return;
|
||||
|
||||
if (value) {
|
||||
this.reset();
|
||||
}
|
||||
_enabled = value;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The maximum number of atlas that can be created.
|
||||
* !#zh 可以创建的最大图集数量。
|
||||
* @property maxAtlasCount
|
||||
* @type {Number}
|
||||
*/
|
||||
get maxAtlasCount () {
|
||||
return _maxAtlasCount;
|
||||
},
|
||||
@@ -25,6 +57,12 @@ let dynamicAtlasManager = {
|
||||
_maxAtlasCount = value;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The size of the atlas that was created
|
||||
* !#zh 创建的图集的宽高
|
||||
* @property textureSize
|
||||
* @type {Number}
|
||||
*/
|
||||
get textureSize () {
|
||||
return _textureSize;
|
||||
},
|
||||
@@ -32,6 +70,12 @@ let dynamicAtlasManager = {
|
||||
_textureSize = value;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The maximum size of the picture that can be added to the atlas.
|
||||
* !#zh 可以添加进图集的图片的最大尺寸。
|
||||
* @property maxFrameSize
|
||||
* @type {Number}
|
||||
*/
|
||||
get maxFrameSize () {
|
||||
return _maxFrameSize;
|
||||
},
|
||||
@@ -39,7 +83,14 @@ let dynamicAtlasManager = {
|
||||
_maxFrameSize = value;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Append a sprite frame into the dynamic atlas.
|
||||
* !#zh 添加碎图进入动态图集。
|
||||
* @method insertSpriteFrame
|
||||
* @param {SpriteFrame} spriteFrame
|
||||
*/
|
||||
insertSpriteFrame (spriteFrame) {
|
||||
if (CC_EDITOR) return;
|
||||
if (!this.enabled || _atlasIndex === _maxAtlasCount ||
|
||||
!spriteFrame || spriteFrame._oriInfo) return;
|
||||
|
||||
@@ -59,12 +110,100 @@ let dynamicAtlasManager = {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resets all dynamic atlas, and the existing ones are not destroyed, but are reused.
|
||||
* !#zh 重置所有动态图集,已有的动态图集并不会被销毁,而是会被复用。
|
||||
* @method reset
|
||||
*/
|
||||
reset () {
|
||||
for (let i = 0; i <= _atlasIndex; i++) {
|
||||
_atlases[i].reset();
|
||||
}
|
||||
_atlasIndex = -1;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Releases all dynamic atlas, which are destroyed.
|
||||
* !#zh 释放所有动态图集,这些图集会被销毁掉。
|
||||
* @method releaseAll
|
||||
*/
|
||||
releaseAll () {
|
||||
for (let i = 0, l = _atlases.length; i < l; i++) {
|
||||
_atlases[i].destroy();
|
||||
}
|
||||
_atlases.length = 0;
|
||||
}
|
||||
_atlasIndex = -1;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Displays all the dynamic atlas in the current scene, which you can use to view the current atlas state.
|
||||
* !#zh 在当前场景中显示所有动态图集,可以用来查看当前的合图状态。
|
||||
* @method showDebug
|
||||
* @param {Boolean} show
|
||||
*/
|
||||
showDebug: CC_DEV && function (show) {
|
||||
if (show) {
|
||||
if (!_debugNode) {
|
||||
let width = cc.visibleRect.width;
|
||||
let height = cc.visibleRect.height;
|
||||
|
||||
_debugNode = new cc.Node('DYNAMIC_ATLAS_DEBUG_NODE');
|
||||
_debugNode.width = width;
|
||||
_debugNode.height = height;
|
||||
_debugNode.x = width/2;
|
||||
_debugNode.y = height/2;
|
||||
_debugNode._zIndex = cc.macro.MAX_ZINDEX;
|
||||
_debugNode.parent = cc.director.getScene();
|
||||
|
||||
let scroll = _debugNode.addComponent(cc.ScrollView);
|
||||
|
||||
let content = new cc.Node('CONTENT');
|
||||
let layout = content.addComponent(cc.Layout);
|
||||
layout.type = cc.Layout.Type.VERTICAL;
|
||||
layout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
|
||||
content.parent = _debugNode;
|
||||
|
||||
for (let i = 0; i <= _atlasIndex; i++) {
|
||||
let node = new cc.Node('ATLAS');
|
||||
|
||||
let texture = _atlases[i]._texture;
|
||||
let spriteFrame = new cc.SpriteFrame();
|
||||
spriteFrame.setTexture(_atlases[i]._texture);
|
||||
|
||||
let sprite = node.addComponent(cc.Sprite)
|
||||
sprite.spriteFrame = spriteFrame;
|
||||
sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
|
||||
|
||||
node.width = height;
|
||||
node.height = height;
|
||||
|
||||
node.parent = content;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (_debugNode) {
|
||||
_debugNode.parent = null;
|
||||
_debugNode = null;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
update () {
|
||||
if (!this.enabled) return;
|
||||
|
||||
for (let i = 0; i <= _atlasIndex; i++) {
|
||||
_atlases[i].update();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @module cc
|
||||
*/
|
||||
|
||||
/**
|
||||
* @property dynamicAtlasManager
|
||||
* @type DynamicAtlasManager
|
||||
*/
|
||||
module.exports = cc.dynamicAtlasManager = dynamicAtlasManager;
|
||||
@@ -386,9 +386,8 @@ let TiledLayer = cc.Class({
|
||||
* tiledLayer.setTexture(texture);
|
||||
*/
|
||||
setTexture (texture){
|
||||
if (this._texture === texture) return;
|
||||
this._texture = texture;
|
||||
this._material = null;
|
||||
this._activateMaterial();
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user