Phaser API Documentation

  Version: 
texture(key, [url], [xhrSettings])

Description:

Adds a Compressed Texture file to the current load queue. This feature is WebGL only.

This method takes a key and a configuration object, which lists the different formats and files associated with them.

The texture format object should be ordered in GPU priority order, with IMG as the last entry.

You can call this method from within your Scene's preload, along with any other files you wish to load:

preload ()
{
    this.load.texture('yourPic', {
        ASTC: { type: 'PVR', textureURL: 'pic-astc-4x4.pvr' },
        PVRTC: { type: 'PVR', textureURL: 'pic-pvrtc-4bpp-rgba.pvr' },
        S3TC: { type: 'PVR', textureURL: 'pic-dxt5.pvr' },
        IMG: { textureURL: 'pic.png' }
    });

If you wish to load a texture atlas, provide the atlasURL property:

preload ()
{
    const path = 'assets/compressed';

    this.load.texture('yourAtlas', {
        'ASTC': { type: 'PVR', textureURL: `${path}/textures-astc-4x4.pvr`, atlasURL: `${path}/textures.json` },
        'PVRTC': { type: 'PVR', textureURL: `${path}/textures-pvrtc-4bpp-rgba.pvr`, atlasURL: `${path}/textures-pvrtc-4bpp-rgba.json` },
        'S3TC': { type: 'PVR', textureURL: `${path}/textures-dxt5.pvr`, atlasURL: `${path}/textures-dxt5.json` },
        'IMG': { textureURL: `${path}/textures.png`, atlasURL: `${path}/textures.json` }
    });
}

If you wish to load a Multi Atlas, as exported from Texture Packer Pro, use the multiAtlasURL property instead:

preload ()
{
    const path = 'assets/compressed';

    this.load.texture('yourAtlas', {
        'ASTC': { type: 'PVR', atlasURL: `${path}/textures.json` },
        'PVRTC': { type: 'PVR', atlasURL: `${path}/textures-pvrtc-4bpp-rgba.json` },
        'S3TC': { type: 'PVR', atlasURL: `${path}/textures-dxt5.json` },
        'IMG': { atlasURL: `${path}/textures.json` }
    });
}

When loading a Multi Atlas you do not need to specify the textureURL property as it will be read from the JSON file.

Instead of passing arguments you can pass a configuration object, such as:

this.load.texture({
    key: 'yourPic',
    url: {
        ASTC: { type: 'PVR', textureURL: 'pic-astc-4x4.pvr' },
        PVRTC: { type: 'PVR', textureURL: 'pic-pvrtc-4bpp-rgba.pvr' },
        S3TC: { type: 'PVR', textureURL: 'pic-dxt5.pvr' },
        IMG: { textureURL: 'pic.png' }
   }
});

See the documentation for Phaser.Types.Loader.FileTypes.CompressedTextureFileConfig for more details.

The number of formats you provide to this function is up to you, but you should ensure you cover the primary platforms where appropriate.

The 'IMG' entry is a fallback to a JPG or PNG, should the browser be unable to load any of the other formats presented to this function. You should really always include this, although it is optional.

Phaser supports loading both the PVR and KTX container formats. Within those, it can parse the following texture compression formats:

ETC ETC1 ATC ASTC BPTC RGTC PVRTC S3TC S3TCSRGB

For more information about the benefits of compressed textures please see the following articles:

Texture Compression in 2020 (https://aras-p.info/blog/2020/12/08/Texture-Compression-in-2020/) Compressed GPU Texture Formats (https://themaister.net/blog/2020/08/12/compressed-gpu-texture-formats-a-review-and-compute-shader-decoders-part-1/)

To create compressed texture files use a 3rd party application such as:

Texture Packer (https://www.codeandweb.com/texturepacker/tutorials/how-to-create-sprite-sheets-for-phaser3?utm_source=ad&utm_medium=banner&utm_campaign=phaser-2018-10-16) PVRTexTool (https://developer.imaginationtech.com/pvrtextool/) - available for Windows, macOS and Linux. Mali Texture Compression Tool (https://developer.arm.com/tools-and-software/graphics-and-gaming/mali-texture-compression-tool) ASTC Encoder (https://github.com/ARM-software/astc-encoder)

ASTCs must have a Channel Type of Unsigned Normalized Bytes (UNorm) and a Linear RGB Color Space.

The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's preload method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene's preload method and then when the Scene's create method is called you are guaranteed that all of those assets are ready for use and have been loaded.

The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.

If you have specified a prefix in the loader, via Loader.setPrefix then this value will be prepended to this files key. For example, if the prefix was LEVEL1. and the key was Data the final key will be LEVEL1.Data and this is what you would use to retrieve the text from the Texture Manager.

The URL can be relative or absolute. If the URL is relative the Loader.baseURL and Loader.path values will be prepended to it.

Unlike other file loaders in Phaser, the URLs must include the file extension.

Note: The ability to load this type of file will only be available if the Compressed Texture File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.

Parameters:

name type arguments description
key string | Phaser.Types.Loader.FileTypes.CompressedTextureFileConfig | Array.<Phaser.Types.Loader.FileTypes.CompressedTextureFileConfig>

The key to use for this file, or a file configuration object, or array of them.

url Phaser.Types.Loader.FileTypes.CompressedTextureFileConfig <optional>

The compressed texture configuration object. Not required if passing a config object as the key parameter.

xhrSettings Phaser.Types.Loader.XHRSettingsObject <optional>

An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.

Returns:
Description:

The Loader instance.

Since: 3.60.0