@margoengine/lighting2d 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/dist/cookie.js ADDED
@@ -0,0 +1,701 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const _=(engine,version="1.0.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, r as p, s as m, t as h } from "./src-8W7SHUG7.js";
6
+ import { hexColor as g } from "@margoengine/math";
7
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
8
+ //#region src/cookie.ts
9
+ var v = class {
10
+ constructor(e) {
11
+ let { width: t, height: n, data: i } = e;
12
+ if (!Number.isInteger(t) || t <= 0 || !Number.isInteger(n) || n <= 0) throw r("Invalid cookie texture dimensions", "pixel-lighting");
13
+ if (!i || i.length !== t * n * 4) throw r("Cookie texture data length must match width * height * 4", "pixel-lighting");
14
+ this.width = t, this.height = n;
15
+ let a = i instanceof Float32Array, o = new Float32Array(t * n * 4);
16
+ for (let e = 0; e < o.length; e++) {
17
+ let t = i[e];
18
+ if (!Number.isFinite(t)) throw r("Invalid cookie texture pixel value", "pixel-lighting");
19
+ o[e] = a ? Math.max(0, Math.min(1, t)) : Math.max(0, Math.min(255, t)) / 255;
20
+ }
21
+ this.data = o;
22
+ }
23
+ };
24
+ function y(e, t, n) {
25
+ return new v({
26
+ width: e,
27
+ height: t,
28
+ data: n
29
+ });
30
+ }
31
+ function b(e, t, n = 6, i = !1) {
32
+ if (n < 1 || !Number.isInteger(n)) throw r("Invalid slats count", "pixel-lighting");
33
+ let a = new Uint8Array(e * t * 4);
34
+ for (let r = 0; r < t; r++) for (let o = 0; o < e; o++) {
35
+ let s = (i ? o / e : r / t) * n % 1 < .65 ? 255 : 30, c = (r * e + o) * 4;
36
+ a[c] = s, a[c + 1] = s, a[c + 2] = s, a[c + 3] = 255;
37
+ }
38
+ return new v({
39
+ width: e,
40
+ height: t,
41
+ data: a
42
+ });
43
+ }
44
+ function x(e, t = .5, n = .95) {
45
+ if (e < 2 || !Number.isInteger(e)) throw r("Invalid cookie size", "pixel-lighting");
46
+ let i = new Uint8Array(e * e * 4), a = e / 2;
47
+ for (let r = 0; r < e; r++) for (let o = 0; o < e; o++) {
48
+ let s = (o + .5 - a) / a, c = (r + .5 - a) / a, l = Math.hypot(s, c), u = 0;
49
+ if (l <= t) u = 255;
50
+ else if (l < n) {
51
+ let e = (l - t) / (n - t);
52
+ u = Math.round(255 * (1 - e * e));
53
+ }
54
+ let d = (r * e + o) * 4;
55
+ i[d] = u, i[d + 1] = u, i[d + 2] = u, i[d + 3] = 255;
56
+ }
57
+ return new v({
58
+ width: e,
59
+ height: e,
60
+ data: i
61
+ });
62
+ }
63
+ var S = 1 / 1024;
64
+ function C(e, t, n, r = "zero") {
65
+ let i = t, a = n;
66
+ if (r === "zero") {
67
+ if (i < 0 || i > 1 || a < 0 || a > 1) return [
68
+ 0,
69
+ 0,
70
+ 0,
71
+ 0
72
+ ];
73
+ } else if (r === "clamp") i = Math.max(0, Math.min(1, i)), a = Math.max(0, Math.min(1, a));
74
+ else if (r === "repeat") i -= Math.floor(i), a -= Math.floor(a);
75
+ else if (r === "mirror") {
76
+ let e = Math.abs(i) % 2;
77
+ i = e > 1 ? 2 - e : e;
78
+ let t = Math.abs(a) % 2;
79
+ a = t > 1 ? 2 - t : t;
80
+ }
81
+ let { width: o, height: s, data: c } = e, l = Math.max(0, Math.min(o - 1, Math.floor(i * o + S))), u = (Math.max(0, Math.min(s - 1, Math.floor(a * s + S))) * o + l) * 4;
82
+ return [
83
+ c[u],
84
+ c[u + 1],
85
+ c[u + 2],
86
+ c[u + 3]
87
+ ];
88
+ }
89
+ var w = class extends h {
90
+ constructor(e = {}) {
91
+ super(e), this.cookie = e.cookie;
92
+ let t = this.radius > 0 ? this.radius * 2 : 64;
93
+ this.cookieScale = Array.isArray(e.cookieScale) ? [e.cookieScale[0], e.cookieScale[1]] : typeof e.cookieScale == "number" ? [e.cookieScale, e.cookieScale] : [t, t], this.cookieOffset = e.cookieOffset ? [e.cookieOffset[0], e.cookieOffset[1]] : [0, 0], this.cookieRotation = e.cookieRotation ?? (this.kind === "spot" ? this.angle : 0), this.cookieWrap = e.cookieWrap ?? "zero", this.cookieMode = e.cookieMode ?? "multiply", T(this);
94
+ }
95
+ };
96
+ function T(e) {
97
+ a(e);
98
+ for (let t of e.cookieScale) s(t, "cookieScale", Number.MIN_VALUE);
99
+ for (let t of e.cookieOffset) s(t, "cookieOffset");
100
+ if (s(e.cookieRotation, "cookieRotation"), ![
101
+ "zero",
102
+ "clamp",
103
+ "repeat",
104
+ "mirror"
105
+ ].includes(e.cookieWrap)) throw r("Invalid cookieWrap: use zero, clamp, repeat or mirror", "pixel-lighting");
106
+ if (![
107
+ "multiply",
108
+ "alpha",
109
+ "color"
110
+ ].includes(e.cookieMode)) throw r("Invalid cookieMode: use multiply, alpha or color", "pixel-lighting");
111
+ }
112
+ var E = (e) => [
113
+ -Math.cos(e.angle) * Math.cos(e.elevation),
114
+ -Math.sin(e.angle) * Math.cos(e.elevation),
115
+ Math.sin(e.elevation)
116
+ ];
117
+ function D(e, t, n, r = [
118
+ 0,
119
+ 0,
120
+ 1
121
+ ]) {
122
+ let i = f(e, t, n, r);
123
+ if (!e.cookie) return i;
124
+ if (e.mask && !p(e.mask, t, n)) return [
125
+ 0,
126
+ 0,
127
+ 0
128
+ ];
129
+ let a = t - e.x - e.cookieOffset[0], o = n - e.y - e.cookieOffset[1], s = Math.cos(e.cookieRotation), c = Math.sin(e.cookieRotation), l = a * s + o * c, u = -a * c + o * s, d = l / e.cookieScale[0] + .5, m = u / e.cookieScale[1] + .5, [h, g, _, v] = C(e.cookie, d, m, e.cookieWrap);
130
+ if (e.cookieMode === "alpha") return [
131
+ i[0] * v,
132
+ i[1] * v,
133
+ i[2] * v
134
+ ];
135
+ if (e.cookieMode === "color") {
136
+ let i = Math.hypot(r[0], r[1], r[2]), a = r.map((e) => e / i), o = 1, s;
137
+ if (e.kind === "directional") s = E(e);
138
+ else {
139
+ let r = e.x - t, i = e.y - n, c = Math.hypot(r, i), l = Math.hypot(c, e.height);
140
+ if (o = Math.max(0, 1 - c / e.radius) ** e.falloff, s = l ? [
141
+ r / l,
142
+ i / l,
143
+ e.height / l
144
+ ] : a, e.kind === "spot") {
145
+ let t = c ? -(r * Math.cos(e.angle) + i * Math.sin(e.angle)) / c : 1, n = Math.cos(e.cone), a = Math.cos(e.cone * (1 - e.softness));
146
+ o *= a > n ? Math.min(1, Math.max(0, (t - n) / (a - n))) : +(t >= n);
147
+ }
148
+ }
149
+ let c = o * Math.max(0, a[0] * s[0] + a[1] * s[1] + a[2] * s[2]) * e.intensity;
150
+ return [
151
+ h * v * c,
152
+ g * v * c,
153
+ _ * v * c
154
+ ];
155
+ }
156
+ return [
157
+ i[0] * h * v,
158
+ i[1] * g * v,
159
+ i[2] * _ * v
160
+ ];
161
+ }
162
+ var O = `
163
+ struct Uniforms{
164
+ view:vec4f,size:vec4f,at:vec4f,way:vec4f,color:vec4f,cookie_scale:vec4f,cookie_rot:vec4f,cookie_info:vec4f,cookie_origin:vec4f,mask_bounds:vec4f,mask_info:vec4f,}
165
+ @group(0)@binding(0)var<uniform>u:Uniforms;
166
+ @group(0)@binding(1)var bumps:texture_2d<f32>;
167
+ @group(0)@binding(2)var<storage,read>edges:array<vec4f>;
168
+ @group(1)@binding(0)var cookie_tex:texture_2d<f32>;
169
+ @group(1)@binding(1)var cookie_samp:sampler;
170
+ @group(0)@binding(3)var sum:texture_2d_array<f32>;
171
+ @vertex fn wide(@builtin(vertex_index)i:u32)->@builtin(position)vec4f{
172
+ return vec4f(f32(i&1u)*4.0 - 1.0,f32(i>>1u)*4.0 - 1.0,0.0,1.0);
173
+ }
174
+ fn clip(d:vec2f)->vec2f{
175
+ return d*u.view.z/u.size.xy*vec2f(2.0,-2.0);
176
+ }
177
+ @vertex fn edge(@builtin(vertex_index)i:u32,@location(0)e:vec4f)->@builtin(position)vec4f{
178
+ var p=e.xy;
179
+ if((i&1u)==1u){
180
+ p=e.zw;
181
+ }
182
+ if(i<2u){
183
+ return vec4f(clip(p - u.view.xy)+vec2f(-1.0,1.0),0.0,1.0);
184
+ }
185
+ return vec4f(clip(p*u.at.w - u.at.xy),0.0,0.0);
186
+ }
187
+ @fragment fn none()->@location(0)vec4f{
188
+ return vec4f(0.0);
189
+ }
190
+ @fragment fn show(@builtin(position)f:vec4f)->@location(0)vec4f{
191
+ let p=vec2i(f.xy);
192
+ return 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);
193
+ }
194
+ @fragment fn lit(@builtin(position)f:vec4f)->@location(0)vec4f{
195
+ let world=u.view.xy+f.xy/u.view.z;
196
+ if(!maskVisible(world,u.mask_bounds,u.mask_info)){
197
+ return vec4f(0.0);
198
+ }
199
+ var n=vec3f(0.0,0.0,1.0);
200
+ if(u.size.z>0.0){
201
+ let s=textureLoad(bumps,vec2i(f.xy),0);
202
+ if(s.a>0.0){
203
+ n=normalize(s.xyz*2.0 - 1.0);
204
+ }
205
+ }
206
+ var l=u.at.xyz;
207
+ var a=1.0;
208
+ let isPoint=u.at.w>0.0;
209
+ var dL=0.0;
210
+ if(isPoint){
211
+ let d=u.at.xy - world;
212
+ dL=length(d);
213
+ a=pow(max(0.0,1.0 - dL/u.color.w),u.way.w);
214
+ let v=vec3f(d,u.at.z);
215
+ l=n;
216
+ if(dot(v,v)>0.0){
217
+ l=normalize(v);
218
+ }
219
+ if(u.size.w>0.0){
220
+ var c=1.0;
221
+ if(dL>0.0){
222
+ c=-dot(d,u.way.xy)/dL;
223
+ }
224
+ if(u.way.z>u.view.w){
225
+ a*=clamp((c - u.view.w)/(u.way.z - u.view.w),0.0,1.0);
226
+ }
227
+ else if(c<u.view.w){
228
+ a=0.0;
229
+ }
230
+ }
231
+ }
232
+ var vis=1.0;
233
+ let hasShadows=u.cookie_info.z>0.5;
234
+ let edgeCount=u32(u.cookie_info.y);
235
+ if(hasShadows&&edgeCount>0u&&a>0.0){
236
+ let thetaL=select(atan2(u.at.y,u.at.x),atan2(u.at.y - world.y,u.at.x - world.x),isPoint);
237
+ let rx=cos(thetaL);
238
+ let ry=sin(thetaL);
239
+ for(var e=0u;
240
+ e<edgeCount;
241
+ e++){
242
+ let seg=edges[e];
243
+ let ax=seg.x - world.x;
244
+ let ay=seg.y - world.y;
245
+ let ex=seg.z - seg.x;
246
+ let ey=seg.w - seg.y;
247
+ let across=rx*ey - ry*ex;
248
+ if(abs(across)>1e-6){
249
+ let t=(ax*ey - ay*ex)/across;
250
+ let s=(ax*ry - ay*rx)/across;
251
+ if(t>=0.0&&(!isPoint||t<=dL)&&s>=0.0&&s<=1.0){
252
+ vis=0.0;
253
+ break;
254
+ }
255
+ }
256
+ }
257
+ }
258
+ if(vis<=0.0||a<=0.0){
259
+ return vec4f(0.0);
260
+ }
261
+ var cookieMod=vec4f(1.0);
262
+ if(u.cookie_info.x>0.5){
263
+ let delta=world - u.cookie_origin.xy - u.cookie_scale.zw;
264
+ let rx=delta.x*u.cookie_rot.x - delta.y*u.cookie_rot.y;
265
+ let ry=delta.x*u.cookie_rot.y+delta.y*u.cookie_rot.x;
266
+ var uv=vec2f(rx/u.cookie_scale.x+0.5,ry/u.cookie_scale.y+0.5);
267
+ let wrapMode=u32(u.cookie_rot.z);
268
+ var oob=false;
269
+ if(wrapMode==0u){
270
+ if(uv.x<0.0||uv.x>1.0||uv.y<0.0||uv.y>1.0){
271
+ oob=true;
272
+ }
273
+ }
274
+ else if(wrapMode==1u){
275
+ uv=clamp(uv,vec2f(0.0),vec2f(1.0));
276
+ }
277
+ else if(wrapMode==2u){
278
+ uv=fract(uv);
279
+ }
280
+ else if(wrapMode==3u){
281
+ let m=abs(uv)% vec2f(2.0);
282
+ uv=select(m,vec2f(2.0)- m,m>vec2f(1.0));
283
+ }
284
+ if(oob){
285
+ cookieMod=vec4f(0.0);
286
+ }
287
+ else{
288
+ let dims=vec2i(textureDimensions(cookie_tex));
289
+ let texel=clamp(vec2i(floor(uv*vec2f(dims)+vec2f(${S}
290
+ ))),vec2i(0),dims - vec2i(1));
291
+ cookieMod=textureLoad(cookie_tex,texel,0);
292
+ }
293
+ }
294
+ let diffuse=max(0.0,dot(n,l));
295
+ let blendMode=u32(u.cookie_rot.w);
296
+ var outCol=vec3f(0.0);
297
+ if(blendMode==1u&&u.cookie_info.x>0.5){
298
+ outCol=u.color.rgb*a*diffuse*vis*cookieMod.a;
299
+ }
300
+ else if(blendMode==2u&&u.cookie_info.x>0.5){
301
+ outCol=cookieMod.rgb*cookieMod.a*a*diffuse*vis*u.cookie_info.w;
302
+ }
303
+ else{
304
+ outCol=u.color.rgb*a*diffuse*vis*cookieMod.rgb*cookieMod.a;
305
+ }
306
+ return vec4f(outCol,0.0);
307
+ }
308
+ `, k = 256, A = "rgba16float", j = 4, M = k / 4, N = 0, P = class a {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
309
+ #e;
310
+ #t;
311
+ #n;
312
+ #r;
313
+ #i;
314
+ #a;
315
+ #o;
316
+ #s;
317
+ #c;
318
+ #l;
319
+ #u;
320
+ #d;
321
+ #f;
322
+ #p;
323
+ #m;
324
+ #h;
325
+ #g;
326
+ #_;
327
+ #v;
328
+ #y;
329
+ #b;
330
+ #x;
331
+ #S;
332
+ #C;
333
+ #w;
334
+ #T;
335
+ #E;
336
+ #D;
337
+ #O;
338
+ #k;
339
+ #A;
340
+ constructor(e) {
341
+ if (this.lights = [], this.#n = new c(), this.#i = /* @__PURE__ */ new Float32Array(), this.#a = !1, this.#v = [], this.#b = /* @__PURE__ */ new Float32Array(), this.#D = /* @__PURE__ */ new Map(), this.#O = /* @__PURE__ */ new WeakMap(), this.#k = new u(), this.#A = !1, !e?.target) throw l("Lighting2D needs a target", "pixel-lighting");
342
+ let t = e.order ?? 20;
343
+ s(t, "order"), this.#e = new o(e.target, "CookieLighting2D target", "setTarget"), this.#t = new o(e.normals, "CookieLighting2D normals", "setNormals"), this.#r = t, this.camera = e.camera ?? {
344
+ x: 0,
345
+ y: 0,
346
+ zoom: 1
347
+ }, this.ambient = e.ambient ?? "#000000", this.layer = e.layer ?? 4294967295;
348
+ let n = e.cookieScale ?? 64;
349
+ this.cookieScale = Array.isArray(n) ? [n[0], n[1]] : [n, n], this.cookieWrap = e.cookieWrap ?? "zero", this.cookieMode = e.cookieMode ?? "multiply", this.name = e.name ?? `cookie-lighting2d-${++N}`, e.occluders && this.setOccluders(e.occluders);
350
+ }
351
+ static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
352
+ let n = new a(t);
353
+ return _(e).use(n), n;
354
+ }
355
+ addLight(e = {}) {
356
+ if (this.#A) throw t("disposed", "CookieLighting2D has been disposed", "pixel-lighting");
357
+ let n = new w({
358
+ cookieScale: this.cookieScale,
359
+ cookieWrap: this.cookieWrap,
360
+ cookieMode: this.cookieMode,
361
+ ...e
362
+ });
363
+ return this.lights.push(n), n;
364
+ }
365
+ removeLight(e) {
366
+ let t = this.lights.indexOf(e);
367
+ t >= 0 && this.lights.splice(t, 1);
368
+ }
369
+ setOccluders(e) {
370
+ this.#i = m(e), this.#a = !1;
371
+ }
372
+ setTarget(e) {
373
+ if (!e) throw l("Lighting2D needs a target", "pixel-lighting");
374
+ this.#e.set(e, this.#o?.gpu);
375
+ }
376
+ setNormals(e) {
377
+ this.#t.set(e, this.#o?.gpu);
378
+ }
379
+ setup(e) {
380
+ if (this.#A) throw t("disposed", "CookieLighting2D disposed; create a new one");
381
+ if (e.backend !== "webgpu") throw t("unsupported", "CookieLighting2D needs WebGPU", "webgpu");
382
+ this.#o = e, this.#e.fix(e.gpu), this.#t.fix(e.gpu), e.frameGraph.add({
383
+ name: this.name,
384
+ order: this.#r,
385
+ execute: (e) => {
386
+ e.backend === "webgpu" && !e.view?.index && this.#P(e);
387
+ }
388
+ });
389
+ }
390
+ onDeviceRestored() {
391
+ this.#j();
392
+ }
393
+ dispose() {
394
+ this.#A || (this.#A = !0, this.#o?.sceneOf(this)?.removePlugin(this.name), this.#o = void 0, this.#j());
395
+ }
396
+ [Symbol.dispose ?? Symbol()]() {
397
+ this.dispose();
398
+ }
399
+ #j() {
400
+ this.#k.dispose();
401
+ for (let e of this.#D.values()) e.destroy();
402
+ this.#D.clear(), this.#O = /* @__PURE__ */ new WeakMap(), this.#y?.destroy(), this.#x?.destroy(), this.#S?.destroy(), this.#w?.destroy(), this.#g?.destroy(), this.#y = this.#x = this.#S = this.#w = void 0, this.#T = void 0, this.#E = void 0, this.#c = this.#l = this.#u = this.#d = this.#f = this.#p = void 0, this.#m = this.#h = this.#g = this.#_ = void 0, this.#v = [], this.#C = this.#s = void 0, this.#n.clear(), this.#a = !1;
403
+ }
404
+ #M(e, t) {
405
+ let n = this.#D.get(t);
406
+ if (!n) {
407
+ n = e.createTexture({
408
+ label: "lighting2d cookie upload",
409
+ size: [
410
+ t.width,
411
+ t.height,
412
+ 1
413
+ ],
414
+ format: "rgba8unorm",
415
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
416
+ });
417
+ let r = new Uint8Array(t.data.length);
418
+ for (let e = 0; e < t.data.length; e++) r[e] = Math.round(t.data[e] * 255);
419
+ e.queue.writeTexture({ texture: n }, r, { bytesPerRow: t.width * 4 }, {
420
+ width: t.width,
421
+ height: t.height
422
+ }), this.#D.set(t, n);
423
+ }
424
+ return n;
425
+ }
426
+ #N(e, t) {
427
+ if (this.#s !== e && (this.#s && this.#j(), this.#s = e), this.#c || (this.#c = e.createShaderModule({
428
+ label: "cookie-lighting2d",
429
+ code: d(4) + O
430
+ }), this.#l = e.createBindGroupLayout({ entries: [
431
+ {
432
+ binding: 0,
433
+ visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
434
+ buffer: {
435
+ type: "uniform",
436
+ hasDynamicOffset: !0,
437
+ minBindingSize: 176
438
+ }
439
+ },
440
+ {
441
+ binding: 1,
442
+ visibility: GPUShaderStage.FRAGMENT,
443
+ texture: { sampleType: "unfilterable-float" }
444
+ },
445
+ {
446
+ binding: 2,
447
+ visibility: GPUShaderStage.FRAGMENT,
448
+ buffer: { type: "read-only-storage" }
449
+ },
450
+ {
451
+ binding: 4,
452
+ visibility: GPUShaderStage.FRAGMENT,
453
+ buffer: { type: "read-only-storage" }
454
+ }
455
+ ] }), this.#u = e.createBindGroupLayout({ entries: [{
456
+ binding: 0,
457
+ visibility: GPUShaderStage.FRAGMENT,
458
+ texture: { sampleType: "float" }
459
+ }, {
460
+ binding: 1,
461
+ visibility: GPUShaderStage.FRAGMENT,
462
+ sampler: { type: "filtering" }
463
+ }] }), this.#d = e.createPipelineLayout({ bindGroupLayouts: [this.#l, this.#u] }), this.#S = e.createTexture({
464
+ size: [1, 1],
465
+ format: "rgba8unorm",
466
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
467
+ }), this.#w = e.createTexture({
468
+ size: [1, 1],
469
+ format: "rgba8unorm",
470
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
471
+ }), e.queue.writeTexture({ texture: this.#w }, new Uint8Array([
472
+ 255,
473
+ 255,
474
+ 255,
475
+ 255
476
+ ]), { bytesPerRow: 4 }, {
477
+ width: 1,
478
+ height: 1
479
+ }), this.#T = e.createSampler({
480
+ magFilter: "nearest",
481
+ minFilter: "nearest"
482
+ }), this.#E = e.createBindGroup({
483
+ layout: this.#u,
484
+ entries: [{
485
+ binding: 0,
486
+ resource: this.#w.createView()
487
+ }, {
488
+ binding: 1,
489
+ resource: this.#T
490
+ }]
491
+ })), !this.#p) {
492
+ let t = this.#c, n = this.#d, r = A;
493
+ this.#p = e.createRenderPipeline({
494
+ label: "cookie-lighting2d light",
495
+ layout: n,
496
+ vertex: {
497
+ module: t,
498
+ entryPoint: "wide"
499
+ },
500
+ fragment: {
501
+ module: t,
502
+ entryPoint: "lit",
503
+ targets: [{
504
+ format: r,
505
+ blend: {
506
+ color: {
507
+ srcFactor: "one",
508
+ dstFactor: "one"
509
+ },
510
+ alpha: {
511
+ srcFactor: "zero",
512
+ dstFactor: "one"
513
+ }
514
+ }
515
+ }]
516
+ }
517
+ });
518
+ }
519
+ t !== this.#f && (this.#f = t, this.#m = e.createRenderPipeline({
520
+ label: "cookie-lighting2d show",
521
+ layout: "auto",
522
+ vertex: {
523
+ module: this.#c,
524
+ entryPoint: "wide"
525
+ },
526
+ fragment: {
527
+ module: this.#c,
528
+ entryPoint: "show",
529
+ targets: [{ format: t }]
530
+ }
531
+ }), this.#h = void 0);
532
+ }
533
+ #P(a) {
534
+ let o = a.device, s = this.#e.get(o, this.#o);
535
+ if (!s) return;
536
+ let c = this.#t.get(o, this.#o), l = this.camera, u = g(this.ambient), d = this.lights.filter((e) => (e.layers & this.layer) !== 0);
537
+ if (!u) throw r("Invalid ambient color: use a hex color such as #rrggbb", "pixel-lighting");
538
+ if (c && (c.width !== s.width || c.height !== s.height)) throw t("state", "Lighting2D normals differ in size from the target", "pixel-lighting");
539
+ n(l), e(this.layer), this.lights.forEach(T), this.#N(o, s.format), this.#k.upload(o, d), this.#C = void 0;
540
+ let f = Math.max(1, d.length) * M;
541
+ (!this.#y || this.#b.length < f) && (this.#y?.destroy(), this.#b = new Float32Array(f * 2), this.#y = o.createBuffer({
542
+ label: "cookie-lighting2d",
543
+ size: this.#b.byteLength,
544
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
545
+ }));
546
+ let p = this.#i, m = p.length / 4, h = Math.max(16, p.byteLength);
547
+ if ((!this.#x || this.#x.size < h) && (this.#x?.destroy(), this.#x = o.createBuffer({
548
+ label: "cookie-lighting2d edges",
549
+ size: h * 2,
550
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
551
+ }), this.#a = !1), this.#a ||= (p.length ? o.queue.writeBuffer(this.#x, 0, p) : o.queue.writeBuffer(this.#x, 0, /* @__PURE__ */ new Float32Array(4)), !0), this.#C = this.#n.get(o, "light", this.#l, [
552
+ {
553
+ binding: 0,
554
+ resource: {
555
+ buffer: this.#y,
556
+ size: 176
557
+ }
558
+ },
559
+ {
560
+ binding: 1,
561
+ resource: i(c ?? this.#S)
562
+ },
563
+ {
564
+ binding: 4,
565
+ resource: { buffer: this.#k.buffer }
566
+ },
567
+ {
568
+ binding: 2,
569
+ resource: { buffer: this.#x }
570
+ }
571
+ ]), !this.#g || this.#g.width !== s.width || this.#g.height !== s.height) {
572
+ this.#g?.destroy();
573
+ let e = [s.width, s.height];
574
+ this.#g = o.createTexture({
575
+ label: "cookie-lighting2d sum",
576
+ size: [
577
+ e[0],
578
+ e[1],
579
+ j
580
+ ],
581
+ format: A,
582
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
583
+ }), this.#_ = this.#g.createView({ dimension: "2d-array" }), this.#v = Array.from({ length: j }, (e, t) => this.#g.createView({
584
+ dimension: "2d",
585
+ baseArrayLayer: t,
586
+ arrayLayerCount: 1
587
+ })), this.#h = void 0;
588
+ }
589
+ this.#h ??= o.createBindGroup({
590
+ layout: this.#m.getBindGroupLayout(0),
591
+ entries: [{
592
+ binding: 3,
593
+ resource: this.#_
594
+ }]
595
+ });
596
+ let _ = this.#b;
597
+ d.forEach((e, t) => {
598
+ let n = g(e.color), r = e.kind === "directional", i = r ? E(e) : [
599
+ e.x,
600
+ e.y,
601
+ e.height
602
+ ], a = [
603
+ "zero",
604
+ "clamp",
605
+ "repeat",
606
+ "mirror"
607
+ ].indexOf(e.cookieWrap), o = [
608
+ "multiply",
609
+ "alpha",
610
+ "color"
611
+ ].indexOf(e.cookieMode);
612
+ _.set([
613
+ l.x,
614
+ l.y,
615
+ l.zoom,
616
+ Math.cos(e.cone),
617
+ s.width,
618
+ s.height,
619
+ +!!c,
620
+ +(e.kind === "spot"),
621
+ i[0],
622
+ i[1],
623
+ i[2],
624
+ +!r,
625
+ Math.cos(e.angle),
626
+ Math.sin(e.angle),
627
+ Math.cos(e.cone * (1 - e.softness)),
628
+ e.falloff,
629
+ n[0] * e.intensity,
630
+ n[1] * e.intensity,
631
+ n[2] * e.intensity,
632
+ e.radius,
633
+ e.cookieScale[0],
634
+ e.cookieScale[1],
635
+ e.cookieOffset[0],
636
+ e.cookieOffset[1],
637
+ Math.cos(e.cookieRotation),
638
+ -Math.sin(e.cookieRotation),
639
+ a >= 0 ? a : 0,
640
+ o >= 0 ? o : 0,
641
+ +!!e.cookie,
642
+ m,
643
+ +!!e.shadows,
644
+ e.intensity,
645
+ e.x,
646
+ e.y,
647
+ 0,
648
+ 0,
649
+ ...this.#k.fields[t]
650
+ ], t * M);
651
+ }), o.queue.writeBuffer(this.#y, 0, _), this.#v.forEach((e, t) => a.encoder.beginRenderPass({ colorAttachments: [{
652
+ view: e,
653
+ loadOp: "clear",
654
+ clearValue: t ? [
655
+ 0,
656
+ 0,
657
+ 0,
658
+ 1
659
+ ] : [
660
+ u[0],
661
+ u[1],
662
+ u[2],
663
+ 1
664
+ ],
665
+ storeOp: "store"
666
+ }] }).end()), d.forEach((e, t) => {
667
+ let n = this.#E;
668
+ if (e.cookie) {
669
+ let t = this.#O.get(e.cookie);
670
+ if (!t) {
671
+ let n = this.#M(o, e.cookie);
672
+ t = o.createBindGroup({
673
+ layout: this.#u,
674
+ entries: [{
675
+ binding: 0,
676
+ resource: n.createView()
677
+ }, {
678
+ binding: 1,
679
+ resource: this.#T
680
+ }]
681
+ }), this.#O.set(e.cookie, t);
682
+ }
683
+ n = t;
684
+ }
685
+ let r = a.encoder.beginRenderPass({ colorAttachments: [{
686
+ view: this.#v[t % j],
687
+ loadOp: "load",
688
+ storeOp: "store"
689
+ }] });
690
+ r.setBindGroup(0, this.#C, [t * k]), r.setBindGroup(1, n), r.setPipeline(this.#p), r.draw(3), r.end();
691
+ });
692
+ let v = a.encoder.beginRenderPass({ colorAttachments: [{
693
+ view: i(s),
694
+ loadOp: "clear",
695
+ storeOp: "store"
696
+ }] });
697
+ v.setBindGroup(0, this.#h), v.setPipeline(this.#m), v.draw(3), v.end();
698
+ }
699
+ };
700
+ //#endregion
701
+ export { w as CookieLight2D, P as CookieLighting2D, v as CookieTexture2D, D as cookieLightAt, C as cookieSample, y as createCookieTexture, x as createSpotCookie, b as createWindowCookie };