diff --git a/cocos2d/core/components/CCLabel.js b/cocos2d/core/components/CCLabel.js index 9ce192ad96..8199e2a152 100644 --- a/cocos2d/core/components/CCLabel.js +++ b/cocos2d/core/components/CCLabel.js @@ -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 () { diff --git a/cocos2d/core/components/CCMotionStreak.js b/cocos2d/core/components/CCMotionStreak.js index 24623e2b80..282c4d0960 100644 --- a/cocos2d/core/components/CCMotionStreak.js +++ b/cocos2d/core/components/CCMotionStreak.js @@ -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(); }, diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index f72905492a..3158b33506 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -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(); }, diff --git a/cocos2d/core/graphics/graphics.js b/cocos2d/core/graphics/graphics.js index f4c893e19b..91aae49813 100644 --- a/cocos2d/core/graphics/graphics.js +++ b/cocos2d/core/graphics/graphics.js @@ -176,7 +176,6 @@ let Graphics = cc.Class({ onEnable () { this._super(); - this._activateMaterial(); }, onDestroy () { diff --git a/cocos2d/core/renderer/assemblers/sprite/index.js b/cocos2d/core/renderer/assemblers/sprite/index.js index 1ded075403..964ff562cb 100644 --- a/cocos2d/core/renderer/assemblers/sprite/index.js +++ b/cocos2d/core/renderer/assemblers/sprite/index.js @@ -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; diff --git a/cocos2d/core/renderer/render-component-walker.js b/cocos2d/core/renderer/render-component-walker.js index 3f076d4bae..f0a110ca73 100644 --- a/cocos2d/core/renderer/render-component-walker.js +++ b/cocos2d/core/renderer/render-component-walker.js @@ -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); diff --git a/cocos2d/core/renderer/utils/atlas/atlas-packer.js b/cocos2d/core/renderer/utils/atlas/atlas-packer.js new file mode 100644 index 0000000000..b6acc4171b --- /dev/null +++ b/cocos2d/core/renderer/utils/atlas/atlas-packer.js @@ -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); + } + } +}; diff --git a/cocos2d/core/renderer/utils/atlas/atlas.js b/cocos2d/core/renderer/utils/atlas/atlas.js new file mode 100644 index 0000000000..a8eb340ed6 --- /dev/null +++ b/cocos2d/core/renderer/utils/atlas/atlas.js @@ -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; diff --git a/cocos2d/tilemap/CCTiledLayer.js b/cocos2d/tilemap/CCTiledLayer.js index 75f4378d4e..92e21fed3a 100644 --- a/cocos2d/tilemap/CCTiledLayer.js +++ b/cocos2d/tilemap/CCTiledLayer.js @@ -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; }, /**