-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsplitAtlas.js
71 lines (71 loc) · 2.58 KB
/
splitAtlas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function splitAtlas() {
var gm = require('gm');
let fs = require('fs');
if(process.argv.length<=2){
console.error("请查看输入命令是否正确");
}
let atlasname = process.argv[2];
fs.readFile(`./atlas/${atlasname}.png`, function (err, data) {
if (err) {
console.error("请查看png是否正确");
return console.error(err);
}
fs.readFile(`./atlas/${atlasname}.atlas`, function (err, data) {
if (err) {
console.error("请查看atlas是否正确");
return console.error(err);
}
console.log("都加载完成")
/**
* 1.加载图片及配置文件
* 2.读取配置文件
* 3.创建图片信息对象
* 4.裁切图片
* 5.导出图片到新目录
*/
let text = JSON.parse(data.toString());//将字符串转换为json对象
let imgInfo = {};
fs.exists(`${atlasname}`, function (exists) {
if (exists) {
console.log("文件存在")
}
if (!exists) {
fs.mkdir(`${atlasname}`, function (error) {
if (error) {
return console.log(error);
}
console.log('创建目录成功');
})
}
})
function copyIt(from, to) {
fs.writeFileSync(to, fs.readFileSync(from));
}
for (let i in text.frames) {
// i 为图片名称
// text.frames[i] 为图片信息
// console.log(text.frames[i]);
imgInfo['name'] = i;
let x = text.frames[i].frame.x;
let y = text.frames[i].frame.y;
let h = text.frames[i].frame.h;
let w = text.frames[i].frame.w;
gm(`./atlas/${atlasname}.png`)
.crop(w, h, x, y)
.write(`${i}`,
function (err) {
if (!err) {
console.log(`生成${i}成功`);
copyIt(`${i}`, `./${atlasname}/${i}`);
fs.unlink(`${i}`);
}
else {
console.log(`切图失败`);
}
}
);
}
})
})
}
splitAtlas();