@liamsteele/phaser-texture-util
v1.0.6
Published
A set of utilities for handling textures in Phaser
Downloads
610
Readme
phaser-texture-util
This repo contains a few texture helpers I've used to make Phaser development a bit nicer.
DynamicTextureManager
DynamicTextureManager allows you to create a DynamicTexture at runtime and add other textures to it dynamically. It handles filling the texture in a reasonably efficient way. You can add textures, texture frames, or whole atlases.
This allows you to consolidate many smaller textures into a single larger texture, improving rendering performance by reducing texture swaps. If you're using small textures you may be able fit them all into a single dynamic texture. At the least you should make sprites which are on the same layer together use the same dynamic texture since they will be next to each other on the display list.
It doesn't always tightly pack textures, instead it uses a row-based approach. E.g. when you add a texture with height 16, it will assign one row of 16 pixels and pop that texture as the first one in that row. Whenever you add another texture of the same height (width doesn't matter), it will use that existing row. If you add a texture of a different height, it makes a new row. It will only be inefficient if you have a lot of textures with different heights which should be rare.
You can use this to batch generated textures which you can only do (in a simple way) at runtime.
Usage:
// Inside a Phaser.Scene (usually preloader)
const dynamicTextureManager = new DynamicTextureManager(this);
this.dynamicTextureManager.createDynamicTexture('sprites', 2048, 2048, true);
// Start batch mode for efficient drawing (optional)
dynamicTextureManager.dynamicTexture.beginDraw();
// Add textures to the dynamic texture
dynamicTextureManager.addTextureToDynamicTexture('existingTextureWithoutFrames', 'newFrameName1');
dynamicTextureManager.addFrameToDynamicTexture('existingTextureWithFrames', 'existingFrameName', 'newFrameName2');
dynamicTextureManager.addTextureAtlasToDynamicTexture(this.textures.get('existingTextureAtlas'));
const texture =
// End batch mode and commit all draws (optional, required if you used startDraw)
dynamicTextureManager.dynamicTexture.endDraw();TextureColorExtractor
TextureColorExtractor allows you to find the unique colours in a texture. These could be used with TextureRecolorer to generate different sprites.
TextureRecolorer
TextureRecolorer allows you to replace colours in a texture. This is useful because tinting is done at runtime and doesn't allow much control over the actual color. This allows you to create alternate versions of textures easily which are stored as actual textures so they're efficient.
Plans
I have no plans to maintain or extend this, and the interfaces are just what I wanted. Feel free to fork/copy/use the code however you like.
