From 145b2fcecf103da9fbb44b77ec24f4bc9ea8dede Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Mon, 9 Oct 2017 14:59:19 +0800 Subject: [PATCH] support spine --- extensions/spine/Skeleton.js | 47 ++++++++++------ extensions/spine/SkeletonData.js | 77 +++++++++------------------ extensions/spine/editor/spine-meta.js | 22 ++++---- 3 files changed, 66 insertions(+), 80 deletions(-) diff --git a/extensions/spine/Skeleton.js b/extensions/spine/Skeleton.js index 17fdec539f..e1c1469317 100644 --- a/extensions/spine/Skeleton.js +++ b/extensions/spine/Skeleton.js @@ -402,33 +402,46 @@ sp.Skeleton = cc.Class({ }, _createSgNode: function () { - if (this.skeletonData/* && self.atlasFile*/) { + var skeletonData = this.skeletonData; + if (skeletonData/* && self.atlasFile*/) { if (CC_JSB) { - var uuid = this.skeletonData._uuid; + var uuid = skeletonData._uuid; if ( !uuid ) { cc.errorID(7504); return null; } - var jsonFile = this.skeletonData.nativeUrl; - var atlasFile = this.skeletonData.atlasUrl; - if (atlasFile) { - if (typeof atlasFile !== 'string') { - cc.errorID(7505); - return null; - } - try { - return new sp._SGSkeletonAnimation(jsonFile, atlasFile, this.skeletonData.scale); - } - catch (e) { - cc._throw(e); - } + var jsonFile = skeletonData.nativeUrl; + var atlasText = skeletonData.atlasText; + if (!atlasText) { + cc.errorID(7508, skeletonData.name); + return null; } + var texValues = skeletonData.textures; + var texKeys = skeletonData.textureNames; + if ( !(texValues && texValues.length > 0 && texKeys && texKeys.length > 0) ) { + cc.errorID(7507, skeletonData.name); + return null; + } + var textures = {}; + for (var i = 0; i < texValues.length; ++i) { + textures[texKeys[i]] = texValues[i]; + } + + var sgNode = new sp._SGSkeletonAnimation(); + try { + sp._initSkeletonRenderer(sgNode, jsonFile, atlasText, textures, skeletonData.scale); + } + catch (e) { + cc._throw(e); + return null; + } + return sgNode; } else { - var data = this.skeletonData.getRuntimeData(); + var data = skeletonData.getRuntimeData(); if (data) { try { - return new sp._SGSkeletonAnimation(data, null, this.skeletonData.scale); + return new sp._SGSkeletonAnimation(data, null, skeletonData.scale); } catch (e) { cc._throw(e); diff --git a/extensions/spine/SkeletonData.js b/extensions/spine/SkeletonData.js index 6b76d9b9b6..4b9204cbf7 100644 --- a/extensions/spine/SkeletonData.js +++ b/extensions/spine/SkeletonData.js @@ -27,48 +27,6 @@ * @module sp */ -// implements a simple texture loader like sp._atlasLoader -var TextureLoader = !CC_JSB && cc.Class({ - ctor: function () { - // {SkeletonData} - this.asset = arguments[0]; - }, - getTexture: function (line) { - //var name = cc.path.mainFileName(line); - //var textures = this.asset.textures; - //for (var i = 0; i < textures.length; i++) { - // var tex = textures[i]; - // if (tex.name === name) { - // return tex; - // } - //} - var textures = this.asset.textures; - for (var i = 0; i < textures.length; i++) { - var texture = textures[i]; - var url = texture && texture.url; - if (url.endsWith(line)) { - var tex = new sp.SkeletonTexture({ width: texture.getPixelWidth(), height: texture.getPixelHeight() }); - tex.setRealTexture(texture); - return tex; - } - } - return null; - }, - load: function (line) { - var texture = this.getTexture(line); - if (texture) { - return texture; - } - else { - cc.errorID(7506, line); - return null; - } - }, - unload: function (obj) { - //page.rendererObject.release(); - } -}); - /** * !#en The skeleton data of spine. * !#zh Spine 的 骨骼数据。 @@ -117,13 +75,6 @@ var SkeletonData = cc.Class({ } }, - // Fallback to raw asset for JSB ------------------- - atlasUrl: { - default: '', - url: cc.RawAsset - }, - // ------------------------------------------------- - /** * @property {Texture2D[]} textures */ @@ -132,6 +83,15 @@ var SkeletonData = cc.Class({ type: [cc.Texture2D] }, + /** + * @property {String[]} textureNames + * @private + */ + textureNames: { + default: [], + type: [cc.String] + }, + /** * !#en * A scale can be specified on the JSON or binary loader which will scale the bone positions, @@ -199,7 +159,7 @@ var SkeletonData = cc.Class({ } - if ( !(this.textures && this.textures.length > 0) ) { + if ( !(this.textures && this.textures.length > 0 && this.textureNames && this.textureNames.length > 0) ) { if ( !quiet ) { cc.errorID(7507, this.name); } @@ -259,6 +219,20 @@ var SkeletonData = cc.Class({ // PRIVATE + _getTexture: !CC_JSB && function (line) { + var names = this.textureNames; + for (var i = 0; i < names.length; i++) { + if (names[i] === line) { + var texture = this.textures[i]; + var tex = new sp.SkeletonTexture({ width: texture.getPixelWidth(), height: texture.getPixelHeight() }); + tex.setRealTexture(texture); + return tex; + } + } + cc.errorID(7506, line); + return null; + }, + /** * @method _getAtlas * @param {boolean} [quiet=false] @@ -277,8 +251,7 @@ var SkeletonData = cc.Class({ return null; } - var loader = new TextureLoader(this); - return this._atlasCache = new sp.spine.TextureAtlas(this.atlasText, loader.load.bind(loader)); + return this._atlasCache = new sp.spine.TextureAtlas(this.atlasText, this._getTexture.bind(this)); } }); diff --git a/extensions/spine/editor/spine-meta.js b/extensions/spine/editor/spine-meta.js index 1a60e36d54..5e9fdb63eb 100644 --- a/extensions/spine/editor/spine-meta.js +++ b/extensions/spine/editor/spine-meta.js @@ -54,8 +54,10 @@ function loadAtlasText (skeletonPath, callback) { class TextureParser { constructor (atlasPath) { this.atlasPath = atlasPath; - // the loaded texture array + // array of loaded texture uuid this.textures = []; + // array of corresponding line + this.textureNames = []; } load (line) { var name = Path.basename(line); @@ -65,6 +67,7 @@ class TextureParser { if (uuid) { console.log('UUID is initialized for "%s".', path); this.textures.push(uuid); + this.textureNames.push(line); var tex = new Spine.Texture({}); tex.setFilters = function() {}; tex.setWraps = function() {}; @@ -80,7 +83,6 @@ class TextureParser { return null; } - unload () {} } const RAW_SKELETON_FILE = 'raw-skeleton.json'; @@ -103,7 +105,7 @@ class SpineMeta extends CustomAssetMeta { //this.textures[0] = value; } - static version () { return '1.1.0'; } + static version () { return '1.2.0'; } static defaultType () { return 'spine'; } @@ -171,30 +173,28 @@ class SpineMeta extends CustomAssetMeta { // parse atlas textures var textureParser = new TextureParser(res.atlasPath); + try { new Spine.TextureAtlas(res.data, textureParser.load.bind(textureParser)); } catch (err) { return cb(new Error(`Failed to load atlas file: "${res.atlasPath}". ${err.stack || err}`)); } - if (textureParser.textures.length > 0) { - this.textures = textureParser.textures; - } - asset.textures = this.textures.map(Editor.serialize.asAsset); + this.textures = textureParser.textures; + asset.textures = textureParser.textures.map(Editor.serialize.asAsset); + asset.textureNames = textureParser.textureNames; // asset.atlasText = res.data; // save raw assets for JSB.. - var atlasUuid = db.fspathToUuid(res.atlasPath); - asset.atlasUrl = db.uuidToUrl(atlasUuid); - db.mkdirForAsset(this.uuid); var rawJsonPath = Path.join(db._uuidToImportPathNoExt(this.uuid), RAW_SKELETON_FILE); Fs.copySync(fspath, rawJsonPath); asset._setRawAsset(RAW_SKELETON_FILE); - + + var atlasUuid = db.fspathToUuid(res.atlasPath); this.atlas = atlasUuid; // save for dest() //