@margoengine/shader 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1771 -3
- package/dist/black-hole.d.ts +246 -0
- package/dist/black-hole.js +445 -0
- package/dist/clock-BLGNexlt.js +52 -0
- package/dist/clock.d.ts +42 -0
- package/dist/compute-D-BghbXj.js +354 -0
- package/dist/compute-texture.d.ts +100 -0
- package/dist/compute-texture.js +131 -0
- package/dist/compute-volume.d.ts +57 -0
- package/dist/compute-volume.js +130 -0
- package/dist/compute.d.ts +194 -0
- package/dist/control-DvjfV7R0.js +201 -0
- package/dist/control.d.ts +93 -0
- package/dist/deform.d.ts +111 -0
- package/dist/deform.js +89 -0
- package/dist/display-BrJM_Qkd.js +5 -0
- package/dist/editor-model.d.ts +192 -0
- package/dist/editor-pass.d.ts +71 -0
- package/dist/editor-pass.js +216 -0
- package/dist/editor.d.ts +150 -0
- package/dist/editor.js +1071 -0
- package/dist/effects.d.ts +150 -0
- package/dist/effects.js +87 -0
- package/dist/errors-CsQwshHA.js +7 -0
- package/dist/glsl/emit.d.ts +43 -0
- package/dist/glsl/host.d.ts +55 -0
- package/dist/glsl/material.d.ts +173 -0
- package/dist/glsl/parser.d.ts +76 -0
- package/dist/glsl/preprocess.d.ts +9 -0
- package/dist/glsl/presets.d.ts +33 -0
- package/dist/glsl/program.d.ts +210 -0
- package/dist/glsl/scene.d.ts +9 -0
- package/dist/glsl-material.d.ts +82 -0
- package/dist/glsl-material.js +288 -0
- package/dist/glsl-pass.d.ts +93 -0
- package/dist/glsl-pass.js +186 -0
- package/dist/glsl.d.ts +94 -0
- package/dist/glsl.js +170 -0
- package/dist/gpu-DQ5Jmggz.js +31 -0
- package/dist/graph.d.ts +82 -0
- package/dist/graph.js +365 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/lighting-Dx8_RsBz.js +77 -0
- package/dist/lighting.d.ts +62 -0
- package/dist/lighting.js +1 -0
- package/dist/material-CU7p-Q8u.js +1184 -0
- package/dist/material-REzUrXtu.js +275 -0
- package/dist/material.d.ts +74 -0
- package/dist/node-DzKv9CF2.js +590 -0
- package/dist/node.d.ts +427 -0
- package/dist/noise.d.ts +38 -0
- package/dist/noise.js +107 -0
- package/dist/own-stack-D1XnXrrz.js +11 -0
- package/dist/own-stack.d.ts +12 -0
- package/dist/pass.d.ts +108 -0
- package/dist/pass.js +236 -0
- package/dist/post-DC62ZleD.js +113 -0
- package/dist/post.d.ts +69 -0
- package/dist/preprocess-CWGWlBWQ.js +1195 -0
- package/dist/raw-D0W8U3Qa.js +1351 -0
- package/dist/raw.d.ts +634 -0
- package/dist/raw.js +1 -0
- package/dist/readback.d.ts +42 -0
- package/dist/readback.js +84 -0
- package/dist/scope.d.ts +21 -0
- package/dist/serialization.d.ts +11 -0
- package/dist/serialization.js +192 -0
- package/dist/shadertoy/sound.d.ts +48 -0
- package/dist/shadertoy.d.ts +249 -0
- package/dist/shadertoy.js +909 -0
- package/dist/shared/caster.d.ts +152 -0
- package/dist/shared/fog.d.ts +33 -0
- package/dist/shared/fractal.d.ts +9 -0
- package/dist/shared/msaa.d.ts +25 -0
- package/dist/shared/stack.d.ts +22 -0
- package/dist/sound-CF_dzP41.js +78 -0
- package/dist/storage-format-DWaiSxmq.js +25 -0
- package/dist/storage-format.d.ts +7 -0
- package/dist/subgroups.d.ts +97 -0
- package/dist/subgroups.js +95 -0
- package/dist/texture-D6vy0bam.js +62 -0
- package/dist/texture-array.d.ts +7 -0
- package/dist/texture-array.js +11 -0
- package/dist/texture.d.ts +16 -0
- package/dist/utils.d.ts +106 -0
- package/dist/utils.js +90 -0
- package/dist/view-rect-Bm1ac-TK.js +16 -0
- package/package.json +151 -6
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
type N = 2 | 3 | 4;
|
|
2
|
+
/** WGSL scalar types a node can have. */
|
|
3
|
+
export type Scalar = 'f32' | 'f16' | 'i32' | 'u32' | 'bool';
|
|
4
|
+
/** WGSL vector types with 2-4 components (`vec3f`, `vec2u`, `vec4<bool>`, ...). */
|
|
5
|
+
export type Vector = `vec${N}${'f' | 'h' | 'i' | 'u'}` | `vec${N}<bool>`;
|
|
6
|
+
/** Square f32 matrix types (column-major, as WGSL stores them). */
|
|
7
|
+
export type Matrix = 'mat2x2f' | 'mat3x3f' | 'mat4x4f';
|
|
8
|
+
/** Every WGSL type a {@link Node} can have; `Node<T>` carries it in its TypeScript type and in `node.type`. */
|
|
9
|
+
export type Type = Scalar | Vector | Matrix;
|
|
10
|
+
/** Component scalar of a type: `ScalarOf<'vec3f'>` is `'f32'` (matrices are f32). */
|
|
11
|
+
export type ScalarOf<T extends Type> = T extends 'f32' | `vec${N}f` | Matrix ? 'f32' : T extends 'f16' | `vec${N}h` ? 'f16' : T extends 'i32' | `vec${N}i` ? 'i32' : T extends 'u32' | `vec${N}u` ? 'u32' : 'bool';
|
|
12
|
+
/** Component count of a vector type, 1 for scalars and matrices. */
|
|
13
|
+
export type Size<T extends Type> = T extends `vec${infer K extends number}${string}` ? K : 1;
|
|
14
|
+
/** The type with scalar `S` and `K` components: `VecOf<'f32', 3>` is `'vec3f'`, `VecOf<'u32', 1>` is `'u32'`. */
|
|
15
|
+
export type VecOf<S extends Scalar, K extends number> = K extends 1 ? S : S extends 'bool' ? `vec${K & N}<bool>` : `vec${K & N}${S extends 'f32' ? 'f' : S extends 'f16' ? 'h' : S extends 'i32' ? 'i' : 'u'}`;
|
|
16
|
+
/** Result of `a op b` for + - * / %: equal types, scalar with vector or matrix, and matrix times vector. */
|
|
17
|
+
export type Arith<A extends Type, B extends Type> = A extends B ? A : A extends ScalarOf<B> ? B : B extends ScalarOf<A> ? A : A extends `mat${infer K}x${string}` ? B extends `vec${K}f` ? B : never : B extends `mat${infer K}x${string}` ? A extends `vec${K}f` ? A : never : never;
|
|
18
|
+
type Bools<T extends Type> = Node<VecOf<'bool', Size<T>>>;
|
|
19
|
+
type Floats<T extends Type> = Node<VecOf<'f32', Size<T>>>;
|
|
20
|
+
type C = 'x' | 'y' | 'z' | 'w';
|
|
21
|
+
type R = 'r' | 'g' | 'b' | 'a';
|
|
22
|
+
type Sw<X extends string> = X | `${X}${X}` | `${X}${X}${X}` | `${X}${X}${X}${X}`;
|
|
23
|
+
type Count<S extends string, A extends 0[] = []> = S extends `${infer _}${infer Rest}` ? Count<Rest, [...A, 0]> : A['length'];
|
|
24
|
+
/** `.x`, `.xy`, `.rgb`, `.zyx` ... on vectors; on a scalar they splat (`f.xxx` is `vec3f(f)`). */
|
|
25
|
+
export type Swizzles<T extends Type> = {
|
|
26
|
+
readonly [K in Sw<C> | Sw<R>]: Node<VecOf<ScalarOf<T>, Count<K>>>;
|
|
27
|
+
};
|
|
28
|
+
/** What node functions accept: a `Node<T>`, or a plain number that becomes a constant of the needed type. */
|
|
29
|
+
export type NodeInput<T extends Type = Type> = Node<T> | number;
|
|
30
|
+
/** The node type of an operand: `T` for a `Node<T>`, never for a plain number. */
|
|
31
|
+
type TypeOf<X> = X extends {
|
|
32
|
+
readonly type: infer T extends Type;
|
|
33
|
+
} ? T : never;
|
|
34
|
+
/**
|
|
35
|
+
* Result type of the component-wise built-ins (`abs`, `min`, `clamp`, `mix`, ...) for the operand list `A`: the widest
|
|
36
|
+
* node operand (a vector or matrix beats a scalar), or `f32` when every operand is a plain number.
|
|
37
|
+
*/
|
|
38
|
+
export type Widest<A extends readonly unknown[]> = [TypeOf<A[number]>] extends [never] ? 'f32' : [Exclude<TypeOf<A[number]>, Scalar>] extends [never] ? TypeOf<A[number]> : Exclude<TypeOf<A[number]>, Scalar>;
|
|
39
|
+
/** Result type of `add(a, b)` and the other free operators: a plain number adopts the other operand's type ({@link Arith}). */
|
|
40
|
+
export type BinaryResult<A, B> = [TypeOf<A>] extends [never] ? [TypeOf<B>] extends [never] ? 'f32' : TypeOf<B> : [TypeOf<B>] extends [never] ? TypeOf<A> : Arith<TypeOf<A>, TypeOf<B>>;
|
|
41
|
+
/** Never bound to a `let`. */
|
|
42
|
+
export declare const LEAF = 1;
|
|
43
|
+
/** A binary or unary operator: parenthesised when inlined into another operator. */
|
|
44
|
+
export declare const OP = 2;
|
|
45
|
+
/** Writes statements and returns a variable name, once per scope. */
|
|
46
|
+
export declare const STMT = 4;
|
|
47
|
+
/** Its value is settled when it is emitted, whatever it read: an assignment later does not change it. */
|
|
48
|
+
export declare const FIXED = 8;
|
|
49
|
+
/** Runs where it was made and never again: using it outside that scope is an error, not a second run. */
|
|
50
|
+
export declare const ONCE = 16;
|
|
51
|
+
/** Reads a mutable variable, so its value can change from one statement to the next. */
|
|
52
|
+
export declare const MUTABLE = 32;
|
|
53
|
+
/** Writes the WGSL for node `n` with builder `b` and returns its expression (or a variable name for statement nodes). */
|
|
54
|
+
export type Emit = (b: Builder, n: Node) => string;
|
|
55
|
+
/** A process-wide increasing id (0, 1, 2, ...) that keeps node keys of distinct uniforms, params and loops apart. */
|
|
56
|
+
export declare const unique: () => number;
|
|
57
|
+
/** An immutable expression. Build them with the functions in this package; `key` plus `args` must determine `emit`. */
|
|
58
|
+
export declare class Node<T extends Type = Type> {
|
|
59
|
+
readonly type: T;
|
|
60
|
+
readonly key: string;
|
|
61
|
+
readonly args: readonly Node[];
|
|
62
|
+
readonly emit: Emit;
|
|
63
|
+
readonly flags: number;
|
|
64
|
+
constructor(type: T, key: string, args: readonly Node[], emit: Emit, flags?: number);
|
|
65
|
+
}
|
|
66
|
+
export interface Node<T extends Type = Type> extends Swizzles<T> {
|
|
67
|
+
add(b: number): Node<T>;
|
|
68
|
+
add<U extends Type>(b: Node<U>): Node<Arith<T, U>>;
|
|
69
|
+
sub(b: number): Node<T>;
|
|
70
|
+
sub<U extends Type>(b: Node<U>): Node<Arith<T, U>>;
|
|
71
|
+
mul(b: number): Node<T>;
|
|
72
|
+
mul<U extends Type>(b: Node<U>): Node<Arith<T, U>>;
|
|
73
|
+
div(b: number): Node<T>;
|
|
74
|
+
div<U extends Type>(b: Node<U>): Node<Arith<T, U>>;
|
|
75
|
+
mod(b: number): Node<T>;
|
|
76
|
+
mod<U extends Type>(b: Node<U>): Node<Arith<T, U>>;
|
|
77
|
+
negate(): Node<T>;
|
|
78
|
+
lessThan(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Bools<T>;
|
|
79
|
+
lessThanEqual(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Bools<T>;
|
|
80
|
+
greaterThan(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Bools<T>;
|
|
81
|
+
greaterThanEqual(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Bools<T>;
|
|
82
|
+
equal(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Bools<T>;
|
|
83
|
+
notEqual(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Bools<T>;
|
|
84
|
+
and(b: Node<T>): Node<T>;
|
|
85
|
+
or(b: Node<T>): Node<T>;
|
|
86
|
+
not(): Node<T>;
|
|
87
|
+
abs(): Node<T>;
|
|
88
|
+
sign(): Node<T>;
|
|
89
|
+
floor(): Node<T>;
|
|
90
|
+
ceil(): Node<T>;
|
|
91
|
+
round(): Node<T>;
|
|
92
|
+
trunc(): Node<T>;
|
|
93
|
+
fract(): Node<T>;
|
|
94
|
+
sin(): Node<T>;
|
|
95
|
+
cos(): Node<T>;
|
|
96
|
+
tan(): Node<T>;
|
|
97
|
+
asin(): Node<T>;
|
|
98
|
+
acos(): Node<T>;
|
|
99
|
+
atan(): Node<T>;
|
|
100
|
+
tanh(): Node<T>;
|
|
101
|
+
exp(): Node<T>;
|
|
102
|
+
exp2(): Node<T>;
|
|
103
|
+
log(): Node<T>;
|
|
104
|
+
log2(): Node<T>;
|
|
105
|
+
sqrt(): Node<T>;
|
|
106
|
+
inverseSqrt(): Node<T>;
|
|
107
|
+
saturate(): Node<T>;
|
|
108
|
+
normalize(): Node<T>;
|
|
109
|
+
radians(): Node<T>;
|
|
110
|
+
degrees(): Node<T>;
|
|
111
|
+
dpdx(): Node<T>;
|
|
112
|
+
dpdy(): Node<T>;
|
|
113
|
+
fwidth(): Node<T>;
|
|
114
|
+
oneMinus(): Node<T>;
|
|
115
|
+
length(): Node<ScalarOf<T>>;
|
|
116
|
+
distance(b: NodeInput<T>): Node<ScalarOf<T>>;
|
|
117
|
+
dot(b: NodeInput<T>): Node<ScalarOf<T>>;
|
|
118
|
+
cross(b: Node<'vec3f'>): Node<'vec3f'>;
|
|
119
|
+
min(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Node<T>;
|
|
120
|
+
max(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Node<T>;
|
|
121
|
+
pow(b: NodeInput<T> | NodeInput<ScalarOf<T>>): Node<T>;
|
|
122
|
+
clamp(low: NodeInput<T> | NodeInput<ScalarOf<T>>, high: NodeInput<T> | NodeInput<ScalarOf<T>>): Node<T>;
|
|
123
|
+
step(edge: NodeInput<T> | NodeInput<ScalarOf<T>>): Node<T>;
|
|
124
|
+
mix(b: NodeInput<T>, t: NodeInput<T> | NodeInput<ScalarOf<T>>): Node<T>;
|
|
125
|
+
reflect(n: NodeInput<T>): Node<T>;
|
|
126
|
+
/** Component `i` of a vector, column of a matrix. */
|
|
127
|
+
element(i: NodeInput<'i32' | 'u32'>): Node<T extends Matrix ? `vec${T extends `mat${infer K extends number}x${string}` ? K : 4}f` : ScalarOf<T>>;
|
|
128
|
+
toFloat(): Floats<T>;
|
|
129
|
+
toInt(): Node<VecOf<'i32', Size<T>>>;
|
|
130
|
+
toUint(): Node<VecOf<'u32', Size<T>>>;
|
|
131
|
+
}
|
|
132
|
+
/** Runtime {@link ScalarOf}: `scalarOf('vec3u')` is `'u32'`. */
|
|
133
|
+
export declare const scalarOf: (t: Type) => Scalar;
|
|
134
|
+
/** Runtime {@link Size}: 2-4 for vectors, 1 for scalars and matrices. */
|
|
135
|
+
export declare const sizeOf: (t: Type) => number;
|
|
136
|
+
/** True for `mat2x2f`, `mat3x3f` and `mat4x4f`. */
|
|
137
|
+
export declare const isMatrix: (t: Type) => boolean;
|
|
138
|
+
/** Runtime {@link VecOf}: the type with scalar `s` and `n` (1-4) components. */
|
|
139
|
+
export declare const vecOf: (s: Scalar, n: number) => Type;
|
|
140
|
+
/** A constant (`float(0.5)`) or the conversion of a node (`float(intNode)`). */
|
|
141
|
+
export declare function constant<T extends Type>(value: number | boolean, type: T): Node<T>;
|
|
142
|
+
/** Lifts a number to `type` (scalars splat into vectors) and checks a node already has it. */
|
|
143
|
+
export declare function cast<T extends Type>(x: NodeInput | boolean, type: T, label?: string): Node<T>;
|
|
144
|
+
/** A WGSL built-in or user function call; `rule` picks the result type from the unified arguments. */
|
|
145
|
+
export declare function call<T extends Type>(name: string, args: Node[], type: T, wgsl?: string): Node<T>;
|
|
146
|
+
/**
|
|
147
|
+
* `a + b`, `a - b`, `a * b`, `a / b`, `a % b` as nodes. Numbers adopt the other operand's scalar type; a scalar
|
|
148
|
+
* combines with a vector or matrix, and `mul` multiplies a matrix by a vector (either side).
|
|
149
|
+
* @throws TypeError when the operand types do not combine (e.g. vec2f + vec3f, or bool arithmetic).
|
|
150
|
+
*/
|
|
151
|
+
export declare const add: <A extends NodeInput, B extends NodeInput>(a: A, b: B) => Node<BinaryResult<A, B>>;
|
|
152
|
+
/** `a - b` as a node; operand rules as {@link add}. */
|
|
153
|
+
export declare const sub: <A extends NodeInput, B extends NodeInput>(a: A, b: B) => Node<BinaryResult<A, B>>;
|
|
154
|
+
/** `a * b` as a node (matrix times vector either side); operand rules as {@link add}. */
|
|
155
|
+
export declare const mul: <A extends NodeInput, B extends NodeInput>(a: A, b: B) => Node<BinaryResult<A, B>>;
|
|
156
|
+
/** `a / b` as a node; operand rules as {@link add}. */
|
|
157
|
+
export declare const div: <A extends NodeInput, B extends NodeInput>(a: A, b: B) => Node<BinaryResult<A, B>>;
|
|
158
|
+
/** `a % b` (WGSL remainder, sign of `a`) as a node; operand rules as {@link add}. */
|
|
159
|
+
export declare const mod: <A extends NodeInput, B extends NodeInput>(a: A, b: B) => Node<BinaryResult<A, B>>;
|
|
160
|
+
/** WGSL built-ins. Scalars and numbers splat to the widest argument, so `mix(a, b, 0.5)` and `clamp(v, 0, 1)` work for vectors. */
|
|
161
|
+
export declare const abs: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, sign: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, floor: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, ceil: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, round: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, trunc: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, fract: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>;
|
|
162
|
+
/** WGSL trigonometry, component-wise, in radians: `atan2(y, x)` takes two operands. */
|
|
163
|
+
export declare const sin: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, cos: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, tan: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, asin: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, acos: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, atan: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, tanh: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, atan2: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>;
|
|
164
|
+
/** WGSL exponentials and roots, component-wise (`log` is natural; `saturate` clamps to 0..1). */
|
|
165
|
+
export declare const exp: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, exp2: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, log: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, log2: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, sqrt: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, inverseSqrt: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, saturate: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>;
|
|
166
|
+
/** `normalize` (unit vector), angle conversion (`radians`, `degrees`) and screen-space derivatives (`dpdx`, `dpdy`,
|
|
167
|
+
* `fwidth`; fragment and post stages only, WGSL rule). */
|
|
168
|
+
export declare const normalize: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, radians: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, degrees: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, dpdx: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, dpdy: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, fwidth: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>;
|
|
169
|
+
/**
|
|
170
|
+
* WGSL built-ins over several operands, WGSL argument order: `clamp(x, low, high)`, `step(edge, x)`,
|
|
171
|
+
* `smoothstep(low, high, x)` (edges first, unlike `@margoengine/math`'s), `mix(a, b, t)`, `reflect(i, n)`, `cross(a, b)`.
|
|
172
|
+
* @throws TypeError when the operand types do not match.
|
|
173
|
+
*/
|
|
174
|
+
export declare const min: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, max: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, pow: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, clamp: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, step: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, smoothstep: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, mix: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, reflect: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>, cross: <A extends NodeInput[]>(...a: A) => Node<Widest<A>>;
|
|
175
|
+
/** WGSL `length(v)`, `distance(a, b)` and `dot(a, b)`: scalar results of the operands' component type. */
|
|
176
|
+
export declare const length: <A extends NodeInput[]>(...a: A) => Node<ScalarOf<Widest<A>>>, distance: <A extends NodeInput[]>(...a: A) => Node<ScalarOf<Widest<A>>>, dot: <A extends NodeInput[]>(...a: A) => Node<ScalarOf<Widest<A>>>;
|
|
177
|
+
/** Refraction of incident `i` about unit normal `n` with index ratio `eta` (WGSL `refract`); zero on total internal reflection. */
|
|
178
|
+
export declare const refract: (i: NodeInput<'vec3f'>, n: NodeInput<'vec3f'>, eta: NodeInput<'f32'>) => Node<'vec3f'>;
|
|
179
|
+
/** `1 - x`, component-wise. */
|
|
180
|
+
export declare const oneMinus: <T extends Type>(x: Node<T>) => Node<T>;
|
|
181
|
+
/** `-x`. */
|
|
182
|
+
export declare const negate: <T extends Type>(x: Node<T>) => Node<T>;
|
|
183
|
+
/**
|
|
184
|
+
* Logical `!x` of a bool or bool vector.
|
|
185
|
+
* @throws TypeError for a non-bool node.
|
|
186
|
+
*/
|
|
187
|
+
export declare const not: <T extends Type>(x: Node<T>) => Node<T>;
|
|
188
|
+
/** `cond ? ifTrue : ifFalse` per component (both sides are evaluated; `If` runs only one). */
|
|
189
|
+
export declare function select<T extends Type>(cond: Node<'bool' | `vec${N}<bool>`> | boolean, ifTrue: NodeInput<T>, ifFalse: NodeInput<T>): Node<T>;
|
|
190
|
+
/** Discards the fragment where `condition` holds and is `value` elsewhere: `colorNode: discard(texture(map).a.lessThan(0.5), texture(map))`.
|
|
191
|
+
* NodeMaterial fragment nodes and NodePass nodes only (a discarded NodePass pixel stays the cleared transparent black). */
|
|
192
|
+
export declare function discard<T extends Type>(condition: NodeInput<'bool'> | boolean, value: NodeInput<T>): Node<T>;
|
|
193
|
+
type Part = NodeInput | boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Scalar constants (`float(0.5)`) or conversions of a scalar node (`float(intNode)`, WGSL `f32(...)`).
|
|
196
|
+
* @throws TypeError for a vector or matrix node, a non-finite number, or a non-integer i32/u32 constant.
|
|
197
|
+
*/
|
|
198
|
+
export declare const float: (x: Part) => Node<'f32'>, int: (x: Part) => Node<'i32'>, uint: (x: Part) => Node<'u32'>, bool: (x: Part) => Node<'bool'>;
|
|
199
|
+
/**
|
|
200
|
+
* f32 vectors from scalars and smaller vectors (`vec4(rgb, 1)`), or one splatted scalar (`vec3(0)`).
|
|
201
|
+
* @throws TypeError when the parts do not add up to the component count.
|
|
202
|
+
*/
|
|
203
|
+
export declare const vec2: (...p: Part[]) => Node<'vec2f'>, vec3: (...p: Part[]) => Node<'vec3f'>, vec4: (...p: Part[]) => Node<'vec4f'>;
|
|
204
|
+
/** i32 vectors, built like {@link vec2}. */
|
|
205
|
+
export declare const ivec2: (...p: Part[]) => Node<'vec2i'>, ivec3: (...p: Part[]) => Node<'vec3i'>, ivec4: (...p: Part[]) => Node<'vec4i'>;
|
|
206
|
+
/** u32 vectors, built like {@link vec2}. */
|
|
207
|
+
export declare const uvec2: (...p: Part[]) => Node<'vec2u'>, uvec3: (...p: Part[]) => Node<'vec3u'>, uvec4: (...p: Part[]) => Node<'vec4u'>;
|
|
208
|
+
/** Column-major: three vec3 columns or nine numbers. */
|
|
209
|
+
export declare const mat3: (...p: Part[]) => Node<'mat3x3f'>, mat4: (...p: Part[]) => Node<'mat4x4f'>;
|
|
210
|
+
/** A colour for {@link color} and uniforms: `'#rrggbb'` or `0xrrggbb` (sRGB, decoded to linear) or linear `[r, g, b]`. */
|
|
211
|
+
export type ColorValue = string | number | readonly number[];
|
|
212
|
+
/** `'#rrggbb'`, `'#rgb'` or `0xrrggbb` (sRGB) or linear `[r, g, b]`, as linear RGB. */
|
|
213
|
+
export declare function linearColor(c: ColorValue): number[];
|
|
214
|
+
/**
|
|
215
|
+
* A linear-RGB `vec3f` constant from a {@link ColorValue} (components rounded to 6 decimals).
|
|
216
|
+
* @throws TypeError for anything but `#rrggbb`, `#rgb`, `0xrrggbb` (0..0xffffff) or a 3-number array.
|
|
217
|
+
*/
|
|
218
|
+
export declare const color: (c: ColorValue) => Node<'vec3f'>;
|
|
219
|
+
/** A per-stage built-in such as `uv()` or `normalWorld`; the host stage decides what it reads. */
|
|
220
|
+
export declare const builtin: <T extends Type>(name: string, type: T) => Node<T>;
|
|
221
|
+
/** The mesh's first UV set (material stages) or the full-screen UV, origin top left (NodePass). */
|
|
222
|
+
export declare const uv: () => Node<'vec2f'>;
|
|
223
|
+
/** The mesh's vertex colour attribute (RGBA). NodeMaterial stages only. */
|
|
224
|
+
export declare const vertexColor: () => Node<'vec4f'>;
|
|
225
|
+
/** Vertex position in model space and world space (world units). NodeMaterial stages only. */
|
|
226
|
+
export declare const positionLocal: Node<"vec3f">, positionWorld: Node<"vec3f">;
|
|
227
|
+
/** Surface normal in model space and world space (unit length). NodeMaterial stages only. */
|
|
228
|
+
export declare const normalLocal: Node<"vec3f">, normalWorld: Node<"vec3f">;
|
|
229
|
+
/** Position and unit normal in view space: x right, y up, the camera looks along -z. NodeMaterial stages only. */
|
|
230
|
+
export declare const positionView: Node<"vec3f">, normalView: Node<"vec3f">;
|
|
231
|
+
/** Unit tangent in model and world space, and the unit world bitangent with the mesh's handedness. NodeMaterial stages only. */
|
|
232
|
+
export declare const tangentLocal: Node<"vec3f">, tangentWorld: Node<"vec3f">, bitangentWorld: Node<"vec3f">;
|
|
233
|
+
/** Distance from the camera plane to the surface in world units, positive in front of the camera. NodeMaterial stages only. */
|
|
234
|
+
export declare const linearDepth: Node<"f32">;
|
|
235
|
+
/** The camera's near and far planes in world units, read back from its projection. NodeMaterial stages only. */
|
|
236
|
+
export declare const cameraNear: Node<"f32">, cameraFar: Node<"f32">;
|
|
237
|
+
/** The mesh's model matrix (the node's, without an instance's), the camera's view and projection. NodeMaterial stages only. */
|
|
238
|
+
export declare const modelMatrix: Node<"mat4x4f">, viewMatrix: Node<"mat4x4f">, projectionMatrix: Node<"mat4x4f">;
|
|
239
|
+
/** The vertex's index in its buffer; in a fragment, the provoking vertex's. NodeMaterial stages only. */
|
|
240
|
+
export declare const vertexIndex: Node<"u32">;
|
|
241
|
+
/** This instance's matrix (node space), tint and UV scale (xy) and offset (zw): identity, white and (1, 1, 0, 0) without instances. NodeMaterial stages only. */
|
|
242
|
+
export declare const instanceMatrix: Node<"mat4x4f">, instanceColor: Node<"vec4f">, instanceUV: Node<"vec4f">;
|
|
243
|
+
/** Above 0.5 where the target holds linear light, as under an HDR PostStack; below it the frame is display-encoded. Fragment stage. */
|
|
244
|
+
export declare const hdrTarget: Node<"f32">;
|
|
245
|
+
/** Unit vector from the surface to the camera. */
|
|
246
|
+
export declare const viewDirection: Node<"vec3f">, cameraPosition: Node<"vec3f">;
|
|
247
|
+
/** Framebuffer pixel coordinate and its 0-1 UV (origin top left). */
|
|
248
|
+
export declare const screenCoordinate: Node<"vec2f">, screenUV: Node<"vec2f">;
|
|
249
|
+
/** Seconds since the page loaded, the last frame's duration and the target size in pixels; uniforms the host fills each frame. */
|
|
250
|
+
export declare const time: Node<"f32">, deltaTime: Node<"f32">, resolution: Node<"vec2f">;
|
|
251
|
+
/** Interpolates a vertex-stage value to the fragment stage (material nodes only; elsewhere it is the value itself). */
|
|
252
|
+
export declare function varying<T extends Type>(x: Node<T>): Node<T>;
|
|
253
|
+
/** A bounded fold: `loop(8, init, (i, acc) => next)` runs `acc = next` for i = 0..count-1 and returns the final `acc`. */
|
|
254
|
+
export declare function loop<T extends Type>(count: NodeInput<'i32' | 'u32'>, init: NodeInput<T>, body: (i: Node<'i32'>, acc: Node<T>) => NodeInput<T>): Node<T>;
|
|
255
|
+
interface Spec {
|
|
256
|
+
name: string;
|
|
257
|
+
id: number;
|
|
258
|
+
params: Node[];
|
|
259
|
+
result: Node;
|
|
260
|
+
}
|
|
261
|
+
/** A reusable WGSL function. Annotate parameters (`(a: Node<'vec3f'>, t: Node<'f32'>) => ...`); each argument-type signature
|
|
262
|
+
* emits one function, named `name` (then `name_1`, ...) in each shader. Stage inputs such as `uv()` must be passed in as arguments. */
|
|
263
|
+
export declare function Fn<P extends Type[], R extends Type>(body: (...args: {
|
|
264
|
+
[K in keyof P]: Node<P[K]>;
|
|
265
|
+
}) => Node<R>, name?: string): (...args: {
|
|
266
|
+
[K in keyof P]: NodeInput<P[K]>;
|
|
267
|
+
}) => Node<R>;
|
|
268
|
+
/** Wraps hand-written WGSL: the first `fn name(a: T, ...) -> R` is the one called; further functions in `code` come along. */
|
|
269
|
+
export declare function wgslFn<R extends Type = Type>(code: string): (...args: NodeInput[]) => Node<R>;
|
|
270
|
+
/** A value that changes without recompiling: assign `.value` any time; the host uploads it with each frame. */
|
|
271
|
+
export declare class UniformNode<T extends Type = Type> extends Node<T> {
|
|
272
|
+
readonly name?: string | undefined;
|
|
273
|
+
private data;
|
|
274
|
+
constructor(value: number | ColorValue, type: T, name?: string | undefined);
|
|
275
|
+
get value(): T extends Scalar ? number : number[];
|
|
276
|
+
set value(v: T extends Scalar ? number : number[] | ColorValue);
|
|
277
|
+
/** @internal */
|
|
278
|
+
write(out: DataView, offset: number): void;
|
|
279
|
+
}
|
|
280
|
+
/** A live value; the type follows the value (number f32, colour string vec3f, 2/3/4 numbers vecNf, 9 mat3x3f, 16 mat4x4f) or `type` (`uniform(3, {type: 'u32'})` is a `UniformNode<'u32'>`, e.g. a live `perlin3` seed). */
|
|
281
|
+
export declare function uniform(value: number, options?: {
|
|
282
|
+
type?: 'f32';
|
|
283
|
+
name?: string;
|
|
284
|
+
}): UniformNode<'f32'>;
|
|
285
|
+
export declare function uniform(value: string, options?: {
|
|
286
|
+
name?: string;
|
|
287
|
+
}): UniformNode<'vec3f'>;
|
|
288
|
+
export declare function uniform(value: readonly [number, number], options?: {
|
|
289
|
+
type?: 'vec2f' | 'vec2i' | 'vec2u';
|
|
290
|
+
name?: string;
|
|
291
|
+
}): UniformNode<'vec2f'>;
|
|
292
|
+
export declare function uniform(value: readonly [number, number, number], options?: {
|
|
293
|
+
type?: 'vec3f' | 'vec3i' | 'vec3u';
|
|
294
|
+
name?: string;
|
|
295
|
+
}): UniformNode<'vec3f'>;
|
|
296
|
+
export declare function uniform(value: readonly [number, number, number, number], options?: {
|
|
297
|
+
type?: 'vec4f' | 'vec4i' | 'vec4u' | 'mat2x2f';
|
|
298
|
+
name?: string;
|
|
299
|
+
}): UniformNode<'vec4f'>;
|
|
300
|
+
export declare function uniform<T extends Type>(value: number | ColorValue, options: {
|
|
301
|
+
type: T;
|
|
302
|
+
name?: string;
|
|
303
|
+
}): UniformNode<T>;
|
|
304
|
+
/** Host-side inputs a resource or frame uniform reads when bindings are built. */
|
|
305
|
+
export interface Host {
|
|
306
|
+
width: number;
|
|
307
|
+
height: number;
|
|
308
|
+
/** Seconds on the engine's frame clock (`ShaderClock`): the `time` frame value. Default 0. */
|
|
309
|
+
time?: number;
|
|
310
|
+
/** Seconds since the last frame: the `deltaTime` frame value. Default 0. */
|
|
311
|
+
delta?: number;
|
|
312
|
+
/** Values of frame members that are neither time nor size, by member name: a depth pass's camera. */
|
|
313
|
+
frames?: Readonly<Record<string, ArrayLike<number>>>;
|
|
314
|
+
[key: string]: unknown;
|
|
315
|
+
}
|
|
316
|
+
/** A bound GPU resource (texture, buffer, frame input). `declare` writes its WGSL variables; `entries` returns one resource per binding. */
|
|
317
|
+
export interface Resource {
|
|
318
|
+
/** Owner metadata for optional material serialization; GPU handles are never inferred. */
|
|
319
|
+
readonly source?: unknown;
|
|
320
|
+
readonly prefix: string;
|
|
321
|
+
readonly bindings: number;
|
|
322
|
+
declare(name: string, group: number, binding: number, compute: boolean): string;
|
|
323
|
+
entries(device: GPUDevice, host: Host): GPUBindingResource[];
|
|
324
|
+
/** Runs after each ComputePass dispatch that binds it: a ping-pong pair swaps here. */
|
|
325
|
+
finish?(): void;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* One shader stage a host compiles nodes for (`vertex`, `fragment`, `post` or `compute`): the WGSL each built-in such
|
|
329
|
+
* as `uv` reads there, and how `varying()` crosses to the next stage. Hosts (NodeMaterial, NodePass, ComputePass)
|
|
330
|
+
* define them; nodes only read them.
|
|
331
|
+
*/
|
|
332
|
+
export interface ShaderStage {
|
|
333
|
+
readonly kind: 'vertex' | 'fragment' | 'post' | 'compute';
|
|
334
|
+
readonly builtins: Readonly<Record<string, string | ((b: Builder) => string)>>;
|
|
335
|
+
varying?(b: Builder, x: Node): string;
|
|
336
|
+
}
|
|
337
|
+
/** One compiled shader: shared uniforms, resources and functions for one or more stages. Output text is stable for equal graphs. */
|
|
338
|
+
export declare class Program {
|
|
339
|
+
readonly base: number;
|
|
340
|
+
readonly compute: boolean;
|
|
341
|
+
/** Uniform block members in first-use order. */
|
|
342
|
+
readonly members: {
|
|
343
|
+
name: string;
|
|
344
|
+
type: Type;
|
|
345
|
+
offset: number;
|
|
346
|
+
node?: UniformNode;
|
|
347
|
+
frame?: string;
|
|
348
|
+
}[];
|
|
349
|
+
readonly resources: {
|
|
350
|
+
resource: Resource;
|
|
351
|
+
name: string;
|
|
352
|
+
binding: number;
|
|
353
|
+
}[];
|
|
354
|
+
readonly varyings: Node[];
|
|
355
|
+
/** @internal Fn specialisation key to WGSL name. */ readonly functions: Map<string, string>;
|
|
356
|
+
private readonly declarations;
|
|
357
|
+
private readonly canonical;
|
|
358
|
+
private readonly canonOf;
|
|
359
|
+
private readonly ids;
|
|
360
|
+
private readonly refs;
|
|
361
|
+
private readonly mutable;
|
|
362
|
+
private size;
|
|
363
|
+
private counter;
|
|
364
|
+
/** @internal The binding the next new resource takes: every binding below it is held. */ next: number;
|
|
365
|
+
private block?;
|
|
366
|
+
/** @internal The uniform block per device; TAA reads last frame's `bytes`. */ readonly buffers: WeakMap<GPUDevice, {
|
|
367
|
+
buffer: GPUBuffer;
|
|
368
|
+
view: DataView;
|
|
369
|
+
bytes: Uint8Array;
|
|
370
|
+
}>;
|
|
371
|
+
constructor(base?: number, compute?: boolean);
|
|
372
|
+
/** Compiles `outputs` for one stage; returns its statements and one expression per output. */
|
|
373
|
+
stage(stage: ShaderStage, outputs: readonly Node[]): {
|
|
374
|
+
lines: string[];
|
|
375
|
+
values: string[];
|
|
376
|
+
};
|
|
377
|
+
/** Module-scope WGSL: uniform struct, resource bindings and functions. */
|
|
378
|
+
declare(group: number): string;
|
|
379
|
+
/** Bind group entries; uploads uniforms (and frame values) first. */
|
|
380
|
+
entries(device: GPUDevice, host: Host): GPUBindGroupEntry[];
|
|
381
|
+
/** @internal Destroys the uniform buffer made for `device`; the next `entries` call makes a new one. */
|
|
382
|
+
release(device: GPUDevice): void;
|
|
383
|
+
private upload;
|
|
384
|
+
/** @internal */ name(): string;
|
|
385
|
+
/** @internal */ canon(n: Node): Node;
|
|
386
|
+
/** @internal */ count(n: Node): void;
|
|
387
|
+
/** @internal */ shared(c: Node): boolean;
|
|
388
|
+
/** @internal True when `c` reads a mutable variable and is not settled, so an assignment makes it stale. */
|
|
389
|
+
volatile(c: Node): boolean;
|
|
390
|
+
/** @internal */ declareOnce(key: string, code: () => string): void;
|
|
391
|
+
/** @internal */ resource(resource: Resource): string;
|
|
392
|
+
/** @internal */ member(type: Type, name: string, node?: UniformNode, frame?: string): string;
|
|
393
|
+
}
|
|
394
|
+
/** True when two entry lists bind the same resources (a uniform `{buffer}` compares by buffer). */
|
|
395
|
+
export declare const sameEntries: (a: readonly GPUBindGroupEntry[], b: readonly GPUBindGroupEntry[]) => boolean;
|
|
396
|
+
/** Emits one stage. Expressions referenced more than once become `let ndN`; everything else is inlined. */
|
|
397
|
+
export declare class Builder {
|
|
398
|
+
readonly program: Program;
|
|
399
|
+
readonly stage: ShaderStage;
|
|
400
|
+
inFn: boolean;
|
|
401
|
+
/** Counts assignments and loop entries: a mutable value kept from an earlier epoch is stale. */
|
|
402
|
+
epoch: number;
|
|
403
|
+
private scopes;
|
|
404
|
+
private readonly memo;
|
|
405
|
+
private readonly params;
|
|
406
|
+
constructor(program: Program, stage: ShaderStage);
|
|
407
|
+
expr(n: Node): string;
|
|
408
|
+
/** The expression, parenthesised when it is an inlined operator. */
|
|
409
|
+
wrap(n: Node): string;
|
|
410
|
+
line(code: string): void;
|
|
411
|
+
push(): void;
|
|
412
|
+
pop(): string[];
|
|
413
|
+
name(): string;
|
|
414
|
+
bind(param: Node, name: string): void;
|
|
415
|
+
unbind(param: Node): void;
|
|
416
|
+
param(n: Node): string;
|
|
417
|
+
/** A stage-local expression (reads entry-point inputs), refused inside Fn bodies. */
|
|
418
|
+
local(code: string): string;
|
|
419
|
+
builtin(name: string): string;
|
|
420
|
+
resource(r: Resource): string;
|
|
421
|
+
uniform(u: UniformNode): string;
|
|
422
|
+
frame(name: string, type: Type): string;
|
|
423
|
+
declare(key: string, code: string): void;
|
|
424
|
+
/** Declares a Fn specialisation once per program and returns its WGSL name. */
|
|
425
|
+
fn(spec: Spec): string;
|
|
426
|
+
}
|
|
427
|
+
export {};
|
package/dist/noise.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Node, type NodeInput } from './node.js';
|
|
2
|
+
import { type FractalOptions } from './shared/fractal.js';
|
|
3
|
+
/** Octave sum settings for `fbm` and `ridged` (the CPU twins' FractalOptions, same defaults). */
|
|
4
|
+
export type NoiseFractalOptions = FractalOptions;
|
|
5
|
+
/** `domainWarp` settings. */
|
|
6
|
+
export interface NoiseWarpOptions<T extends 'vec2f' | 'vec3f' | 'vec4f'> {
|
|
7
|
+
/** Field displacing each axis (default: the warped field). */ warp?: (p: Node<T>) => Node<'f32'>;
|
|
8
|
+
/** Displacement scale (default 1). */
|
|
9
|
+
strength?: number;
|
|
10
|
+
}
|
|
11
|
+
/** A noise field over 2D, 3D or 4D positions, as `fbm`, `ridged` and `domainWarp` take it. */
|
|
12
|
+
export type NoiseField<T extends 'vec2f' | 'vec3f' | 'vec4f'> = (p: Node<T>) => Node<'f32'>;
|
|
13
|
+
/** 2D simplex in about [-1, 1]; `new Noise(seed).simplex2(x, y)` on the CPU. */
|
|
14
|
+
export declare const simplex2: (p: NodeInput<'vec2f'>, seed?: number) => Node<'f32'>;
|
|
15
|
+
/** 3D simplex in about [-1, 1]; `new Noise(seed).simplex(x, y, z)` on the CPU. */
|
|
16
|
+
export declare const simplex3: (p: NodeInput<'vec3f'>, seed?: number) => Node<'f32'>;
|
|
17
|
+
/** 4D simplex in about [-1, 1]; `new Noise(seed).simplex4(x, y, z, w)` on the CPU. Walk w, or a circle in two axes, to animate or loop a field. */
|
|
18
|
+
export declare const simplex4: (p: NodeInput<'vec4f'>, seed?: number) => Node<'f32'>;
|
|
19
|
+
/** 3D gradient noise; `new Noise(seed).perlin(x, y, z)` on the CPU. `seed` may be a u32 node (a uniform reseeds without recompiling). */
|
|
20
|
+
export declare const perlin3: (p: NodeInput<'vec3f'>, seed?: number | Node<'u32'>) => Node<'f32'>;
|
|
21
|
+
/**
|
|
22
|
+
* Cellular noise in 2D, as `vec3f`: `.x` the distance to the nearest cell point, `.y` to the second nearest, `.z` the
|
|
23
|
+
* nearest cell's id from 0 to 1. `.y.sub(.x)` is 0 on the borders, for cracks; `.z` gives each cell its own value. `seed`
|
|
24
|
+
* may be a u32 node.
|
|
25
|
+
*/
|
|
26
|
+
export declare const voronoi2: (p: NodeInput<'vec2f'>, seed?: number | Node<'u32'>) => Node<'vec3f'>;
|
|
27
|
+
/** {@link voronoi2} in 3D, with the feature points of `new Noise(seed).worley`. */
|
|
28
|
+
export declare const voronoi3: (p: NodeInput<'vec3f'>, seed?: number | Node<'u32'>) => Node<'vec3f'>;
|
|
29
|
+
/** Distance to the nearest cell point, from 0; about 0.5 on average. */
|
|
30
|
+
export declare const worley2: (p: NodeInput<'vec2f'>, seed?: number | Node<'u32'>) => Node<'f32'>;
|
|
31
|
+
/** {@link worley2} in 3D; `new Noise(seed).worley(x, y, z)` on the CPU. */
|
|
32
|
+
export declare const worley3: (p: NodeInput<'vec3f'>, seed?: number | Node<'u32'>) => Node<'f32'>;
|
|
33
|
+
/** Fractal Brownian motion: octaves of `noise` divided by the total amplitude (unrolled; `fbm` on the CPU). */
|
|
34
|
+
export declare const fbm: <T extends 'vec2f' | 'vec3f' | 'vec4f'>(noise: NoiseField<T>, p: Node<T>, options?: NoiseFractalOptions) => Node<'f32'>;
|
|
35
|
+
/** Ridged multifractal in [0, 1]: octaves of `(1 - |noise|)^2` (`ridged` on the CPU). */
|
|
36
|
+
export declare const ridged: <T extends 'vec2f' | 'vec3f' | 'vec4f'>(noise: NoiseField<T>, p: Node<T>, options?: NoiseFractalOptions) => Node<'f32'>;
|
|
37
|
+
/** Domain warping: `noise(p + strength * (warp(p + o0), warp(p + o1), ...))`, one term per axis (`domainWarp` on the CPU). */
|
|
38
|
+
export declare function domainWarp<T extends 'vec2f' | 'vec3f' | 'vec4f'>(noise: NoiseField<T>, p: Node<T>, { warp, strength }?: NoiseWarpOptions<T>): Node<'f32'>;
|
package/dist/noise.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { T as e, cn as t, ln as n, o as r, sn as i } from "./node-DzKv9CF2.js";
|
|
2
|
+
//#region ../shared/fractal.js
|
|
3
|
+
function a({ octaves: e = 5, lacunarity: t = 2, gain: n = .5, frequency: r = 1 }) {
|
|
4
|
+
if (!Number.isInteger(e) || e < 1 || e > 16 || ![
|
|
5
|
+
t,
|
|
6
|
+
n,
|
|
7
|
+
r
|
|
8
|
+
].every(Number.isFinite)) throw RangeError("Invalid fractal options; pass integer octaves from 1 to 16 and finite lacunarity, gain and frequency");
|
|
9
|
+
return [
|
|
10
|
+
e,
|
|
11
|
+
t,
|
|
12
|
+
n,
|
|
13
|
+
r
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/noise.ts
|
|
18
|
+
var o = [
|
|
19
|
+
0,
|
|
20
|
+
5.2,
|
|
21
|
+
1.7,
|
|
22
|
+
9.2
|
|
23
|
+
], s = (3 - Math.sqrt(3)) / 6, c = .5 * (Math.sqrt(3) - 1), l = (Math.sqrt(5) - 1) / 4, u = (5 - Math.sqrt(5)) / 20;
|
|
24
|
+
function d(e) {
|
|
25
|
+
let t = e >>> 0 || 1, n = Uint8Array.from({ length: 256 }, (e, t) => t);
|
|
26
|
+
for (let e = 255; e > 0; e--) {
|
|
27
|
+
t ^= t << 13, t ^= t >>> 17, t ^= t << 5;
|
|
28
|
+
let r = Math.floor((t >>> 0) / 4294967296 * (e + 1)), i = n[e];
|
|
29
|
+
n[e] = n[r], n[r] = i;
|
|
30
|
+
}
|
|
31
|
+
return n;
|
|
32
|
+
}
|
|
33
|
+
var f = (e) => {
|
|
34
|
+
if (!Number.isSafeInteger(e)) throw RangeError("Noise seed must be a safe integer; pass a whole number.");
|
|
35
|
+
return e >>> 0 || 1;
|
|
36
|
+
}, p = (e) => {
|
|
37
|
+
let t = d(e), n = Array.from({ length: 64 }, (e, n) => ((t[n * 4] | t[n * 4 + 1] << 8 | t[n * 4 + 2] << 16 | t[n * 4 + 3] << 24) >>> 0) + "u"), r = "noisePerm" + e.toString(16);
|
|
38
|
+
return `var<private> ${r}Table: array<u32, 64> = array<u32, 64>(${n.join(", ")});\nfn ${r}(i: i32) -> i32 {\n let k = u32(i) & 255u;\n return i32((${r}Table[k >> 2u] >> ((k & 3u) * 8u)) & 255u);\n}`;
|
|
39
|
+
}, m = "fn noiseGrad2(h: i32) -> vec2f {\n let g = h & 7; let a = select(1.0, -1.0, (g & 1) != 0); let b = select(1.0, -1.0, (g & 2) != 0);\n if (g < 4) { return vec2f(a, b); }\n if (g < 6) { return vec2f(a, 0.0); }\n return vec2f(0.0, a);\n}\nfn noiseCorner2(d: vec2f, h: i32) -> f32 {\n var c = 0.5 - dot(d, d);\n if (c < 0.0) { return 0.0; }\n c = c * c;\n return c * c * dot(noiseGrad2(h), d);\n}", h = "fn noiseGrad3(h: i32) -> vec3f {\n let g = h % 12; let a = select(1.0, -1.0, (g & 1) != 0); let b = select(1.0, -1.0, (g & 2) != 0);\n if (g < 4) { return vec3f(a, b, 0.0); }\n if (g < 8) { return vec3f(a, 0.0, b); }\n return vec3f(0.0, a, b);\n}\nfn noiseCorner3(d: vec3f, h: i32) -> f32 {\n var c = 0.5 - dot(d, d);\n if (c < 0.0) { return 0.0; }\n c = c * c;\n return c * c * dot(noiseGrad3(h), d);\n}", g = "fn noiseCorner4(d: vec4f, h: i32) -> f32 {\n var c = 0.5 - dot(d, d);\n if (c < 0.0) { return 0.0; }\n c = c * c;\n\n let g = h & 31; let zero = g >> 3u;\n let s = vec3f(select(1.0, -1.0, (g & 4) != 0), select(1.0, -1.0, (g & 2) != 0), select(1.0, -1.0, (g & 1) != 0));\n var v = d.xyz;\n if (zero == 0) { v = d.yzw; } else if (zero == 1) { v = d.xzw; } else if (zero == 2) { v = d.xyw; }\n return c * c * dot(s, v);\n}", _ = "fn noiseHash(x: i32, y: i32, z: i32, seed: u32) -> u32 {\n var h = (bitcast<u32>(x) * 374761393u) ^ (bitcast<u32>(y) * 668265263u) ^ (bitcast<u32>(z) * 1442695041u) ^ seed;\n h = (h ^ (h >> 13u)) * 1274126177u;\n return h ^ (h >> 16u);\n}", v = "fn noiseVoronoi2(p: vec2f, seed: u32) -> vec3f {\n let c = floor(p); let f = p - c; let i = vec2i(c);\n var best = vec2f(1e30); var id = 0u;\n for (var a = -2; a <= 2; a++) { for (var b = -2; b <= 2; b++) {\n let x = i.x + a; let y = i.y + b;\n let h = vec2f(f32(noiseHash(x, y, 0, seed)), f32(noiseHash(x, y, 0, seed ^ 0x9e3779b9u)));\n let d = vec2f(f32(a), f32(b)) + h / 4294967296.0 - f;\n let e = dot(d, d);\n if (e < best.x) { best = vec2f(e, best.x); id = noiseHash(x, y, 0, seed ^ 0xc2b2ae35u); }\n else if (e < best.y) { best.y = e; }\n } }\n return vec3f(sqrt(best), f32(id >> 8u) / 16777216.0);\n}", y = "fn noiseVoronoi3(p: vec3f, seed: u32) -> vec3f {\n let c = floor(p); let f = p - c; let i = vec3i(c);\n var best = vec2f(1e30); var id = 0u;\n for (var a = -2; a <= 2; a++) { for (var b = -2; b <= 2; b++) { for (var k = -2; k <= 2; k++) {\n let x = i.x + a; let y = i.y + b; let z = i.z + k;\n let h = vec3f(f32(noiseHash(x, y, z, seed)), f32(noiseHash(x, y, z, seed ^ 0x9e3779b9u)), f32(noiseHash(x, y, z, seed ^ 0x85ebca6bu)));\n let d = vec3f(f32(a), f32(b), f32(k)) + h / 4294967296.0 - f;\n let e = dot(d, d);\n if (e < best.x) { best = vec2f(e, best.x); id = noiseHash(x, y, z, seed ^ 0xc2b2ae35u); }\n else if (e < best.y) { best.y = e; }\n } } }\n return vec3f(sqrt(best), f32(id >> 8u) / 16777216.0);\n}", b = "\n\nvar<private> noisePerlinGradients: array<vec3f, 16> = array<vec3f, 16>(\n vec3f(1, 1, 0), vec3f(-1, 1, 0), vec3f(1, -1, 0), vec3f(-1, -1, 0), vec3f(1, 0, 1), vec3f(-1, 0, 1), vec3f(1, 0, -1),\n vec3f(-1, 0, -1), vec3f(0, 1, 1), vec3f(0, -1, 1), vec3f(0, 1, -1), vec3f(0, -1, -1), vec3f(1, 1, 0), vec3f(0, -1, 1),\n vec3f(-1, 1, 0), vec3f(0, -1, -1));\nfn noisePerlinGrad(h: u32, d: vec3f) -> f32 { return dot(noisePerlinGradients[h & 15u], d); }\nfn noisePerlin3(p: vec3f, seed: u32) -> f32 {\n let c = floor(p); let f = p - c; let i = vec3i(c); let w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n var value = 0.0;\n for (var a = 0; a < 2; a++) { for (var b = 0; b < 2; b++) { for (var e = 0; e < 2; e++) {\n let o = vec3i(a, b, e); let s = select(1.0 - w, w, o == vec3i(1));\n value += noisePerlinGrad(noiseHash(i.x + a, i.y + b, i.z + e, seed), f - vec3f(o)) * s.x * s.y * s.z;\n } } }\n return value;\n}", x = (e) => `fn ${e}Simplex2(v: vec2f) -> f32 {
|
|
40
|
+
let s = (v.x + v.y) * ${c}; let i = floor(v.x + s); let j = floor(v.y + s); let t = (i + j) * ${s};
|
|
41
|
+
let x0 = v - vec2f(i - t, j - t); let o = select(vec2i(0, 1), vec2i(1, 0), x0.x > x0.y);
|
|
42
|
+
let ii = i32(i) & 255; let jj = i32(j) & 255;
|
|
43
|
+
return 70.0 * (noiseCorner2(x0, ${e}(ii + ${e}(jj))) + noiseCorner2(x0 - vec2f(o) + ${s}, ${e}(ii + o.x + ${e}(jj + o.y))) +
|
|
44
|
+
noiseCorner2(x0 - 1.0 + ${2 * s}, ${e}(ii + 1 + ${e}(jj + 1))));
|
|
45
|
+
}`, S = (e) => `fn ${e}Simplex3(v: vec3f) -> f32 {
|
|
46
|
+
let s = (v.x + v.y + v.z) * ${1 / 3}; let c = floor(v + s); let t = (c.x + c.y + c.z) * ${1 / 6};
|
|
47
|
+
let x0 = v - (c - t);
|
|
48
|
+
|
|
49
|
+
let gt = vec3i(select(0, 1, x0.x > x0.y), select(0, 1, x0.x > x0.z), select(0, 1, x0.y > x0.z));
|
|
50
|
+
let rank = vec3i(gt.x + gt.y, 1 - gt.x + gt.z, 2 - gt.y - gt.z);
|
|
51
|
+
let o1 = vec3i(rank >= vec3i(2)); let o2 = vec3i(rank >= vec3i(1));
|
|
52
|
+
let i = vec3i(c) & vec3i(255);
|
|
53
|
+
return 76.0 * (noiseCorner3(x0, ${e}(i.x + ${e}(i.y + ${e}(i.z)))) +
|
|
54
|
+
noiseCorner3(x0 - vec3f(o1) + ${1 / 6}, ${e}(i.x + o1.x + ${e}(i.y + o1.y + ${e}(i.z + o1.z)))) +
|
|
55
|
+
noiseCorner3(x0 - vec3f(o2) + ${2 / 6}, ${e}(i.x + o2.x + ${e}(i.y + o2.y + ${e}(i.z + o2.z)))) +
|
|
56
|
+
noiseCorner3(x0 - 0.5, ${e}(i.x + 1 + ${e}(i.y + 1 + ${e}(i.z + 1)))));
|
|
57
|
+
}`, C = (e) => `fn ${e}Simplex4(v: vec4f) -> f32 {
|
|
58
|
+
let s = (v.x + v.y + v.z + v.w) * ${l}; let c = floor(v + s); let t = (c.x + c.y + c.z + c.w) * ${u};
|
|
59
|
+
let x0 = v - (c - t);
|
|
60
|
+
|
|
61
|
+
let a = select(0, 1, x0.x > x0.y); let b = select(0, 1, x0.x > x0.z); let d = select(0, 1, x0.x > x0.w);
|
|
62
|
+
let e = select(0, 1, x0.y > x0.z); let g = select(0, 1, x0.y > x0.w); let h = select(0, 1, x0.z > x0.w);
|
|
63
|
+
let rank = vec4i(a + b + d, 1 - a + e + g, 2 - b - e + h, 3 - d - g - h);
|
|
64
|
+
let i = vec4i(c) & vec4i(255);
|
|
65
|
+
var sum = 0.0;
|
|
66
|
+
for (var n = 0; n < 5; n++) {
|
|
67
|
+
let o = vec4i(rank >= vec4i(4 - n));
|
|
68
|
+
sum += noiseCorner4(x0 - vec4f(o) + f32(n) * ${u}, ${e}(i.x + o.x + ${e}(i.y + o.y + ${e}(i.z + o.z + ${e}(i.w + o.w)))));
|
|
69
|
+
}
|
|
70
|
+
return 62.0 * sum;
|
|
71
|
+
}`;
|
|
72
|
+
function w(e, t, n, i = "f32") {
|
|
73
|
+
return new r(i, "z" + e, t, (t, r) => {
|
|
74
|
+
for (let [e, r] of n) t.declare(e, r);
|
|
75
|
+
return `${e}(${r.args.map((e) => t.expr(e)).join(", ")})`;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
var T = (e, t, n, r, i) => {
|
|
79
|
+
let a = f(e), o = "noisePerm" + a.toString(16);
|
|
80
|
+
return w(o + r, [i], [
|
|
81
|
+
["noise:" + o, p(a)],
|
|
82
|
+
["noise:" + r, t],
|
|
83
|
+
["noise:" + o + r, n(o)]
|
|
84
|
+
]);
|
|
85
|
+
}, E = (t, n = 1) => T(n, m, x, "Simplex2", e(t, "vec2f", "simplex2 position")), D = (t, n = 1) => T(n, h, S, "Simplex3", e(t, "vec3f", "simplex3 position")), O = (t, n = 1) => T(n, g, C, "Simplex4", e(t, "vec4f", "simplex4 position")), k = (t) => {
|
|
86
|
+
if (typeof t == "number" && !Number.isSafeInteger(t)) throw RangeError("Noise seed must be a safe integer; pass a whole number.");
|
|
87
|
+
return typeof t == "number" ? e((t | 0) >>> 0, "u32") : t;
|
|
88
|
+
}, A = (t, n = 1) => w("noisePerlin3", [e(t, "vec3f", "perlin3 position"), k(n)], [["noise:hash", _], ["noise:perlin3", b]]), j = (t, n = 1) => w("noiseVoronoi2", [e(t, "vec2f", "voronoi2 position"), k(n)], [["noise:hash", _], ["noise:voronoi2", v]], "vec3f"), M = (t, n = 1) => w("noiseVoronoi3", [e(t, "vec3f", "voronoi3 position"), k(n)], [["noise:hash", _], ["noise:voronoi3", y]], "vec3f"), N = (e, t = 1) => j(e, t).x, P = (e, t = 1) => M(e, t).x, F = a;
|
|
89
|
+
function I(e, t, n) {
|
|
90
|
+
let [r, i, a, o] = F(t), s = 0, c = 1, l = o, u;
|
|
91
|
+
for (let t = 0; t < r; t++) {
|
|
92
|
+
let t = n(e.mul(l)).mul(c);
|
|
93
|
+
u = u ? u.add(t) : t, s += c, c *= a, l *= i;
|
|
94
|
+
}
|
|
95
|
+
return u.div(s);
|
|
96
|
+
}
|
|
97
|
+
var L = (e, t, n = {}) => I(t, n, e), R = (e, t, n = {}) => I(t, n, (t) => {
|
|
98
|
+
let n = e(t).abs().oneMinus();
|
|
99
|
+
return n.mul(n);
|
|
100
|
+
});
|
|
101
|
+
function z(r, a, { warp: s = r, strength: c = 1 } = {}) {
|
|
102
|
+
if (!Number.isFinite(c)) throw RangeError("Domain warp strength must be a finite number; pass a number such as 0.5.");
|
|
103
|
+
let l = (e) => s(a.add(e)).mul(c), u = a.type === "vec2f" ? i(l(o[0]), l(o[1])) : a.type === "vec3f" ? t(l(o[0]), l(o[1]), l(o[2])) : n(l(o[0]), l(o[1]), l(o[2]), l(o[3]));
|
|
104
|
+
return r(e(a.add(u), a.type, "warped position"));
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
export { z as domainWarp, L as fbm, A as perlin3, R as ridged, E as simplex2, D as simplex3, O as simplex4, j as voronoi2, M as voronoi3, N as worley2, P as worley3 };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/own-stack.ts
|
|
2
|
+
async function e(e, t, n) {
|
|
3
|
+
try {
|
|
4
|
+
let { PostStack: r } = await import("@margoengine/post/stack");
|
|
5
|
+
return n?.throwIfAborted(), r.join(e, t);
|
|
6
|
+
} catch (e) {
|
|
7
|
+
throw t.dispose?.(), e;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { e as t };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import type { EnginePlugin } from '@margoengine/core/extension';
|
|
3
|
+
/** What a pass's `attach` keeps of the PostStack it joined. */
|
|
4
|
+
export type OwnStack = {
|
|
5
|
+
removeEffect(effect: EnginePlugin): boolean;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Adds `pass` to the PostStack of the engine's scene, after the last effect of its kind, or to a new PostStack when
|
|
9
|
+
* the scene has none, loading `@margoengine/post` only now. The pass is disposed when it cannot join, so a failed or
|
|
10
|
+
* aborted attach leaves nothing behind.
|
|
11
|
+
*/
|
|
12
|
+
export declare function ownStack(engine: EngineKernel, pass: EnginePlugin, signal?: AbortSignal): Promise<OwnStack>;
|