-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptimize.mjs
94 lines (82 loc) · 3 KB
/
optimize.mjs
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
import { Document, NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
import draco3d from 'draco3dgltf';
import { resample, prune, dedup, draco, textureCompress, meshopt } from '@gltf-transform/functions';
import sharp from 'sharp';
import { ensureDirSync } from 'fs-extra';
import { readdirSync, statSync } from 'fs';
import { join, relative } from 'path';
const inputDir = '_output';
const outputDir = '_optimized';
// Variables
const imageFormat = 'webp'; // jpeg or png
const imageSize = 512; // 512 or 256
// Configure I/O.
const io = new NodeIO()
.registerExtensions(ALL_EXTENSIONS)
.registerDependencies({
'draco3d.decoder': await draco3d.createDecoderModule(),
'draco3d.encoder': await draco3d.createEncoderModule(),
});
// Create the '_optimized' directory if it doesn't exist
ensureDirSync(outputDir);
// Function to process the files recursively
async function processFiles(directory) {
const files = readdirSync(directory);
for (const file of files) {
const filePath = join(directory, file);
const stats = statSync(filePath);
if (stats.isDirectory()) {
const subOutputDir = join(outputDir, relative(inputDir, directory), file);
ensureDirSync(subOutputDir);
await processFiles(filePath); // Recursive call for subdirectories
} else if (stats.isFile() && file.endsWith('.glb')) {
const outputFile = join(outputDir, relative(inputDir, directory), file);
try {
// Read the glTF document from file
const document = await io.read(filePath);
// Apply transformations
await document.transform(
// Losslessly resample animation frames.
resample(),
// Remove unused nodes, textures, or other data.
prune(),
// Remove duplicate vertex or texture data, if any.
dedup(),
// Compress mesh geometry with Draco.
draco(),
// Convert textures to WebP (Requires glTF Transform v3 and Node.js).
textureCompress({
encoder: sharp,
targetFormat: imageFormat,
resize: [imageSize, imageSize],
}),
// Custom transform.
backfaceCulling({ cull: true })
);
// Save the optimized glTF document to the output file
await io.write(outputFile, document);
console.log(`Processed file: ${filePath}`);
} catch (error) {
console.error(`Error processing file: ${filePath}`);
console.error(error);
}
}
}
}
// Custom transform: enable/disable backface culling.
function backfaceCulling(options) {
return (document) => {
for (const material of document.getRoot().listMaterials()) {
material.setDoubleSided(!options.cull);
}
};
}
// Call the asynchronous function to process files starting from the '_output' directory
processFiles(inputDir)
.then(() => {
console.log('Files processed successfully.');
})
.catch((error) => {
console.error('Error processing files:', error);
});