The 2D layer · interface
SpritePass
Explained in Sprites and tilemaps.
interface SpritePass extends PassDefinitionimport type { SpritePass } from '@driftengine/ui2d';Properties
| Name | Type | Description |
|---|---|---|
batchreadonly | SpriteBatch | The quads for this frame. Reset it, fill it with drawSprite, then call drawPass.MoreHeld by the pass rather than handed in per frame, because its size is the pass's GPU buffer and the two must agree. |
whitereadonly | number | A slot holding one opaque white texel, filled by the pass and not settable.MoreWhite is the identity of the multiply the shader does, so a quad on this slot draws exactly its
tint — which is what a solid rectangle is, and what A background on this slot and an image from a sheet are two runs, because a slot change is a run and the order may not be regrouped. A caller with many filled boxes and many images interleaved should pack a white texel into its own sheet and tint a frame of that instead: then the whole tree is one run. This slot is the convenience, and the sheet is the fast path. |
Methods
reset
reset(): voidClear the batch. Call once at the top of a frame.
setTransform
setTransform(affine: Affine2D): voidWhere the batch's coordinates are, from screenToNdc or worldToNdc.
| Parameter | Type | Description |
|---|---|---|
affine | Affine2D |
More
Copied, so the caller may reuse its array; and read at draw time, so this can be called before the pass has a device.
setTexture
setTexture(slot: number, source: SpriteImage, options?: SpriteTextureOptions): voidFill a texture slot. Safe before the pass is registered; applied when it gets a device.
| Parameter | Type | Description |
|---|---|---|
slot | number | |
source | SpriteImage | |
options? | SpriteTextureOptions |
updateTexture
updateTexture(slot: number, source: SpriteImage, region: SpriteRegion): voidCopy region of source into the slot's texture at the same place, keeping the rest: what an
atlas painted one picture at a time sends, instead of all of itself.
| Parameter | Type | Description |
|---|---|---|
slot | number | |
source | SpriteImage | |
region | SpriteRegion |
More
The slot must hold a texture made from an image of source's size, through setTexture; it
keeps the options it was made with. Otherwise, or before the pass has a device, this does what
setTexture does with the slot's options (the default ones for an empty slot), so a caller
never has to ask which. The region is clipped to the image, and an empty one copies nothing. A
slot with a mip chain has the chain rebuilt.
The saving, measured by a consumer painting a 2D layer into atlases: setTexture sends the
whole image and, on WebGPU, makes the texture again, about 100 ms for a 5120-texel side, while
the pictures new on that frame covered about 6 of its 26 million texels and a typical later
frame changes a timer's digits. Call it before drawPass in a frame, as setTexture is.
Paint such an atlas into a 2D context made with willReadFrequently: true. On WebGPU a
canvas drawn on the GPU is transferred whole before any part of it is copied, measured at 66 ms
for one 256 by 64 rectangle of a 5120-texel atlas against 0.4 ms from a canvas kept in memory;
WebGL2 copies a rectangle quickly from either. What the option gives up is painting on the GPU,
which matters for a canvas redrawn whole every frame and not for an atlas painted in pieces.