Merge pull request #1805 from pandamicro/master

Support skeletons batching for spine
This commit is contained in:
pandamicro
2017-07-13 23:50:04 +08:00
committed by GitHub
8 changed files with 149 additions and 100 deletions
+8 -9
View File
@@ -29,15 +29,13 @@
* @constructor
* @private
*/
var HashElement = cc._Class.extend({
ctor:function () {
this.actions = [];
this.target = null; //ccobject
this.actionIndex = 0;
this.currentAction = null; //CCAction
this.paused = false;
}
});
var HashElement = function () {
this.actions = [];
this.target = null; //ccobject
this.actionIndex = 0;
this.currentAction = null; //CCAction
this.paused = false;
};
/**
* !#en
@@ -91,6 +89,7 @@ cc.ActionManager = cc._Class.extend(/** @lends cc.ActionManager# */{
element.actionIndex = 0;
element.currentAction = null;
element.paused = false;
element.target = null;
this._elementPool.push(element);
},
+13 -8
View File
@@ -128,7 +128,7 @@ cc.rendererWebGL = {
_cacheToBufferCmds: {}, // an array saves the renderer commands need for cache to other canvas
_cacheInstanceIds: [],
_currentID: 0,
_clearColor: {r: 0, g: 0, b: 0, a: 255}, //background color,default BLACK
_clearColor: {r: 0, g: 0, b: 0, a: 1}, //background color,default BLACK
init: function () {
var gl = cc._renderContext;
@@ -311,17 +311,22 @@ cc.rendererWebGL = {
},
_updateBatchedInfo: function (texture, blendFunc, shaderProgram) {
if (texture) {
if (texture !== _batchedInfo.texture ||
blendFunc.src !== _batchedInfo.blendSrc ||
blendFunc.dst !== _batchedInfo.blendDst ||
shaderProgram !== _batchedInfo.shader) {
// Draw batched elements
this._batchRendering();
// Update _batchedInfo
_batchedInfo.texture = texture;
}
if (blendFunc) {
_batchedInfo.blendSrc = blendFunc.src;
_batchedInfo.blendDst = blendFunc.dst;
}
if (shaderProgram) {
_batchedInfo.shader = shaderProgram;
return true;
}
else {
return false;
}
},
+27 -16
View File
@@ -36,7 +36,6 @@ sp._SGSkeleton = _ccsg.Node.extend({
_premultipliedAlpha: false,
_ownsSkeletonData: null,
_atlas: null,
_blendFunc: null,
ctor: function(skeletonDataFile, atlasFile, scale) {
_ccsg.Node.prototype.ctor.call(this);
@@ -57,7 +56,6 @@ sp._SGSkeleton = _ccsg.Node.extend({
init: function () {
_ccsg.Node.prototype.init.call(this);
this._premultipliedAlpha = (cc._renderType === cc.game.RENDER_TYPE_WEBGL && cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA);
this._blendFunc = {src: cc.BLEND_SRC, dst: cc.BLEND_DST};
this.scheduleUpdate();
},
@@ -147,7 +145,7 @@ sp._SGSkeleton = _ccsg.Node.extend({
*/
getBoundingBox: function () {
var minX = cc.macro.FLT_MAX, minY = cc.macro.FLT_MAX, maxX = cc.macro.FLT_MIN, maxY = cc.macro.FLT_MIN;
var scaleX = this.getScaleX(), scaleY = this.getScaleY(), vertices = [],
var scaleX = this.getScaleX(), scaleY = this.getScaleY(), vertices,
slots = this._skeleton.slots, VERTEX = spine.RegionAttachment;
for (var i = 0, slotCount = slots.length; i < slotCount; ++i) {
@@ -289,27 +287,27 @@ sp._SGSkeleton = _ccsg.Node.extend({
},
/**
* Returns the blendFunc of sp._SGSkeleton.
* Returns the blendFunc of sp._SGSkeleton, actually it's the first slot's blend function.
* @returns {BlendFunc}
*/
getBlendFunc: function () {
return this._blendFunc;
var slot = this._skeleton.drawOrder[0];
if (slot) {
var blend = this._renderCmd._getBlendFunc(slot.data.blendMode, this._premultipliedAlpha);
return blend;
}
else {
return {};
}
},
/**
* Sets the blendFunc of sp._SGSkeleton.
* Sets the blendFunc of sp._SGSkeleton, it won't have any effect for skeleton, skeleton is using slot's data to determine the blend function.
* @param {BlendFunc|Number} src
* @param {Number} [dst]
*/
setBlendFunc: function (src, dst) {
var locBlendFunc = this._blendFunc;
if (dst === undefined) {
locBlendFunc.src = src.src;
locBlendFunc.dst = src.dst;
} else {
locBlendFunc.src = src;
locBlendFunc.dst = dst;
}
return;
},
/**
@@ -324,6 +322,19 @@ sp._SGSkeleton = _ccsg.Node.extend({
// fireball#2856
Object.defineProperty(sp._SGSkeleton.prototype, 'opacityModifyRGB', {
get: sp._SGSkeleton.prototype.isOpacityModifyRGB
var proto = sp._SGSkeleton.prototype;
Object.defineProperty(proto, 'opacityModifyRGB', {
get: proto.isOpacityModifyRGB
});
// For renderer webgl to identify skeleton's default blend function
Object.defineProperty(proto, '_blendFunc', {
get: proto.getBlendFunc
});
// For renderer webgl to identify skeleton's default texture
Object.defineProperty(proto, '_texture', {
get: function () {
return this._renderCmd._currTexture;
}
});
-15
View File
@@ -28,21 +28,6 @@
var spine = sp.spine;
var animEventType = sp.AnimationEventType;
sp._atlasPage_createTexture_webGL = function (self, path) {
var texture = cc.textureCache.addImage(path);
self.rendererObject = new cc.TextureAtlas(texture, 128);
self.width = texture.getPixelWidth();
self.height = texture.getPixelHeight();
};
sp._atlasPage_createTexture_canvas = function(self, path) {
self._texture = cc.textureCache.addImage(path);
};
sp._atlasPage_disposeTexture = function (self) {
self.rendererObject.release();
};
sp._atlasLoader = {
spAtlasFile:null,
setAtlasFile:function(spAtlasFile){
+23
View File
@@ -1,3 +1,26 @@
/****************************************************************************
Copyright (c) 2017 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
sp.SkeletonTexture = cc.Class({
name: 'sp.SkeletonTexture',
+49 -52
View File
@@ -30,38 +30,21 @@ sp._SGSkeleton.WebGLRenderCmd = function (renderableObject) {
this._needDraw = true;
this._matrix = new cc.math.Matrix4();
this._matrix.identity();
this._currTexture = null;
this._currBlendFunc = {};
this.vertexType = cc.renderer.VertexType.CUSTOM;
this.setShaderProgram(cc.shaderCache.programForKey(cc.macro.SHADER_POSITION_TEXTURECOLOR));
this.setShaderProgram(cc.shaderCache.programForKey(cc.macro.SHADER_SPRITE_POSITION_TEXTURECOLOR));
};
var proto = sp._SGSkeleton.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.WebGLRenderCmd.prototype);
proto.constructor = sp._SGSkeleton.WebGLRenderCmd;
proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset){
// rendering the cached data first
cc.renderer._batchRendering();
vertexDataOffset = 0;
var node = this._node;
var color = this._displayedColor, locSkeleton = node._skeleton;
var textureAtlas, attachment, slot, i, n;
var attachment, slot, i, n;
var premultiAlpha = node._premultipliedAlpha;
var blendMode = -1;
var dataInited = false;
var cachedVertices = 0;
var wt = this._worldTransform, mat = this._matrix.mat;
mat[0] = wt.a;
mat[4] = wt.c;
mat[12] = wt.tx;
mat[1] = wt.b;
mat[5] = wt.d;
mat[13] = wt.ty;
this._shaderProgram.use();
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
locSkeleton.r = color.r / 255;
locSkeleton.g = color.g / 255;
@@ -101,27 +84,19 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset){
continue;
}
var regionTextureAtlas = node.getTextureAtlas(attachment);
// init data at the first time
if (!dataInited) {
textureAtlas = regionTextureAtlas;
blendMode = slot.data.blendMode;
cc.renderer._updateBatchedInfo(textureAtlas.texture.getRealTexture(), this._getBlendFunc(blendMode, premultiAlpha), this.getShaderProgram());
dataInited = true;
}
// if data changed or the vertices will be overflow
if ((cachedVertices + vertCount) * 6 > f32buffer.length ||
textureAtlas.texture.getRealTexture() !== regionTextureAtlas.texture.getRealTexture() ||
blendMode !== slot.data.blendMode) {
// Broken for changing batch info
this._currTexture = regionTextureAtlas.texture.getRealTexture();
var batchBroken = cc.renderer._updateBatchedInfo(this._currTexture, this._getBlendFunc(slot.data.blendMode, premultiAlpha), this._shaderProgram);
// Broken for vertex data overflow
if (!batchBroken && vertexDataOffset + vertCount * 6 > f32buffer.length) {
// render the cached data
cc.renderer._batchRendering();
batchBroken = true;
}
if (batchBroken) {
vertexDataOffset = 0;
cachedVertices = 0;
// update the batched info
textureAtlas = regionTextureAtlas;
blendMode = slot.data.blendMode;
cc.renderer._updateBatchedInfo(textureAtlas.texture.getRealTexture(), this._getBlendFunc(blendMode, premultiAlpha), this.getShaderProgram());
}
// update the vertex buffer
@@ -148,16 +123,21 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset){
}
// update the index data
cachedVertices += vertCount;
vertexDataOffset += vertCount * 6;
}
// render the left vertices
if (cachedVertices > 0) {
cc.renderer._batchRendering();
}
if (node._debugBones || node._debugSlots) {
// flush previous vertices
cc.renderer._batchRendering();
var wt = this._worldTransform, mat = this._matrix.mat;
mat[0] = wt.a;
mat[4] = wt.c;
mat[12] = wt.tx;
mat[1] = wt.b;
mat[5] = wt.d;
mat[13] = wt.ty;
cc.math.glMatrixMode(cc.math.KM_GL_MODELVIEW);
//cc.math.glPushMatrixWitMat4(this._matrix);
cc.current_stack.stack.push(cc.current_stack.top);
@@ -209,7 +189,7 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset){
};
proto._getBlendFunc = function (blendMode, premultiAlpha) {
var ret = {};
var ret = this._currBlendFunc;
switch (blendMode) {
case spine.BlendMode.Normal:
ret.src = premultiAlpha ? cc.macro.ONE : cc.macro.SRC_ALPHA;
@@ -254,19 +234,28 @@ proto._uploadRegionAttachmentData = function(attachment, slot, premultipliedAlph
nodeB = nodeColor.b,
nodeA = this._displayedOpacity;
var vertices = attachment.updateWorldVertices(slot, premultipliedAlpha);
var wt = this._worldTransform,
wa = wt.a, wb = wt.b, wc = wt.c, wd = wt.d,
wx = wt.tx, wy = wt.ty,
z = this._node.vertexZ;
var offset = vertexDataOffset;
// generate 6 vertices data (two triangles) from the quad vertices
// using two angles : (0, 1, 2) & (0, 2, 3)
for (var i = 0; i < 6; i++) {
var srcIdx = i < 4 ? i % 3 : i - 2;
var vx = vertices[srcIdx * 8],
vy = vertices[srcIdx * 8 + 1];
var x = vx * wa + vy * wb + wx,
y = vx * wc + vy * wd + wy;
var r = vertices[srcIdx * 8 + 2] * nodeR,
g = vertices[srcIdx * 8 + 3] * nodeG,
b = vertices[srcIdx * 8 + 4] * nodeB,
a = vertices[srcIdx * 8 + 5] * nodeA;
var color = ((a<<24) | (b<<16) | (g<<8) | r);
f32buffer[offset] = vertices[srcIdx * 8];
f32buffer[offset + 1] = vertices[srcIdx * 8 + 1];
f32buffer[offset + 2] = this._node.vertexZ;
f32buffer[offset] = x;
f32buffer[offset + 1] = y;
f32buffer[offset + 2] = z;
ui32buffer[offset + 3] = color;
f32buffer[offset + 4] = vertices[srcIdx * 8 + 6];
f32buffer[offset + 5] = vertices[srcIdx * 8 + 7];
@@ -286,6 +275,10 @@ proto._uploadRegionAttachmentData = function(attachment, slot, premultipliedAlph
};
proto._uploadMeshAttachmentData = function(attachment, slot, premultipliedAlpha, f32buffer, ui32buffer, vertexDataOffset) {
var wt = this._worldTransform,
wa = wt.a, wb = wt.b, wc = wt.c, wd = wt.d,
wx = wt.tx, wy = wt.ty,
z = this._node.vertexZ;
// get the vertex data
var vertices = attachment.updateWorldVertices(slot, premultipliedAlpha);
var offset = vertexDataOffset;
@@ -295,18 +288,22 @@ proto._uploadMeshAttachmentData = function(attachment, slot, premultipliedAlpha,
nodeB = nodeColor.b,
nodeA = this._displayedOpacity;
for (var i = 0, n = vertices.length; i < n; i += 8) {
var vx = vertices[i],
vy = vertices[i + 1];
var x = vx * wa + vy * wb + wx,
y = vx * wc + vy * wd + wy;
var r = vertices[i + 2] * nodeR,
g = vertices[i + 3] * nodeG,
b = vertices[i + 4] * nodeB,
a = vertices[i + 5] * nodeA;
var color = ((a<<24) | (b<<16) | (g<<8) | r);
f32buffer[offset] = vertices[i];
f32buffer[offset + 1] = vertices[i + 1];
f32buffer[offset + 2] = this._node.vertexZ;
f32buffer[offset] = x;
f32buffer[offset + 1] = y;
f32buffer[offset + 2] = z;
ui32buffer[offset + 3] = color;
f32buffer[offset + 4] = vertices[i + 6];
f32buffer[offset + 5] = vertices[i + 7];
offset += 6;
}
};
};
+27
View File
@@ -0,0 +1,27 @@
Spine Runtimes Software License v2.5
Copyright (c) 2013-2016, Esoteric Software
All rights reserved.
You are granted a perpetual, non-exclusive, non-sublicensable, and
non-transferable license to use, install, execute, and perform the Spine
Runtimes software and derivative works solely for personal or internal
use. Without the written permission of Esoteric Software (see Section 2 of
the Spine Software License Agreement), you may not (a) modify, translate,
adapt, or develop new applications using the Spine Runtimes or otherwise
create derivative works or improvements of the Spine Runtimes or (b) remove,
delete, alter, or obscure any trademarks or any copyright, trademark, patent,
or other intellectual property or proprietary rights notices on or in the
Software, including any copy thereof. Redistributions in binary or source
form must include this license and terms.
THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
+2
View File
@@ -1,3 +1,5 @@
// Spine runtime version 3.5.35
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }