PaletteAllocator
Shares the world's 256-slot palette between the entities stamped into it. Each entity (or each key, for entities that look alike) gets a contiguous range, and role r of an entity placed at base becomes world voxel value base + r - 1. Build the renderer's palette and material block from it once...
Defined in: engine/src/palette.ts:42
Shares the world's 256-slot palette between the entities stamped into it. Each entity (or each
key, for entities that look alike) gets a contiguous range, and role r of an entity placed at
base becomes world voxel value base + r - 1. Build the renderer's palette and material block
from it once everything is allocated; recolouring a range later restyles every copy.
Only for what is stamped into the world. Instanced models get palettes of their own
(InstanceLayer.palettes) and need no slots here. Ranges are never freed.
Example
const palette = new PaletteAllocator(1);
const { base: groundBase } = palette.allocate(terrain.roles, "terrain");
const { base: oakBase } = palette.allocateFor(oak);
const renderer = await createRenderer(gpu, {
size: SIZE, palette: palette.buildPalette(), materials: palette.buildMaterials(),
});
blitModelToBricks(renderer, oak.model, { x: 40, y: 12, z: 40 }, oakBase);Constructors
Constructor
new PaletteAllocator(firstSlot?: number): PaletteAllocator;Defined in: engine/src/palette.ts:54
The world's palette: 256 slots, since world voxels are 8-bit. Slot 0 is empty; reserve low slots for a host's own colours if wanted. Instanced models do not need slots here: they get palettes of their own (instancePalette, Renderer.addPalette).
Parameters
| Parameter | Type | Default value |
|---|---|---|
firstSlot? | number | 1 |
Returns
PaletteAllocator
Accessors
free
Get Signature
get free(): number;Defined in: engine/src/palette.ts:66
Slots still available.
Returns
number
used
Get Signature
get used(): number;Defined in: engine/src/palette.ts:61
Slots handed out so far, counting any reserved below firstSlot.
Returns
number
Methods
allocate()
allocate(roles: Role[], key?: string): Allocation;Defined in: engine/src/palette.ts:76
Reserve a range for these roles. key deduplicates: two entities from the
same generator and style share one range instead of burning the palette.
Parameters
| Parameter | Type |
|---|---|
roles | Role[] |
key? | string |
Returns
Throws
When the roles do not fit in the slots left.
allocateFor()
allocateFor(entity: Entity, key?: string): Allocation;Defined in: engine/src/palette.ts:92
Convenience: allocate for an entity, keyed by kind so clones share slots.
Parameters
| Parameter | Type | Default value |
|---|---|---|
entity | Entity | undefined |
key? | string | entity.kind |
Returns
buildMaterials()
buildMaterials(): Float32Array<ArrayBufferLike> | undefined;Defined in: engine/src/palette.ts:117
256 × 8 floats for RenderScene.materials, or undefined when no role asked
for one (the renderer then keeps its cheaper flat-palette path).
Returns
Float32Array<ArrayBufferLike> | undefined
buildPalette()
buildPalette(): Float32Array;Defined in: engine/src/palette.ts:103
256 × RGBA floats for RenderScene.palette.
Returns
Float32Array
setColor()
setColor(slot: number, color: RGB): void;Defined in: engine/src/palette.ts:97
Override one slot's colour, e.g. to tint a single placement.
Parameters
| Parameter | Type |
|---|---|
slot | number |
color | RGB |
Returns
void