Skip to content

Commit

Permalink
tiledmap texture compress (cocos#5999)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylanwanjun authored and SantyWang committed Dec 31, 2019
1 parent cf82420 commit 4230553
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
8 changes: 5 additions & 3 deletions cocos2d/tilemap/CCTMXXMLParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function getPropertyList (node, map) {
* @param {Object} tsxMap
* @param {Object} textures
*/
cc.TMXMapInfo = function (tmxFile, tsxMap, textures, imageLayerTextures) {
cc.TMXMapInfo = function (tmxFile, tsxMap, textures, textureSizes, imageLayerTextures) {
this.properties = [];
this.orientation = null;
this.parentElement = null;
Expand Down Expand Up @@ -364,7 +364,7 @@ cc.TMXMapInfo = function (tmxFile, tsxMap, textures, imageLayerTextures) {

this._imageLayerTextures = null;

this.initWithXML(tmxFile, tsxMap, textures, imageLayerTextures);
this.initWithXML(tmxFile, tsxMap, textures, textureSizes, imageLayerTextures);
};
cc.TMXMapInfo.prototype = {
constructor: cc.TMXMapInfo,
Expand Down Expand Up @@ -650,14 +650,15 @@ cc.TMXMapInfo.prototype = {
* @param {Object} textures
* @return {Boolean}
*/
initWithXML (tmxString, tsxMap, textures, imageLayerTextures) {
initWithXML (tmxString, tsxMap, textures, textureSizes, imageLayerTextures) {
this._tilesets.length = 0;
this._layers.length = 0;
this._imageLayers.length = 0;

this._tsxMap = tsxMap;
this._textures = textures;
this._imageLayerTextures = imageLayerTextures;
this._textureSizes = textureSizes;

this._objectGroups.length = 0;
this._allChildren.length = 0;
Expand Down Expand Up @@ -817,6 +818,7 @@ cc.TMXMapInfo.prototype = {
tileset._tileSize = tilesetSize;
tileset.tileOffset = tileOffset;
tileset.sourceImage = this._textures[firstImageName];
tileset.imageSize = this._textureSizes[firstImageName] || tileset.imageSize;
if (!tileset.sourceImage) {
cc.errorID(7221, firstImageName);
}
Expand Down
4 changes: 3 additions & 1 deletion cocos2d/tilemap/CCTiledLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1300,8 +1300,9 @@ let TiledLayer = cc.Class({

let texIdMatIdx = this._texIdToMatIndex = {};
let textures = this._textures;
let matLen = tilesetIndexArr.length;

for (let i = 0; i < tilesetIndexArr.length; i++) {
for (let i = 0; i < matLen; i++) {
let tilesetIdx = tilesetIndexArr[i];
let texture = textures[tilesetIdx];

Expand All @@ -1318,6 +1319,7 @@ let TiledLayer = cc.Class({

texIdMatIdx[tilesetIdx] = i;
}
this._materials.length = matLen;
this.markForRender(true);
}
});
Expand Down
8 changes: 6 additions & 2 deletions cocos2d/tilemap/CCTiledMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,13 @@ let TiledMap = cc.Class({
if (file) {
let texValues = file.textures;
let texKeys = file.textureNames;
let texSizes = file.textureSizes;
let textures = {};
let textureSizes = {};
for (let i = 0; i < texValues.length; ++i) {
textures[texKeys[i]] = texValues[i];
let texName = texKeys[i];
textures[texName] = texValues[i];
textureSizes[texName] = texSizes[i];
}

let imageLayerTextures = {};
Expand All @@ -557,7 +561,7 @@ let TiledMap = cc.Class({
}
}

let mapInfo = new cc.TMXMapInfo(file.tmxXmlStr, tsxMap, textures, imageLayerTextures);
let mapInfo = new cc.TMXMapInfo(file.tmxXmlStr, tsxMap, textures, textureSizes, imageLayerTextures);
let tilesets = mapInfo.getTilesets();
if(!tilesets || tilesets.length === 0)
cc.logID(7241);
Expand Down
8 changes: 8 additions & 0 deletions cocos2d/tilemap/CCTiledMapAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ let TiledMapAsset = cc.Class({
*/
textureNames: [cc.String],

/**
* @property {Size[]} textureSizes
*/
textureSizes: {
default: [],
type: [cc.Size]
},

/**
* @property {Texture2D[]} imageLayerTextures
*/
Expand Down
28 changes: 21 additions & 7 deletions cocos2d/tilemap/editor/tiled-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const Url = require('fire-url');

const DOMParser = require('xmldom').DOMParser;
const TMX_ENCODING = { encoding: 'utf-8' };
const Sharp = require(Editor.url('app://editor/share/sharp'));

function searchDependFiles(tmxFile, tmxFileData, cb) {
async function searchDependFiles(tmxFile, tmxFileData, cb) {
var doc = new DOMParser().parseFromString(tmxFileData);
if (!doc) {
return cb(new Error(cc.debug.getError(7222, tmxFile)));
Expand All @@ -45,13 +46,22 @@ function searchDependFiles(tmxFile, tmxFileData, cb) {
var textures = [];
var tsxFiles = [];
var textureNames = [];
function parseTilesetImages(tilesetNode, sourcePath) {
var textureSizes = [];
async function parseTilesetImages(tilesetNode, sourcePath) {
var images = tilesetNode.getElementsByTagName('image');
for (var i = 0, n = images.length; i < n ; i++) {
var imageCfg = images[i].getAttribute('source');
if (imageCfg) {
var imgPath = Path.join(Path.dirname(sourcePath), imageCfg);
textures.push(imgPath);

// Since it is possible to compress the texture at build time, meta file must save the original size of the texture when importing it
let metaData = await Sharp(imgPath).metadata();
textureSizes.push(cc.size(
metaData.width,
metaData.height
));

let textureName = Path.relative(Path.dirname(sourcePath), imgPath);
textureName = textureName.replace(/\\/g, '\/');
textureNames.push(textureName);
Expand All @@ -72,15 +82,15 @@ function searchDependFiles(tmxFile, tmxFileData, cb) {
var tsxContent = Fs.readFileSync(tsxPath, 'utf-8');
var tsxDoc = new DOMParser().parseFromString(tsxContent);
if (tsxDoc) {
parseTilesetImages(tsxDoc, tsxPath);
await parseTilesetImages(tsxDoc, tsxPath);
} else {
Editor.warn('Parse %s failed.', tsxPath);
}
}
}

// import images
parseTilesetImages(tileset, tmxFile);
await parseTilesetImages(tileset, tmxFile);
}

var imageLayerElements = rootElement.getElementsByTagName('imagelayer');
Expand All @@ -101,7 +111,8 @@ function searchDependFiles(tmxFile, tmxFileData, cb) {
}
}
}
cb(null, { textures, tsxFiles, textureNames, imageLayerTextures, imageLayerTextureNames});

cb(null, { textures, tsxFiles, textureNames, textureSizes, imageLayerTextures, imageLayerTextureNames});
}

const AssetRootUrl = 'db://assets/';
Expand All @@ -113,11 +124,12 @@ class TiledMapMeta extends CustomAssetMeta {
this._textures = [];
this._tsxFiles = [];
this._textureNames = [];
this._textureSizes = [];
this._imageLayerTextures = [];
this._imageLayerTextureNames = [];
}

static version () { return '2.0.2'; }
static version () { return '2.0.3'; }
static defaultType() { return 'tiled-map'; }

import (fspath, cb) {
Expand All @@ -135,6 +147,7 @@ class TiledMapMeta extends CustomAssetMeta {
this._textures = info.textures;
this._tsxFiles = info.tsxFiles;
this._textureNames = info.textureNames;
this._textureSizes = info.textureSizes;
this._imageLayerTextures = info.imageLayerTextures;
this._imageLayerTextureNames = info.imageLayerTextureNames;

Expand All @@ -153,12 +166,13 @@ class TiledMapMeta extends CustomAssetMeta {
return uuid ? Editor.serialize.asAsset(uuid) : null;
});
asset.textureNames = this._textureNames;
asset.textureSizes = this._textureSizes;

asset.imageLayerTextures = this._imageLayerTextures.map(p => {
var uuid = db.fspathToUuid(p);
return uuid ? Editor.serialize.asAsset(uuid) : null;
});
asset.imageLayerTextureNames = this._imageLayerTextureNames;
asset.imageLayerTextureNames = this._imageLayerTextureNames;

asset.tsxFiles = this._tsxFiles.map(p => {
var tsxPath = Path.join(Path.dirname(fspath), p);
Expand Down

0 comments on commit 4230553

Please sign in to comment.