-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.js
More file actions
255 lines (206 loc) · 7.92 KB
/
build.js
File metadata and controls
255 lines (206 loc) · 7.92 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const fs = require('fs/promises')
const path = require('path').posix
// 支持命令行参数:node build.js fromBasePath=models toBasePath=indexes isCompress=false
const buildOptions = setBuildOptions({
fromBasePath: 'models', // 模型文件夹,只读不写
toBasePath: 'indexes', // 索引文件夹,每次 build 完全重建
isCompress: false
})
const fromBasePath = buildOptions.fromBasePath
const toBasePath = buildOptions.toBasePath
const isCompress = buildOptions.isCompress
const modelTemplateJsonFileName = 'model.json'
const modelTexturesFolderPath = 'textures'
const modelsIndexJsonFileName = 'models.json'
const textureDefaultJsonFileName = 'default.json'
const texturesIndexJsonFileName = 'textures.json'
class Helper {
static async ensureExistFolder(folderPath) {
try {
await fs.stat(folderPath)
} catch (e) {
await fs.mkdir(folderPath, { recursive: true })
console.warn(`已新建 ${folderPath}`)
}
}
static async deleteFolder(folderPath) {
try {
const stat = await fs.stat(folderPath)
if (!stat.isDirectory()) {
console.warn(`${folderPath} 不是文件夹`)
return
}
} catch {
return
}
await fs.rm(folderPath, { recursive: true, maxRetries: 5 })
console.log(`已删除 ${folderPath}`)
}
static stringify(json) {
return JSON.stringify(json, null, isCompress ? '' : '\t')
}
static factorial(rest, result = [], detail = []) {
if (rest.length === 0) {
result.push(detail)
return result
}
const current = rest[0]
const newRest = rest.slice(1)
current.forEach(currentSub => {
Helper.factorial(newRest, result, [...detail, currentSub])
})
return result
}
}
class Setting {
static updateModel(fromModelPath, toModelPath, json) {
if (json.model) {
json.model = path.relative(toModelPath, path.normalize(`${fromModelPath}/${json.model}`))
}
}
static updatePose(fromModelPath, toModelPath, json) {
if (json.pose) {
json.pose = path.relative(toModelPath, path.normalize(`${fromModelPath}/${json.pose}`))
}
}
static updatePhysics(fromModelPath, toModelPath, json) {
if (json.physics) {
json.physics = path.relative(toModelPath, path.normalize(`${fromModelPath}/${json.physics}`))
}
}
static updateExpressions(fromModelPath, toModelPath, json) {
if (json.expressions) {
json.expressions.forEach(({ file }, index, array) => {
array[index].file = path.relative(toModelPath, path.normalize(`${fromModelPath}/${file}`))
})
}
}
static updateMotions(fromModelPath, toModelPath, json) {
if (json.motions) {
Object.values(json.motions).forEach(groups => {
groups.forEach((motion, index, array) => {
if (motion.file) {
array[index].file = path.relative(toModelPath, path.normalize(`${fromModelPath}/${motion.file}`))
}
if (motion.sound) {
array[index].sound = path.relative(toModelPath, path.normalize(`${fromModelPath}/${motion.sound}`))
}
})
})
}
}
static updateTextures(fromModelPath, toModelPath, json, textures = []) {
json.textures = textures.map(texture => {
return path.relative(toModelPath, path.normalize(`${fromModelPath}/${texture}`))
})
}
}
init()
async function init() {
await Helper.deleteFolder(toBasePath)
await Helper.ensureExistFolder(toBasePath)
const modelPaths = await createModelsIndexJsonFile()
modelPaths.map(async ({ modelPath }) => {
console.log(`开始转换:${modelPath}`)
const fromModelPath = `${fromBasePath}/${modelPath}`
const toModelPath = `${toBasePath}/${modelPath}`
await Helper.ensureExistFolder(toModelPath)
await createTextureJsonFiles(fromModelPath, toModelPath)
await createTexturesIndexJsonFile(toModelPath)
console.log(`转换完成:${modelPath}`)
})
}
async function createModelsIndexJsonFile() {
const modelPaths = await getAllModelPaths()
await fs.writeFile(`${toBasePath}/${modelsIndexJsonFileName}`, Helper.stringify(modelPaths))
return modelPaths
}
async function createTexturesIndexJsonFile(toModelPath) {
const dirents = await fs.readdir(toModelPath, { withFileTypes: true })
const textureNames = dirents.map(dirent => path.parse(dirent.name).name)
await fs.writeFile(`${toModelPath}/${texturesIndexJsonFileName}`, Helper.stringify(textureNames))
return textureNames
}
async function createTextureJsonFiles(fromModelPath, toModelPath) {
const textureDefaultJsonStr = await getTextureDefaultJsonStr(fromModelPath, toModelPath)
await fs.writeFile(`${toModelPath}/${textureDefaultJsonFileName}`, textureDefaultJsonStr)
const textureGroups = await getTextureGroups(fromModelPath, toModelPath)
const allTextures = Helper.factorial(textureGroups.map(({ textures }) => textures))
await Promise.all(allTextures.map(async textures => {
const textureJson = JSON.parse(textureDefaultJsonStr)
Setting.updateTextures(fromModelPath, toModelPath, textureJson, textures.map((texture, index) => {
const { partName } = textureGroups[index]
return `${modelTexturesFolderPath}/${partName}/${texture}`
}))
await fs.writeFile(`${toModelPath}/${textures.map(texture => path.parse(texture).name).join('&')}.json`, Helper.stringify(textureJson))
}))
}
async function getTextureDefaultJsonStr(fromModelPath, toModelPath) {
const modelTemplateJsonFile = await fs.readFile(`${fromModelPath}/${modelTemplateJsonFileName}`, 'utf-8')
const defaultTextureJson = JSON.parse(modelTemplateJsonFile)
Setting.updateModel(fromModelPath, toModelPath, defaultTextureJson)
Setting.updatePose(fromModelPath, toModelPath, defaultTextureJson)
Setting.updatePhysics(fromModelPath, toModelPath, defaultTextureJson)
Setting.updateExpressions(fromModelPath, toModelPath, defaultTextureJson)
Setting.updateMotions(fromModelPath, toModelPath, defaultTextureJson)
Setting.updateTextures(fromModelPath, toModelPath, defaultTextureJson, defaultTextureJson.textures)
return Helper.stringify(defaultTextureJson)
}
async function getTextureGroups(fromModelPath) {
// texture 是分部分的,几个文件夹就几部分,也就是数组的长度,从模板文件获取顺序
const modelTemplateJsonFile = await fs.readFile(`${fromModelPath}/${modelTemplateJsonFileName}`, 'utf-8')
const defaultPartNames = JSON.parse(modelTemplateJsonFile).textures.map(texture => path.parse(texture).dir.split('/').pop())
const textureGroups = Array(defaultPartNames.length).fill(null)
await Promise.all(defaultPartNames.map(async (partName, index) => {
textureGroups[index] = {
partName,
textures: await fs.readdir(`${fromModelPath}/${modelTexturesFolderPath}/${partName}`)
}
}))
return textureGroups
}
async function getAllModelPaths() {
const modelPaths = []
await fillModelPath(fromBasePath)
return modelPaths
async function fillModelPath(path) {
const modelTemplateJsonFilePath = `${path}/${modelTemplateJsonFileName}`
try {
const stat = await fs.stat(modelTemplateJsonFilePath)
if (stat.isFile()) {
modelPaths.push({
modelIntroduce: JSON.parse(await fs.readFile(modelTemplateJsonFilePath, 'utf-8')).modelIntroduce,
modelPath: path.slice(path.indexOf('/') + 1)
})
return
}
} catch (e) {
// do nothing
}
const dirents = await fs.readdir(path, { withFileTypes: true })
await Promise.all(dirents.map(async dirent => {
if (!dirent.isDirectory()) {
return
}
await fillModelPath(`${path}/${dirent.name}`)
}))
}
}
function setBuildOptions(buildOptions) {
process.argv.slice(2).forEach(arg => {
const [key, val] = arg.split('=')
switch (key) {
case 'isCompress':
Object.assign(buildOptions, {
[key]: val === 'true'
})
break
case 'fromBasePath':
case 'indexes':
Object.assign(buildOptions, {
[key]: val
})
}
})
return buildOptions
}