This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gltf-exporter.js
394 lines (386 loc) · 15.4 KB
/
gltf-exporter.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
const icondumper2 = require("./icon.js");
const iconjs = icondumper2.readers;
const filesystem = require("fs");
const processObj = require("process");
const gltfConstants = {
"FLOAT": 5126,
"ARRAY_BUFFER": 34962,
"LINEAR": 9729,
"NEAREST_MIPMAP_LINEAR": 9986,
"REPEAT": 10497
};
function getCrc32(data) {
let output = -1;
for (let byteIndex of data) {
for (let index = 0; index < 8; index++, byteIndex >>>= 1) {
output = (output >>> 1) ^ (-((output ^ byteIndex) & 1) & 0xEDB88320);
} // 0xEDB88320 is a reverse polynomial that's common in crc32
} //i know it's some bitwise madness, it works, either way.
return ((~output) >>> 0);
}
function getAdler32(data) {
let s1 = 1;
let s2 = 0;
for (let index of data) {
s1 = (s1 + index) % 65521;
s2 = (s2 + s1) % 65521;
}
return (s2 << 16) | s1;
}
function rgb5a1_rgb8(colour) {
let b = ( colour & 0b11111);
let g = ((colour >> 5) & 0b11111);
let r = ((colour >> 10) & 0b11111);
let output = new Number();
output |= ((r * 8) << 16);
output |= ((g * 8) << 8);
output |= ((b * 8) << 0);
return output;
}
function imf2gltf(icon = null, filename = "untitled") {
if (icon === null) {
throw "Missing first argument, of which should be a icondumper2 Intermediate Model Format object.";
}
if (icon.hasOwnProperty("numberOfShapes") === false) {
throw "Expected a icondumper2 Intermediate Model Format object.";
}
let shapesArray = new Array(icon.numberOfShapes);
for (let index = 0; index < icon.numberOfShapes; index++) {
shapesArray[index] = new Array();
}
let verticesArray = new Array();
let normalsArray = new Array();
let uvArray = new Array();
let colourArray = new Array();
icon.vertices.forEach(function(vertexObject){
for (let index = 0; index < icon.numberOfShapes; index++) {
shapesArray[index].push(vertexObject.shapes[index].x);
shapesArray[index].push(vertexObject.shapes[index].y);
shapesArray[index].push(vertexObject.shapes[index].z);
}
normalsArray.push(vertexObject.normal.x);
normalsArray.push(vertexObject.normal.y);
normalsArray.push(vertexObject.normal.z);
uvArray.push(vertexObject.uv.u);
uvArray.push(vertexObject.uv.v);
// gamma correction, glTF clients expect lineari(s|z)ed-sRGB, not sRGB.
colourArray.push(Math.pow((vertexObject.color.r/255), 2.2));
colourArray.push(Math.pow((vertexObject.color.g/255), 2.2));
colourArray.push(Math.pow((vertexObject.color.b/255), 2.2));
colourArray.push((vertexObject.color.a > 1) ? (vertexObject.color.a/255): 1);
});
shapesArray.forEach(function(arr) {
verticesArray = [...verticesArray, ...arr];
});
let outputFloatArray = new Float32Array([...verticesArray, ...normalsArray, ...uvArray, ...colourArray]); // 3[nOS], 3, 2, 4#
let gltfOutputArray = new Array(icon.numberOfShapes);
for (let index = 0; index < icon.numberOfShapes; index++) {
const gltfOutput = new Object();
//setting up GLTF
gltfOutput.scene = 0;
gltfOutput.scenes = [{"name": filename, "nodes": [0]}];
gltfOutput.nodes = [{"mesh": 0, "name": `${filename}#${index}`, "rotation": [1,0,0,0]}];
gltfOutput.meshes = [{
"name": `Mesh (${filename}#${index})`,
"primitives": [{
"attributes": {
"POSITION": 0,
"NORMAL": 1,
"TEXCOORD_0": 2,
"COLOR_0": 3,
},
"material": 0
}]
}]; // no indices because who needs indexing when you're transcoding?
gltfOutput.materials = [{
"name": `Material (${filename}#${index})`,
"pbrMetallicRoughness": null,
"extensions": { // or we get annoying PBR and specular stuff we don't need
"KHR_materials_unlit": {}
}
}];
if(icon.textureFormat !== "N") {
gltfOutput.materials[0].pbrMetallicRoughness = {
"baseColorTexture": {"index":0, "texCoord": 0}
};
} else {
gltfOutput.materials[0].pbrMetallicRoughness = {
"baseColorFactor": [1.0, 1.0, 1.0, 1.0],
"metallicFactor": 0.0,
"roughnessFactor": 1.0
}
}
gltfOutput.buffers = [{"uri": `${filename}.bin`, "byteLength": outputFloatArray.byteLength}];
gltfOutput.bufferViews = [
{
"buffer": 0,
"byteOffset": (((icon.vertices.length*3)*4)*index),
"byteLength": ((icon.vertices.length*3)*4),
"target": gltfConstants.ARRAY_BUFFER
},
{
"buffer": 0,
"byteOffset": (((icon.vertices.length*3)*4)*icon.numberOfShapes),
"byteLength": (normalsArray.length*4),
"target": gltfConstants.ARRAY_BUFFER
},
{
"buffer": 0,
"byteOffset": ((((icon.vertices.length*3)*4)*icon.numberOfShapes)+(normalsArray.length*4)),
"byteLength": (uvArray.length*4),
"target": gltfConstants.ARRAY_BUFFER
},
{
"buffer": 0,
"byteOffset": (((((icon.vertices.length*3)*4)*icon.numberOfShapes)+(normalsArray.length*4))+(uvArray.length*4)),
"byteLength": (colourArray.length*4),
"target": gltfConstants.ARRAY_BUFFER
}
];
gltfOutput.accessors = [
{
"bufferView": 0,
"componentType": gltfConstants.FLOAT,
"count": icon.vertices.length,
"type": "VEC3",
"max": [ 5.0, 5.0, 5.0],
"min": [-5.0, -5.0, -5.0],
"name": "Vertex Position Accessor"
},
{
"bufferView": 1,
"componentType": gltfConstants.FLOAT,
"count": icon.vertices.length,
"type": "VEC3",
"max": [ 1.0, 1.0, 1.0],
"min": [-1.0, -1.0, -1.0],
"name": "Normal Accessor"
},
{
"bufferView": 2,
"componentType": gltfConstants.FLOAT,
"count": icon.vertices.length,
"type": "VEC2",
"max": [ 1.0, 1.0],
"min": [-1.0, -1.0],
"name": "Texture Coordinate Accessor"
},
{
"bufferView": 3,
"componentType": gltfConstants.FLOAT,
"count": icon.vertices.length,
"type": "VEC4",
"max": [ 1.0, 1.0, 1.0, 1.0],
"min": [ 0.0, 0.0, 0.0, 1.0],
"name": "Colour Accessor"
}
];
gltfOutput.asset = {"version": "2.0", "generator": `icondumper2/${icondumper2.version}`}
gltfOutput.extensionsUsed = ["KHR_materials_unlit"];
if(icon.textureFormat !== "N") {
gltfOutput.textures = [{"source": 0}];
gltfOutput.images = [{"name": `Texture (${filename}#${index})`, "uri": `${filename}.png`}]
}
gltfOutputArray[index] = (gltfOutput);
}
let texture16 = null; // Uint16Array(16384)
switch(icon.textureFormat) {
case "C": {
texture16 = icondumper2.helpers.uncompressTexture(icon.texture.data);
break;
}
case "U": {
texture16 = icon.texture;
break;
}
}
if(texture16 !== null) {
let texture24 = new Uint8Array(49983);
texture24.set([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80,
0x08, 0x02, 0x00, 0x00, 0x00, // you may know
0x4c, 0x5c, 0xf6, 0x9c, // what this is from 0x89.
0x00, 0x00, 0xc3, 0x06, 0x49, 0x44, 0x41, 0x54,
0x78, 0x01 // if you didn't get it, here's a clue
],0);
let textureOffset = 43;
let texture24Data = new Array();
let texture24CheckedData = new Array();
for (let x = 0; x < 128; x++) {
let line = [(x === 127 ? 1 : 0), 0x81, 0x01, 0x7e, 0xfe, 0x00];
texture24Data = texture24Data.concat(line);
texture24CheckedData.push(0);
let scanline = new Array(128*3);
for (let y = 0; y < 128; y++) {
color = rgb5a1_rgb8(texture16[(x*128)+y]);
scanline[(y*3) ] = ((color >> 0 ) & 255);
scanline[(y*3)+1] = ((color >> 8 ) & 255);
scanline[(y*3)+2] = ((color >> 16) & 255);
}
texture24Data = texture24Data.concat(scanline);
texture24CheckedData = texture24CheckedData.concat(scanline);
}
texture24.set(texture24Data, textureOffset);
textureOffset += texture24Data.length;
let a32conv = new DataView(new ArrayBuffer(4));
a32conv.setInt32(0, getAdler32(new Uint8Array(texture24CheckedData)))
texture24.set([a32conv.getUint8(0), a32conv.getUint8(1), a32conv.getUint8(2), a32conv.getUint8(3)], textureOffset);
textureOffset += 4;
let crc32 = getCrc32(new Uint8Array([
0x49, 0x44, 0x41, 0x54, 0x78, 0x01, ...texture24Data,
a32conv.getUint8(0), a32conv.getUint8(1),
a32conv.getUint8(2), a32conv.getUint8(3)
]));
texture24.set([
(crc32 >> 24) & 0xff,
(crc32 >> 16) & 0xff,
(crc32 >> 8) & 0xff,
crc32 & 0xff
], textureOffset);
textureOffset += 4;
texture24.set([
0x00, 0x00, 0x00, 0x00,
0x49, 0x45, 0x4E, 0x44,
0xae, 0x42, 0x60, 0x82
], textureOffset);
return {objects: gltfOutputArray, buffer: outputFloatArray, texture: texture24};
} else {
return {objects: gltfOutputArray, buffer: outputFloatArray, texture: null};
}
}
function loadAndConvertIcon(inputData, attemptedFilename = "-") {
if (inputData.hasOwnProperty("numberOfShapes") === false) {
throw "Expected a icondumper2 Intermediate Model Format object.";
}
const filename = encodeURIComponent(attemptedFilename).replace(/\%[0-9A-F]{2,2}/g, "").replace(/\./g, "_");
const glTF_output = imf2gltf(inputData, filename);
for (let index = 0; index < (inputData.numberOfShapes); index++) {
(require("fs")).writeFileSync(`${filename}_${index}.gltf`, new TextEncoder().encode(JSON.stringify(glTF_output.objects[index])));
console.info(`Saved shape ${filename}#${index} as "${filename}_${index}.gltf".`);
}
(require("fs")).writeFileSync(`${filename}.bin`, glTF_output.buffer);
console.info(`Saved glTF buffer as "${filename}.bin".`);
if(glTF_output.texture !== null) {
(require("fs")).writeFileSync(`${filename}.png`, glTF_output.texture);
console.info(`Saved texture as "${filename}.png".\n`);
}
}
// can anything de-dupe this code somehow? (index.js)
console.info(`icon.js version ${icondumper2.version}, ${(new Date()).getFullYear().toString()} (c) yellows111`);
switch(processObj.argv[2]) {
case "psu": {
let inputFile = filesystem.readFileSync(processObj.argv[3] ? processObj.argv[3] : "file.psu");
const parsed = iconjs.readEmsPsuFile(inputFile.buffer.slice(inputFile.byteOffset, inputFile.byteOffset + inputFile.byteLength));
const PS2D = iconjs.readPS2D(parsed[parsed.rootDirectory]["icon.sys"].data);
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.n].data), PS2D.filenames.n);
if(PS2D.filenames.n !== PS2D.filenames.c) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.c].data), PS2D.filenames.c);
}
if(PS2D.filenames.n !== PS2D.filenames.d) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.d].data), PS2D.filenames.d);
}
break;
}
case "psv": {
let inputFile = filesystem.readFileSync(processObj.argv[3] ? processObj.argv[3] : "file.psv");
const parsed = iconjs.readPsvFile(inputFile.buffer.slice(inputFile.byteOffset, inputFile.byteOffset + inputFile.byteLength));
const PS2D = iconjs.readPS2D(parsed["icon.sys"]);
//i should probably make PSV readers more like the others, but why should I? It's giving me shortcuts to what I want.
loadAndConvertIcon(iconjs.readIconFile(parsed.icons.n), PS2D.filenames.n)
if(PS2D.filenames.n !== PS2D.filenames.c) {
loadAndConvertIcon(iconjs.readIconFile(parsed.icons.c), PS2D.filenames.c)
}
if(PS2D.filenames.n !== PS2D.filenames.d) {
loadAndConvertIcon(iconjs.readIconFile(parsed.icons.d), PS2D.filenames.d)
}
break;
}
case "sps":
case "xps": {
let inputFile = filesystem.readFileSync(processObj.argv[3] ? processObj.argv[3] : "file.sps");
const parsed = iconjs.readSharkXPortSxpsFile(inputFile.buffer.slice(inputFile.byteOffset, inputFile.byteOffset + inputFile.byteLength));
const PS2D = iconjs.readPS2D(parsed[parsed.rootDirectory]["icon.sys"].data);
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.n].data), PS2D.filenames.n);
if(PS2D.filenames.n !== PS2D.filenames.c) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.c].data), PS2D.filenames.c);
}
if(PS2D.filenames.n !== PS2D.filenames.d) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.d].data), PS2D.filenames.d);
}
break;
}
case "cbs": {
let inputFile = filesystem.readFileSync(processObj.argv[3] ? processObj.argv[3] : "file.cbs");
function myInflator(inputBuffer) {
return (require("zlib").inflateSync(inputBuffer)).buffer;
}
const parsed = iconjs.readCodeBreakerCbsFile(inputFile.buffer.slice(inputFile.byteOffset, inputFile.byteOffset + inputFile.byteLength), myInflator);
const PS2D = iconjs.readPS2D(parsed[parsed.rootDirectory]["icon.sys"].data);
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.n].data), PS2D.filenames.n);
if(PS2D.filenames.n !== PS2D.filenames.c) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.c].data), PS2D.filenames.c);
}
if(PS2D.filenames.n !== PS2D.filenames.d) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.d].data), PS2D.filenames.d);
}
break;
}
case "max":
case "pws": {
let inputFile = filesystem.readFileSync(processObj.argv[3] ? processObj.argv[3] : "file.max");
function myUnlzari(inputBuffer) {
return (require("./lzari.js").decodeLzari(inputBuffer)).buffer;
}
const parsed = iconjs.readMaxPwsFile(inputFile.buffer.slice(inputFile.byteOffset, inputFile.byteOffset + inputFile.byteLength), myUnlzari);
const PS2D = iconjs.readPS2D(parsed[parsed.rootDirectory]["icon.sys"].data);
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.n].data), PS2D.filenames.n);
if(PS2D.filenames.n !== PS2D.filenames.c) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.c].data), PS2D.filenames.c);
}
if(PS2D.filenames.n !== PS2D.filenames.d) {
loadAndConvertIcon(iconjs.readIconFile(parsed[parsed.rootDirectory][PS2D.filenames.d].data), PS2D.filenames.d);
}
break;
}
case "sys": {
let inputFile = filesystem.readFileSync(processObj.argv[3] ? processObj.argv[3] : "icon.sys");
const PS2D = iconjs.readPS2D(inputFile.buffer.slice(inputFile.byteOffset, inputFile.byteOffset + inputFile.byteLength));
let getFile = filesystem.readFileSync(PS2D.filenames.n);
loadAndConvertIcon(iconjs.readIconFile(getFile.buffer.slice(getFile.byteOffset, getFile.byteOffset + getFile.byteLength)), PS2D.filenames.n);
if(PS2D.filenames.n !== PS2D.filenames.c) {
let getFile = filesystem.readFileSync(PS2D.filenames.c);
loadAndConvertIcon(iconjs.readIconFile(getFile.buffer.slice(getFile.byteOffset, getFile.byteOffset + getFile.byteLength)), PS2D.filenames.c);
}
if(PS2D.filenames.n !== PS2D.filenames.d) {
let getFile = filesystem.readFileSync(PS2D.filenames.d);
loadAndConvertIcon(iconjs.readIconFile(getFile.buffer.slice(getFile.byteOffset, getFile.byteOffset + getFile.byteLength)), PS2D.filenames.d);
}
break;
}
case "ico":
case "icn": {
let inputFile = filesystem.readFileSync(processObj.argv[3] ? processObj.argv[3] : "input.icn");
loadAndConvertIcon(iconjs.readIconFile(inputFile.buffer.slice(inputFile.byteOffset, inputFile.byteOffset + inputFile.byteLength)), require("path").basename(processObj.argv[3]));
break;
}
default: {
//Template literal goes here.
console.info(
`${(processObj.argv.length > 2) ? "Unknown argument: "+processObj.argv[2]+"\n\n": ""}icondumper2 node.js client (glTF exporter version) subcommands:
psu: Read a EMS Memory Adapter export file.
psv: Read a PS3 export file.
sps: Read a SharkPort export file.
xps: Read a X-Port export file.
cbs: Read a CodeBreaker Save export file.
max: Read a Max Drive export file.
pws: Read a PowerSave export file.
sys: Read a icon.sys (964 bytes) file, and attempt
to read icon files from the current directory.
icn: Read an icon file directly. (Also as: ico)
` ); // end of template
processObj.exit(1);
}
}
processObj.exit(0);