@margoengine/shader 0.0.0-stage → 1.0.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 +1919 -2
- package/dist/attribute.d.ts +28 -0
- 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-CXGG9qvI.js +360 -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 +200 -0
- package/dist/control-CFjkGPBk.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-DQJk2xAv.js +55 -0
- package/dist/graph.d.ts +82 -0
- package/dist/graph.js +365 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +103 -0
- package/dist/lighting-JU9yQPfM.js +77 -0
- package/dist/lighting.d.ts +62 -0
- package/dist/lighting.js +1 -0
- package/dist/material-C6bpqlnE.js +1184 -0
- package/dist/material-CZ8V1FeA.js +299 -0
- package/dist/material.d.ts +79 -0
- package/dist/node-B6yoIC9V.js +590 -0
- package/dist/node.d.ts +433 -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 +109 -0
- package/dist/pass.js +239 -0
- package/dist/post-DLpZ110L.js +113 -0
- package/dist/post.d.ts +69 -0
- package/dist/preprocess-CWGWlBWQ.js +1195 -0
- package/dist/raw-0AYej3Ak.js +1500 -0
- package/dist/raw.d.ts +744 -0
- package/dist/raw.js +1 -0
- package/dist/readback.d.ts +42 -0
- package/dist/readback.js +85 -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 +159 -0
- package/dist/shared/fog.d.ts +33 -0
- package/dist/shared/fractal.d.ts +9 -0
- package/dist/shared/msaa.d.ts +30 -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-BawKO_h9.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 +149 -4
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { type Declaration, type Program } from './parser.js';
|
|
2
|
+
import type { GLSLHostName } from './host.js';
|
|
3
|
+
import { type GLSLAttributeSemantic, type GLSLBuiltins, type GLSLPreset } from './presets.js';
|
|
4
|
+
export type { GLSLAttributeSemantic, GLSLBuiltins } from './presets.js';
|
|
5
|
+
/** The two stages of a GLSL program. */
|
|
6
|
+
export type GLSLStage = 'vertex' | 'fragment';
|
|
7
|
+
/** What {@link linkGLSLProgram} translates. */
|
|
8
|
+
export interface GLSLProgramSource {
|
|
9
|
+
/** GLSL ES 1.00 or 3.00 with `void main()`. */
|
|
10
|
+
readonly vertexShader: string;
|
|
11
|
+
/** GLSL ES 1.00 or 3.00 with `void main()`. */
|
|
12
|
+
readonly fragmentShader: string;
|
|
13
|
+
/** Object-like macros defined before each stage, as `#define NAME value` lines. Default none. */
|
|
14
|
+
readonly defines?: Readonly<Record<string, string | number | boolean>>;
|
|
15
|
+
/** The version of a stage without `#version`. Default 100 (GLSL ES 1.00), as GL reads such a source. */
|
|
16
|
+
readonly glslVersion?: 100 | 300;
|
|
17
|
+
/** Flip v at every 2D texture lookup and texel fetch, so GL's bottom-up texture rows read Margo's top-down images upright. Default true. */
|
|
18
|
+
readonly flipY?: boolean;
|
|
19
|
+
/** `'gl'`: gl_Position is GL clip space (z from -w to w) and gl_FragCoord counts rows from the bottom. Default `'gl'`. */
|
|
20
|
+
readonly clipSpace?: 'gl' | 'webgpu';
|
|
21
|
+
/** Keep WGSL's derivative uniformity rule. Default false. */
|
|
22
|
+
readonly strict?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* The engine values a program may read under a web convention's names, named by that convention's signature uniform:
|
|
25
|
+
* `'modelViewMatrix'`, `'worldViewProjection'` or `'matrix_model'` (the tables are in the README). `'auto'` picks the
|
|
26
|
+
* one whose names the program uses and refuses a program that uses two; `false` supplies none (declare every
|
|
27
|
+
* uniform and attribute yourself). Default `'auto'`.
|
|
28
|
+
*/
|
|
29
|
+
readonly builtins?: GLSLBuiltins;
|
|
30
|
+
/** Uniform names you set yourself: a built-in name listed here is your uniform, not the engine's. Default none. */
|
|
31
|
+
readonly uniformNames?: readonly string[];
|
|
32
|
+
/** What each attribute holds, by GLSL name (`{aPosition: 'position'}`), for `shaderMesh`. Names of the built-ins map by themselves. */
|
|
33
|
+
readonly attributes?: Readonly<Record<string, GLSLAttributeSemantic>>;
|
|
34
|
+
/** Values an attribute the mesh lacks reads, by GLSL name. Default GL's (0, 0, 0, 1), or the convention's own. */
|
|
35
|
+
readonly defaultAttributeValues?: Readonly<Record<string, readonly number[]>>;
|
|
36
|
+
/** Text of each `#include <name>` (or `"name"`) the stages use; `colorspace_fragment` and `tonemapping_fragment` are built in. */
|
|
37
|
+
readonly includes?: Readonly<Record<string, string>>;
|
|
38
|
+
/**
|
|
39
|
+
* What the fragment stage writes: `'srgb'`, display-ready colour (written as returned on a display target, decoded to
|
|
40
|
+
* linear inside an HDR capture), or `'linear'`, linear light that the scene's display transform then maps. A
|
|
41
|
+
* program with `#include <colorspace_fragment>` writes for its target itself. Default `'srgb'`.
|
|
42
|
+
*/
|
|
43
|
+
readonly outputColorSpace?: 'srgb' | 'linear';
|
|
44
|
+
/**
|
|
45
|
+
* sampler2D uniforms whose texture the device cannot filter (`rgba32float` without `float32-filterable`), with the
|
|
46
|
+
* filter and wrap GL would apply: each binds as an `unfilterable-float` texture, and its `texture()` lookups read
|
|
47
|
+
* texels with `textureLoad` and filter them as OpenGL ES 3.0.6 section 3.8.10 defines (four loads a level). Default none.
|
|
48
|
+
*/
|
|
49
|
+
readonly unfilterable?: Readonly<Record<string, GLSLTexelFilter>>;
|
|
50
|
+
/**
|
|
51
|
+
* The scene's lights as uniforms the program may declare: `sunDirection`, `sunColor`, `ambientLight`, and the lists
|
|
52
|
+
* `pointLights[N]` and `spotLights[N]` (arrays of a struct you declare with any of their fields) with
|
|
53
|
+
* `pointLightCount` and `spotLightCount`. Default false: those names are ordinary uniforms.
|
|
54
|
+
*/
|
|
55
|
+
readonly lights?: boolean;
|
|
56
|
+
/** The scene's fog as uniforms: `fogColor`, `fogNear`, `fogFar`, `fogDensity`, `fogHeight`, `fogHeightFalloff`. Default false. */
|
|
57
|
+
readonly fog?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Motion vectors for TAA from the vertex stage itself: `true` runs it a second time with last frame's world matrix,
|
|
60
|
+
* view-projection and time, so TAA sees where each vertex was. An object also names, per uniform the motion reads,
|
|
61
|
+
* the uniform that holds its value of last frame (`{shift: 'shiftBefore'}`); those are scalars, vectors or
|
|
62
|
+
* matrices, and the second ones need no declaration. Default false.
|
|
63
|
+
*/
|
|
64
|
+
readonly velocity?: boolean | Readonly<Record<string, string>>;
|
|
65
|
+
}
|
|
66
|
+
/** How `unfilterable` filters a texture: `'mipmap'` is GL's LINEAR_MIPMAP_LINEAR, `'linear'` reads level 0. */
|
|
67
|
+
export interface GLSLTexelFilter {
|
|
68
|
+
readonly filter: 'nearest' | 'linear' | 'mipmap';
|
|
69
|
+
readonly wrap: 'clamp' | 'repeat';
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A non-sampler uniform of a linked program and the uniform-block member that holds it. A struct, a block instance or
|
|
73
|
+
* an array of anything but vec3, vec4, mat3, mat4 and their integer vectors is listed one member per leaf, under the
|
|
74
|
+
* name GL gives it (`lights[0].color`), with `root` and `path` saying where its value sits.
|
|
75
|
+
*/
|
|
76
|
+
export interface GLSLProgramUniform {
|
|
77
|
+
readonly name: string;
|
|
78
|
+
/** Its GLSL type, as declared (an array as `vec4[8]`). */
|
|
79
|
+
readonly glsl: string;
|
|
80
|
+
/** The RawShaderMaterial uniform type that stores it (a bool as u32, a mat2 as vec4f, a non-square matrix as mat4x4f). */
|
|
81
|
+
readonly type: string;
|
|
82
|
+
/** The member's name in `uniforms`. */
|
|
83
|
+
readonly member: string;
|
|
84
|
+
/** Elements of an array member (`uniforms.<member>[i]`). */
|
|
85
|
+
readonly count?: number;
|
|
86
|
+
/** For a leaf: the uniform whose value holds it. */
|
|
87
|
+
readonly root?: string;
|
|
88
|
+
/** For a leaf: field names and `[element, numbers per element in a flat list]` steps from the root's value to it. */
|
|
89
|
+
readonly path?: readonly (string | readonly [number, number])[];
|
|
90
|
+
}
|
|
91
|
+
/** A sampler uniform of a linked program and the RawShaderMaterial texture that binds it. */
|
|
92
|
+
export interface GLSLProgramTexture {
|
|
93
|
+
/** The GLSL name; an element of a sampler array is `maps[1]`. */
|
|
94
|
+
readonly name: string;
|
|
95
|
+
readonly glsl: string;
|
|
96
|
+
readonly dimension: '2d' | 'cube' | '3d' | '2d-array';
|
|
97
|
+
/** `'sint'` or `'uint'` for an integer sampler, `'depth'` for a shadow sampler, `'unfilterable-float'` for one in `unfilterable`; absent for a float one. */
|
|
98
|
+
readonly sampleType?: 'sint' | 'uint' | 'depth' | 'unfilterable-float';
|
|
99
|
+
/** For an element of a sampler array: the array's name and the element's index in its value. */
|
|
100
|
+
readonly root?: string;
|
|
101
|
+
readonly index?: number;
|
|
102
|
+
/** The texture's name in WGSL; its sampler is this name plus `Sampler`. */
|
|
103
|
+
readonly binding: string;
|
|
104
|
+
}
|
|
105
|
+
/** A vertex input of a linked program. */
|
|
106
|
+
export interface GLSLProgramAttribute {
|
|
107
|
+
readonly name: string;
|
|
108
|
+
readonly glsl: string;
|
|
109
|
+
/** What it holds when it is a built-in or mapped by `attributes`. */
|
|
110
|
+
readonly semantic?: GLSLAttributeSemantic;
|
|
111
|
+
}
|
|
112
|
+
/** A value passed from the vertex to the fragment stage. */
|
|
113
|
+
export interface GLSLProgramVarying {
|
|
114
|
+
readonly name: string;
|
|
115
|
+
readonly glsl: string;
|
|
116
|
+
readonly location: number;
|
|
117
|
+
readonly interpolation: 'perspective' | 'centroid' | 'flat';
|
|
118
|
+
}
|
|
119
|
+
/** The WGSL of a linked program: each stage follows a RawShaderMaterial's prelude. */
|
|
120
|
+
export interface GLSLProgramWGSL {
|
|
121
|
+
/** The vertex stage, with an entry `margoGLSLVertex` for a mesh whose attributes carry the GLSL names and types. */
|
|
122
|
+
readonly vertex: string;
|
|
123
|
+
/** The fragment stage, with an entry `margoGLSLFragment`. */
|
|
124
|
+
readonly fragment: string;
|
|
125
|
+
readonly uniforms: readonly GLSLProgramUniform[];
|
|
126
|
+
readonly textures: readonly GLSLProgramTexture[];
|
|
127
|
+
readonly attributes: readonly GLSLProgramAttribute[];
|
|
128
|
+
readonly varyings: readonly GLSLProgramVarying[];
|
|
129
|
+
/** The GLSL ES version both stages share. */
|
|
130
|
+
readonly version: 100 | 300;
|
|
131
|
+
/** The convention whose engine values the program reads, or false. */
|
|
132
|
+
readonly builtins: Exclude<GLSLBuiltins, 'auto'>;
|
|
133
|
+
/** The vertex stage writes gl_PointSize, so `shaderMesh` draws a point list as squares of that size. */
|
|
134
|
+
readonly points: boolean;
|
|
135
|
+
/** The uniforms the scene fills (`lights` and `fog` names the program declares and `uniforms` does not set). */
|
|
136
|
+
readonly scene: readonly string[];
|
|
137
|
+
/** The vertex stage also has the entry `margoGLSLVelocity` (`velocity`). */
|
|
138
|
+
readonly velocity: boolean;
|
|
139
|
+
}
|
|
140
|
+
/** One attribute of a drawn mesh: its name and WGSL type (`vec3f`). */
|
|
141
|
+
export interface GLSLMeshAttribute {
|
|
142
|
+
readonly name: string;
|
|
143
|
+
readonly type: string;
|
|
144
|
+
}
|
|
145
|
+
/** @internal A linked program: its WGSL, and the vertex entry for any mesh layout. */
|
|
146
|
+
export interface GLSLLinked extends GLSLProgramWGSL {
|
|
147
|
+
/** The vertex stage for a mesh with `layout` (attribute names as the mesh has them, mapped by `rename`). */
|
|
148
|
+
vertexFor(layout?: readonly GLSLMeshAttribute[], rename?: (glsl: string) => string): string;
|
|
149
|
+
/** The fragment stage writes display-ready colour itself (`outputColorSpace: 'srgb'` without colorspace_fragment). */
|
|
150
|
+
readonly display: boolean;
|
|
151
|
+
}
|
|
152
|
+
/** @internal */
|
|
153
|
+
export declare const uniformTypes: Record<string, string>;
|
|
154
|
+
/** @internal */
|
|
155
|
+
export declare const rect: (t: string) => [number, number] | undefined;
|
|
156
|
+
/** @internal */
|
|
157
|
+
export declare const identifier: RegExp;
|
|
158
|
+
/** Programs linked, for work-count tests: one per distinct source, defines and options. @internal */
|
|
159
|
+
export declare const glslPrograms: {
|
|
160
|
+
count: number;
|
|
161
|
+
};
|
|
162
|
+
/** @internal One parsed stage. */
|
|
163
|
+
export interface StageUnit {
|
|
164
|
+
stage: GLSLStage;
|
|
165
|
+
text: string;
|
|
166
|
+
header: number;
|
|
167
|
+
version: 100 | 300;
|
|
168
|
+
program: Program;
|
|
169
|
+
/** The built-in includes the stage used. */
|
|
170
|
+
included: Set<string>;
|
|
171
|
+
}
|
|
172
|
+
/** @internal */
|
|
173
|
+
export declare function refuse(unit: StageUnit | undefined, message: string, name?: string): never;
|
|
174
|
+
/** @internal */
|
|
175
|
+
export declare const has: (q: string, word: string) => boolean;
|
|
176
|
+
/** @internal */
|
|
177
|
+
export declare const uses: (unit: StageUnit, name: string) => boolean;
|
|
178
|
+
/** @internal */
|
|
179
|
+
export declare function read(u: GLSLProgramUniform): string;
|
|
180
|
+
/**
|
|
181
|
+
* @internal What `lights`, `fog` and `velocity` add to one link, from glsl/scene.ts: only ShaderMaterial of
|
|
182
|
+
* shader/glsl-material imports it, so ShaderPass and Shadertoy carry none of it.
|
|
183
|
+
*/
|
|
184
|
+
export interface GLSLSceneLink {
|
|
185
|
+
/** The vertex stage's extra declarations: last frame's uniforms of `velocity` the program does not declare. */
|
|
186
|
+
declare(vs: StageUnit, fs: StageUnit, own: ReadonlySet<string>, preset: GLSLPreset | undefined): string[];
|
|
187
|
+
/** True when uniform `d` is a scene value (its type checked). */
|
|
188
|
+
uniform(unit: StageUnit, d: Declaration, own: ReadonlySet<string>): boolean;
|
|
189
|
+
/** Under `velocity`: the moving values' private variables and helpers; renames them in `names`. */
|
|
190
|
+
motion(vs: StageUnit, engine: Record<string, GLSLHostName>, uniforms: ReadonlyMap<string, GLSLProgramUniform>, names: Record<string, GLSLHostName>, wgslOf: (unit: StageUnit, t: string) => string, helpers: ReadonlySet<string>): string;
|
|
191
|
+
/** The statements that fill the moving values with this frame's. */
|
|
192
|
+
readonly now: string;
|
|
193
|
+
readonly velocity: boolean;
|
|
194
|
+
/** The velocity entry, from the vertex entry's parameters, attribute statements, copies, main and point code. */
|
|
195
|
+
entry(params: string, lines: string, copy: string, main: string, gl: boolean, sprite: string, vs: StageUnit, names: Record<string, GLSLHostName>): string;
|
|
196
|
+
}
|
|
197
|
+
/** @internal Starts a link's scene part, or returns undefined when `source` sets no scene option. */
|
|
198
|
+
export type GLSLSceneLinker = (source: GLSLProgramSource) => GLSLSceneLink | undefined;
|
|
199
|
+
/**
|
|
200
|
+
* @internal Links a GLSL program: parses both stages, joins their uniforms, links varyings by name (locations in vertex
|
|
201
|
+
* declaration order) and translates each stage with `main` called from a generated entry. Cached per source and options.
|
|
202
|
+
*/
|
|
203
|
+
export declare function linkGLSLProgram(source: GLSLProgramSource, sceneLinker?: GLSLSceneLinker): GLSLLinked;
|
|
204
|
+
/**
|
|
205
|
+
* Translates a whole GLSL program (GLSL ES 1.00 or 3.00, a vertex and a fragment stage with `void main()`) to the two
|
|
206
|
+
* WGSL stages a `RawShaderMaterial` of `@margoengine/shader/raw` draws, with the uniforms, textures, attributes and
|
|
207
|
+
* varyings it declares. `ShaderMaterial` does this for you; this shows the WGSL.
|
|
208
|
+
* @throws MargoError `failed` with `stage`, `line` and `column` for a syntax or link error; TypeError for bad options.
|
|
209
|
+
*/
|
|
210
|
+
export declare function glslProgramToWGSL(source: GLSLProgramSource, sceneLinker?: GLSLSceneLinker): GLSLProgramWGSL;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type GLSLProgramSource, type GLSLSceneLink } from './program.js';
|
|
2
|
+
/** @internal Plain scene uniforms by name, with their GLSL types. */
|
|
3
|
+
export declare const lightUniforms: Readonly<Record<string, string>>;
|
|
4
|
+
/** @internal The light lists: the fields a program's struct may hold, with their GLSL types. */
|
|
5
|
+
export declare const lightLists: Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
6
|
+
/** @internal Fog uniforms by name, with their GLSL types. */
|
|
7
|
+
export declare const fogUniforms: Readonly<Record<string, string>>;
|
|
8
|
+
/** @internal The scene part of one link (see GLSLSceneLinker in program.ts), or undefined when no option is set. */
|
|
9
|
+
export declare function sceneLink(source: GLSLProgramSource): GLSLSceneLink | undefined;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { RawShaders } from './raw.js';
|
|
2
|
+
import { type FogLike } from './shared/fog.js';
|
|
3
|
+
import { type GLSLProgramSource, type GLSLProgramWGSL } from './glsl/program.js';
|
|
4
|
+
import { GLSLMaterial, type ShaderMaterialOptions } from './glsl/material.js';
|
|
5
|
+
export { shaderMesh } from './glsl/material.js';
|
|
6
|
+
export type { ShaderUniformValue, ShaderUniform, ShaderSide, ShaderDepthFunc, ShaderMaterialOptions, ShaderGeometry, ShaderMeshOptions, } from './glsl/material.js';
|
|
7
|
+
/** Programs linked so far, for work-count tests. @internal */
|
|
8
|
+
export { glslPrograms } from './glsl/program.js';
|
|
9
|
+
export type { GLSLProgramSource, GLSLProgramWGSL, GLSLProgramUniform, GLSLProgramTexture, GLSLProgramAttribute, GLSLProgramVarying, GLSLStage, GLSLAttributeSemantic, GLSLBuiltins, } from './glsl/program.js';
|
|
10
|
+
export type { FogLike } from './shared/fog.js';
|
|
11
|
+
/** A point light for {@link ShaderScene.setLights}, as `@margoengine/clustered` takes it. */
|
|
12
|
+
export interface ShaderPointLight {
|
|
13
|
+
readonly position: ArrayLike<number>;
|
|
14
|
+
/** Where the light ends; 0 is unlimited. */
|
|
15
|
+
readonly range: number;
|
|
16
|
+
/** Linear RGB. Default 1, 1, 1. */
|
|
17
|
+
readonly color?: ArrayLike<number>;
|
|
18
|
+
/** Default 1. */
|
|
19
|
+
readonly intensity?: number;
|
|
20
|
+
/** Distance falloff exponent. Default 2. */
|
|
21
|
+
readonly decay?: number;
|
|
22
|
+
}
|
|
23
|
+
/** A spot light for {@link ShaderScene.setLights}: a point light with a direction and a cone. */
|
|
24
|
+
export interface ShaderSpotLight extends ShaderPointLight {
|
|
25
|
+
readonly direction: ArrayLike<number>;
|
|
26
|
+
readonly outerAngle?: number;
|
|
27
|
+
readonly innerAngle?: number;
|
|
28
|
+
readonly angle?: number;
|
|
29
|
+
readonly penumbra?: number;
|
|
30
|
+
}
|
|
31
|
+
/** What {@link ShaderScene.setLights} takes: a `Clustered` fits. */
|
|
32
|
+
export interface ShaderLightSource {
|
|
33
|
+
readonly list: readonly (ShaderPointLight | ShaderSpotLight)[];
|
|
34
|
+
readonly intensityScale?: number | (() => number);
|
|
35
|
+
}
|
|
36
|
+
/** The fog and the point and spot lights that ShaderMaterials drawn by one RawShaders read: see {@link shaderScene}. */
|
|
37
|
+
export interface ShaderScene {
|
|
38
|
+
/** The fog materials with `fog: true` read, or null (no fog). */
|
|
39
|
+
readonly fog: FogLike | null;
|
|
40
|
+
/** The point and spot lights materials with `lights: true` read, or null (none). */
|
|
41
|
+
readonly lights: ShaderLightSource | null;
|
|
42
|
+
/**
|
|
43
|
+
* Sets the fog: `@margoengine/environment`'s `Fog` or any object with its fields, read every frame; null for none.
|
|
44
|
+
* @throws TypeError when `fog` is neither an object nor null; RangeError or TypeError for an invalid field.
|
|
45
|
+
*/
|
|
46
|
+
setFog(fog: FogLike | null): this;
|
|
47
|
+
/**
|
|
48
|
+
* Sets the point and spot lights: a `Clustered` of `@margoengine/clustered`, or any object with its `list` (and
|
|
49
|
+
* optional `intensityScale`), read every frame; null for none. The sun and ambient light are always `engine.lighting`'s.
|
|
50
|
+
* @throws TypeError when `lights` has no `list` array.
|
|
51
|
+
*/
|
|
52
|
+
setLights(lights: ShaderLightSource | null): this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The scene that ShaderMaterials made with `fog: true` or `lights: true` read when `raw` draws them:
|
|
56
|
+
* `shaderScene(raw).setFog(fog).setLights(clustered)`. One per RawShaders; it starts with no fog and no lights.
|
|
57
|
+
* @throws TypeError when `raw` is not a RawShaders.
|
|
58
|
+
*/
|
|
59
|
+
export declare function shaderScene(raw: RawShaders): ShaderScene;
|
|
60
|
+
/**
|
|
61
|
+
* A material from a whole GLSL program, as a web engine's `RawShaderMaterial` takes one: GLSL ES 1.00 or 3.00 vertex and
|
|
62
|
+
* fragment stages with `void main()`, live `uniforms` by GLSL name, `defines` and the render state of
|
|
63
|
+
* RawShaderMaterial. Draw it with a RawMesh in a RawShaders plugin; attributes match by name. The GLSL is translated
|
|
64
|
+
* here, once per source and defines (syntax and link errors throw here, with `stage`, `line` and `column`), and
|
|
65
|
+
* `material.code(stage, mesh)` shows the WGSL each stage becomes. `lights`, `fog` and `velocity` read the scene
|
|
66
|
+
* (see {@link shaderScene}) and write motion vectors for TAA.
|
|
67
|
+
*/
|
|
68
|
+
export declare class ShaderMaterial extends GLSLMaterial {
|
|
69
|
+
constructor(options: ShaderMaterialOptions);
|
|
70
|
+
/**
|
|
71
|
+
* True for every material made from a GLSL program, including those ShaderPass and Shadertoy make, which are the
|
|
72
|
+
* same program core without the scene options (so those entries carry no scene code). A subclass checks as usual.
|
|
73
|
+
*/
|
|
74
|
+
static [Symbol.hasInstance](value: unknown): boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Translates a whole GLSL program (GLSL ES 1.00 or 3.00, a vertex and a fragment stage with `void main()`) to the two
|
|
78
|
+
* WGSL stages a `RawShaderMaterial` of `@margoengine/shader/raw` draws, with the uniforms, textures, attributes and
|
|
79
|
+
* varyings it declares. `ShaderMaterial` does this for you; this shows the WGSL.
|
|
80
|
+
* @throws MargoError `failed` with `stage`, `line` and `column` for a syntax or link error; TypeError for bad options.
|
|
81
|
+
*/
|
|
82
|
+
export declare function glslProgramToWGSL(source: GLSLProgramSource): GLSLProgramWGSL;
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { a as e, c as t, d as n, f as r, i, l as a, n as o, o as s, p as c, r as l, s as u, t as d, u as f } from "./material-C6bpqlnE.js";
|
|
2
|
+
import { LinearToSRGB as p, parseColor as m } from "@margoengine/math/color";
|
|
3
|
+
//#region ../shared/fog.js
|
|
4
|
+
var h = (e, t, n, r) => {
|
|
5
|
+
let i = e === void 0 ? t : e;
|
|
6
|
+
if (typeof i != "number" || !Number.isFinite(i) || i < r) throw RangeError(`Fog ${n} is ${String(i)}; pass a finite number${r === 0 ? ", 0 or more" : ""}`);
|
|
7
|
+
return i;
|
|
8
|
+
};
|
|
9
|
+
function g(e) {
|
|
10
|
+
if (e !== null && (typeof e != "object" || Array.isArray(e))) throw TypeError("Fog must be an object with Fog fields; pass an environment Fog, such an object or null");
|
|
11
|
+
return e && _(e, [
|
|
12
|
+
0,
|
|
13
|
+
0,
|
|
14
|
+
0
|
|
15
|
+
], /* @__PURE__ */ new Float32Array(12)), e;
|
|
16
|
+
}
|
|
17
|
+
function _(e, t, n, r = !1) {
|
|
18
|
+
let i = e.enabled ?? !0;
|
|
19
|
+
if (typeof i != "boolean") throw TypeError(`Fog enabled is ${String(i)}; pass true or false`);
|
|
20
|
+
let a = h(e.height, 0, "height", -Infinity), o = h(e.near, 1, "near", 0), s = h(e.far, 1e3, "far", 0), c = h(e.density, 0, "density", 0), l = h(e.heightFalloff, 0, "heightFalloff", 0);
|
|
21
|
+
if (m(e.color ?? "#ffffff", n, 0), r) for (let e = 0; e < 3; e++) n[e] = p(n[e]);
|
|
22
|
+
n[3] = a, n[4] = o, n[5] = s, n[6] = c, n[7] = l, i || (n.fill(0, 0, 8), n[4] = n[5] = 34028234663852886e22), n[8] = t[0], n[9] = t[1], n[10] = t[2], n[11] = 0;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/glsl/scene.ts
|
|
26
|
+
var v = {
|
|
27
|
+
sunDirection: "vec3",
|
|
28
|
+
sunColor: "vec3",
|
|
29
|
+
ambientLight: "vec3",
|
|
30
|
+
pointLightCount: "int",
|
|
31
|
+
spotLightCount: "int"
|
|
32
|
+
}, y = {
|
|
33
|
+
pointLights: {
|
|
34
|
+
position: "vec3",
|
|
35
|
+
color: "vec3",
|
|
36
|
+
range: "float",
|
|
37
|
+
decay: "float"
|
|
38
|
+
},
|
|
39
|
+
spotLights: {
|
|
40
|
+
position: "vec3",
|
|
41
|
+
direction: "vec3",
|
|
42
|
+
color: "vec3",
|
|
43
|
+
range: "float",
|
|
44
|
+
decay: "float",
|
|
45
|
+
outerCos: "float",
|
|
46
|
+
innerCos: "float"
|
|
47
|
+
}
|
|
48
|
+
}, b = {
|
|
49
|
+
fogColor: "vec3",
|
|
50
|
+
fogNear: "float",
|
|
51
|
+
fogFar: "float",
|
|
52
|
+
fogDensity: "float",
|
|
53
|
+
fogHeight: "float",
|
|
54
|
+
fogHeightFalloff: "float"
|
|
55
|
+
};
|
|
56
|
+
function x(o) {
|
|
57
|
+
let { lights: s = !1, fog: l = !1, velocity: d = !1 } = o;
|
|
58
|
+
if (typeof s != "boolean" || typeof l != "boolean") throw TypeError("ShaderMaterial lights and fog must be booleans");
|
|
59
|
+
if (typeof d != "boolean" && (!d || typeof d != "object" || Array.isArray(d))) throw TypeError("ShaderMaterial velocity must be true, false or {uniform: 'uniformOfLastFrame'}");
|
|
60
|
+
if (!s && !l && !d) return;
|
|
61
|
+
let p = d && typeof d == "object" ? { ...d } : {};
|
|
62
|
+
for (let [t, n] of Object.entries(p)) if (!e.test(t) || typeof n != "string" || !e.test(n) || t === n) throw TypeError(`ShaderMaterial velocity.${t} must name another uniform, the one holding ${t}'s value of last frame`);
|
|
63
|
+
let m = [], h = (e) => m.map((t) => `margoNow_${t.name}=${e ? t.before : t.now};\n`).join("");
|
|
64
|
+
return {
|
|
65
|
+
velocity: !!d,
|
|
66
|
+
get now() {
|
|
67
|
+
return h(!1);
|
|
68
|
+
},
|
|
69
|
+
declare(e, n, r, o) {
|
|
70
|
+
let s = [];
|
|
71
|
+
for (let [c, l] of Object.entries(p)) {
|
|
72
|
+
let u = (t) => [e, n].flatMap((e) => e.program.globals).find((e) => e.name === t && i(e.qualifier, "uniform"));
|
|
73
|
+
!r.has(c) && o?.uniforms[c] && a(e, `velocity.${c}: the engine's matrices follow by themselves; name your own uniforms`, c);
|
|
74
|
+
let d = (u(c) ?? a(e, `velocity names uniform ${c}, which the program does not declare`)).type;
|
|
75
|
+
f[d] || t(d) || a(e, `velocity.${c}: the motion's uniforms are your own scalars, vectors or matrices; the engine's matrices follow by themselves`, c);
|
|
76
|
+
let p = u(l);
|
|
77
|
+
p && p.type !== d && a(e, `velocity.${c} names ${l}, a ${p.type}; give it the type of ${c} (${d})`, l), p || s.push(`uniform ${d} ${l};`);
|
|
78
|
+
}
|
|
79
|
+
return s;
|
|
80
|
+
},
|
|
81
|
+
uniform(e, t, n) {
|
|
82
|
+
if (n.has(t.name)) return !1;
|
|
83
|
+
let r = s && v[t.name] || l && b[t.name] || void 0, i = s ? y[t.name] : void 0;
|
|
84
|
+
if (r) return t.type !== r && a(e, `uniform ${t.name} is the scene's ${r} under ${b[t.name] ? "fog" : "lights"}; declare it ${r}, or set it yourself in uniforms`, t.name), !0;
|
|
85
|
+
if (!i) return !1;
|
|
86
|
+
let [, o] = /^(\w+)\[\d+\]$/.exec(t.type) ?? [], c = o ? e.program.structs.get(o) : void 0;
|
|
87
|
+
c || a(e, `uniform ${t.name} is the scene's light list under lights; declare it an array of a struct of ${Object.keys(i).join(", ")}`, t.name);
|
|
88
|
+
for (let n of c) i[n.name] !== n.type && a(e, `${t.name} field ${n.name} ${i[n.name] ? `must be ${i[n.name]}` : `is not a light field; use ${Object.keys(i).join(", ")}`}`, n.name);
|
|
89
|
+
return !0;
|
|
90
|
+
},
|
|
91
|
+
motion(e, t, i, a, o, s) {
|
|
92
|
+
if (!d) return "";
|
|
93
|
+
let l = /* @__PURE__ */ new Set(), f = (e, t) => e === "isOrthographic" || e === "camera_params" ? t : t.replaceAll("margoGLProjection()", "margoGLProjectionBefore()").replaceAll("mesh.world", "margoBefore.world").replaceAll("mesh.normal", "margoBeforeNormal()").replaceAll("camera.viewProjection", "margoBefore.viewProjection").replaceAll("camera.projection", "margoBeforeProjection()").replaceAll("camera.time", "margoBefore.time");
|
|
94
|
+
for (let [r, i] of Object.entries(t)) n(e, r) && m.push({
|
|
95
|
+
name: r,
|
|
96
|
+
type: i.type,
|
|
97
|
+
now: i.code,
|
|
98
|
+
before: f(r, i.code)
|
|
99
|
+
});
|
|
100
|
+
for (let [e, t] of Object.entries(p)) {
|
|
101
|
+
let n = i.get(e);
|
|
102
|
+
m.push({
|
|
103
|
+
name: e,
|
|
104
|
+
type: n.glsl,
|
|
105
|
+
now: u(n),
|
|
106
|
+
before: u(i.get(t))
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
for (let e of m) {
|
|
110
|
+
a[e.name] = {
|
|
111
|
+
code: "margoNow_" + e.name,
|
|
112
|
+
type: e.type
|
|
113
|
+
};
|
|
114
|
+
for (let t of [
|
|
115
|
+
"margoBeforeNormal",
|
|
116
|
+
"margoBeforeProjection",
|
|
117
|
+
"margoGLProjectionBefore"
|
|
118
|
+
]) e.before.includes(t) && l.add(t);
|
|
119
|
+
}
|
|
120
|
+
return l.has("margoGLProjectionBefore") && l.add("margoBeforeProjection"), m.map((t) => `var<private> margoNow_${t.name}:${o(e, t.type)};\n`).join("") + (l.has("margoBeforeNormal") ? "fn margoBeforeNormal()->mat3x3f{let w=margoBefore.world;let a=w[0].xyz;let b=w[1].xyz;let c=w[2].xyz;let x=mat3x3f(cross(b,c),cross(c,a),cross(a,b));let d=dot(a,x[0]);return x*select(0.0,1.0/d,d!=0.0);}\n" : "") + (l.has("margoBeforeProjection") ? (s.has("fn margoViewInverse()->mat4x4f{let r=transpose(mat3x3f(camera.view[0].xyz,camera.view[1].xyz,camera.view[2].xyz));return mat4x4f(vec4f(r[0],0.0),vec4f(r[1],0.0),vec4f(r[2],0.0),vec4f(camera.position,1.0));}\n") ? "" : c) + "fn margoBeforeProjection()->mat4x4f{return margoBefore.viewProjection*margoViewInverse();}\n" : "") + (l.has("margoGLProjectionBefore") ? r.replace("margoGLProjection", "margoGLProjectionBefore").replace("camera.projection", "margoBeforeProjection()") : "");
|
|
121
|
+
},
|
|
122
|
+
entry(e, t, n, r, i, a, o, s) {
|
|
123
|
+
let c = [
|
|
124
|
+
"glsl_Position",
|
|
125
|
+
"glsl_PointSize",
|
|
126
|
+
...o.program.globals.filter((e) => !/\b(uniform|attribute|in|out|varying|const)\b/.test(e.qualifier) && !Object.hasOwn(s, e.name)).map((e) => "user_" + e.name)
|
|
127
|
+
], l = (e) => `var ${e}=glsl_Position;${i ? `\n${e}.z=(${e}.w-${e}.z)*0.5;` : ""}\n${a ? `{${a.replaceAll("o.clip", e)}}\n` : ""}`;
|
|
128
|
+
return `@vertex fn margoGLSLVelocity(${e})->MargoVelocity{
|
|
129
|
+
${t}
|
|
130
|
+
${h(!1)}${n}${c.map((e) => `let margoKept_${e}=${e};\n`).join("")}${r}();
|
|
131
|
+
${l("margoClip")}${c.map((e) => `${e}=margoKept_${e};\n`).join("")}${h(!0)}${r}();
|
|
132
|
+
${l("margoPrevious")}return MargoVelocity(margoClip,margoPrevious,margoClip);
|
|
133
|
+
}
|
|
134
|
+
`;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/glsl-material.ts
|
|
140
|
+
var S = /* @__PURE__ */ new WeakMap();
|
|
141
|
+
function C(e) {
|
|
142
|
+
if (!e || typeof e != "object" || typeof e.add != "function") throw TypeError("shaderScene needs the RawShaders that draws the materials");
|
|
143
|
+
let t = S.get(e);
|
|
144
|
+
if (!t) {
|
|
145
|
+
let n = null, r = null;
|
|
146
|
+
t = {
|
|
147
|
+
get fog() {
|
|
148
|
+
return n;
|
|
149
|
+
},
|
|
150
|
+
get lights() {
|
|
151
|
+
return r;
|
|
152
|
+
},
|
|
153
|
+
setFog(e) {
|
|
154
|
+
return n = g(e), this;
|
|
155
|
+
},
|
|
156
|
+
setLights(e) {
|
|
157
|
+
if (e !== null && (typeof e != "object" || !Array.isArray(e.list))) throw TypeError("ShaderScene.setLights needs a Clustered, an object with a list of point and spot lights, or null");
|
|
158
|
+
return r = e, this;
|
|
159
|
+
}
|
|
160
|
+
}, S.set(e, t);
|
|
161
|
+
}
|
|
162
|
+
return t;
|
|
163
|
+
}
|
|
164
|
+
var w = /* @__PURE__ */ new WeakSet(), T = { enabled: !1 };
|
|
165
|
+
function E(e, t) {
|
|
166
|
+
let n = t.material?.drawnBy, r = n?.pass ?? -1;
|
|
167
|
+
if (t.cache && t.key[0] === n && t.key[1] === r) return t.cache;
|
|
168
|
+
t.key = [n, r];
|
|
169
|
+
let i = n?.kernel?.lighting, a = n ? S.get(n) : void 0, o = {}, s = i?.direction ?? [
|
|
170
|
+
0,
|
|
171
|
+
1,
|
|
172
|
+
0,
|
|
173
|
+
0
|
|
174
|
+
], c = i?.radiance ?? [
|
|
175
|
+
0,
|
|
176
|
+
0,
|
|
177
|
+
0,
|
|
178
|
+
0
|
|
179
|
+
], l = i?.ambient ?? [
|
|
180
|
+
1,
|
|
181
|
+
1,
|
|
182
|
+
1
|
|
183
|
+
], u = Math.hypot(s[0], s[1], s[2]) || 1;
|
|
184
|
+
o.sunDirection = [
|
|
185
|
+
s[0] / u,
|
|
186
|
+
s[1] / u,
|
|
187
|
+
s[2] / u
|
|
188
|
+
], o.sunColor = [
|
|
189
|
+
c[0] * s[3],
|
|
190
|
+
c[1] * s[3],
|
|
191
|
+
c[2] * s[3]
|
|
192
|
+
], o.ambientLight = [
|
|
193
|
+
l[0] * c[3],
|
|
194
|
+
l[1] * c[3],
|
|
195
|
+
l[2] * c[3]
|
|
196
|
+
];
|
|
197
|
+
let d = /* @__PURE__ */ new Float32Array(12);
|
|
198
|
+
_(a?.fog ?? T, [
|
|
199
|
+
0,
|
|
200
|
+
0,
|
|
201
|
+
0
|
|
202
|
+
], d, e.display), Object.assign(o, {
|
|
203
|
+
fogColor: [
|
|
204
|
+
d[0],
|
|
205
|
+
d[1],
|
|
206
|
+
d[2]
|
|
207
|
+
],
|
|
208
|
+
fogHeight: d[3],
|
|
209
|
+
fogNear: d[4],
|
|
210
|
+
fogFar: d[5],
|
|
211
|
+
fogDensity: d[6],
|
|
212
|
+
fogHeightFalloff: d[7]
|
|
213
|
+
});
|
|
214
|
+
let f = a?.lights, p = typeof f?.intensityScale == "function" ? f.intensityScale() : f?.intensityScale ?? 1, m = [], h = [], g = (t) => {
|
|
215
|
+
let n = 0;
|
|
216
|
+
for (let r of e.uniforms) r.root === t && (n = Math.max(n, (r.path?.[0])[0] + 1));
|
|
217
|
+
return e.scene.includes(t) ? n : Infinity;
|
|
218
|
+
}, v = g("pointLights"), y = g("spotLights");
|
|
219
|
+
for (let e of f?.list ?? []) {
|
|
220
|
+
let t = e.color ?? [
|
|
221
|
+
1,
|
|
222
|
+
1,
|
|
223
|
+
1
|
|
224
|
+
], n = (e.intensity ?? 1) * p, r = {
|
|
225
|
+
position: Array.from(e.position),
|
|
226
|
+
color: [
|
|
227
|
+
t[0] * n,
|
|
228
|
+
t[1] * n,
|
|
229
|
+
t[2] * n
|
|
230
|
+
],
|
|
231
|
+
range: e.range,
|
|
232
|
+
decay: e.decay ?? 2
|
|
233
|
+
};
|
|
234
|
+
if (!("direction" in e)) {
|
|
235
|
+
m.length < v && m.push(r);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (h.length >= y) continue;
|
|
239
|
+
let i = e, a = Math.hypot(i.direction[0], i.direction[1], i.direction[2]) || 1, o = i.angle ?? i.outerAngle ?? Math.PI / 2, s = i.angle === void 0 ? i.innerAngle ?? 0 : i.angle * (1 - (i.penumbra ?? 0));
|
|
240
|
+
h.push({
|
|
241
|
+
...r,
|
|
242
|
+
direction: [
|
|
243
|
+
i.direction[0] / a,
|
|
244
|
+
i.direction[1] / a,
|
|
245
|
+
i.direction[2] / a
|
|
246
|
+
],
|
|
247
|
+
outerCos: Math.cos(o),
|
|
248
|
+
innerCos: Math.cos(s)
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return Object.assign(o, {
|
|
252
|
+
pointLights: m,
|
|
253
|
+
spotLights: h,
|
|
254
|
+
pointLightCount: m.length,
|
|
255
|
+
spotLightCount: h.length
|
|
256
|
+
}), t.cache = o;
|
|
257
|
+
}
|
|
258
|
+
var D = {
|
|
259
|
+
link: x,
|
|
260
|
+
uniforms(e, t, n) {
|
|
261
|
+
for (let e of Object.keys(t)) w.has(t[e]) && delete t[e];
|
|
262
|
+
for (let r of e.scene) {
|
|
263
|
+
if (t[r]) continue;
|
|
264
|
+
let i = {
|
|
265
|
+
get value() {
|
|
266
|
+
return E(e, n)[r] ?? null;
|
|
267
|
+
},
|
|
268
|
+
set value(e) {
|
|
269
|
+
throw TypeError(`ShaderMaterial uniform ${r} comes from the scene; give it in uniforms to set it yourself`);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
w.add(i), t[r] = i;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}, O = class e extends d {
|
|
276
|
+
constructor(e) {
|
|
277
|
+
super(e, D);
|
|
278
|
+
}
|
|
279
|
+
static [Symbol.hasInstance](t) {
|
|
280
|
+
return this === e ? t instanceof d : Function.prototype[Symbol.hasInstance].call(this, t);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
function k(e) {
|
|
284
|
+
let { vertexFor: t, display: n, ...r } = s(e, x);
|
|
285
|
+
return r;
|
|
286
|
+
}
|
|
287
|
+
//#endregion
|
|
288
|
+
export { O as ShaderMaterial, k as glslProgramToWGSL, l as glslPrograms, o as shaderMesh, C as shaderScene };
|