support dynamic atlas packer

This commit is contained in:
yinkaile
2018-04-15 18:08:55 +08:00
parent e50f37d6fb
commit 4ea054b46c
9 changed files with 146 additions and 31 deletions
-2
View File
@@ -383,7 +383,6 @@ var Label = cc.Class({
this._fontAtlas = null;
this._material = null;
this._updateAssembler();
this._activateMaterial();
this._updateRenderData();
},
type: cc.Font,
@@ -470,7 +469,6 @@ var Label = cc.Class({
this._super();
this._updateAssembler();
this._activateMaterial();
},
onDestroy () {
@@ -159,7 +159,6 @@ var MotionStreak = cc.Class({
set: function (value) {
this._texture = value;
this._material = null;
this._activateMaterial();
},
type: cc.Texture2D,
animatable: false,
@@ -210,7 +209,6 @@ var MotionStreak = cc.Class({
onEnable: function () {
this._super();
this._activateMaterial();
this.reset();
},
+24 -25
View File
@@ -466,7 +466,6 @@ var Sprite = cc.Class({
setState: function (state) {
this._state = state;
this._material = null;
this._activateMaterial();
},
/**
@@ -483,8 +482,6 @@ var Sprite = cc.Class({
this._updateAssembler();
this._activateMaterial();
this.node.on('size-changed', this._onNodeSizeDirty, this);
this.node.on('anchor-changed', this._onNodeSizeDirty, this);
},
@@ -517,40 +514,45 @@ var Sprite = cc.Class({
},
_activateMaterial: function () {
if (this._material) return;
let spriteFrame = this._spriteFrame;
// cannot be activated if texture not loaded yet
if (!this._spriteFrame || !this._spriteFrame.textureLoaded()) {
if (!spriteFrame || !spriteFrame.textureLoaded()) {
return;
}
// Get material
if (!this._material) {
let texture = this._spriteFrame.getTexture();
let url = texture.url;
let key = url;
if (this._state === State.GRAY) {
key = url + ':gray';
}
if (this._state === State.GRAY) {
this._material = new GraySpriteMaterial();
}
else {
this._material = new SpriteMaterial();
}
// TODO: old texture in material have been released by loader
this._material.texture = texture;
let texture = spriteFrame.getTexture();
let url = texture.url;
let key = url;
if (this._state === State.GRAY) {
key = url + ':gray';
}
let material;
if (this._state === State.GRAY) {
material = new GraySpriteMaterial();
}
else {
material = new SpriteMaterial();
}
// TODO: old texture in material have been released by loader
material.texture = texture;
if (this.srcBlendFactor !== gfx.BLEND_SRC_ALPHA || this.dstBlendFactor !== gfx.BLEND_ONE_MINUS_SRC_ALPHA) {
// Update hash inside
this._updateBlendFunc();
}
else {
this._material.updateHash();
material.updateHash();
}
if (this._renderData) {
this._renderData.material = this._material;
this._renderData.material = material;
}
this._material = material;
},
_updateBlendFunc: function () {
@@ -632,10 +634,7 @@ var Sprite = cc.Class({
if (!this.isValid) {
return;
}
// Reactivate material
if (this.enabledInHierarchy) {
this._activateMaterial();
}
this._applySpriteSize();
},
-1
View File
@@ -176,7 +176,6 @@ let Graphics = cc.Class({
onEnable () {
this._super();
this._activateMaterial();
},
onDestroy () {
@@ -27,6 +27,8 @@ const js = require('../../../platform/js');
const assembler = require('../assembler');
const Sprite = require('../../../components/CCSprite');
const renderEngine = require('../../render-engine');
const atlasPakcer = require('../../utils/atlas/atlas-packer');
const SpriteType = Sprite.Type;
const FillType = Sprite.FillType;
@@ -41,6 +43,10 @@ const meshRenderUtil = require('./mesh');
let spriteAssembler = js.addon({
useModel: false,
checkPacker (comp) {
atlasPakcer.insertSpriteFrame(comp.spriteFrame);
},
getAssembler (sprite) {
let util = simpleRenderUtil;
@@ -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 gfx = renderEngine.gfx;
const RecyclePool = renderEngine.RecyclePool;
const InputAssembler = renderEngine.InputAssembler;
@@ -41,6 +42,8 @@ const MAX_VERTEX_BYTES = MAX_VERTEX * defaultVertexFormat._bytes;
const MAX_INDICE = MAX_VERTEX * BYTE_PER_INDEX;
const MAX_INDICE_BYTES = MAX_INDICE * 2;
const AUTO_PACK_ATLAS = true;
var RenderComponentWalker = function (device, renderScene) {
this._renderScene = renderScene;
this._device = device;
@@ -259,6 +262,13 @@ RenderComponentWalker.prototype = {
broken = false,
iaData = false;
if (AUTO_PACK_ATLAS && assembler.checkPacker) {
assembler.checkPacker(comp);
}
if (comp._activateMaterial) {
comp._activateMaterial();
}
// Update render data
datas = assembler.updateRenderData(comp, batchData);
@@ -0,0 +1,35 @@
const Atlas = require('./atlas');
let _atlases = [];
let _atlasIndex = -1;
let _textureWidth = 2048;
let _textureHeight = 2048;
let _maxFrameWidth = 512;
let _maxFrameHeight = 512;
module.exports = {
insertSpriteFrame (spriteFrame) {
if (!spriteFrame || spriteFrame._oriInfo) return;
let rect = spriteFrame._rect;
if (rect.width > _maxFrameWidth || rect.height > _maxFrameHeight) {
return;
}
let atlas = _atlases[_atlasIndex];
if (!atlas) {
atlas = new Atlas(_textureWidth, _textureHeight);
_atlases.push(atlas);
_atlasIndex++;
}
if (!atlas.insertSpriteFrame(spriteFrame)) {
atlas = new Atlas(_textureWidth, _textureHeight);
_atlases.push(atlas);
_atlasIndex++;
atlas.insertSpriteFrame(spriteFrame);
}
}
};
@@ -0,0 +1,69 @@
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);
texture.handleLoadedTexture();
this._texture = texture;
this._ctx = ctx;
this._x = space;
this._y = space;
this._nexty = space;
this._width = width;
this._height = height;
}
insertSpriteFrame (spriteFrame) {
let rect = spriteFrame._rect;
let sx = rect.x, sy = rect.y, width = rect.width, height = rect.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._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
}
rect.x = this._x;
rect.y = this._y;
this._x += width;
spriteFrame._texture = this._texture;
this._texture.handleLoadedTexture();
return true;
}
}
module.exports = Atlas;
+2 -1
View File
@@ -386,8 +386,9 @@ let TiledLayer = cc.Class({
* tiledLayer.setTexture(texture);
*/
setTexture (texture){
if (this._texture === texture) return;
this._texture = texture;
this._activateMaterial();
this._material = null;
},
/**