-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
executable file
·189 lines (152 loc) · 5.68 KB
/
cli.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
#!/usr/bin/env node
const Jimp = require('jimp');
const fs = require('fs-extra');
const package = require('./package.json');
const QuixelMapping = {
't3roughness': [ 'Roughness', 'roughness' ],
't3ao': [ 'AO', 'ao', 'ambient_occlusion', 'Ambient_Occlusion' ],
't3metalness': [ 'Metalness', 'metalness', 'metallic' ],
't3normal': [ 'Normal', 'normal' ],
't3map': [ 'Albedo', 'albedo', 'color', 'base_color', 'Base_Color' ],
't3displacement': [ 'Displacement', 'displacement', 'height', 'Height' ]
};
const DefaultOutputWidth = 1024;
const DefaultOutputFiletype = 'jpg';
const SupportedFiletypesRegExp = new RegExp([
'jpeg',
'jpg',
'png',
'bmp',
'tiff',
'tif',
'gif'
].join('|', 'gmi'));
const [ _, __, arg1, arg2 ] = process.argv;
if (!arg1 && !arg2) {
console.info('quixel-to-three [path/to/any/quixel/asset.jpg] [output-resolution=1024]')
return;
}
const cleanArg1 = arg1.split('-').join('');
if (['version', 'v'].includes(cleanArg1)) {
console.info('quixel-to-three', `v${package.version}`);
return;
}
let pathBase;
const isPathValid = arg1 && Object.keys(QuixelMapping).reduce((valid, next) => {
const split = `${arg1}`.split(new RegExp(QuixelMapping[next].join('|')));
if (split.length === 2) {
pathBase = split;
return true;
}
return valid;
}, false);
if (!isPathValid) {
console.warn('Invalid path, link one of Quixel assets (Albedo, AO, Roughness, Metalness, Normal etc.)');
return;
}
const outputWidth = !isNaN(parseInt(arg2, 10)) ? parseInt(arg2, 10) : DefaultOutputWidth;
let outputAspectRatio;
const availableAssets = {};
console.info('Searching ...');
return Promise.all(
Object.keys(QuixelMapping).map((id) => {
availableAssets[id] = QuixelMapping[id].some(path => {
const filepath = pathBase.join(path);
const exists = fs.pathExistsSync(filepath);
if (exists) {
QuixelMapping[id] = path;
console.info([
`Asset: ${filepath}`,
`Type: ${path}`
].join(' '));
return true;
}
return false;
});
if (availableAssets[id] && !outputAspectRatio) {
return new Promise((resolve) => {
const sample = Jimp.read(pathBase.join(QuixelMapping[id]))
.then(image => {
const result = image.resize(outputWidth, Jimp.AUTO, Jimp.RESIZE_NEAREST_NEIGHBOR);
const aspectRatio = result.bitmap ? (result.bitmap.height / outputWidth) : 1.0;
outputAspectRatio = isNaN(aspectRatio) ? 1.0 : aspectRatio;
resolve();
});
});
}
return Promise.resolve();
})
).then(() => {
let outputColorMap;
let outputNormalMap;
let outputPBRMap;
if (availableAssets.t3map) {
Jimp.read(pathBase.join(QuixelMapping.t3map)).then(image => {
console.info('Converting albedo ...');
const result = image.resize(outputWidth, Jimp.AUTO, Jimp.RESIZE_NEAREST_NEIGHBOR)
.write(pathBase.join('t3map').replace(SupportedFiletypesRegExp, DefaultOutputFiletype));
});
}
const aoPromise = new Promise(resolve => {
if (availableAssets.t3ao) {
console.info('Converting ambient occlusion ...');
Jimp.read(pathBase.join(QuixelMapping.t3ao)).then(image => {
resolve(image.resize(outputWidth, Jimp.AUTO, Jimp.RESIZE_NEAREST_NEIGHBOR));
});
} else {
resolve(new Jimp(outputWidth, outputWidth * outputAspectRatio, 0xffffffff));
}
});
const roughnessPromise = new Promise(resolve => {
if (availableAssets.t3roughness) {
console.info('Converting roughness ...');
Jimp.read(pathBase.join(QuixelMapping.t3roughness)).then(image => {
resolve(image.resize(outputWidth, Jimp.AUTO, Jimp.RESIZE_NEAREST_NEIGHBOR));
});
} else {
resolve(new Jimp(outputWidth, outputWidth * outputAspectRatio, 0xffffffff));
}
});
const metalnessPromise = new Promise(resolve => {
if (availableAssets.t3metalness) {
console.info('Converting metalness ...');
Jimp.read(pathBase.join(QuixelMapping.t3metalness)).then(image => {
resolve(image.resize(outputWidth, Jimp.AUTO, Jimp.RESIZE_NEAREST_NEIGHBOR));
});
} else {
resolve(new Jimp(outputWidth, outputWidth * outputAspectRatio, 0x000000ff));
}
});
if (availableAssets.t3normal) {
console.info('Converting normals ...');
Jimp.read(pathBase.join(QuixelMapping.t3normal)).then(image => {
const result = image.resize(outputWidth, Jimp.AUTO, Jimp.RESIZE_NEAREST_NEIGHBOR)
.write(pathBase.join('t3normal').replace(SupportedFiletypesRegExp, DefaultOutputFiletype));
});
}
if (availableAssets.t3displacement) {
console.info('Converting displacements ...');
Jimp.read(pathBase.join(QuixelMapping.t3displacement)).then(image => {
const result = image.resize(outputWidth, Jimp.AUTO, Jimp.RESIZE_NEAREST_NEIGHBOR)
.write(pathBase.join('t3displacement').replace(SupportedFiletypesRegExp, DefaultOutputFiletype));
});
}
return Promise.all([
aoPromise,
roughnessPromise,
metalnessPromise,
]).then(([ ao, roughness, metalness ]) => {
const pbrMap = new Jimp(outputWidth, outputWidth * outputAspectRatio, 0xffffff);
console.info('Converting PBR ...');
pbrMap.scan(0, 0, pbrMap.bitmap.width, pbrMap.bitmap.height, function (x, y, index) {
this.bitmap.data[index + 0] = ao.bitmap.data[index + 0];
this.bitmap.data[index + 1] = roughness.bitmap.data[index + 1]
this.bitmap.data[index + 2] = metalness.bitmap.data[index + 2]
})
.write(pathBase.join('t3pbr').replace(SupportedFiletypesRegExp, DefaultOutputFiletype));
});
})
.catch((error) => {
console.error('Error occured. Make sure asset file exists.');
console.error('If possible, use assets in 2K and 4K resolution to avoid memory overflow.');
});