Skip to content

Commit

Permalink
upgrade spine (cocos#3721)
Browse files Browse the repository at this point in the history
* Update spine-assembler.js

spine骨骼动画一直播放导致内存不断上涨

* Update spine-assembler.js

优化释放数据逻辑;格式化代码

* Update spine-assembler.js

删掉空行

* fix spine leak

* upgrade spine

* upgrade spine

* upgrade spine

* upgrade spine

* upgrade spine

* upgrade spine

* upgrade spine
  • Loading branch information
sunnylanwanjun authored and pandamicro committed Jan 10, 2019
1 parent 1ca330b commit 5969250
Show file tree
Hide file tree
Showing 10 changed files with 3,724 additions and 2,124 deletions.
115 changes: 115 additions & 0 deletions cocos2d/core/renderer/render-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -13718,6 +13718,16 @@ var templates = [
{ name: 'alphaTest', },
{ name: 'use2DPos', },
{ name: 'useColor', } ],
},
{
name: 'spine',
vert: '\n \nuniform mat4 viewProj;\n\n#ifdef use2DPos\n attribute vec2 a_position;\n#else\n attribute vec3 a_position;\n#endif\n\nattribute lowp vec4 a_color;\n#ifdef useTint\n attribute lowp vec4 a_color0;\n#endif\n\n#ifdef useModel\n uniform mat4 model;\n#endif\n\nattribute mediump vec2 a_uv0;\nvarying mediump vec2 uv0;\n\nvarying lowp vec4 v_light;\n#ifdef useTint\n varying lowp vec4 v_dark;\n#endif\n\nvoid main () {\n mat4 mvp;\n #ifdef useModel\n mvp = viewProj * model;\n #else\n mvp = viewProj;\n #endif\n\n #ifdef use2DPos\n vec4 pos = mvp * vec4(a_position, 0, 1);\n #else\n vec4 pos = mvp * vec4(a_position, 1);\n #endif\n\n v_light = a_color;\n #ifdef useTint\n v_dark = a_color0;\n #endif\n\n uv0 = a_uv0;\n\n gl_Position = pos;\n}',
frag: '\n \nuniform sampler2D texture;\nvarying mediump vec2 uv0;\n\n#ifdef alphaTest\n uniform lowp float alphaThreshold;\n#endif\n\nvarying lowp vec4 v_light;\n#ifdef useTint\n varying lowp vec4 v_dark;\n#endif\n\nvoid main () {\n vec4 texColor = texture2D(texture, uv0);\n vec4 finalColor;\n\n #ifdef useTint\n finalColor.a = v_light.a * texColor.a;\n finalColor.rgb = ((texColor.a - 1.0) * v_dark.a + 1.0 - texColor.rgb) * v_dark.rgb + texColor.rgb * v_light.rgb;\n #else\n finalColor = texColor * v_light;\n #endif\n\n #ifdef alphaTest\n if (finalColor.a <= alphaThreshold)\n discard;\n #endif\n\n gl_FragColor = finalColor;\n}',
defines: [
{ name: 'useModel', },
{ name: 'alphaTest', },
{ name: 'use2DPos', },
{ name: 'useTint', } ],
} ];

var shaders = {
Expand Down Expand Up @@ -14245,6 +14255,110 @@ var SpriteMaterial = (function (Material$$1) {

// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

var SpineMaterial = (function (Material$$1) {
function SpineMaterial() {
Material$$1.call(this, false);

var pass = new renderer.Pass('spine');
pass.setDepth(false, false);
pass.setCullMode(gfx.CULL_NONE);
pass.setBlend(
gfx.BLEND_FUNC_ADD,
gfx.BLEND_SRC_ALPHA, gfx.BLEND_ONE_MINUS_SRC_ALPHA,
gfx.BLEND_FUNC_ADD,
gfx.BLEND_SRC_ALPHA, gfx.BLEND_ONE_MINUS_SRC_ALPHA
);

var mainTech = new renderer.Technique(
['transparent'],
[
{ name: 'texture', type: renderer.PARAM_TEXTURE_2D } ],
[
pass
]
);

this._effect = new renderer.Effect(
[
mainTech ],
{

},
[
{ name: 'useModel', value: true },
{ name: 'alphaTest', value: false },
{ name: 'use2DPos', value: true },
{ name: 'useTint', value: false } ]
);

this._mainTech = mainTech;
this._texture = null;
}

if ( Material$$1 ) SpineMaterial.__proto__ = Material$$1;
SpineMaterial.prototype = Object.create( Material$$1 && Material$$1.prototype );
SpineMaterial.prototype.constructor = SpineMaterial;

var prototypeAccessors = { effect: { configurable: true },useModel: { configurable: true },use2DPos: { configurable: true },useTint: { configurable: false },texture: { configurable: true } };

prototypeAccessors.effect.get = function () {
return this._effect;
};

prototypeAccessors.useModel.get = function () {
return this._effect.getDefine('useModel');
};

prototypeAccessors.useModel.set = function (val) {
this._effect.define('useModel', val);
};

prototypeAccessors.use2DPos.get = function () {
return this._effect.getDefine('use2DPos');
};

prototypeAccessors.use2DPos.set = function (val) {
this._effect.define('use2DPos', val);
};

prototypeAccessors.useTint.get = function () {
return this._effect.getDefine('useTint');
};

prototypeAccessors.useTint.set = function (val) {
this._effect.define('useTint', val);
};

prototypeAccessors.texture.get = function () {
return this._texture;
};

prototypeAccessors.texture.set = function (val) {
if (this._texture !== val) {
this._texture = val;
this._effect.setProperty('texture', val.getImpl());
this._texIds['texture'] = val.getId();
}
};

SpineMaterial.prototype.clone = function clone () {
var copy = new SpineMaterial();
copy._mainTech.copy(this._mainTech);
copy.texture = this.texture;
copy.useModel = this.useModel;
copy.use2DPos = this.use2DPos;
copy.useTint = this.useTint;
copy._hash = this._hash;
return copy;
};

Object.defineProperties( SpineMaterial.prototype, prototypeAccessors );

return SpineMaterial;
}(Material));

// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

var GraySpriteMaterial = (function (Material$$1) {
function GraySpriteMaterial() {
Material$$1.call(this, false);
Expand Down Expand Up @@ -14612,6 +14726,7 @@ var renderEngine = {
Material: Material,

// materials
SpineMaterial: SpineMaterial,
SpriteMaterial: SpriteMaterial,
GraySpriteMaterial: GraySpriteMaterial,
StencilMaterial: StencilMaterial,
Expand Down
117 changes: 116 additions & 1 deletion cocos2d/core/renderer/render-engine.jsb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,17 @@ var templates = [
{ name: 'alphaTest', },
{ name: 'use2DPos', },
{ name: 'useColor', } ],
} ];
},
{
name: 'spine',
vert: '\n \nuniform mat4 viewProj;\n\n#ifdef use2DPos\n attribute vec2 a_position;\n#else\n attribute vec3 a_position;\n#endif\n\nattribute lowp vec4 a_color;\n#ifdef useTint\n attribute lowp vec4 a_color0;\n#endif\n\n#ifdef useModel\n uniform mat4 model;\n#endif\n\nattribute mediump vec2 a_uv0;\nvarying mediump vec2 uv0;\n\nvarying lowp vec4 v_light;\n#ifdef useTint\n varying lowp vec4 v_dark;\n#endif\n\nvoid main () {\n mat4 mvp;\n #ifdef useModel\n mvp = viewProj * model;\n #else\n mvp = viewProj;\n #endif\n\n #ifdef use2DPos\n vec4 pos = mvp * vec4(a_position, 0, 1);\n #else\n vec4 pos = mvp * vec4(a_position, 1);\n #endif\n\n v_light = a_color;\n #ifdef useTint\n v_dark = a_color0;\n #endif\n\n uv0 = a_uv0;\n\n gl_Position = pos;\n}',
frag: '\n \nuniform sampler2D texture;\nvarying mediump vec2 uv0;\n\n#ifdef alphaTest\n uniform lowp float alphaThreshold;\n#endif\n\nvarying lowp vec4 v_light;\n#ifdef useTint\n varying lowp vec4 v_dark;\n#endif\n\nvoid main () {\n vec4 texColor = texture2D(texture, uv0);\n vec4 finalColor;\n\n #ifdef useTint\n finalColor.a = v_light.a * v_dark.a;\n finalColor.rgb = ((texColor.a - 1.0) * v_dark.a + 1.0 - texColor.rgb) * v_dark.rgb + texColor.rgb * v_light.rgb;\n #else\n finalColor = texColor * v_light;\n #endif\n\n #ifdef alphaTest\n if (finalColor.a <= alphaThreshold)\n discard;\n #endif\n\n gl_FragColor = finalColor;\n}',
defines: [
{ name: 'useModel', },
{ name: 'alphaTest', },
{ name: 'use2DPos', },
{ name: 'useTint', } ],
} ];

var shaders = {
chunks: chunks,
Expand Down Expand Up @@ -2824,6 +2834,110 @@ var SpriteMaterial = (function (Material$$1) {

// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

var SpineMaterial = (function (Material$$1) {
function SpineMaterial() {
Material$$1.call(this, false);

var pass = new renderer.Pass('spine');
pass.setDepth(false, false);
pass.setCullMode(gfx.CULL_NONE);
pass.setBlend(
gfx.BLEND_FUNC_ADD,
gfx.BLEND_SRC_ALPHA, gfx.BLEND_ONE_MINUS_SRC_ALPHA,
gfx.BLEND_FUNC_ADD,
gfx.BLEND_SRC_ALPHA, gfx.BLEND_ONE_MINUS_SRC_ALPHA
);

var mainTech = new renderer.Technique(
['transparent'],
[
{ name: 'texture', type: renderer.PARAM_TEXTURE_2D } ],
[
pass
]
);

this._effect = new renderer.Effect(
[
mainTech ],
{

},
[
{ name: 'useModel', value: true },
{ name: 'alphaTest', value: false },
{ name: 'use2DPos', value: true },
{ name: 'useTint', value: false } ]
);

this._mainTech = mainTech;
this._texture = null;
}

if ( Material$$1 ) SpineMaterial.__proto__ = Material$$1;
SpineMaterial.prototype = Object.create( Material$$1 && Material$$1.prototype );
SpineMaterial.prototype.constructor = SpineMaterial;

var prototypeAccessors = { effect: { configurable: true },useModel: { configurable: true },use2DPos: { configurable: true },useTint: { configurable: false },texture: { configurable: true } };

prototypeAccessors.effect.get = function () {
return this._effect;
};

prototypeAccessors.useModel.get = function () {
return this._effect.getDefine('useModel');
};

prototypeAccessors.useModel.set = function (val) {
this._effect.define('useModel', val);
};

prototypeAccessors.use2DPos.get = function () {
return this._effect.getDefine('use2DPos');
};

prototypeAccessors.use2DPos.set = function (val) {
this._effect.define('use2DPos', val);
};

prototypeAccessors.useTint.get = function () {
return this._effect.getDefine('useTint');
};

prototypeAccessors.useTint.set = function (val) {
this._effect.define('useTint', val);
};

prototypeAccessors.texture.get = function () {
return this._texture;
};

prototypeAccessors.texture.set = function (val) {
if (this._texture !== val) {
this._texture = val;
this._effect.setProperty('texture', val.getImpl());
this._texIds['texture'] = val.getId();
}
};

SpineMaterial.prototype.clone = function clone () {
var copy = new SpineMaterial();
copy._mainTech.copy(this._mainTech);
copy.texture = this.texture;
copy.useModel = this.useModel;
copy.use2DPos = this.use2DPos;
copy.useTint = this.useTint;
copy._hash = this._hash;
return copy;
};

Object.defineProperties( SpineMaterial.prototype, prototypeAccessors );

return SpineMaterial;
}(Material));

// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

var GraySpriteMaterial = (function (Material$$1) {
function GraySpriteMaterial() {
Material$$1.call(this, false);
Expand Down Expand Up @@ -10458,6 +10572,7 @@ var renderEngine = {
Material: Material,

// materials
SpineMaterial: SpineMaterial,
SpriteMaterial: SpriteMaterial,
GraySpriteMaterial: GraySpriteMaterial,
StencilMaterial: StencilMaterial,
Expand Down
4 changes: 4 additions & 0 deletions cocos2d/core/renderer/webgl/render-component-walker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const dynamicAtlasManager = require('../utils/dynamic-atlas/manager');
const RenderFlow = require('../render-flow');
const QuadBuffer = require('./quad-buffer');
const MeshBuffer = require('./mesh-buffer');
const SpineBuffer = require('./spine-buffer');

let idGenerater = new (require('../../platform/id-generater'))('VertextFormat');

Expand Down Expand Up @@ -239,6 +240,9 @@ RenderComponentWalker.prototype = {
else if (type === 'quad') {
buffer = new QuadBuffer(this, vertextFormat);
}
else if (type === 'spine') {
buffer = new SpineBuffer(this, vertextFormat);
}
else {
cc.error(`Not support buffer type [${type}]`);
return null;
Expand Down
34 changes: 34 additions & 0 deletions cocos2d/core/renderer/webgl/spine-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var SpineBuffer = cc.Class({
name: 'cc.SpineBuffer',
extends: require('./mesh-buffer'),

requestStatic (vertexCount, indiceCount) {
let byteOffset = this.byteOffset + vertexCount * this._vertexBytes;
let indiceOffset = this.indiceOffset + indiceCount;

let byteLength = this._vData.byteLength;
let indiceLength = this._iData.length;
if (byteOffset > byteLength || indiceOffset > indiceLength) {
while (byteLength < byteOffset || indiceLength < indiceOffset) {
this._initVDataCount *= 2;
this._initIDataCount *= 2;

byteLength = this._initVDataCount * 4;
indiceLength = this._initIDataCount;
}

this._reallocBuffer();
}
},

adjust (vertexCount, indiceCount) {
this.vertexOffset += vertexCount;
this.indiceOffset += indiceCount;

this.byteOffset = this.byteOffset + vertexCount * this._vertexBytes;

this._dirty = true;
}
});

cc.SpineBuffer = module.exports = SpineBuffer;
10 changes: 10 additions & 0 deletions cocos2d/core/renderer/webgl/vertex-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ var vfmtPosUvColor = new gfx.VertexFormat([
vfmtPosUvColor.name = 'vfmtPosUvColor';
gfx.VertexFormat.XY_UV_Color = vfmtPosUvColor;

var vfmtPosUvTwoColor = new gfx.VertexFormat([
{ name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
{ name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
{ name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_UINT8, num: 4, normalize: true },
{ name: gfx.ATTR_COLOR0, type: gfx.ATTR_TYPE_UINT8, num: 4, normalize: true },
]);
vfmtPosUvTwoColor.name = 'vfmtPosUvTwoColor';
gfx.VertexFormat.XY_UV_Two_Color = vfmtPosUvTwoColor;

var vfmtPosUv = new gfx.VertexFormat([
{ name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
{ name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 }
Expand All @@ -59,6 +68,7 @@ gfx.VertexFormat.XY_Color = vfmtPosColor;
module.exports = {
vfmt3D,
vfmtPosUvColor,
vfmtPosUvTwoColor,
vfmtPosUv,
vfmtPosColor
};
1 change: 1 addition & 0 deletions editor/i18n/en/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ module.exports = {
"debug_slots": "Indicates whether show debug slots.",
"debug_bones": "Indicates whether show debug bones.",
"premultipliedAlpha": "Indicates whether to enable premultiplied alpha.",
"use_tint": "Indicates whether to use tint effect."
},
"dragon_bones": {
"dragon_bones_asset": "The json data contains the DragonBones information, drag the json file exported from DragonBones to get started.",
Expand Down
1 change: 1 addition & 0 deletions editor/i18n/zh/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ module.exports = {
"debug_slots": "是否显示 slot 的 debug 信息",
"debug_bones": "是否显示 bone 的 debug 信息",
"premultipliedAlpha": "是否启用贴图预乘",
"use_tint": "是否启用染色效果"
},
"dragon_bones": {
"dragon_bones_asset": "骨骼信息数据,拖拽 DragonBones 导出的骨骼动画信息 json 资源到这里来开始使用",
Expand Down
Loading

0 comments on commit 5969250

Please sign in to comment.