Compressing textures
Introduction
Compressed textures are used to reduce the size in video memory without sacrificing quality. Compressing textures can be done automatically when exporting the project but can require time to compute depending on the number of textures and their size.
Compressed textures are not necessary less heavy in terms of file size but are optimized for GPU usage. Therefore, they can significantly reduce the memory usage and also increase the performance of your game in terms of rendering speed by reducing the internal bandwidth transfers.
To compress textures using Babylon.js Editor, 2 methods are available:
- PVRTexTool: A command-line tool provided by Imagination Technologies used to generate one .ktx texture file per format (ASCT, DXT, ETC1, ETC2 and PVRTC).
- KTX-Software: An open-source texture compression CLI provided by the Khronos Group that supports KTX 2.0 container. Compared to PVRTexTool which generates one KTX file per format, KTX-Software is capable of creating a unique KTX 2.0 file per texture that is supported on all platforms which makes the compression process much faster and less storage consuming.
Installing KTX-Software
KTX-Software can be downloaded from their release page on Github. Only versions 4.x are supported. Download the appropriate package according to your current operating system (Windows, Linux or MacOS) and install it on your machine.
The KTX-Software installer may ask to install the command in PATH. This option must be enabled so the Babylon.js Editor can use the command.
Once installed, a new command is available on your system. To verify and try it, open a terminal and type the following command:
bash
Enabling KTX-Software in your project
Compressing textures in the Babylon.js Editor can be enabled by opening the project's configuration. To acess the project's settings, simply use the main toolbar Edit -> Project. In the Editor tab of the project's settings and under the "Textures" section, 3 options are available for compressing textures:
Enabled
Sets wether or not compressing textures is enabled when exporting the project. By default, this option is not enabled. When using Babylon.js Editor CLI, this option will also be used to determine if textures should be compressed during the generation process.
Enabled in preview
Sets wether or not textures used in the editor are also compressed. By default, this option is disabled. Using KTX allows to load textures faster. Enabling this feature will make the used textures in each scene being compressed and stored in the temporary directory .bjseditor available in the root folder of the project and scenes will load way faster than using original .png, .jpg, etc. textures.
Compressing temporary textures for the editor is done on the fly and will not affect the original texture files. To not overload the editor's process on your computer, textures are compressed one by one (not parallelized) and quality is set to Very fast.
Quality
The quality of the compression can be set to either Very fast, Fast, Normal or High. The higher the quality, the longer the compression process will take but the better the result will be. By default, the quality is set to Very fast.
Using Babylon.js Editor CLI
When packing the assets of your project using the Babylon.js Editor CLI, textures will be automatically compressed if the option is enabled in the project configuration. If not, only the original textures (.png, .jpg, etc.) will be packed without compression.
bash
Enabling KTX2 in your game
In your project, scenes are being loaded using the loadScene method from the babylonjs-editor-tools package. It is important to notify the package that KTX2 compressed textures should be used when loading the scene. To do so, simply call the following method before loading the scene:
typescript