src/scene/__docs__/container/cache-as-texture.md
If a container has many children that rarely change (a decorated UI panel, a complex background), you can snapshot it into a single texture. Subsequent frames draw that one texture instead of processing every child, which can significantly reduce draw calls.
[!NOTE]
cacheAsTextureis PixiJS v8's equivalent of the previouscacheAsBitmapfunctionality. If you're migrating from v7 or earlier, replacecacheAsBitmapwithcacheAsTexturein your code.
Call container.cacheAsTexture() to snapshot the container into a texture.
To update the texture after making changes to the container:
container.updateCacheTexture();
To turn it off:
container.cacheAsTexture(false);
import { Application, Assets, Container, Sprite } from 'pixi.js';
(async () => {
const app = new Application();
await app.init({ background: '#1099bb', resizeTo: window });
document.body.appendChild(app.canvas);
await Assets.load('https://pixijs.com/assets/spritesheet/monsters.json');
const aliens = [];
const alienFrames = ['eggHead.png', 'flowerTop.png', 'helmlok.png', 'skully.png'];
const alienContainer = new Container();
alienContainer.x = 400;
alienContainer.y = 300;
app.stage.addChild(alienContainer);
for (let i = 0; i < 100; i++) {
const frameName = alienFrames[i % 4];
const alien = Sprite.from(frameName);
alien.tint = Math.random() * 0xffffff;
alien.x = Math.random() * 800 - 400;
alien.y = Math.random() * 600 - 300;
alien.anchor.x = 0.5;
alien.anchor.y = 0.5;
aliens.push(alien);
alienContainer.addChild(alien);
}
// Cache as a single texture instead of drawing 100 sprites
alienContainer.cacheAsTexture();
})();
In this example, the container and its children are rendered to a single texture, reducing the rendering overhead when the scene is drawn.
Pass a configuration object instead of true:
container.cacheAsTexture({
resolution: 2,
antialias: true,
});
resolution: Resolution of the texture. Defaults to the renderer or application resolution.antialias: Antialias mode for the texture. Defaults to the renderer or application antialias mode.cacheAsTexture trades rendering speed for increased memory usage.renderer.texture.maxTextureSize if unsure.Under the hood, cacheAsTexture converts the container into a render group and renders it to a texture. It uses the same texture cache mechanism as filters:
container.enableRenderGroup();
container.renderGroup.cacheAsTexture = true;
Once cached, calling updateCacheTexture() is efficient and incurs minimal overhead. It's as fast as rendering the container normally.
cacheAsTexture to containers with elements that don't change frequently, such as a UI panel with static decorations.cacheAsTexture on containers larger than 4096x4096 pixels, as they may fail to cache due to GPU limitations. Split them into smaller containers instead.cacheAsTexture on and off repeatedly on containers. This causes constant re-caching and negates the benefits. Cache once, then use updateCacheTexture to refresh.cacheAsTexture for containers with few elements. The performance improvement will be negligible.cacheAsTexture can lead to resource constraints.Rendering depends on scene visibility: The cache updates only when the containing scene is rendered. Modifying the layout after calling cacheAsTexture but before rendering will be reflected in the cache.
Containers are rendered with no transform: Cached items are rendered at their actual size, ignoring transforms like scaling. An item scaled down by 50% will have its texture cached at 100% size, then scaled down by the scene.
Caching and filters: Filters may not behave as expected with cacheAsTexture. To cache the filter effect, wrap the item in a parent container and apply cacheAsTexture to the parent.
Reusing the texture: If you want to create a new texture based on the container, use const texture = renderer.generateTexture(container) and share it across your objects.