-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile.js
More file actions
198 lines (157 loc) · 4.53 KB
/
Copy pathcompile.js
File metadata and controls
198 lines (157 loc) · 4.53 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"use strict"
//DEPENDENCIES:
//Vox2PNG (https://github.com/StijnBrouwer/vox2png)
//vox2png assets/voxdrafts/samidle1.vox assets/fake3ds/samidle1.png
let jsonfile = require('jsonfile')
var fs = require('fs'),
PNG = require('pngjs').PNG
let recursive = require('recursive-readdir')
let exec = require('child_process').exec
var shell = require('shelljs')
let dirPath = "./assets/voxes/"
let voxes = []
//where we save each png to
let pngSaveDirectories = []
//vox file names
let voxFileNames = []
//collect voxes that we need to convert
new Promise(function(resolve, reject){
recursive(dirPath, ['*.exe'], function (err, files){
files.forEach(file => {
if(file.indexOf(".vox") != -1){
voxes.push(file)
}
})
resolve()
})
}).then(createPNGs)
//use vox2png to generate PNGs from .vox files
function createPNGs(){
//vox directory without vox file
let voxFileDirectories = []
for(let i = 0; i < voxes.length; i++){
let voxPath = voxes[i]
let voxPathArr = voxPath.split("\\")
let voxFile = voxPathArr[voxPathArr.length - 1]
voxFileNames.push(voxFile)
let voxDir = voxPath.replace(voxFile, "")
voxFileDirectories.push(voxDir.replace(/\\/g, "/"))
let pngSaveDir = voxDir.replace("voxes", "pngs").replace(/\\/g, "\\\\")
pngSaveDirectories.push(pngSaveDir)
//make save directory if it doesn't exist
shell.mkdir('-p', pngSaveDir)
}
//run vox2png on every .vox file
let vox2pngPromises = []
for(let i = 0; i < voxes.length; i++){
vox2pngPromises.push(
new Promise(function(resolve, reject){
//vox2png command
let cmd = "vox2png " + voxFileDirectories[i]
+ voxFileNames[i] + " " + pngSaveDirectories[i].replace(/\\\\/g, "/")
+ voxFileNames[i].replace(".vox", ".png")
exec(cmd, function(error, stdout, stderr) {
if(error)
console.log(error)
resolve()
})
})
)
}
Promise.all(vox2pngPromises).then(function(){
createAtlases()
})
}
function createAtlases(){
let atlastPromises = []
let assets = {}
//create an atlas for each png
for(let i = 0; i < voxes.length; i++){
let pngInfo
let pngDir = pngSaveDirectories[i].replace(/\\\\/g, "/")
let pngName = voxFileNames[i].replace(".vox", ".png")
atlastPromises.push(
new Promise(function(resolve, reject){
fs.createReadStream(pngDir + pngName)
.pipe(new PNG({
filterType: 4
}))
.on('parsed', function() {
let atlas = fillInAtlas(this, voxFileNames[i].replace(".vox", ""))
let jsonName = pngName.replace(".png", ".json")
jsonfile.writeFile(
pngDir + jsonName,
atlas,
{spaces: 4},
function(err) {
// console.error("writefile error: " + err)
}
)
assignObject(assets,
['spritePaths', voxFileNames[i].replace(".vox", "")],
pngDir + jsonName)
resolve()
})
})
)
}
Promise.all(atlastPromises).then(function(){
jsonfile.writeFile("assets/assets.json", assets, {spaces: 4}, function(err) {
// console.error(err)
})
})
}
function fillInAtlas(result, name){
let atlas = {}
let frameIter = -1
atlas['meta'] = {}
atlas['meta']['image'] = name + '.png'
atlas['meta']['size'] = {
"w": result.width,
"h": result.height
}
atlas['meta']['scale'] = '1'
atlas['frames'] = {}
//get number from vox name
let frameW = parseInt(name.replace( /^\D+/g, ''))
let frameH = parseInt(name.replace( /^\D+/g, ''))
let numFrames = result.width / (frameW + 1)
for(let i = 0; i < numFrames; i++){
var n = name + "_" + i
atlas['frames'][n] = {}
atlas['frames'][n]['frame'] = {}
atlas['frames'][n]['frame'].x = (i * frameW) + i
atlas['frames'][n]['frame'].y = 0
atlas['frames'][n]['frame'].w = frameW
atlas['frames'][n]['frame'].h = frameH
atlas['frames'][n]['rotated'] = false
atlas['frames'][n]['trimmed'] = false
atlas['frames'][n]['spriteSourceSize'] = {}
atlas['frames'][n]['spriteSourceSize'].x = 0
atlas['frames'][n]['spriteSourceSize'].y = 0
atlas['frames'][n]['spriteSourceSize'].w = frameW
atlas['frames'][n]['spriteSourceSize'].h = frameH
atlas['frames'][n]['sourceSize'] = {}
atlas['frames'][n]['sourceSize'].w = frameW
atlas['frames'][n]['sourceSize'].h = frameH
}
return atlas
}
/**
* Shove an object into a nested object
*
* @param {[type]}
* @param {[type]}
* @param {[type]}
* @return {[type]}
*/
function assignObject(obj, keyPath, value) {
let lastKeyIndex = keyPath.length - 1
for (let i = 0; i < lastKeyIndex; i++) {
let key = keyPath[i]
if (!(key in obj))
obj[key] = {}
obj = obj[key]
}
obj[keyPath[lastKeyIndex]] = value
}