@margoengine/creative 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 +310 -2
- package/dist/display-C6RC-F2-.js +3 -0
- package/dist/errors-XvcwUnU4.js +7 -0
- package/dist/gpu-DaSofAeP.js +30 -0
- package/dist/grass-OkjgCaF7.js +450 -0
- package/dist/grass-settings.d.ts +63 -0
- package/dist/grass-settings.js +131 -0
- package/dist/grass-terrain.d.ts +33 -0
- package/dist/grass-terrain.js +185 -0
- package/dist/grass.d.ts +106 -0
- package/dist/grass.js +1 -0
- package/dist/hdr-format-DXpjc3bW.js +4 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/msaa-8hWFieoE.js +220 -0
- package/dist/paint-aniso.d.ts +36 -0
- package/dist/paint-aniso.js +217 -0
- package/dist/paint.d.ts +56 -0
- package/dist/paint.js +167 -0
- package/dist/particles.d.ts +38 -0
- package/dist/particles.js +79 -0
- package/dist/pass-B5mPip-t.js +27 -0
- package/dist/planet-BYTix5S1.js +505 -0
- package/dist/planet-atmosphere.d.ts +37 -0
- package/dist/planet-atmosphere.js +196 -0
- package/dist/planet-pass.d.ts +47 -0
- package/dist/planet.d.ts +76 -0
- package/dist/planet.js +1 -0
- package/dist/post.d.ts +108 -0
- package/dist/post.js +265 -0
- package/dist/quality-DFm-cQWC.js +7 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/stars-C0SH8-aJ.js +90 -0
- package/dist/stars.d.ts +34 -0
- package/dist/stars.js +1 -0
- package/dist/targets-depth.d.ts +35 -0
- package/dist/targets-depth.js +169 -0
- package/dist/targets.d.ts +35 -0
- package/dist/targets.js +42 -0
- package/dist/texture-target-CGqE9pOa.js +131 -0
- package/dist/texture-target.d.ts +49 -0
- package/dist/toon.d.ts +32 -0
- package/dist/toon.js +58 -0
- package/dist/view-rect-TO68f_UR.js +48 -0
- package/package.json +124 -4
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { r as e } from "./errors-XvcwUnU4.js";
|
|
2
|
+
import { t } from "./texture-target-CGqE9pOa.js";
|
|
3
|
+
//#region src/targets-depth.ts
|
|
4
|
+
var n = (t) => e(`Depth target ${t}`, "creative-target");
|
|
5
|
+
function r(e) {
|
|
6
|
+
if (![1, 4].includes(e.samples ?? 1)) throw n("samples must be 1 or 4; choose a supported count");
|
|
7
|
+
if (e.depth !== void 0 && typeof e.depth != "boolean") throw n("depth must be boolean; use true or false");
|
|
8
|
+
if (![
|
|
9
|
+
"depth32float",
|
|
10
|
+
"depth24plus",
|
|
11
|
+
"depth24plus-stencil8"
|
|
12
|
+
].includes(e.depthFormat ?? "depth32float")) throw n("depthFormat is unsupported; choose a depth attachment format");
|
|
13
|
+
let t = 0;
|
|
14
|
+
for (let n = 0; n < (e.mipLevelCount ?? 1) && n < 32; n++) {
|
|
15
|
+
let r = Math.max(1, Math.floor(e.width / 2 ** n)), i = Math.max(1, Math.floor((e.height ?? e.width) / 2 ** n)), a = e.kind === "3d" ? Math.max(1, Math.floor((e.depthOrLayers ?? 1) / 2 ** n)) : e.depthOrLayers ?? (e.kind === "cube" ? 6 : 1), o = e.format === "rgba16float" ? 8 : 4, s = e.samples ?? 1;
|
|
16
|
+
t += r * i * a * (o + (e.depth === !1 ? 0 : 4 * s) + (s === 4 ? o * (4 + +(e.kind === "3d")) : 0));
|
|
17
|
+
}
|
|
18
|
+
if (t > 268435456) throw n("allocation exceeds 256 MiB; reduce dimensions or samples");
|
|
19
|
+
return e;
|
|
20
|
+
}
|
|
21
|
+
var i = class extends t {
|
|
22
|
+
#e;
|
|
23
|
+
#t = /* @__PURE__ */ new Map();
|
|
24
|
+
#n;
|
|
25
|
+
constructor(e, t) {
|
|
26
|
+
super(e, r(t)), this.#e = e, this.samples = t.samples ?? 1, this.depth = t.depth ?? !0, this.depthFormat = t.depthFormat ?? "depth32float";
|
|
27
|
+
}
|
|
28
|
+
textureUsage() {
|
|
29
|
+
return super.textureUsage() | GPUTextureUsage.COPY_DST;
|
|
30
|
+
}
|
|
31
|
+
#r(e, t) {
|
|
32
|
+
let [r, i, a] = this.mipSize(e);
|
|
33
|
+
if (!Number.isInteger(t) || t < 0 || t >= a) throw n("layer is out of range; choose an existing surface");
|
|
34
|
+
let o = `${e}:${t}`, s = this.#t.get(o);
|
|
35
|
+
if (!s) {
|
|
36
|
+
s = {}, this.#t.set(o, s);
|
|
37
|
+
let e = (e, t, n = GPUTextureUsage.RENDER_ATTACHMENT) => this.#e.createTexture({
|
|
38
|
+
size: [r, i],
|
|
39
|
+
format: e,
|
|
40
|
+
sampleCount: t,
|
|
41
|
+
usage: n
|
|
42
|
+
});
|
|
43
|
+
this.depth && (s.depth = e(this.depthFormat, this.samples, GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING)), this.samples === 4 && (s.msaa = e(this.options.format, 4), this.options.kind === "3d" && (s.resolve = e(this.options.format, 1, GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC)));
|
|
44
|
+
}
|
|
45
|
+
return s;
|
|
46
|
+
}
|
|
47
|
+
depthTexture(e = {}) {
|
|
48
|
+
return this.#r(e.mip ?? 0, e.layer ?? 0).depth;
|
|
49
|
+
}
|
|
50
|
+
begin(e, t = {}) {
|
|
51
|
+
let r = t.depthLoadOp ?? "clear", i = t.clearDepth ?? 0;
|
|
52
|
+
if (!["clear", "load"].includes(r) || !Number.isFinite(i) || i < 0 || i > 1) throw n("depth clear/load is invalid; use clear or load and a depth in 0..1");
|
|
53
|
+
let a;
|
|
54
|
+
super.begin({ beginRenderPass: (e) => (a = e, {}) }, t);
|
|
55
|
+
let o = t.mip ?? 0, s = t.layer ?? 0, c = this.#r(o, s), l = Array.from(a.colorAttachments)[0];
|
|
56
|
+
c.msaa && (l.resolveTarget = c.resolve?.createView() ?? l.view, l.view = c.msaa.createView(), delete l.depthSlice), c.depth && (a.depthStencilAttachment = {
|
|
57
|
+
view: c.depth.createView(),
|
|
58
|
+
depthLoadOp: r,
|
|
59
|
+
depthStoreOp: "store",
|
|
60
|
+
depthClearValue: i,
|
|
61
|
+
...this.depthFormat === "depth24plus-stencil8" ? {
|
|
62
|
+
stencilLoadOp: r,
|
|
63
|
+
stencilStoreOp: "store",
|
|
64
|
+
stencilClearValue: 0
|
|
65
|
+
} : {}
|
|
66
|
+
});
|
|
67
|
+
let u = e.beginRenderPass(a);
|
|
68
|
+
if (!c.resolve) return u;
|
|
69
|
+
let d = this.texture, f = this.mipSize(o), p = c.resolve;
|
|
70
|
+
return new Proxy(u, { get(t, n) {
|
|
71
|
+
if (n === "end") return () => {
|
|
72
|
+
t.end(), e.copyTextureToTexture({ texture: p }, {
|
|
73
|
+
texture: d,
|
|
74
|
+
mipLevel: o,
|
|
75
|
+
origin: [
|
|
76
|
+
0,
|
|
77
|
+
0,
|
|
78
|
+
s
|
|
79
|
+
]
|
|
80
|
+
}, [
|
|
81
|
+
f[0],
|
|
82
|
+
f[1],
|
|
83
|
+
1
|
|
84
|
+
]);
|
|
85
|
+
};
|
|
86
|
+
let r = Reflect.get(t, n, t);
|
|
87
|
+
return typeof r == "function" ? r.bind(t) : r;
|
|
88
|
+
} });
|
|
89
|
+
}
|
|
90
|
+
generateMipmaps(e) {
|
|
91
|
+
this.mipSize();
|
|
92
|
+
let t = this.options.kind === "3d";
|
|
93
|
+
if (!this.#n) {
|
|
94
|
+
let e = this.#e.createShaderModule({ code: `
|
|
95
|
+
@group(0) @binding(0) var source: texture_${t ? "3d" : "2d_array"}<f32>;
|
|
96
|
+
struct Out { @builtin(position) p: vec4f, @location(0) @interpolate(flat) layer: u32 }
|
|
97
|
+
@vertex fn vs(@builtin(vertex_index) i:u32, @builtin(instance_index) layer:u32)->Out {
|
|
98
|
+
let p = array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));
|
|
99
|
+
return Out(vec4f(p[i],0,1),layer);
|
|
100
|
+
}
|
|
101
|
+
@fragment fn fs(input:Out)->@location(0) vec4f {
|
|
102
|
+
let size = vec3f(vec2f(textureDimensions(source,0).xy), ${t ? "f32(textureDimensions(source,0).z)" : "1.0"});
|
|
103
|
+
let output = max(floor(size/2.0),vec3f(1));
|
|
104
|
+
let pixel = vec3f(floor(input.p.xy),${t ? "f32(input.layer)" : "0.0"});
|
|
105
|
+
let lo = pixel*size/output; let hi = (pixel+1.0)*size/output;
|
|
106
|
+
var sum = vec4f(0); var weight = 0.0; var least = vec4f(3.4e38); var most = vec4f(-3.4e38);
|
|
107
|
+
for(var z=i32(floor(lo.z)); z<i32(ceil(hi.z)); z++) {
|
|
108
|
+
for(var y=i32(floor(lo.y)); y<i32(ceil(hi.y)); y++) {
|
|
109
|
+
for(var x=i32(floor(lo.x)); x<i32(ceil(hi.x)); x++) {
|
|
110
|
+
let delta = max(vec3f(0), min(hi,vec3f(f32(x+1),f32(y+1),f32(z+1)))-max(lo,vec3f(f32(x),f32(y),f32(z))));
|
|
111
|
+
let w = delta.x*delta.y*delta.z;
|
|
112
|
+
let v = textureLoad(source, ${t ? "vec3i(x,y,z),0" : "vec2i(x,y),i32(input.layer),0"});
|
|
113
|
+
if (w > 0.0) { least = min(least, v); most = max(most, v); }
|
|
114
|
+
sum += v*w; weight += w;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
return clamp(sum/weight, least, most);
|
|
122
|
+
}` });
|
|
123
|
+
this.#n = this.#e.createRenderPipeline({
|
|
124
|
+
layout: "auto",
|
|
125
|
+
vertex: {
|
|
126
|
+
module: e,
|
|
127
|
+
entryPoint: "vs"
|
|
128
|
+
},
|
|
129
|
+
fragment: {
|
|
130
|
+
module: e,
|
|
131
|
+
entryPoint: "fs",
|
|
132
|
+
targets: [{ format: this.options.format }]
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
for (let n = 1; n < this.options.mipLevelCount; n++) {
|
|
137
|
+
let r = this.#e.createBindGroup({
|
|
138
|
+
layout: this.#n.getBindGroupLayout(0),
|
|
139
|
+
entries: [{
|
|
140
|
+
binding: 0,
|
|
141
|
+
resource: this.texture.createView({
|
|
142
|
+
dimension: t ? "3d" : "2d-array",
|
|
143
|
+
baseMipLevel: n - 1,
|
|
144
|
+
mipLevelCount: 1
|
|
145
|
+
})
|
|
146
|
+
}]
|
|
147
|
+
});
|
|
148
|
+
for (let t = 0; t < this.mipSize(n)[2]; t++) {
|
|
149
|
+
let i = super.begin(e, {
|
|
150
|
+
mip: n,
|
|
151
|
+
layer: t
|
|
152
|
+
});
|
|
153
|
+
i.setPipeline(this.#n), i.setBindGroup(0, r), i.draw(3, 1, 0, t), i.end();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
#i() {
|
|
158
|
+
for (let e of this.#t.values()) e.depth?.destroy(), e.msaa?.destroy(), e.resolve?.destroy();
|
|
159
|
+
this.#t.clear(), this.#n = void 0;
|
|
160
|
+
}
|
|
161
|
+
restore(e) {
|
|
162
|
+
super.restore(e), this.#i(), this.#e = e;
|
|
163
|
+
}
|
|
164
|
+
dispose() {
|
|
165
|
+
this.#i(), super.dispose();
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
//#endregion
|
|
169
|
+
export { i as DepthTextureRenderTarget };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { PassContext } from '@margoengine/core/extension';
|
|
3
|
+
export { TextureRenderTarget } from './texture-target.js';
|
|
4
|
+
export type { TextureRenderTargetOptions, TargetSurface } from './texture-target.js';
|
|
5
|
+
/**
|
|
6
|
+
* A linear HDR scene target: an `rgba16float` colour texture and a depth texture (`depth32float-stencil8` when the
|
|
7
|
+
* device has it), multisampled at `samples` with a resolve, for effects that render the scene off-screen
|
|
8
|
+
* (`PostProcessing.target`). Sizes are pixels, 1-4096 within the device limit.
|
|
9
|
+
* @throws RangeError when a size exceeds the limits.
|
|
10
|
+
*/
|
|
11
|
+
export declare class HDRRenderTarget {
|
|
12
|
+
readonly device: GPUDevice;
|
|
13
|
+
readonly samples: number;
|
|
14
|
+
readonly depthFormat: GPUTextureFormat;
|
|
15
|
+
scene: {
|
|
16
|
+
readonly color: GPUTextureView;
|
|
17
|
+
readonly depth: GPUTextureView;
|
|
18
|
+
begin(context: Extract<PassContext, {
|
|
19
|
+
backend: 'webgpu';
|
|
20
|
+
}>): void;
|
|
21
|
+
resolve(encoder: GPUCommandEncoder, final?: boolean): void;
|
|
22
|
+
restore(context: Extract<PassContext, {
|
|
23
|
+
backend: 'webgpu';
|
|
24
|
+
}>): void;
|
|
25
|
+
dispose(): void;
|
|
26
|
+
};
|
|
27
|
+
color: GPUTexture;
|
|
28
|
+
depth: GPUTexture;
|
|
29
|
+
width: number;
|
|
30
|
+
height: number;
|
|
31
|
+
constructor(device: GPUDevice, width: number, height: number, samples?: number, depthFormat?: GPUTextureFormat);
|
|
32
|
+
resize(width: number, height: number): void;
|
|
33
|
+
dispose(): void;
|
|
34
|
+
[Symbol.dispose]: () => void;
|
|
35
|
+
}
|
package/dist/targets.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { t as e } from "./msaa-8hWFieoE.js";
|
|
2
|
+
import { t } from "./quality-DFm-cQWC.js";
|
|
3
|
+
import { t as n } from "./texture-target-CGqE9pOa.js";
|
|
4
|
+
//#region src/targets.ts
|
|
5
|
+
var r = class {
|
|
6
|
+
constructor(e, n, r, i = t(), a = e.features.has("depth32float-stencil8") ? "depth32float-stencil8" : "depth32float") {
|
|
7
|
+
this.device = e, this.samples = i, this.depthFormat = a, this.width = 0, this.height = 0, this.resize(n, r);
|
|
8
|
+
}
|
|
9
|
+
resize(t, n) {
|
|
10
|
+
if (!Number.isSafeInteger(t) || !Number.isSafeInteger(n) || Math.min(t, n) < 1 || Math.max(t, n) > Math.min(4096, this.device.limits.maxTextureDimension2D)) throw RangeError("HDR target size exceeds limits; use a size between 1 and min(4096, device maxTextureDimension2D)");
|
|
11
|
+
if (t === this.width && n === this.height) return;
|
|
12
|
+
let r = this.device.createTexture({
|
|
13
|
+
size: [t, n],
|
|
14
|
+
format: "rgba16float",
|
|
15
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_SRC
|
|
16
|
+
}), i;
|
|
17
|
+
try {
|
|
18
|
+
i = this.device.createTexture({
|
|
19
|
+
size: [t, n],
|
|
20
|
+
format: this.depthFormat,
|
|
21
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
22
|
+
});
|
|
23
|
+
} catch (e) {
|
|
24
|
+
throw r.destroy(), e;
|
|
25
|
+
}
|
|
26
|
+
let a;
|
|
27
|
+
try {
|
|
28
|
+
a = new e(this.device, r, i, this.samples);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
throw r.destroy(), i.destroy(), e;
|
|
31
|
+
}
|
|
32
|
+
this.scene?.dispose(), this.color?.destroy(), this.depth?.destroy(), this.scene = a, this.color = r, this.depth = i, this.width = t, this.height = n;
|
|
33
|
+
}
|
|
34
|
+
dispose() {
|
|
35
|
+
this.scene?.dispose(), this.color?.destroy(), this.depth?.destroy(), this.width = this.height = 0;
|
|
36
|
+
}
|
|
37
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
38
|
+
this.dispose();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
//#endregion
|
|
42
|
+
export { r as HDRRenderTarget, n as TextureRenderTarget };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { r as e, t } from "./errors-XvcwUnU4.js";
|
|
2
|
+
//#region src/texture-target.ts
|
|
3
|
+
var n = (t) => e(`Texture target ${t}`, "creative-target"), r = (e, t, r, i) => {
|
|
4
|
+
if (!Number.isSafeInteger(e) || e < t || e > r) throw n(`${i} must be an integer in ${t}..${r}`);
|
|
5
|
+
}, i = (e) => {
|
|
6
|
+
if (!e || typeof e.createTexture != "function") throw t("unsupported", "TextureRenderTarget needs WebGPU; create a WebGPU engine and pass engine.gpu");
|
|
7
|
+
return e;
|
|
8
|
+
}, a = class {
|
|
9
|
+
#e;
|
|
10
|
+
#t;
|
|
11
|
+
#n;
|
|
12
|
+
#r = /* @__PURE__ */ new Map();
|
|
13
|
+
#i = !1;
|
|
14
|
+
constructor(e, t) {
|
|
15
|
+
if (this.#e = i(e), !t || ![
|
|
16
|
+
"cube",
|
|
17
|
+
"array",
|
|
18
|
+
"3d"
|
|
19
|
+
].includes(t.kind)) throw n("kind must be cube, array or 3d");
|
|
20
|
+
this.options = Object.freeze({
|
|
21
|
+
kind: t.kind,
|
|
22
|
+
width: t.width,
|
|
23
|
+
height: t.height ?? t.width,
|
|
24
|
+
depthOrLayers: t.depthOrLayers ?? (t.kind === "cube" ? 6 : 1),
|
|
25
|
+
mipLevelCount: t.mipLevelCount ?? 1,
|
|
26
|
+
format: t.format ?? "rgba8unorm"
|
|
27
|
+
}), this.#a(this.#e);
|
|
28
|
+
let r = this.#o(this.#e);
|
|
29
|
+
this.#t = r.texture, this.#n = r.view;
|
|
30
|
+
}
|
|
31
|
+
#a(e) {
|
|
32
|
+
let t = this.options, i = t.kind === "3d" ? e.limits.maxTextureDimension3D : e.limits.maxTextureDimension2D;
|
|
33
|
+
if (r(t.width, 1, i, "width"), r(t.height, 1, i, "height"), r(t.depthOrLayers, 1, t.kind === "3d" ? i : e.limits.maxTextureArrayLayers, "depthOrLayers"), t.kind === "cube" && (t.width !== t.height || t.depthOrLayers !== 6)) throw n("cube must be square with exactly six faces");
|
|
34
|
+
let a = 1 + Math.floor(Math.log2(Math.max(t.width, t.height, t.kind === "3d" ? t.depthOrLayers : 1)));
|
|
35
|
+
if (r(t.mipLevelCount, 1, a, "mipLevelCount"), t.format !== "rgba8unorm" && t.format !== "rgba16float") throw n("format must be rgba8unorm or rgba16float");
|
|
36
|
+
let o = 0;
|
|
37
|
+
for (let e = 0; e < t.mipLevelCount; e++) {
|
|
38
|
+
let [n, r, i] = this.mipSize(e);
|
|
39
|
+
o += n * r * i * (t.format === "rgba16float" ? 8 : 4);
|
|
40
|
+
}
|
|
41
|
+
if (o > 268435456) throw n("allocation exceeds 256 MiB; reduce dimensions or mip levels");
|
|
42
|
+
}
|
|
43
|
+
#o(e) {
|
|
44
|
+
let t = this.options, n = e.createTexture({
|
|
45
|
+
label: "creative texture target",
|
|
46
|
+
dimension: t.kind === "3d" ? "3d" : "2d",
|
|
47
|
+
size: [
|
|
48
|
+
t.width,
|
|
49
|
+
t.height,
|
|
50
|
+
t.depthOrLayers
|
|
51
|
+
],
|
|
52
|
+
mipLevelCount: t.mipLevelCount,
|
|
53
|
+
format: t.format,
|
|
54
|
+
usage: this.textureUsage()
|
|
55
|
+
});
|
|
56
|
+
try {
|
|
57
|
+
return {
|
|
58
|
+
texture: n,
|
|
59
|
+
view: n.createView({ dimension: t.kind === "array" ? "2d-array" : t.kind })
|
|
60
|
+
};
|
|
61
|
+
} catch (e) {
|
|
62
|
+
throw n.destroy(), e;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
textureUsage() {
|
|
66
|
+
return GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC;
|
|
67
|
+
}
|
|
68
|
+
#s() {
|
|
69
|
+
if (this.#i) throw t("disposed", "TextureRenderTarget is disposed; create a new target");
|
|
70
|
+
}
|
|
71
|
+
get texture() {
|
|
72
|
+
return this.#s(), this.#t;
|
|
73
|
+
}
|
|
74
|
+
get view() {
|
|
75
|
+
return this.#s(), this.#n;
|
|
76
|
+
}
|
|
77
|
+
mipSize(e = 0) {
|
|
78
|
+
this.#s();
|
|
79
|
+
let t = this.options;
|
|
80
|
+
r(e, 0, t.mipLevelCount - 1, "mip");
|
|
81
|
+
let n = 2 ** e;
|
|
82
|
+
return [
|
|
83
|
+
Math.max(1, Math.floor(t.width / n)),
|
|
84
|
+
Math.max(1, Math.floor(t.height / n)),
|
|
85
|
+
t.kind === "3d" ? Math.max(1, Math.floor(t.depthOrLayers / n)) : t.depthOrLayers
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
begin(e, t = {}) {
|
|
89
|
+
this.#s();
|
|
90
|
+
let { mip: i = 0, layer: a = 0, loadOp: o = "clear", clearColor: s = [
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
0,
|
|
94
|
+
0
|
|
95
|
+
] } = t;
|
|
96
|
+
if (r(a, 0, this.mipSize(i)[2] - 1, "layer"), o !== "clear" && o !== "load") throw n("loadOp must be clear or load");
|
|
97
|
+
let c = this.options.format === "rgba16float" ? 65504 : 1, l = this.options.format === "rgba16float" ? -65504 : 0;
|
|
98
|
+
if (!Array.isArray(s) || s.length !== 4 || Array.from(s).some((e) => !Number.isFinite(e) || e < l || e > c)) throw n(`clearColor must contain four finite components in ${l}..${c}`);
|
|
99
|
+
let u = this.options.kind === "3d", d = `${i}:${u ? 0 : a}`, f = this.#r.get(d);
|
|
100
|
+
return f || (f = this.#t.createView({
|
|
101
|
+
dimension: u ? "3d" : "2d",
|
|
102
|
+
baseMipLevel: i,
|
|
103
|
+
mipLevelCount: 1,
|
|
104
|
+
...u ? {} : {
|
|
105
|
+
baseArrayLayer: a,
|
|
106
|
+
arrayLayerCount: 1
|
|
107
|
+
}
|
|
108
|
+
}), this.#r.set(d, f)), e.beginRenderPass({ colorAttachments: [{
|
|
109
|
+
view: f,
|
|
110
|
+
...u ? { depthSlice: a } : {},
|
|
111
|
+
loadOp: o,
|
|
112
|
+
storeOp: "store",
|
|
113
|
+
clearValue: [...s]
|
|
114
|
+
}] });
|
|
115
|
+
}
|
|
116
|
+
restore(e) {
|
|
117
|
+
this.#s();
|
|
118
|
+
let t = i(e);
|
|
119
|
+
this.#a(t);
|
|
120
|
+
let n = this.#o(t);
|
|
121
|
+
this.#t.destroy(), this.#t = n.texture, this.#n = n.view, this.#e = t, this.#r.clear();
|
|
122
|
+
}
|
|
123
|
+
dispose() {
|
|
124
|
+
this.#i || (this.#i = !0, this.#t.destroy(), this.#r.clear());
|
|
125
|
+
}
|
|
126
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
127
|
+
this.dispose();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
export { a as t };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
/** A cube uses the WebGPU face order +X, -X, +Y, -Y, +Z, -Z. */
|
|
3
|
+
export interface TextureRenderTargetOptions {
|
|
4
|
+
readonly kind: 'cube' | 'array' | '3d';
|
|
5
|
+
/** Pixel width, bounded by the device. */
|
|
6
|
+
readonly width: number;
|
|
7
|
+
/** Pixel height. Default: width. Cubes must be square. */
|
|
8
|
+
readonly height?: number;
|
|
9
|
+
/** Array layers or volume depth. Default: 1, or 6 for a cube (fixed). */
|
|
10
|
+
readonly depthOrLayers?: number;
|
|
11
|
+
/** Allocated levels. Default: 1. Mips are rendered explicitly, never generated. */
|
|
12
|
+
readonly mipLevelCount?: number;
|
|
13
|
+
/** Linear color format. Default: rgba8unorm. */
|
|
14
|
+
readonly format?: 'rgba8unorm' | 'rgba16float';
|
|
15
|
+
}
|
|
16
|
+
/** Select one renderable subresource; all defaults address mip 0, layer/slice 0. */
|
|
17
|
+
export interface TargetSurface {
|
|
18
|
+
readonly mip?: number;
|
|
19
|
+
/** Cube face, array layer, or depth slice at the selected mip. */
|
|
20
|
+
readonly layer?: number;
|
|
21
|
+
/** Default: clear. Use load to preserve earlier draws to the same surface. */
|
|
22
|
+
readonly loadOp?: 'clear' | 'load';
|
|
23
|
+
/** Default: transparent black. Four finite components, 0..1 (rgba8) or +/-65504 (HDR). */
|
|
24
|
+
readonly clearColor?: readonly [number, number, number, number];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Owned color-only cube, array or volume texture. Render with begin(), sample with view.
|
|
28
|
+
* No core scene capture, depth, MSAA, camera updates or automatic mip generation.
|
|
29
|
+
* Call restore(newDevice) after device loss and redraw every surface; dispose is terminal.
|
|
30
|
+
*/
|
|
31
|
+
export declare class TextureRenderTarget {
|
|
32
|
+
#private;
|
|
33
|
+
readonly options: Readonly<Required<TextureRenderTargetOptions>>;
|
|
34
|
+
constructor(device: GPUDevice | null | undefined, options: TextureRenderTargetOptions);
|
|
35
|
+
/** Attachment extensions may request additional copy usages before allocation. */
|
|
36
|
+
protected textureUsage(): number;
|
|
37
|
+
/** Live owned texture, also usable for copyTextureToBuffer readback. */
|
|
38
|
+
get texture(): GPUTexture;
|
|
39
|
+
/** Whole-resource sampling view, with dimension cube, 2d-array or 3d. */
|
|
40
|
+
get view(): GPUTextureView;
|
|
41
|
+
/** Logical width, height and depth/layer count at a mip. Array and cube layers never shrink. */
|
|
42
|
+
mipSize(mip?: number): readonly [number, number, number];
|
|
43
|
+
/** Begin a color pass. The caller sets a matching single-sample pipeline, draws and ends the pass. */
|
|
44
|
+
begin(encoder: GPUCommandEncoder, surface?: TargetSurface): GPURenderPassEncoder;
|
|
45
|
+
/** Replace device resources, preserving only options. Fetch fresh texture/view handles and redraw afterward. */
|
|
46
|
+
restore(device: GPUDevice | null | undefined): void;
|
|
47
|
+
dispose(): void;
|
|
48
|
+
[Symbol.dispose]: () => void;
|
|
49
|
+
}
|
package/dist/toon.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
/** Toon ramp settings, fixed at construction; out of range throws RangeError. */
|
|
5
|
+
export interface ToonOptions {
|
|
6
|
+
/** Diffuse light steps, an integer 2-16. Default 4. */
|
|
7
|
+
bands?: number;
|
|
8
|
+
/** Rim light strength at silhouettes, 0..1. Default 0.15. */
|
|
9
|
+
rim?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Stepped (cel) diffuse lighting with a rim term, patched into core's PBR shader for every core mesh.
|
|
13
|
+
* `ToonShading.attach(engine, options)`; `@margoengine/toon` adds outlines on top. WebGPU only.
|
|
14
|
+
* @throws RangeError when an option is out of range; MargoError `'unsupported'` on a WebGL engine.
|
|
15
|
+
*/
|
|
16
|
+
export declare class ToonShading implements EnginePlugin {
|
|
17
|
+
readonly name = "toon-shading";
|
|
18
|
+
private engine?;
|
|
19
|
+
private buffer?;
|
|
20
|
+
private entry?;
|
|
21
|
+
private readonly values;
|
|
22
|
+
private readonly shaders;
|
|
23
|
+
/** Same as `engine.use(new ToonShading(...))`; returns the ToonShading. */
|
|
24
|
+
static attach(engine: EngineKernel, options?: ToonOptions): ToonShading;
|
|
25
|
+
constructor({ bands, rim }?: ToonOptions);
|
|
26
|
+
setup(engine: Engine): void;
|
|
27
|
+
registerPasses(graph: FrameGraph): void;
|
|
28
|
+
private release;
|
|
29
|
+
onDeviceRestored(engine: Engine): void;
|
|
30
|
+
dispose(): void;
|
|
31
|
+
[Symbol.dispose]: () => void;
|
|
32
|
+
}
|
package/dist/toon.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/toon.ts
|
|
6
|
+
var t = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
7
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
8
|
+
let r = new t(n);
|
|
9
|
+
return e.use(r), r;
|
|
10
|
+
}
|
|
11
|
+
constructor({ bands: e = 4, rim: t = .15 } = {}) {
|
|
12
|
+
if (this.name = "toon-shading", this.shaders = /* @__PURE__ */ new Map(), !Number.isSafeInteger(e) || e < 2 || e > 16 || !Number.isFinite(t) || t < 0 || t > 1) throw RangeError("Invalid toon options; use integer bands 2..16 and rim 0..1");
|
|
13
|
+
this.values = new Float32Array([
|
|
14
|
+
e,
|
|
15
|
+
t,
|
|
16
|
+
0,
|
|
17
|
+
0
|
|
18
|
+
]);
|
|
19
|
+
}
|
|
20
|
+
setup(t) {
|
|
21
|
+
if (!t.gpu) throw new e("unsupported", "ToonShading requires WebGPU; use a WebGPU engine; see https://margoengine.com/errors#webgpu");
|
|
22
|
+
this.engine = t, this.buffer = t.gpu.createBuffer({
|
|
23
|
+
size: 16,
|
|
24
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
25
|
+
}), this.entry = {
|
|
26
|
+
binding: 19,
|
|
27
|
+
resource: { buffer: this.buffer }
|
|
28
|
+
}, t.gpu.queue.writeBuffer(this.buffer, 0, this.values);
|
|
29
|
+
}
|
|
30
|
+
registerPasses(e) {
|
|
31
|
+
e.add({
|
|
32
|
+
name: "toon:shade",
|
|
33
|
+
order: -25,
|
|
34
|
+
execute: (e) => {
|
|
35
|
+
if (e.backend !== "webgpu") return;
|
|
36
|
+
let t = this.shaders.get(19);
|
|
37
|
+
t || (t = /* @__PURE__ */ new Map(), this.shaders.set(19, t));
|
|
38
|
+
let n = t.get(e.shader);
|
|
39
|
+
n === void 0 && (n = "@group(0) @binding(19) var<uniform> toon:vec4f;\n" + e.shader.replace(/let direct=[^;]+;/, "let band=min(toon.x-1.0,floor(max(dot(n,l),0.0)*toon.x))/(toon.x-1.0);let rim=pow(1.0-clamp(dot(n,v),0.0,1.0),3.0)*toon.y;let direct=i.color*band;").replace("i.color*(1.0-i.material.y)*frame.radiance.w", "i.color*frame.radiance.w").replace(/let linear=([^;]+);/, "let linear=$1+vec3f(rim);"), t.set(e.shader, n)), e.shader = n, this.entry.binding = 19, e.bindings.push(this.entry);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
release() {
|
|
44
|
+
this.buffer?.destroy(), this.buffer = void 0, this.entry = void 0, this.shaders.clear();
|
|
45
|
+
}
|
|
46
|
+
onDeviceRestored(e) {
|
|
47
|
+
this.release(), this.setup(e);
|
|
48
|
+
}
|
|
49
|
+
dispose() {
|
|
50
|
+
let e = this.engine?.sceneOf(this);
|
|
51
|
+
this.engine = void 0, e?.removePlugin(this.name), this.release();
|
|
52
|
+
}
|
|
53
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
54
|
+
this.dispose();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
//#endregion
|
|
58
|
+
export { t as ToonShading };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//#region ../shared/view-rect.js
|
|
2
|
+
function e(e) {
|
|
3
|
+
return e?.view?.viewport;
|
|
4
|
+
}
|
|
5
|
+
function t(t, n) {
|
|
6
|
+
let r = e(n);
|
|
7
|
+
if (!r) return;
|
|
8
|
+
t.setViewport(r[0], r[1], r[2], r[3], 0, 1);
|
|
9
|
+
let i = n.view.scissor ?? r;
|
|
10
|
+
t.setScissorRect(i[0], i[1], i[2], i[3]);
|
|
11
|
+
}
|
|
12
|
+
function n(e, t = "clear") {
|
|
13
|
+
return e?.view?.index ? "load" : t;
|
|
14
|
+
}
|
|
15
|
+
function r(e) {
|
|
16
|
+
return e?.view?.id ?? 0;
|
|
17
|
+
}
|
|
18
|
+
var i = class {
|
|
19
|
+
constructor(e, t = () => {}) {
|
|
20
|
+
this.create = e, this.release = t, this.states = /* @__PURE__ */ new Map(), this.frame = 0;
|
|
21
|
+
}
|
|
22
|
+
get(e) {
|
|
23
|
+
let t = r(e), n = this.states.get(t);
|
|
24
|
+
return n || this.states.set(t, n = {
|
|
25
|
+
state: this.create(e),
|
|
26
|
+
frame: 0
|
|
27
|
+
}), n.frame = this.frame, n.state;
|
|
28
|
+
}
|
|
29
|
+
find(e) {
|
|
30
|
+
return this.states.get(e)?.state;
|
|
31
|
+
}
|
|
32
|
+
*values() {
|
|
33
|
+
for (let e of this.states.values()) yield e.state;
|
|
34
|
+
}
|
|
35
|
+
*drawn() {
|
|
36
|
+
for (let e of this.states.values()) e.frame === this.frame && (yield e.state);
|
|
37
|
+
}
|
|
38
|
+
prune() {
|
|
39
|
+
for (let [e, t] of this.states) t.frame < this.frame && (this.states.delete(e), this.release(t.state));
|
|
40
|
+
this.frame++;
|
|
41
|
+
}
|
|
42
|
+
clear() {
|
|
43
|
+
for (let e of this.states.values()) this.release(e.state);
|
|
44
|
+
this.states.clear();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
export { e as i, t as n, n as r, i as t };
|