-
Notifications
You must be signed in to change notification settings - Fork 248
/
build.js
695 lines (589 loc) · 21 KB
/
build.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const rimraf = require("rimraf");
const sizeOf = require('image-size');
const JSZip = require('jszip');
const child_process = require('child_process');
const spawn = child_process.spawn;
const execSync = child_process.execSync;
const ColorThief = require('colorthief');
const pLimit = require('p-limit');
// Limit Proccessing images
const limitProccess = pLimit(10);
// Limit Proccessing render (Blender)
const limitRender = pLimit(5);
// Temp dir, clear and recreate
rimraf.sync('./.tmp');
['./.tmp', '64', '128', '256', '512', 'palette', 'preview'].forEach(folder => {
folder = path.join('./', folder);
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
});
var LOG_IDX = 0;
const WARNING = chalk.keyword('orange');
var COLORS = [
chalk.green,
chalk.yellow,
chalk.blue,
chalk.magenta,
chalk.cyan,
chalk.white,
chalk.blackBright,
chalk.greenBright,
chalk.yellowBright,
chalk.blueBright,
chalk.magentaBright,
chalk.cyanBright,
chalk.whiteBright
];
/**
* ALGORITMO
*
* 1 - Converter arquivos .jpg, .bmp, .tiff para .png
* 4 - Remover arquivos com extensão não aceita
* 2 - Remover arquivos com dimensão menor que 1024x1024
* 3 - Redimensionar arquivos com dimensão maior que 1024x1024 para esse valor
* 5 - Para cada arquivo:
* a) Gerar definição de cor (usar circulo temporario ou extrair a partir do nome do arquivo se esse já foi processado)
* b) Verificar se todos os fluxos foram processados (Redimensionar, Gerar Paleta, etc)
* 1 - Gerar paleta de cores
* 2 - Redimensionar para 512,256,128 e 64
* 3 - Renderizar cena de testes em 512 e 128px
* 6 - Gerar documentação de todos os materiais
*
* @TODO: Verificar se possui zmt associado (mesmo nome)
*/
// All files
const FILES = [];
// File name regex
var REG_FILE_NAME_COLORS = /^([A-F0-9]{6})_([A-F0-9]{6})_([A-F0-9]{6})_([A-F0-9]{6}).*/;
/**
* Work with one file (all references)
*
* @param name
* @constructor
*/
function File(name) {
this.name = name;
var idx = LOG_IDX++;
var color = COLORS[idx % COLORS.length];
var prefix = '[' + (('0000' + idx).split('').reverse().slice(0, 4).reverse().join('')) + '] ';
this.LOG = {
info: function () {
var parts = arguments;
parts[0] = color(prefix + parts[0]);
console.log.apply(undefined, parts);
}.bind(this),
warn: function () {
var parts = arguments;
parts[0] = WARNING(prefix + parts[0]);
console.warn.apply(undefined, parts);
}.bind(this),
error: function () {
var parts = arguments;
parts[0] = chalk.bold.red(prefix + parts[0]);
console.warn.apply(undefined, parts);
}.bind(this)
};
this.LOG.info(this.name);
}
/**
* Remove this file from disk (All references)
*/
File.prototype.delete = function () {
// Unlink all references
['1024', '512', '256', '128', '64'].forEach(function (dir) {
var fpath = path.join(dir, this.name);
if (fs.existsSync(fpath)) {
fs.unlinkSync(fpath);
}
}.bind(this));
// Remove from list
var idx = FILES.indexOf(this);
if (idx >= 0) {
FILES.splice(idx, 1);
}
const resolve = function () {
return Promise.resolve();
}.bind(this);
// Ignore all next execution
this.processExtension
= this.validateDimension
= this.extractPalette
= this.savePalettePreview
= this.render
= this.resize
= resolve;
return resolve();
};
/**
* Initialize file proccess
*
* @returns {Promise<any>}
*/
File.prototype.process = function () {
return this.processExtension()
.then(value => {
return this.validateDimension();
})
.then(value => {
return this.extractPalette();
})
.then(value => {
return this.savePalettePreview();
})
.then(value => {
return this.render();
})
.then(value => {
return this.resize();
})
;
};
/**
* Initialize file proccess
*
* @returns {Promise<any>}
*/
File.prototype.processExtension = function () {
return new Promise((resolve, reject) => {
// Get this file extension
const ext = path.extname(this.name);
switch (ext.toLowerCase()) {
case '.png':
resolve();
break;
case '.jpg':
case '.jpeg':
case '.bmp':
case '.tga':
case '.tif':
case '.tiff':
const original = path.join('1024', this.name);
let basename = path.basename(this.name, ext);
let renamed = path.join('1024', basename + '.png');
let inc = 1;
while (true) {
if (fs.existsSync(renamed)) {
renamed = path.join('1024', basename + '-' + (inc++) + '.png');
} else {
break;
}
}
execSync([
`magick "${original}" -profile ./sRGB2014.icc -colorspace RGB`,
`-strip "PNG24:${renamed}"`,
].join(' '), {stdio: [0, 1, 2]});
// Remove original
fs.unlinkSync(original);
this.name = path.basename(renamed);
this.renameZMT(path.basename(original), this.name)
.then(resolve)
.catch(reject);
break;
default:
// Remove invalid extension
this.delete();
resolve();
}
});
};
/**
* Parse zmt file after rename this
*
* @param oldname
* @param name
* @returns {*}
*/
File.prototype.renameZMT = function (oldname, name) {
let basename = path.basename(oldname, '.png');
const originalZMT = path.join('zmt', basename + '.zmt');
const originalZMTu = path.join('zmt', basename + '.ZMT');
basename = path.basename(name, '.png');
let renamedZMT = path.join('zmt', basename + '.zmt');
if (fs.existsSync(originalZMT)) {
fs.renameSync(originalZMT, renamedZMT);
} else if (fs.existsSync(originalZMTu)) {
fs.renameSync(originalZMTu, renamedZMT);
}
return Promise.resolve();
};
/**
* Remover arquivos com dimensão menor que 1024x1024
* Redimensionar arquivos com dimensão maior que 1024x1024 para esse valor
*/
File.prototype.validateDimension = function () {
let original = path.join('1024', this.name);
var dimensions = sizeOf(original);
var width = dimensions.width;
var height = dimensions.height;
if (width === 1024 && height === 1024) {
// Ok
return Promise.resolve();
}
if (width < 1024 || height < 1024) {
// Remover arquivos com dimensão menor que 1024x1024
this.LOG.warn(`Removing "${original}": invalid resolution ${width}x${height}`);
return this.delete();
}
if (width !== height) {
// Remove imagens que não são quadradas
this.LOG.warn(`Removing "${original}": invalid resolution ${width}x${height}`);
return this.delete();
}
execSync([
`magick "${original}" -profile ./sRGB2014.icc -colorspace RGB`,
`-resize 1024x1024 -strip "PNG24:${original}"`,
].join(' '), {stdio: [0, 1, 2]});
};
function rgbToHex(rgb) {
var hex = Number(rgb).toString(16);
if (hex.length < 2) {
hex = "0" + hex;
}
return hex;
}
/**
* Faz o processamento da paleta de cores do arquivo
*
* @returns {Promise<any>}
*/
File.prototype.extractPalette = function () {
return new Promise((resolve, reject) => {
if (!this.palette) {
var parts = this.name.match(REG_FILE_NAME_COLORS);
if (parts) {
this.palette = {
A: parts[1],
B: parts[2],
C: parts[3],
D: parts[4]
};
return resolve();
}
// Gera a paleta a partir da imagem original, e renomeia o arquivo atual
const original = path.join('1024', this.name);
const circular = path.join('.tmp', path.basename(original, '.png') + '.png');
var colorA = {};
// Create circular file to parse palette
execSync([
`magick convert "${original}"`,
`( -size 1024x1024 xc:none -fill white -draw "circle 512,512 512,0" )`,
`-compose copy_opacity -composite "${circular}"`
].join(' '), {stdio: [0, 1, 2]});
return ColorThief.getColor(circular)
.then(rgb => {
colorA = `${rgb.map(rgbToHex).join('')}`;
return ColorThief.getPalette(circular, 5);
})
.then(rgb => {
// Get name from palette
// Ex. C65646_B36458_EC8C83_841D14
var name = [
colorA,
`${rgb[1].map(rgbToHex).join('')}`,
`${rgb[2].map(rgbToHex).join('')}`,
`${rgb[3].map(rgbToHex).join('')}`,
].join('_').replace(/[#]/g, '').toUpperCase();
let renamed = path.join('1024', name + '.png');
let inc = 1;
while (true) {
if (fs.existsSync(renamed)) {
renamed = path.join('1024', name + '-' + (inc++) + '.png');
} else {
break;
}
}
// Rename file on disk, force sRGB without embedded profile
execSync(
`magick "${original}" -profile ./sRGB2014.icc -colorspace RGB -strip "PNG24:${renamed}"`,
{stdio: [0, 1, 2]}
);
// Delete old and temp files
fs.unlinkSync(original);
fs.unlinkSync(circular);
this.name = name + '.png';
return this.renameZMT(path.basename(original), this.name);
})
.then(value => {
// Check agai, by name now
return this.extractPalette();
})
.then(resolve)
.catch(reject);
} else {
resolve();
}
})
};
/**
* Cria uma paleta com as cores prominentes do matcap (https://github.com/akfish/node-vibrant/)
*
* A = Main
* B = Palette
* C = Palette
* D = Palette
*
* +----------------+--------+
* | | |
* | | |
* | | B |
* | A | |
* | | |
* | +--------+
* | | |
* +-----------+----+ |
* | | |
* | C | D |
* | | |
* +-----------+-------------+
*/
File.prototype.savePalettePreview = function () {
return new Promise((resolve, reject) => {
// palette
const paletteFile = path.join('palette', path.basename(this.name, '.png') + '-palette.png');
fs.exists(paletteFile, (exists) => {
if (exists) {
return resolve();
}
const colors = this.palette;
// magick convert -size 140x140 xc:"rgb(255, 0, 0)" -fill White -draw "rectangle 5,5 10,10" square.png
execSync([
`magick convert -size 94x94`,
`xc:"#${colors.D}"`,
`-fill "#${colors.C}" -draw "rectangle 0, 62, 47, 94"`,
`-fill "#${colors.B}" -draw "rectangle 62, 0, 94, 47"`,
`-fill "#${colors.A}" -draw "rectangle 0, 0, 62, 62"`,
`"${paletteFile}"`
].join(' '), {stdio: [0, 1, 2]});
//borderRadius(paletteFile, 94);
resolve();
}
);
});
};
/**
* Use Blender to render preview
*/
File.prototype.render = function () {
// Limit number of render
return limitRender(() => {
return new Promise((resolve, reject) => {
let basename = path.basename(this.name, '.png');
const rendered = path.join('preview', basename + '-preview.png');
if (fs.existsSync(rendered)) {
return resolve();
}
const outputdir = path.join('.tmp', basename);
fs.mkdirSync(outputdir);
const blender = spawn('blender', [
'-b', 'scene.blend',
'-o',
`//${outputdir}/`,
'-P', 'scene-texture.py',
'-F', 'PNG',
'-a',
'-s', '1',
'-e', '1',
'-j', '1',
'-t', '0',
'-E', 'CYCLES',
], {
stdio: 'pipe',
env: {
texture: `1024/${this.name}`
}
});
var stdout = '';
var stderr = '';
blender.stdout.on('data', (data) => {
stdout += data.toString();
var parts = stdout.split('\n');
stdout = parts.pop();
parts.forEach(value => {
this.LOG.info(value);
});
});
blender.stderr.on('data', (data) => {
stderr += data.toString();
var parts = stderr.split('\n');
stderr = parts.pop();
parts.forEach(value => {
this.LOG.error(value);
});
});
blender.on('close', (code) => {
if (code !== 0) {
return reject(`Blender process exited with code ${code}`);
}
execSync([
`magick "${path.join(outputdir, '0001.png')}" -profile ./sRGB2014.icc -colorspace RGB `,
`-strip "PNG24:${rendered}"`,
].join(' '), {stdio: [0, 1, 2]});
// fs.renameSync(path.join(outputdir, '0001.png'), rendered);
rimraf.sync(outputdir);
resolve();
});
});
}
);
};
/**
* Create resized versions (512, 256, 128, 64)
*/
File.prototype.resize = function () {
const original = path.join('1024', this.name);
['64', '128', '256', '512'].forEach(size => {
const resized = path.join(size, path.basename(this.name, '.png') + '-' + size + 'px.png');
if (!fs.existsSync(resized)) {
execSync(`magick "${original}" -resize ${size}x${size} -strip "${resized}"`, {stdio: [0, 1, 2]});
}
});
// For github list
const thumbnail = path.join('thumbnail', path.basename(this.name, '.png') + '.jpg');
if (!fs.existsSync(thumbnail)) {
execSync(`magick "${original}" -resize 232x232 -strip "${thumbnail}"`, {stdio: [0, 1, 2]});
}
return Promise.resolve();
};
/**
* Add border radius to file
*
* @param file
* @param size
* @param ext
*/
function borderRadius(file, size) {
var dir = path.dirname(file);
var maskFile = path.join('./.tmp', `corner_mask_${size}.png`);
if (!fs.existsSync(maskFile)) {
execSync(`magick convert -size ${size}x${size} xc:none -alpha transparent -background none -fill white -draw "roundRectangle 0,0 ${size - 1},${size - 1} 12,12" "${maskFile}"`, {stdio: [0, 1, 2]});
}
execSync(`magick convert "${file}" -alpha on "${maskFile}" -compose Dst_In -composite -background white -alpha Remove "${file}"`, {stdio: [0, 1, 2]});
}
// Read all files
fs.readdirSync('1024').forEach(file => {
FILES.push(new File(file));
});
// Proccess
Promise
.all(FILES.map(file => {
// Limit number of files to proccess
return limitProccess(() => {
return file.process()
})
}))
.then(value => {
// Remove orphans
const allfiles = fs.readdirSync('1024');
['64', '128', '256', '512'].forEach(size => {
fs.readdirSync(size)
.map(file => {
return file.replace(/-\d+px.png$/, '');
})
.filter(file => {
return allfiles.indexOf(file + '.png') < 0
})
.forEach(file => {
var fpath = path.join(size, file + '-' + size + 'px.png');
if (fs.existsSync(fpath)) {
fs.unlinkSync(fpath);
}
});
});
['palette', 'preview'].forEach(folder => {
var ext = folder === 'palette' ? '.png' : '.jpg';
fs.readdirSync(folder)
.map(file => {
return file.replace(/-[^.]+.(png|jpg)$/, '');
})
.filter(file => {
return allfiles.indexOf(file + '.png') < 0
})
.forEach(file => {
var fpath = path.join(folder, file + '-' + folder + ext);
if (fs.existsSync(fpath)) {
fs.unlinkSync(fpath);
}
});
});
return allfiles;
})
.then(allfiles => {
// @TODO: Sort by colors
// Gerar documentação
const TPL_README = fs.readFileSync('resources/TPL_README.md', 'utf8');
const TPL_PAGE = fs.readFileSync('resources/TPL_PAGE.md', 'utf8');
const COLS = 4;
const ROWS = 5;
const PAGE_SIZE = COLS * ROWS;
const IDEAL_SIZE = 882;
const THUMB = Math.round(IDEAL_SIZE / COLS) - 4; //2 border + 2 spaces
const CROP = 250;
const CROP_L = (512 / 2) - (CROP / 2);
const PAGES = Math.ceil(allfiles.length / PAGE_SIZE);
const readmeMD = [TPL_README];
var i = 0;
for (var page = 1; page <= PAGES; page++) {
const pageMD = [TPL_PAGE];
// * [Page 1](PAGE-2.md)
// Navigation
pageMD.push(`## Navigation`);
pageMD.push(`* [Home](/)`);
for (var pagei = 1; pagei <= PAGES; pagei++) {
if (pagei === page) {
pageMD.push(`* Page ${pagei}`);
} else {
pageMD.push(`* [Page ${pagei}](PAGE-${pagei}.md)`);
}
}
pageMD.push(`## Page ${page} Matcaps`);
const pagePreviews = [];
for (var m = page * PAGE_SIZE; i < m && i < allfiles.length; i++) {
var file = allfiles[i];
var name = path.basename(file, '.png');
pagePreviews.push(`preview/${name}-preview.png`);
pageMD.push([
`### ${name}`,
`![](preview/${name}-preview.png)`,
`![](thumbnail/${name}.jpg)`,
`![](palette/${name}-palette.png)`,
'',// BR
`[[1024px](https://github.com/nidorx/matcaps/raw/master/1024/${name}.png)]`,
`[[512px](https://github.com/nidorx/matcaps/raw/master/512/${name}-512px.png)]`,
`[[256px](https://github.com/nidorx/matcaps/raw/master/256/${name}-256px.png)]`,
`[[128px](https://github.com/nidorx/matcaps/raw/master/128/${name}-128px.png)]`,
`[[64px](https://github.com/nidorx/matcaps/raw/master/64/${name}-64px.png)]`,
fs.existsSync(path.join('zmt', name + '.zmt'))
? `[[ZBrush Material (ZMT)](https://github.com/nidorx/matcaps/raw/master/zmt/${name}.zmt)]`
: '[~~ZBrush Material (ZMT)~~]',
'\n---'
].join('\n'));
}
fs.writeFileSync(`./PAGE-${page}.md`, pageMD.join('\n'));
// Generate page preview
execSync([
`magick montage -tile ${COLS}x0 -geometry +1+1 -border 1 -bordercolor black -crop ${CROP}x${CROP}+${CROP_L}+0 -scale ${THUMB}x${THUMB}`,
`"${pagePreviews.join('" "')}"`,
`"preview/page-${page}.jpg"`
].join(' '), {stdio: [0, 1, 2]});
readmeMD.push([
`## Page ${page}`,
`[![](preview/page-${page}.jpg)](PAGE-${page}.md)`,
].join('\n'));
}
// Update README.md
fs.writeFileSync(`./README.md`, readmeMD.join('\n'));
})
.then(value => {
// Criar tile com grupo de previews
// magick montage -tile 10x0 -geometry +1+1 -border 1 -bordercolor black -scale 84x84 *.jpg out.jpeg
})
.then(value => {
console.log('Ok!')
})
.catch(reason => {
console.error(reason);
});