Skip to content

packages/core/src/features/textures/lib/material-field.ts

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import type { ProceduralTextureProps } from './procedural-texture.js';
2 import { reuseFieldMemory, lazyKernel } from "./wasm/memory.js";
3 import { bytes as marbleBytes } from "./wasm/marble-bytes.js";
4 import { bytes as paperBytes } from "./wasm/paper-bytes.js";
5 import { bytes as cellularBytes } from "./wasm/cellular-bytes.js";
6 /**
7  * The supported static material generators: marble, paper, and cellular.
8  *
9  * @see {@link ProceduralTextureProps}
10  */
11 export type TextureKind = "marble" | "paper" | "cellular";
12 export type MaterialGenerator = (
13   width: number,
14   height: number,
15   seed: number,
16   scale: number,
17   detail: number,
18   previous?: Float32Array,
19 ) => Float32Array;
20 function generator(bytes: string): MaterialGenerator {
21   const module = lazyKernel(bytes);
22   return (width, height, seed, scale, detail, previous) => {
23     const owner = reuseFieldMemory(previous, module(), width, height);
24     const field = owner.fields[0];
25     owner.kernel.run(field.byteOffset, width, height, seed, scale, detail);
26     return field;
27   };
28 }
29 export const marbleField = /* @__PURE__ */ generator(marbleBytes);
30 export const paperField = /* @__PURE__ */ generator(paperBytes);
31 export const cellularField = /* @__PURE__ */ generator(cellularBytes);
32 const generators: Record<TextureKind, MaterialGenerator> = {
33   marble: marbleField,
34   paper: paperField,
35   cellular: cellularField,
36 };
37 export function materialField(
38   width: number,
39   height: number,
40   kind: TextureKind,
41   seed: number,
42   scale: number,
43   detail: number,
44   previous?: Float32Array,
45 ): Float32Array {
46   return generators[kind](width, height, seed, scale, detail, previous);
47 }
48 

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.