@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,135 @@
|
|
|
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
|
+
/** Configuration for an emissive glowing sprite in 2D. */
|
|
5
|
+
export interface EmissiveSpriteItem2D {
|
|
6
|
+
/** World x coordinate. Default 0. */
|
|
7
|
+
x?: number;
|
|
8
|
+
/** World y coordinate. Default 0. */
|
|
9
|
+
y?: number;
|
|
10
|
+
/** Shape of the emissive sprite: 'circle' or 'box'. Default 'circle'. */
|
|
11
|
+
shape?: 'circle' | 'box';
|
|
12
|
+
/** Radius in world units for circle sprites. Default 16. */
|
|
13
|
+
radius?: number;
|
|
14
|
+
/** Width in world units for box sprites. Default 32. */
|
|
15
|
+
width?: number;
|
|
16
|
+
/** Height in world units for box sprites. Default 32. */
|
|
17
|
+
height?: number;
|
|
18
|
+
/** Emissive hex color string (#rrggbb). Default '#ffffff'. */
|
|
19
|
+
color?: string;
|
|
20
|
+
/** Emission intensity multiplier (1.0 = standard, > 1.0 = HDR glow). Default 1.0. */
|
|
21
|
+
intensity?: number;
|
|
22
|
+
/** How far the emitted light reaches beyond the sprite boundary in world units. Default 48. */
|
|
23
|
+
glowRadius?: number;
|
|
24
|
+
/** Falloff exponent for emitted light: higher is steeper falloff. Default 2. */
|
|
25
|
+
falloff?: number;
|
|
26
|
+
/** Minimum luminance threshold above which light bleeds into bloom. Default 0.8. */
|
|
27
|
+
bloomThreshold?: number;
|
|
28
|
+
/** Bloom glow strength multiplier. Default 1.0. */
|
|
29
|
+
bloomStrength?: number;
|
|
30
|
+
/** Layer bitmask this emissive sprite belongs to. Default ~0. */
|
|
31
|
+
layer?: number;
|
|
32
|
+
}
|
|
33
|
+
/** One emissive sprite item. */
|
|
34
|
+
export declare class EmissiveSprite2D implements Required<EmissiveSpriteItem2D> {
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
shape: 'circle' | 'box';
|
|
38
|
+
radius: number;
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
color: string;
|
|
42
|
+
intensity: number;
|
|
43
|
+
glowRadius: number;
|
|
44
|
+
falloff: number;
|
|
45
|
+
bloomThreshold: number;
|
|
46
|
+
bloomStrength: number;
|
|
47
|
+
layer: number;
|
|
48
|
+
constructor(options?: EmissiveSpriteItem2D);
|
|
49
|
+
}
|
|
50
|
+
/** Validates an {@link EmissiveSpriteItem2D}. Throws RangeError if invalid. */
|
|
51
|
+
export declare function checkEmissiveSprite(sp: EmissiveSpriteItem2D): void;
|
|
52
|
+
/**
|
|
53
|
+
* CPU reference form: computes [r, g, b] emitted light and bloom glow
|
|
54
|
+
* from an emissive sprite at world point (x, y).
|
|
55
|
+
*/
|
|
56
|
+
export declare function emissiveLightAt(sprite: EmissiveSpriteItem2D, x: number, y: number, layerMask?: number, bloomIntensity?: number, bloomThreshold?: number): number[];
|
|
57
|
+
/** Options for the {@link EmissiveLighting2D} plugin. */
|
|
58
|
+
export interface EmissiveLighting2DOptions {
|
|
59
|
+
/**
|
|
60
|
+
* Target texture, or a function read each frame. A plain texture lost at a device restore skips the pass, reported
|
|
61
|
+
* once, until {@link EmissiveLighting2D.setTarget} gives a new one.
|
|
62
|
+
*/
|
|
63
|
+
target?: GPUTexture | (() => GPUTexture);
|
|
64
|
+
/** Active emissive sprites. */
|
|
65
|
+
sprites?: readonly EmissiveSpriteItem2D[];
|
|
66
|
+
/** Camera configuration. Default { x: 0, y: 0, zoom: 1 }. */
|
|
67
|
+
camera?: {
|
|
68
|
+
x: number;
|
|
69
|
+
y: number;
|
|
70
|
+
zoom: number;
|
|
71
|
+
};
|
|
72
|
+
/** Whether bloom glow calculation is enabled. Default true. */
|
|
73
|
+
bloom?: boolean;
|
|
74
|
+
/** Global bloom intensity multiplier. Default 1.0. */
|
|
75
|
+
bloomIntensity?: number;
|
|
76
|
+
/** Global bloom threshold. Default 0.8. */
|
|
77
|
+
bloomThreshold?: number;
|
|
78
|
+
/** Pass name. */
|
|
79
|
+
name?: string;
|
|
80
|
+
/** Pass order in frameGraph. Default 22. */
|
|
81
|
+
order?: number;
|
|
82
|
+
/** Layer mask for filtering sprites. Default ~0. */
|
|
83
|
+
layer?: number;
|
|
84
|
+
}
|
|
85
|
+
/** An owned, unattached image effect, such as a new Bloom from @margoengine/post/bloom. */
|
|
86
|
+
export interface EmissiveBloom2D {
|
|
87
|
+
encodeImage(context: {
|
|
88
|
+
device: GPUDevice;
|
|
89
|
+
encoder: GPUCommandEncoder;
|
|
90
|
+
width: number;
|
|
91
|
+
height: number;
|
|
92
|
+
}, input: GPUTextureView, output: GPUTextureView): void;
|
|
93
|
+
dispose(): void;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 2D Emissive Sprites & Bloom hookup plugin.
|
|
97
|
+
* Adds glowing emissive sprites and isolates high-luminance light for bloom.
|
|
98
|
+
*/
|
|
99
|
+
export declare class EmissiveLighting2D implements EnginePlugin, Disposable {
|
|
100
|
+
#private;
|
|
101
|
+
readonly name: string;
|
|
102
|
+
readonly order: number;
|
|
103
|
+
/** Owned rgba16float emission and local glow, excluding all other scene lighting. Replaced on resize/restore. */
|
|
104
|
+
emissiveTexture: GPUTexture | null;
|
|
105
|
+
/** Owned rgba16float local threshold extraction, before spatial filtering. */
|
|
106
|
+
bloomTexture: GPUTexture | null;
|
|
107
|
+
camera: {
|
|
108
|
+
x: number;
|
|
109
|
+
y: number;
|
|
110
|
+
zoom: number;
|
|
111
|
+
};
|
|
112
|
+
bloom: boolean;
|
|
113
|
+
bloomIntensity: number;
|
|
114
|
+
bloomThreshold: number;
|
|
115
|
+
layer: number;
|
|
116
|
+
constructor(options?: EmissiveLighting2DOptions);
|
|
117
|
+
static attach(instance: EngineKernel, options?: EmissiveLighting2DOptions): EmissiveLighting2D;
|
|
118
|
+
setup(engine: Engine): void;
|
|
119
|
+
/**
|
|
120
|
+
* Replaces the texture the attached pass draws to, from the next frame: a texture, a function read each frame, or
|
|
121
|
+
* undefined for none. After a device restore, hand in a texture made on `engine.gpu` here.
|
|
122
|
+
*/
|
|
123
|
+
setTarget(target: GPUTexture | (() => GPUTexture) | undefined): void;
|
|
124
|
+
/** Adds an emissive sprite. */
|
|
125
|
+
addSprite(options?: EmissiveSpriteItem2D): EmissiveSprite2D;
|
|
126
|
+
/** Removes an emissive sprite. */
|
|
127
|
+
removeSprite(sprite: EmissiveSprite2D): boolean;
|
|
128
|
+
/** Takes ownership of an unattached Bloom made per device. Called again after restoration. */
|
|
129
|
+
connectBloom(create: () => EmissiveBloom2D): void;
|
|
130
|
+
/** Renders the emissive pass onto target texture. */
|
|
131
|
+
render(targetTexture: GPUTexture | (() => GPUTexture) | undefined, commandEncoder?: GPUCommandEncoder): void;
|
|
132
|
+
dispose(): void;
|
|
133
|
+
[Symbol.dispose](): void;
|
|
134
|
+
onDeviceRestored(): void;
|
|
135
|
+
}
|
package/dist/emissive.js
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const u=(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, r as a, s as o, t as s, u as c } from "./resources-Cskg0FqE.js";
|
|
5
|
+
import { hexColor as l } from "@margoengine/math";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
//#region src/emissive.ts
|
|
8
|
+
var d = 0, f = class {
|
|
9
|
+
constructor(e = {}) {
|
|
10
|
+
this.x = e.x ?? 0, this.y = e.y ?? 0, this.shape = e.shape ?? "circle", this.radius = e.radius ?? 16, this.width = e.width ?? 32, this.height = e.height ?? 32, this.color = e.color ?? "#ffffff", this.intensity = e.intensity ?? 1, this.glowRadius = e.glowRadius ?? 48, this.falloff = e.falloff ?? 2, this.bloomThreshold = e.bloomThreshold ?? .8, this.bloomStrength = e.bloomStrength ?? 1, this.layer = e.layer ?? 4294967295, p(this);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
function p(t) {
|
|
14
|
+
if (t.shape !== void 0 && t.shape !== "circle" && t.shape !== "box") throw r("Unknown emissive sprite shape: use circle or box", "pixel-lighting");
|
|
15
|
+
for (let [e, n, r] of [
|
|
16
|
+
[
|
|
17
|
+
"x",
|
|
18
|
+
-Infinity,
|
|
19
|
+
"x"
|
|
20
|
+
],
|
|
21
|
+
[
|
|
22
|
+
"y",
|
|
23
|
+
-Infinity,
|
|
24
|
+
"y"
|
|
25
|
+
],
|
|
26
|
+
[
|
|
27
|
+
"radius",
|
|
28
|
+
Number.MIN_VALUE,
|
|
29
|
+
"sprite radius"
|
|
30
|
+
],
|
|
31
|
+
[
|
|
32
|
+
"width",
|
|
33
|
+
Number.MIN_VALUE,
|
|
34
|
+
"sprite width"
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
"height",
|
|
38
|
+
Number.MIN_VALUE,
|
|
39
|
+
"sprite height"
|
|
40
|
+
],
|
|
41
|
+
[
|
|
42
|
+
"falloff",
|
|
43
|
+
Number.MIN_VALUE,
|
|
44
|
+
"falloff"
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
"intensity",
|
|
48
|
+
0,
|
|
49
|
+
"sprite intensity"
|
|
50
|
+
],
|
|
51
|
+
[
|
|
52
|
+
"glowRadius",
|
|
53
|
+
0,
|
|
54
|
+
"glow radius (glowRadius)"
|
|
55
|
+
],
|
|
56
|
+
[
|
|
57
|
+
"bloomStrength",
|
|
58
|
+
0,
|
|
59
|
+
"bloomStrength"
|
|
60
|
+
],
|
|
61
|
+
[
|
|
62
|
+
"bloomThreshold",
|
|
63
|
+
0,
|
|
64
|
+
"bloomThreshold"
|
|
65
|
+
]
|
|
66
|
+
]) {
|
|
67
|
+
let i = t[e];
|
|
68
|
+
i !== void 0 && o(i, r, n);
|
|
69
|
+
}
|
|
70
|
+
if (t.layer !== void 0 && e(t.layer), t.color !== void 0 && !l(t.color)) throw r("Invalid emissive color: use a hex color such as #rrggbb", "pixel-lighting");
|
|
71
|
+
}
|
|
72
|
+
function m(e, t, n, r, i = 1, a = .8) {
|
|
73
|
+
p(e), o(i, "bloomIntensity", 0), o(a, "bloomThreshold", 0);
|
|
74
|
+
let s = e.layer ?? 4294967295;
|
|
75
|
+
if (r !== void 0 && (s & r) === 0) return [
|
|
76
|
+
0,
|
|
77
|
+
0,
|
|
78
|
+
0
|
|
79
|
+
];
|
|
80
|
+
let c = e.x ?? 0, u = e.y ?? 0, d = e.shape ?? "circle", f = e.radius ?? 16, m = (e.width ?? 32) * .5, h = (e.height ?? 32) * .5, g = e.glowRadius ?? 48, _ = e.falloff ?? 2, v = e.intensity ?? 1, y = Math.max(e.bloomThreshold ?? .8, a), b = e.bloomStrength ?? 1, x = l(e.color ?? "#ffffff"), S = t - c, C = n - u, w = !1, T = 0;
|
|
81
|
+
if (d === "circle") {
|
|
82
|
+
let e = Math.hypot(S, C);
|
|
83
|
+
e <= f ? w = !0 : e < f + g && (T = e - f);
|
|
84
|
+
} else {
|
|
85
|
+
let e = Math.max(0, Math.abs(S) - m), t = Math.max(0, Math.abs(C) - h), n = Math.hypot(e, t);
|
|
86
|
+
n === 0 ? w = !0 : n < g && (T = n);
|
|
87
|
+
}
|
|
88
|
+
let E = 0, D = 0;
|
|
89
|
+
if (w) {
|
|
90
|
+
E = v;
|
|
91
|
+
let e = Math.max(x[0], x[1], x[2]) * v;
|
|
92
|
+
e > y && (D = v * (e - y) / e * b * i);
|
|
93
|
+
} else if (T > 0 && g > 0) {
|
|
94
|
+
let e = v * Math.max(0, 1 - T / g) ** _;
|
|
95
|
+
E = e;
|
|
96
|
+
let t = Math.max(x[0] * e, x[1] * e, x[2] * e);
|
|
97
|
+
t > y && (D = e * (t - y) / t * b * i);
|
|
98
|
+
}
|
|
99
|
+
let O = E + D;
|
|
100
|
+
return [
|
|
101
|
+
x[0] * O,
|
|
102
|
+
x[1] * O,
|
|
103
|
+
x[2] * O
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
var h = "\nstruct EmissiveSpriteGPU{\npos:vec2f,size:vec2f,colorIntensity:vec4f,params:vec4f,flags:vec4u,}\nstruct Uniforms{\ncamX:f32,camY:f32,camZoom:f32,targetW:f32,targetH:f32,spriteCount:u32,passLayer:u32,enableBloom:u32,bloomThreshold:f32,bloomIntensity:f32,pad0:f32,pad1:f32,}\n@group(0)@binding(0)var<uniform>uniforms:Uniforms;\n@group(0)@binding(1)var<storage,read>sprites:array<EmissiveSpriteGPU>;\nstruct VertexOutput{\n@builtin(position)position:vec4f,@location(0)uv:vec2f,}\n@vertex fn vs_main(@builtin(vertex_index)index:u32)->VertexOutput{\nvar out:VertexOutput;\nlet x=f32((index<<1u)&2u);\nlet y=f32(index&2u);\nout.position=vec4f(x*2.0 - 1.0,1.0 - y*2.0,0.0,1.0);\nout.uv=vec2f(x,y);\nreturn out;\n}\nstruct EmissionOutput{\n@location(0)emission:vec4f,@location(1)extracted:vec4f,}\n@fragment fn fs_main(in:VertexOutput)->EmissionOutput{\nlet screenPos=in.position.xy;\nlet worldPos=vec2f(uniforms.camX+screenPos.x/uniforms.camZoom,uniforms.camY+screenPos.y/uniforms.camZoom);\nvar totalLight=vec3f(0.0);\nvar bloomGlow=vec3f(0.0);\nfor(var i=0u;\ni<uniforms.spriteCount;\ni=i+1u){\nlet sp=sprites[i];\nif((sp.flags.y&uniforms.passLayer)==0u){\ncontinue;\n}\nlet d=worldPos - sp.pos;\nvar inside=false;\nvar distFromEdge=0.0;\nlet shape=sp.flags.x;\nlet glowR=sp.params.y;\nlet falloff=sp.params.x;\nif(shape==0u){\nlet r=sp.size.x;\nlet dist=length(d);\nif(dist<=r){\ninside=true;\n}\nelse if(dist<r+glowR){\ndistFromEdge=dist - r;\n}\n}\nelse{\nlet hw=sp.size.x;\nlet hh=sp.size.y;\nlet ox=max(0.0,abs(d.x)- hw);\nlet oy=max(0.0,abs(d.y)- hh);\nlet dist=length(vec2f(ox,oy));\nif(dist==0.0){\ninside=true;\n}\nelse if(dist<glowR){\ndistFromEdge=dist;\n}\n}\nlet col=sp.colorIntensity.xyz;\nlet intens=sp.colorIntensity.w;\nif(inside){\ntotalLight+=col*intens;\nlet peak=max(col.r,max(col.g,col.b))*intens;\nif(uniforms.enableBloom==1u&&peak>max(sp.params.z,uniforms.bloomThreshold)){\nbloomGlow+=col*(intens*(peak - max(sp.params.z,uniforms.bloomThreshold))/peak*sp.params.w*uniforms.bloomIntensity);\n}\n}\nelse if(distFromEdge>0.0&&glowR>0.0){\nlet atten=pow(max(0.0,1.0 - distFromEdge/glowR),falloff);\nlet added=intens*atten;\ntotalLight+=col*added;\nlet luma=max(col.r*added,max(col.g*added,col.b*added));\nif(uniforms.enableBloom==1u&&luma>max(sp.params.z,uniforms.bloomThreshold)){\nbloomGlow+=col*(added*(luma - max(sp.params.z,uniforms.bloomThreshold))/luma*sp.params.w*uniforms.bloomIntensity);\n}\n}\n}\nlet finalColor=totalLight+bloomGlow;\nvar result:EmissionOutput;\nresult.emission=vec4f(finalColor,1.0);\nresult.extracted=vec4f(bloomGlow,1.0);\nreturn result;\n}\n@group(1)@binding(0)var image:texture_2d<f32>;\n@fragment fn present(in:VertexOutput)->@location(0)vec4f{\nreturn textureLoad(image,vec2i(in.position.xy),0);\n}\n", g = class m {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
107
|
+
#e;
|
|
108
|
+
#t;
|
|
109
|
+
#n;
|
|
110
|
+
#r;
|
|
111
|
+
#i;
|
|
112
|
+
#a;
|
|
113
|
+
#o;
|
|
114
|
+
#s;
|
|
115
|
+
#c;
|
|
116
|
+
#l;
|
|
117
|
+
#u;
|
|
118
|
+
#d;
|
|
119
|
+
#f;
|
|
120
|
+
#p;
|
|
121
|
+
#m;
|
|
122
|
+
#h;
|
|
123
|
+
constructor(e = {}) {
|
|
124
|
+
if (this.#e = /* @__PURE__ */ new Set(), this.#t = null, this.#n = null, this.#r = null, this.#i = null, this.#a = new s(), this.#c = null, this.#l = null, this.#u = null, this.#d = null, this.#f = null, this.#p = null, this.#m = !1, this.emissiveTexture = null, this.bloomTexture = null, this.name = e.name ?? `emissive-lighting2d-${++d}`, this.order = e.order ?? 22, this.#h = new a(e.target, "EmissiveLighting2D target", "setTarget"), this.camera = e.camera ?? {
|
|
125
|
+
x: 0,
|
|
126
|
+
y: 0,
|
|
127
|
+
zoom: 1
|
|
128
|
+
}, this.bloom = e.bloom ?? !0, this.bloomIntensity = e.bloomIntensity ?? 1, this.bloomThreshold = e.bloomThreshold ?? .8, this.layer = e.layer ?? 4294967295, this.#g(), e.sprites) for (let t of e.sprites) this.addSprite(t);
|
|
129
|
+
}
|
|
130
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
131
|
+
let r = new m(n), i = u(e);
|
|
132
|
+
if (!i.gpu || typeof i.gpu.createShaderModule != "function") throw t("unsupported", "WebGL2 is not supported for @margoengine/lighting2d/emissive; use WebGPU", "pixel-lighting");
|
|
133
|
+
return i.use(r), r;
|
|
134
|
+
}
|
|
135
|
+
setup(e) {
|
|
136
|
+
if (this.#m) throw t("disposed", "EmissiveLighting2D disposed; create a new one");
|
|
137
|
+
if (e.backend !== "webgpu" || !e.gpu) throw t("unsupported", "WebGL2 is not supported for @margoengine/lighting2d/emissive; use WebGPU", "pixel-lighting");
|
|
138
|
+
this.#t = e, this.#h.fix(e.gpu), e.frameGraph.add({
|
|
139
|
+
name: this.name,
|
|
140
|
+
order: this.order,
|
|
141
|
+
execute: (t) => {
|
|
142
|
+
t.backend === "webgpu" && e.gpu && this.render(this.#h.get(e.gpu, e), t.encoder);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
setTarget(e) {
|
|
147
|
+
this.#h.set(e, this.#t?.gpu);
|
|
148
|
+
}
|
|
149
|
+
addSprite(e = {}) {
|
|
150
|
+
if (this.#m) throw c("EmissiveLighting2D has been disposed", "pixel-lighting");
|
|
151
|
+
let t = new f(e);
|
|
152
|
+
return this.#e.add(t), t;
|
|
153
|
+
}
|
|
154
|
+
removeSprite(e) {
|
|
155
|
+
return this.#e.delete(e);
|
|
156
|
+
}
|
|
157
|
+
connectBloom(e) {
|
|
158
|
+
if (this.#m) throw c("EmissiveLighting2D has been disposed", "pixel-lighting");
|
|
159
|
+
if (typeof e != "function") throw c("connectBloom needs a factory returning an unattached Bloom", "pixel-lighting");
|
|
160
|
+
this.#s?.dispose(), this.#s = void 0, this.#o = e;
|
|
161
|
+
}
|
|
162
|
+
render(e, t) {
|
|
163
|
+
if (!this.#t || !this.#t.gpu || this.#m) return;
|
|
164
|
+
let n = this.#t.gpu, a = typeof e == "function" ? e() : e;
|
|
165
|
+
if (!a) return;
|
|
166
|
+
this.#g();
|
|
167
|
+
for (let e of this.#e) p(e);
|
|
168
|
+
if ((this.#f !== n || this.#p !== a.format) && (this.#_(), this.#f = n, this.#p = a.format), !this.emissiveTexture || this.emissiveTexture.width !== a.width || this.emissiveTexture.height !== a.height) {
|
|
169
|
+
this.emissiveTexture?.destroy(), this.bloomTexture?.destroy(), this.#c?.destroy();
|
|
170
|
+
let e = {
|
|
171
|
+
size: [a.width, a.height],
|
|
172
|
+
format: "rgba16float",
|
|
173
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
|
|
174
|
+
};
|
|
175
|
+
this.emissiveTexture = n.createTexture({
|
|
176
|
+
...e,
|
|
177
|
+
label: "emissive HDR"
|
|
178
|
+
}), this.bloomTexture = n.createTexture({
|
|
179
|
+
...e,
|
|
180
|
+
label: "emissive threshold extraction"
|
|
181
|
+
}), this.#c = n.createTexture({
|
|
182
|
+
...e,
|
|
183
|
+
label: "emissive spatial bloom"
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
if (!this.#n) {
|
|
187
|
+
let e = n.createShaderModule({ code: h });
|
|
188
|
+
this.#d = n.createBindGroupLayout({ entries: [{
|
|
189
|
+
binding: 0,
|
|
190
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
191
|
+
buffer: {
|
|
192
|
+
type: "uniform",
|
|
193
|
+
minBindingSize: 48
|
|
194
|
+
}
|
|
195
|
+
}, {
|
|
196
|
+
binding: 1,
|
|
197
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
198
|
+
buffer: { type: "read-only-storage" }
|
|
199
|
+
}] }), this.#n = n.createRenderPipeline({
|
|
200
|
+
layout: n.createPipelineLayout({ bindGroupLayouts: [this.#d] }),
|
|
201
|
+
vertex: {
|
|
202
|
+
module: e,
|
|
203
|
+
entryPoint: "vs_main"
|
|
204
|
+
},
|
|
205
|
+
fragment: {
|
|
206
|
+
module: e,
|
|
207
|
+
entryPoint: "fs_main",
|
|
208
|
+
targets: [{ format: "rgba16float" }, { format: "rgba16float" }]
|
|
209
|
+
}
|
|
210
|
+
}), this.#r = n.createRenderPipeline({
|
|
211
|
+
layout: "auto",
|
|
212
|
+
vertex: {
|
|
213
|
+
module: e,
|
|
214
|
+
entryPoint: "vs_main"
|
|
215
|
+
},
|
|
216
|
+
fragment: {
|
|
217
|
+
module: e,
|
|
218
|
+
entryPoint: "present",
|
|
219
|
+
targets: [{ format: a.format }]
|
|
220
|
+
}
|
|
221
|
+
}), this.#i = this.#r.getBindGroupLayout(1);
|
|
222
|
+
}
|
|
223
|
+
let o = Array.from(this.#e), s = o.length, c = Math.max(64, s * 64);
|
|
224
|
+
if (c > n.limits.maxStorageBufferBindingSize) throw r("Emissive sprites exceed maxStorageBufferBindingSize", "pixel-lighting");
|
|
225
|
+
(!this.#u || this.#u.size < c) && (this.#u?.destroy(), this.#u = n.createBuffer({
|
|
226
|
+
size: c,
|
|
227
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
228
|
+
}));
|
|
229
|
+
let u = new Float32Array(c / 4), d = new Uint32Array(u.buffer);
|
|
230
|
+
for (let e = 0; e < s; e++) {
|
|
231
|
+
let t = o[e], n = e * 16;
|
|
232
|
+
u.set([t.x, t.y], n + 0), t.shape === "box" ? u.set([t.width * .5, t.height * .5], n + 2) : u.set([t.radius, 0], n + 2);
|
|
233
|
+
let r = l(t.color) ?? [
|
|
234
|
+
1,
|
|
235
|
+
1,
|
|
236
|
+
1
|
|
237
|
+
];
|
|
238
|
+
u.set([
|
|
239
|
+
r[0],
|
|
240
|
+
r[1],
|
|
241
|
+
r[2],
|
|
242
|
+
t.intensity,
|
|
243
|
+
t.falloff,
|
|
244
|
+
t.glowRadius,
|
|
245
|
+
t.bloomThreshold,
|
|
246
|
+
t.bloomStrength
|
|
247
|
+
], n + 4), d.set([
|
|
248
|
+
+(t.shape === "box"),
|
|
249
|
+
t.layer,
|
|
250
|
+
0,
|
|
251
|
+
0
|
|
252
|
+
], n + 12);
|
|
253
|
+
}
|
|
254
|
+
n.queue.writeBuffer(this.#u, 0, u), this.#l ||= n.createBuffer({
|
|
255
|
+
size: 48,
|
|
256
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
257
|
+
});
|
|
258
|
+
let f = /* @__PURE__ */ new Float32Array(12), m = new Uint32Array(f.buffer);
|
|
259
|
+
f.set([
|
|
260
|
+
this.camera.x,
|
|
261
|
+
this.camera.y,
|
|
262
|
+
this.camera.zoom,
|
|
263
|
+
a.width ?? 32,
|
|
264
|
+
a.height ?? 16
|
|
265
|
+
], 0), m.set([
|
|
266
|
+
s,
|
|
267
|
+
this.layer,
|
|
268
|
+
+!!this.bloom
|
|
269
|
+
], 5), f.set([
|
|
270
|
+
this.bloomThreshold,
|
|
271
|
+
this.bloomIntensity,
|
|
272
|
+
0,
|
|
273
|
+
0
|
|
274
|
+
], 8), n.queue.writeBuffer(this.#l, 0, f);
|
|
275
|
+
let g = t ?? n.createCommandEncoder(), _ = g.beginRenderPass({ colorAttachments: [{
|
|
276
|
+
view: i(this.emissiveTexture),
|
|
277
|
+
loadOp: "clear",
|
|
278
|
+
clearValue: [
|
|
279
|
+
0,
|
|
280
|
+
0,
|
|
281
|
+
0,
|
|
282
|
+
1
|
|
283
|
+
],
|
|
284
|
+
storeOp: "store"
|
|
285
|
+
}, {
|
|
286
|
+
view: i(this.bloomTexture),
|
|
287
|
+
loadOp: "clear",
|
|
288
|
+
clearValue: [
|
|
289
|
+
0,
|
|
290
|
+
0,
|
|
291
|
+
0,
|
|
292
|
+
1
|
|
293
|
+
],
|
|
294
|
+
storeOp: "store"
|
|
295
|
+
}] });
|
|
296
|
+
_.setPipeline(this.#n);
|
|
297
|
+
let v = this.#a.get(n, "sprites", this.#d, [{
|
|
298
|
+
binding: 0,
|
|
299
|
+
resource: { buffer: this.#l }
|
|
300
|
+
}, {
|
|
301
|
+
binding: 1,
|
|
302
|
+
resource: { buffer: this.#u }
|
|
303
|
+
}]);
|
|
304
|
+
_.setBindGroup(0, v), _.draw(3), _.end();
|
|
305
|
+
let y = this.emissiveTexture;
|
|
306
|
+
this.#o && (this.#s ??= this.#o(), this.#s.encodeImage({
|
|
307
|
+
device: n,
|
|
308
|
+
encoder: g,
|
|
309
|
+
width: a.width,
|
|
310
|
+
height: a.height
|
|
311
|
+
}, i(y), i(this.#c)), y = this.#c);
|
|
312
|
+
let b = g.beginRenderPass({ colorAttachments: [{
|
|
313
|
+
view: i(a),
|
|
314
|
+
loadOp: "clear",
|
|
315
|
+
clearValue: [
|
|
316
|
+
0,
|
|
317
|
+
0,
|
|
318
|
+
0,
|
|
319
|
+
1
|
|
320
|
+
],
|
|
321
|
+
storeOp: "store"
|
|
322
|
+
}] });
|
|
323
|
+
b.setPipeline(this.#r), b.setBindGroup(1, this.#a.get(n, "present", this.#i, [{
|
|
324
|
+
binding: 0,
|
|
325
|
+
resource: i(y)
|
|
326
|
+
}])), b.draw(3), b.end(), t || n.queue.submit([g.finish()]);
|
|
327
|
+
}
|
|
328
|
+
dispose() {
|
|
329
|
+
this[Symbol.dispose]();
|
|
330
|
+
}
|
|
331
|
+
[Symbol.dispose]() {
|
|
332
|
+
this.#m || (this.#m = !0, this.#e.clear(), this.#t?.sceneOf(this)?.removePlugin(this.name), this.#t = null, this.#_());
|
|
333
|
+
}
|
|
334
|
+
onDeviceRestored() {
|
|
335
|
+
this.#_();
|
|
336
|
+
}
|
|
337
|
+
#g() {
|
|
338
|
+
n(this.camera), e(this.layer), o(this.order, "emissive order"), o(this.bloomThreshold, "bloomThreshold", 0), o(this.bloomIntensity, "bloomIntensity", 0);
|
|
339
|
+
}
|
|
340
|
+
#_() {
|
|
341
|
+
this.#l?.destroy(), this.#u?.destroy(), this.#l = this.#u = null, this.#n = this.#d = this.#f = this.#p = null, this.emissiveTexture?.destroy(), this.bloomTexture?.destroy(), this.#c?.destroy(), this.emissiveTexture = this.bloomTexture = this.#c = null, this.#s?.dispose(), this.#s = void 0, this.#r = this.#i = null, this.#a.clear();
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
//#endregion
|
|
345
|
+
export { g as EmissiveLighting2D, f as EmissiveSprite2D, p as checkEmissiveSprite, m as emissiveLightAt };
|
package/dist/gi.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
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 type { SDFScene2D } from './sdf.js';
|
|
5
|
+
import { type Light2D, type Occluder2D, type Point2D } from './index.js';
|
|
6
|
+
/** Shape of an emissive sprite. */
|
|
7
|
+
export type EmissiveShape2D = 'rect' | 'circle';
|
|
8
|
+
/** A 2D sprite or area that emits light into its surroundings. */
|
|
9
|
+
export interface EmissiveSprite2D {
|
|
10
|
+
/** Center X position in world units. */
|
|
11
|
+
readonly x: number;
|
|
12
|
+
/** Center Y position in world units. */
|
|
13
|
+
readonly y: number;
|
|
14
|
+
/** Width of rectangular emitter in world units (default 10). */
|
|
15
|
+
readonly width?: number;
|
|
16
|
+
/** Height of rectangular emitter in world units (default 10). */
|
|
17
|
+
readonly height?: number;
|
|
18
|
+
/** Radius of circular emitter in world units. */
|
|
19
|
+
readonly radius?: number;
|
|
20
|
+
/** Geometry shape of the emitter ('rect' or 'circle', default 'rect'). */
|
|
21
|
+
readonly shape?: EmissiveShape2D;
|
|
22
|
+
/** Emissive color as a hex number (0xffffff), CSS hex string ('#ffffff'), or [r, g, b] (default 0xffffff). */
|
|
23
|
+
readonly color?: number | string | readonly [number, number, number];
|
|
24
|
+
/** Emissive intensity multiplier (>= 0, default 1.0). */
|
|
25
|
+
readonly intensity?: number;
|
|
26
|
+
}
|
|
27
|
+
/** Options configuring 2D radiance cascade global illumination. */
|
|
28
|
+
export interface RadianceCascadeOptions2D {
|
|
29
|
+
/** Number of cascade interval levels (1..8, default 4). */
|
|
30
|
+
readonly cascades?: number;
|
|
31
|
+
/** Base ray direction count at cascade 0 (1..32, default 4). */
|
|
32
|
+
readonly baseRays?: number;
|
|
33
|
+
/** Base interval distance in world units at cascade 0 (> 0, default 16). */
|
|
34
|
+
readonly baseDistance?: number;
|
|
35
|
+
/** Interval distance multiplier per cascade level (> 1, default 4). */
|
|
36
|
+
readonly intervalRatio?: number;
|
|
37
|
+
/** Angular direction branching ratio per cascade level (1..8, default 4). */
|
|
38
|
+
readonly angularRatio?: number;
|
|
39
|
+
/** Diffuse bounce reflection count (0..4, default 1). 0 disables indirect bounce. */
|
|
40
|
+
readonly bounces?: number;
|
|
41
|
+
/** Default diffuse surface albedo for bounce reflections (0..1, default 0.5). */
|
|
42
|
+
readonly albedo?: number;
|
|
43
|
+
/** Overall GI indirect intensity scale (>= 0, default 1.0). */
|
|
44
|
+
readonly intensity?: number;
|
|
45
|
+
}
|
|
46
|
+
/** A 2D scene containing emitters, direct lights, and occluders. */
|
|
47
|
+
export interface GIScene2D {
|
|
48
|
+
/** Emissive sprites lighting surroundings. */
|
|
49
|
+
readonly emitters?: readonly EmissiveSprite2D[];
|
|
50
|
+
/** Direct 2D lights. */
|
|
51
|
+
readonly lights?: readonly Light2D[];
|
|
52
|
+
/** Occluder obstacles blocking rays and reflecting bounce light. */
|
|
53
|
+
readonly occluders?: readonly Occluder2D[];
|
|
54
|
+
/** Direct-light layer mask. Default all bits. */
|
|
55
|
+
readonly layer?: number;
|
|
56
|
+
}
|
|
57
|
+
type RGB = [number, number, number];
|
|
58
|
+
/** CPU spatial radiance-interval hierarchy with finite-order diffuse surface reflection. */
|
|
59
|
+
export declare class RadianceCascade2D {
|
|
60
|
+
readonly cascades: number;
|
|
61
|
+
readonly baseRays: number;
|
|
62
|
+
readonly baseDistance: number;
|
|
63
|
+
readonly intervalRatio: number;
|
|
64
|
+
readonly angularRatio: number;
|
|
65
|
+
readonly bounces: number;
|
|
66
|
+
readonly albedo: number;
|
|
67
|
+
readonly intensity: number;
|
|
68
|
+
constructor(options?: RadianceCascadeOptions2D);
|
|
69
|
+
/** Evaluate the hierarchy at a world point. Upper probes use a fixed world lattice centred at .5. */
|
|
70
|
+
evaluateAt(point: Point2D, normal?: Point2D, scene?: GIScene2D): RGB;
|
|
71
|
+
/** Compute world-space queries starting at zero, separated by spacing (default 1). */
|
|
72
|
+
computeGrid(width: number, height: number, scene?: GIScene2D, spacing?: number): Float32Array;
|
|
73
|
+
}
|
|
74
|
+
/** Evaluate diffuse 2D GI using the same hierarchy as the WebGPU entry. */
|
|
75
|
+
export declare function evaluate2DGI(point: Point2D, normal?: Point2D, scene?: GIScene2D, options?: RadianceCascadeOptions2D): RGB;
|
|
76
|
+
/** Options for GILighting2D WebGPU engine plugin. */
|
|
77
|
+
export interface GILighting2DOptions {
|
|
78
|
+
/** Reuse identical world-probe and surface intervals between frames. Default true; changes invalidate immediately. */
|
|
79
|
+
readonly temporal?: boolean;
|
|
80
|
+
/** Borrowed painted alpha/albedo/emission scene. Encoded every frame before the GI passes. */
|
|
81
|
+
readonly painted?: SDFScene2D;
|
|
82
|
+
/** Direct-light layer mask. Default all bits. */
|
|
83
|
+
readonly layer?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Target texture receiving GI irradiance; a function is read each frame. A plain texture lost at a device restore
|
|
86
|
+
* skips the pass, reported once, until {@link GILighting2D.setTarget} gives a new one.
|
|
87
|
+
*/
|
|
88
|
+
readonly target: GPUTexture | (() => GPUTexture);
|
|
89
|
+
/** Render pass execution order (default 20). */
|
|
90
|
+
readonly order?: number;
|
|
91
|
+
/** Active camera with x, y, and zoom. */
|
|
92
|
+
readonly camera?: {
|
|
93
|
+
x: number;
|
|
94
|
+
y: number;
|
|
95
|
+
zoom: number;
|
|
96
|
+
};
|
|
97
|
+
/** Radiance cascade options. */
|
|
98
|
+
readonly cascades?: RadianceCascadeOptions2D;
|
|
99
|
+
/** Emissive sprites in the scene. */
|
|
100
|
+
readonly emitters?: readonly EmissiveSprite2D[];
|
|
101
|
+
/** Direct lights in the scene. */
|
|
102
|
+
readonly lights?: readonly Light2D[];
|
|
103
|
+
/** Occluders casting shadows and reflecting bounce light. */
|
|
104
|
+
readonly occluders?: readonly Occluder2D[];
|
|
105
|
+
/** Ambient light color (default 0x000000). */
|
|
106
|
+
readonly ambient?: number | string;
|
|
107
|
+
/** Unique plugin name. */
|
|
108
|
+
readonly name?: string;
|
|
109
|
+
}
|
|
110
|
+
/** WebGPU spatial radiance intervals, with finite-order diffuse surface gathers. */
|
|
111
|
+
export declare class GILighting2D implements EnginePlugin, Disposable {
|
|
112
|
+
#private;
|
|
113
|
+
/** Exact interval reuse when inputs match the preceding frame. Default true. */
|
|
114
|
+
temporal: boolean;
|
|
115
|
+
/** Borrowed painted scene; dispose it separately after all consumers. */
|
|
116
|
+
painted?: SDFScene2D;
|
|
117
|
+
name: string;
|
|
118
|
+
order: number;
|
|
119
|
+
readonly cascades: RadianceCascade2D;
|
|
120
|
+
camera: {
|
|
121
|
+
x: number;
|
|
122
|
+
y: number;
|
|
123
|
+
zoom: number;
|
|
124
|
+
};
|
|
125
|
+
ambient: number | string;
|
|
126
|
+
layer: number;
|
|
127
|
+
emitters: EmissiveSprite2D[];
|
|
128
|
+
lights: Light2D[];
|
|
129
|
+
occluders: Occluder2D[];
|
|
130
|
+
constructor(options: GILighting2DOptions);
|
|
131
|
+
static attach(instance: EngineKernel, options: GILighting2DOptions): GILighting2D;
|
|
132
|
+
setup(engine: Engine): void;
|
|
133
|
+
/**
|
|
134
|
+
* Replaces the texture receiving the irradiance, from the next frame: a texture, or a function read each frame.
|
|
135
|
+
* After a device restore, hand in a texture made on `engine.gpu` here.
|
|
136
|
+
*/
|
|
137
|
+
setTarget(target: GPUTexture | (() => GPUTexture)): void;
|
|
138
|
+
/** Force the next frame to rebuild all surface and probe intervals. */
|
|
139
|
+
resetHistory(): void;
|
|
140
|
+
onDeviceRestored(): void;
|
|
141
|
+
dispose(): void;
|
|
142
|
+
[Symbol.dispose](): void;
|
|
143
|
+
}
|
|
144
|
+
export {};
|