Skip to content

Commit c44b97c

Browse files
committed
Improve spine rendering
1 parent aa69d8c commit c44b97c

File tree

8 files changed

+558
-497
lines changed

8 files changed

+558
-497
lines changed

cocos2d/core/textures/CCTexture2D.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
190190
*/
191191
handleLoadedTexture: function () {
192192
var self = this;
193-
if (self._textureLoaded) return;
194193
if (!self._htmlElementObj) {
195194
return;
196195
}

extensions/spine/CCSkeleton.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ sp.ATTACHMENT_TYPE = {
5959
SKINNED_MESH:3
6060
};
6161

62+
var spine = sp.spine;
63+
6264
/**
6365
* <p>
6466
* The skeleton of Spine. <br/>
@@ -78,7 +80,6 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
7880
_premultipliedAlpha: false,
7981
_ownsSkeletonData: null,
8082
_atlas: null,
81-
_blendFunc: null,
8283

8384
/**
8485
* The constructor of sp.Skeleton. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
@@ -105,7 +106,6 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
105106
init: function () {
106107
cc.Node.prototype.init.call(this);
107108
this._premultipliedAlpha = (cc._renderType === cc.game.RENDER_TYPE_WEBGL && cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA);
108-
this._blendFunc = {src: cc.BLEND_SRC, dst: cc.BLEND_DST};
109109
this.scheduleUpdate();
110110
},
111111

@@ -171,7 +171,7 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
171171

172172
/**
173173
* Initializes sp.Skeleton with Data.
174-
* @param {spine.SkeletonData|String} skeletonDataFile
174+
* @param {sp.spine.SkeletonData|String} skeletonDataFile
175175
* @param {String|spine.Atlas|spine.SkeletonData} atlasFile atlas filename or atlas data or owns SkeletonData
176176
* @param {Number} [scale] scale can be specified on the JSON or binary loader which will scale the bone positions, image sizes, and animation translations.
177177
*/
@@ -211,13 +211,13 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
211211
*/
212212
getBoundingBox: function () {
213213
var minX = cc.FLT_MAX, minY = cc.FLT_MAX, maxX = cc.FLT_MIN, maxY = cc.FLT_MIN;
214-
var scaleX = this.getScaleX(), scaleY = this.getScaleY(), vertices = [],
214+
var scaleX = this.getScaleX(), scaleY = this.getScaleY(), vertices,
215215
slots = this._skeleton.slots, VERTEX = spine.RegionAttachment;
216216

217217
for (var i = 0, slotCount = slots.length; i < slotCount; ++i) {
218218
var slot = slots[i];
219219
var attachment = slot.attachment;
220-
if (!attachment || ! (attachment instanceof spine.RegionAttachment))
220+
if (!attachment || !(attachment instanceof spine.RegionAttachment))
221221
continue;
222222
vertices = attachment.updateWorldVertices(slot, false);
223223
minX = Math.min(minX, vertices[VERTEX.X1] * scaleX, vertices[VERTEX.X4] * scaleX, vertices[VERTEX.X2] * scaleX, vertices[VERTEX.X3] * scaleX);
@@ -260,7 +260,7 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
260260
/**
261261
* Finds a bone by name. This does a string comparison for every bone.
262262
* @param {String} boneName
263-
* @returns {spine.Bone}
263+
* @returns {sp.spine.Bone}
264264
*/
265265
findBone: function (boneName) {
266266
return this._skeleton.findBone(boneName);
@@ -269,7 +269,7 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
269269
/**
270270
* Finds a slot by name. This does a string comparison for every slot.
271271
* @param {String} slotName
272-
* @returns {spine.Slot}
272+
* @returns {sp.spine.Slot}
273273
*/
274274
findSlot: function (slotName) {
275275
return this._skeleton.findSlot(slotName);
@@ -278,7 +278,7 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
278278
/**
279279
* Finds a skin by name and makes it the active skin. This does a string comparison for every skin. Note that setting the skin does not change which attachments are visible.
280280
* @param {string} skinName
281-
* @returns {spine.Skin}
281+
* @returns {sp.spine.Skin}
282282
*/
283283
setSkin: function (skinName) {
284284
return this._skeleton.setSkinByName(skinName);
@@ -288,7 +288,7 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
288288
* Returns the attachment for the slot and attachment name. The skeleton looks first in its skin, then in the skeleton data’s default skin.
289289
* @param {String} slotName
290290
* @param {String} attachmentName
291-
* @returns {spine.RegionAttachment|spine.BoundingBoxAttachment}
291+
* @returns {sp.spine.Attachment}
292292
*/
293293
getAttachment: function (slotName, attachmentName) {
294294
return this._skeleton.getAttachmentByName(slotName, attachmentName);
@@ -321,8 +321,8 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
321321

322322
/**
323323
* Sets skeleton data to sp.Skeleton.
324-
* @param {spine.SkeletonData} skeletonData
325-
* @param {spine.SkeletonData} ownsSkeletonData
324+
* @param {sp.spine.SkeletonData} skeletonData
325+
* @param {sp.spine.SkeletonData} ownsSkeletonData
326326
*/
327327
setSkeletonData: function (skeletonData, ownsSkeletonData) {
328328
if(skeletonData.width != null && skeletonData.height != null)
@@ -338,8 +338,8 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
338338

339339
/**
340340
* Return the renderer of attachment.
341-
* @param {spine.RegionAttachment|spine.BoundingBoxAttachment} regionAttachment
342-
* @returns {cc.Node}
341+
* @param {sp.spine.RegionAttachment|sp.spine.BoundingBoxAttachment} regionAttachment
342+
* @returns {sp.spine.TextureAtlasRegion}
343343
*/
344344
getTextureAtlas: function (regionAttachment) {
345345
return regionAttachment.region;
@@ -350,23 +350,23 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
350350
* @returns {cc.BlendFunc}
351351
*/
352352
getBlendFunc: function () {
353-
return this._blendFunc;
353+
var slot = this._skeleton.drawOrder[0];
354+
if (slot) {
355+
var blend = this._renderCmd._getBlendFunc(slot.data.blendMode, this._premultipliedAlpha);
356+
return blend;
357+
}
358+
else {
359+
return {};
360+
}
354361
},
355362

356363
/**
357-
* Sets the blendFunc of sp.Skeleton.
364+
* Sets the blendFunc of sp.Skeleton, it won't have any effect for skeleton, skeleton is using slot's data to determine the blend function.
358365
* @param {cc.BlendFunc|Number} src
359366
* @param {Number} [dst]
360367
*/
361368
setBlendFunc: function (src, dst) {
362-
var locBlendFunc = this._blendFunc;
363-
if (dst === undefined) {
364-
locBlendFunc.src = src.src;
365-
locBlendFunc.dst = src.dst;
366-
} else {
367-
locBlendFunc.src = src;
368-
locBlendFunc.dst = dst;
369-
}
369+
return;
370370
},
371371

372372
/**
@@ -378,6 +378,14 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
378378
}
379379
});
380380

381+
cc.defineGetterSetter(sp.Skeleton.prototype, "opacityModifyRGB", sp.Skeleton.prototype.isOpacityModifyRGB);
382+
383+
// For renderer webgl to identify skeleton's default texture and blend function
384+
cc.defineGetterSetter(sp.Skeleton.prototype, "_blendFunc", sp.Skeleton.prototype.getBlendFunc);
385+
cc.defineGetterSetter(sp.Skeleton.prototype, '_texture', function () {
386+
return this._renderCmd._currTexture;
387+
});
388+
381389
/**
382390
* Creates a skeleton object.
383391
* @deprecated since v3.0, please use new sp.Skeleton(skeletonDataFile, atlasFile, scale) instead.

extensions/spine/CCSkeletonAnimation.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ sp._atlasLoader = {
3232
load:function(line){
3333
var texturePath = cc.path.join(cc.path.dirname(this.spAtlasFile), line);
3434
var texture = cc.textureCache.addImage(texturePath);
35-
var tex = new sp.SkeletonTexture();
36-
tex._image = { width: texture.getPixelsWide(), height: texture.getPixelsHigh() };
35+
var tex = new sp.SkeletonTexture({ width: texture.getPixelsWide(), height: texture.getPixelsHigh() });
3736
tex.setRealTexture(texture);
3837
return tex;
3938
},
@@ -55,7 +54,7 @@ sp.ANIMATION_EVENT_TYPE = {
5554
EVENT: 5
5655
};
5756

58-
sp.TrackEntryListeners = function(startListener, endListener, completeListener, eventListener, interruptListener, disposeListener){
57+
sp.TrackEntryListeners = function (startListener, endListener, completeListener, eventListener, interruptListener, disposeListener) {
5958
this.startListener = startListener || null;
6059
this.endListener = endListener || null;
6160
this.completeListener = completeListener || null;
@@ -156,7 +155,7 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
156155

157156
/**
158157
* Sets animation state data to sp.SkeletonAnimation.
159-
* @param {spine.AnimationStateData} stateData
158+
* @param {sp.spine.AnimationStateData} stateData
160159
*/
161160
setAnimationStateData: function (stateData) {
162161
var state = new spine.AnimationState(stateData);
@@ -192,7 +191,7 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
192191
* @param {Number} trackIndex
193192
* @param {String} name
194193
* @param {Boolean} loop
195-
* @returns {spine.TrackEntry|null}
194+
* @returns {sp.spine.TrackEntry|null}
196195
*/
197196
setAnimation: function (trackIndex, name, loop) {
198197
var animation = this._skeleton.data.findAnimation(name);
@@ -209,7 +208,7 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
209208
* @param {String} name
210209
* @param {Boolean} loop
211210
* @param {Number} [delay=0]
212-
* @returns {spine.TrackEntry|null}
211+
* @returns {sp.spine.TrackEntry|null}
213212
*/
214213
addAnimation: function (trackIndex, name, loop, delay) {
215214
delay = delay == null ? 0 : delay;
@@ -221,10 +220,19 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
221220
return this._state.addAnimationWith(trackIndex, animation, loop, delay);
222221
},
223222

223+
/**
224+
* Find animation with specified name
225+
* @param {String} name
226+
* @returns {sp.spine.Animation|null}
227+
*/
228+
findAnimation: function (name) {
229+
return this._skeleton.data.findAnimation(name);
230+
},
231+
224232
/**
225233
* Returns track entry by trackIndex.
226234
* @param trackIndex
227-
* @returns {spine.TrackEntry|null}
235+
* @returns {sp.spine.TrackEntry|null}
228236
*/
229237
getCurrent: function (trackIndex) {
230238
return this._state.getCurrent(trackIndex);
@@ -254,6 +262,7 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
254262
update: function (dt) {
255263
this._super(dt);
256264
dt *= this._timeScale;
265+
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.contentDirty);
257266
this._state.update(dt);
258267
this._state.apply(this._skeleton);
259268
this._skeleton.updateWorldTransform();
@@ -339,4 +348,4 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
339348
*/
340349
sp.SkeletonAnimation.createWithJsonFile = sp.SkeletonAnimation.create = function (skeletonDataFile, atlasFile/* or atlas*/, scale) {
341350
return new sp.SkeletonAnimation(skeletonDataFile, atlasFile, scale);
342-
};
351+
};

0 commit comments

Comments
 (0)