Create a TexturePacker by calling:
let pack = new BABYLON.TexturePacker( name, targetMeshes, options, scene );
pack.processAsync().then( success ).catch( error );
The options argument has a few parameters you can use to tweak the result of the texture packing.
Default = [ 'ambientTexture', 'bumpTexture', 'diffuseTexture', 'emissiveTexture', 'lightmapTexture', 'opacityTexture', 'specularTexture' ]
Default = BABYLON.VertexBuffer.UVKind
Default = BABYLON.VertexBuffer.UVKind
Default = TexturePacker.LAYOUT_STRIP;
Default = 8;
Default = 256;
Default = TexturePacker.SUBUV_WRAP;
Default = new Color4(0, 0, 0, 1.0);
Default = 0.0115;
Default = true;
Default = 'black';
Default = true;
Default = true;
For PBR materials you will need to change the map to reflect the channels you want to target. The Environment map should be handled separately.
In order to ensure that the packing process does not lock your thread you will have to start the compilation process with
pack.processAsync().then( success ).catch( error )
Having all interactions with your pack happening in the success callback on the returned promise. See the below playgrounds for examples.
Downloading the pack is simple! When initializing the package through both a JSON load or naturally like in the above mentioned constructor, a Promise Object is created.
In order to assure that the textures are all packed and ready to go we call any interactions with the texture pack inside the success callback of the then
method.
pack.processAsync().then(
//Success
()=>{
pack.download( type, ?quality);
}
)
You can tell the downloaded to change between jpeg and png image types depending on if you need an alpha channel. Due to the fact that the images are stored as base64 you should avoid using png unless absolutely necessary. You can always download both types and then manually mix and match inside the JSON file.
To load from a downloaded package is easy! First create a blank Texture Package.
let pack = new BABYLON.TexturePacker( '', [], {}, scene );
Then simply call the loadFromJSON method, with JSON file as a string and then the same success/error promise callback structure as the processAsync.
pack.updateFromJSON( jsonString ).then( success ).catch( error );