@nova-lab/core 1.1.2-dev.255 → 1.2.0-dev.256
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/2d/lighting.js
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
function
|
|
2
|
-
if (!Number.isFinite(o) || o <
|
|
3
|
-
throw new RangeError(`[Nova2D lighting] ${e} must be finite in ${
|
|
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
4
|
return o;
|
|
5
5
|
}
|
|
6
|
-
function
|
|
7
|
-
return Object.freeze({ x:
|
|
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) });
|
|
8
8
|
}
|
|
9
|
-
function
|
|
9
|
+
function M(o, e = !1) {
|
|
10
10
|
if (o?.length !== 3)
|
|
11
11
|
throw new TypeError("[Nova2D lighting] Expected an XYZ tuple");
|
|
12
|
-
const
|
|
13
|
-
if (e &&
|
|
12
|
+
const s = o.map((r, i) => d(r, `vector[${i}]`)), t = Math.hypot(...s);
|
|
13
|
+
if (e && t < 1e-5)
|
|
14
14
|
throw new RangeError("[Nova2D lighting] Direction cannot be zero");
|
|
15
|
-
return Object.freeze(e ?
|
|
15
|
+
return Object.freeze(e ? s.map((r) => r / t) : s);
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function D(o = 255) {
|
|
18
18
|
if (!Number.isInteger(o))
|
|
19
19
|
throw new RangeError("[Nova2D lighting] Layers must be an eight-bit integer");
|
|
20
|
-
return
|
|
20
|
+
return d(o, "layers", 0, 255);
|
|
21
21
|
}
|
|
22
|
-
function
|
|
22
|
+
function N(o = "#ffffff") {
|
|
23
23
|
let e;
|
|
24
24
|
if (typeof o == "string") {
|
|
25
25
|
if (!/^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(o))
|
|
26
26
|
throw new TypeError("[Nova2D lighting] Color must be #RGB, #RRGGBB or an RGB tuple");
|
|
27
|
-
const
|
|
28
|
-
e = [0, 2, 4].map((
|
|
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);
|
|
29
29
|
} else {
|
|
30
30
|
if (o.length !== 3)
|
|
31
31
|
throw new TypeError("[Nova2D lighting] Color needs three channels");
|
|
32
|
-
e = o.map((
|
|
32
|
+
e = o.map((s) => d(s, "color", 0, 1));
|
|
33
33
|
}
|
|
34
34
|
return Object.freeze([...e]);
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
const
|
|
36
|
+
function C(o, e) {
|
|
37
|
+
const s = {
|
|
38
38
|
enabled: e.enabled ?? !0,
|
|
39
|
-
color:
|
|
40
|
-
intensity:
|
|
41
|
-
layers:
|
|
42
|
-
shadows: Object.freeze({ enabled: e.shadows?.enabled ?? !1, opacity:
|
|
43
|
-
},
|
|
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
44
|
if (o === "directional") {
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
45
|
+
const l = e, u = M(l.direction, !0);
|
|
46
|
+
if (u[2] >= -1e-5)
|
|
47
47
|
throw new RangeError("[Nova2D lighting] Directional light must point down towards the ground");
|
|
48
|
-
const
|
|
49
|
-
return { kind: o, color:
|
|
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 }) };
|
|
50
50
|
}
|
|
51
|
-
const
|
|
52
|
-
let f = [0, 0, -1],
|
|
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
53
|
if (o === "spot") {
|
|
54
|
-
const
|
|
55
|
-
return f =
|
|
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 }) };
|
|
56
56
|
}
|
|
57
|
-
return { kind: o, color:
|
|
57
|
+
return { kind: o, color: t, position: i, direction: f, range: n, radius: h, inner: m, outer: a, falloff: g, options: Object.freeze(c) };
|
|
58
58
|
}
|
|
59
|
-
function
|
|
59
|
+
function z(o, e) {
|
|
60
60
|
if (!o.polygon || o.polygon.length < 3 || o.polygon.length > 8)
|
|
61
61
|
throw new RangeError("[Nova2D lighting] Occluder needs 3..8 convex vertices");
|
|
62
|
-
const
|
|
63
|
-
let
|
|
64
|
-
for (let
|
|
65
|
-
const g =
|
|
66
|
-
if (Math.abs(
|
|
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)
|
|
67
67
|
throw new RangeError("[Nova2D lighting] Footprint must be strictly convex without duplicate vertices");
|
|
68
|
-
|
|
68
|
+
r = Math.sign(m), t += g[0] * c[1] - c[0] * g[1];
|
|
69
69
|
}
|
|
70
|
-
for (let
|
|
71
|
-
const g =
|
|
72
|
-
if (
|
|
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
73
|
throw new RangeError("[Nova2D lighting] Footprint edges must enclose a convex polygon");
|
|
74
74
|
}
|
|
75
|
-
|
|
76
|
-
const i =
|
|
77
|
-
return Object.freeze({ ...o, polygon: Object.freeze(
|
|
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
78
|
}
|
|
79
|
-
function
|
|
79
|
+
function O(o, e) {
|
|
80
80
|
if (!o.image)
|
|
81
81
|
throw new TypeError("[Nova2D lighting] Receiver needs a borrowed alpha image");
|
|
82
|
-
const
|
|
83
|
-
return Object.freeze({ ...o, bounds:
|
|
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 });
|
|
84
84
|
}
|
|
85
|
-
const
|
|
85
|
+
const F = `#version 300 es
|
|
86
86
|
precision highp float;
|
|
87
87
|
out vec2 uv;
|
|
88
88
|
void main(){
|
|
89
89
|
vec2 p=vec2(float((gl_VertexID<<1)&2),float(gl_VertexID&2));
|
|
90
90
|
uv=vec2(p.x,1.-p.y);
|
|
91
91
|
gl_Position=vec4(p*2.-1.,0.,1.);
|
|
92
|
-
}`,
|
|
92
|
+
}`, L = `#version 300 es
|
|
93
93
|
precision highp float;
|
|
94
94
|
precision highp int;
|
|
95
95
|
in vec2 uv;
|
|
@@ -99,13 +99,14 @@ uniform highp sampler2D geometry;
|
|
|
99
99
|
uniform vec4 bounds;
|
|
100
100
|
uniform vec3 ambient;
|
|
101
101
|
uniform float groundScale, maxHeight;
|
|
102
|
-
uniform int lightCount, casterCount;
|
|
102
|
+
uniform int lightCount, casterCount, categoricalField;
|
|
103
103
|
uniform vec4 positions[8], colors[8], directions[8], cones[8], shadows[8];
|
|
104
104
|
float cross2(vec2 a,vec2 b){return a.x*b.y-a.y*b.x;}
|
|
105
|
-
bool blocked(vec3 p,vec3 end,int layers){
|
|
105
|
+
bool blocked(vec3 p,vec3 end,int layers,int ignored){
|
|
106
106
|
vec3 d=end-p;
|
|
107
107
|
for(int c=0;c<64;c++){
|
|
108
108
|
if(c>=casterCount)break;
|
|
109
|
+
if(c==ignored)continue;
|
|
109
110
|
vec4 h=texelFetch(geometry,ivec2(0,c),0);
|
|
110
111
|
if((int(h.z)&layers)==0)continue;
|
|
111
112
|
vec4 bb=texelFetch(geometry,ivec2(9,c),0);
|
|
@@ -133,9 +134,12 @@ bool blocked(vec3 p,vec3 end,int layers){
|
|
|
133
134
|
void main(){
|
|
134
135
|
vec2 screen=bounds.xy+uv*bounds.zw;
|
|
135
136
|
vec4 field=texture(heights,uv);
|
|
136
|
-
|
|
137
|
+
ivec2 size=textureSize(heights,0);
|
|
138
|
+
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;}
|
|
137
140
|
float z=field.r*maxHeight;
|
|
138
|
-
int receiverLayers=int(round(
|
|
141
|
+
int receiverLayers=int(round(material.g*255.));
|
|
142
|
+
int ignored=categoricalField>0?int(round(material.b*255.))-1:-1;
|
|
139
143
|
vec3 p=vec3(screen.x,(screen.y+z)/groundScale,z);
|
|
140
144
|
vec3 light=ambient;
|
|
141
145
|
for(int i=0;i<8;i++){
|
|
@@ -162,7 +166,7 @@ void main(){
|
|
|
162
166
|
float angle=float(s)*2.39996323;
|
|
163
167
|
vec2 disk=vec2(cos(angle),sin(angle))*sqrt((float(s)+.5)/8.);
|
|
164
168
|
vec3 target=end+vec3(disk*radius,0.);
|
|
165
|
-
visibility+=blocked(p,target,layers)?0.:1./float(samples);
|
|
169
|
+
visibility+=blocked(p,target,layers,ignored)?0.:1./float(samples);
|
|
166
170
|
}
|
|
167
171
|
amount*=mix(1.,visibility,shadows[i].x);
|
|
168
172
|
}
|
|
@@ -170,7 +174,7 @@ void main(){
|
|
|
170
174
|
}
|
|
171
175
|
// 1 maps EXACTLY to byte 64: the neutral baseline does not introduce a seam.
|
|
172
176
|
outColor=vec4(clamp(light*(64./255.),0.,1.),1.);
|
|
173
|
-
}`,
|
|
177
|
+
}`, U = `#version 300 es
|
|
174
178
|
precision highp float;
|
|
175
179
|
in vec2 uv;
|
|
176
180
|
out vec4 outColor;
|
|
@@ -182,18 +186,18 @@ void main(){
|
|
|
182
186
|
vec3 irradiance=texture(illumination,vec2(uv.x,1.-uv.y)).rgb*(255./64.);
|
|
183
187
|
outColor=vec4(srgb(clamp(linearRGB(art.rgb)*irradiance,0.,1.)),art.a);
|
|
184
188
|
}`;
|
|
185
|
-
function
|
|
186
|
-
const
|
|
187
|
-
return
|
|
189
|
+
function E(o = 1, e = 1) {
|
|
190
|
+
const s = document.createElement("canvas");
|
|
191
|
+
return s.width = o, s.height = e, s;
|
|
188
192
|
}
|
|
189
|
-
class
|
|
193
|
+
class P {
|
|
190
194
|
constructor(e) {
|
|
191
195
|
this._options = e;
|
|
192
|
-
const
|
|
193
|
-
if (!
|
|
196
|
+
const s = this.canvas.getContext("webgl2", { alpha: !0, premultipliedAlpha: !1, antialias: !1, depth: !1, stencil: !1 });
|
|
197
|
+
if (!s)
|
|
194
198
|
throw new Error("[Nova2D lighting] WebGL2 is required");
|
|
195
|
-
this._gl =
|
|
196
|
-
|
|
199
|
+
this._gl = s, this.canvas.addEventListener("webglcontextlost", (t) => {
|
|
200
|
+
t.preventDefault(), this._lost = !0;
|
|
197
201
|
}, { signal: this._lifetime.signal }), this.canvas.addEventListener("webglcontextrestored", () => {
|
|
198
202
|
if (!this._disposed) {
|
|
199
203
|
this._lost = !0;
|
|
@@ -206,13 +210,13 @@ class F {
|
|
|
206
210
|
}, { signal: this._lifetime.signal });
|
|
207
211
|
try {
|
|
208
212
|
this._initialize();
|
|
209
|
-
} catch (
|
|
210
|
-
throw this.dispose(),
|
|
213
|
+
} catch (t) {
|
|
214
|
+
throw this.dispose(), t;
|
|
211
215
|
}
|
|
212
216
|
}
|
|
213
|
-
canvas =
|
|
214
|
-
_field =
|
|
215
|
-
_source =
|
|
217
|
+
canvas = E();
|
|
218
|
+
_field = E();
|
|
219
|
+
_source = E();
|
|
216
220
|
_fieldContext = this._field.getContext("2d");
|
|
217
221
|
_sourceContext = this._source.getContext("2d");
|
|
218
222
|
_receiverMasks = /* @__PURE__ */ new Map();
|
|
@@ -235,116 +239,124 @@ class F {
|
|
|
235
239
|
_initialize() {
|
|
236
240
|
const e = this._gl;
|
|
237
241
|
this._textures = [], this._programs = [], this._shaders = [], this._uniforms.clear();
|
|
238
|
-
const
|
|
239
|
-
if (this._shaders.push(
|
|
240
|
-
throw new Error(e.getShaderInfoLog(
|
|
241
|
-
for (const
|
|
242
|
-
const
|
|
243
|
-
if (this._shaders.push(
|
|
244
|
-
throw new Error(e.getShaderInfoLog(
|
|
245
|
-
if (e.attachShader(i,
|
|
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]) {
|
|
246
|
+
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))
|
|
248
|
+
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))
|
|
246
250
|
throw new Error(e.getProgramInfoLog(i) ?? "[Nova2D lighting] Program link failed");
|
|
247
251
|
this._uniforms.set(i, /* @__PURE__ */ new Map());
|
|
248
252
|
}
|
|
249
|
-
for (let
|
|
250
|
-
const
|
|
251
|
-
this._textures.push(
|
|
253
|
+
for (let t = 0; t < 4; t++) {
|
|
254
|
+
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);
|
|
252
256
|
}
|
|
253
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++;
|
|
254
258
|
}
|
|
255
|
-
_uniform(e,
|
|
256
|
-
const
|
|
257
|
-
return
|
|
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);
|
|
258
262
|
}
|
|
259
263
|
_bind(e) {
|
|
260
264
|
this._gl.activeTexture(this._gl.TEXTURE0 + e), this._gl.bindTexture(this._gl.TEXTURE_2D, this._textures[e]);
|
|
261
265
|
}
|
|
262
|
-
_uploadField(e,
|
|
263
|
-
const
|
|
264
|
-
if (
|
|
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)
|
|
265
269
|
return;
|
|
266
|
-
const { bounds: i, receivers:
|
|
267
|
-
this._field.width =
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
for (const [
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
for (const
|
|
275
|
-
const
|
|
276
|
-
if (!
|
|
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))
|
|
277
281
|
continue;
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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)
|
|
282
287
|
throw new RangeError("[Nova2D lighting] Visible receiver masks exceed the 16MiB budget; reduce their bounds or render a smaller region");
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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, _);
|
|
286
298
|
}
|
|
287
|
-
|
|
299
|
+
g.drawImage(_.image, l.x, l.y, l.width, l.height);
|
|
288
300
|
}
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
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;
|
|
292
304
|
}
|
|
293
|
-
render(e, t, r
|
|
305
|
+
render(e, s, t, r) {
|
|
294
306
|
if (this._disposed)
|
|
295
307
|
throw new Error("[Nova2D lighting] Renderer is disposed");
|
|
296
308
|
if (!this.available)
|
|
297
309
|
return;
|
|
298
310
|
const i = this._gl;
|
|
299
|
-
(this.canvas.width !==
|
|
300
|
-
const
|
|
301
|
-
if (this._uploadField(
|
|
311
|
+
(this.canvas.width !== s || this.canvas.height !== t) && (this.canvas.width = s, this.canvas.height = t);
|
|
312
|
+
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])) {
|
|
302
314
|
const a = new Float32Array(2560);
|
|
303
|
-
for (const [
|
|
304
|
-
const
|
|
305
|
-
a.set([
|
|
306
|
-
for (const [
|
|
307
|
-
a.set(
|
|
308
|
-
a.set([Math.min(...
|
|
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);
|
|
309
321
|
}
|
|
310
|
-
this._bind(2), i.texImage2D(i.TEXTURE_2D, 0, i.RGBA32F, 10, 64, 0, i.RGBA, i.FLOAT, a), this._lastOccluders =
|
|
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;
|
|
311
323
|
}
|
|
312
|
-
if (this._bind(3), (
|
|
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)
|
|
313
325
|
throw new Error("[Nova2D lighting] Incomplete light buffer");
|
|
314
|
-
const
|
|
315
|
-
i.useProgram(
|
|
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);
|
|
316
328
|
const f = Array.from({ length: 5 }, () => new Float32Array(32));
|
|
317
|
-
|
|
318
|
-
const
|
|
319
|
-
f[0].set([...a.position, a.kind === "directional" ? 0 : a.kind === "point" ? 1 : 2],
|
|
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);
|
|
320
332
|
});
|
|
321
|
-
for (const [a,
|
|
322
|
-
i.uniform4fv(this._uniform(
|
|
323
|
-
i.viewport(0, 0,
|
|
324
|
-
const
|
|
325
|
-
return i.useProgram(
|
|
333
|
+
for (const [a, l] of ["positions", "colors", "directions", "cones", "shadows"].entries())
|
|
334
|
+
i.uniform4fv(this._uniform(c, `${l}[0]`), f[a]);
|
|
335
|
+
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;
|
|
326
338
|
}
|
|
327
|
-
apply(e,
|
|
339
|
+
apply(e, s) {
|
|
328
340
|
if (!this.available)
|
|
329
341
|
return !1;
|
|
330
|
-
const
|
|
331
|
-
if (Math.abs(
|
|
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)
|
|
332
344
|
throw new Error("[Nova2D lighting] apply requires positive axis-aligned camera scale; use render for rotated cameras");
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
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)
|
|
335
347
|
throw new RangeError("[Nova2D lighting] Source exceeds maxTextureSize; use a smaller lighting region");
|
|
336
|
-
(this._source.width !==
|
|
337
|
-
const f = this.render(this._source,
|
|
338
|
-
return f ? (e.save(), e.globalAlpha = 1, e.globalCompositeOperation = "source-over", e.filter = "none", e.shadowColor = "transparent", e.beginPath(), e.rect(
|
|
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;
|
|
339
351
|
}
|
|
340
352
|
_releaseGPU() {
|
|
341
353
|
const e = this._gl;
|
|
342
|
-
for (const
|
|
343
|
-
e.deleteTexture(
|
|
344
|
-
for (const
|
|
345
|
-
e.deleteProgram(
|
|
346
|
-
for (const
|
|
347
|
-
e.deleteShader(
|
|
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);
|
|
348
360
|
this._framebuffer && e.deleteFramebuffer(this._framebuffer), this._textures = [], this._programs = [], this._shaders = [], this._framebuffer = void 0, this._uniforms.clear();
|
|
349
361
|
}
|
|
350
362
|
get available() {
|
|
@@ -357,7 +369,7 @@ class F {
|
|
|
357
369
|
if (!this._disposed) {
|
|
358
370
|
this._disposed = !0, this._lifetime.abort(), this._releaseGPU();
|
|
359
371
|
for (const e of this._receiverMasks.values())
|
|
360
|
-
e.width = e.height = 0;
|
|
372
|
+
e.image.width = e.image.height = 0;
|
|
361
373
|
this._receiverMasks.clear(), this._lastOccluders = [];
|
|
362
374
|
for (const e of [this.canvas, this._field, this._source])
|
|
363
375
|
e.width = e.height = 0;
|
|
@@ -365,9 +377,10 @@ class F {
|
|
|
365
377
|
}
|
|
366
378
|
}
|
|
367
379
|
}
|
|
368
|
-
class
|
|
380
|
+
class I {
|
|
369
381
|
_lights = /* @__PURE__ */ new Set();
|
|
370
382
|
_occluders = /* @__PURE__ */ new Set();
|
|
383
|
+
_occluderValues = /* @__PURE__ */ new WeakMap();
|
|
371
384
|
_receivers = /* @__PURE__ */ new Set();
|
|
372
385
|
_renderer;
|
|
373
386
|
_options;
|
|
@@ -377,12 +390,12 @@ class O {
|
|
|
377
390
|
_ambient;
|
|
378
391
|
constructor(e = {}) {
|
|
379
392
|
this._options = {
|
|
380
|
-
groundScale:
|
|
381
|
-
maxHeight:
|
|
382
|
-
resolution:
|
|
383
|
-
maxTextureSize:
|
|
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),
|
|
384
397
|
ambient: this._ambientValue(e.ambient ?? [1, 1, 1])
|
|
385
|
-
}, this._ambient = this._options.ambient, this._renderer = new
|
|
398
|
+
}, this._ambient = this._options.ambient, this._renderer = new P(this._options);
|
|
386
399
|
}
|
|
387
400
|
_alive() {
|
|
388
401
|
if (this._disposed)
|
|
@@ -391,41 +404,41 @@ class O {
|
|
|
391
404
|
_ambientValue(e) {
|
|
392
405
|
if (e.length !== 3)
|
|
393
406
|
throw new TypeError("[Nova2D lighting] Ambient needs three linear RGB channels");
|
|
394
|
-
return Object.freeze(e.map((
|
|
407
|
+
return Object.freeze(e.map((s) => d(s, "ambient", 0, 4)));
|
|
395
408
|
}
|
|
396
409
|
setAmbient(e) {
|
|
397
410
|
this._alive(), this._ambient = this._ambientValue(e), this._revision++;
|
|
398
411
|
}
|
|
399
|
-
_handle(e, t, r,
|
|
412
|
+
_handle(e, s, t, r, i = !1) {
|
|
400
413
|
this._alive();
|
|
401
|
-
const
|
|
402
|
-
e.add(
|
|
403
|
-
const
|
|
414
|
+
const n = { value: s };
|
|
415
|
+
e.add(n);
|
|
416
|
+
const h = () => {
|
|
404
417
|
this._revision++, i && this._receiverRevision++;
|
|
405
418
|
};
|
|
406
|
-
|
|
419
|
+
h();
|
|
407
420
|
const g = () => {
|
|
408
|
-
if (this._alive(), !e.has(
|
|
421
|
+
if (this._alive(), !e.has(n))
|
|
409
422
|
throw new Error("[Nova2D lighting] Handle is disposed");
|
|
410
423
|
};
|
|
411
424
|
return Object.freeze({
|
|
412
|
-
set: (
|
|
413
|
-
g(),
|
|
425
|
+
set: (c) => {
|
|
426
|
+
g(), n.value = t(n.value, c), h();
|
|
414
427
|
},
|
|
415
|
-
getOptions: () => (g(),
|
|
428
|
+
getOptions: () => (g(), r(n.value)),
|
|
416
429
|
dispose: () => {
|
|
417
|
-
e.delete(
|
|
430
|
+
e.delete(n) && h();
|
|
418
431
|
}
|
|
419
432
|
});
|
|
420
433
|
}
|
|
421
|
-
_light(e,
|
|
434
|
+
_light(e, s) {
|
|
422
435
|
if (this._alive(), this._lights.size >= 8)
|
|
423
436
|
throw new RangeError("[Nova2D lighting] Limit is 8 retained lights per lighting scene");
|
|
424
|
-
return this._handle(this._lights,
|
|
425
|
-
...
|
|
426
|
-
...
|
|
427
|
-
shadows: { ...
|
|
428
|
-
}), (
|
|
437
|
+
return this._handle(this._lights, C(e, s), (t, r) => C(e, {
|
|
438
|
+
...t.options,
|
|
439
|
+
...r,
|
|
440
|
+
shadows: { ...t.options.shadows, ...r.shadows }
|
|
441
|
+
}), (t) => t.options);
|
|
429
442
|
}
|
|
430
443
|
createDirectionalLight(e) {
|
|
431
444
|
return this._light("directional", e);
|
|
@@ -439,45 +452,67 @@ class O {
|
|
|
439
452
|
createOccluder(e) {
|
|
440
453
|
if (this._alive(), this._occluders.size >= 4096)
|
|
441
454
|
throw new RangeError("[Nova2D lighting] Limit is 4096 retained occluders per lighting scene");
|
|
442
|
-
|
|
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;
|
|
457
|
+
const r = Object.freeze({
|
|
458
|
+
...s,
|
|
459
|
+
dispose: () => {
|
|
460
|
+
t = !1, s.dispose();
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
return this._occluderValues.set(r, () => t ? s.getOptions() : void 0), r;
|
|
443
464
|
}
|
|
444
465
|
createReceiver(e) {
|
|
445
466
|
if (this._alive(), this._receivers.size >= 4096)
|
|
446
467
|
throw new RangeError("[Nova2D lighting] Limit is 4096 retained receivers per lighting scene");
|
|
447
|
-
|
|
468
|
+
const s = (t) => {
|
|
469
|
+
if (t.ignoreOccluder !== void 0 && !this._occluderValues.has(t.ignoreOccluder))
|
|
470
|
+
throw new TypeError("[Nova2D lighting] ignoreOccluder must be an occluder from this lighting scene");
|
|
471
|
+
return O(t, this._options.maxHeight);
|
|
472
|
+
};
|
|
473
|
+
return this._handle(this._receivers, s(e), (t, r) => s({ ...t, ...r }), (t) => t, !0);
|
|
448
474
|
}
|
|
449
475
|
_frame(e) {
|
|
450
476
|
this._alive();
|
|
451
|
-
const
|
|
452
|
-
if (!
|
|
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) => {
|
|
478
|
+
if (!n.enabled)
|
|
453
479
|
return !1;
|
|
454
|
-
const
|
|
455
|
-
return
|
|
456
|
-
if (!
|
|
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))
|
|
457
483
|
return !1;
|
|
458
|
-
if (
|
|
484
|
+
if (l.kind === "directional")
|
|
459
485
|
return !0;
|
|
460
|
-
const [
|
|
461
|
-
return
|
|
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;
|
|
462
488
|
});
|
|
463
489
|
});
|
|
464
|
-
if (
|
|
490
|
+
if (t.length > 64)
|
|
465
491
|
throw new RangeError("[Nova2D lighting] More than 64 occluders intersect the active shadow lights; split the lighting region");
|
|
492
|
+
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
|
+
for (const n of r) {
|
|
494
|
+
const h = n.ignoreOccluder;
|
|
495
|
+
if (h) {
|
|
496
|
+
const g = this._occluderValues.get(h)?.();
|
|
497
|
+
i.set(n, g ? t.indexOf(g) + 1 : 0);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
466
500
|
return {
|
|
467
|
-
bounds:
|
|
501
|
+
bounds: A(e.bounds),
|
|
468
502
|
ambient: this._ambient,
|
|
469
503
|
receiverRevision: this._receiverRevision,
|
|
470
|
-
lights:
|
|
471
|
-
occluders:
|
|
472
|
-
receivers:
|
|
504
|
+
lights: s,
|
|
505
|
+
occluders: t,
|
|
506
|
+
receivers: r,
|
|
507
|
+
ignoredOccluders: i
|
|
473
508
|
};
|
|
474
509
|
}
|
|
475
|
-
render(e,
|
|
476
|
-
const
|
|
477
|
-
return this._renderer.render(e, i,
|
|
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);
|
|
478
513
|
}
|
|
479
|
-
apply(e,
|
|
480
|
-
return this._renderer.apply(e, this._frame(
|
|
514
|
+
apply(e, s) {
|
|
515
|
+
return this._renderer.apply(e, this._frame(s));
|
|
481
516
|
}
|
|
482
517
|
get state() {
|
|
483
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 };
|
|
@@ -486,10 +521,10 @@ class O {
|
|
|
486
521
|
this._disposed || (this._disposed = !0, this._lights.clear(), this._occluders.clear(), this._receivers.clear(), this._renderer.dispose());
|
|
487
522
|
}
|
|
488
523
|
}
|
|
489
|
-
function
|
|
490
|
-
return new
|
|
524
|
+
function H(o = {}) {
|
|
525
|
+
return new I(o);
|
|
491
526
|
}
|
|
492
527
|
export {
|
|
493
|
-
|
|
494
|
-
|
|
528
|
+
I as NovaLighting2D,
|
|
529
|
+
H as createLighting2D
|
|
495
530
|
};
|
|
@@ -5,6 +5,7 @@ export interface LightingFrame2D {
|
|
|
5
5
|
lights: LightState2D[];
|
|
6
6
|
occluders: Readonly<NovaLightOccluderOptions2D>[];
|
|
7
7
|
receivers: Readonly<NovaLightReceiverOptions2D>[];
|
|
8
|
+
ignoredOccluders: ReadonlyMap<Readonly<NovaLightReceiverOptions2D>, number>;
|
|
8
9
|
receiverRevision: number;
|
|
9
10
|
ambient: readonly number[];
|
|
10
11
|
bounds: NovaLightingBounds2D;
|
|
@@ -7,6 +7,7 @@ import { NovaDirectionalLightOptions2D, NovaLightingHandle2D, NovaLightingOption
|
|
|
7
7
|
export declare class NovaLighting2D {
|
|
8
8
|
private readonly _lights;
|
|
9
9
|
private readonly _occluders;
|
|
10
|
+
private readonly _occluderValues;
|
|
10
11
|
private readonly _receivers;
|
|
11
12
|
private readonly _renderer;
|
|
12
13
|
private readonly _options;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export declare const lightingVertex2D = "#version 300 es\nprecision highp float;\nout vec2 uv;\nvoid main(){\n vec2 p=vec2(float((gl_VertexID<<1)&2),float(gl_VertexID&2));\n uv=vec2(p.x,1.-p.y);\n gl_Position=vec4(p*2.-1.,0.,1.);\n}";
|
|
2
|
-
export declare const lightingFragment2D = "#version 300 es\nprecision highp float;\nprecision highp int;\nin vec2 uv;\nout vec4 outColor;\nuniform sampler2D heights;\nuniform highp sampler2D geometry;\nuniform vec4 bounds;\nuniform vec3 ambient;\nuniform float groundScale, maxHeight;\nuniform int lightCount, casterCount;\nuniform vec4 positions[8], colors[8], directions[8], cones[8], shadows[8];\nfloat cross2(vec2 a,vec2 b){return a.x*b.y-a.y*b.x;}\nbool blocked(vec3 p,vec3 end,int layers){\n vec3 d=end-p;\n for(int c=0;c<64;c++){\n if(c>=casterCount)break;\n vec4 h=texelFetch(geometry,ivec2(0,c),0);\n if((int(h.z)&layers)==0)continue;\n vec4 bb=texelFetch(geometry,ivec2(9,c),0);\n if(any(lessThan(max(p.xy,end.xy),bb.xy))||any(greaterThan(min(p.xy,end.xy),bb.zw)))continue;\n float lo=.001, hi=.999;\n if(abs(d.z)<.00001){if(p.z<h.y||p.z>h.y+h.x)continue;}\n else{\n float a=(h.y-p.z)/d.z,b=(h.y+h.x-p.z)/d.z;\n lo=max(lo,min(a,b));hi=min(hi,max(a,b));\n }\n int n=int(h.w);\n for(int i=0;i<8;i++){\n if(i>=n||lo>hi)break;\n vec2 a=texelFetch(geometry,ivec2(i+1,c),0).xy;\n vec2 b=texelFetch(geometry,ivec2((i+1)%n+1,c),0).xy;\n float f=cross2(b-a,p.xy-a),g=cross2(b-a,d.xy);\n if(abs(g)<.00001){if(f<0.){hi=-1.;break;}}\n else if(g>0.)lo=max(lo,-f/g);\n else hi=min(hi,-f/g);\n }\n if(lo<hi)return true;\n }\n return false;\n}\nvoid main(){\n vec2 screen=bounds.xy+uv*bounds.zw;\n vec4 field=texture(heights,uv);\n if(
|
|
2
|
+
export declare const lightingFragment2D = "#version 300 es\nprecision highp float;\nprecision highp int;\nin vec2 uv;\nout vec4 outColor;\nuniform sampler2D heights;\nuniform highp sampler2D geometry;\nuniform vec4 bounds;\nuniform vec3 ambient;\nuniform float groundScale, maxHeight;\nuniform int lightCount, casterCount, categoricalField;\nuniform vec4 positions[8], colors[8], directions[8], cones[8], shadows[8];\nfloat cross2(vec2 a,vec2 b){return a.x*b.y-a.y*b.x;}\nbool blocked(vec3 p,vec3 end,int layers,int ignored){\n vec3 d=end-p;\n for(int c=0;c<64;c++){\n if(c>=casterCount)break;\n if(c==ignored)continue;\n vec4 h=texelFetch(geometry,ivec2(0,c),0);\n if((int(h.z)&layers)==0)continue;\n vec4 bb=texelFetch(geometry,ivec2(9,c),0);\n if(any(lessThan(max(p.xy,end.xy),bb.xy))||any(greaterThan(min(p.xy,end.xy),bb.zw)))continue;\n float lo=.001, hi=.999;\n if(abs(d.z)<.00001){if(p.z<h.y||p.z>h.y+h.x)continue;}\n else{\n float a=(h.y-p.z)/d.z,b=(h.y+h.x-p.z)/d.z;\n lo=max(lo,min(a,b));hi=min(hi,max(a,b));\n }\n int n=int(h.w);\n for(int i=0;i<8;i++){\n if(i>=n||lo>hi)break;\n vec2 a=texelFetch(geometry,ivec2(i+1,c),0).xy;\n vec2 b=texelFetch(geometry,ivec2((i+1)%n+1,c),0).xy;\n float f=cross2(b-a,p.xy-a),g=cross2(b-a,d.xy);\n if(abs(g)<.00001){if(f<0.){hi=-1.;break;}}\n else if(g>0.)lo=max(lo,-f/g);\n else hi=min(hi,-f/g);\n }\n if(lo<hi)return true;\n }\n return false;\n}\nvoid main(){\n vec2 screen=bounds.xy+uv*bounds.zw;\n vec4 field=texture(heights,uv);\n ivec2 size=textureSize(heights,0);\n vec4 material=categoricalField>0?texelFetch(heights,clamp(ivec2(uv*vec2(size)),ivec2(0),size-1),0):field;\n if(material.b>(categoricalField>0?.99:.5)){outColor=vec4(vec3(64./255.),1.);return;}\n float z=field.r*maxHeight;\n int receiverLayers=int(round(material.g*255.));\n int ignored=categoricalField>0?int(round(material.b*255.))-1:-1;\n vec3 p=vec3(screen.x,(screen.y+z)/groundScale,z);\n vec3 light=ambient;\n for(int i=0;i<8;i++){\n if(i>=lightCount)break;\n int layers=int(cones[i].z);\n if((layers&receiverLayers)==0)continue;\n bool directional=positions[i].w<.5;\n vec3 end=directional?p-directions[i].xyz*shadows[i].w:positions[i].xyz;\n float amount=colors[i].w;\n if(!directional){\n vec3 delta=p-end;\n float dist=length(delta);\n if(dist>=directions[i].w)continue;\n amount*=pow(max(0.,1.-dist/directions[i].w),cones[i].w);\n if(positions[i].w>1.5)amount*=smoothstep(cones[i].y,cones[i].x,dot(delta/max(dist,.00001),directions[i].xyz));\n }\n if(amount<.00001)continue;\n if(shadows[i].x>0.){\n float visibility=0.;\n float radius=shadows[i].y*(directional?shadows[i].w:1.);\n int samples=radius>0.?8:1;\n for(int s=0;s<8;s++){\n if(s>=samples)break;\n float angle=float(s)*2.39996323;\n vec2 disk=vec2(cos(angle),sin(angle))*sqrt((float(s)+.5)/8.);\n vec3 target=end+vec3(disk*radius,0.);\n visibility+=blocked(p,target,layers,ignored)?0.:1./float(samples);\n }\n amount*=mix(1.,visibility,shadows[i].x);\n }\n light+=colors[i].rgb*amount;\n }\n // 1 maps EXACTLY to byte 64: the neutral baseline does not introduce a seam.\n outColor=vec4(clamp(light*(64./255.),0.,1.),1.);\n}";
|
|
3
3
|
export declare const lightingComposite2D = "#version 300 es\nprecision highp float;\nin vec2 uv;\nout vec4 outColor;\nuniform sampler2D albedo, illumination;\nvec3 linearRGB(vec3 c){return mix(c/12.92,pow((c+.055)/1.055,vec3(2.4)),step(vec3(.04045),c));}\nvec3 srgb(vec3 c){return mix(c*12.92,1.055*pow(max(c,vec3(0.)),vec3(1./2.4))-.055,step(vec3(.0031308),c));}\nvoid main(){\n vec4 art=texture(albedo,uv);\n vec3 irradiance=texture(illumination,vec2(uv.x,1.-uv.y)).rgb*(255./64.);\n outColor=vec4(srgb(clamp(linearRGB(art.rgb)*irradiance,0.,1.)),art.a);\n}";
|
|
@@ -64,6 +64,11 @@ export interface NovaLightOccluderOptions2D {
|
|
|
64
64
|
enabled?: boolean;
|
|
65
65
|
}
|
|
66
66
|
export interface NovaLightReceiverOptions2D {
|
|
67
|
+
/**
|
|
68
|
+
* Skip this same-scene proxy for this receiver only; it still shadows the world.
|
|
69
|
+
* A disposed/disabled/culled proxy no longer excludes anything. Foreign handles reject.
|
|
70
|
+
*/
|
|
71
|
+
ignoreOccluder?: NovaLightingHandle2D<NovaLightOccluderOptions2D>;
|
|
67
72
|
/** Borrowed alpha silhouette. The engine never closes this image. */
|
|
68
73
|
image: CanvasImageSource;
|
|
69
74
|
/** Projected image bounds; top-of-image height ramps down to its bottom. */
|