Sharing experience of optimizing gltf using several command line tools.
The tool I used is below.
- imagemagick : used for texture resize. (Resizing or Scaling -- ImageMagick Examples)
- toktx : used to convert the format of texture to ktx2 (KTX Tools Reference: toktx)
- gltf-pipeline : used for converting glTF to glb and extracting separate textures(GitHub - CesiumGS/gltf-pipeline: Content pipeline tools for optimizing glTF assets. π)
- @gltf-transform/cli : used for optimizing the mesh (Command-line quickstart | glTF Transform)
As a result, it has been optimized to about 1/4 the size of the original. This greatly reduced the loading time and especially the ktx2 compression saved a lot of GPU memory. (The crash on mobile has been resolved after application.)
I created an automatic optimization process by linking each optimization process with Python code, and with one command, we were able to create optimized assets for each quality level version as shown below.
What I would most like to share is that gltf texture uri can be changed at runtime
SceneLoader.OnPluginActivatedObservable.add((loader) => {
const gltfLoader = loader as unknown as GLTFFileLoader;
gltfLoader.onParsed = (data) => {
// @ts-ignore
const images: IImage[] = data.json['images'];
for (const image of images) {
if (image.uri) {
// textureMaxSize : 256, 512 ...
image.uri = `${textureMaxSize}/${image.uri}`;
}
}
};
});
In this way, when loading a gltf/glb file, you can receive data through the onParsed event and change the uri at runtime. Even if itβs not an image uri, being able to modify the loading gltf/glb without creating a separate loader at runtime is useful and intriguing in many ways.
const src = `/assets/model${suffix}.glb`
await SceneLoader.ImportMeshAsync('', src);
Also, it was useful to load modeling extracted by quality level like this.
Through this optimization process, we have been able to select and load models and images at runtime according to quality settings, and we plan to load them according to the user device class in the future.
Itβs not enough, but I share the optimization tool I made in the path below. I plan to deploy to Docker in the future, so if you are interested, please give us your opinion about the tool.
I hope it is useful to you.