@margoengine/lighting2d 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +352 -2
- package/dist/cookie.d.ts +104 -0
- package/dist/cookie.js +701 -0
- package/dist/emissive.d.ts +135 -0
- package/dist/emissive.js +345 -0
- package/dist/gi.d.ts +144 -0
- package/dist/gi.js +546 -0
- package/dist/height.d.ts +115 -0
- package/dist/height.js +411 -0
- package/dist/index.d.ts +192 -0
- package/dist/index.js +1 -0
- package/dist/masks.d.ts +11 -0
- package/dist/resources-Cskg0FqE.js +118 -0
- package/dist/resources.d.ts +10 -0
- package/dist/sdf-shader.d.ts +2 -0
- package/dist/sdf.d.ts +68 -0
- package/dist/sdf.js +234 -0
- package/dist/shared/texture-option.d.ts +14 -0
- package/dist/soft.d.ts +85 -0
- package/dist/soft.js +375 -0
- package/dist/src-8W7SHUG7.js +539 -0
- package/dist/validation.d.ts +10 -0
- package/package.json +79 -4
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const d=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./resources-Cskg0FqE.js";
|
|
5
|
+
import { hexColor as u } from "@margoengine/math";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
//#region src/masks.ts
|
|
8
|
+
var f = class {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.fields = [];
|
|
11
|
+
}
|
|
12
|
+
upload(e, t) {
|
|
13
|
+
let n = [];
|
|
14
|
+
this.fields = t.map(({ mask: e }) => {
|
|
15
|
+
if (!e) return [
|
|
16
|
+
0,
|
|
17
|
+
0,
|
|
18
|
+
0,
|
|
19
|
+
0,
|
|
20
|
+
0,
|
|
21
|
+
0,
|
|
22
|
+
0,
|
|
23
|
+
0
|
|
24
|
+
];
|
|
25
|
+
let t = +!!e.invert;
|
|
26
|
+
if (e.kind === "rect") return [
|
|
27
|
+
e.minX,
|
|
28
|
+
e.minY,
|
|
29
|
+
e.maxX,
|
|
30
|
+
e.maxY,
|
|
31
|
+
1,
|
|
32
|
+
t,
|
|
33
|
+
0,
|
|
34
|
+
0
|
|
35
|
+
];
|
|
36
|
+
if (e.kind === "circle") return [
|
|
37
|
+
e.center?.[0] ?? 0,
|
|
38
|
+
e.center?.[1] ?? 0,
|
|
39
|
+
0,
|
|
40
|
+
e.radius ** 2,
|
|
41
|
+
2,
|
|
42
|
+
t,
|
|
43
|
+
0,
|
|
44
|
+
0
|
|
45
|
+
];
|
|
46
|
+
let r = n.length / 4;
|
|
47
|
+
for (let [t, r] of e.points) n.push(t, r, 0, 0);
|
|
48
|
+
return [
|
|
49
|
+
0,
|
|
50
|
+
0,
|
|
51
|
+
0,
|
|
52
|
+
0,
|
|
53
|
+
3,
|
|
54
|
+
t,
|
|
55
|
+
r,
|
|
56
|
+
e.points.length
|
|
57
|
+
];
|
|
58
|
+
});
|
|
59
|
+
let i = new Float32Array(n.length ? n : [
|
|
60
|
+
0,
|
|
61
|
+
0,
|
|
62
|
+
0,
|
|
63
|
+
0
|
|
64
|
+
]);
|
|
65
|
+
if (i.byteLength > e.limits.maxStorageBufferBindingSize) throw r("Light mask vertices exceed maxStorageBufferBindingSize", "pixel-lighting");
|
|
66
|
+
(!this.buffer || this.buffer.size < i.byteLength) && (this.buffer?.destroy(), this.buffer = e.createBuffer({
|
|
67
|
+
size: i.byteLength,
|
|
68
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
69
|
+
})), e.queue.writeBuffer(this.buffer, 0, i);
|
|
70
|
+
}
|
|
71
|
+
dispose() {
|
|
72
|
+
this.buffer?.destroy(), this.buffer = void 0, this.fields = [];
|
|
73
|
+
}
|
|
74
|
+
}, p = (e) => `
|
|
75
|
+
@group(0)@binding(${e}
|
|
76
|
+
)var<storage,read>maskPoints:array<vec4f>;
|
|
77
|
+
fn maskVisible(world:vec2f,bounds:vec4f,info:vec4f)->bool{
|
|
78
|
+
let kind=u32(info.x);
|
|
79
|
+
if(kind==0u){
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
var inside=false;
|
|
83
|
+
if(kind==1u){
|
|
84
|
+
inside=all(world>=bounds.xy)&&all(world<=bounds.zw);
|
|
85
|
+
}
|
|
86
|
+
else if(kind==2u){
|
|
87
|
+
let d=world-bounds.xy;
|
|
88
|
+
inside=dot(d,d)<=bounds.w;
|
|
89
|
+
}
|
|
90
|
+
else{
|
|
91
|
+
let start=u32(info.z);
|
|
92
|
+
let count=u32(info.w);
|
|
93
|
+
var j=count-1u;
|
|
94
|
+
for(var i=0u;
|
|
95
|
+
i<count;
|
|
96
|
+
i++){
|
|
97
|
+
let a=maskPoints[start+i].xy;
|
|
98
|
+
let b=maskPoints[start+j].xy;
|
|
99
|
+
if((a.y>world.y)!=(b.y>world.y)){
|
|
100
|
+
if(world.x<(b.x-a.x)*(world.y-a.y)/(b.y-a.y)+a.x){
|
|
101
|
+
inside=!inside;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
j=i;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return select(inside,!inside,info.y>0.5);
|
|
108
|
+
}
|
|
109
|
+
`;
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/index.ts
|
|
112
|
+
function m(e, t, n) {
|
|
113
|
+
let r = !1;
|
|
114
|
+
if (e.kind === "rect") r = t >= e.minX && t <= e.maxX && n >= e.minY && n <= e.maxY;
|
|
115
|
+
else if (e.kind === "circle") {
|
|
116
|
+
let i = e.center ? e.center[0] : 0, a = e.center ? e.center[1] : 0;
|
|
117
|
+
r = Math.hypot(t - i, n - a) <= e.radius;
|
|
118
|
+
} else if (e.kind === "polygon") {
|
|
119
|
+
let i = e.points;
|
|
120
|
+
for (let e = 0, a = i.length - 1; e < i.length; a = e++) {
|
|
121
|
+
let o = i[e][0], s = i[e][1], c = i[a][0], l = i[a][1];
|
|
122
|
+
s > n != l > n && t < (c - o) * (n - s) / (l - s) + o && (r = !r);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return e.invert ? !r : r;
|
|
126
|
+
}
|
|
127
|
+
function h(e, t) {
|
|
128
|
+
return ((e.layers ?? 4294967295) & t) !== 0;
|
|
129
|
+
}
|
|
130
|
+
var g = class {
|
|
131
|
+
constructor(e = {}) {
|
|
132
|
+
this.kind = e.kind ?? "point", this.x = e.x ?? 0, this.y = e.y ?? 0, this.color = e.color ?? "#ffffff", this.intensity = e.intensity ?? 1, this.radius = e.radius ?? 64, this.falloff = e.falloff ?? 2, this.height = e.height ?? 32, this.angle = e.angle ?? 0, this.cone = e.cone ?? Math.PI / 4, this.softness = e.softness ?? 0, this.elevation = e.elevation ?? Math.PI / 4, this.shadows = e.shadows ?? !0, this.layers = e.layers ?? 4294967295, this.mask = e.mask, a(this);
|
|
133
|
+
}
|
|
134
|
+
}, _ = (e) => [
|
|
135
|
+
-Math.cos(e.angle) * Math.cos(e.elevation),
|
|
136
|
+
-Math.sin(e.angle) * Math.cos(e.elevation),
|
|
137
|
+
Math.sin(e.elevation)
|
|
138
|
+
];
|
|
139
|
+
function v(e, t, n, r = [
|
|
140
|
+
0,
|
|
141
|
+
0,
|
|
142
|
+
1
|
|
143
|
+
], i) {
|
|
144
|
+
if (a(e), i !== void 0 && (e.layers & i) === 0 || e.mask && !m(e.mask, t, n)) return [
|
|
145
|
+
0,
|
|
146
|
+
0,
|
|
147
|
+
0
|
|
148
|
+
];
|
|
149
|
+
let o = Math.hypot(r[0], r[1], r[2]), s = r.map((e) => e / o), c = u(e.color), l = 1, d;
|
|
150
|
+
if (e.kind === "directional") d = _(e);
|
|
151
|
+
else {
|
|
152
|
+
let r = e.x - t, i = e.y - n, a = Math.hypot(r, i), o = Math.hypot(a, e.height);
|
|
153
|
+
if (l = Math.max(0, 1 - a / e.radius) ** e.falloff, d = o ? [
|
|
154
|
+
r / o,
|
|
155
|
+
i / o,
|
|
156
|
+
e.height / o
|
|
157
|
+
] : s, e.kind === "spot") {
|
|
158
|
+
let t = a ? -(r * Math.cos(e.angle) + i * Math.sin(e.angle)) / a : 1, n = Math.cos(e.cone), o = Math.cos(e.cone * (1 - e.softness));
|
|
159
|
+
l *= o > n ? Math.min(1, Math.max(0, (t - n) / (o - n))) : +(t >= n);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
let f = l * Math.max(0, s[0] * d[0] + s[1] * d[1] + s[2] * d[2]) * e.intensity;
|
|
163
|
+
return [
|
|
164
|
+
c[0] * f,
|
|
165
|
+
c[1] * f,
|
|
166
|
+
c[2] * f
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
var y = 24;
|
|
170
|
+
function b(e) {
|
|
171
|
+
let t = [], n = (e, n) => {
|
|
172
|
+
for (let n = 0; n + 1 < e.length; n++) t.push(...e[n], ...e[n + 1]);
|
|
173
|
+
n && e.length > 2 && t.push(...e[e.length - 1], ...e[0]);
|
|
174
|
+
};
|
|
175
|
+
for (let i of e) if (i.shape === "polygon") n(i.points, !0);
|
|
176
|
+
else if (i.shape === "polyline") n(i.points, !1);
|
|
177
|
+
else if (i.shape === "segment") t.push(...i.from, ...i.to);
|
|
178
|
+
else if (i.shape === "box" || i.shape === "circle") {
|
|
179
|
+
let [e, t] = i.offset ?? [0, 0], r = i.shape === "box" ? i.rotation ?? 0 : 0, a = Math.cos(r), o = Math.sin(r), s = (n, r) => [e + n * a - r * o, t + n * o + r * a];
|
|
180
|
+
if (i.shape === "box") {
|
|
181
|
+
let e = i.width / 2, t = i.height / 2;
|
|
182
|
+
n([
|
|
183
|
+
s(-e, -t),
|
|
184
|
+
s(e, -t),
|
|
185
|
+
s(e, t),
|
|
186
|
+
s(-e, t)
|
|
187
|
+
], !0);
|
|
188
|
+
} else n(Array.from({ length: y }, (e, t) => {
|
|
189
|
+
let n = t / y * Math.PI * 2;
|
|
190
|
+
return s(i.radius * Math.cos(n), i.radius * Math.sin(n));
|
|
191
|
+
}), !0);
|
|
192
|
+
} else throw r("Unknown occluder shape", "pixel-lighting");
|
|
193
|
+
if (!t.every(Number.isFinite)) throw r("Invalid occluder value", "pixel-lighting");
|
|
194
|
+
return Float32Array.from(t);
|
|
195
|
+
}
|
|
196
|
+
function x(e, t, n, r) {
|
|
197
|
+
if (!e.shadows) return !1;
|
|
198
|
+
let i = e.kind !== "directional", a = _(e), o = i ? e.x - n : a[0], s = i ? e.y - r : a[1];
|
|
199
|
+
for (let e = 0; e + 3 < t.length; e += 4) {
|
|
200
|
+
let a = t[e] - n, c = t[e + 1] - r, l = t[e + 2] - t[e], u = t[e + 3] - t[e + 1], d = o * u - s * l;
|
|
201
|
+
if (!d) continue;
|
|
202
|
+
let f = (a * u - c * l) / d, p = (a * s - c * o) / d;
|
|
203
|
+
if (f >= 0 && (!i || f <= 1) && p >= 0 && p <= 1) return !0;
|
|
204
|
+
}
|
|
205
|
+
return !1;
|
|
206
|
+
}
|
|
207
|
+
var S = "\nstruct L{view:vec4f,size:vec4f,at:vec4f,way:vec4f,color:vec4f,mask_bounds:vec4f,mask_info:vec4f}\n@group(0) @binding(0) var<uniform> u:L;\n@group(0) @binding(1) var bumps:texture_2d<f32>;\n@group(0) @binding(2) var sum:texture_2d_array<f32>;\n@vertex fn wide(@builtin(vertex_index) i:u32)->@builtin(position) vec4f{\nreturn vec4f(f32(i&1u)*4-1,f32(i>>1u)*4-1,0,1);\n}\nfn clip(d:vec2f)->vec2f{return d*u.view.z/u.size.xy*vec2f(2,-2);}\n@vertex fn edge(@builtin(vertex_index) i:u32,@location(0) e:vec4f)->@builtin(position) vec4f{\nvar p=e.xy;\nif((i&1u)==1u){p=e.zw;}\nif(i<2u){return vec4f(clip(p-u.view.xy)+vec2f(-1,1),0,1);}\nreturn vec4f(clip(p*u.at.w-u.at.xy),0,0);\n}\n@fragment fn none()->@location(0) vec4f{return vec4f(0);}\n@fragment fn show(@builtin(position) f:vec4f)->@location(0) vec4f{let p=vec2i(f.xy);return vec4f(textureLoad(sum,p,0,0).rgb+textureLoad(sum,p,1,0).rgb+textureLoad(sum,p,2,0).rgb+textureLoad(sum,p,3,0).rgb,1);}\n@fragment fn lit(@builtin(position) f:vec4f)->@location(0) vec4f{\nlet world=u.view.xy+f.xy/u.view.z;\nif(!maskVisible(world,u.mask_bounds,u.mask_info)){return vec4f(0);}\nvar n=vec3f(0,0,1);\nif(u.size.z>0){\nlet s=textureLoad(bumps,vec2i(f.xy),0);\nif(s.a>0){n=normalize(s.xyz*2-1);}\n}\nvar l=u.at.xyz;\nvar a=1.0;\nif(u.at.w>0){\nlet d=u.at.xy-world;\nlet far=length(d);\na=pow(max(0,1-far/u.color.w),u.way.w);\nlet v=vec3f(d,u.at.z);\nl=n;\nif(dot(v,v)>0){l=normalize(v);}\nif(u.size.w>0){\nvar c=1.0;\nif(far>0){c=-dot(d,u.way.xy)/far;}\nif(u.way.z>u.view.w){a*=clamp((c-u.view.w)/(u.way.z-u.view.w),0,1);}\nelse if(c<u.view.w){a=0;}\n}\n}\nreturn vec4f(u.color.rgb*a*max(0,dot(n,l)),0);\n}", C = 256, w = "rgba16float", T = 4, E = C / 4, D = 0, O = class m {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
208
|
+
#e;
|
|
209
|
+
#t;
|
|
210
|
+
#n;
|
|
211
|
+
#r;
|
|
212
|
+
#i;
|
|
213
|
+
#a;
|
|
214
|
+
#o;
|
|
215
|
+
#s;
|
|
216
|
+
#c;
|
|
217
|
+
#l;
|
|
218
|
+
#u;
|
|
219
|
+
#d;
|
|
220
|
+
#f;
|
|
221
|
+
#p;
|
|
222
|
+
#m;
|
|
223
|
+
#h;
|
|
224
|
+
#g;
|
|
225
|
+
#_;
|
|
226
|
+
#v;
|
|
227
|
+
#y;
|
|
228
|
+
#b;
|
|
229
|
+
#x;
|
|
230
|
+
#S;
|
|
231
|
+
#C;
|
|
232
|
+
#w;
|
|
233
|
+
#T;
|
|
234
|
+
#E;
|
|
235
|
+
#D;
|
|
236
|
+
constructor(e) {
|
|
237
|
+
if (this.lights = [], this.#n = new c(), this.#i = /* @__PURE__ */ new Float32Array(), this.#a = !1, this.#v = [], this.#b = /* @__PURE__ */ new Float32Array(), this.#E = new f(), this.#D = !1, !e?.target) throw l("Lighting2D needs a target", "pixel-lighting");
|
|
238
|
+
let t = e.order ?? 20;
|
|
239
|
+
s(t, "order"), this.#e = new o(e.target, "Lighting2D target", "setTarget"), this.#t = new o(e.normals, "Lighting2D normals", "setNormals"), this.#r = t, this.camera = e.camera ?? {
|
|
240
|
+
x: 0,
|
|
241
|
+
y: 0,
|
|
242
|
+
zoom: 1
|
|
243
|
+
}, this.ambient = e.ambient ?? "#000000", this.layer = e.layer ?? 4294967295, this.name = e.name ?? `lighting2d-${++D}`, e.occluders && this.setOccluders(e.occluders);
|
|
244
|
+
}
|
|
245
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
246
|
+
let n = new m(t);
|
|
247
|
+
return d(e).use(n), n;
|
|
248
|
+
}
|
|
249
|
+
addLight(e) {
|
|
250
|
+
let t = new g(e);
|
|
251
|
+
return this.lights.push(t), t;
|
|
252
|
+
}
|
|
253
|
+
removeLight(e) {
|
|
254
|
+
let t = this.lights.indexOf(e);
|
|
255
|
+
t >= 0 && this.lights.splice(t, 1);
|
|
256
|
+
}
|
|
257
|
+
setOccluders(e) {
|
|
258
|
+
this.#i = b(e), this.#a = !1;
|
|
259
|
+
}
|
|
260
|
+
setTarget(e) {
|
|
261
|
+
if (!e) throw l("Lighting2D needs a target", "pixel-lighting");
|
|
262
|
+
this.#e.set(e, this.#o?.gpu);
|
|
263
|
+
}
|
|
264
|
+
setNormals(e) {
|
|
265
|
+
this.#t.set(e, this.#o?.gpu);
|
|
266
|
+
}
|
|
267
|
+
setup(e) {
|
|
268
|
+
if (this.#D) throw t("disposed", "Lighting2D disposed; create a new one");
|
|
269
|
+
if (e.backend !== "webgpu") throw t("unsupported", "Lighting2D needs WebGPU", "webgpu");
|
|
270
|
+
this.#o = e, this.#e.fix(e.gpu), this.#t.fix(e.gpu), e.frameGraph.add({
|
|
271
|
+
name: this.name,
|
|
272
|
+
order: this.#r,
|
|
273
|
+
execute: (e) => {
|
|
274
|
+
e.backend === "webgpu" && !e.view?.index && this.#A(e);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
onDeviceRestored() {
|
|
279
|
+
this.#O();
|
|
280
|
+
}
|
|
281
|
+
dispose() {
|
|
282
|
+
this.#D || (this.#D = !0, this.#o?.sceneOf(this)?.removePlugin(this.name), this.#o = void 0, this.#O());
|
|
283
|
+
}
|
|
284
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
285
|
+
this.dispose();
|
|
286
|
+
}
|
|
287
|
+
#O() {
|
|
288
|
+
this.#E.dispose(), this.#y?.destroy(), this.#x?.destroy(), this.#S?.destroy(), this.#w?.destroy(), this.#g?.destroy(), this.#y = this.#x = this.#S = this.#C = this.#w = void 0, this.#c = this.#l = this.#u = this.#d = this.#f = this.#p = void 0, this.#m = this.#h = this.#g = this.#_ = void 0, this.#v = [], this.#T = this.#s = void 0, this.#n.clear(), this.#a = !1;
|
|
289
|
+
}
|
|
290
|
+
#k(e, t) {
|
|
291
|
+
if (this.#s !== e && (this.#s && this.#O(), this.#s = e), this.#c || (this.#c = e.createShaderModule({
|
|
292
|
+
label: "lighting2d",
|
|
293
|
+
code: p(3) + S
|
|
294
|
+
}), this.#l = e.createBindGroupLayout({ entries: [
|
|
295
|
+
{
|
|
296
|
+
binding: 0,
|
|
297
|
+
visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
|
|
298
|
+
buffer: {
|
|
299
|
+
type: "uniform",
|
|
300
|
+
hasDynamicOffset: !0,
|
|
301
|
+
minBindingSize: 112
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
binding: 1,
|
|
306
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
307
|
+
texture: { sampleType: "unfilterable-float" }
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
binding: 3,
|
|
311
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
312
|
+
buffer: { type: "read-only-storage" }
|
|
313
|
+
}
|
|
314
|
+
] }), this.#u = e.createPipelineLayout({ bindGroupLayouts: [this.#l] }), this.#w = e.createTexture({
|
|
315
|
+
size: [1, 1],
|
|
316
|
+
format: "rgba8unorm",
|
|
317
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
|
|
318
|
+
})), !this.#f) {
|
|
319
|
+
let t = this.#c, n = this.#u, r = w, i = (e, t) => ({
|
|
320
|
+
format: "stencil8",
|
|
321
|
+
stencilFront: {
|
|
322
|
+
compare: e,
|
|
323
|
+
passOp: t
|
|
324
|
+
},
|
|
325
|
+
stencilBack: {
|
|
326
|
+
compare: e,
|
|
327
|
+
passOp: t
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
this.#f = e.createRenderPipeline({
|
|
331
|
+
label: "lighting2d light",
|
|
332
|
+
layout: n,
|
|
333
|
+
vertex: {
|
|
334
|
+
module: t,
|
|
335
|
+
entryPoint: "wide"
|
|
336
|
+
},
|
|
337
|
+
fragment: {
|
|
338
|
+
module: t,
|
|
339
|
+
entryPoint: "lit",
|
|
340
|
+
targets: [{
|
|
341
|
+
format: r,
|
|
342
|
+
blend: {
|
|
343
|
+
color: {
|
|
344
|
+
srcFactor: "one",
|
|
345
|
+
dstFactor: "one"
|
|
346
|
+
},
|
|
347
|
+
alpha: {
|
|
348
|
+
srcFactor: "zero",
|
|
349
|
+
dstFactor: "one"
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}]
|
|
353
|
+
},
|
|
354
|
+
depthStencil: i("equal", "keep")
|
|
355
|
+
}), this.#p = e.createRenderPipeline({
|
|
356
|
+
label: "lighting2d shadow",
|
|
357
|
+
layout: n,
|
|
358
|
+
vertex: {
|
|
359
|
+
module: t,
|
|
360
|
+
entryPoint: "edge",
|
|
361
|
+
buffers: [{
|
|
362
|
+
arrayStride: 16,
|
|
363
|
+
stepMode: "instance",
|
|
364
|
+
attributes: [{
|
|
365
|
+
format: "float32x4",
|
|
366
|
+
offset: 0,
|
|
367
|
+
shaderLocation: 0
|
|
368
|
+
}]
|
|
369
|
+
}]
|
|
370
|
+
},
|
|
371
|
+
fragment: {
|
|
372
|
+
module: t,
|
|
373
|
+
entryPoint: "none",
|
|
374
|
+
targets: [{
|
|
375
|
+
format: r,
|
|
376
|
+
writeMask: 0
|
|
377
|
+
}]
|
|
378
|
+
},
|
|
379
|
+
primitive: { topology: "triangle-strip" },
|
|
380
|
+
depthStencil: i("always", "replace")
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
t !== this.#d && (this.#d = t, this.#m = e.createRenderPipeline({
|
|
384
|
+
label: "lighting2d show",
|
|
385
|
+
layout: "auto",
|
|
386
|
+
vertex: {
|
|
387
|
+
module: this.#c,
|
|
388
|
+
entryPoint: "wide"
|
|
389
|
+
},
|
|
390
|
+
fragment: {
|
|
391
|
+
module: this.#c,
|
|
392
|
+
entryPoint: "show",
|
|
393
|
+
targets: [{ format: t }]
|
|
394
|
+
}
|
|
395
|
+
}), this.#h = void 0);
|
|
396
|
+
}
|
|
397
|
+
#A(o) {
|
|
398
|
+
let s = o.device, c = this.#e.get(s, this.#o);
|
|
399
|
+
if (!c) return;
|
|
400
|
+
let l = this.#t.get(s, this.#o), d = this.camera, f = u(this.ambient), p = this.lights;
|
|
401
|
+
if (n(d), e(this.layer), !f) throw r("Invalid ambient color: use a hex color such as #rrggbb", "pixel-lighting");
|
|
402
|
+
if (l && (l.width !== c.width || l.height !== c.height)) throw t("state", "Lighting2D normals differ in size from the target", "pixel-lighting");
|
|
403
|
+
p.forEach(a), this.#k(s, c.format);
|
|
404
|
+
let m = p.filter((e) => (e.layers & this.layer) !== 0);
|
|
405
|
+
this.#E.upload(s, m);
|
|
406
|
+
let h = Math.max(1, m.length) * E;
|
|
407
|
+
if ((!this.#y || this.#b.length < h) && (this.#y?.destroy(), this.#b = new Float32Array(h * 2), this.#y = s.createBuffer({
|
|
408
|
+
label: "lighting2d",
|
|
409
|
+
size: this.#b.byteLength,
|
|
410
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
411
|
+
})), this.#T = this.#n.get(s, "light", this.#l, [
|
|
412
|
+
{
|
|
413
|
+
binding: 0,
|
|
414
|
+
resource: {
|
|
415
|
+
buffer: this.#y,
|
|
416
|
+
size: 112
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
binding: 1,
|
|
421
|
+
resource: i(l ?? this.#w)
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
binding: 3,
|
|
425
|
+
resource: { buffer: this.#E.buffer }
|
|
426
|
+
}
|
|
427
|
+
]), !this.#S || this.#S.width !== c.width || this.#S.height !== c.height) {
|
|
428
|
+
this.#S?.destroy(), this.#g?.destroy();
|
|
429
|
+
let e = [c.width, c.height];
|
|
430
|
+
this.#S = s.createTexture({
|
|
431
|
+
label: "lighting2d stencil",
|
|
432
|
+
size: e,
|
|
433
|
+
format: "stencil8",
|
|
434
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT
|
|
435
|
+
}), this.#C = this.#S.createView(), this.#g = s.createTexture({
|
|
436
|
+
label: "lighting2d sum",
|
|
437
|
+
size: [
|
|
438
|
+
e[0],
|
|
439
|
+
e[1],
|
|
440
|
+
T
|
|
441
|
+
],
|
|
442
|
+
format: w,
|
|
443
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
444
|
+
}), this.#_ = this.#g.createView({ dimension: "2d-array" }), this.#v = Array.from({ length: T }, (e, t) => this.#g.createView({
|
|
445
|
+
dimension: "2d",
|
|
446
|
+
baseArrayLayer: t,
|
|
447
|
+
arrayLayerCount: 1
|
|
448
|
+
})), this.#h = void 0;
|
|
449
|
+
}
|
|
450
|
+
this.#h ??= s.createBindGroup({
|
|
451
|
+
layout: this.#m.getBindGroupLayout(0),
|
|
452
|
+
entries: [{
|
|
453
|
+
binding: 2,
|
|
454
|
+
resource: this.#_
|
|
455
|
+
}]
|
|
456
|
+
});
|
|
457
|
+
let g = this.#i, v = g.length / 4;
|
|
458
|
+
this.#a ||= (g.length && (!this.#x || this.#x.size < g.byteLength) && (this.#x?.destroy(), this.#x = s.createBuffer({
|
|
459
|
+
label: "lighting2d edges",
|
|
460
|
+
size: g.byteLength * 2,
|
|
461
|
+
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
462
|
+
})), g.length && s.queue.writeBuffer(this.#x, 0, g), !0);
|
|
463
|
+
let y = this.#b;
|
|
464
|
+
m.forEach((e, t) => {
|
|
465
|
+
let n = u(e.color), r = e.kind === "directional", i = r ? _(e) : [
|
|
466
|
+
e.x,
|
|
467
|
+
e.y,
|
|
468
|
+
e.height
|
|
469
|
+
];
|
|
470
|
+
y.set([
|
|
471
|
+
d.x,
|
|
472
|
+
d.y,
|
|
473
|
+
d.zoom,
|
|
474
|
+
Math.cos(e.cone),
|
|
475
|
+
c.width,
|
|
476
|
+
c.height,
|
|
477
|
+
+!!l,
|
|
478
|
+
+(e.kind === "spot"),
|
|
479
|
+
i[0],
|
|
480
|
+
i[1],
|
|
481
|
+
i[2],
|
|
482
|
+
+!r,
|
|
483
|
+
Math.cos(e.angle),
|
|
484
|
+
Math.sin(e.angle),
|
|
485
|
+
Math.cos(e.cone * (1 - e.softness)),
|
|
486
|
+
e.falloff,
|
|
487
|
+
n[0] * e.intensity,
|
|
488
|
+
n[1] * e.intensity,
|
|
489
|
+
n[2] * e.intensity,
|
|
490
|
+
e.radius,
|
|
491
|
+
...this.#E.fields[t]
|
|
492
|
+
], t * E);
|
|
493
|
+
}), s.queue.writeBuffer(this.#y, 0, y), this.#v.forEach((e, t) => o.encoder.beginRenderPass({ colorAttachments: [{
|
|
494
|
+
view: e,
|
|
495
|
+
loadOp: "clear",
|
|
496
|
+
clearValue: t ? [
|
|
497
|
+
0,
|
|
498
|
+
0,
|
|
499
|
+
0,
|
|
500
|
+
1
|
|
501
|
+
] : [
|
|
502
|
+
f[0],
|
|
503
|
+
f[1],
|
|
504
|
+
f[2],
|
|
505
|
+
1
|
|
506
|
+
],
|
|
507
|
+
storeOp: "store"
|
|
508
|
+
}] }).end()), m.forEach((e, t) => {
|
|
509
|
+
let n = o.encoder.beginRenderPass({
|
|
510
|
+
colorAttachments: [{
|
|
511
|
+
view: this.#v[t % T],
|
|
512
|
+
loadOp: "load",
|
|
513
|
+
storeOp: "store"
|
|
514
|
+
}],
|
|
515
|
+
depthStencilAttachment: {
|
|
516
|
+
view: this.#C,
|
|
517
|
+
stencilLoadOp: "clear",
|
|
518
|
+
stencilClearValue: 0,
|
|
519
|
+
stencilStoreOp: "discard"
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
n.setBindGroup(0, this.#T, [t * C]), e.shadows && v && (n.setPipeline(this.#p), n.setStencilReference(1), n.setVertexBuffer(0, this.#x), n.draw(4, v)), n.setPipeline(this.#f), n.setStencilReference(0), n.draw(3), n.end();
|
|
523
|
+
});
|
|
524
|
+
let b = o.encoder.beginRenderPass({ colorAttachments: [{
|
|
525
|
+
view: i(c),
|
|
526
|
+
loadOp: "clear",
|
|
527
|
+
clearValue: [
|
|
528
|
+
0,
|
|
529
|
+
0,
|
|
530
|
+
0,
|
|
531
|
+
1
|
|
532
|
+
],
|
|
533
|
+
storeOp: "store"
|
|
534
|
+
}] });
|
|
535
|
+
b.setPipeline(this.#m), b.setBindGroup(0, this.#h), b.draw(3), b.end();
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
//#endregion
|
|
539
|
+
export { h as a, f as c, x as i, p as l, O as n, v as o, m as r, b as s, g as t };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Light2D } from './index.js';
|
|
2
|
+
/** Checks a finite scalar, optionally bounded below by zero or the smallest positive number. */
|
|
3
|
+
export declare function checkNumber(value: number, field: string, minimum?: number): void;
|
|
4
|
+
export declare function checkCamera(camera: {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
zoom: number;
|
|
8
|
+
}): void;
|
|
9
|
+
export declare function checkLayer(layer: number, field?: string): void;
|
|
10
|
+
export declare function checkLight(l: Light2D): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,81 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@margoengine/lighting2d",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "2D lights for Margo Engine: point, spot and directional lights with normal-mapped shading and hard shadows from occluder edges, for pixel layers.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"margoengine",
|
|
7
|
+
"webgpu",
|
|
8
|
+
"2d",
|
|
9
|
+
"lighting",
|
|
10
|
+
"shadows",
|
|
11
|
+
"normal-map"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://margoengine.com",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/scottcampbelldata/margoengine/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/scottcampbelldata/margoengine.git",
|
|
20
|
+
"directory": "packages/lighting2d"
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"author": "Scott Campbell",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "node ../../scripts/build-optional.js lighting2d",
|
|
30
|
+
"test": "npm run build && node --test tests/*.test.js",
|
|
31
|
+
"test:gpu": "cd ../.. && node packages/lighting2d/tests/browser-check.mjs",
|
|
32
|
+
"prepack": "npm run build"
|
|
33
|
+
},
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"exports": {
|
|
36
|
+
"./sdf": {
|
|
37
|
+
"types": "./dist/sdf.d.ts",
|
|
38
|
+
"import": "./dist/sdf.js"
|
|
39
|
+
},
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"import": "./dist/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./soft": {
|
|
45
|
+
"types": "./dist/soft.d.ts",
|
|
46
|
+
"import": "./dist/soft.js"
|
|
47
|
+
},
|
|
48
|
+
"./cookie": {
|
|
49
|
+
"types": "./dist/cookie.d.ts",
|
|
50
|
+
"import": "./dist/cookie.js"
|
|
51
|
+
},
|
|
52
|
+
"./gi": {
|
|
53
|
+
"types": "./dist/gi.d.ts",
|
|
54
|
+
"import": "./dist/gi.js"
|
|
55
|
+
},
|
|
56
|
+
"./height": {
|
|
57
|
+
"types": "./dist/height.d.ts",
|
|
58
|
+
"import": "./dist/height.js"
|
|
59
|
+
},
|
|
60
|
+
"./emissive": {
|
|
61
|
+
"types": "./dist/emissive.d.ts",
|
|
62
|
+
"import": "./dist/emissive.js"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"files": [
|
|
66
|
+
"dist",
|
|
67
|
+
"LICENSE",
|
|
68
|
+
"NOTICE"
|
|
69
|
+
],
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"@margoengine/core": "^1.0.0"
|
|
72
|
+
},
|
|
73
|
+
"dependencies": {
|
|
74
|
+
"@margoengine/math": "^1.0.0"
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@margoengine/physics2d": "^1.0.0",
|
|
78
|
+
"@margoengine/pixel": "^1.0.0",
|
|
79
|
+
"@margoengine/sprites": "^1.0.0"
|
|
80
|
+
}
|
|
81
|
+
}
|