diff --git a/extensions/dragonbones/ArmatureDisplay.js b/extensions/dragonbones/ArmatureDisplay.js index 94d0e53a26..32409311c1 100644 --- a/extensions/dragonbones/ArmatureDisplay.js +++ b/extensions/dragonbones/ArmatureDisplay.js @@ -23,12 +23,20 @@ THE SOFTWARE. ****************************************************************************/ +const RenderComponent = require('../../cocos2d/core/components/CCRenderComponent'); +const renderer = require('../../cocos2d/core/renderer'); +const renderEngine = require('../../cocos2d/core/renderer/render-engine'); +const RenderData = renderEngine.RenderData; +const SpriteMaterial = renderEngine.SpriteMaterial; + +let EventTarget = require('../../cocos2d/core/event/event-target'); + /** * @module dragonBones */ -var DefaultArmaturesEnum = cc.Enum({ 'default': -1 }); -var DefaultAnimsEnum = cc.Enum({ '': 0 }); +let DefaultArmaturesEnum = cc.Enum({ 'default': -1 }); +let DefaultAnimsEnum = cc.Enum({ '': 0 }); function setEnumAttr (obj, propName, enumDef) { cc.Class.attr(obj, propName, { @@ -54,21 +62,17 @@ function setEnumAttr (obj, propName, enumDef) { * @class ArmatureDisplay * @extends Component */ -dragonBones.ArmatureDisplay = cc.Class({ +let ArmatureDisplay = cc.Class({ name: 'dragonBones.ArmatureDisplay', - extends: cc.Component, + extends: RenderComponent, + mixins: [EventTarget], + editor: CC_EDITOR && { menu: 'i18n:MAIN_MENU.component.renderers/DragonBones', //help: 'app://docs/html/components/spine.html', // TODO help document of dragonBones }, - + properties: { - _factory: { - default: null, - type: dragonBones.CCFactory, - serializable: false, - }, - _dragonBonesData: { default: null, type: dragonBones.DragonBonesData, @@ -92,7 +96,6 @@ dragonBones.ArmatureDisplay = cc.Class({ notify: function () { // parse the asset data this._parseDragonAsset(); - this._refresh(); if (CC_EDITOR) { this._defaultArmatureIndex = 0; @@ -115,8 +118,7 @@ dragonBones.ArmatureDisplay = cc.Class({ notify: function () { // parse the atlas asset data this._parseDragonAtlasAsset(); - - this._refreshSgNode(); + this._buildArmature(); }, tooltip: CC_DEV && 'i18n:COMPONENT.dragon_bones.dragon_bones_atlas_asset' }, @@ -133,7 +135,7 @@ dragonBones.ArmatureDisplay = cc.Class({ }, set: function (value) { this._armatureName = value; - var animNames = this.getAnimationNames(this._armatureName); + let animNames = this.getAnimationNames(this._armatureName); if (!this.animationName || animNames.indexOf(this.animationName) < 0) { if (CC_EDITOR) { @@ -171,9 +173,9 @@ dragonBones.ArmatureDisplay = cc.Class({ _defaultArmatureIndex: { default: 0, notify: function () { - var armatureName = ''; + let armatureName = ''; if (this.dragonAsset) { - var armaturesEnum; + let armaturesEnum; if (this.dragonAsset) { armaturesEnum = this.dragonAsset.getArmatureEnum(); } @@ -207,7 +209,7 @@ dragonBones.ArmatureDisplay = cc.Class({ return; } - var animsEnum; + let animsEnum; if (this.dragonAsset) { animsEnum = this.dragonAsset.getAnimsEnum(this.armatureName); } @@ -216,7 +218,7 @@ dragonBones.ArmatureDisplay = cc.Class({ return; } - var animName = animsEnum[this._animationIndex]; + let animName = animsEnum[this._animationIndex]; if (animName !== undefined) { this.animationName = animName; } @@ -282,160 +284,113 @@ dragonBones.ArmatureDisplay = cc.Class({ }, }, - // IMPLEMENT ctor: function () { - if (CC_JSB) { - // TODO Fix me - // If using the getFactory in JSB. - // There may be throw errors when close the application. - this._factory = new dragonBones.CCFactory(); - } - else { - this._factory = dragonBones.CCFactory.getFactory(); - } + this._armatures = []; + this._inited = false; }, __preload: function () { + this._init(); + }, + + _init: function () { + if (this._inited) return; + this._inited = true; + this._parseDragonAsset(); this._parseDragonAtlasAsset(); this._refresh(); + this._activateMaterial(); }, - _createSgNode: function () { - if (this.dragonAsset && this.dragonAtlasAsset && this.armatureName) { - return this._factory.buildArmatureDisplay(this.armatureName, this._dragonBonesData.name); + _onDestroy: function () { + this._armature.dispose(); + }, + + _activateMaterial: function () { + if (this._material) return; + + let url = this.dragonAtlasAsset.texture; + let material = renderer.materialUtil.get(url); + + // Get material + if (!material) { + material = new SpriteMaterial(); + renderer.materialUtil.register(url, material); } - return null; + let texture = cc.loader.getRes(url); + // TODO: old texture in material have been released by loader + material.texture = texture ? texture.getImpl() : undefined; + + this._material = material; }, - _initSgNode: function () { - // set the time scale - var sgNode = this._sgNode; - sgNode.animation().timeScale = this.timeScale; + _buildArmature: function () { + if (!this.dragonAsset || !this.dragonAtlasAsset || !this.armatureName) return; + let factory = dragonBones.CCFactory.getFactory(); + this._armature = factory.buildArmature(this.armatureName, this._dragonBonesData.name, this); + if (this.animationName) { this.playAnimation(this.animationName, this.playTimes); } - - if (CC_EDITOR) { - sgNode.setDebugBones(this.debugBones); - } - }, - - _removeSgNode: function () { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.armature().dispose(); - } }, _parseDragonAsset: function () { - if (this.dragonAsset) { - if (CC_JSB) { - // The '_factory' create a new one every time in JSB, they can't use getDragonBonesData - // to get cached data, and only parse data every time - this._dragonBonesData = this._factory.parseDragonBonesData(this.dragonAsset.dragonBonesJson); - } - else { - var jsonObj = JSON.parse(this.dragonAsset.dragonBonesJson); - var data = this._factory.getDragonBonesData(jsonObj.name); - if (data) { - // already added asset - var armature, dragonBonesData; - for (var i = 0, len = jsonObj.armature.length; i < len; i++) { - armature = jsonObj.armature[i]; - if (!data.armatures[armature.name]) { - //add new armature - if (!dragonBonesData) { - dragonBonesData = this._factory._dataParser.parseDragonBonesData(jsonObj); - } - data.addArmature(dragonBonesData.armatures[armature.name]); - } + if (!this.dragonAsset) return; + + let factory = dragonBones.CCFactory.getFactory(); + let jsonObj = JSON.parse(this.dragonAsset.dragonBonesJson); + let data = factory.getDragonBonesData(jsonObj.name); + if (data) { + // already added asset + let armature, dragonBonesData; + for (let i = 0, len = jsonObj.armature.length; i < len; i++) { + armature = jsonObj.armature[i]; + if (!data.armatures[armature.name]) { + //add new armature + if (!dragonBonesData) { + dragonBonesData = factory._dataParser.parseDragonBonesData(jsonObj); } - this._dragonBonesData = data; - return; + data.addArmature(dragonBonesData.armatures[armature.name]); } - this._dragonBonesData = this._factory.parseDragonBonesData(jsonObj); } + this._dragonBonesData = data; + return; } + this._dragonBonesData = factory.parseDragonBonesData(jsonObj); }, _parseDragonAtlasAsset: function () { - if (this.dragonAtlasAsset) { - if (CC_JSB) { - // TODO parse the texture atlas data from json string & texture path - this._factory.parseTextureAtlasData(this.dragonAtlasAsset.atlasJson, this.dragonAtlasAsset.texture); - } - else { - var atlasJsonObj = JSON.parse(this.dragonAtlasAsset.atlasJson); - var atlasName = atlasJsonObj.name; - var existedAtlasData = null; - var atlasDataList = this._factory.getTextureAtlasData(atlasName); - var texturePath = this.dragonAtlasAsset.texture; - if (atlasDataList && atlasDataList.length > 0) { - for (var idx in atlasDataList) { - var data = atlasDataList[idx]; - if (data && data.texture && data.texture.url === texturePath) { - existedAtlasData = data; - break; - } - } - } + if (!this.dragonAtlasAsset) return; - var texture = cc.textureCache.getTextureForKey(texturePath); - if (existedAtlasData) { - existedAtlasData.texture = texture; - } - else { - this._factory.parseTextureAtlasData(atlasJsonObj, texture); + let factory = dragonBones.CCFactory.getFactory(); + let atlasJsonObj = JSON.parse(this.dragonAtlasAsset.atlasJson); + let atlasName = atlasJsonObj.name; + let existedAtlasData = null; + let atlasDataList = factory.getTextureAtlasData(atlasName); + let texturePath = this.dragonAtlasAsset.texture; + if (atlasDataList && atlasDataList.length > 0) { + for (let idx in atlasDataList) { + let data = atlasDataList[idx]; + if (data && data.texture && data.texture.url === texturePath) { + existedAtlasData = data; + break; } } } - }, - _refreshSgNode: function () { - var self = this; - - // discard exists sgNode - var listenersBefore = null; - if (self._sgNode) { - listenersBefore = self._sgNode._bubblingListeners; // get the listeners added before - if (self.node._sizeProvider === self._sgNode) { - self.node._sizeProvider = null; - } - self._removeSgNode(); - self._sgNode = null; + let texture = cc.loader.getRes(texturePath); + if (existedAtlasData) { + existedAtlasData.texture = texture; } - - // recreate sgNode... - var sgNode = self._sgNode = self._createSgNode(); - if (sgNode) { - if (CC_JSB) { - sgNode.retain(); - } - if (!self.enabledInHierarchy) { - sgNode.setVisible(false); - } - - if (listenersBefore) { - sgNode._bubblingListeners = listenersBefore; // using the listeners added before - if (CC_JSB && !sgNode.hasEventCallback()) { - // In JSB, should set event callback of the new sgNode - // to make the listeners work well. - sgNode.setEventCallback(function (eventObject) { - sgNode.emit(eventObject.type, eventObject); - }); - } - } - - self._initSgNode(); - self._appendSgNode(sgNode); - self._registSizeProvider(); + else { + factory.parseTextureAtlasData(atlasJsonObj, texture); } }, _refresh: function () { - this._refreshSgNode(); + this._buildArmature(); if (CC_EDITOR) { // update inspector @@ -447,7 +402,7 @@ dragonBones.ArmatureDisplay = cc.Class({ // update animation list for editor _updateAnimEnum: CC_EDITOR && function () { - var animEnum; + let animEnum; if (this.dragonAsset) { animEnum = this.dragonAsset.getAnimsEnum(this.armatureName); } @@ -457,7 +412,7 @@ dragonBones.ArmatureDisplay = cc.Class({ // update armature list for editor _updateArmatureEnum: CC_EDITOR && function () { - var armatureEnum; + let armatureEnum; if (this.dragonAsset) { armatureEnum = this.dragonAsset.getArmatureEnum(); } @@ -465,6 +420,18 @@ dragonBones.ArmatureDisplay = cc.Class({ setEnumAttr(this, '_defaultArmatureIndex', armatureEnum || DefaultArmaturesEnum); }, + update (passedTime) { + if (CC_EDITOR) return; + + let armatures = this._armatures; + for (let i = 0, l = armatures.length; i < l; i++) { + let armature = armatures[i]; + if (armature._display && armature._display.shouldAdvanced) { + armature.advanceTime(passedTime); + } + } + }, + /** * !#en * Play the specified animation. @@ -486,10 +453,10 @@ dragonBones.ArmatureDisplay = cc.Class({ * @return {dragonBones.AnimationState} */ playAnimation: function (animName, playTimes) { - if (this._sgNode) { + if (this._armature) { this.playTimes = (playTimes === undefined) ? -1 : playTimes; this.animationName = animName; - return this._sgNode.animation().play(animName, this.playTimes); + return this._armature.animation.play(animName, this.playTimes); } return null; @@ -521,11 +488,11 @@ dragonBones.ArmatureDisplay = cc.Class({ * @returns {Array} */ getAnimationNames: function (armatureName) { - var ret = []; + let ret = []; if (this._dragonBonesData) { - var armatureData = this._dragonBonesData.getArmature(armatureName); + let armatureData = this._dragonBonesData.getArmature(armatureName); if (armatureData) { - for (var animName in armatureData.animations) { + for (let animName in armatureData.animations) { if (armatureData.animations.hasOwnProperty(animName)) { ret.push(animName); } @@ -547,9 +514,8 @@ dragonBones.ArmatureDisplay = cc.Class({ * @param {Object} target */ addEventListener: function (eventType, listener, target) { - if (this._sgNode) { - this._sgNode.addEvent(eventType, listener, target); - } + if (!this._armature) return; + this._armature._display.addEvent(eventType, listener, target); }, /** @@ -563,9 +529,8 @@ dragonBones.ArmatureDisplay = cc.Class({ * @param {Object} target */ removeEventListener: function (eventType, listener, target) { - if (this._sgNode) { - this._sgNode.removeEvent(eventType, listener, target); - } + if (!this._armature) return; + this._armature._display.removeEvent(eventType, listener, target); }, /** @@ -575,14 +540,23 @@ dragonBones.ArmatureDisplay = cc.Class({ * 构建指定名称的 armature 对象 * @method buildArmature * @param {String} armatureName + * @param {Node} node * @return {dragonBones.Armature} */ buildArmature: function (armatureName) { - if (this._factory) { - return this._factory.buildArmature(armatureName); - } + return dragonBones.CCFactory.getFactory().buildArmature(armatureName, this._dragonBonesData.name, this); + }, - return null; + buildArmatureDisplay (armatureName, node) { + node = node || new cc.Node(); + let display = node.addComponent(dragonBones.ArmatureDisplay); + + display.armatureName = armatureName; + display._N$dragonAsset = this.dragonAsset; + display._N$dragonAtlasAsset = this.dragonAtlasAsset; + display._init(); + + return display; }, /** @@ -594,10 +568,8 @@ dragonBones.ArmatureDisplay = cc.Class({ * @returns {Object} */ armature: function () { - if (this._sgNode) { - return this._sgNode.armature(); - } - - return null; + return this._armature; } }); + +module.exports = dragonBones.ArmatureDisplay = ArmatureDisplay; diff --git a/extensions/dragonbones/CCArmatureDisplay.js b/extensions/dragonbones/CCArmatureDisplay.js index ea4d628d6e..0bc95ef9c8 100644 --- a/extensions/dragonbones/CCArmatureDisplay.js +++ b/extensions/dragonbones/CCArmatureDisplay.js @@ -25,13 +25,19 @@ var EventTarget = require('../../cocos2d/core/event/event-target'); // require('../../cocos2d/shape-nodes/CCDrawNode'); +const math = cc.vmath; + +let _matrix = math.mat4.create(); + dragonBones.CCArmatureDisplay = cc.Class({ name: 'dragonBones.CCArmatureDisplay', - extends: _ccsg.Node, mixins: [EventTarget], - _armature : null, - _debugDrawer : null, + ctor () { + this._armature = null; + this._debugDrawer = null; + this.shouldAdvanced = false; + }, _onClear : function () { this._armature = null; @@ -67,18 +73,8 @@ dragonBones.CCArmatureDisplay = cc.Class({ } }, - update : function (passedTime) { - if (this._armature) { - this._armature.advanceTime(passedTime); - } - }, - advanceTimeBySelf : function (on) { - if (on) { - this.scheduleUpdate(); - } else { - this.unscheduleUpdate(); - } + this.shouldAdvanced = !!on; }, hasEvent : function(type) { @@ -118,5 +114,24 @@ dragonBones.CCArmatureDisplay = cc.Class({ this._debugDrawer.clear(); } } + }, + + convertToWorldSpace (point) { + if (this.component) { + let node = this.component.node; + let mat = node._worldMatrix; + + if (this._armature._parentSlot) { + math.mat4.mul(_matrix, mat, this._armature._parentSlot._matrix); + mat = _matrix; + } + + let out = new cc.Vec2( + point.x - node._anchorPoint.x * node._contentSize.width, + point.y - node._anchorPoint.y * node._contentSize.height + ); + return math.vec2.transformMat4(out, out, _matrix); + } + return point; } }); diff --git a/extensions/dragonbones/CCFactory.js b/extensions/dragonbones/CCFactory.js index 5cdea5abfa..0cf69b5785 100644 --- a/extensions/dragonbones/CCFactory.js +++ b/extensions/dragonbones/CCFactory.js @@ -22,23 +22,24 @@ THE SOFTWARE. ****************************************************************************/ -var BaseObject = dragonBones.BaseObject; +let BaseObject = dragonBones.BaseObject; dragonBones.CCFactory = cc.Class({ name: 'dragonBones.CCFactory', extends: dragonBones.BaseFactory, - buildArmatureDisplay : function (armatureName, dragonBonesName, skinName) { - var armature = this.buildArmature(armatureName, dragonBonesName, skinName); - var armatureDisplay = armature ? armature._display : null; + buildArmature (armatureName, dragonBonesName, comp) { + this._comp = comp; + let armature = dragonBones.BaseFactory.prototype.buildArmature.call(this, armatureName, dragonBonesName); + let armatureDisplay = armature ? armature._display : null; if (armatureDisplay) { armatureDisplay.advanceTimeBySelf(true); } - - return armatureDisplay; + this._comp = null; + return armature; }, - _generateTextureAtlasData : function (textureAtlasData, texture) { + _generateTextureAtlasData (textureAtlasData, texture) { if (! textureAtlasData) { textureAtlasData = BaseObject.borrowObject(dragonBones.CCTextureAtlasData); } @@ -47,66 +48,76 @@ dragonBones.CCFactory = cc.Class({ return textureAtlasData; }, - _generateArmature : function (dataPackage) { - var armature = BaseObject.borrowObject(dragonBones.Armature); - var armatureDisplayContainer = new dragonBones.CCArmatureDisplay(); + _sortSlots () { + let slots = this._slots; + let sortedSlots = []; + for (let i = 0, l = slots.length; i < l; i++) { + let slot = slots[i]; + let zOrder = slot._zOrder; + let inserted = false; + for (let j = sortedSlots.length - 1; j >= 0; j--) { + if (zOrder >= sortedSlots[j]._zOrder) { + sortedSlots.splice(j+1, 0, slot); + inserted = true; + break; + } + } + if (!inserted) { + sortedSlots.splice(0, 0, slot); + } + } + this._slots = sortedSlots; + }, + + _generateArmature (dataPackage) { + let armature = BaseObject.borrowObject(dragonBones.Armature); armature._armatureData = dataPackage.armature; armature._skinData = dataPackage.skin; armature._animation = BaseObject.borrowObject(dragonBones.Animation); - armature._display = armatureDisplayContainer; - armatureDisplayContainer.setCascadeOpacityEnabled(true); - armatureDisplayContainer.setCascadeColorEnabled(true); - armatureDisplayContainer._armature = armature; armature._animation._armature = armature; armature.animation.animations = dataPackage.armature.animations; + armature._display = new dragonBones.CCArmatureDisplay(); + armature._display.component = this._comp; + armature._display._armature = armature; + + // fixed dragonbones sort issue + armature._sortSlots = this._sortSlots; + + this._comp._armatures.push(armature); + return armature; }, - _generateSlot : function (dataPackage, slotDisplayDataSet) { - var slot = BaseObject.borrowObject(dragonBones.CCSlot); - var slotData = slotDisplayDataSet.slot; - var displayList = []; + _generateSlot (dataPackage, slotDisplayDataSet) { + let slot = BaseObject.borrowObject(dragonBones.CCSlot); + let slotData = slotDisplayDataSet.slot; + let displayList = []; slot.name = slotData.name; - slot._rawDisplay = new cc.Scale9Sprite(); - slot._rawDisplay.setRenderingType(cc.Scale9Sprite.RenderingType.SIMPLE); // use simple rendering type as default - slot._rawDisplay.setAnchorPoint(cc.p(0,0)); - slot._meshDisplay = slot._rawDisplay; + slot._meshDisplay = slot._rawDisplay = slot; - for (var i = 0, l = slotDisplayDataSet.displays.length; i < l; ++i) { - var displayData = slotDisplayDataSet.displays[i]; + for (let i = 0, l = slotDisplayDataSet.displays.length; i < l; ++i) { + let displayData = slotDisplayDataSet.displays[i]; switch (displayData.type) { case dragonBones.DisplayType.Image: - if (!displayData.texture) { - displayData.texture = this._getTextureData(dataPackage.dataName, displayData.name); - } - - displayList.push(slot._rawDisplay); - break; - case dragonBones.DisplayType.Mesh: if (!displayData.texture) { displayData.texture = this._getTextureData(dataPackage.dataName, displayData.name); } - if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) { - slot._meshDisplay.setRenderingType(cc.Scale9Sprite.RenderingType.MESH); - displayList.push(slot._meshDisplay); - } else { - cc.warnID(6200); - } + displayList.push(slot); break; case dragonBones.DisplayType.Armature: - var childArmature = this.buildArmature(displayData.name, dataPackage.dataName); + let childArmature = dragonBones.BaseFactory.prototype.buildArmature.call(this, displayData.name, dataPackage.dataName); if (childArmature) { if (!slot.inheritAnimation) { - var actions = slotData.actions.length > 0 ? slotData.actions : childArmature.armatureData.actions; + let actions = slotData.actions.length > 0 ? slotData.actions : childArmature.armatureData.actions; if (actions.length > 0) { - for (var j = 0, n = actions.length; j < n; ++j) { + for (let j = 0, n = actions.length; j < n; ++j) { childArmature._bufferAction(actions[j]); } } else { @@ -127,29 +138,8 @@ dragonBones.CCFactory = cc.Class({ } slot._setDisplayList(displayList); - slot._rawDisplay.setLocalZOrder(slotData.zOrder); return slot; - }, - - getTextureDisplay: function(textureName, textureAtlasName) { - var textureData = this._getTextureData(textureAtlasName, textureName); - if (textureData) { - if (!textureData.texture) { - var textureAtlasTexture = textureData.parent.texture; - var rect = cc.rect(textureData.region.x, textureData.region.y, textureData.region.width, textureData.region.height); - var offset = cc.p(0, 0); - var originSize = cc.size(textureData.region.width, textureData.region.height); - textureData.texture = new cc.SpriteFrame(); - textureData.texture.setTexture(textureAtlasTexture, rect, textureData.rotated, offset, originSize); // TODO multiply textureAtlas - } - - var ret = new cc.Scale9Sprite(); - ret.initWithSpriteFrame(textureData.texture); - return ret; - } - - return null; } }); diff --git a/extensions/dragonbones/CCSlot.js b/extensions/dragonbones/CCSlot.js index d4051d909b..efa1d0496e 100644 --- a/extensions/dragonbones/CCSlot.js +++ b/extensions/dragonbones/CCSlot.js @@ -22,10 +22,24 @@ THE SOFTWARE. ****************************************************************************/ +const renderEngine = require('../../cocos2d/core/renderer/render-engine'); +const math = renderEngine.math; + +let _matrix = math.mat4.create(); + dragonBones.CCSlot = cc.Class({ name: 'dragonBones.CCSlot', extends: dragonBones.Slot, - _renderDisplay : null, + + ctor: function () { + this._vertices = []; + this._localVertices = []; + this._indices = []; + this._matrix = math.mat4.create(); + this._worldMatrix = math.mat4.create(); + this._boundsRect = cc.rect(); + this._visible = false; + }, statics : { toString: function () { @@ -35,35 +49,26 @@ dragonBones.CCSlot = cc.Class({ _onClear : function () { dragonBones.Slot.prototype._onClear.call(this); - this._renderDisplay = null; }, _onUpdateDisplay : function () { - if (!this._rawDisplay) { - this._rawDisplay = new cc.Scale9Sprite(); + if (this._display instanceof dragonBones.CCArmatureDisplay) { + this._display._armature._parentSlot = this; } - - this._renderDisplay = this._display || this._rawDisplay; }, _initDisplay : function (value) { - }, _addDisplay : function () { - var container = this._armature._display; - container.addChild(this._renderDisplay); + this._visible = true; }, _replaceDisplay : function (value) { - var container = this._armature._display; - var prevDisplay = value; - container.addChild(this._renderDisplay, prevDisplay.getLocalZOrder()); - container.removeChild(prevDisplay); }, _removeDisplay : function () { - this._renderDisplay.removeFromParent(); + this._visible = false; }, _disposeDisplay : function (object) { @@ -71,25 +76,19 @@ dragonBones.CCSlot = cc.Class({ }, _updateVisible : function () { - this._renderDisplay.setVisible(this._parent.visible); }, _updateZOrder: function() { - if (!this._renderDisplay._parent) { - var container = this._armature._display; - container.addChild(this._renderDisplay, this._zOrder); - } else { - this._renderDisplay.setLocalZOrder(this._zOrder); - } }, _updateBlendMode : function () { + return; if (this._renderDisplay instanceof cc.Scale9Sprite) { switch (this._blendMode) { case 0: // BlendMode Normal break; case 1: // BlendMode Add - var texture = this._renderDisplay._spriteFrame.getTexture(); + let texture = this._renderDisplay._spriteFrame.getTexture(); if (texture && texture.hasPremultipliedAlpha()) { this._renderDisplay.setBlendFunc(cc.BlendFunc.BlendFactor.ONE, cc.BlendFunc.BlendFactor.ONE); } @@ -102,9 +101,9 @@ dragonBones.CCSlot = cc.Class({ } } else if (this._childArmature) { - var childSlots = this._childArmature.getSlots(); - for (var i = 0, l = childSlots.length; i < l; i++) { - var slot = childSlots[i]; + let childSlots = this._childArmature.getSlots(); + for (let i = 0, l = childSlots.length; i < l; i++) { + let slot = childSlots[i]; slot._blendMode = this._blendMode; slot._updateBlendMode(); } @@ -112,11 +111,11 @@ dragonBones.CCSlot = cc.Class({ }, _updateColor : function() { - this._renderDisplay.setOpacity(this._colorTransform.alphaMultiplier * 255); - var r = this._colorTransform.redMultiplier * 255; - var g = this._colorTransform.greenMultiplier * 255; - var b = this._colorTransform.blueMultiplier * 255; - this._renderDisplay.setColor(cc.color(r, g, b)); + let r = this._colorTransform.redMultiplier * 255; + let g = this._colorTransform.greenMultiplier * 255; + let b = this._colorTransform.blueMultiplier * 255; + let a = this._colorTransform.alphaMultiplier * 255; + this._color = ((a<<24) >>> 0) + (b<<16) + (g<<8) + r; }, _updateFilters : function () { @@ -124,69 +123,76 @@ dragonBones.CCSlot = cc.Class({ }, _updateFrame : function () { + this._vertices.length = 0; + this._indices.length = 0; + let vertices = this._vertices, + indices = this._indices, + localVertices = this._localVertices; + + vertices.length = 0; + indices.length = 0; + localVertices.length = 0; + // update the frame if (this._display && this._displayIndex >= 0) { - var rawDisplayData = this._displayIndex < this._displayDataSet.displays.length ? this._displayDataSet.displays[this._displayIndex] : null; - var replacedDisplayData = this._displayIndex < this._replacedDisplayDataSet.length ? this._replacedDisplayDataSet[this._displayIndex] : null; - var currentDisplayData = replacedDisplayData || rawDisplayData; - var currentTextureData = currentDisplayData.texture; + let rawDisplayData = this._displayIndex < this._displayDataSet.displays.length ? this._displayDataSet.displays[this._displayIndex] : null; + let replacedDisplayData = this._displayIndex < this._replacedDisplayDataSet.length ? this._replacedDisplayDataSet[this._displayIndex] : null; + let currentDisplayData = replacedDisplayData || rawDisplayData; + let currentTextureData = currentDisplayData.texture; if (currentTextureData) { - var textureAtlasTexture = currentTextureData.parent.texture; - if (textureAtlasTexture && (!currentTextureData.texture || currentTextureData.texture.getTexture() !== textureAtlasTexture)) { + let boundsRect = this._boundsRect; + let textureAtlas = this._armature._replacedTexture || currentTextureData.parent.texture; + if (textureAtlas && (!currentTextureData.texture || currentTextureData.texture.getTexture() !== textureAtlas)) { // Create and cache texture - var rect = cc.rect(currentTextureData.region.x, currentTextureData.region.y, + let rect = cc.rect(currentTextureData.region.x, currentTextureData.region.y, currentTextureData.region.width, currentTextureData.region.height); - var offset = cc.p(0, 0); - var size = cc.size(currentTextureData.region.width, currentTextureData.region.height); + let offset = cc.p(0, 0); + let size = cc.size(currentTextureData.region.width, currentTextureData.region.height); currentTextureData.texture = new cc.SpriteFrame(); - currentTextureData.texture.setTexture(textureAtlasTexture, rect, false, offset, size); + currentTextureData.texture.setTexture(textureAtlas, rect, false, offset, size); } - var texture = this._armature._replacedTexture || (currentTextureData.texture ? currentTextureData.texture.getTexture() : null); + let textureAtlasWidth = textureAtlas.width; + let textureAtlasHeight = textureAtlas.height; + let region = currentTextureData.region; + if (this._meshData && this._display === this._meshDisplay) { - var region = currentTextureData.region; - var textureAtlasSize = currentTextureData.texture.getTexture().getContentSize(); - var displayVertices = [], vertexIndices = []; - var boundsRect = cc.rect(999999, 999999, -999999, -999999); + boundsRect.x = boundsRect.y = 9999999; + boundsRect.width = boundsRect.height = -999999; - if (this._meshData !== rawDisplayData.mesh && rawDisplayData && rawDisplayData !== currentDisplayData) - { + if (this._meshData !== rawDisplayData.mesh && rawDisplayData && + rawDisplayData !== currentDisplayData) { this._pivotX = rawDisplayData.transform.x - currentDisplayData.transform.x; this._pivotY = rawDisplayData.transform.y - currentDisplayData.transform.y; } - else - { + else { this._pivotX = 0; this._pivotY = 0; } - var i, n; - for (i = 0, n = this._meshData.uvs.length; i < n; i +=2) { - var x = this._meshData.vertices[i]; - var y = this._meshData.vertices[i + 1]; - var u = (region.x + this._meshData.uvs[i] * region.width) / textureAtlasSize.width; - var v = (region.y + this._meshData.uvs[i + 1] * region.height) / textureAtlasSize.height; - displayVertices.push({ x: x, y: -y, u: u, v: v }); + for (let i = 0, n = this._meshData.uvs.length; i < n; i +=2) { + let x = this._meshData.vertices[i]; + let y = this._meshData.vertices[i + 1]; + let u = (region.x + this._meshData.uvs[i] * region.width) / textureAtlasWidth; + let v = (region.y + this._meshData.uvs[i + 1] * region.height) / textureAtlasHeight; + vertices.push({ x: x, y: -y, u: u, v: v }); + localVertices.push({ x: x, y: -y}); - if (boundsRect.x > x) - { + if (boundsRect.x > x) { boundsRect.x = x; } - if (boundsRect.width < x) - { + if (boundsRect.width < x) { boundsRect.width = x; } - if (boundsRect.y > -y) - { + if (boundsRect.y > -y) { boundsRect.y = -y; } - if (boundsRect.height < -y) - { + if (boundsRect.height < -y) { boundsRect.height = -y; } } @@ -194,42 +200,19 @@ dragonBones.CCSlot = cc.Class({ boundsRect.width -= boundsRect.x; boundsRect.height -= boundsRect.y; - for (i = 0, n = this._meshData.vertexIndices.length; i < n; ++i) - { - vertexIndices.push(this._meshData.vertexIndices[i]); + for (let i = 0, n = this._meshData.vertexIndices.length; i < n; ++i) { + indices.push(this._meshData.vertexIndices[i]); } - var polygonInfo = { - triangles: { - verts: displayVertices, - indices: vertexIndices - }, - rect: boundsRect - }; - this._meshDisplay.setSpriteFrame(currentTextureData.texture); - if (texture != currentTextureData.texture.getTexture()) - { - this._meshDisplay.setTexture(texture); - } - this._meshDisplay.setMeshPolygonInfo(polygonInfo); - this._meshDisplay.setContentSize(cc.size(boundsRect.width, boundsRect.height)); - - if (this._meshData.skinned) { - this._meshDisplay.setScale(1, 1); - this._meshDisplay.setRotationX(0); - this._meshDisplay.setRotationY(0); - this._meshDisplay.setPosition(0, 0); - } - this._meshDisplay.setAnchorPoint(cc.p(0, 0)); } else { - var scale = this._armature.armatureData.scale; + let scale = this._armature.armatureData.scale; this._pivotX = currentDisplayData.pivot.x; this._pivotY = currentDisplayData.pivot.y; if (currentDisplayData.isRelativePivot) { - var rectData = currentTextureData.frame || currentTextureData.region; - var width = rectData.width * scale; - var height = rectData.height * scale; + let rectData = currentTextureData.frame || currentTextureData.region; + let width = rectData.width * scale; + let height = rectData.height * scale; if (!currentTextureData.frame && currentTextureData.rotated) { width = rectData.height; height = rectData.width; @@ -248,59 +231,70 @@ dragonBones.CCSlot = cc.Class({ this._pivotX += rawDisplayData.transform.x - currentDisplayData.transform.x; this._pivotY += rawDisplayData.transform.y - currentDisplayData.transform.y; } - this._pivotY -= currentTextureData.region.height * scale; + this._pivotY -= region.height * scale; - this._rawDisplay.setSpriteFrame(currentTextureData.texture); - this._rawDisplay.setContentSize(currentTextureData.texture.getOriginalSize()); - if (texture !== currentTextureData.texture.getTexture()) { - this._rawDisplay.setTexture(texture); + + for (let i = 0; i < 4; i++) { + vertices.push({}); + localVertices.push({}); } + + let l = region.x / textureAtlasWidth; + let b = (region.y + region.height) / textureAtlasHeight; + let r = (region.x + region.width) / textureAtlasWidth; + let t = region.y / textureAtlasHeight; + vertices[0].u = l; vertices[0].v = b; + vertices[1].u = r; vertices[1].v = b; + vertices[2].u = l; vertices[2].v = t; + vertices[3].u = r; vertices[3].v = t; + + localVertices[0].x = localVertices[2].x = vertices[0].x = vertices[2].x = 0; + localVertices[1].x = localVertices[3].x = vertices[1].x = vertices[3].x = region.width; + localVertices[0].y = localVertices[1].y = vertices[0].y = vertices[1].y = 0; + localVertices[2].y = localVertices[3].y = vertices[2].y = vertices[3].y = region.height; + + boundsRect.x = boundsRect.y = 0; + boundsRect.width = currentTextureData.texture.getOriginalSize().width; + boundsRect.height = currentTextureData.texture.getOriginalSize().height; + + indices[0] = 0; + indices[1] = 1; + indices[2] = 2; + indices[3] = 1; + indices[4] = 3; + indices[5] = 2; + this._blendModeDirty = true; } - - this._updateVisible(); - return; } } - - this._pivotX = 0; - this._pivotY = 0; - this._rawDisplay.setTexture(null); - this._rawDisplay.setVisible(false); - this._rawDisplay.setPosition(this.origin.x, this.origin.y); }, _updateMesh : function() { - var meshDisplay = this._meshDisplay; - var polygonInfo = meshDisplay.getMeshPolygonInfo(); - if (!meshDisplay || !polygonInfo) - { - return; - } + let hasFFD = this._ffdVertices.length > 0; + let localVertices = this._localVertices; - var hasFFD = this._ffdVertices.length > 0; - var displayVertices = polygonInfo.triangles.verts; - var boundsRect = cc.rect(999999, 999999, -999999, -999999); + let boundsRect = this._boundsRect; + boundsRect.x = boundsRect.y = 9999999; + boundsRect.width = boundsRect.height = -999999; - var iH = 0, xG = 0, yG = 0, i, l; - if (this._meshData.skinned) - { - var iF = 0; - for (i = 0, l = this._meshData.vertices.length; i < l; i += 2) - { + let iH = 0, xG = 0, yG = 0, i, l; + if (this._meshData.skinned) { + let iF = 0; + for (i = 0, l = this._meshData.vertices.length; i < l; i += 2) { iH = Math.floor(i / 2); - var boneIndices = this._meshData.boneIndices[iH]; - var boneVertices = this._meshData.boneVertices[iH]; - var weights = this._meshData.weights[iH]; + let boneIndices = this._meshData.boneIndices[iH]; + let boneVertices = this._meshData.boneVertices[iH]; + let weights = this._meshData.weights[iH]; xG = 0; yG = 0; - for (var iB = 0, lB = boneIndices.length; iB < lB; ++iB) + for (let iB = 0, lB = boneIndices.length; iB < lB; ++iB) { - var bone = this._meshBones[boneIndices[iB]]; - var matrix = bone.globalTransformMatrix; - var weight = weights[iB]; + let bone = this._meshBones[boneIndices[iB]]; + let matrix = bone.globalTransformMatrix; + let weight = weights[iB]; - var xL = 0, yL = 0; + let xL = 0, yL = 0; if (hasFFD) { xL = boneVertices[iB * 2] + this._ffdVertices[iF]; @@ -318,57 +312,47 @@ dragonBones.CCSlot = cc.Class({ iF += 2; } - displayVertices[iH].x = xG; - displayVertices[iH].y = -yG; - if (boundsRect.x > xG) - { + localVertices[iH].x = xG; + localVertices[iH].y = -yG; + if (boundsRect.x > xG) { boundsRect.x = xG; } - if (boundsRect.width < xG) - { + if (boundsRect.width < xG) { boundsRect.width = xG; } - if (boundsRect.y > -yG) - { + if (boundsRect.y > -yG) { boundsRect.y = -yG; } - if (boundsRect.height < -yG) - { + if (boundsRect.height < -yG) { boundsRect.height = -yG; } } } - else if (hasFFD) - { - var vertices = this._meshData.vertices; - for (i = 0, l = this._meshData.vertices.length; i < l; i += 2) - { + else if (hasFFD) { + let vertices = this._meshData.vertices; + for (i = 0, l = this._meshData.vertices.length; i < l; i += 2) { iH = Math.floor(i / 2); xG = vertices[i] + this._ffdVertices[i]; yG = vertices[i + 1] + this._ffdVertices[i + 1]; - displayVertices[iH].x = xG; - displayVertices[iH].y = -yG; - if (boundsRect.x > xG) - { + localVertices[iH].x = xG; + localVertices[iH].y = -yG; + if (boundsRect.x > xG) { boundsRect.x = xG; } - if (boundsRect.width < xG) - { + if (boundsRect.width < xG) { boundsRect.width = xG; } - if (boundsRect.y > -yG) - { + if (boundsRect.y > -yG) { boundsRect.y = -yG; } - if (boundsRect.height < -yG) - { + if (boundsRect.height < -yG) { boundsRect.height = -yG; } } @@ -377,23 +361,27 @@ dragonBones.CCSlot = cc.Class({ boundsRect.width -= boundsRect.x; boundsRect.height -= boundsRect.y; - polygonInfo.rect = boundsRect; - var transform = meshDisplay.getNodeToParentTransform(); - meshDisplay.setContentSize(cc.size(boundsRect.width, boundsRect.height)); - meshDisplay.setMeshPolygonInfo(polygonInfo); - this._renderDisplay._renderCmd.setNodeToParentTransform(transform); + this._updateTransform(); }, _updateTransform : function () { // update the transform - var transform = { - a: this.globalTransformMatrix.a, - b: -this.globalTransformMatrix.b, - c: -this.globalTransformMatrix.c, - d: this.globalTransformMatrix.d, - tx: this.globalTransformMatrix.tx - (this.globalTransformMatrix.a * this._pivotX + this.globalTransformMatrix.c * this._pivotY), - ty: -(this.globalTransformMatrix.ty - (this.globalTransformMatrix.b * this._pivotX + this.globalTransformMatrix.d * this._pivotY)) - }; - this._renderDisplay._renderCmd.setNodeToParentTransform(transform); + let t = this._matrix; + let a = t.m00 = this.globalTransformMatrix.a; + let b = t.m01 = -this.globalTransformMatrix.b; + let c = t.m04 = -this.globalTransformMatrix.c; + let d = t.m05 = this.globalTransformMatrix.d; + let tx = t.m12 = this.globalTransformMatrix.tx - (this.globalTransformMatrix.a * this._pivotX + this.globalTransformMatrix.c * this._pivotY); + let ty = t.m13 = -(this.globalTransformMatrix.ty - (this.globalTransformMatrix.b * this._pivotX + this.globalTransformMatrix.d * this._pivotY)); + + let vertices = this._vertices; + let localVertices = this._localVertices; + for (let i = 0, l = vertices.length; i < l; i++) { + let x = localVertices[i].x; + let y = localVertices[i].y; + + vertices[i].x = a * x + c * y + tx; + vertices[i].y = b * x + d * y + ty; + } } }); diff --git a/extensions/dragonbones/dragonblones-assembler.js b/extensions/dragonbones/dragonblones-assembler.js new file mode 100644 index 0000000000..1027357b96 --- /dev/null +++ b/extensions/dragonbones/dragonblones-assembler.js @@ -0,0 +1,153 @@ +/**************************************************************************** + Copyright (c) 2017-2018 Chukong Technologies Inc. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Chukong Aipu reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +const Armature = require('./ArmatureDisplay'); + +const renderEngine = require('../../cocos2d/core/renderer/render-engine'); +const RenderData = renderEngine.RenderData; +const math = renderEngine.math; + + +let _matrix = math.mat4.create(); +let _v3 = cc.v3(); + +let _vbuf, _uintbuf, + _vertexId, _ibuf, + _off, + _a, _b, _c, _d, _tx, _ty, + _renderData, _z, + _worldMatrix; + +let assembler = { + useModel: false, + + createData (comp) { + return RenderData.alloc(); + }, + + updateRenderData (comp) { + let renderData = comp._renderData; + if (!renderData) { + renderData = comp._renderData = this.createData(comp); + } + + let size = comp.node._contentSize; + let anchor = comp.node._anchorPoint; + renderData.updateSizeNPivot(size.width, size.height, anchor.x, anchor.y); + }, + + fillVertexBuffer (comp, index, vbuf, uintbuf) { + if (!comp._armature) return; + let node = comp.node; + + _vbuf = vbuf; + _uintbuf = uintbuf; + _off = index * comp._vertexFormat._bytes / 4; + _z = node._position.z; + + node._updateWorldMatrix(); + _worldMatrix = node._worldMatrix; + _a = _worldMatrix.m00; _b = _worldMatrix.m01; _c = _worldMatrix.m04; _d = _worldMatrix.m05; + _tx = _worldMatrix.m12; _ty = _worldMatrix.m13; + + _renderData = comp._renderData; + _renderData.vertexCount = 0; + + this.fillVertexBufferWithArmature (comp._armature); + + _vbuf = _uintbuf = _renderData = null; + }, + + fillVertexBufferWithArmature (armature) { + let slots = armature._slots; + for (let i = 0, l = slots.length; i < l; i++) { + let slot = slots[i]; + if (!slot._visible || !slot.displayData) continue; + + if (slot.childArmature) { + math.mat4.mul(_matrix, _worldMatrix, slot._matrix); + _a = _matrix.m00; _b = _matrix.m01; _c = _matrix.m04; _d = _matrix.m05; + _tx = _matrix.m12; _ty = _matrix.m13; + this.fillVertexBufferWithArmature(slot.childArmature); + _a = _worldMatrix.m00; _b = _worldMatrix.m01; _c = _worldMatrix.m04; _d = _worldMatrix.m05; + _tx = _worldMatrix.m12; _ty = _worldMatrix.m13; + continue; + } + + let vertices = slot._vertices; + let color = slot._color; + for (let j = 0, vl = vertices.length; j < vl; j++) { + let vertex = vertices[j]; + _vbuf[_off + 0] = vertex.x * _a + vertex.y * _c + _tx; + _vbuf[_off + 1] = vertex.x * _b + vertex.y * _d + _ty; + _vbuf[_off + 2] = _z; + _vbuf[_off + 4] = vertex.u; + _vbuf[_off + 5] = vertex.v; + _uintbuf[_off + 3] = color; + _off += 6; + } + + _renderData.vertexCount += vertices.length; + } + }, + + fillIndexBuffer (comp, offset, vertexId, ibuf) { + if (!comp._armature) return; + + _ibuf = ibuf; + _vertexId = vertexId; + _off = offset; + + _renderData = comp._renderData; + _renderData.indiceCount = 0; + + this.fillIndexBufferWithArmature(comp._armature); + + _ibuf = _renderData = null; + }, + + fillIndexBufferWithArmature (armature) { + let slots = armature._slots; + for (let i = 0, l = slots.length; i < l; i++) { + let slot = slots[i]; + if (!slot._visible || !slot.displayData) continue; + + if (slot.childArmature) { + this.fillIndexBufferWithArmature(slot.childArmature); + continue; + } + + let indices = slot._indices; + for (let j = 0, il = indices.length; j < il; j++) { + _ibuf[_off++] = _vertexId + indices[j]; + } + + _vertexId += slot._vertices.length; + _renderData.indiceCount += indices.length; + } + } +} + +module.exports = Armature._assembler = assembler; diff --git a/extensions/dragonbones/index.js b/extensions/dragonbones/index.js index e458d58782..0cc1c7d718 100644 --- a/extensions/dragonbones/index.js +++ b/extensions/dragonbones/index.js @@ -98,6 +98,10 @@ if (!CC_EDITOR || !Editor.isMainProcess) { require('./DragonBonesAsset'); require('./DragonBonesAtlasAsset'); require('./ArmatureDisplay') + + if (!CC_JSB) { + require('./dragonblones-assembler'); + } } else { require('./DragonBonesAsset'); require('./DragonBonesAtlasAsset');