@margoengine/creative 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.
Files changed (47) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +310 -2
  4. package/dist/display-C6RC-F2-.js +3 -0
  5. package/dist/errors-XvcwUnU4.js +7 -0
  6. package/dist/gpu-DaSofAeP.js +30 -0
  7. package/dist/grass-OkjgCaF7.js +450 -0
  8. package/dist/grass-settings.d.ts +63 -0
  9. package/dist/grass-settings.js +131 -0
  10. package/dist/grass-terrain.d.ts +33 -0
  11. package/dist/grass-terrain.js +185 -0
  12. package/dist/grass.d.ts +106 -0
  13. package/dist/grass.js +1 -0
  14. package/dist/hdr-format-DXpjc3bW.js +4 -0
  15. package/dist/index.d.ts +7 -0
  16. package/dist/index.js +8 -0
  17. package/dist/msaa-8hWFieoE.js +220 -0
  18. package/dist/paint-aniso.d.ts +36 -0
  19. package/dist/paint-aniso.js +217 -0
  20. package/dist/paint.d.ts +56 -0
  21. package/dist/paint.js +167 -0
  22. package/dist/particles.d.ts +38 -0
  23. package/dist/particles.js +79 -0
  24. package/dist/pass-B5mPip-t.js +27 -0
  25. package/dist/planet-BYTix5S1.js +505 -0
  26. package/dist/planet-atmosphere.d.ts +37 -0
  27. package/dist/planet-atmosphere.js +196 -0
  28. package/dist/planet-pass.d.ts +47 -0
  29. package/dist/planet.d.ts +76 -0
  30. package/dist/planet.js +1 -0
  31. package/dist/post.d.ts +108 -0
  32. package/dist/post.js +265 -0
  33. package/dist/quality-DFm-cQWC.js +7 -0
  34. package/dist/shared/settings.d.ts +89 -0
  35. package/dist/stars-C0SH8-aJ.js +90 -0
  36. package/dist/stars.d.ts +34 -0
  37. package/dist/stars.js +1 -0
  38. package/dist/targets-depth.d.ts +35 -0
  39. package/dist/targets-depth.js +169 -0
  40. package/dist/targets.d.ts +35 -0
  41. package/dist/targets.js +42 -0
  42. package/dist/texture-target-CGqE9pOa.js +131 -0
  43. package/dist/texture-target.d.ts +49 -0
  44. package/dist/toon.d.ts +32 -0
  45. package/dist/toon.js +58 -0
  46. package/dist/view-rect-TO68f_UR.js +48 -0
  47. package/package.json +124 -4
package/dist/post.js ADDED
@@ -0,0 +1,265 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { r as e, t } from "./view-rect-TO68f_UR.js";
5
+ import { HDRRenderTarget as n } from "./targets.js";
6
+ import { n as r, t as i } from "./display-C6RC-F2-.js";
7
+ import { MargoError as a } from "@margoengine/math/errors";
8
+ import { invertMatrixStable as o } from "@margoengine/math/mat4";
9
+ //#region src/post.ts
10
+ var s = 288, c = {}, l = [
11
+ .3,
12
+ .6,
13
+ -.7
14
+ ], u = [
15
+ .45,
16
+ .62,
17
+ .8
18
+ ], d = [
19
+ 1,
20
+ 1,
21
+ 1
22
+ ], f = [
23
+ 0,
24
+ 0,
25
+ 0
26
+ ], p = (e) => Number.isFinite(Math.fround(e)), m = (e, t, n) => p(e) && e >= t && e <= n;
27
+ function h(e, t, n) {
28
+ for (let r = 0; r < e.length; r++) if (r in e && (e[r] < t || e[r] > n)) return !1;
29
+ return !0;
30
+ }
31
+ var g = (e) => e.length === 3 && e.every(p), _ = "struct Out {@builtin(position) clip:vec4f,@location(0) uv:vec2f}\n@vertex fn vertex(@builtin(vertex_index) i:u32)->Out{let p=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));var o:Out;o.clip=vec4f(p[i],0,1);o.uv=p[i]*vec2f(0.5,-0.5)+0.5;return o;}", v = `struct Frame {inverse:mat4x4f,eye:vec4f,sun:vec4f,fog:vec4f,color:vec4f,grade:vec4f,lens:vec4f,settings:vec4f,gain:vec4f,lift:vec4f,camera:mat4x4f,rect:vec4f}
32
+ @group(0) @binding(0) var<uniform> f:Frame;
33
+ fn world(uv:vec2f,depth:f32)->vec3f{let p=f.inverse*vec4f(uv*vec2f(2,-2)+vec2f(-1,1),depth,1);return p.xyz/p.w;}
34
+ @group(0) @binding(1) var scene:texture_2d<f32>;
35
+ @group(0) @binding(2) var depth:texture_depth_2d;
36
+ @group(0) @binding(4) var output:texture_storage_2d<rgba8unorm,write>;
37
+ ${i}
38
+
39
+
40
+ fn grainPcg(v:u32)->u32 {let s=v*747796405u+2891336453u;let w=((s>>((s>>28u)+4u))^s)*277803737u;return (w>>22u)^w;}
41
+ @compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u){let size=vec2i(f.rect.zw);if(any(vec2i(id.xy)>=size)){return;}let local=vec2i(id.xy);let origin=vec2i(f.rect.xy);let p=origin+local;let uv=(vec2f(id.xy)+0.5)/vec2f(size);let d=textureLoad(depth,p,0);let w=world(uv,d);let distance=length(w-f.eye.xyz);var c=textureLoad(scene,p,0).xyz;
42
+ let radius=min(8.0,abs(distance-f.lens.x)/max(distance,0.001)*f.lens.y);
43
+ if(radius>0.5){var blurred=vec3f(0);var weights=0.0;for(var y=-2;y<=2;y++){for(var x=-2;x<=2;x++){let q=clamp(local+vec2i(round(vec2f(f32(x),f32(y))*radius*0.5)),vec2i(0),size-1);let neighbor=length(world((vec2f(q)+0.5)/vec2f(size),textureLoad(depth,origin+q,0))-f.eye.xyz);let weight=exp(-f32(x*x+y*y)*0.25)*select(0.0,1.0,neighbor>=distance-radius);blurred+=textureLoad(scene,origin+q,0).xyz*weight;weights+=weight;}}c=blurred/max(weights,0.001);}
44
+ let density=f.fog.x*exp(clamp(-f.eye.y*f.fog.y,-20.0,20.0));let dy=(w.y-f.eye.y)*f.fog.y;let integral=select((1-exp(clamp(-dy,-20.0,20.0)))/select(dy,1.0,abs(dy)<0.001),1.0,abs(dy)<0.001);let visibility=exp(-max(0.0,density*distance*integral));c=mix(f.color.xyz,c,visibility);
45
+ let sunWorld=f.eye.xyz+f.sun.xyz*f.settings.y;let ray=normalize(sunWorld-f.eye.xyz);let farRay=normalize(w-f.eye.xyz);let alignment=pow(max(dot(ray,farRay),0.0),16.0);var shafts=0.0;
46
+ let sunClip=f.camera*vec4f(sunWorld,1);if(sunClip.w>0&&f.fog.z>0){let sunUV=sunClip.xy/sunClip.w*vec2f(0.5,-0.5)+0.5;for(var i=1;i<=16;i++){let q=clamp(vec2i(mix(uv,sunUV,f32(i)*0.05)*vec2f(size)),vec2i(0),size-1);shafts+=select(0.0,1.0,textureLoad(depth,origin+q,0)<0.000001)*pow(0.95,f32(i))/16.0;}}c+=vec3f(1,0.8,0.5)*shafts*alignment*f.fog.z;
47
+ c=max(vec3f(0),c*exp2(f.grade.x)*f.gain.xyz+f.lift.xyz);let luminance=dot(c,vec3f(0.2126,0.7152,0.0722));c=mix(vec3f(luminance),c,f.grade.z);c=max(c,vec3f(0));if(f.grade.y==0.0){c=vec3f(0.18);}else if(f.grade.y!=1.0){c=0.18*pow(c/0.18,vec3f(f.grade.y));}
48
+ c=toDisplay(c);let noise=f32(grainPcg(id.x+grainPcg(id.y+grainPcg(bitcast<u32>(f.lens.w))))>>8u)*5.9604645e-8-0.5;textureStore(output,p,vec4f(clamp(c+noise*f.lens.z,vec3f(0),vec3f(1)),1));}`, y = _ + "@group(0) @binding(0) var image:texture_2d<f32>;@fragment fn fragment(i:Out)->@location(0) vec4f{return textureLoad(image,clamp(vec2i(i.uv*vec2f(textureDimensions(image))),vec2i(0),vec2i(textureDimensions(image))-1),0);}", b = class i {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
49
+ static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
50
+ let n = new i(t);
51
+ return e.use(n), n;
52
+ }
53
+ constructor(e = {}) {
54
+ this.options = e, this.name = "post-processing", this.values = /* @__PURE__ */ new Float32Array(72), this.clock = 0, this.updated = !1, this.ready = !1, this.presentKey = "", this.inverse = this.values.subarray(0, 16), this.inverseWorkspace = /* @__PURE__ */ new Float64Array(52), this.linearShader = "", this.views = new t((e) => ({ buffer: e.device.createBuffer({
55
+ size: s,
56
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
57
+ }) }), (e) => e.buffer.destroy()), this.validate();
58
+ }
59
+ joinStack(e) {
60
+ if (this.device || this.host) throw new a("attached", "PostProcessing is already attached; create a new PostProcessing");
61
+ return this.host = e, {
62
+ kind: "display",
63
+ render: (t, n, r) => {
64
+ let i = e.depthTexture;
65
+ (this.colorView !== n || this.sceneDepth !== i) && (this.colorView = n, this.sceneDepth = i, this.bind(t, n, i.createView({ aspect: "depth-only" }))), this.preparePresent(t, r.format, r.multisample), this.apply(t, r.view);
66
+ }
67
+ };
68
+ }
69
+ validate() {
70
+ let e = this.options, t = e.atmosphere ?? c;
71
+ if ("bloom" in e || "threshold" in e) throw TypeError("PostProcessing has no bloom: put Bloom from @margoengine/post/bloom before it in a PostStack");
72
+ if ("sky" in t || "scattering" in t || "anisotropy" in t) throw TypeError("PostProcessing draws no sky; attach Sky from @margoengine/sky");
73
+ for (let t in e) if (Object.hasOwn(e, t)) {
74
+ let n = e[t];
75
+ if (typeof n == "number" && !p(n)) throw RangeError(`Invalid post ${t}; use a finite number`);
76
+ }
77
+ if (!m(e.exposure ?? 0, -10, 10) || !m(e.contrast ?? 1, 0, 4) || !m(e.saturation ?? 1, 0, 4) || !m(e.focus ?? 5, .01, 1e4) || !m(e.aperture ?? 0, 0, 64) || !m(e.grain ?? 0, 0, 1) || !m(t.density ?? 0, 0, 10) || !m(t.heightFalloff ?? 0, 0, 10) || !m(t.godRays ?? 0, 0, 4)) throw RangeError("Invalid post or atmosphere options; keep each value inside its documented range");
78
+ let n = e.gain ?? d, r = e.lift ?? f, i = t.color ?? u, a = t.sun ?? l;
79
+ if (!h(n, 0, 16) || !h(r, -4, 4) || !h(i, 0, 1)) throw RangeError("Post color vector exceeds limits; keep gain in 0..16, lift in -4..4 and colour in 0..1");
80
+ if (!g(n) || !g(r) || !g(i) || !g(a)) throw RangeError("Invalid post vector; use finite numbers");
81
+ if (!Math.hypot(...a)) throw RangeError("Sun direction must be nonzero");
82
+ }
83
+ setup(e) {
84
+ if (!e.gpu) throw new a("unsupported", "PostProcessing requires WebGPU; use a WebGPU engine; see https://margoengine.com/errors#webgpu");
85
+ this.engine = e, this.device = e.gpu, this.resolve = this.device.createComputePipeline({
86
+ layout: "auto",
87
+ compute: {
88
+ module: this.device.createShaderModule({ code: v }),
89
+ entryPoint: "main"
90
+ }
91
+ });
92
+ }
93
+ registerPasses(e) {
94
+ e.add({
95
+ name: "post:capture",
96
+ order: -50,
97
+ execute: (e) => {
98
+ e.backend === "webgpu" && (this.validate(), this.resize(e), this.host ? this.update(e) : (this.preparePresent(e, e.format, e.descriptor.multisample), this.update(e), this.original = e.color, this.target.scene.begin(e), this.shaderSource !== e.shader && (this.shaderSource = e.shader, this.linearShader = r(e.shader)), e.shader = this.linearShader));
99
+ }
100
+ }), e.add({
101
+ name: "post:resolve",
102
+ order: 30,
103
+ execute: (e) => {
104
+ e.backend === "webgpu" && !this.host && this.process(e);
105
+ }
106
+ });
107
+ }
108
+ resize(e) {
109
+ let t = e.descriptor.multisample?.count ?? 4, r = e.descriptor.depthStencil.format;
110
+ this.ready && (this.host ? this.output.width === e.width && this.output.height === e.height : this.target?.width === e.width && this.target.height === e.height && this.target.samples === t && this.target.depthFormat === r) || (this.ready = !1, this.target?.dispose(), this.output?.destroy(), this.host || (this.target = new n(e.device, e.width, e.height, t, r)), this.presentGroup = void 0, this.output = e.device.createTexture({
111
+ size: [e.width, e.height],
112
+ format: "rgba8unorm",
113
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING
114
+ }), this.host ? this.colorView = void 0 : (this.colorView = this.target.color.createView(), this.depthView = this.target.depth.createView(), this.bind(e, this.colorView, this.target.depth.createView({ aspect: "depth-only" }))), this.ready = !0);
115
+ }
116
+ bind(e, t, n) {
117
+ this.inputs = {
118
+ color: t,
119
+ depth: n,
120
+ output: this.output.createView()
121
+ };
122
+ for (let e of this.views.values()) e.group = void 0;
123
+ }
124
+ groupOf(e) {
125
+ let t = this.slot, { color: n, depth: r, output: i } = this.inputs;
126
+ return t.group ??= e.device.createBindGroup({
127
+ layout: this.resolve.getBindGroupLayout(0),
128
+ entries: [
129
+ {
130
+ binding: 0,
131
+ resource: { buffer: t.buffer }
132
+ },
133
+ {
134
+ binding: 1,
135
+ resource: n
136
+ },
137
+ {
138
+ binding: 2,
139
+ resource: r
140
+ },
141
+ {
142
+ binding: 4,
143
+ resource: i
144
+ }
145
+ ]
146
+ });
147
+ }
148
+ preparePresent(e, t, n) {
149
+ let r = t + ":" + (n?.count ?? 1);
150
+ if (!this.present || this.presentKey !== r) {
151
+ let i = e.device.createShaderModule({ code: y });
152
+ this.present = e.device.createRenderPipeline({
153
+ layout: "auto",
154
+ vertex: {
155
+ module: i,
156
+ entryPoint: "vertex"
157
+ },
158
+ fragment: {
159
+ module: i,
160
+ entryPoint: "fragment",
161
+ targets: [{ format: t }]
162
+ },
163
+ multisample: n
164
+ }), this.presentKey = r, this.presentGroup = void 0;
165
+ }
166
+ this.presentGroup ??= e.device.createBindGroup({
167
+ layout: this.present.getBindGroupLayout(0),
168
+ entries: [{
169
+ binding: 0,
170
+ resource: this.output.createView()
171
+ }]
172
+ });
173
+ }
174
+ update(e) {
175
+ let t = this.options, n = t.atmosphere ?? c, r = this.values;
176
+ if (!o(e.camera.matrix, this.inverse, this.inverseWorkspace)) throw new a("state", "Invalid post camera; give the camera a finite, invertible projection");
177
+ this.updated || (this.clock += e.delta), r.set(e.camera.position, 16);
178
+ let i = n.sun ?? l, s = Math.hypot(i[0], i[1], i[2]);
179
+ for (let e = 0; e < 3; e++) r[20 + e] = i[e] / s;
180
+ r[24] = n.density ?? 0, r[25] = n.heightFalloff ?? 0, r[26] = n.godRays ?? 0, r.set(n.color ?? u, 28), r[32] = t.exposure ?? 0, r[33] = t.contrast ?? 1, r[34] = t.saturation ?? 1, r[36] = t.focus ?? 5, r[37] = t.aperture ?? 0, r[38] = t.grain ?? 0, r[39] = this.clock, r[40] = e.camera.near, r[41] = e.camera.far, r.set(t.gain ?? d, 44), r.set(t.lift ?? f, 48), r.set(e.camera.matrix, 52), r.set(e.view?.viewport ?? [
181
+ 0,
182
+ 0,
183
+ e.width,
184
+ e.height
185
+ ], 68);
186
+ let p = this.slot = this.views.get(e);
187
+ e.device.queue.writeBuffer(p.buffer, 0, r);
188
+ }
189
+ dispatch(e, t, n, r, i) {
190
+ let a = e.encoder.beginComputePass();
191
+ try {
192
+ a.setPipeline(t), a.setBindGroup(0, n), a.dispatchWorkgroups(Math.ceil(r / 8), Math.ceil(i / 8));
193
+ } finally {
194
+ a.end();
195
+ }
196
+ }
197
+ failed(e, t) {
198
+ try {
199
+ this.target.scene.restore(e);
200
+ } catch {}
201
+ throw t;
202
+ }
203
+ process(e) {
204
+ try {
205
+ this.target.scene.resolve(e.encoder, !0), this.apply(e, this.original), this.target.scene.restore(e);
206
+ } catch (t) {
207
+ this.failed(e, t);
208
+ }
209
+ }
210
+ apply(t, n) {
211
+ let r = t.view, i = r?.viewport ?? [
212
+ 0,
213
+ 0,
214
+ t.width,
215
+ t.height
216
+ ];
217
+ this.dispatch(t, this.resolve, this.groupOf(t), i[2], i[3]);
218
+ let a = this.presentPass ??= { colorAttachments: [{
219
+ view: n,
220
+ loadOp: "clear",
221
+ clearValue: {
222
+ r: 0,
223
+ g: 0,
224
+ b: 0,
225
+ a: 1
226
+ },
227
+ storeOp: "store"
228
+ }] }, o = a.colorAttachments[0];
229
+ o.view = n, o.loadOp = e(t, "clear");
230
+ let s = t.encoder.beginRenderPass(a);
231
+ try {
232
+ r && s.setScissorRect(...r.scissor ?? r.viewport), s.setPipeline(this.present), s.setBindGroup(0, this.presentGroup), s.draw(3);
233
+ } finally {
234
+ s.end();
235
+ }
236
+ }
237
+ get time() {
238
+ return this.clock;
239
+ }
240
+ resetTime() {
241
+ this.clock = 0, this.updated = !1;
242
+ }
243
+ advanceTime(e) {
244
+ e > 0 && (this.clock += e);
245
+ }
246
+ onPreUpdate(e) {
247
+ e > 0 && this.views.prune(), this.updated = !0, this.advanceTime(e);
248
+ }
249
+ onDeviceRestored(e) {
250
+ let t = this.clock;
251
+ this.release(), this.setup(e), this.clock = t;
252
+ }
253
+ dispose() {
254
+ let e = this.engine;
255
+ this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name), this.release();
256
+ }
257
+ release() {
258
+ this.sceneDepth = void 0, this.target?.dispose(), this.target = void 0, this.output?.destroy(), this.views.clear(), this.slot = this.inputs = void 0, this.output = void 0, this.device = void 0, this.ready = !1, this.presentKey = "", this.resolve = void 0, this.present = void 0, this.original = void 0, this.colorView = this.depthView = void 0, this.presentGroup = void 0, this.presentPass = void 0;
259
+ }
260
+ [Symbol.dispose ?? Symbol()]() {
261
+ this.dispose();
262
+ }
263
+ };
264
+ //#endregion
265
+ export { b as PostProcessing };
@@ -0,0 +1,7 @@
1
+ //#region ../core/dist/quality.js
2
+ var e = /Mobi|Android/;
3
+ function t(t) {
4
+ return t?.exact || (globalThis.margoLow || e.test(globalThis.navigator?.userAgent) ? 1 : 4);
5
+ }
6
+ //#endregion
7
+ export { t };
@@ -0,0 +1,89 @@
1
+ /** A colour value: linear RGB. */
2
+ export type SettingColor = readonly [number, number, number];
3
+ /** One gradient stop: a colour at a position (`elevation` unless the row's `stopKey` says otherwise). */
4
+ export type SettingStop = { readonly color: SettingColor };
5
+ /** One value of a setting: a number, a colour, a choice, gradient stops, or null where `nullable`. */
6
+ export type SettingValue = number | SettingColor | string | readonly SettingStop[] | null;
7
+ /** Values by setting name; a name left out takes its default. */
8
+ export type SettingValues = Readonly<Record<string, SettingValue>>;
9
+ /** One slider row, the shape every package's `/settings` entry shares. */
10
+ export interface SettingRow {
11
+ readonly name: string;
12
+ readonly group: string;
13
+ /** `number` is a slider, `color` three sliders or a picker, `choice` a list, `gradient` a stop editor. */
14
+ readonly kind: 'number' | 'color' | 'choice' | 'gradient';
15
+ /** The value with which the target draws exactly as it does without the option (null: follow the weather). */
16
+ readonly default: SettingValue;
17
+ readonly min?: number;
18
+ readonly max?: number;
19
+ readonly step?: number;
20
+ readonly choices?: readonly string[];
21
+ /** May be null: the target uses its own value, or follows the weather when `weather` is set. */
22
+ readonly nullable?: boolean;
23
+ /** The weather master slider that drives this row while it is null. */
24
+ readonly weather?: string;
25
+ /** The key a gradient stop is placed by (default `elevation`). */
26
+ readonly stopKey?: string;
27
+ readonly unit: string;
28
+ readonly about: string;
29
+ /** True: cheap to write every frame (no pipeline, buffer or texture is created), so it can be keyed over a shot. False: writing it rebuilds something. */
30
+ readonly live: boolean;
31
+ }
32
+ export declare const numberSetting: (
33
+ name: string,
34
+ group: string,
35
+ value: number,
36
+ min: number,
37
+ max: number,
38
+ step: number,
39
+ unit: string,
40
+ about: string,
41
+ live?: boolean,
42
+ ) => SettingRow;
43
+ export declare const colorSetting: (
44
+ name: string,
45
+ group: string,
46
+ value: SettingColor,
47
+ min: number,
48
+ max: number,
49
+ about: string,
50
+ live?: boolean,
51
+ ) => SettingRow;
52
+ export declare const choiceSetting: (
53
+ name: string,
54
+ group: string,
55
+ choices: readonly string[],
56
+ about: string,
57
+ live?: boolean,
58
+ ) => SettingRow;
59
+ /** The row with a null default that follows the weather master slider `weather` until a value is given. */
60
+ export declare const followsWeather: (row: SettingRow, weather: string) => SettingRow;
61
+ /** Marks `key` of `target` as set by hand (`held` true) or hands it back to the weather (false). */
62
+ export declare function holdFromWeather(target: object, key: string, held?: boolean): void;
63
+ /** True while `key` of `target` holds a value set by hand, which the weather drive leaves alone. */
64
+ export declare const heldFromWeather: (target: object, key: string) => boolean;
65
+ export declare const sameSettingValue: (a: unknown, b: unknown) => boolean;
66
+ export declare function copySettingValue(v: SettingValue): SettingValue;
67
+ /** A fresh object holding every row at its default (a slider panel's state). */
68
+ export declare function settingDefaults(rows: readonly SettingRow[]): Record<string, SettingValue>;
69
+ /** The rows that may change every frame without a rebuild. */
70
+ export declare const liveSettings: (rows: readonly SettingRow[]) => SettingRow[];
71
+ /** A name to row map. */
72
+ export declare const settingIndex: (rows: readonly SettingRow[]) => Map<string, SettingRow>;
73
+ /** @throws RangeError when `v` is not a value of `row`. */
74
+ export declare function checkSettingValue(row: SettingRow, v: unknown, label: string): void;
75
+ /** @throws TypeError for an unknown name; RangeError for a bad value or a null on a row that is not nullable. */
76
+ export declare function checkSettings(
77
+ rows: readonly SettingRow[] | ReadonlyMap<string, SettingRow>,
78
+ values: SettingValues,
79
+ label: string,
80
+ entry?: string,
81
+ ): void;
82
+ /** Checks `values`, then calls `write` once per row (or per row `only` accepts) with the given value or the default. */
83
+ export declare function applySettings(
84
+ rows: readonly SettingRow[],
85
+ values: SettingValues,
86
+ label: string,
87
+ write: (row: SettingRow, value: SettingValue) => void,
88
+ only?: (row: SettingRow) => boolean,
89
+ ): void;
@@ -0,0 +1,90 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { n as e } from "./view-rect-TO68f_UR.js";
5
+ import { n as t } from "./pass-B5mPip-t.js";
6
+ import { MargoError as n } from "@margoengine/math/errors";
7
+ import { invertMatrixStable as r } from "@margoengine/math/mat4";
8
+ //#region src/shaders/stars.wgsl?raw
9
+ var i = "struct CameraUniforms {\n inverseViewProjection: mat4x4<f32>,\n position: vec4<f32>,\n};\n@group(0) @binding(0) var<uniform> camera: CameraUniforms;\n\nstruct VertexOutput {\n @builtin(position) position: vec4<f32>,\n @location(0) ray: vec3<f32>,\n};\n\n@vertex\nfn vs_main(@builtin(vertex_index) index: u32) -> VertexOutput {\n let x = f32(i32(index & 1u) * 4 - 1);\n let y = f32(i32(index & 2u) * 2 - 1);\n var out: VertexOutput;\n out.position = vec4<f32>(x, y, 0.0, 1.0);\n let world = camera.inverseViewProjection * out.position;\n out.ray = world.xyz - camera.position.xyz * world.w;\n return out;\n}\n\n\n\n\nfn pcg(v: u32) -> u32 {\n let state = v * 747796405u + 2891336453u;\n let word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;\n return (word >> 22u) ^ word;\n}\n\nfn hash(p: vec3<f32>) -> f32 {\n let cell = bitcast<vec3<u32>>(vec3<i32>(p));\n return f32(pcg(cell.x + pcg(cell.y + pcg(cell.z))) >> 8u) / 16777216.0;\n}\n\n@fragment\nfn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {\n let ray = normalize(in.ray);\n let magnitude = abs(ray);\n var plane: vec2<f32>;\n var face: f32;\n if (magnitude.x >= magnitude.y && magnitude.x >= magnitude.z) {\n plane = ray.yz / magnitude.x;\n face = select(0.0, 1.0, ray.x > 0.0);\n } else if (magnitude.y >= magnitude.z) {\n plane = ray.xz / magnitude.y;\n face = select(2.0, 3.0, ray.y > 0.0);\n } else {\n plane = ray.xy / magnitude.z;\n face = select(4.0, 5.0, ray.z > 0.0);\n }\n let grid = (plane * 0.5 + 0.5) * 450.0;\n let seed = hash(vec3<f32>(floor(grid), face));\n let distance = length(fract(grid) - 0.5);\n let pixel = max(fwidth(grid.x), fwidth(grid.y));\n let star = (1.0 - smoothstep(0.02, 0.065 + pixel * 0.5, distance)) * smoothstep(0.998, 1.0, seed);\n let tint = mix(vec3<f32>(0.70, 0.84, 1.0), vec3<f32>(1.0, 0.86, 0.66), hash(vec3<f32>(floor(grid), face + 10.0)));\n return vec4<f32>(vec3<f32>(0.0015, 0.002, 0.004) + tint * star * 0.8, 1.0);\n}\n", a = class a {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
10
+ constructor() {
11
+ this.enabled = !0, this.name = "creative:stars", this.pipelines = /* @__PURE__ */ new Map(), this.data = /* @__PURE__ */ new Float32Array(20), this.inverse = /* @__PURE__ */ new Float32Array(16), this.workspace = /* @__PURE__ */ new Float64Array(52), this.descriptor = t(), this.color = this.descriptor.colorAttachments[0], this.depth = this.descriptor.depthStencilAttachment, this.execute = (t) => {
12
+ if (t.backend !== "webgpu" || !this.enabled) return;
13
+ if (!r(t.camera.matrix, this.inverse, this.workspace)) throw new n("state", "Invalid starfield camera; give the camera a finite, invertible projection");
14
+ let i = this.pipelines.get(t.format + ":" + t.descriptor.multisample?.count);
15
+ i || (i = t.device.createRenderPipeline({
16
+ layout: t.device.createPipelineLayout({ bindGroupLayouts: [this.layout] }),
17
+ vertex: {
18
+ module: this.module,
19
+ entryPoint: "vs_main"
20
+ },
21
+ fragment: {
22
+ module: this.module,
23
+ entryPoint: "fs_main",
24
+ targets: [{ format: t.format }]
25
+ },
26
+ primitive: { topology: "triangle-list" },
27
+ multisample: t.descriptor.multisample,
28
+ depthStencil: {
29
+ format: this.depthFormat,
30
+ depthWriteEnabled: !1,
31
+ depthCompare: "greater-equal"
32
+ }
33
+ }), this.pipelines.set(t.format + ":" + t.descriptor.multisample?.count, i)), this.data.set(this.inverse), this.data.set(t.camera.position, 16), t.device.queue.writeBuffer(this.buffer, 0, this.data), this.color.view = t.color, this.depth.view = t.depth;
34
+ let a = t.encoder.beginRenderPass(this.descriptor);
35
+ a.setPipeline(i), a.setBindGroup(0, this.bindings), e(a, t), a.draw(3), a.end();
36
+ };
37
+ }
38
+ static attach(e) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
39
+ let t = new a();
40
+ return e.use(t), t;
41
+ }
42
+ setup(e) {
43
+ let t = e.gpu;
44
+ if (!t) throw new n("unsupported", "The Earth starfield requires WebGPU; use a WebGPU engine; see https://margoengine.com/errors#webgpu");
45
+ this.engine = e, this.depthFormat = t.features.has("depth32float-stencil8") ? "depth32float-stencil8" : "depth32float", this.buffer = t.createBuffer({
46
+ size: this.data.byteLength,
47
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
48
+ });
49
+ try {
50
+ this.layout = t.createBindGroupLayout({ entries: [{
51
+ binding: 0,
52
+ visibility: GPUShaderStage.VERTEX,
53
+ buffer: {
54
+ type: "uniform",
55
+ minBindingSize: 80
56
+ }
57
+ }] }), this.bindings = t.createBindGroup({
58
+ layout: this.layout,
59
+ entries: [{
60
+ binding: 0,
61
+ resource: { buffer: this.buffer }
62
+ }]
63
+ }), this.module = t.createShaderModule({ code: i });
64
+ } catch (e) {
65
+ throw this.release(), e;
66
+ }
67
+ }
68
+ registerPasses(e) {
69
+ e.add({
70
+ name: this.name,
71
+ order: 1,
72
+ execute: this.execute
73
+ });
74
+ }
75
+ release() {
76
+ this.buffer?.destroy(), this.buffer = this.layout = this.bindings = this.module = void 0, this.pipelines.clear();
77
+ }
78
+ onDeviceRestored(e) {
79
+ this.release(), this.setup(e);
80
+ }
81
+ dispose() {
82
+ let e = this.engine?.sceneOf(this);
83
+ this.engine = void 0, e?.removePlugin(this.name), this.release();
84
+ }
85
+ [Symbol.dispose ?? Symbol()]() {
86
+ this.dispose();
87
+ }
88
+ };
89
+ //#endregion
90
+ export { a as t };
@@ -0,0 +1,34 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel } from '@margoengine/core';
3
+ import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
4
+ /**
5
+ * A procedural starfield drawn behind the scene (only where depth is clear), for the Earth/planet demos.
6
+ * `Stars.attach(engine)`; toggle with `stars.enabled`. WebGPU only.
7
+ * @throws MargoError `'unsupported'` on a WebGL engine, `'state'` when the camera matrix is not invertible.
8
+ */
9
+ export declare class Stars implements EnginePlugin {
10
+ /** Same as `engine.use(new Stars(...))`; returns the Stars. */
11
+ static attach(engine: EngineKernel): Stars;
12
+ enabled: boolean;
13
+ readonly name = "creative:stars";
14
+ private engine?;
15
+ private buffer?;
16
+ private layout?;
17
+ private bindings?;
18
+ private module?;
19
+ private depthFormat?;
20
+ private readonly pipelines;
21
+ private readonly data;
22
+ private readonly inverse;
23
+ private readonly workspace;
24
+ private readonly descriptor;
25
+ private readonly color;
26
+ private readonly depth;
27
+ setup(engine: Engine): void;
28
+ registerPasses(graph: FrameGraph): void;
29
+ private readonly execute;
30
+ private release;
31
+ onDeviceRestored(engine: Engine): void;
32
+ dispose(): void;
33
+ [Symbol.dispose]: () => void;
34
+ }
package/dist/stars.js ADDED
@@ -0,0 +1 @@
1
+ export { t as Stars } from "./stars-C0SH8-aJ.js";
@@ -0,0 +1,35 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import { TextureRenderTarget } from './texture-target.js';
3
+ import type { TextureRenderTargetOptions, TargetSurface } from './texture-target.js';
4
+ /** Depth and resolve configuration. Reversed depth defaults to zero, as in the engine. */
5
+ export interface DepthTargetOptions extends TextureRenderTargetOptions {
6
+ /** Default: true. */
7
+ readonly depth?: boolean;
8
+ /** Default: depth32float. */
9
+ readonly depthFormat?: 'depth32float' | 'depth24plus' | 'depth24plus-stencil8';
10
+ /** Default: 1. WebGPU supports 1 or 4 samples. */
11
+ readonly samples?: 1 | 4;
12
+ }
13
+ /** A selected surface with independent depth load and clear controls. */
14
+ export interface DepthTargetSurface extends TargetSurface {
15
+ /** Default: clear. */
16
+ readonly depthLoadOp?: 'clear' | 'load';
17
+ /** Default: 0 (reversed Z). */
18
+ readonly clearDepth?: number;
19
+ }
20
+ /** Cube/array/volume target with per-surface depth, 4x resolve and area-filtered mip generation. */
21
+ export declare class DepthTextureRenderTarget extends TextureRenderTarget {
22
+ #private;
23
+ readonly samples: 1 | 4;
24
+ readonly depthFormat: GPUTextureFormat;
25
+ readonly depth: boolean;
26
+ constructor(device: GPUDevice | null | undefined, options: DepthTargetOptions);
27
+ protected textureUsage(): number;
28
+ /** Depth texture for this surface, including multisampled depth when samples is 4. */
29
+ depthTexture(surface?: TargetSurface): GPUTexture | undefined;
30
+ begin(encoder: GPUCommandEncoder, surface?: DepthTargetSurface): GPURenderPassEncoder;
31
+ /** Record all lower mips after rendering mip zero. Exact box footprints include odd-sized edge texels. */
32
+ generateMipmaps(encoder: GPUCommandEncoder): void;
33
+ restore(device: GPUDevice | null | undefined): void;
34
+ dispose(): void;
35
+ }