voxolith

makeGeneratorPool()

Run entity generators on a pool of workers, main-thread side. Generation is pure CPU work, so doing it here keeps the render thread free and spreads it across cores. Each worker runs serveGenerators with its own registry; the pool queues requests and hands each to the next free worker....

function makeGeneratorPool(opts: GeneratorPoolOptions): GeneratorPool;

Defined in: engine/src/worker/pool.ts:240

Run entity generators on a pool of workers, main-thread side. Generation is pure CPU work, so doing it here keeps the render thread free and spreads it across cores. Each worker runs serveGenerators with its own registry; the pool queues requests and hands each to the next free worker. Browser-only (needs Worker).

The queue is the app's to steer, and without options it is a plain FIFO: requests take a priority and an AbortSignal, and the pool can pause(), resume() and reprioritise(). Which models matter first is the game's call; the pool only offers the controls.

Parameters

ParameterTypeDescription
optsGeneratorPoolOptionsspawn creates one worker; only the consumer's bundler can resolve its entry.

Returns

GeneratorPool

The pool. Call destroy() when done; idle workers hold their memory.

Example

import { broadleafGenerator as oak } from "@voxolith/gen-tree";

const pool = makeGeneratorPool({
  spawn: () => new Worker(new URL("./gen.worker.ts", import.meta.url), { type: "module" }),
});
try {
  await pool.ready();
  const trees = await pool.generateMany(
    [1, 2, 3].map((seed) => ({ generator: oak.id, params: oak.defaults, seed })),
    { onDone: (done, total) => (info.textContent = `${done}/${total}`) },
  );
} finally {
  pool.destroy();
}

// Streaming: near models first, and drop the ones the player has walked away from.
const leave = new AbortController();
pool.generate(spec, { priority: -distance, signal: leave.signal }).catch(() => {});
pool.reprioritise((s) => -distanceTo(s));
leave.abort();

On this page