@pixi-essentials/texture-allocator
This package implements a slab-based texture allocator. It aims to help improve texture batching efficiency by
generating atlases on-the-fly. You can allocate and free texture space on demand. The following implementations
are provided:
- TextureAllocator: This generic allocator issues texture that are not backed by any resource. You must handle
resource management yourself.
- AtlasAllocator: This allocator can be used to issue image/canvas/bitmap- backed textures. Each image item is
uploaded separately with a
texSubImage2D call.
- CanvasTextureAllocator: This allocator creates texture slabs backed by a canvas. You can draw to that canvas
(
texture.baseTexture.resource.source.getContext('2d')) to draw each texture.
- RenderTextureAllocator: This allocator issues render-textures. Since you render directly into the render-texture,
it requires no resource management.
Installation :package:
npm install @pixi-essentials/texture-allocator
Usage :page_facing_up:
AtlasAllocator
import { AtlasAllocator } from '@pixi-essentials/texture-allocator';
const allocator = new AtlasAllocator();
const image = document.createElement('img');
image.src = "example.com/example.jpg";
image.onload = function() {
const { naturalWidth, naturalHeight } = image;
const texture = allocator.allocate(naturalWidth, naturalHeight);
allocate.free(texture);
}
CanvasTextureAllocator
import { CanvasTextureAllocator } from '@pixi-essentials/texture-allocator';
import type { BaseImageResource, Texture } from '@pixi/core';
const allocator = new CanvasTextureAllocator();
const texture = allocator.allocate(256, 256);
function cacheGeometry(texture: Texture) {
const baseTexture = texture.baseTexture;
const canvas = (baseTexture.resource as BaseImageResource).source as HTMLCanvasElement;
const context = canvas.getContext('2d');
const frame = texture.frame;
context.fillStyle = 'green';
context.fillRect(frame.left, frame.top, frame.width, frame.height);
}
cacheGeometry(texture);
RenderTextureAllocator
import { Graphics } from '@pixi/graphics';
import { RenderTextureAllocator } from '@pixi-essentials/texture-allocator';
const allocator = new RenderTextureAllocator();
const complexGraphics = new Graphics()
.beginFill(0xff)
.drawCircle(150, 150, 100, 100)
.endFill();
const texture = allocator.allocate(0, 0, 250, 250);
renderer.render(complexGraphics, texture);