-
-
Save donmccurdy/8a1dc3b3a3363c61b454fe7d4821aafa to your computer and use it in GitHub Desktop.
Basic glTF 2.0 optimization pipeline, using glTF-Transform.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NodeIO } from '@gltf-transform/core'; | |
import { ALL_EXTENSIONS, DracoMeshCompression } from '@gltf-transform/extensions'; | |
import { center, dedup, inspect, quantize, weld } from '@gltf-transform/lib'; | |
const draco3d = require('draco3dgltf'); | |
const INPUT = '../glTF-Sample-Models/2.0/FlightHelmet/glTF/FlightHelmet.gltf'; | |
const OUTPUT = './FlightHelmet.glb'; | |
const MIN_DRACO_BYTES = 200000; | |
Promise.all([ | |
draco3d.createDecoderModule(), | |
draco3d.createEncoderModule() | |
]).then(async ([decoder, encoder]) => { | |
// Initialize I/O. | |
const io = new NodeIO() | |
.registerExtensions(ALL_EXTENSIONS) | |
.registerDependencies({ | |
'draco3d.decoder': decoder, | |
'draco3d.encoder': encoder, | |
}); | |
// Read input. | |
const document = io.read(INPUT); | |
// Apply transforms. | |
await document.transform( | |
center(), | |
dedup(), | |
weld() | |
); | |
// Calculate size of geometry. | |
let geometrySize = 0; | |
for (const meshReport of inspect(document).meshes.properties) { | |
geometrySize += meshReport.size; | |
} | |
// For large geometry, use Draco. For smaller geometry, just quantize. | |
if (geometrySize >= MIN_DRACO_BYTES) { | |
document.createExtension(DracoMeshCompression) | |
.setRequired(true) | |
.setEncoderOptions({ | |
decodeSpeed: 5, | |
encodeSpeed: 5, | |
}); | |
} else { | |
await document.transform(quantize()); | |
} | |
// Write output. | |
io.write(OUTPUT, document); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment