@nova-lab/core 1.2.1-dev.258 → 1.2.2-dev.272

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 (30) hide show
  1. package/README.md +55 -0
  2. package/dist/2d/effects.js +165 -404
  3. package/dist/2d/lighting.d.ts +2 -0
  4. package/dist/2d/lighting.js +483 -227
  5. package/dist/2d/lottie.js +1 -1
  6. package/dist/2d/meshes.js +2 -2
  7. package/dist/2d/render-textures.js +3 -537
  8. package/dist/2d.js +3 -3
  9. package/dist/{NovaNode-CBZ-Ol-W.js → NovaNode-CshmSV9Y.js} +21 -21
  10. package/dist/{NovaRenderCommandWriter-CWQRa42y.js → NovaRenderCommandWriter-DeHyKjke.js} +272 -251
  11. package/dist/NovaRenderTexture-Desjl5-m.js +539 -0
  12. package/dist/{NovaTextSelectionService-Bdi0AqZ5.js → NovaTextSelectionService-CWJKn7TW.js} +3658 -3216
  13. package/dist/{NovaWebGLProgram-BO250QNF.js → NovaWebGLProgram-ytnblA5d.js} +28 -25
  14. package/dist/core.js +3 -3
  15. package/dist/model/lighting/LightingFields2D.d.ts +35 -0
  16. package/dist/model/lighting/LightingRenderer2D.d.ts +11 -18
  17. package/dist/model/lighting/LightingScene2D.d.ts +43 -0
  18. package/dist/model/lighting/NovaLighting2D.d.ts +18 -33
  19. package/dist/model/lighting/NovaLightingEffect2D.d.ts +46 -0
  20. package/dist/model/lighting/lighting-effect.d.ts +24 -0
  21. package/dist/model/lighting/lighting-shaders.d.ts +2 -2
  22. package/dist/model/lighting/lighting.types.d.ts +5 -1
  23. package/dist/model/render/backends/webgl/NovaWebGLCompositionPass.d.ts +2 -1
  24. package/dist/model/render/backends/webgl/NovaWebGLFrameRenderer.d.ts +29 -3
  25. package/dist/model/render/backends/webgl/NovaWebGLPathClipPass.d.ts +3 -2
  26. package/dist/model/render/backends/webgl/NovaWebGLProgram.d.ts +1 -1
  27. package/dist/model/render/graph/nova-render-dirty-items.d.ts +12 -0
  28. package/dist/model/runtime/cursor/NovaCursorManager.d.ts +7 -0
  29. package/dist/shader-effect-CHXZBHrO.js +247 -0
  30. package/package.json +2 -2
@@ -1,105 +1,112 @@
1
- function d(o, e, s = -1e7, t = 1e7) {
2
- if (!Number.isFinite(o) || o < s || o > t)
3
- throw new RangeError(`[Nova2D lighting] ${e} must be finite in ${s}..${t}`);
4
- return o;
1
+ import { a as X } from "../NovaRenderTexture-Desjl5-m.js";
2
+ import { a as R } from "../NovaAssetRegistry-p2QlCwvT.js";
3
+ import { d as B } from "../shader-effect-CHXZBHrO.js";
4
+ function f(a, e, t = -1e7, s = 1e7) {
5
+ if (!Number.isFinite(a) || a < t || a > s)
6
+ throw new RangeError(`[Nova2D lighting] ${e} must be finite in ${t}..${s}`);
7
+ return a;
5
8
  }
6
- function A(o) {
7
- return Object.freeze({ x: d(o.x, "bounds.x"), y: d(o.y, "bounds.y"), width: d(o.width, "bounds.width", 1e-3), height: d(o.height, "bounds.height", 1e-3) });
9
+ function P(a) {
10
+ return Object.freeze({ x: f(a.x, "bounds.x"), y: f(a.y, "bounds.y"), width: f(a.width, "bounds.width", 1e-3), height: f(a.height, "bounds.height", 1e-3) });
8
11
  }
9
- function M(o, e = !1) {
10
- if (o?.length !== 3)
12
+ function F(a, e = !1) {
13
+ if (a?.length !== 3)
11
14
  throw new TypeError("[Nova2D lighting] Expected an XYZ tuple");
12
- const s = o.map((r, i) => d(r, `vector[${i}]`)), t = Math.hypot(...s);
13
- if (e && t < 1e-5)
15
+ const t = a.map((r, i) => f(r, `vector[${i}]`)), s = Math.hypot(...t);
16
+ if (e && s < 1e-5)
14
17
  throw new RangeError("[Nova2D lighting] Direction cannot be zero");
15
- return Object.freeze(e ? s.map((r) => r / t) : s);
18
+ return Object.freeze(e ? t.map((r) => r / s) : t);
16
19
  }
17
- function D(o = 255) {
18
- if (!Number.isInteger(o))
20
+ function N(a = 255) {
21
+ if (!Number.isInteger(a))
19
22
  throw new RangeError("[Nova2D lighting] Layers must be an eight-bit integer");
20
- return d(o, "layers", 0, 255);
23
+ return f(a, "layers", 0, 255);
21
24
  }
22
- function N(o = "#ffffff") {
25
+ function V(a = "#ffffff") {
23
26
  let e;
24
- if (typeof o == "string") {
25
- if (!/^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(o))
27
+ if (typeof a == "string") {
28
+ if (!/^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(a))
26
29
  throw new TypeError("[Nova2D lighting] Color must be #RGB, #RRGGBB or an RGB tuple");
27
- const s = o.slice(1).split("").map((t) => o.length === 4 ? t + t : t).join("");
28
- e = [0, 2, 4].map((t) => Number.parseInt(s.slice(t, t + 2), 16) / 255);
30
+ const t = a.slice(1).split("").map((s) => a.length === 4 ? s + s : s).join("");
31
+ e = [0, 2, 4].map((s) => Number.parseInt(t.slice(s, s + 2), 16) / 255);
29
32
  } else {
30
- if (o.length !== 3)
33
+ if (a.length !== 3)
31
34
  throw new TypeError("[Nova2D lighting] Color needs three channels");
32
- e = o.map((s) => d(s, "color", 0, 1));
35
+ e = a.map((t) => f(t, "color", 0, 1));
33
36
  }
34
37
  return Object.freeze([...e]);
35
38
  }
36
- function C(o, e) {
37
- const s = {
39
+ function I(a, e) {
40
+ const t = {
38
41
  enabled: e.enabled ?? !0,
39
- color: N(e.color),
40
- intensity: d(e.intensity ?? 1, "intensity", 0, 16),
41
- layers: D(e.layers),
42
- shadows: Object.freeze({ enabled: e.shadows?.enabled ?? !1, opacity: d(e.shadows?.opacity ?? 1, "shadow opacity", 0, 1) })
43
- }, t = s.color.map((l) => l <= 0.04045 ? l / 12.92 : ((l + 0.055) / 1.055) ** 2.4);
44
- if (o === "directional") {
45
- const l = e, u = M(l.direction, !0);
46
- if (u[2] >= -1e-5)
42
+ color: V(e.color),
43
+ intensity: f(e.intensity ?? 1, "intensity", 0, 16),
44
+ layers: N(e.layers),
45
+ shadows: Object.freeze({ enabled: e.shadows?.enabled ?? !1, opacity: f(e.shadows?.opacity ?? 1, "shadow opacity", 0, 1) })
46
+ }, s = t.color.map((o) => o <= 0.04045 ? o / 12.92 : ((o + 0.055) / 1.055) ** 2.4);
47
+ if (a === "directional") {
48
+ const o = e, g = F(o.direction, !0);
49
+ if (g[2] >= -1e-5)
47
50
  throw new RangeError("[Nova2D lighting] Directional light must point down towards the ground");
48
- const _ = d(l.angularRadius ?? 0, "angularRadius", 0, 0.5), v = d(l.shadowDistance ?? 1e4, "shadowDistance", 1);
49
- return { kind: o, color: t, position: [0, 0, 0], direction: u, range: v, radius: _, inner: 1, outer: 0, falloff: 2, options: Object.freeze({ ...s, direction: u, angularRadius: _, shadowDistance: v }) };
51
+ const u = f(o.angularRadius ?? 0, "angularRadius", 0, 0.5), m = f(o.shadowDistance ?? 1e4, "shadowDistance", 1);
52
+ return { kind: a, color: s, position: [0, 0, 0], direction: g, range: m, radius: u, inner: 1, outer: 0, falloff: 2, options: Object.freeze({ ...t, direction: g, angularRadius: u, shadowDistance: m }) };
50
53
  }
51
- const r = e, i = M(r.position), n = d(r.range, "range", 1e-3), h = d(r.sourceRadius ?? 0, "sourceRadius", 0, n), g = d(r.falloff ?? 2, "falloff", 0.1, 16), c = { ...s, position: i, range: n, sourceRadius: h, falloff: g };
52
- let f = [0, 0, -1], m = 1, a = 0;
53
- if (o === "spot") {
54
- const l = e;
55
- return f = M(l.direction, !0), d(l.outerConeAngle, "outerConeAngle", 1e-3, Math.PI / 2), d(l.innerConeAngle, "innerConeAngle", 0, l.outerConeAngle - 1e-5), m = Math.cos(l.innerConeAngle), a = Math.cos(l.outerConeAngle), { kind: o, color: t, position: i, direction: f, range: n, radius: h, inner: m, outer: a, falloff: g, options: Object.freeze({ ...c, direction: f, innerConeAngle: l.innerConeAngle, outerConeAngle: l.outerConeAngle }) };
54
+ const r = e, i = F(r.position), n = f(r.range, "range", 1e-3), h = f(r.sourceRadius ?? 0, "sourceRadius", 0, n), l = f(r.falloff ?? 2, "falloff", 0.1, 16), c = { ...t, position: i, range: n, sourceRadius: h, falloff: l };
55
+ let d = [0, 0, -1], _ = 1, p = 0;
56
+ if (a === "spot") {
57
+ const o = e;
58
+ return d = F(o.direction, !0), f(o.outerConeAngle, "outerConeAngle", 1e-3, Math.PI / 2), f(o.innerConeAngle, "innerConeAngle", 0, o.outerConeAngle - 1e-5), _ = Math.cos(o.innerConeAngle), p = Math.cos(o.outerConeAngle), { kind: a, color: s, position: i, direction: d, range: n, radius: h, inner: _, outer: p, falloff: l, options: Object.freeze({ ...c, direction: d, innerConeAngle: o.innerConeAngle, outerConeAngle: o.outerConeAngle }) };
56
59
  }
57
- return { kind: o, color: t, position: i, direction: f, range: n, radius: h, inner: m, outer: a, falloff: g, options: Object.freeze(c) };
60
+ return { kind: a, color: s, position: i, direction: d, range: n, radius: h, inner: _, outer: p, falloff: l, options: Object.freeze(c) };
58
61
  }
59
- function z(o, e) {
60
- if (!o.polygon || o.polygon.length < 3 || o.polygon.length > 8)
62
+ function U(a, e) {
63
+ if (!a.polygon || a.polygon.length < 3 || a.polygon.length > 8)
61
64
  throw new RangeError("[Nova2D lighting] Occluder needs 3..8 convex vertices");
62
- const s = o.polygon.map(([h, g]) => [d(h, "polygon.x"), d(g, "polygon.y")]);
63
- let t = 0, r = 0;
64
- for (let h = 0; h < s.length; h++) {
65
- const g = s[h], c = s[(h + 1) % s.length], f = s[(h + 2) % s.length], m = (c[0] - g[0]) * (f[1] - c[1]) - (c[1] - g[1]) * (f[0] - c[0]);
66
- if (Math.abs(m) < 1e-5 || r && Math.sign(m) !== r)
65
+ const t = a.polygon.map(([h, l]) => [f(h, "polygon.x"), f(l, "polygon.y")]);
66
+ let s = 0, r = 0;
67
+ for (let h = 0; h < t.length; h++) {
68
+ const l = t[h], c = t[(h + 1) % t.length], d = t[(h + 2) % t.length], _ = (c[0] - l[0]) * (d[1] - c[1]) - (c[1] - l[1]) * (d[0] - c[0]);
69
+ if (Math.abs(_) < 1e-5 || r && Math.sign(_) !== r)
67
70
  throw new RangeError("[Nova2D lighting] Footprint must be strictly convex without duplicate vertices");
68
- r = Math.sign(m), t += g[0] * c[1] - c[0] * g[1];
71
+ r = Math.sign(_), s += l[0] * c[1] - c[0] * l[1];
69
72
  }
70
- for (let h = 0; h < s.length; h++) {
71
- const g = s[h], c = s[(h + 1) % s.length];
72
- if (s.some((f) => ((c[0] - g[0]) * (f[1] - g[1]) - (c[1] - g[1]) * (f[0] - g[0])) * r < -1e-5))
73
+ for (let h = 0; h < t.length; h++) {
74
+ const l = t[h], c = t[(h + 1) % t.length];
75
+ if (t.some((d) => ((c[0] - l[0]) * (d[1] - l[1]) - (c[1] - l[1]) * (d[0] - l[0])) * r < -1e-5))
73
76
  throw new RangeError("[Nova2D lighting] Footprint edges must enclose a convex polygon");
74
77
  }
75
- t < 0 && s.reverse();
76
- const i = d(o.baseHeight ?? 0, "baseHeight", 0, e), n = d(o.height, "height", 1e-3, e - i);
77
- return Object.freeze({ ...o, polygon: Object.freeze(s.map((h) => Object.freeze(h))), baseHeight: i, height: n, layers: D(o.layers), enabled: o.enabled ?? !0 });
78
+ s < 0 && t.reverse();
79
+ const i = f(a.baseHeight ?? 0, "baseHeight", 0, e), n = f(a.height, "height", 1e-3, e - i);
80
+ return Object.freeze({ ...a, polygon: Object.freeze(t.map((h) => Object.freeze(h))), baseHeight: i, height: n, layers: N(a.layers), enabled: a.enabled ?? !0 });
78
81
  }
79
- function O(o, e) {
80
- if (!o.image)
82
+ function j(a, e) {
83
+ if (!a.image)
81
84
  throw new TypeError("[Nova2D lighting] Receiver needs a borrowed alpha image");
82
- const s = d(o.baseHeight ?? 0, "baseHeight", 0, e);
83
- return Object.freeze({ ...o, bounds: A(o.bounds), baseHeight: s, height: d(o.height, "height", 0, e - s), layers: D(o.layers), enabled: o.enabled ?? !0, imageRevision: d(o.imageRevision ?? 0, "imageRevision", 0), order: o.order === void 0 ? void 0 : d(o.order, "receiver order"), unlit: o.unlit ?? !1 });
85
+ for (const s of ["normalMap", "lightMask"])
86
+ if (a[s] !== void 0 && (!a[s] || typeof a[s] != "object"))
87
+ throw new TypeError(`[Nova2D lighting] ${s} must be a borrowed CanvasImageSource`);
88
+ const t = f(a.baseHeight ?? 0, "baseHeight", 0, e);
89
+ return Object.freeze({ ...a, bounds: P(a.bounds), baseHeight: t, height: f(a.height, "height", 0, e - t), layers: N(a.layers), enabled: a.enabled ?? !0, imageRevision: f(a.imageRevision ?? 0, "imageRevision", 0), order: a.order === void 0 ? void 0 : f(a.order, "receiver order"), unlit: a.unlit ?? !1 });
84
90
  }
85
- const F = `#version 300 es
91
+ const W = `#version 300 es
86
92
  precision highp float;
87
93
  out vec2 uv;
88
94
  void main(){
89
95
  vec2 p=vec2(float((gl_VertexID<<1)&2),float(gl_VertexID&2));
90
96
  uv=vec2(p.x,1.-p.y);
91
97
  gl_Position=vec4(p*2.-1.,0.,1.);
92
- }`, L = `#version 300 es
98
+ }`, k = `#version 300 es
93
99
  precision highp float;
94
100
  precision highp int;
95
101
  in vec2 uv;
96
102
  out vec4 outColor;
97
103
  uniform sampler2D heights;
104
+ uniform sampler2D normals, lightMask;
98
105
  uniform highp sampler2D geometry;
99
106
  uniform vec4 bounds;
100
107
  uniform vec3 ambient;
101
- uniform float groundScale, maxHeight;
102
- uniform int lightCount, casterCount, categoricalField;
108
+ uniform float groundScale, maxHeight, irradianceScale;
109
+ uniform int lightCount, casterCount, categoricalField, surfaceField;
103
110
  uniform vec4 positions[8], colors[8], directions[8], cones[8], shadows[8];
104
111
  float cross2(vec2 a,vec2 b){return a.x*b.y-a.y*b.x;}
105
112
  bool blocked(vec3 p,vec3 end,int layers,int ignored){
@@ -136,12 +143,16 @@ void main(){
136
143
  vec4 field=texture(heights,uv);
137
144
  ivec2 size=textureSize(heights,0);
138
145
  vec4 material=categoricalField>0?texelFetch(heights,clamp(ivec2(uv*vec2(size)),ivec2(0),size-1),0):field;
139
- if(material.b>(categoricalField>0?.99:.5)){outColor=vec4(vec3(64./255.),1.);return;}
146
+ if(material.b>(categoricalField>0?.99:.5)){outColor=vec4(vec3(irradianceScale),1.);return;}
140
147
  float z=field.r*maxHeight;
141
148
  int receiverLayers=int(round(material.g*255.));
142
149
  int ignored=categoricalField>0?int(round(material.b*255.))-1:-1;
143
150
  vec3 p=vec3(screen.x,(screen.y+z)/groundScale,z);
144
- vec3 light=ambient;
151
+ vec2 response=surfaceField>0?texture(lightMask,uv).rg:vec2(1.);
152
+ vec3 normal=surfaceField>0?clamp((texture(normals,uv).rgb*255.-128.)/127.,-1.,1.):vec3(0.);
153
+ bool hasNormal=length(normal)>.1;
154
+ if(hasNormal)normal=normalize(normal);
155
+ vec3 light=ambient*response.g;
145
156
  for(int i=0;i<8;i++){
146
157
  if(i>=lightCount)break;
147
158
  int layers=int(cones[i].z);
@@ -170,34 +181,135 @@ void main(){
170
181
  }
171
182
  amount*=mix(1.,visibility,shadows[i].x);
172
183
  }
173
- light+=colors[i].rgb*amount;
184
+ if(hasNormal){vec3 toLight=end-p;amount*=max(0.,dot(normal,toLight/max(length(toLight),.00001)));}
185
+ light+=colors[i].rgb*amount*response.r;
174
186
  }
175
187
  // 1 maps EXACTLY to byte 64: the neutral baseline does not introduce a seam.
176
- outColor=vec4(clamp(light*(64./255.),0.,1.),1.);
177
- }`, U = `#version 300 es
188
+ outColor=vec4(max(light,vec3(0.))*irradianceScale,1.);
189
+ }`, M = `#version 300 es
178
190
  precision highp float;
179
191
  in vec2 uv;
180
192
  out vec4 outColor;
181
193
  uniform sampler2D albedo, illumination;
194
+ uniform float irradianceScale;
182
195
  vec3 linearRGB(vec3 c){return mix(c/12.92,pow((c+.055)/1.055,vec3(2.4)),step(vec3(.04045),c));}
183
196
  vec3 srgb(vec3 c){return mix(c*12.92,1.055*pow(max(c,vec3(0.)),vec3(1./2.4))-.055,step(vec3(.0031308),c));}
184
197
  void main(){
185
198
  vec4 art=texture(albedo,uv);
186
- vec3 irradiance=texture(illumination,vec2(uv.x,1.-uv.y)).rgb*(255./64.);
199
+ vec3 irradiance=texture(illumination,vec2(uv.x,1.-uv.y)).rgb*irradianceScale;
187
200
  outColor=vec4(srgb(clamp(linearRGB(art.rgb)*irradiance,0.,1.)),art.a);
188
201
  }`;
189
- function E(o = 1, e = 1) {
190
- const s = document.createElement("canvas");
191
- return s.width = o, s.height = e, s;
202
+ function w(a = 1, e = 1) {
203
+ const t = document.createElement("canvas");
204
+ return t.width = a, t.height = e, t;
192
205
  }
193
- class P {
206
+ function A(a) {
207
+ for (const e of [a.image, a.normal, a.response])
208
+ e && (e.width = e.height = 0);
209
+ }
210
+ function H(a, e, t, s, r) {
211
+ const i = w(e, t), n = i.getContext("2d", { willReadFrequently: !0 });
212
+ n.fillStyle = s, n.fillRect(0, 0, e, t), a && n.drawImage(a, 0, 0, e, t);
213
+ const h = n.getImageData(0, 0, e, t);
214
+ for (let l = 3; l < h.data.length; l += 4)
215
+ h.data[l] = r[l];
216
+ return n.putImageData(h, 0, 0), i;
217
+ }
218
+ class G {
194
219
  constructor(e) {
195
220
  this._options = e;
196
- const s = this.canvas.getContext("webgl2", { alpha: !0, premultipliedAlpha: !1, antialias: !1, depth: !1, stencil: !1 });
197
- if (!s)
221
+ }
222
+ _field = w();
223
+ _fieldContext = this._field.getContext("2d");
224
+ _receiverMasks = /* @__PURE__ */ new Map();
225
+ _normalField;
226
+ _responseField;
227
+ _surfaceField = !1;
228
+ _fieldKey = "";
229
+ _disposed = !1;
230
+ update(e, t, s) {
231
+ const { bounds: r, receivers: i } = e, n = (o) => o.bounds.x + o.bounds.width >= r.x && o.bounds.y + o.bounds.height >= r.y && o.bounds.x <= r.x + r.width && o.bounds.y <= r.y + r.height, h = i.some((o) => n(o) && (o.normalMap || o.lightMask)), l = `${t},${s},${e.receiverRevision},${Object.values(e.bounds).join(",")},${[...e.ignoredOccluders.values()].join(",")},${h}`;
232
+ if (l === this._fieldKey)
233
+ return !1;
234
+ const c = e.ignoredOccluders.size > 0 || h;
235
+ this._surfaceField = h, this._field.width = t, this._field.height = s;
236
+ const d = this._fieldContext;
237
+ d.fillStyle = "#00ff00", d.fillRect(0, 0, t, s);
238
+ const _ = [];
239
+ if (h) {
240
+ this._normalField ??= w(), this._responseField ??= w();
241
+ for (const [o, g] of [this._normalField, this._responseField].entries()) {
242
+ g.width = t, g.height = s;
243
+ const u = g.getContext("2d");
244
+ u.fillStyle = o ? "#ffff00" : "rgb(128,128,128)", u.fillRect(0, 0, t, s), u.scale(t / r.width, s / r.height), u.translate(-r.x, -r.y), _.push(u);
245
+ }
246
+ }
247
+ const p = new Set(i.filter(n));
248
+ for (const [o, g] of this._receiverMasks)
249
+ p.has(o) || (A(g), this._receiverMasks.delete(o));
250
+ d.save(), d.scale(t / r.width, s / r.height), d.translate(-r.x, -r.y), d.imageSmoothingEnabled = !c;
251
+ for (const o of i) {
252
+ const g = o.bounds;
253
+ if (!o.enabled || !n(o))
254
+ continue;
255
+ const u = e.ignoredOccluders.get(o) ?? 0;
256
+ let m = this._receiverMasks.get(o);
257
+ if (m && (m.ignored !== u || m.categorical !== c || m.surface !== h) && (A(m), this._receiverMasks.delete(o), m = void 0), !m) {
258
+ const v = this._options.maxTextureSize, E = Math.min(1, v / Math.max(g.width, g.height));
259
+ if ([...this._receiverMasks.values()].reduce((x, y) => x + y.image.width * y.image.height * (y.surface ? 3 : 1), 0) + Math.max(1, Math.ceil(g.width * E)) * Math.max(1, Math.ceil(g.height * E)) * (h ? 3 : 1) > 4194304)
260
+ throw new RangeError("[Nova2D lighting] Visible receiver masks exceed the 16MiB budget; reduce their bounds or render a smaller region");
261
+ const b = w(Math.max(1, Math.ceil(g.width * E)), Math.max(1, Math.ceil(g.height * E)));
262
+ m = { image: b, ignored: u, categorical: c, surface: h };
263
+ const T = b.getContext("2d", { willReadFrequently: c }), C = T.createLinearGradient(0, 0, 0, b.height), O = (x) => `rgb(${Math.round(x / this._options.maxHeight * 255)},${o.layers},${o.unlit ? 255 : u})`;
264
+ if (C.addColorStop(0, O(o.baseHeight + o.height)), C.addColorStop(1, O(o.baseHeight)), T.fillStyle = C, T.fillRect(0, 0, b.width, b.height), T.globalCompositeOperation = "destination-in", T.drawImage(o.image, 0, 0, b.width, b.height), c) {
265
+ const x = T.getImageData(0, 0, b.width, b.height);
266
+ for (let y = 0; y < x.data.length; y += 4)
267
+ x.data[y + 1] = o.layers, x.data[y + 2] = o.unlit ? 255 : u, x.data[y + 3] = x.data[y + 3] >= 128 ? 255 : 0;
268
+ T.putImageData(x, 0, 0), h && (m.normal = H(o.normalMap, b.width, b.height, "rgb(128,128,128)", x.data), m.response = H(o.lightMask, b.width, b.height, "#ffff00", x.data));
269
+ }
270
+ this._receiverMasks.set(o, m);
271
+ }
272
+ d.drawImage(m.image, g.x, g.y, g.width, g.height), h && (_[0].drawImage(m.normal, g.x, g.y, g.width, g.height), _[1].drawImage(m.response, g.x, g.y, g.width, g.height));
273
+ }
274
+ return d.restore(), this._fieldKey = l, !0;
275
+ }
276
+ invalidate() {
277
+ this._fieldKey = "";
278
+ }
279
+ get image() {
280
+ return this._field;
281
+ }
282
+ get normal() {
283
+ return this._normalField;
284
+ }
285
+ get response() {
286
+ return this._responseField;
287
+ }
288
+ get surface() {
289
+ return this._surfaceField;
290
+ }
291
+ get state() {
292
+ return { receiverMasks: this._receiverMasks.size, ownedCanvases: this._disposed ? 0 : 1 + +!!this._normalField + +!!this._responseField + [...this._receiverMasks.values()].reduce((e, t) => e + (t.surface ? 3 : 1), 0) };
293
+ }
294
+ dispose() {
295
+ if (!this._disposed) {
296
+ this._disposed = !0;
297
+ for (const e of this._receiverMasks.values())
298
+ A(e);
299
+ this._receiverMasks.clear();
300
+ for (const e of [this._field, this._normalField, this._responseField])
301
+ e && (e.width = e.height = 0);
302
+ }
303
+ }
304
+ }
305
+ class q {
306
+ constructor(e) {
307
+ this._options = e, this._fields = new G(this._options);
308
+ const t = this.canvas.getContext("webgl2", { alpha: !0, premultipliedAlpha: !1, antialias: !1, depth: !1, stencil: !1 });
309
+ if (!t)
198
310
  throw new Error("[Nova2D lighting] WebGL2 is required");
199
- this._gl = s, this.canvas.addEventListener("webglcontextlost", (t) => {
200
- t.preventDefault(), this._lost = !0;
311
+ this._gl = t, this.canvas.addEventListener("webglcontextlost", (s) => {
312
+ s.preventDefault(), this._lost = !0;
201
313
  }, { signal: this._lifetime.signal }), this.canvas.addEventListener("webglcontextrestored", () => {
202
314
  if (!this._disposed) {
203
315
  this._lost = !0;
@@ -210,16 +322,15 @@ class P {
210
322
  }, { signal: this._lifetime.signal });
211
323
  try {
212
324
  this._initialize();
213
- } catch (t) {
214
- throw this.dispose(), t;
325
+ } catch (s) {
326
+ throw this.dispose(), s;
215
327
  }
216
328
  }
217
- canvas = E();
218
- _field = E();
219
- _source = E();
220
- _fieldContext = this._field.getContext("2d");
329
+ canvas = w();
330
+ _source = w();
221
331
  _sourceContext = this._source.getContext("2d");
222
- _receiverMasks = /* @__PURE__ */ new Map();
332
+ _fields;
333
+ _surfaceField = !1;
223
334
  _lifetime = new AbortController();
224
335
  _gl;
225
336
  _textures = [];
@@ -227,162 +338,141 @@ class P {
227
338
  _shaders = [];
228
339
  _framebuffer;
229
340
  _uniforms = /* @__PURE__ */ new Map();
230
- _fieldKey = "";
231
341
  _lost = !1;
232
342
  _disposed = !1;
233
343
  _renders = 0;
234
344
  _generation = 0;
235
345
  _lightWidth = 0;
236
346
  _lightHeight = 0;
347
+ _lightBufferComplete = !1;
348
+ _floatAvailable = !1;
349
+ _lightFloat = !1;
237
350
  _geometryValid = !1;
238
351
  _lastOccluders = [];
239
352
  _initialize() {
240
353
  const e = this._gl;
241
- this._textures = [], this._programs = [], this._shaders = [], this._uniforms.clear();
242
- const s = e.createShader(e.VERTEX_SHADER);
243
- if (this._shaders.push(s), e.shaderSource(s, F), e.compileShader(s), !e.getShaderParameter(s, e.COMPILE_STATUS))
244
- throw new Error(e.getShaderInfoLog(s) ?? "[Nova2D lighting] Vertex compile failed");
245
- for (const t of [L, U]) {
354
+ this._floatAvailable = !!e.getExtension("EXT_color_buffer_float"), this._textures = [], this._programs = [], this._shaders = [], this._uniforms.clear();
355
+ const t = e.createShader(e.VERTEX_SHADER);
356
+ if (this._shaders.push(t), e.shaderSource(t, W), e.compileShader(t), !e.getShaderParameter(t, e.COMPILE_STATUS))
357
+ throw new Error(e.getShaderInfoLog(t) ?? "[Nova2D lighting] Vertex compile failed");
358
+ for (const s of [k, M]) {
246
359
  const r = e.createShader(e.FRAGMENT_SHADER), i = e.createProgram();
247
- if (this._shaders.push(r), this._programs.push(i), e.shaderSource(r, t), e.compileShader(r), !e.getShaderParameter(r, e.COMPILE_STATUS))
360
+ if (this._shaders.push(r), this._programs.push(i), e.shaderSource(r, s), e.compileShader(r), !e.getShaderParameter(r, e.COMPILE_STATUS))
248
361
  throw new Error(e.getShaderInfoLog(r) ?? "[Nova2D lighting] Fragment compile failed");
249
- if (e.attachShader(i, s), e.attachShader(i, r), e.linkProgram(i), !e.getProgramParameter(i, e.LINK_STATUS))
362
+ if (e.attachShader(i, t), e.attachShader(i, r), e.linkProgram(i), !e.getProgramParameter(i, e.LINK_STATUS))
250
363
  throw new Error(e.getProgramInfoLog(i) ?? "[Nova2D lighting] Program link failed");
251
364
  this._uniforms.set(i, /* @__PURE__ */ new Map());
252
365
  }
253
- for (let t = 0; t < 4; t++) {
366
+ for (let s = 0; s < 4; s++) {
254
367
  const r = e.createTexture();
255
- this._textures.push(r), e.activeTexture(e.TEXTURE0 + t), e.bindTexture(e.TEXTURE_2D, r), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, t === 2 ? e.NEAREST : e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, t === 2 ? e.NEAREST : e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE);
368
+ this._textures.push(r), e.activeTexture(e.TEXTURE0 + s), e.bindTexture(e.TEXTURE_2D, r), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, s === 2 ? e.NEAREST : e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, s === 2 ? e.NEAREST : e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE);
256
369
  }
257
- this._framebuffer = e.createFramebuffer(), e.disable(e.DEPTH_TEST), e.disable(e.BLEND), e.pixelStorei(e.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !1), e.pixelStorei(e.UNPACK_COLORSPACE_CONVERSION_WEBGL, e.NONE), this._fieldKey = "", this._lightWidth = this._lightHeight = 0, this._geometryValid = !1, this._lastOccluders = [], this._generation++;
370
+ this._framebuffer = e.createFramebuffer(), e.disable(e.DEPTH_TEST), e.disable(e.BLEND), e.pixelStorei(e.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !1), e.pixelStorei(e.UNPACK_COLORSPACE_CONVERSION_WEBGL, e.NONE), this._fields.invalidate(), this._lightWidth = this._lightHeight = 0, this._lightBufferComplete = !1, this._lightFloat = !1, this._geometryValid = !1, this._lastOccluders = [], this._generation++;
258
371
  }
259
- _uniform(e, s) {
260
- const t = this._uniforms.get(e);
261
- return t.has(s) || t.set(s, this._gl.getUniformLocation(e, s)), t.get(s);
372
+ _uniform(e, t) {
373
+ const s = this._uniforms.get(e);
374
+ return s.has(t) || s.set(t, this._gl.getUniformLocation(e, t)), s.get(t);
262
375
  }
263
376
  _bind(e) {
264
377
  this._gl.activeTexture(this._gl.TEXTURE0 + e), this._gl.bindTexture(this._gl.TEXTURE_2D, this._textures[e]);
265
378
  }
266
- _uploadField(e, s, t) {
267
- const r = `${s},${t},${e.receiverRevision},${Object.values(e.bounds).join(",")},${[...e.ignoredOccluders.values()].join(",")}`;
268
- if (r === this._fieldKey)
379
+ _uploadField(e, t, s) {
380
+ if (!this._fields.update(e, t, s))
269
381
  return;
270
- const { bounds: i, receivers: n } = e, h = e.ignoredOccluders.size > 0;
271
- this._field.width = s, this._field.height = t;
272
- const g = this._fieldContext;
273
- g.fillStyle = "#00ff00", g.fillRect(0, 0, s, t);
274
- const c = (a) => a.bounds.x + a.bounds.width >= i.x && a.bounds.y + a.bounds.height >= i.y && a.bounds.x <= i.x + i.width && a.bounds.y <= i.y + i.height, f = new Set(n.filter(c));
275
- for (const [a, l] of this._receiverMasks)
276
- f.has(a) || (l.image.width = l.image.height = 0, this._receiverMasks.delete(a));
277
- g.save(), g.scale(s / i.width, t / i.height), g.translate(-i.x, -i.y), g.imageSmoothingEnabled = !h;
278
- for (const a of n) {
279
- const l = a.bounds;
280
- if (!a.enabled || !c(a))
281
- continue;
282
- const u = e.ignoredOccluders.get(a) ?? 0;
283
- let _ = this._receiverMasks.get(a);
284
- if (_ && (_.ignored !== u || _.categorical !== h) && (_.image.width = _.image.height = 0, this._receiverMasks.delete(a), _ = void 0), !_) {
285
- const v = this._options.maxTextureSize, x = Math.min(1, v / Math.max(l.width, l.height));
286
- if ([...this._receiverMasks.values()].reduce((b, w) => b + w.image.width * w.image.height, 0) + Math.ceil(l.width * x) * Math.ceil(l.height * x) > 4194304)
287
- throw new RangeError("[Nova2D lighting] Visible receiver masks exceed the 16MiB budget; reduce their bounds or render a smaller region");
288
- const p = E(Math.max(1, Math.ceil(l.width * x)), Math.max(1, Math.ceil(l.height * x)));
289
- _ = { image: p, ignored: u, categorical: h };
290
- const y = p.getContext("2d", { willReadFrequently: h }), T = y.createLinearGradient(0, 0, 0, p.height), S = (b) => `rgb(${Math.round(b / this._options.maxHeight * 255)},${a.layers},${a.unlit ? 255 : u})`;
291
- if (T.addColorStop(0, S(a.baseHeight + a.height)), T.addColorStop(1, S(a.baseHeight)), y.fillStyle = T, y.fillRect(0, 0, p.width, p.height), y.globalCompositeOperation = "destination-in", y.drawImage(a.image, 0, 0, p.width, p.height), h) {
292
- const b = y.getImageData(0, 0, p.width, p.height);
293
- for (let w = 0; w < b.data.length; w += 4)
294
- b.data[w + 1] = a.layers, b.data[w + 2] = a.unlit ? 255 : u, b.data[w + 3] = b.data[w + 3] >= 128 ? 255 : 0;
295
- y.putImageData(b, 0, 0);
296
- }
297
- this._receiverMasks.set(a, _);
382
+ const r = this._fields.surface;
383
+ this._surfaceField = r, this._bind(1);
384
+ const i = this._gl;
385
+ if (i.texImage2D(i.TEXTURE_2D, 0, i.RGBA, i.RGBA, i.UNSIGNED_BYTE, this._fields.image), r) {
386
+ for (; this._textures.length < 6; ) {
387
+ const n = this._textures.length, h = i.createTexture();
388
+ this._textures.push(h), this._bind(n), i.texParameteri(i.TEXTURE_2D, i.TEXTURE_MIN_FILTER, i.LINEAR), i.texParameteri(i.TEXTURE_2D, i.TEXTURE_MAG_FILTER, i.LINEAR), i.texParameteri(i.TEXTURE_2D, i.TEXTURE_WRAP_S, i.CLAMP_TO_EDGE), i.texParameteri(i.TEXTURE_2D, i.TEXTURE_WRAP_T, i.CLAMP_TO_EDGE);
298
389
  }
299
- g.drawImage(_.image, l.x, l.y, l.width, l.height);
390
+ for (const [n, h] of [this._fields.normal, this._fields.response].entries())
391
+ this._bind(4 + n), i.texImage2D(i.TEXTURE_2D, 0, i.RGBA, i.RGBA, i.UNSIGNED_BYTE, h);
300
392
  }
301
- g.restore(), this._bind(1);
302
- const m = this._gl;
303
- m.texImage2D(m.TEXTURE_2D, 0, m.RGBA, m.RGBA, m.UNSIGNED_BYTE, this._field), this._fieldKey = r;
304
393
  }
305
- render(e, s, t, r) {
394
+ render(e, t, s, r) {
306
395
  if (this._disposed)
307
396
  throw new Error("[Nova2D lighting] Renderer is disposed");
308
397
  if (!this.available)
309
398
  return;
310
399
  const i = this._gl;
311
- (this.canvas.width !== s || this.canvas.height !== t) && (this.canvas.width = s, this.canvas.height = t);
400
+ (this.canvas.width !== t || this.canvas.height !== s) && (this.canvas.width = t, this.canvas.height = s);
312
401
  const n = Math.max(1, Math.min(this._options.maxTextureSize, Math.ceil(r.bounds.width * this._options.resolution))), h = Math.max(1, Math.min(this._options.maxTextureSize, Math.ceil(r.bounds.height * this._options.resolution)));
313
- if (this._uploadField(r, n, h), this._bind(0), e !== this._source && ((this._source.width !== s || this._source.height !== t) && (this._source.width = s, this._source.height = t), this._sourceContext.clearRect(0, 0, s, t), this._sourceContext.drawImage(e, 0, 0, s, t)), i.texImage2D(i.TEXTURE_2D, 0, i.RGBA, i.RGBA, i.UNSIGNED_BYTE, this._source), !this._geometryValid || r.occluders.length !== this._lastOccluders.length || r.occluders.some((a, l) => a !== this._lastOccluders[l])) {
314
- const a = new Float32Array(2560);
315
- for (const [l, u] of r.occluders.entries()) {
316
- const _ = l * 40;
317
- a.set([u.height, u.baseHeight, u.layers, u.polygon.length], _);
318
- for (const [v, x] of u.polygon.entries())
319
- a.set(x, _ + (v + 1) * 4);
320
- a.set([Math.min(...u.polygon.map((v) => v[0])), Math.min(...u.polygon.map((v) => v[1])), Math.max(...u.polygon.map((v) => v[0])), Math.max(...u.polygon.map((v) => v[1]))], _ + 36);
402
+ if (this._uploadField(r, n, h), this._bind(0), e !== this._source && ((this._source.width !== t || this._source.height !== s) && (this._source.width = t, this._source.height = s), this._sourceContext.clearRect(0, 0, t, s), this._sourceContext.drawImage(e, 0, 0, t, s)), i.texImage2D(i.TEXTURE_2D, 0, i.RGBA, i.RGBA, i.UNSIGNED_BYTE, this._source), !this._geometryValid || r.occluders.length !== this._lastOccluders.length || r.occluders.some((o, g) => o !== this._lastOccluders[g])) {
403
+ const o = new Float32Array(2560);
404
+ for (const [g, u] of r.occluders.entries()) {
405
+ const m = g * 40;
406
+ o.set([u.height, u.baseHeight, u.layers, u.polygon.length], m);
407
+ for (const [v, E] of u.polygon.entries())
408
+ o.set(E, m + (v + 1) * 4);
409
+ o.set([Math.min(...u.polygon.map((v) => v[0])), Math.min(...u.polygon.map((v) => v[1])), Math.max(...u.polygon.map((v) => v[0])), Math.max(...u.polygon.map((v) => v[1]))], m + 36);
321
410
  }
322
- this._bind(2), i.texImage2D(i.TEXTURE_2D, 0, i.RGBA32F, 10, 64, 0, i.RGBA, i.FLOAT, a), this._lastOccluders = r.occluders, this._geometryValid = !0;
411
+ this._bind(2), i.texImage2D(i.TEXTURE_2D, 0, i.RGBA32F, 10, 64, 0, i.RGBA, i.FLOAT, o), this._lastOccluders = r.occluders, this._geometryValid = !0;
412
+ }
413
+ this._bind(3);
414
+ const c = this._surfaceField && this._floatAvailable;
415
+ if ((n !== this._lightWidth || h !== this._lightHeight || c !== this._lightFloat) && (this._lightBufferComplete = !1, i.texImage2D(i.TEXTURE_2D, 0, c ? i.RGBA16F : i.RGBA, n, h, 0, i.RGBA, c ? i.HALF_FLOAT : i.UNSIGNED_BYTE, null), this._lightWidth = n, this._lightHeight = h, this._lightFloat = c), i.bindFramebuffer(i.FRAMEBUFFER, this._framebuffer), i.framebufferTexture2D(i.FRAMEBUFFER, i.COLOR_ATTACHMENT0, i.TEXTURE_2D, this._textures[3], 0), !this._lightBufferComplete) {
416
+ if (i.checkFramebufferStatus(i.FRAMEBUFFER) !== i.FRAMEBUFFER_COMPLETE)
417
+ throw new Error("[Nova2D lighting] Incomplete light buffer");
418
+ this._lightBufferComplete = !0;
323
419
  }
324
- if (this._bind(3), (n !== this._lightWidth || h !== this._lightHeight) && (i.texImage2D(i.TEXTURE_2D, 0, i.RGBA, n, h, 0, i.RGBA, i.UNSIGNED_BYTE, null), this._lightWidth = n, this._lightHeight = h), i.bindFramebuffer(i.FRAMEBUFFER, this._framebuffer), i.framebufferTexture2D(i.FRAMEBUFFER, i.COLOR_ATTACHMENT0, i.TEXTURE_2D, this._textures[3], 0), i.checkFramebufferStatus(i.FRAMEBUFFER) !== i.FRAMEBUFFER_COMPLETE)
325
- throw new Error("[Nova2D lighting] Incomplete light buffer");
326
- const c = this._programs[0];
327
- i.useProgram(c), i.uniform1i(this._uniform(c, "heights"), 1), i.uniform1i(this._uniform(c, "categoricalField"), r.ignoredOccluders.size > 0 ? 1 : 0), i.uniform1i(this._uniform(c, "geometry"), 2), i.uniform4f(this._uniform(c, "bounds"), r.bounds.x, r.bounds.y, r.bounds.width, r.bounds.height), i.uniform3fv(this._uniform(c, "ambient"), r.ambient), i.uniform1f(this._uniform(c, "groundScale"), this._options.groundScale), i.uniform1f(this._uniform(c, "maxHeight"), this._options.maxHeight), i.uniform1i(this._uniform(c, "lightCount"), r.lights.length), i.uniform1i(this._uniform(c, "casterCount"), r.occluders.length);
328
- const f = Array.from({ length: 5 }, () => new Float32Array(32));
329
- r.lights.forEach((a, l) => {
330
- const u = l * 4;
331
- f[0].set([...a.position, a.kind === "directional" ? 0 : a.kind === "point" ? 1 : 2], u), f[1].set([...a.color, a.options.intensity], u), f[2].set([...a.direction, a.range], u), f[3].set([a.inner, a.outer, a.options.layers, a.falloff], u), f[4].set([a.options.shadows.enabled ? a.options.shadows.opacity : 0, a.radius, 0, a.range], u);
420
+ const d = this._programs[0];
421
+ i.useProgram(d), i.uniform1i(this._uniform(d, "heights"), 1), i.uniform1i(this._uniform(d, "categoricalField"), r.ignoredOccluders.size > 0 || this._surfaceField ? 1 : 0), i.uniform1i(this._uniform(d, "surfaceField"), this._surfaceField ? 1 : 0), i.uniform1i(this._uniform(d, "normals"), this._surfaceField ? 4 : 0), i.uniform1i(this._uniform(d, "lightMask"), this._surfaceField ? 5 : 0), i.uniform1i(this._uniform(d, "geometry"), 2), i.uniform4f(this._uniform(d, "bounds"), r.bounds.x, r.bounds.y, r.bounds.width, r.bounds.height), i.uniform3fv(this._uniform(d, "ambient"), r.ambient), i.uniform1f(this._uniform(d, "groundScale"), this._options.groundScale), i.uniform1f(this._uniform(d, "maxHeight"), this._options.maxHeight), i.uniform1f(this._uniform(d, "irradianceScale"), c ? 1 : 64 / 255), i.uniform1i(this._uniform(d, "lightCount"), r.lights.length), i.uniform1i(this._uniform(d, "casterCount"), r.occluders.length);
422
+ const _ = Array.from({ length: 5 }, () => new Float32Array(32));
423
+ r.lights.forEach((o, g) => {
424
+ const u = g * 4;
425
+ _[0].set([...o.position, o.kind === "directional" ? 0 : o.kind === "point" ? 1 : 2], u), _[1].set([...o.color, o.options.intensity], u), _[2].set([...o.direction, o.range], u), _[3].set([o.inner, o.outer, o.options.layers, o.falloff], u), _[4].set([o.options.shadows.enabled ? o.options.shadows.opacity : 0, o.radius, 0, o.range], u);
332
426
  });
333
- for (const [a, l] of ["positions", "colors", "directions", "cones", "shadows"].entries())
334
- i.uniform4fv(this._uniform(c, `${l}[0]`), f[a]);
427
+ for (const [o, g] of ["positions", "colors", "directions", "cones", "shadows"].entries())
428
+ i.uniform4fv(this._uniform(d, `${g}[0]`), _[o]);
335
429
  i.viewport(0, 0, n, h), i.drawArrays(i.TRIANGLES, 0, 3), i.framebufferTexture2D(i.FRAMEBUFFER, i.COLOR_ATTACHMENT0, i.TEXTURE_2D, null, 0), i.bindFramebuffer(i.FRAMEBUFFER, null);
336
- const m = this._programs[1];
337
- return i.useProgram(m), i.uniform1i(this._uniform(m, "albedo"), 0), i.uniform1i(this._uniform(m, "illumination"), 3), i.viewport(0, 0, s, t), i.drawArrays(i.TRIANGLES, 0, 3), this._renders++, this.canvas;
430
+ const p = this._programs[1];
431
+ return i.useProgram(p), i.uniform1i(this._uniform(p, "albedo"), 0), i.uniform1i(this._uniform(p, "illumination"), 3), i.uniform1f(this._uniform(p, "irradianceScale"), c ? 1 : 255 / 64), i.viewport(0, 0, t, s), i.drawArrays(i.TRIANGLES, 0, 3), this._renders++, this.canvas;
338
432
  }
339
- apply(e, s) {
433
+ apply(e, t) {
340
434
  if (!this.available)
341
435
  return !1;
342
- const t = e.getTransform();
343
- if (Math.abs(t.b) > 1e-5 || Math.abs(t.c) > 1e-5 || t.a <= 0 || t.d <= 0)
436
+ const s = e.getTransform();
437
+ if (Math.abs(s.b) > 1e-5 || Math.abs(s.c) > 1e-5 || s.a <= 0 || s.d <= 0)
344
438
  throw new Error("[Nova2D lighting] apply requires positive axis-aligned camera scale; use render for rotated cameras");
345
- const r = s.bounds, i = Math.floor(r.x * t.a + t.e), n = Math.floor(r.y * t.d + t.f), h = Math.max(1, Math.ceil((r.x + r.width) * t.a + t.e) - i), g = Math.max(1, Math.ceil((r.y + r.height) * t.d + t.f) - n), c = { x: (i - t.e) / t.a, y: (n - t.f) / t.d, width: h / t.a, height: g / t.d };
346
- if (h > this._options.maxTextureSize || g > this._options.maxTextureSize)
439
+ const r = t.bounds, i = Math.floor(r.x * s.a + s.e), n = Math.floor(r.y * s.d + s.f), h = Math.max(1, Math.ceil((r.x + r.width) * s.a + s.e) - i), l = Math.max(1, Math.ceil((r.y + r.height) * s.d + s.f) - n), c = { x: (i - s.e) / s.a, y: (n - s.f) / s.d, width: h / s.a, height: l / s.d };
440
+ if (h > this._options.maxTextureSize || l > this._options.maxTextureSize)
347
441
  throw new RangeError("[Nova2D lighting] Source exceeds maxTextureSize; use a smaller lighting region");
348
- (this._source.width !== h || this._source.height !== g) && (this._source.width = h, this._source.height = g), this._sourceContext.clearRect(0, 0, h, g), this._sourceContext.drawImage(e.canvas, i, n, h, g, 0, 0, h, g);
349
- const f = this.render(this._source, h, g, { ...s, bounds: c });
350
- return f ? (e.save(), e.globalAlpha = 1, e.globalCompositeOperation = "source-over", e.filter = "none", e.shadowColor = "transparent", e.beginPath(), e.rect(c.x, c.y, c.width, c.height), e.clip(), e.clearRect(c.x, c.y, c.width, c.height), e.drawImage(f, c.x, c.y, c.width, c.height), e.restore(), !0) : !1;
442
+ (this._source.width !== h || this._source.height !== l) && (this._source.width = h, this._source.height = l), this._sourceContext.clearRect(0, 0, h, l), this._sourceContext.drawImage(e.canvas, i, n, h, l, 0, 0, h, l);
443
+ const d = this.render(this._source, h, l, { ...t, bounds: c });
444
+ return d ? (e.save(), e.globalAlpha = 1, e.globalCompositeOperation = "source-over", e.filter = "none", e.shadowColor = "transparent", e.beginPath(), e.rect(c.x, c.y, c.width, c.height), e.clip(), e.clearRect(c.x, c.y, c.width, c.height), e.drawImage(d, c.x, c.y, c.width, c.height), e.restore(), !0) : !1;
351
445
  }
352
446
  _releaseGPU() {
353
447
  const e = this._gl;
354
- for (const s of this._textures)
355
- e.deleteTexture(s);
356
- for (const s of this._programs)
357
- e.deleteProgram(s);
358
- for (const s of this._shaders)
359
- e.deleteShader(s);
360
- this._framebuffer && e.deleteFramebuffer(this._framebuffer), this._textures = [], this._programs = [], this._shaders = [], this._framebuffer = void 0, this._uniforms.clear();
448
+ for (const t of this._textures)
449
+ e.deleteTexture(t);
450
+ for (const t of this._programs)
451
+ e.deleteProgram(t);
452
+ for (const t of this._shaders)
453
+ e.deleteShader(t);
454
+ this._framebuffer && e.deleteFramebuffer(this._framebuffer), this._textures = [], this._programs = [], this._shaders = [], this._framebuffer = void 0, this._lightBufferComplete = !1, this._uniforms.clear();
361
455
  }
362
456
  get available() {
363
457
  return !this._disposed && !this._lost && !this._gl.isContextLost();
364
458
  }
365
459
  get state() {
366
- return { available: this.available, generation: this._generation, renders: this._renders, textures: this._textures.length, programs: this._programs.length, shaders: this._shaders.length, framebuffers: this._framebuffer ? 1 : 0, receiverMasks: this._receiverMasks.size, ownedCanvases: this._disposed ? 0 : 3 + this._receiverMasks.size };
460
+ return { available: this.available, generation: this._generation, renders: this._renders, lightPrecision: this._lightFloat ? "float16" : "unorm8", textures: this._textures.length, programs: this._programs.length, shaders: this._shaders.length, framebuffers: this._framebuffer ? 1 : 0, ...this._fields.state, ownedCanvases: this._disposed ? 0 : 2 + this._fields.state.ownedCanvases };
367
461
  }
368
462
  dispose() {
369
463
  if (!this._disposed) {
370
- this._disposed = !0, this._lifetime.abort(), this._releaseGPU();
371
- for (const e of this._receiverMasks.values())
372
- e.image.width = e.image.height = 0;
373
- this._receiverMasks.clear(), this._lastOccluders = [];
374
- for (const e of [this.canvas, this._field, this._source])
375
- e.width = e.height = 0;
464
+ this._disposed = !0, this._lifetime.abort(), this._releaseGPU(), this._fields.dispose(), this._lastOccluders = [];
465
+ for (const e of [this.canvas, this._source])
466
+ e && (e.width = e.height = 0);
376
467
  this._gl.getExtension("WEBGL_lose_context")?.loseContext();
377
468
  }
378
469
  }
379
470
  }
380
- class I {
471
+ class $ {
381
472
  _lights = /* @__PURE__ */ new Set();
382
473
  _occluders = /* @__PURE__ */ new Set();
383
474
  _occluderValues = /* @__PURE__ */ new WeakMap();
384
475
  _receivers = /* @__PURE__ */ new Set();
385
- _renderer;
386
476
  _options;
387
477
  _receiverRevision = 0;
388
478
  _revision = 0;
@@ -390,12 +480,12 @@ class I {
390
480
  _ambient;
391
481
  constructor(e = {}) {
392
482
  this._options = {
393
- groundScale: d(e.groundScale ?? 1, "groundScale", 0.01, 10),
394
- maxHeight: d(e.maxHeight ?? 1024, "maxHeight", 1),
395
- resolution: d(e.resolution ?? 0.5, "resolution", 0.01, 1),
396
- maxTextureSize: d(e.maxTextureSize ?? 768, "maxTextureSize", 64, 2048),
483
+ groundScale: f(e.groundScale ?? 1, "groundScale", 0.01, 10),
484
+ maxHeight: f(e.maxHeight ?? 1024, "maxHeight", 1),
485
+ resolution: f(e.resolution ?? 0.5, "resolution", 0.01, 1),
486
+ maxTextureSize: f(e.maxTextureSize ?? 768, "maxTextureSize", 64, 2048),
397
487
  ambient: this._ambientValue(e.ambient ?? [1, 1, 1])
398
- }, this._ambient = this._options.ambient, this._renderer = new P(this._options);
488
+ }, this._ambient = this._options.ambient;
399
489
  }
400
490
  _alive() {
401
491
  if (this._disposed)
@@ -404,41 +494,41 @@ class I {
404
494
  _ambientValue(e) {
405
495
  if (e.length !== 3)
406
496
  throw new TypeError("[Nova2D lighting] Ambient needs three linear RGB channels");
407
- return Object.freeze(e.map((s) => d(s, "ambient", 0, 4)));
497
+ return Object.freeze(e.map((t) => f(t, "ambient", 0, 4)));
408
498
  }
409
499
  setAmbient(e) {
410
500
  this._alive(), this._ambient = this._ambientValue(e), this._revision++;
411
501
  }
412
- _handle(e, s, t, r, i = !1) {
502
+ _handle(e, t, s, r, i = !1) {
413
503
  this._alive();
414
- const n = { value: s };
504
+ const n = { value: t };
415
505
  e.add(n);
416
506
  const h = () => {
417
507
  this._revision++, i && this._receiverRevision++;
418
508
  };
419
509
  h();
420
- const g = () => {
510
+ const l = () => {
421
511
  if (this._alive(), !e.has(n))
422
512
  throw new Error("[Nova2D lighting] Handle is disposed");
423
513
  };
424
514
  return Object.freeze({
425
515
  set: (c) => {
426
- g(), n.value = t(n.value, c), h();
516
+ l(), n.value = s(n.value, c), h();
427
517
  },
428
- getOptions: () => (g(), r(n.value)),
518
+ getOptions: () => (l(), r(n.value)),
429
519
  dispose: () => {
430
520
  e.delete(n) && h();
431
521
  }
432
522
  });
433
523
  }
434
- _light(e, s) {
524
+ _light(e, t) {
435
525
  if (this._alive(), this._lights.size >= 8)
436
526
  throw new RangeError("[Nova2D lighting] Limit is 8 retained lights per lighting scene");
437
- return this._handle(this._lights, C(e, s), (t, r) => C(e, {
438
- ...t.options,
527
+ return this._handle(this._lights, I(e, t), (s, r) => I(e, {
528
+ ...s.options,
439
529
  ...r,
440
- shadows: { ...t.options.shadows, ...r.shadows }
441
- }), (t) => t.options);
530
+ shadows: { ...s.options.shadows, ...r.shadows }
531
+ }), (s) => s.options);
442
532
  }
443
533
  createDirectionalLight(e) {
444
534
  return this._light("directional", e);
@@ -452,79 +542,245 @@ class I {
452
542
  createOccluder(e) {
453
543
  if (this._alive(), this._occluders.size >= 4096)
454
544
  throw new RangeError("[Nova2D lighting] Limit is 4096 retained occluders per lighting scene");
455
- const s = this._handle(this._occluders, z(e, this._options.maxHeight), (i, n) => z({ ...i, ...n }, this._options.maxHeight), (i) => i);
456
- let t = !0;
545
+ const t = this._handle(this._occluders, U(e, this._options.maxHeight), (i, n) => U({ ...i, ...n }, this._options.maxHeight), (i) => i);
546
+ let s = !0;
457
547
  const r = Object.freeze({
458
- ...s,
548
+ ...t,
459
549
  dispose: () => {
460
- t = !1, s.dispose();
550
+ s = !1, t.dispose();
461
551
  }
462
552
  });
463
- return this._occluderValues.set(r, () => t ? s.getOptions() : void 0), r;
553
+ return this._occluderValues.set(r, () => s ? t.getOptions() : void 0), r;
464
554
  }
465
555
  createReceiver(e) {
466
556
  if (this._alive(), this._receivers.size >= 4096)
467
557
  throw new RangeError("[Nova2D lighting] Limit is 4096 retained receivers per lighting scene");
468
- const s = (t) => {
469
- if (t.ignoreOccluder !== void 0 && !this._occluderValues.has(t.ignoreOccluder))
558
+ const t = (s) => {
559
+ if (s.ignoreOccluder !== void 0 && !this._occluderValues.has(s.ignoreOccluder))
470
560
  throw new TypeError("[Nova2D lighting] ignoreOccluder must be an occluder from this lighting scene");
471
- return O(t, this._options.maxHeight);
561
+ return j(s, this._options.maxHeight);
472
562
  };
473
- return this._handle(this._receivers, s(e), (t, r) => s({ ...t, ...r }), (t) => t, !0);
563
+ return this._handle(this._receivers, t(e), (s, r) => t({ ...s, ...r }), (s) => s, !0);
474
564
  }
475
565
  _frame(e) {
476
566
  this._alive();
477
- const s = [...this._lights].map((n) => n.value).filter((n) => n.options.enabled && n.options.intensity > 0), t = [...this._occluders].map((n) => n.value).filter((n) => {
567
+ const t = [...this._lights].map((n) => n.value).filter((n) => n.options.enabled && n.options.intensity > 0), s = [...this._occluders].map((n) => n.value).filter((n) => {
478
568
  if (!n.enabled)
479
569
  return !1;
480
- const h = n.polygon.map((l) => l[0]), g = n.polygon.map((l) => l[1]), c = Math.min(...h), f = Math.max(...h), m = Math.min(...g), a = Math.max(...g);
481
- return s.some((l) => {
482
- if (!l.options.shadows.enabled || !(l.options.layers & n.layers))
570
+ const h = n.polygon.map((o) => o[0]), l = n.polygon.map((o) => o[1]), c = Math.min(...h), d = Math.max(...h), _ = Math.min(...l), p = Math.max(...l);
571
+ return t.some((o) => {
572
+ if (!o.options.shadows.enabled || !(o.options.layers & n.layers))
483
573
  return !1;
484
- if (l.kind === "directional")
574
+ if (o.kind === "directional")
485
575
  return !0;
486
- const [u, _, v] = l.position, x = Math.max(c - u, 0, u - f), R = Math.max(m - _, 0, _ - a), p = Math.max(n.baseHeight - v, 0, v - n.baseHeight - n.height);
487
- return x * x + R * R + p * p <= (l.range + l.radius) ** 2;
576
+ const [g, u, m] = o.position, v = Math.max(c - g, 0, g - d), E = Math.max(_ - u, 0, u - p), D = Math.max(n.baseHeight - m, 0, m - n.baseHeight - n.height);
577
+ return v * v + E * E + D * D <= (o.range + o.radius) ** 2;
488
578
  });
489
579
  });
490
- if (t.length > 64)
580
+ if (s.length > 64)
491
581
  throw new RangeError("[Nova2D lighting] More than 64 occluders intersect the active shadow lights; split the lighting region");
492
582
  const r = [...this._receivers].map((n) => n.value).filter((n) => n.enabled).sort((n, h) => (n.order ?? n.bounds.y + n.bounds.height) - (h.order ?? h.bounds.y + h.bounds.height)), i = /* @__PURE__ */ new Map();
493
583
  for (const n of r) {
494
584
  const h = n.ignoreOccluder;
495
585
  if (h) {
496
- const g = this._occluderValues.get(h)?.();
497
- i.set(n, g ? t.indexOf(g) + 1 : 0);
586
+ const l = this._occluderValues.get(h)?.();
587
+ i.set(n, l ? s.indexOf(l) + 1 : 0);
498
588
  }
499
589
  }
500
590
  return {
501
- bounds: A(e.bounds),
591
+ bounds: P(e.bounds),
502
592
  ambient: this._ambient,
503
593
  receiverRevision: this._receiverRevision,
504
- lights: s,
505
- occluders: t,
594
+ lights: t,
595
+ occluders: s,
506
596
  receivers: r,
507
597
  ignoredOccluders: i
508
598
  };
509
599
  }
510
- render(e, s) {
511
- const t = this._frame(s), r = e, i = d(r.naturalWidth || r.videoWidth || (typeof r.width == "number" ? r.width : t.bounds.width), "source width", 1, this._options.maxTextureSize), n = d(r.naturalHeight || r.videoHeight || (typeof r.height == "number" ? r.height : t.bounds.height), "source height", 1, this._options.maxTextureSize);
512
- return this._renderer.render(e, i, n, t);
600
+ get state() {
601
+ return { disposed: this._disposed, revision: this._revision, lights: this._lights.size, occluders: this._occluders.size, receivers: this._receivers.size, ambient: this._ambient };
602
+ }
603
+ dispose() {
604
+ this._disposed || (this._disposed = !0, this._lights.clear(), this._occluders.clear(), this._receivers.clear());
605
+ }
606
+ }
607
+ const L = ["positions", "colors", "directions", "cones", "shadows"], Y = Object.fromEntries(L.flatMap((a) => Array.from({ length: 8 }, (e, t) => [`${a}${t}`, "vec4"]))), K = `vec2 uv; vec4 outColor;
608
+ float geometryValue(int column,int row){
609
+ vec2 q=(vec2(float(column),float(row))+.5)/vec2(40.,64.);
610
+ uvec3 low=uvec3(round(novaSample_geometryLow(q).rgb*255.));
611
+ uint high=uint(round(novaSample_geometryHigh(q).r*255.));
612
+ return uintBitsToFloat(low.r|(low.g<<8)|(low.b<<16)|(high<<24));
613
+ }
614
+ vec4 geometryAt(ivec2 p){int x=p.x*4;return vec4(geometryValue(x,p.y),geometryValue(x+1,p.y),geometryValue(x+2,p.y),geometryValue(x+3,p.y));}
615
+ ${L.map((a) => `vec4 ${a}At(int i){${Array.from({ length: 8 }, (e, t) => `if(i==${t})return ${a}${t};`).join("")}return vec4(0.);}`).join(`
616
+ `)}
617
+ `;
618
+ let z = k.replace(/^#version.*$/m, "").replace(/precision highp \w+;/g, "").replace(/in vec2 uv;/, "").replace(/out vec4 outColor;/, "").replace(/uniform [^;]+;/g, "").replace("void main()", "void lightMain()").replace(/texelFetch\(geometry,(ivec2\([^;]+?\)),0\)/g, "geometryAt($1)").replace("texture(heights,uv)", "novaSample_heights(uv)").replace("textureSize(heights,0)", "ivec2(fieldSize)").replace("texelFetch(heights,clamp(ivec2(uv*vec2(size)),ivec2(0),size-1),0)", "novaSample_heights((vec2(clamp(ivec2(uv*vec2(size)),ivec2(0),size-1))+.5)/vec2(size))").replace("texture(lightMask,uv)", "novaSample_lightMask(uv)").replace("texture(normals,uv)", "novaSample_normals(uv)");
619
+ for (const a of L)
620
+ z = z.replaceAll(`${a}[i]`, `${a}At(i)`);
621
+ const Z = B({
622
+ name: "nova-lighting-field-2d",
623
+ uniforms: {
624
+ heights: "sampler2D",
625
+ normals: "sampler2D",
626
+ lightMask: "sampler2D",
627
+ geometryLow: "sampler2D",
628
+ geometryHigh: "sampler2D",
629
+ bounds: "vec4",
630
+ ambient: "vec3",
631
+ fieldSize: "vec2",
632
+ groundScale: "float",
633
+ maxHeight: "float",
634
+ irradianceScale: "float",
635
+ lightCount: "int",
636
+ casterCount: "int",
637
+ categoricalField: "int",
638
+ surfaceField: "int",
639
+ ...Y
640
+ },
641
+ fragment: `${K}
642
+ ${z}
643
+ vec4 novaEffect(vec2 point){uv=point;lightMain();return outColor;}`
644
+ }), J = M.slice(M.indexOf("vec3 linearRGB"), M.indexOf("void main")), Q = B({
645
+ name: "nova-lighting-composite-2d",
646
+ uniforms: { illumination: "sampler2D" },
647
+ fragment: `${J}
648
+ vec4 novaEffect(vec2 point){
649
+ vec4 art=novaSample(point);
650
+ vec3 rgb=art.a>.00001?art.rgb/art.a:vec3(0.);
651
+ vec3 irradiance=novaSample_illumination(point).rgb*(255./64.);
652
+ return vec4(srgb(clamp(linearRGB(rgb)*irradiance,0.,1.))*art.a,art.a);
653
+ }`
654
+ });
655
+ let ee = 0;
656
+ class S extends $ {
657
+ constructor(e, t = {}) {
658
+ super(t), this._app = e, this._fields = new G(this._options);
659
+ const s = `nova-lighting-${++ee}`;
660
+ this._normal.getContext("2d").fillStyle = "rgb(128,128,128)", this._normal.getContext("2d").fillRect(0, 0, 1, 1), this._response.getContext("2d").fillStyle = "#ffff00", this._response.getContext("2d").fillRect(0, 0, 1, 1), this._bundle = R.define(s, { images: {
661
+ heights: R.canvas(this._fields.image),
662
+ normals: R.canvas(this._normal),
663
+ lightMask: R.canvas(this._response),
664
+ geometryLow: R.canvas(this._geometryLow),
665
+ geometryHigh: R.canvas(this._geometryHigh)
666
+ } }), e.assets.use(this._bundle);
667
+ const r = Object.fromEntries(["positions", "colors", "directions", "cones", "shadows"].flatMap((i) => Array.from({ length: 8 }, (n, h) => [`${i}${h}`, [0, 0, 0, 0]])));
668
+ this._fieldEffect = Z.create({ ...r, ...this._bundle.images, bounds: [0, 0, 1, 1], ambient: this._options.ambient, fieldSize: [1, 1], groundScale: this._options.groundScale, maxHeight: this._options.maxHeight, irradianceScale: 64 / 255, lightCount: 0, casterCount: 0, categoricalField: 0, surfaceField: 0 }), this._lightTexture = X(e, {
669
+ width: 1,
670
+ height: 1,
671
+ dpr: 1,
672
+ update: "on-dirty",
673
+ dependencies: Object.values(this._bundle.images),
674
+ capture: (i, n) => {
675
+ const h = { x: 0, y: 0, width: n.width, height: n.height };
676
+ i.beginComposition({ bounds: h, effects: [this._fieldEffect], opacity: 1, blendMode: "normal" }), i.rect({ ...h, styles: { background: "#ffffff" } }), i.endComposition();
677
+ }
678
+ }), this.effect = Q.create({ illumination: this._lightTexture.image });
679
+ }
680
+ _fields;
681
+ _geometryLow = w(40, 64);
682
+ _geometryHigh = w(40, 64);
683
+ _normal = w();
684
+ _response = w();
685
+ _bundle;
686
+ _geometry = [];
687
+ _geometryValid = !1;
688
+ _updates = 0;
689
+ _frameSource;
690
+ _fieldEffect;
691
+ _lightTexture;
692
+ effect;
693
+ /** @internal */
694
+ static bound(e, t, s) {
695
+ const r = new S(e, t);
696
+ return r._frameSource = s, r;
697
+ }
698
+ update(e) {
699
+ const t = this._frameSource ? this._frameSource(e) : this._frame(e), s = Math.max(1, Math.min(this._options.maxTextureSize, Math.ceil(t.bounds.width * this._options.resolution))), r = Math.max(1, Math.min(this._options.maxTextureSize, Math.ceil(t.bounds.height * this._options.resolution)));
700
+ this._app.raph.core.batch(() => {
701
+ let i = !1;
702
+ if (this._fields.update(t, s, r)) {
703
+ i = !0, this._app.assets.invalidateDrawable(this._bundle.images.heights);
704
+ for (const [l, c, d] of [
705
+ [this._normal, this._fields.normal, this._bundle.images.normals],
706
+ [this._response, this._fields.response, this._bundle.images.lightMask]
707
+ ])
708
+ this._fields.surface && c && (l.width = c.width, l.height = c.height, l.getContext("2d").drawImage(c, 0, 0), this._app.assets.invalidateDrawable(d));
709
+ }
710
+ if (!this._geometryValid || t.occluders.length !== this._geometry.length || t.occluders.some((l, c) => l !== this._geometry[c])) {
711
+ i = !0;
712
+ const l = new Float32Array(2560);
713
+ for (const [p, o] of t.occluders.entries()) {
714
+ const g = p * 40;
715
+ l.set([o.height, o.baseHeight, o.layers, o.polygon.length], g), o.polygon.forEach((u, m) => l.set(u, g + (m + 1) * 4)), l.set([Math.min(...o.polygon.map((u) => u[0])), Math.min(...o.polygon.map((u) => u[1])), Math.max(...o.polygon.map((u) => u[0])), Math.max(...o.polygon.map((u) => u[1]))], g + 36);
716
+ }
717
+ const c = new Uint32Array(l.buffer), d = new ImageData(40, 64), _ = new ImageData(40, 64);
718
+ c.forEach((p, o) => {
719
+ d.data.set([p & 255, p >>> 8 & 255, p >>> 16 & 255, 255], o * 4), _.data.set([p >>> 24, 0, 0, 255], o * 4);
720
+ }), this._geometryLow.getContext("2d").putImageData(d, 0, 0), this._geometryHigh.getContext("2d").putImageData(_, 0, 0), this._app.assets.invalidateDrawable(this._bundle.images.geometryLow), this._app.assets.invalidateDrawable(this._bundle.images.geometryHigh), this._geometry = t.occluders, this._geometryValid = !0;
721
+ }
722
+ const n = {};
723
+ t.lights.forEach((l, c) => {
724
+ n[`positions${c}`] = [...l.position, l.kind === "directional" ? 0 : l.kind === "point" ? 1 : 2], n[`colors${c}`] = [...l.color, l.options.intensity], n[`directions${c}`] = [...l.direction, l.range], n[`cones${c}`] = [l.inner, l.outer, l.options.layers, l.falloff], n[`shadows${c}`] = [l.options.shadows.enabled ? l.options.shadows.opacity : 0, l.radius, 0, l.range];
725
+ });
726
+ const h = this._fieldEffect.version;
727
+ this._fieldEffect.setUniforms({ ...n, bounds: [t.bounds.x, t.bounds.y, t.bounds.width, t.bounds.height], ambient: t.ambient, fieldSize: [s, r], lightCount: t.lights.length, casterCount: t.occluders.length, categoricalField: Number(t.ignoredOccluders.size > 0 || this._fields.surface), surfaceField: Number(this._fields.surface) }), (this._lightTexture.width !== s || this._lightTexture.height !== r) && this._lightTexture.resize({ width: s, height: r, dpr: 1 }), (i || this._fieldEffect.version !== h || !this._lightTexture.ready) && (this._lightTexture.capture(), this._updates++);
728
+ });
729
+ }
730
+ get state() {
731
+ return { ...super.state, backend: "app-effect", lightBuffer: this._disposed ? [0, 0] : [this._lightTexture.width, this._lightTexture.height], updates: this._updates, ...this._fields.state, ownedCanvases: this._disposed ? 0 : 4 + this._fields.state.ownedCanvases };
732
+ }
733
+ dispose() {
734
+ if (!this._disposed) {
735
+ super.dispose(), this._lightTexture.destroy(), this._app.assets.unuse(this._bundle), this._fields.dispose();
736
+ for (const e of [this._normal, this._response, this._geometryLow, this._geometryHigh])
737
+ e.width = e.height = 0;
738
+ this._geometry = [];
739
+ }
740
+ }
741
+ }
742
+ function oe(a, e = {}) {
743
+ return new S(a, e);
744
+ }
745
+ class te extends $ {
746
+ _renderer;
747
+ _effects = /* @__PURE__ */ new Set();
748
+ constructor(e = {}) {
749
+ super(e), this._renderer = new q(this._options);
750
+ }
751
+ createEffect(e) {
752
+ if (this._disposed)
753
+ throw new Error("[Nova2D lighting] Scene is disposed");
754
+ const t = S.bound(e, this._options, (s) => this._frame(s));
755
+ return this._effects.add(t), { effect: t.effect, update: (s) => t.update(s), dispose: () => {
756
+ t.dispose(), this._effects.delete(t);
757
+ } };
758
+ }
759
+ render(e, t) {
760
+ const s = this._frame(t), r = e, i = f(r.naturalWidth || r.videoWidth || (typeof r.width == "number" ? r.width : s.bounds.width), "source width", 1, this._options.maxTextureSize), n = f(r.naturalHeight || r.videoHeight || (typeof r.height == "number" ? r.height : s.bounds.height), "source height", 1, this._options.maxTextureSize);
761
+ return this._renderer.render(e, i, n, s);
513
762
  }
514
- apply(e, s) {
515
- return this._renderer.apply(e, this._frame(s));
763
+ apply(e, t) {
764
+ return this._renderer.apply(e, this._frame(t));
516
765
  }
517
766
  get state() {
518
- return { disposed: this._disposed, revision: this._revision, lights: this._lights.size, occluders: this._occluders.size, receivers: this._receivers.size, ambient: this._ambient, ...this._renderer.state };
767
+ return { ...super.state, ...this._renderer.state };
519
768
  }
520
769
  dispose() {
521
- this._disposed || (this._disposed = !0, this._lights.clear(), this._occluders.clear(), this._receivers.clear(), this._renderer.dispose());
770
+ if (!this._disposed) {
771
+ super.dispose(), this._renderer.dispose();
772
+ for (const e of this._effects)
773
+ e.dispose();
774
+ this._effects.clear();
775
+ }
522
776
  }
523
777
  }
524
- function H(o = {}) {
525
- return new I(o);
778
+ function ae(a = {}) {
779
+ return new te(a);
526
780
  }
527
781
  export {
528
- I as NovaLighting2D,
529
- H as createLighting2D
782
+ te as NovaLighting2D,
783
+ S as NovaLightingEffect2D,
784
+ ae as createLighting2D,
785
+ oe as createLightingEffect2D
530
786
  };