@margoengine/lighting2d 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/dist/soft.js ADDED
@@ -0,0 +1,375 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const g=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./resources-Cskg0FqE.js";
5
+ import { c as u, l as d, o as f, s as p, t as m } from "./src-8W7SHUG7.js";
6
+ import { hexColor as h } from "@margoengine/math";
7
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
8
+ //#region src/soft.ts
9
+ var _ = .0046505, v = class extends m {
10
+ constructor(e = {}) {
11
+ super(e), this.sourceRadius = e.sourceRadius ?? 4, this.angularRadius = e.angularRadius ?? .0046505, this.samples = e.samples ?? 16, b(this);
12
+ }
13
+ };
14
+ function y(e, t, n = 0) {
15
+ if (s(e, "sourceRadius", 0), !(n >= 0 && n <= Math.PI / 2)) throw r("Invalid angularRadius: pass radians from 0 to pi / 2 (the Sun is 0.0046505)", "pixel-lighting");
16
+ if (!Number.isInteger(t) || t < 1 || t > 64) throw r("Invalid samples: must be an integer in [1, 64]", "pixel-lighting");
17
+ }
18
+ function b(e) {
19
+ a(e), y(e.sourceRadius, e.samples, e.angularRadius);
20
+ }
21
+ var x = (e) => [
22
+ -Math.cos(e.angle) * Math.cos(e.elevation),
23
+ -Math.sin(e.angle) * Math.cos(e.elevation),
24
+ Math.sin(e.elevation)
25
+ ];
26
+ function S(e, t, n, r, i) {
27
+ a(e);
28
+ let o = i?.samples ?? e.samples ?? 16, s = i?.sourceRadius ?? e.sourceRadius ?? 4, c = i?.angularRadius ?? e.angularRadius ?? .0046505;
29
+ if (y(s, o, c), !e.shadows || t.length < 4) return 1;
30
+ let l = e.kind !== "directional", u = 0, d = 0;
31
+ if (l) {
32
+ let t = e.x - n, i = e.y - r;
33
+ if (d = Math.hypot(t, i), s > 0 && d <= s) return 1;
34
+ u = Math.atan2(i, t);
35
+ } else {
36
+ let t = x(e);
37
+ u = Math.atan2(t[1], t[0]);
38
+ }
39
+ let f = 0;
40
+ if (l ? s > 0 && d > s && (f = Math.asin(Math.min(.9999, s / d))) : f = c, f <= 0 || o === 1) {
41
+ let e = Math.cos(u), i = Math.sin(u);
42
+ for (let a = 0; a + 3 < t.length; a += 4) {
43
+ let o = t[a] - n, s = t[a + 1] - r, c = t[a + 2] - t[a], u = t[a + 3] - t[a + 1], f = e * u - i * c;
44
+ if (Math.abs(f) < 1e-6) continue;
45
+ let p = (o * u - s * c) / f, m = (o * i - s * e) / f;
46
+ if (p >= 0 && (!l || p <= d) && m >= 0 && m <= 1) return 0;
47
+ }
48
+ return 1;
49
+ }
50
+ let p = 0;
51
+ for (let e = 0; e < o; e++) {
52
+ let i = (e + .5) / o, a = u - f + 2 * f * i, s = Math.cos(a), c = Math.sin(a), m = !1;
53
+ for (let e = 0; e + 3 < t.length; e += 4) {
54
+ let i = t[e] - n, a = t[e + 1] - r, o = t[e + 2] - t[e], u = t[e + 3] - t[e + 1], f = s * u - c * o;
55
+ if (Math.abs(f) < 1e-6) continue;
56
+ let p = (i * u - a * o) / f, h = (i * c - a * s) / f;
57
+ if (p >= 0 && (!l || p <= d) && h >= 0 && h <= 1) {
58
+ m = !0;
59
+ break;
60
+ }
61
+ }
62
+ m || p++;
63
+ }
64
+ return p / o;
65
+ }
66
+ function C(e, t, n, r, i) {
67
+ return S(e, t, n, r, i) === 0;
68
+ }
69
+ function w(e, t, n, r, i = [
70
+ 0,
71
+ 0,
72
+ 1
73
+ ], a) {
74
+ let o = f(e, t, n, i), s = S(e, r, t, n, a);
75
+ return [
76
+ o[0] * s,
77
+ o[1] * s,
78
+ o[2] * s
79
+ ];
80
+ }
81
+ var T = "\nstruct L{\nview:vec4f,size:vec4f,at:vec4f,way:vec4f,color:vec4f,soft:vec4f,mask_bounds:vec4f,mask_info:vec4f,}\n@group(0)@binding(0)var<uniform>u:L;\n@group(0)@binding(1)var bumps:texture_2d<f32>;\n@group(0)@binding(2)var<storage,read>edges:array<vec4f>;\n@group(0)@binding(3)var sum:texture_2d_array<f32>;\n@vertex fn wide(@builtin(vertex_index)i:u32)->@builtin(position)vec4f{\nreturn vec4f(f32(i&1u)*4.0 - 1.0,f32(i>>1u)*4.0 - 1.0,0.0,1.0);\n}\n@fragment fn show(@builtin(position)f:vec4f)->@location(0)vec4f{\nlet p=vec2i(f.xy);\nreturn vec4f(textureLoad(sum,p,0,0).rgb+textureLoad(sum,p,1,0).rgb+textureLoad(sum,p,2,0).rgb+textureLoad(sum,p,3,0).rgb,1.0);\n}\n@fragment fn lit(@builtin(position)f:vec4f)->@location(0)vec4f{\nlet world=u.view.xy+f.xy/u.view.z;\nif(!maskVisible(world,u.mask_bounds,u.mask_info)){\nreturn vec4f(0.0);\n}\nvar n=vec3f(0.0,0.0,1.0);\nif(u.size.z>0.0){\nlet s=textureLoad(bumps,vec2i(f.xy),0);\nif(s.a>0.0){\nn=normalize(s.xyz*2.0 - 1.0);\n}\n}\nvar l=u.at.xyz;\nvar a=1.0;\nlet isPoint=u.at.w>0.0;\nvar dL=0.0;\nvar thetaL=0.0;\nif(isPoint){\nlet d=u.at.xy - world;\ndL=length(d);\na=pow(max(0.0,1.0 - dL/u.color.w),u.way.w);\nlet v=vec3f(d,u.at.z);\nl=n;\nif(dot(v,v)>0.0){\nl=normalize(v);\n}\nif(u.size.w>0.0){\nvar c=1.0;\nif(dL>0.0){\nc=-dot(d,u.way.xy)/dL;\n}\nif(u.way.z>u.view.w){\na*=clamp((c - u.view.w)/(u.way.z - u.view.w),0.0,1.0);\n}\nelse if(c<u.view.w){\na=0.0;\n}\n}\nthetaL=atan2(d.y,d.x);\n}\nelse{\nthetaL=atan2(u.at.y,u.at.x);\n}\nvar vis=1.0;\nlet hasShadows=u.soft.w>0.5;\nlet edgeCount=u32(u.soft.y);\nif(hasShadows&&edgeCount>0u&&a>0.0&&!(isPoint&&u.soft.x>0.0&&dL<=u.soft.x)){\nlet sourceRadius=u.soft.x;\nlet samples=max(1u,min(64u,u32(u.soft.z)));\nvar alphaL=0.0;\nif(isPoint){\nif(dL>sourceRadius&&sourceRadius>0.0){\nalphaL=asin(clamp(sourceRadius/dL,0.0,0.9999));\n}\n}\nelse{\nalphaL=sourceRadius;\n}\nif(alphaL>0.0&&samples>1u){\nvar unblocked=0u;\nfor(var k=0u;\nk<samples;\nk++){\nlet frac=(f32(k)+0.5)/f32(samples);\nlet angle=thetaL - alphaL+2.0*alphaL*frac;\nlet rx=cos(angle);\nlet ry=sin(angle);\nvar hit=false;\nfor(var e=0u;\ne<edgeCount;\ne++){\nlet seg=edges[e];\nlet ax=seg.x - world.x;\nlet ay=seg.y - world.y;\nlet ex=seg.z - seg.x;\nlet ey=seg.w - seg.y;\nlet across=rx*ey - ry*ex;\nif(abs(across)>1e-6){\nlet t=(ax*ey - ay*ex)/across;\nlet s=(ax*ry - ay*rx)/across;\nif(t>=0.0&&(!isPoint||t<=dL)&&s>=0.0&&s<=1.0){\nhit=true;\nbreak;\n}\n}\n}\nif(!hit){\nunblocked++;\n}\n}\nvis=f32(unblocked)/f32(samples);\n}\nelse{\nlet rx=cos(thetaL);\nlet ry=sin(thetaL);\nvar hit=false;\nfor(var e=0u;\ne<edgeCount;\ne++){\nlet seg=edges[e];\nlet ax=seg.x - world.x;\nlet ay=seg.y - world.y;\nlet ex=seg.z - seg.x;\nlet ey=seg.w - seg.y;\nlet across=rx*ey - ry*ex;\nif(abs(across)>1e-6){\nlet t=(ax*ey - ay*ex)/across;\nlet s=(ax*ry - ay*rx)/across;\nif(t>=0.0&&(!isPoint||t<=dL)&&s>=0.0&&s<=1.0){\nhit=true;\nbreak;\n}\n}\n}\nif(hit){\nvis=0.0;\n}\n}\n}\nlet shade=a*vis*max(0.0,dot(n,l));\nreturn vec4f(u.color.rgb*shade,0.0);\n}\n", E = 256, D = "rgba16float", O = 4, k = E / 4, A = 0, j = class a {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
82
+ #e;
83
+ #t;
84
+ #n;
85
+ #r;
86
+ #i;
87
+ #a;
88
+ #o;
89
+ #s;
90
+ #c;
91
+ #l;
92
+ #u;
93
+ #d;
94
+ #f;
95
+ #p;
96
+ #m;
97
+ #h;
98
+ #g;
99
+ #_;
100
+ #v;
101
+ #y;
102
+ #b;
103
+ #x;
104
+ #S;
105
+ #C;
106
+ #w;
107
+ constructor(e) {
108
+ if (this.lights = [], this.#n = new c(), this.#i = /* @__PURE__ */ new Float32Array(), this.#a = !1, this.#_ = [], this.#y = /* @__PURE__ */ new Float32Array(), this.#C = new u(), this.#w = !1, !e?.target) throw l("Lighting2D needs a target", "pixel-lighting");
109
+ let t = e.order ?? 20;
110
+ s(t, "order"), this.#e = new o(e.target, "SoftLighting2D target", "setTarget"), this.#t = new o(e.normals, "SoftLighting2D normals", "setNormals"), this.#r = t, this.camera = e.camera ?? {
111
+ x: 0,
112
+ y: 0,
113
+ zoom: 1
114
+ }, this.ambient = e.ambient ?? "#000000", this.layer = e.layer ?? 4294967295, this.sourceRadius = e.sourceRadius ?? 4, this.samples = e.samples ?? 16, this.name = e.name ?? `soft-lighting2d-${++A}`, e.occluders && this.setOccluders(e.occluders);
115
+ }
116
+ static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
117
+ let n = new a(t);
118
+ return g(e).use(n), n;
119
+ }
120
+ addLight(e) {
121
+ let t = new v({
122
+ sourceRadius: this.sourceRadius,
123
+ samples: this.samples,
124
+ ...e
125
+ });
126
+ return this.lights.push(t), t;
127
+ }
128
+ removeLight(e) {
129
+ let t = this.lights.indexOf(e);
130
+ t >= 0 && this.lights.splice(t, 1);
131
+ }
132
+ setOccluders(e) {
133
+ this.#i = p(e), this.#a = !1;
134
+ }
135
+ setTarget(e) {
136
+ if (!e) throw l("Lighting2D needs a target", "pixel-lighting");
137
+ this.#e.set(e, this.#o?.gpu);
138
+ }
139
+ setNormals(e) {
140
+ this.#t.set(e, this.#o?.gpu);
141
+ }
142
+ setup(e) {
143
+ if (this.#w) throw t("disposed", "SoftLighting2D disposed; create a new one");
144
+ if (e.backend !== "webgpu") throw t("unsupported", "SoftLighting2D needs WebGPU", "webgpu");
145
+ this.#o = e, this.#e.fix(e.gpu), this.#t.fix(e.gpu), e.frameGraph.add({
146
+ name: this.name,
147
+ order: this.#r,
148
+ execute: (e) => {
149
+ e.backend === "webgpu" && !e.view?.index && this.#D(e);
150
+ }
151
+ });
152
+ }
153
+ onDeviceRestored() {
154
+ this.#T();
155
+ }
156
+ dispose() {
157
+ this.#w || (this.#w = !0, this.#o?.sceneOf(this)?.removePlugin(this.name), this.#o = void 0, this.#T());
158
+ }
159
+ [Symbol.dispose ?? Symbol()]() {
160
+ this.dispose();
161
+ }
162
+ #T() {
163
+ this.#C.dispose(), this.#v?.destroy(), this.#b?.destroy(), this.#x?.destroy(), this.#h?.destroy(), this.#v = this.#b = this.#x = void 0, this.#c = this.#l = this.#u = this.#d = this.#f = void 0, this.#p = this.#m = this.#h = this.#g = void 0, this.#_ = [], this.#S = this.#s = void 0, this.#n.clear(), this.#a = !1;
164
+ }
165
+ #E(e, t) {
166
+ if (this.#s !== e && (this.#s && this.#T(), this.#s = e), this.#c || (this.#c = e.createShaderModule({
167
+ label: "soft-lighting2d",
168
+ code: d(4) + T
169
+ }), this.#l = e.createBindGroupLayout({ entries: [
170
+ {
171
+ binding: 0,
172
+ visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
173
+ buffer: {
174
+ type: "uniform",
175
+ hasDynamicOffset: !0,
176
+ minBindingSize: 128
177
+ }
178
+ },
179
+ {
180
+ binding: 1,
181
+ visibility: GPUShaderStage.FRAGMENT,
182
+ texture: { sampleType: "unfilterable-float" }
183
+ },
184
+ {
185
+ binding: 2,
186
+ visibility: GPUShaderStage.FRAGMENT,
187
+ buffer: { type: "read-only-storage" }
188
+ },
189
+ {
190
+ binding: 4,
191
+ visibility: GPUShaderStage.FRAGMENT,
192
+ buffer: { type: "read-only-storage" }
193
+ }
194
+ ] }), this.#u = e.createPipelineLayout({ bindGroupLayouts: [this.#l] }), this.#x = e.createTexture({
195
+ size: [1, 1],
196
+ format: "rgba8unorm",
197
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
198
+ })), !this.#f) {
199
+ let t = this.#c, n = this.#u, r = D;
200
+ this.#f = e.createRenderPipeline({
201
+ label: "soft-lighting2d light",
202
+ layout: n,
203
+ vertex: {
204
+ module: t,
205
+ entryPoint: "wide"
206
+ },
207
+ fragment: {
208
+ module: t,
209
+ entryPoint: "lit",
210
+ targets: [{
211
+ format: r,
212
+ blend: {
213
+ color: {
214
+ srcFactor: "one",
215
+ dstFactor: "one"
216
+ },
217
+ alpha: {
218
+ srcFactor: "zero",
219
+ dstFactor: "one"
220
+ }
221
+ }
222
+ }]
223
+ }
224
+ });
225
+ }
226
+ t !== this.#d && (this.#d = t, this.#p = e.createRenderPipeline({
227
+ label: "soft-lighting2d show",
228
+ layout: "auto",
229
+ vertex: {
230
+ module: this.#c,
231
+ entryPoint: "wide"
232
+ },
233
+ fragment: {
234
+ module: this.#c,
235
+ entryPoint: "show",
236
+ targets: [{ format: t }]
237
+ }
238
+ }), this.#m = void 0);
239
+ }
240
+ #D(a) {
241
+ let o = a.device, s = this.#e.get(o, this.#o);
242
+ if (!s) return;
243
+ let c = this.#t.get(o, this.#o), l = this.camera, u = h(this.ambient), d = this.lights.filter((e) => (e.layers & this.layer) !== 0);
244
+ if (!u) throw r("Invalid ambient color: use a hex color such as #rrggbb", "pixel-lighting");
245
+ if (c && (c.width !== s.width || c.height !== s.height)) throw t("state", "Lighting2D normals differ in size from the target", "pixel-lighting");
246
+ n(l), e(this.layer), this.lights.forEach(b), this.#E(o, s.format), this.#C.upload(o, d), this.#S = void 0;
247
+ let f = Math.max(1, d.length) * k;
248
+ (!this.#v || this.#y.length < f) && (this.#v?.destroy(), this.#y = new Float32Array(f * 2), this.#v = o.createBuffer({
249
+ label: "soft-lighting2d",
250
+ size: this.#y.byteLength,
251
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
252
+ }));
253
+ let p = this.#i, m = p.length / 4, g = Math.max(16, p.byteLength);
254
+ if ((!this.#b || this.#b.size < g) && (this.#b?.destroy(), this.#b = o.createBuffer({
255
+ label: "soft-lighting2d edges",
256
+ size: g * 2,
257
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
258
+ }), this.#a = !1), this.#a ||= (p.length ? o.queue.writeBuffer(this.#b, 0, p) : o.queue.writeBuffer(this.#b, 0, /* @__PURE__ */ new Float32Array(4)), !0), this.#S = this.#n.get(o, "light", this.#l, [
259
+ {
260
+ binding: 0,
261
+ resource: {
262
+ buffer: this.#v,
263
+ size: 128
264
+ }
265
+ },
266
+ {
267
+ binding: 1,
268
+ resource: i(c ?? this.#x)
269
+ },
270
+ {
271
+ binding: 4,
272
+ resource: { buffer: this.#C.buffer }
273
+ },
274
+ {
275
+ binding: 2,
276
+ resource: { buffer: this.#b }
277
+ }
278
+ ]), !this.#h || this.#h.width !== s.width || this.#h.height !== s.height) {
279
+ this.#h?.destroy();
280
+ let e = [s.width, s.height];
281
+ this.#h = o.createTexture({
282
+ label: "soft-lighting2d sum",
283
+ size: [
284
+ e[0],
285
+ e[1],
286
+ O
287
+ ],
288
+ format: D,
289
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
290
+ }), this.#g = this.#h.createView({ dimension: "2d-array" }), this.#_ = Array.from({ length: O }, (e, t) => this.#h.createView({
291
+ dimension: "2d",
292
+ baseArrayLayer: t,
293
+ arrayLayerCount: 1
294
+ })), this.#m = void 0;
295
+ }
296
+ this.#m ??= o.createBindGroup({
297
+ layout: this.#p.getBindGroupLayout(0),
298
+ entries: [{
299
+ binding: 3,
300
+ resource: this.#g
301
+ }]
302
+ });
303
+ let _ = this.#y;
304
+ d.forEach((e, t) => {
305
+ let n = h(e.color), r = e.kind === "directional", i = r ? x(e) : [
306
+ e.x,
307
+ e.y,
308
+ e.height
309
+ ];
310
+ _.set([
311
+ l.x,
312
+ l.y,
313
+ l.zoom,
314
+ Math.cos(e.cone),
315
+ s.width,
316
+ s.height,
317
+ +!!c,
318
+ +(e.kind === "spot"),
319
+ i[0],
320
+ i[1],
321
+ i[2],
322
+ +!r,
323
+ Math.cos(e.angle),
324
+ Math.sin(e.angle),
325
+ Math.cos(e.cone * (1 - e.softness)),
326
+ e.falloff,
327
+ n[0] * e.intensity,
328
+ n[1] * e.intensity,
329
+ n[2] * e.intensity,
330
+ e.radius,
331
+ e.kind === "directional" ? e.angularRadius : e.sourceRadius,
332
+ m,
333
+ e.samples,
334
+ +!!e.shadows,
335
+ ...this.#C.fields[t]
336
+ ], t * k);
337
+ }), o.queue.writeBuffer(this.#v, 0, _), this.#_.forEach((e, t) => a.encoder.beginRenderPass({ colorAttachments: [{
338
+ view: e,
339
+ loadOp: "clear",
340
+ clearValue: t ? [
341
+ 0,
342
+ 0,
343
+ 0,
344
+ 1
345
+ ] : [
346
+ u[0],
347
+ u[1],
348
+ u[2],
349
+ 1
350
+ ],
351
+ storeOp: "store"
352
+ }] }).end()), d.forEach((e, t) => {
353
+ let n = a.encoder.beginRenderPass({ colorAttachments: [{
354
+ view: this.#_[t % O],
355
+ loadOp: "load",
356
+ storeOp: "store"
357
+ }] });
358
+ n.setBindGroup(0, this.#S, [t * E]), n.setPipeline(this.#f), n.draw(3), n.end();
359
+ });
360
+ let v = a.encoder.beginRenderPass({ colorAttachments: [{
361
+ view: i(s),
362
+ loadOp: "clear",
363
+ clearValue: [
364
+ 0,
365
+ 0,
366
+ 0,
367
+ 1
368
+ ],
369
+ storeOp: "store"
370
+ }] });
371
+ v.setPipeline(this.#p), v.setBindGroup(0, this.#m), v.draw(3), v.end();
372
+ }
373
+ };
374
+ //#endregion
375
+ export { _ as SUN_ANGULAR_RADIUS, v as SoftLight2D, j as SoftLighting2D, j as SoftShadows2D, C as softInShadow, w as softLightAt, S as softShadowFactor };