@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,118 @@
|
|
|
1
|
+
import { hexColor as e } from "@margoengine/math";
|
|
2
|
+
//#region ../shared/errors.js
|
|
3
|
+
var t = (e, t) => Object.assign(Error(t), {
|
|
4
|
+
name: "MargoError",
|
|
5
|
+
code: e
|
|
6
|
+
}), n = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, r = (e, r, i = e) => t(e, n(r, i)), i = (e, t) => TypeError(n(e, t)), a = (e, t) => RangeError(n(e, t));
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/validation.ts
|
|
9
|
+
function o(e, t, n = -Infinity) {
|
|
10
|
+
if (!Number.isFinite(e) || e < n) throw a(`Invalid ${t}: must be finite${n === Number.MIN_VALUE ? " and positive" : n === 0 ? " and non-negative" : ""}`, "pixel-lighting");
|
|
11
|
+
}
|
|
12
|
+
function s(e) {
|
|
13
|
+
o(e.x, "camera.x"), o(e.y, "camera.y"), o(e.zoom, "camera.zoom", Number.MIN_VALUE);
|
|
14
|
+
}
|
|
15
|
+
function c(e, t = "layer") {
|
|
16
|
+
if (!Number.isInteger(e) || e < 0 || e > 4294967295) throw a(`Invalid ${t}: must be an integer in [0, 4294967295]`, "pixel-lighting");
|
|
17
|
+
}
|
|
18
|
+
function l(e) {
|
|
19
|
+
if (e.kind === "rect") {
|
|
20
|
+
if (![
|
|
21
|
+
e.minX,
|
|
22
|
+
e.minY,
|
|
23
|
+
e.maxX,
|
|
24
|
+
e.maxY
|
|
25
|
+
].every(Number.isFinite) || e.minX > e.maxX || e.minY > e.maxY) throw a("Invalid rect mask bounds: use finite minX <= maxX and minY <= maxY", "pixel-lighting");
|
|
26
|
+
} else if (e.kind === "circle") {
|
|
27
|
+
if (o(e.radius, "circle mask radius", 0), e.center && (e.center.length !== 2 || !e.center.every(Number.isFinite))) throw a("Invalid circle mask center: use two finite coordinates", "pixel-lighting");
|
|
28
|
+
} else if (e.kind === "polygon") {
|
|
29
|
+
if (!Array.isArray(e.points) || e.points.length < 3 || !e.points.every((e) => e.length === 2 && e.every(Number.isFinite))) throw a("Invalid polygon mask points: use at least three points with two finite coordinates each", "pixel-lighting");
|
|
30
|
+
} else throw a("Invalid mask kind: use rect, circle or polygon", "pixel-lighting");
|
|
31
|
+
}
|
|
32
|
+
function u(t) {
|
|
33
|
+
if (t.kind !== "point" && t.kind !== "spot" && t.kind !== "directional") throw a("Invalid light kind: use point, spot or directional", "pixel-lighting");
|
|
34
|
+
for (let e of [
|
|
35
|
+
"x",
|
|
36
|
+
"y",
|
|
37
|
+
"angle",
|
|
38
|
+
"elevation"
|
|
39
|
+
]) o(t[e], e);
|
|
40
|
+
for (let e of [
|
|
41
|
+
"radius",
|
|
42
|
+
"falloff",
|
|
43
|
+
"cone"
|
|
44
|
+
]) o(t[e], e, Number.MIN_VALUE);
|
|
45
|
+
for (let e of ["intensity", "height"]) o(t[e], e, 0);
|
|
46
|
+
if (!Number.isFinite(t.softness) || t.softness < 0 || t.softness > 1) throw a("Invalid softness: must be finite and in [0, 1]", "pixel-lighting");
|
|
47
|
+
if (c(t.layers, "layers"), t.mask && l(t.mask), !e(t.color)) throw a("Invalid light color: use a hex color such as #rrggbb", "pixel-lighting");
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region ../shared/borrowed.js
|
|
51
|
+
function d(e, t, n) {
|
|
52
|
+
return e.value = t, e.device = typeof t == "function" ? void 0 : n, e.made = void 0, e.told = !1, e;
|
|
53
|
+
}
|
|
54
|
+
function f(e, t) {
|
|
55
|
+
typeof e.value != "function" && (e.device ??= t);
|
|
56
|
+
}
|
|
57
|
+
var p = (e, t, n, r) => d({
|
|
58
|
+
name: t,
|
|
59
|
+
setter: n
|
|
60
|
+
}, e, r);
|
|
61
|
+
function m(e, t, n) {
|
|
62
|
+
let { value: i } = e;
|
|
63
|
+
if (typeof i == "function") return e.device !== t && (e.made = i(e.device = t)), e.made;
|
|
64
|
+
if (i != null) {
|
|
65
|
+
if (e.device ??= t, e.device === t) return i;
|
|
66
|
+
e.told || (e.told = !0, n?.onError(r("state", `${e.name} lost with the device; pass a function or call ${e.setter}`, "borrowed-lost")));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region ../shared/texture-option.js
|
|
71
|
+
var h = class {
|
|
72
|
+
constructor(e, t, n) {
|
|
73
|
+
this.value = e, this.slot = p(typeof e == "function" ? void 0 : e, t, n);
|
|
74
|
+
}
|
|
75
|
+
set(e, t) {
|
|
76
|
+
this.value = e, d(this.slot, typeof e == "function" ? void 0 : e, t);
|
|
77
|
+
}
|
|
78
|
+
fix(e) {
|
|
79
|
+
e && f(this.slot, e);
|
|
80
|
+
}
|
|
81
|
+
get(e, t) {
|
|
82
|
+
let n = this.value;
|
|
83
|
+
return typeof n == "function" ? n() : m(this.slot, e, t);
|
|
84
|
+
}
|
|
85
|
+
}, g = /* @__PURE__ */ new WeakMap();
|
|
86
|
+
function _(e) {
|
|
87
|
+
let t = g.get(e);
|
|
88
|
+
return t || g.set(e, t = e.createView()), t;
|
|
89
|
+
}
|
|
90
|
+
var v = (e, t) => {
|
|
91
|
+
let n = e.resource;
|
|
92
|
+
return t === 0 ? e.binding : t === 1 ? n.buffer ?? e.resource : t === 2 ? n.offset : n.size;
|
|
93
|
+
}, y = class {
|
|
94
|
+
#e = /* @__PURE__ */ new Map();
|
|
95
|
+
get(e, t, n, r) {
|
|
96
|
+
let i = this.#e.get(t);
|
|
97
|
+
if (i && i.device === e && i.layout === n && i.parts.length === r.length * 4) {
|
|
98
|
+
let e = !0;
|
|
99
|
+
for (let t = 0; e && t < i.parts.length; t++) e = i.parts[t] === v(r[t >> 2], t & 3);
|
|
100
|
+
if (e) return i.group;
|
|
101
|
+
}
|
|
102
|
+
let a = e.createBindGroup({
|
|
103
|
+
layout: n,
|
|
104
|
+
entries: r
|
|
105
|
+
});
|
|
106
|
+
return this.#e.set(t, {
|
|
107
|
+
device: e,
|
|
108
|
+
layout: n,
|
|
109
|
+
parts: Array.from({ length: r.length * 4 }, (e, t) => v(r[t >> 2], t & 3)),
|
|
110
|
+
group: a
|
|
111
|
+
}), a;
|
|
112
|
+
}
|
|
113
|
+
clear() {
|
|
114
|
+
this.#e.clear();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
//#endregion
|
|
118
|
+
export { c as a, r as c, s as i, a as l, _ as n, u as o, h as r, o as s, y as t, i as u };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { TextureOption, type TextureSource2D } from './shared/texture-option.js';
|
|
2
|
+
/** The texture's whole view, made once per texture. */
|
|
3
|
+
export declare function viewOf(texture: GPUTexture): GPUTextureView;
|
|
4
|
+
/** Bind groups by name, each made again only when its device, layout or anything it binds changes. */
|
|
5
|
+
export declare class BindGroups {
|
|
6
|
+
#private;
|
|
7
|
+
get(device: GPUDevice, name: string, layout: GPUBindGroupLayout, entries: GPUBindGroupEntry[]): GPUBindGroup;
|
|
8
|
+
/** Forgets every group, as at a device restore. */
|
|
9
|
+
clear(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const sdfShader = "\nstruct PaintSettings { camera:vec4f, material:vec4f }\n@group(3)@binding(0)var<uniform>paint:PaintSettings;\n@group(3)@binding(1)var paintedColor:texture_2d<f32>;\n@group(3)@binding(2)var paintedEmission:texture_2d<f32>;\n@group(3)@binding(3)var paintedDistance:texture_2d<f32>;\n@group(3)@binding(4)var<storage,read>paintChanged:array<u32>;\n\nfn tracePaint(p:vec2f,d:vec2f,reach:f32)->vec4f {\n let size=textureDimensions(paintedDistance);let origin=(p-paint.camera.xy)*paint.camera.z;\n let ray=d*paint.camera.z;var near=0.0;var far=reach;\n for(var j=0u;j<2u;j++){\n if(abs(ray[j])<1e-20){if(origin[j]<0||origin[j]>=f32(size[j])){return vec4f(0,0,0,-1);}}\n else{let a=-origin[j]/ray[j];let b=(f32(size[j])-origin[j])/ray[j];near=max(near,min(a,b));far=min(far,max(a,b));}\n }\n if(near>far){return vec4f(0,0,0,-1);}\n var t=near;\n for(var step=0u;step<size.x+size.y+4u;step++){\n if(t>far){break;}\n let q=origin+ray*t;let cell=vec2i(floor(q+sign(ray)*1e-5));\n if(any(cell<vec2i(0))||any(cell>=vec2i(size))){break;}\n let field=textureLoad(paintedDistance,cell,0);\n if(field.y>0){\n let e=textureLoad(paintedEmission,cell,0);var rgb=e.rgb*e.a*paint.material.y;\n if(t>1e-6){\n let f=q-vec2f(cell);let delta=select(vec2f(1)-f,f,ray>vec2f(0));\n var face=select(1u,0u,ray.x>0);if(abs(ray.x)<1e-20||(abs(ray.y)>1e-20&&delta.y<delta.x)){face=select(3u,2u,ray.y>0);}\n rgb+=surfaces[u.counts.x*2u+(u32(cell.y)*size.x+u32(cell.x))*4u+face].rgb;\n }\n return vec4f(rgb,t);\n }\n var next=vec2f(1e30);\n for(var j=0u;j<2u;j++){\n if(abs(ray[j])>1e-20){let boundary=f32(cell[j])+select(0.0,1.0,ray[j]>0);next[j]=(boundary-q[j])/ray[j];}\n }\n let safe=max(0,field.x-.7071068-length(q-(vec2f(cell)+.5)))/paint.camera.z;\n t+=max(safe,min(next.x,next.y)+1e-5/paint.camera.z);\n }\n return vec4f(0,0,0,-1);\n}\n";
|
|
2
|
+
export declare const paintedBounceShader = "\nfn bouncePaint(index:u32){\n let size=textureDimensions(paintedDistance);let pixel=index/4u;if(pixel>=size.x*size.y){return;}\n let cell=vec2i(i32(pixel%size.x),i32(pixel/size.x));let face=index%4u;\n let at=u.counts.x*2u+index;\n if(textureLoad(paintedDistance,cell,0).y==0){output[at]=vec4f(0);return;}\n var n=vec2f(-1,0);if(face==1u){n=vec2f(1,0);}if(face==2u){n=vec2f(0,-1);}if(face==3u){n=vec2f(0,1);}\n let neighbour=cell+vec2i(n);\n if(all(neighbour>=vec2i(0))&&all(neighbour<vec2i(size))){\n if(textureLoad(paintedDistance,neighbour,0).y>0){output[at]=vec4f(0);return;}\n }\n let p=paint.camera.xy+(vec2f(cell)+.5+n*.501)/paint.camera.z;\n var rgb=direct(p,n);var cosineSum=0.0;\n for(var k=0u;k<32u;k++){cosineSum+=max(0,dot(n,direction(k,32u)));}\n for(var k=0u;k<32u;k++){\n let d=direction(k,32u);rgb+=trace(p,d,u.misc.w).rgb*(max(0,dot(n,d))/cosineSum);\n }\n output[at]=vec4f(rgb*clamp(textureLoad(paintedColor,cell,0).rgb,vec3f(0),vec3f(1))*u.material.x,0);\n}\n";
|
package/dist/sdf.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
type TextureSource = GPUTexture | (() => GPUTexture);
|
|
3
|
+
/** What reports a lost texture and gives the device a new one belongs to: the engine of the pass that encodes. */
|
|
4
|
+
interface SDFOwner {
|
|
5
|
+
readonly gpu?: GPUDevice | null;
|
|
6
|
+
onError(error: unknown): void;
|
|
7
|
+
}
|
|
8
|
+
/** Painted world-space geometry consumed by GILighting2D. Textures contain straight, linear RGBA. */
|
|
9
|
+
export interface SDFScene2DOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Alpha defines opaque cells; RGB defines diffuse albedo. A function is read each frame (a pixel layer's target
|
|
12
|
+
* getter). A plain texture lost at a device restore leaves the painted scene out of the light, reported once,
|
|
13
|
+
* until a new one is assigned to {@link SDFScene2D.source}.
|
|
14
|
+
*/
|
|
15
|
+
readonly source: TextureSource;
|
|
16
|
+
/**
|
|
17
|
+
* Emission image, same dimensions as source; a function is read each frame. Default source (black pixels do not emit).
|
|
18
|
+
* A plain texture lost at a device restore uses the default, reported once, until a new one is assigned.
|
|
19
|
+
*/
|
|
20
|
+
readonly emission?: TextureSource;
|
|
21
|
+
/** Top-left world origin and pixels per world unit. Default {x:0,y:0,zoom:1}. Read each frame. */
|
|
22
|
+
readonly camera?: {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
zoom: number;
|
|
26
|
+
};
|
|
27
|
+
/** Positive alpha at or above this value is opaque. Default .5; range [0,1]. */
|
|
28
|
+
readonly alphaThreshold?: number;
|
|
29
|
+
/** Linear HDR emission multiplier, default 1, nonnegative. */
|
|
30
|
+
readonly emissionIntensity?: number;
|
|
31
|
+
}
|
|
32
|
+
/** Exact pixel-centre signed distance transform, rebuilt on the GPU before every GI frame. */
|
|
33
|
+
export declare class SDFScene2D implements Disposable {
|
|
34
|
+
#private;
|
|
35
|
+
/** Top-left world origin and pixel scale, read on every encode. */
|
|
36
|
+
camera: {
|
|
37
|
+
x: number;
|
|
38
|
+
y: number;
|
|
39
|
+
zoom: number;
|
|
40
|
+
};
|
|
41
|
+
/** Opaque alpha cutoff, default .5. */
|
|
42
|
+
alphaThreshold: number;
|
|
43
|
+
/** HDR emission multiplier, default 1. */
|
|
44
|
+
emissionIntensity: number;
|
|
45
|
+
constructor(options: SDFScene2DOptions);
|
|
46
|
+
/** Borrowed alpha/albedo texture or getter, read on every encode. Never destroyed by this object. */
|
|
47
|
+
get source(): TextureSource;
|
|
48
|
+
set source(value: TextureSource);
|
|
49
|
+
/** Borrowed emission texture or getter; undefined uses source. Never destroyed by this object. */
|
|
50
|
+
get emission(): TextureSource | undefined;
|
|
51
|
+
set emission(value: TextureSource | undefined);
|
|
52
|
+
/** Current rgba32float field: R signed distance in source pixels, G occupancy. Replaced on resize/restore. */
|
|
53
|
+
get distanceTexture(): GPUTexture | undefined;
|
|
54
|
+
/** GPU flag for exact source/emission/configuration changes in the current encoded snapshot. */
|
|
55
|
+
get changeBuffer(): GPUBuffer | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Encode the three exact transform passes into the caller's frame; GILighting2D calls this automatically and passes
|
|
58
|
+
* its engine as `owner`, which reports a lost texture once. Returns false, encoding nothing, while a plain source is
|
|
59
|
+
* lost with its device.
|
|
60
|
+
*/
|
|
61
|
+
encode(device: GPUDevice, encoder: GPUCommandEncoder, owner?: SDFOwner): boolean;
|
|
62
|
+
/** Bind the encoded field, source and emission for an owning lighting pass; the same group while nothing changed. */
|
|
63
|
+
bindGroup(device: GPUDevice, layout: GPUBindGroupLayout): GPUBindGroup;
|
|
64
|
+
/** Release owned scratch and field textures; source textures remain borrowed. */
|
|
65
|
+
dispose(): void;
|
|
66
|
+
[Symbol.dispose](): void;
|
|
67
|
+
}
|
|
68
|
+
export {};
|
package/dist/sdf.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { c as e, i as t, l as n, n as r, r as i, s as a, t as o, u as s } from "./resources-Cskg0FqE.js";
|
|
2
|
+
//#region src/sdf.ts
|
|
3
|
+
var c = "\nstruct Settings { camera:vec4f, material:vec4f }\n@group(0)@binding(0)var<uniform>u:Settings;\n@group(0)@binding(1)var image:texture_2d<f32>;\n@group(0)@binding(2)var<storage,read_write>rows:array<vec2f>;\n@group(0)@binding(3)var<storage,read_write>hull:array<vec2f>;\n@group(0)@binding(4)var<storage,read_write>squared:array<f32>;\n@group(0)@binding(5)var field:texture_storage_2d<rgba32float,write>;\n@group(0)@binding(6)var emission:texture_2d<f32>;\n@group(0)@binding(7)var<storage,read_write>history:array<vec4f>;\n@group(0)@binding(8)var<storage,read_write>changed:atomic<u32>;\nconst INF:f32=1e20;\n@compute @workgroup_size(64)fn horizontal(@builtin(global_invocation_id)id:vec3u){\n let size=textureDimensions(image);let y=id.x;if(y>=size.y){return;}\n var previous=vec2f(-1e10);\n for(var x=0u;x<size.x;x++){\n let a=textureLoad(image,vec2u(x,y),0).a;let solid=a>0&&a>=u.material.x;\n if(solid){previous.x=f32(x);}else{previous.y=f32(x);}\n let delta=vec2f(f32(x))-previous;rows[y*size.x+x]=delta*delta;\n }\n previous=vec2f(1e10);\n for(var x=i32(size.x)-1;x>=0;x--){\n let a=textureLoad(image,vec2i(x,i32(y)),0).a;let solid=a>0&&a>=u.material.x;\n if(solid){previous.x=f32(x);}else{previous.y=f32(x);}\n let delta=previous-vec2f(f32(x));let i=y*size.x+u32(x);rows[i]=min(rows[i],delta*delta);\n }\n}\n\n@compute @workgroup_size(64)fn vertical(@builtin(global_invocation_id)id:vec3u){\n let size=textureDimensions(image);if(id.x>=size.x*2u){return;}\n let x=id.x%size.x;let channel=id.x/size.x;let base=id.x*size.y;var k=-1;\n for(var y=0u;y<size.y;y++){\n let f=rows[y*size.x+x][channel];if(f>=INF){continue;}\n var start=-INF;\n loop {\n if(k<0){break;}\n let q=hull[base+u32(k)].x;let fq=rows[u32(q)*size.x+x][channel];\n start=((f+f32(y)*f32(y))-(fq+q*q))/(2*(f32(y)-q));\n if(start>hull[base+u32(k)].y){break;}k--;\n }\n if(k<0){start=-INF;}k++;hull[base+u32(k)]=vec2f(f32(y),start);\n }\n var cursor=0;\n for(var y=0u;y<size.y;y++){\n var d=INF;\n if(k>=0){\n loop{if(cursor>=k){break;}if(hull[base+u32(cursor+1)].y>=f32(y)){break;}cursor++;}\n let q=hull[base+u32(cursor)].x;d=(f32(y)-q)*(f32(y)-q)+rows[u32(q)*size.x+x][channel];\n }\n squared[channel*size.x*size.y+y*size.x+x]=d;\n }\n}\n@compute @workgroup_size(8,8)fn finish(@builtin(global_invocation_id)id:vec3u){\n let size=textureDimensions(image);if(any(id.xy>=size)){return;}\n let a=textureLoad(image,id.xy,0).a;let solid=a>0&&a>=u.material.x;let i=id.y*size.x+id.x;\n let color=textureLoad(image,id.xy,0);let light=textureLoad(emission,id.xy,0);\n if(any(color!=history[i*2u])||any(light!=history[i*2u+1u])){atomicOr(&changed,1u);}\n history[i*2u]=color;history[i*2u+1u]=light;\n var d=sqrt(squared[i]);if(solid){d=-sqrt(squared[size.x*size.y+i]);}\n textureStore(field,id.xy,vec4f(d,select(0.0,1.0,solid),0,0));\n}\n", l = class {
|
|
4
|
+
#e;
|
|
5
|
+
#t;
|
|
6
|
+
#n = [];
|
|
7
|
+
#r = [];
|
|
8
|
+
#i;
|
|
9
|
+
#a;
|
|
10
|
+
#o;
|
|
11
|
+
#s = !1;
|
|
12
|
+
#c;
|
|
13
|
+
#l;
|
|
14
|
+
#u;
|
|
15
|
+
#d;
|
|
16
|
+
#f;
|
|
17
|
+
#p = new o();
|
|
18
|
+
#m = /* @__PURE__ */ new Uint32Array(1);
|
|
19
|
+
#h = /* @__PURE__ */ new Float32Array(8);
|
|
20
|
+
constructor(e) {
|
|
21
|
+
if (!e?.source) throw s("SDFScene2D source is required", "pixel-lighting");
|
|
22
|
+
this.#d = new i(e.source, "SDFScene2D source", "assign a new source"), this.#f = new i(e.emission, "SDFScene2D emission", "assign a new emission"), this.camera = e.camera ?? {
|
|
23
|
+
x: 0,
|
|
24
|
+
y: 0,
|
|
25
|
+
zoom: 1
|
|
26
|
+
}, this.alphaThreshold = e.alphaThreshold ?? .5, this.emissionIntensity = e.emissionIntensity ?? 1, this.#g();
|
|
27
|
+
}
|
|
28
|
+
#g() {
|
|
29
|
+
if (t(this.camera), a(this.alphaThreshold, "alphaThreshold", 0), this.alphaThreshold > 1) throw n("alphaThreshold must be in [0,1]", "pixel-lighting");
|
|
30
|
+
a(this.emissionIntensity, "emissionIntensity", 0);
|
|
31
|
+
}
|
|
32
|
+
get source() {
|
|
33
|
+
return this.#d.value;
|
|
34
|
+
}
|
|
35
|
+
set source(e) {
|
|
36
|
+
if (!e) throw s("SDFScene2D source is required", "pixel-lighting");
|
|
37
|
+
this.#d.set(e, this.#u?.gpu);
|
|
38
|
+
}
|
|
39
|
+
get emission() {
|
|
40
|
+
return this.#f.value;
|
|
41
|
+
}
|
|
42
|
+
set emission(e) {
|
|
43
|
+
this.#f.set(e, this.#u?.gpu);
|
|
44
|
+
}
|
|
45
|
+
get distanceTexture() {
|
|
46
|
+
return this.#t;
|
|
47
|
+
}
|
|
48
|
+
get changeBuffer() {
|
|
49
|
+
return this.#n[5];
|
|
50
|
+
}
|
|
51
|
+
encode(t, i, a) {
|
|
52
|
+
if (this.#s) throw e("disposed", "SDFScene2D disposed; create a new one", "pixel-lighting");
|
|
53
|
+
if (this.#g(), a && (this.#u = a), this.#l === i && this.#e === t) return !0;
|
|
54
|
+
this.#e !== t && (this.#_(), this.#e = t);
|
|
55
|
+
let o = this.#d.get(t, this.#u);
|
|
56
|
+
if (!o) return !1;
|
|
57
|
+
let s = this.#f.get(t, this.#u) ?? o;
|
|
58
|
+
for (let e of [o, s]) if (e.dimension !== "2d" || e.depthOrArrayLayers !== 1 || e.sampleCount !== 1 || !(e.usage & 4) || !/^(rgba8unorm(-srgb)?|bgra8unorm(-srgb)?|rgba16float|rgba32float)$/.test(e.format)) throw n("SDF images must be single-sample 2D normalized or float RGBA textures with TEXTURE_BINDING", "pixel-lighting");
|
|
59
|
+
if (o.width !== s.width || o.height !== s.height) throw n("SDF emission dimensions must match source", "pixel-lighting");
|
|
60
|
+
let l = o.width * o.height;
|
|
61
|
+
if (l * 32 > t.limits.maxStorageBufferBindingSize || l * 32 > t.limits.maxBufferSize) throw n("SDF scratch exceeds device buffer limit", "pixel-lighting");
|
|
62
|
+
if (Math.ceil(Math.max(o.width * 2, o.height) / 64) > t.limits.maxComputeWorkgroupsPerDimension || Math.ceil(Math.max(o.width, o.height) / 8) > t.limits.maxComputeWorkgroupsPerDimension) throw n("SDF dispatch exceeds device limit", "pixel-lighting");
|
|
63
|
+
if (!this.#i) {
|
|
64
|
+
this.#i = t.createBindGroupLayout({ entries: [
|
|
65
|
+
{
|
|
66
|
+
binding: 0,
|
|
67
|
+
visibility: 4,
|
|
68
|
+
buffer: { type: "uniform" }
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
binding: 1,
|
|
72
|
+
visibility: 4,
|
|
73
|
+
texture: { sampleType: "unfilterable-float" }
|
|
74
|
+
},
|
|
75
|
+
...[
|
|
76
|
+
2,
|
|
77
|
+
3,
|
|
78
|
+
4
|
|
79
|
+
].map((e) => ({
|
|
80
|
+
binding: e,
|
|
81
|
+
visibility: 4,
|
|
82
|
+
buffer: { type: "storage" }
|
|
83
|
+
})),
|
|
84
|
+
{
|
|
85
|
+
binding: 5,
|
|
86
|
+
visibility: 4,
|
|
87
|
+
storageTexture: {
|
|
88
|
+
access: "write-only",
|
|
89
|
+
format: "rgba32float"
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
binding: 6,
|
|
94
|
+
visibility: 4,
|
|
95
|
+
texture: { sampleType: "unfilterable-float" }
|
|
96
|
+
},
|
|
97
|
+
...[7, 8].map((e) => ({
|
|
98
|
+
binding: e,
|
|
99
|
+
visibility: 4,
|
|
100
|
+
buffer: { type: "storage" }
|
|
101
|
+
}))
|
|
102
|
+
] });
|
|
103
|
+
let e = t.createShaderModule({
|
|
104
|
+
label: "exact painted distance transform",
|
|
105
|
+
code: c
|
|
106
|
+
}), n = t.createPipelineLayout({ bindGroupLayouts: [this.#i] });
|
|
107
|
+
this.#r = [
|
|
108
|
+
"horizontal",
|
|
109
|
+
"vertical",
|
|
110
|
+
"finish"
|
|
111
|
+
].map((r) => t.createComputePipeline({
|
|
112
|
+
layout: n,
|
|
113
|
+
compute: {
|
|
114
|
+
module: e,
|
|
115
|
+
entryPoint: r
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
if (!this.#t || this.#t.width !== o.width || this.#t.height !== o.height) {
|
|
120
|
+
this.#t?.destroy();
|
|
121
|
+
for (let e of this.#n) e.destroy();
|
|
122
|
+
this.#c = void 0, this.#n = [
|
|
123
|
+
32,
|
|
124
|
+
l * 8,
|
|
125
|
+
l * 16,
|
|
126
|
+
l * 8,
|
|
127
|
+
l * 32,
|
|
128
|
+
16
|
|
129
|
+
].map((e, n) => t.createBuffer({
|
|
130
|
+
size: e,
|
|
131
|
+
usage: (n ? 128 : 64) | 8
|
|
132
|
+
})), this.#t = t.createTexture({
|
|
133
|
+
label: "painted signed distance",
|
|
134
|
+
size: [o.width, o.height],
|
|
135
|
+
format: "rgba32float",
|
|
136
|
+
usage: 13
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
this.#a = o, this.#o = s;
|
|
140
|
+
let u = JSON.stringify([
|
|
141
|
+
this.camera,
|
|
142
|
+
this.alphaThreshold,
|
|
143
|
+
this.emissionIntensity
|
|
144
|
+
]);
|
|
145
|
+
this.#m[0] = this.#c === u ? 0 : 1, t.queue.writeBuffer(this.#n[5], 0, this.#m), this.#h.set([
|
|
146
|
+
this.camera.x,
|
|
147
|
+
this.camera.y,
|
|
148
|
+
this.camera.zoom,
|
|
149
|
+
0,
|
|
150
|
+
this.alphaThreshold,
|
|
151
|
+
this.emissionIntensity
|
|
152
|
+
]), t.queue.writeBuffer(this.#n[0], 0, this.#h);
|
|
153
|
+
let d = this.#n, f = this.#p.get(t, "encode", this.#i, [
|
|
154
|
+
{
|
|
155
|
+
binding: 0,
|
|
156
|
+
resource: { buffer: d[0] }
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
binding: 1,
|
|
160
|
+
resource: r(o)
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
binding: 2,
|
|
164
|
+
resource: { buffer: d[1] }
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
binding: 3,
|
|
168
|
+
resource: { buffer: d[2] }
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
binding: 4,
|
|
172
|
+
resource: { buffer: d[3] }
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
binding: 5,
|
|
176
|
+
resource: r(this.#t)
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
binding: 6,
|
|
180
|
+
resource: r(s)
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
binding: 7,
|
|
184
|
+
resource: { buffer: d[4] }
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
binding: 8,
|
|
188
|
+
resource: { buffer: d[5] }
|
|
189
|
+
}
|
|
190
|
+
]);
|
|
191
|
+
return this.#r.forEach((e, t) => {
|
|
192
|
+
let n = i.beginComputePass();
|
|
193
|
+
n.setPipeline(e), n.setBindGroup(0, f), t === 2 ? n.dispatchWorkgroups(Math.ceil(o.width / 8), Math.ceil(o.height / 8)) : n.dispatchWorkgroups(Math.ceil((t ? o.width * 2 : o.height) / 64)), n.end();
|
|
194
|
+
}), this.#c = u, this.#l = i, !0;
|
|
195
|
+
}
|
|
196
|
+
bindGroup(t, n) {
|
|
197
|
+
if (t !== this.#e || !this.#t) throw e("state", "Encode the SDF scene before binding it", "pixel-lighting");
|
|
198
|
+
return this.#p.get(t, "bind", n, [
|
|
199
|
+
{
|
|
200
|
+
binding: 0,
|
|
201
|
+
resource: { buffer: this.#n[0] }
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
binding: 1,
|
|
205
|
+
resource: r(this.#a)
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
binding: 2,
|
|
209
|
+
resource: r(this.#o)
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
binding: 3,
|
|
213
|
+
resource: r(this.#t)
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
binding: 4,
|
|
217
|
+
resource: { buffer: this.#n[5] }
|
|
218
|
+
}
|
|
219
|
+
]);
|
|
220
|
+
}
|
|
221
|
+
#_() {
|
|
222
|
+
this.#t?.destroy(), this.#t = void 0;
|
|
223
|
+
for (let e of this.#n) e.destroy();
|
|
224
|
+
this.#n = [], this.#r = [], this.#i = void 0, this.#e = void 0, this.#a = this.#o = void 0, this.#c = void 0, this.#l = void 0, this.#p.clear();
|
|
225
|
+
}
|
|
226
|
+
dispose() {
|
|
227
|
+
this.#s || (this.#s = !0, this.#_());
|
|
228
|
+
}
|
|
229
|
+
[Symbol.dispose]() {
|
|
230
|
+
this.dispose();
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
//#endregion
|
|
234
|
+
export { l as SDFScene2D };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** A texture, or a function read each frame for one that changes, such as a pixel layer's `target` getter. */
|
|
2
|
+
export type TextureSource2D = GPUTexture | (() => GPUTexture);
|
|
3
|
+
|
|
4
|
+
/** One texture option: a per-frame getter, or a caller's texture held by the borrowed-object rule. */
|
|
5
|
+
export class TextureOption {
|
|
6
|
+
value: TextureSource2D | undefined;
|
|
7
|
+
constructor(value: TextureSource2D | undefined, name: string, setter: string);
|
|
8
|
+
/** A new value; a plain texture belongs to `device` (the engine's at this moment). */
|
|
9
|
+
set(value: TextureSource2D | undefined, device: GPUDevice | null | undefined): void;
|
|
10
|
+
/** Ties a texture given before attach to the device the pass is set up on. */
|
|
11
|
+
fix(device: GPUDevice | null | undefined): void;
|
|
12
|
+
/** The texture to use on `device` this frame, or undefined (none given, or lost with its device). */
|
|
13
|
+
get(device: GPUDevice, engine?: { onError(error: unknown): void }): GPUTexture | undefined;
|
|
14
|
+
}
|
package/dist/soft.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import { Light2D, type Light2DOptions, type Lighting2DOptions, type Occluder2D } from './index.js';
|
|
5
|
+
/** Options for one {@link SoftLight2D}: the plain light options plus its penumbra size and ray samples. */
|
|
6
|
+
export interface SoftLight2DOptions extends Light2DOptions {
|
|
7
|
+
/** Point and spot lights: emitter radius in world units for penumbra softening; 0 is hard shadow. Default 4. */
|
|
8
|
+
sourceRadius?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Directional lights: angular radius of the source in radians, 0 to pi / 2; 0 is hard shadow. A directional light
|
|
11
|
+
* has no distance, so `sourceRadius` does not apply to it. Default {@link SUN_ANGULAR_RADIUS}.
|
|
12
|
+
*/
|
|
13
|
+
angularRadius?: number;
|
|
14
|
+
/** Filter ray samples across the penumbra on GPU and CPU. Default 16. */
|
|
15
|
+
samples?: number;
|
|
16
|
+
}
|
|
17
|
+
/** The Sun's angular radius seen from Earth, 695,700 km over 149,597,870.7 km (IAU 2015 nominal solar radius, 1 au). */
|
|
18
|
+
export declare const SUN_ANGULAR_RADIUS = 0.0046505;
|
|
19
|
+
/** One 2D light with soft penumbra properties. */
|
|
20
|
+
export declare class SoftLight2D extends Light2D {
|
|
21
|
+
sourceRadius: number;
|
|
22
|
+
angularRadius: number;
|
|
23
|
+
samples: number;
|
|
24
|
+
constructor(options?: SoftLight2DOptions);
|
|
25
|
+
}
|
|
26
|
+
/** Options for evaluating soft shadow factor. */
|
|
27
|
+
export interface SoftShadowOptions {
|
|
28
|
+
/** Point and spot lights: radius of the light emitter in world units; 0 yields hard shadows. Default 4. */
|
|
29
|
+
sourceRadius?: number;
|
|
30
|
+
/** Directional lights: angular radius of the source in radians, 0 to pi / 2. Default {@link SUN_ANGULAR_RADIUS}. */
|
|
31
|
+
angularRadius?: number;
|
|
32
|
+
/** Number of radial ray samples. Default 16. */
|
|
33
|
+
samples?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Computes visibility in [0, 1] of `light` from point (x, y) taking occluder edges into account.
|
|
37
|
+
* Penumbra edges soften with distance from the occluder.
|
|
38
|
+
*/
|
|
39
|
+
export declare function softShadowFactor(light: Light2D, edges: ArrayLike<number>, x: number, y: number, options?: SoftShadowOptions): number;
|
|
40
|
+
/** Whether a point is completely in shadow (visibility is 0). */
|
|
41
|
+
export declare function softInShadow(light: Light2D, edges: ArrayLike<number>, x: number, y: number, options?: SoftShadowOptions): boolean;
|
|
42
|
+
/** Computes what `light` adds at world point (x, y) with soft penumbra shadows applied. */
|
|
43
|
+
export declare function softLightAt(light: Light2D, x: number, y: number, edges: ArrayLike<number>, normal?: readonly number[], options?: SoftShadowOptions): number[];
|
|
44
|
+
/** Options for {@link SoftLighting2D}: the plain lighting options plus the default penumbra size and ray samples. */
|
|
45
|
+
export interface SoftLighting2DOptions extends Lighting2DOptions {
|
|
46
|
+
/** Default light emitter radius in world units for penumbra softening. Default 4. */
|
|
47
|
+
sourceRadius?: number;
|
|
48
|
+
/** Number of radial ray samples on GPU and CPU. Default 16. */
|
|
49
|
+
samples?: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 2D lighting with soft penumbra shadows that soften with distance from the occluder.
|
|
53
|
+
* WebGPU only.
|
|
54
|
+
*/
|
|
55
|
+
export declare class SoftLighting2D implements EnginePlugin {
|
|
56
|
+
#private;
|
|
57
|
+
readonly name: string;
|
|
58
|
+
ambient: string;
|
|
59
|
+
camera: {
|
|
60
|
+
readonly x: number;
|
|
61
|
+
readonly y: number;
|
|
62
|
+
readonly zoom: number;
|
|
63
|
+
};
|
|
64
|
+
sourceRadius: number;
|
|
65
|
+
samples: number;
|
|
66
|
+
readonly lights: SoftLight2D[];
|
|
67
|
+
layer: number;
|
|
68
|
+
constructor(options: SoftLighting2DOptions);
|
|
69
|
+
static attach(instance: EngineKernel, options: SoftLighting2DOptions): SoftLighting2D;
|
|
70
|
+
addLight(options?: SoftLight2DOptions): SoftLight2D;
|
|
71
|
+
removeLight(light: SoftLight2D): void;
|
|
72
|
+
setOccluders(occluders: readonly Occluder2D[]): void;
|
|
73
|
+
/**
|
|
74
|
+
* Replaces where the light goes, from the next frame: a texture, or a function read each frame. After a device
|
|
75
|
+
* restore, hand in a texture made on `engine.gpu` here.
|
|
76
|
+
*/
|
|
77
|
+
setTarget(target: GPUTexture | (() => GPUTexture)): void;
|
|
78
|
+
/** Replaces the surface directions, from the next frame; undefined draws flat. After a device restore, hand in new ones here. */
|
|
79
|
+
setNormals(normals: GPUTexture | (() => GPUTexture) | undefined): void;
|
|
80
|
+
setup(engine: Engine): void;
|
|
81
|
+
onDeviceRestored(): void;
|
|
82
|
+
dispose(): void;
|
|
83
|
+
[Symbol.dispose]: () => void;
|
|
84
|
+
}
|
|
85
|
+
export { SoftLighting2D as SoftShadows2D };
|