@margoengine/creative 0.0.0-stage → 0.1.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 +307 -3
- package/dist/display-C6RC-F2-.js +3 -0
- package/dist/errors-XvcwUnU4.js +7 -0
- package/dist/gpu-DN8sGUpu.js +30 -0
- package/dist/grass-StuKCE_k.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-DjFZ9hVW.js +213 -0
- package/dist/paint-aniso.d.ts +36 -0
- package/dist/paint-aniso.js +215 -0
- package/dist/paint.d.ts +56 -0
- package/dist/paint.js +148 -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-C2ewqAmf.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-DWF7qnc1.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 +126 -6
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const l=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { r as e, t } from "./errors-XvcwUnU4.js";
|
|
5
|
+
import { i as n, n as r, r as i, t as a } from "./view-rect-TO68f_UR.js";
|
|
6
|
+
import { n as o, r as s, t as c } from "./gpu-DN8sGUpu.js";
|
|
7
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
8
|
+
import { PostStack as u } from "@margoengine/post/stack";
|
|
9
|
+
//#region src/paint-aniso.ts
|
|
10
|
+
var d = `${c}
|
|
11
|
+
struct Paint {rect:vec4i,parameters:vec4f}
|
|
12
|
+
@group(0) @binding(0) var<uniform> paint:Paint;
|
|
13
|
+
@group(0) @binding(1) var image:texture_2d<f32>;
|
|
14
|
+
fn coord(p:vec2i)->vec2i {return clamp(p,paint.rect.xy,paint.rect.xy+paint.rect.zw-1);}
|
|
15
|
+
fn read(p:vec2i)->vec3f {return textureLoad(image,coord(p),0).rgb;}
|
|
16
|
+
`, f = `${d}
|
|
17
|
+
@fragment fn fragment(@builtin(position) at:vec4f)->@location(0) vec4f {
|
|
18
|
+
let p=vec2i(at.xy);
|
|
19
|
+
let nw=read(p+vec2i(-1,-1));let n=read(p+vec2i(0,-1));let ne=read(p+vec2i(1,-1));
|
|
20
|
+
let w=read(p+vec2i(-1,0));let e=read(p+vec2i(1,0));
|
|
21
|
+
let sw=read(p+vec2i(-1,1));let s=read(p+vec2i(0,1));let se=read(p+vec2i(1,1));
|
|
22
|
+
let dx=(ne+2.0*e+se-nw-2.0*w-sw)*0.125;
|
|
23
|
+
let dy=(sw+2.0*s+se-nw-2.0*n-ne)*0.125;
|
|
24
|
+
return vec4f(dot(dx,dx),dot(dx,dy),dot(dy,dy),0);
|
|
25
|
+
}`, p = `${d}
|
|
26
|
+
@fragment fn fragment(@builtin(position) at:vec4f)->@location(0) vec4f {
|
|
27
|
+
let p=vec2i(at.xy);
|
|
28
|
+
return vec4f((read(p-vec2i(2,0))+4.0*read(p-vec2i(1,0))+6.0*read(p)+4.0*read(p+vec2i(1,0))+read(p+vec2i(2,0)))/16.0,0);
|
|
29
|
+
}`, m = `${d}
|
|
30
|
+
@group(0) @binding(2) var tensor:texture_2d<f32>;
|
|
31
|
+
fn tensorAt(p:vec2i)->vec3f {return textureLoad(tensor,coord(p),0).rgb;}
|
|
32
|
+
@fragment fn fragment(@builtin(position) at:vec4f)->@location(0) vec4f {
|
|
33
|
+
let p=vec2i(at.xy);let centre=textureLoad(image,p,0);
|
|
34
|
+
if(paint.parameters.y==0.0){return centre;}
|
|
35
|
+
let t=(tensorAt(p-vec2i(0,2))+4.0*tensorAt(p-vec2i(0,1))+6.0*tensorAt(p)+4.0*tensorAt(p+vec2i(0,1))+tensorAt(p+vec2i(0,2)))/16.0;
|
|
36
|
+
let trace=t.x+t.z;let gap=length(vec2f(t.x-t.z,2.0*t.y));
|
|
37
|
+
var angle=0.0;var adaptation=0.0;
|
|
38
|
+
if(trace>1e-8){angle=0.5*atan2(2.0*t.y,t.x-t.z)+1.57079632679;adaptation=clamp(gap/trace,0.0,1.0)*paint.parameters.z;}
|
|
39
|
+
let tangent=vec2f(cos(angle),sin(angle));let normal=vec2f(-tangent.y,tangent.x);
|
|
40
|
+
let radius=paint.parameters.x;let a=radius*(1.0+adaptation);let b=radius/(1.0+adaptation);
|
|
41
|
+
let bound=i32(ceil(a));let zeta=2.0/radius;let eta=(zeta+0.83146961230)/0.30865828382;
|
|
42
|
+
let dirs=array<vec2f,8>(vec2f(1,0),vec2f(0.70710678118,0.70710678118),vec2f(0,1),vec2f(-0.70710678118,0.70710678118),vec2f(-1,0),vec2f(-0.70710678118,-0.70710678118),vec2f(0,-1),vec2f(0.70710678118,-0.70710678118));
|
|
43
|
+
var sums:array<vec3f,8>;var squares:array<vec3f,8>;var weights:array<f32,8>;
|
|
44
|
+
for(var y=-bound;y<=bound;y++){for(var x=-bound;x<=bound;x++){
|
|
45
|
+
let delta=vec2f(f32(x),f32(y));let q=vec2f(dot(delta,tangent)/a,dot(delta,normal)/b);let r2=dot(q,q);
|
|
46
|
+
if(r2>1.0){continue;}
|
|
47
|
+
var w:array<f32,8>;var total=0.0;
|
|
48
|
+
for(var k=0;k<8;k++){let d=dirs[k];let across=dot(q,vec2f(-d.y,d.x));let value=max(0.0,zeta+dot(q,d)-eta*across*across);w[k]=value*value;total+=w[k];}
|
|
49
|
+
let distance=sqrt(r2);let edge=max(0.0,1.0-distance);let radial=exp(-3.125*r2)*edge*edge*edge*edge*(1.0+4.0*distance)/max(total,1e-20);let colour=read(p+vec2i(x,y));
|
|
50
|
+
for(var k=0;k<8;k++){let weight=w[k]*radial;sums[k]+=colour*weight;squares[k]+=colour*colour*weight;weights[k]+=weight;}
|
|
51
|
+
}}
|
|
52
|
+
var result=vec3f(0);var total=0.0;
|
|
53
|
+
for(var k=0;k<8;k++){
|
|
54
|
+
let mean=sums[k]/weights[k];let variance=max(squares[k]/weights[k]-mean*mean,vec3f(0));
|
|
55
|
+
let preference=1.0/(1.0+pow(100.0*sqrt(dot(variance,vec3f(1))),paint.parameters.w));
|
|
56
|
+
result+=mean*preference;total+=preference;
|
|
57
|
+
}
|
|
58
|
+
return vec4f(mix(centre.rgb,clamp(result/total,vec3f(0),vec3f(1)),paint.parameters.y),centre.a);
|
|
59
|
+
}`, h = class c {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
60
|
+
#e;
|
|
61
|
+
#t;
|
|
62
|
+
#n;
|
|
63
|
+
#r;
|
|
64
|
+
#i;
|
|
65
|
+
#a;
|
|
66
|
+
#o;
|
|
67
|
+
#s;
|
|
68
|
+
#c;
|
|
69
|
+
#l;
|
|
70
|
+
#u;
|
|
71
|
+
#d;
|
|
72
|
+
constructor(e = {}) {
|
|
73
|
+
this.options = e, this.name = "anisotropic-painterly", this.#n = !1, this.#r = !1, this.#s = /* @__PURE__ */ new Map(), this.#c = /* @__PURE__ */ new ArrayBuffer(32), this.#l = new Int32Array(this.#c, 0, 4), this.#u = new Float32Array(this.#c, 16, 4), this.#d = new a(() => ({ uniform: this.#e.gpu.createBuffer({
|
|
74
|
+
size: 32,
|
|
75
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
76
|
+
}) }), (e) => {
|
|
77
|
+
e.uniform.destroy(), e.first?.destroy(), e.second?.destroy();
|
|
78
|
+
}), this.#p();
|
|
79
|
+
}
|
|
80
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
81
|
+
let r = new c(n);
|
|
82
|
+
if (!l(e).gpu) throw t("unsupported", "Anisotropic paint needs a GPU device; use WebGPU or the full compatibility layer");
|
|
83
|
+
return r.#t = u.join(e, r), r;
|
|
84
|
+
}
|
|
85
|
+
get enabled() {
|
|
86
|
+
return this.options.enabled ?? !0;
|
|
87
|
+
}
|
|
88
|
+
set enabled(t) {
|
|
89
|
+
if (typeof t != "boolean") throw e("Invalid anisotropic enabled; pass true or false", "arguments");
|
|
90
|
+
this.options.enabled = t;
|
|
91
|
+
}
|
|
92
|
+
joinStack() {
|
|
93
|
+
if (this.#f(), this.#n || this.#e) throw t("attached", "Anisotropic paint is attached; create a new effect", "state");
|
|
94
|
+
return this.#n = !0, {
|
|
95
|
+
kind: "ldr",
|
|
96
|
+
render: (e, t, n) => this.#h(e, t, n)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
setup(e) {
|
|
100
|
+
if (this.#f(), this.#e) throw t("attached", "Anisotropic paint is attached; create a new effect", "state");
|
|
101
|
+
if (!e.gpu) throw t("unsupported", "Anisotropic paint needs a GPU device; use WebGPU or the full compatibility layer");
|
|
102
|
+
if (!this.#n) throw t("state", "Anisotropic paint needs a stack; use attach or PostStack");
|
|
103
|
+
this.#p(), this.#e = e;
|
|
104
|
+
}
|
|
105
|
+
#f() {
|
|
106
|
+
if (this.#r) throw t("disposed", "Anisotropic paint is disposed; create a new effect");
|
|
107
|
+
}
|
|
108
|
+
#p() {
|
|
109
|
+
let t = this.options;
|
|
110
|
+
if (!t || typeof t != "object" || Array.isArray(t)) throw e("Invalid anisotropic options; pass an options object", "arguments");
|
|
111
|
+
if (t.radius !== void 0 && (!Number.isInteger(t.radius) || t.radius < 1 || t.radius > 8)) throw e("Invalid anisotropic radius; pass an integer in 1..8", "arguments");
|
|
112
|
+
for (let [n, r, i, a] of [
|
|
113
|
+
[
|
|
114
|
+
"strength",
|
|
115
|
+
t.strength,
|
|
116
|
+
0,
|
|
117
|
+
1
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
"anisotropy",
|
|
121
|
+
t.anisotropy,
|
|
122
|
+
0,
|
|
123
|
+
1
|
|
124
|
+
],
|
|
125
|
+
[
|
|
126
|
+
"sharpness",
|
|
127
|
+
t.sharpness,
|
|
128
|
+
1,
|
|
129
|
+
8
|
|
130
|
+
]
|
|
131
|
+
]) if (r !== void 0 && (!Number.isFinite(r) || r < i || r > a)) throw e(`Invalid anisotropic ${n}; pass a finite number in ${i}..${a}`, "arguments");
|
|
132
|
+
if (t.enabled !== void 0 && typeof t.enabled != "boolean") throw e("Invalid anisotropic enabled; pass true or false", "arguments");
|
|
133
|
+
}
|
|
134
|
+
#m(e, t) {
|
|
135
|
+
let n = e.device;
|
|
136
|
+
if (!this.#i) {
|
|
137
|
+
let e = [{
|
|
138
|
+
binding: 0,
|
|
139
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
140
|
+
buffer: { type: "uniform" }
|
|
141
|
+
}, {
|
|
142
|
+
binding: 1,
|
|
143
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
144
|
+
texture: { sampleType: "unfilterable-float" }
|
|
145
|
+
}], t = n.createPipelineLayout({ bindGroupLayouts: [n.createBindGroupLayout({ entries: e })] });
|
|
146
|
+
this.#i = s(n, f, "rgba32float", { layout: t }), this.#a = s(n, p, "rgba32float", { layout: t }), this.#o = n.createBindGroupLayout({ entries: [...e, {
|
|
147
|
+
binding: 2,
|
|
148
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
149
|
+
texture: { sampleType: "unfilterable-float" }
|
|
150
|
+
}] });
|
|
151
|
+
}
|
|
152
|
+
let r = t.format + ":" + (t.multisample?.count ?? 1), i = this.#s.get(r);
|
|
153
|
+
return i || (i = s(n, m, t.format, {
|
|
154
|
+
layout: n.createPipelineLayout({ bindGroupLayouts: [this.#o] }),
|
|
155
|
+
multisample: t.multisample
|
|
156
|
+
}), this.#s.set(r, i)), i;
|
|
157
|
+
}
|
|
158
|
+
#h(r, i, a) {
|
|
159
|
+
if (this.#f(), this.#p(), !this.#e) throw t("state", "Anisotropic paint needs a live stack; attach before drawing");
|
|
160
|
+
if (r.width * r.height * 32 > 536870912) throw e("Anisotropic intermediate textures exceed 512 MiB; reduce render resolution", "arguments");
|
|
161
|
+
let s = this.#m(r, a), c = this.#d.get(r);
|
|
162
|
+
if (c.width !== r.width || c.height !== r.height) {
|
|
163
|
+
c.first?.destroy(), c.second?.destroy();
|
|
164
|
+
let e = () => r.device.createTexture({
|
|
165
|
+
size: [r.width, r.height],
|
|
166
|
+
format: "rgba32float",
|
|
167
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
168
|
+
});
|
|
169
|
+
c.first = e(), c.second = e(), c.firstView = c.first.createView(), c.secondView = c.second.createView(), c.width = r.width, c.height = r.height, c.input = void 0;
|
|
170
|
+
}
|
|
171
|
+
c.input !== i && (c.tensorGroup = o(r.device, this.#i, [{ buffer: c.uniform }, i]), c.blurGroup = o(r.device, this.#a, [{ buffer: c.uniform }, c.firstView]), c.filterGroup = o(r.device, s, [
|
|
172
|
+
{ buffer: c.uniform },
|
|
173
|
+
i,
|
|
174
|
+
c.secondView
|
|
175
|
+
]), c.input = i), this.#l.set(n(r) ?? [
|
|
176
|
+
0,
|
|
177
|
+
0,
|
|
178
|
+
r.width,
|
|
179
|
+
r.height
|
|
180
|
+
]), this.#u.set([
|
|
181
|
+
this.options.radius ?? 3,
|
|
182
|
+
this.enabled ? this.options.strength ?? 1 : 0,
|
|
183
|
+
this.options.anisotropy ?? 1,
|
|
184
|
+
this.options.sharpness ?? 8
|
|
185
|
+
]), r.device.queue.writeBuffer(c.uniform, 0, this.#c), this.#u[1] > 0 && (this.#g(r, this.#i, c.tensorGroup, c.firstView), this.#g(r, this.#a, c.blurGroup, c.secondView)), this.#g(r, s, c.filterGroup, a.view);
|
|
186
|
+
}
|
|
187
|
+
#g(e, t, n, a) {
|
|
188
|
+
let o = e.encoder.beginRenderPass({ colorAttachments: [{
|
|
189
|
+
view: a,
|
|
190
|
+
loadOp: i(e, "clear"),
|
|
191
|
+
storeOp: "store"
|
|
192
|
+
}] });
|
|
193
|
+
o.setPipeline(t), o.setBindGroup(0, n), r(o, e), o.draw(3), o.end();
|
|
194
|
+
}
|
|
195
|
+
onPreUpdate(e) {
|
|
196
|
+
e > 0 && this.#d.prune();
|
|
197
|
+
}
|
|
198
|
+
#_() {
|
|
199
|
+
this.#d.clear(), this.#s.clear(), this.#i = void 0, this.#a = void 0, this.#o = void 0;
|
|
200
|
+
}
|
|
201
|
+
onDeviceRestored(e) {
|
|
202
|
+
this.#f(), this.#_(), this.#e = void 0, this.setup(e);
|
|
203
|
+
}
|
|
204
|
+
dispose() {
|
|
205
|
+
if (this.#r) return;
|
|
206
|
+
this.#r = !0;
|
|
207
|
+
let e = this.#e;
|
|
208
|
+
this.#e = void 0, e?.sceneOf(this)?.removePlugin(this.name), this.#_(), this.#t?.removeEffect(this), this.#t = void 0;
|
|
209
|
+
}
|
|
210
|
+
[Symbol.dispose]() {
|
|
211
|
+
this.dispose();
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
//#endregion
|
|
215
|
+
export { h as AnisotropicPainterly };
|
package/dist/paint.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
/** Display-space painterly treatment. */
|
|
5
|
+
export type PainterlyMode = 'kuwahara' | 'watercolour' | 'sketch';
|
|
6
|
+
/** Live options, checked before drawing each frame. */
|
|
7
|
+
export interface PainterlyOptions {
|
|
8
|
+
/** Default 'kuwahara'. Watercolour and sketch are stylizations, not physical media simulations. */
|
|
9
|
+
mode?: PainterlyMode;
|
|
10
|
+
/** Integer pixel radius 1..8. Sketch uses this as its gradient spacing. Default 3. */
|
|
11
|
+
radius?: number;
|
|
12
|
+
/** Blend with the input, 0..1. Zero copies exactly. Default 1. */
|
|
13
|
+
strength?: number;
|
|
14
|
+
/** False copies exactly. Default true. */
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* LDR PostStack effect: classic Kuwahara, a quantized watercolour wash or a Sobel/hatching sketch.
|
|
19
|
+
* Preserves centre alpha; all neighbourhood reads clamp to the current view. No history or scene data required.
|
|
20
|
+
* `attach` owns a stack; otherwise use `new Painterly(options)` in a PostStack after display effects.
|
|
21
|
+
* @throws RangeError for invalid options (also live edits); MargoError 'unsupported' on WebGL2, 'state' outside
|
|
22
|
+
* a PostStack, 'attached' on a second attachment, and 'disposed' after disposal. Errors include a help link.
|
|
23
|
+
*/
|
|
24
|
+
export declare class Painterly implements EnginePlugin {
|
|
25
|
+
readonly options: PainterlyOptions;
|
|
26
|
+
readonly name = "painterly";
|
|
27
|
+
private engine?;
|
|
28
|
+
private stack?;
|
|
29
|
+
private stacked;
|
|
30
|
+
private disposed;
|
|
31
|
+
private pipeline?;
|
|
32
|
+
private key;
|
|
33
|
+
private readonly bytes;
|
|
34
|
+
private readonly rect;
|
|
35
|
+
private readonly flags;
|
|
36
|
+
private readonly blend;
|
|
37
|
+
private readonly views;
|
|
38
|
+
constructor(options?: PainterlyOptions);
|
|
39
|
+
/** Adds a new Painterly to the scene's PostStack after its last LDR effect, or to a new PostStack when the scene
|
|
40
|
+
* has none. Disposing the effect takes it out, and a stack made for it goes with it. */
|
|
41
|
+
static attach(engine: EngineKernel, options?: PainterlyOptions): Painterly;
|
|
42
|
+
/** Default true; false copies the input. */
|
|
43
|
+
get enabled(): boolean;
|
|
44
|
+
set enabled(value: boolean);
|
|
45
|
+
private joinStack;
|
|
46
|
+
setup(engine: Engine): void;
|
|
47
|
+
onPreUpdate(delta: number): void;
|
|
48
|
+
onDeviceRestored(engine: Engine): void;
|
|
49
|
+
/** Terminal, idempotent disposal; also disposes the stack made by attach. */
|
|
50
|
+
dispose(): void;
|
|
51
|
+
[Symbol.dispose]: () => void;
|
|
52
|
+
private alive;
|
|
53
|
+
private validate;
|
|
54
|
+
private draw;
|
|
55
|
+
private release;
|
|
56
|
+
}
|
package/dist/paint.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const l=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { r as e, t } from "./errors-XvcwUnU4.js";
|
|
5
|
+
import { i as n, n as r, r as i, t as a } from "./view-rect-TO68f_UR.js";
|
|
6
|
+
import { n as o, r as s, t as c } from "./gpu-DN8sGUpu.js";
|
|
7
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
8
|
+
import { PostStack as u } from "@margoengine/post/stack";
|
|
9
|
+
//#region src/paint.ts
|
|
10
|
+
var d = `${c}
|
|
11
|
+
struct Paint {rect:vec4i,flags:vec4u,blend:vec4f}
|
|
12
|
+
@group(0) @binding(0) var<uniform> paint:Paint;
|
|
13
|
+
@group(0) @binding(1) var image:texture_2d<f32>;
|
|
14
|
+
fn read(p:vec2i)->vec3f {
|
|
15
|
+
return textureLoad(image,clamp(p,paint.rect.xy,paint.rect.xy+paint.rect.zw-1),0).rgb;
|
|
16
|
+
}
|
|
17
|
+
fn luma(c:vec3f)->f32 {return dot(c,vec3f(0.299,0.587,0.114));}
|
|
18
|
+
fn edge(p:vec2i,r:i32)->f32 {
|
|
19
|
+
let nw=luma(read(p+vec2i(-r,-r)));let n=luma(read(p+vec2i(0,-r)));let ne=luma(read(p+vec2i(r,-r)));
|
|
20
|
+
let w=luma(read(p+vec2i(-r,0)));let e=luma(read(p+vec2i(r,0)));
|
|
21
|
+
let sw=luma(read(p+vec2i(-r,r)));let s=luma(read(p+vec2i(0,r)));let se=luma(read(p+vec2i(r,r)));
|
|
22
|
+
let dx=ne+2*e+se-nw-2*w-sw;let dy=sw+2*s+se-nw-2*n-ne;
|
|
23
|
+
return clamp(length(vec2f(dx,dy))*0.25,0.0,1.0);
|
|
24
|
+
}
|
|
25
|
+
fn smoothColour(p:vec2i,r:i32)->vec3f {
|
|
26
|
+
var best=vec3f(0);var variance=1e10;
|
|
27
|
+
let count=f32((r+1)*(r+1));
|
|
28
|
+
for(var q=0;q<4;q++) {
|
|
29
|
+
let direction=vec2i(select(-1,1,(q%2)==1),select(-1,1,q>=2));
|
|
30
|
+
var sum=vec3f(0);var squares=vec3f(0);
|
|
31
|
+
for(var y=0;y<=r;y++) {for(var x=0;x<=r;x++) {
|
|
32
|
+
let c=read(p+vec2i(x,y)*direction);sum+=c;squares+=c*c;
|
|
33
|
+
}}
|
|
34
|
+
let mean=sum/count;let v=dot(max(squares/count-mean*mean,vec3f(0)),vec3f(1));
|
|
35
|
+
if(v<variance) {variance=v;best=mean;}
|
|
36
|
+
}
|
|
37
|
+
return best;
|
|
38
|
+
}
|
|
39
|
+
@fragment fn fragment(@builtin(position) at:vec4f)->@location(0) vec4f {
|
|
40
|
+
let p=vec2i(at.xy);let centre=textureLoad(image,p,0);
|
|
41
|
+
if(paint.flags.z==0u||paint.blend.x==0.0) {return centre;}
|
|
42
|
+
let r=i32(paint.flags.x);let local=p-paint.rect.xy;
|
|
43
|
+
var colour:vec3f;
|
|
44
|
+
if(paint.flags.y==2u) {
|
|
45
|
+
let tone=luma(centre.rgb);let outline=edge(p,r);
|
|
46
|
+
let first=select(0.0,1.0,((local.x+local.y)%6)==0&&tone<0.75);
|
|
47
|
+
let second=select(0.0,1.0,((local.x-local.y+paint.rect.w)%6)==0&&tone<0.4);
|
|
48
|
+
colour=vec3f((1.0-0.85*outline)*(1.0-max(first*0.45,second*0.6)));
|
|
49
|
+
} else {
|
|
50
|
+
colour=smoothColour(p,r);
|
|
51
|
+
if(paint.flags.y==1u) {
|
|
52
|
+
|
|
53
|
+
let grain=f32((u32(local.x)*13u+u32(local.y)*7u)%17u)/16.0;
|
|
54
|
+
let wash=floor(colour*7.0+0.5)/7.0;
|
|
55
|
+
colour=mix(vec3f(1),wash,0.85)*(1.0-0.2*edge(p,1))*(0.96+0.04*grain);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return vec4f(mix(centre.rgb,clamp(colour,vec3f(0),vec3f(1)),paint.blend.x),centre.a);
|
|
59
|
+
}`, f = class c {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
60
|
+
constructor(e = {}) {
|
|
61
|
+
this.options = e, this.name = "painterly", this.stacked = !1, this.disposed = !1, this.key = "", this.bytes = /* @__PURE__ */ new ArrayBuffer(48), this.rect = new Int32Array(this.bytes, 0, 4), this.flags = new Uint32Array(this.bytes, 16, 4), this.blend = new Float32Array(this.bytes, 32, 4), this.views = new a(() => ({ uniform: this.engine.gpu.createBuffer({
|
|
62
|
+
size: 48,
|
|
63
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
64
|
+
}) }), (e) => e.uniform.destroy()), this.validate();
|
|
65
|
+
}
|
|
66
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
67
|
+
let r = new c(n);
|
|
68
|
+
if (!l(e).gpu) throw t("unsupported", "Painterly requires WebGPU; WebGL2 is unsupported");
|
|
69
|
+
return r.stack = u.join(e, r), r;
|
|
70
|
+
}
|
|
71
|
+
get enabled() {
|
|
72
|
+
return this.options.enabled ?? !0;
|
|
73
|
+
}
|
|
74
|
+
set enabled(t) {
|
|
75
|
+
if (typeof t != "boolean") throw e("Invalid Painterly enabled; pass true or false", "arguments");
|
|
76
|
+
this.options.enabled = t;
|
|
77
|
+
}
|
|
78
|
+
joinStack() {
|
|
79
|
+
if (this.alive(), this.stacked || this.engine) throw t("attached", "Painterly is already attached; create a new effect", "state");
|
|
80
|
+
return this.stacked = !0, {
|
|
81
|
+
kind: "ldr",
|
|
82
|
+
render: (e, t, n) => this.draw(e, t, n)
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
setup(e) {
|
|
86
|
+
if (this.alive(), this.engine) throw t("attached", "Painterly is already attached; create a new effect", "state");
|
|
87
|
+
if (!e.gpu) throw t("unsupported", "Painterly requires WebGPU; WebGL2 is unsupported");
|
|
88
|
+
if (!this.stacked) throw t("state", "Use Painterly.attach(engine) or add a new Painterly to a PostStack");
|
|
89
|
+
this.validate(), this.engine = e;
|
|
90
|
+
}
|
|
91
|
+
onPreUpdate(e) {
|
|
92
|
+
e > 0 && this.views.prune();
|
|
93
|
+
}
|
|
94
|
+
onDeviceRestored(e) {
|
|
95
|
+
this.alive(), this.release(), this.engine = void 0, this.setup(e);
|
|
96
|
+
}
|
|
97
|
+
dispose() {
|
|
98
|
+
if (this.disposed) return;
|
|
99
|
+
this.disposed = !0;
|
|
100
|
+
let e = this.engine;
|
|
101
|
+
this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name), this.release(), this.stack?.removeEffect(this), this.stack = void 0;
|
|
102
|
+
}
|
|
103
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
104
|
+
this.dispose();
|
|
105
|
+
}
|
|
106
|
+
alive() {
|
|
107
|
+
if (this.disposed) throw t("disposed", "Painterly is disposed; create a new effect");
|
|
108
|
+
}
|
|
109
|
+
validate() {
|
|
110
|
+
let t = this.options;
|
|
111
|
+
if (!t || typeof t != "object" || Array.isArray(t)) throw e("Invalid Painterly options; pass an options object", "arguments");
|
|
112
|
+
if (t.mode !== void 0 && ![
|
|
113
|
+
"kuwahara",
|
|
114
|
+
"watercolour",
|
|
115
|
+
"sketch"
|
|
116
|
+
].includes(t.mode)) throw e("Invalid Painterly mode; pass kuwahara, watercolour or sketch", "arguments");
|
|
117
|
+
if (t.radius !== void 0 && (!Number.isInteger(t.radius) || t.radius < 1 || t.radius > 8)) throw e("Invalid Painterly radius; pass an integer in 1..8", "arguments");
|
|
118
|
+
if (t.strength !== void 0 && (typeof t.strength != "number" || !Number.isFinite(t.strength) || t.strength < 0 || t.strength > 1)) throw e("Invalid Painterly strength; pass a finite number in 0..1", "arguments");
|
|
119
|
+
if (t.enabled !== void 0 && typeof t.enabled != "boolean") throw e("Invalid Painterly enabled; pass true or false", "arguments");
|
|
120
|
+
}
|
|
121
|
+
draw(e, a, c) {
|
|
122
|
+
if (this.alive(), this.validate(), !this.engine) throw t("state", "Attach Painterly to a live PostStack before drawing");
|
|
123
|
+
let l = c.format + ":" + (c.multisample?.count ?? 1);
|
|
124
|
+
l !== this.key && (this.pipeline = s(e.device, d, c.format, { multisample: c.multisample }), this.key = l);
|
|
125
|
+
let u = this.views.get(e);
|
|
126
|
+
this.rect.set(n(e) ?? [
|
|
127
|
+
0,
|
|
128
|
+
0,
|
|
129
|
+
e.width,
|
|
130
|
+
e.height
|
|
131
|
+
]), this.flags[0] = this.options.radius ?? 3, this.flags[1] = this.options.mode === "sketch" ? 2 : +(this.options.mode === "watercolour"), this.flags[2] = +this.enabled, this.blend[0] = this.options.strength ?? 1, e.device.queue.writeBuffer(u.uniform, 0, this.bytes), (u.input !== a || u.pipeline !== this.pipeline) && (u.group = o(e.device, this.pipeline, [{ buffer: u.uniform }, a]), u.input = a, u.pipeline = this.pipeline);
|
|
132
|
+
let f = e.encoder.beginRenderPass({ colorAttachments: [{
|
|
133
|
+
view: c.view,
|
|
134
|
+
loadOp: i(e, "clear"),
|
|
135
|
+
storeOp: "store"
|
|
136
|
+
}] });
|
|
137
|
+
try {
|
|
138
|
+
f.setPipeline(this.pipeline), f.setBindGroup(0, u.group), r(f, e), f.draw(3);
|
|
139
|
+
} finally {
|
|
140
|
+
f.end();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
release() {
|
|
144
|
+
this.views.clear(), this.pipeline = void 0, this.key = "";
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
//#endregion
|
|
148
|
+
export { f as Painterly };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import type { Vec3 } from '@margoengine/core/extension';
|
|
3
|
+
import { VFX, type VFXObstacle, type VFXOptions, type VFXTexture } from '@margoengine/vfx/base';
|
|
4
|
+
/** A sphere or box the leaves flow around and bounce off: `@margoengine/vfx`'s `VFXObstacle`. */
|
|
5
|
+
export type WindObstacle = VFXObstacle;
|
|
6
|
+
/** A leaf sprite (RGBA8 sRGB, up to 4096 px per side, optionally an atlas): `@margoengine/vfx`'s `VFXTexture`. */
|
|
7
|
+
export type ParticleTexture = VFXTexture;
|
|
8
|
+
/** `GPUParticles` settings, fixed at construction; invalid values throw RangeError. */
|
|
9
|
+
export interface ParticleOptions {
|
|
10
|
+
/** Particles kept alive in the 6 m cube (the VFX capacity). Default 5000. */
|
|
11
|
+
count?: number;
|
|
12
|
+
/** Acceleration in metres per second squared, added to `wind`. Default [0, -0.5, 0]. */
|
|
13
|
+
gravity?: Vec3;
|
|
14
|
+
/** Steady drift acceleration in metres per second squared. Default [0.4, 0, 0]. */
|
|
15
|
+
wind?: Vec3;
|
|
16
|
+
/** Leaf radius in metres. Default 0.035. */
|
|
17
|
+
size?: number;
|
|
18
|
+
/** Curl-noise turbulence strength, 0-10. Default 0. */
|
|
19
|
+
curl?: number;
|
|
20
|
+
/** Up to 64 obstacles. */
|
|
21
|
+
obstacles?: readonly WindObstacle[];
|
|
22
|
+
/** Draw back to front. Default false. */
|
|
23
|
+
sort?: boolean;
|
|
24
|
+
/** Sprite instead of the soft disc. */
|
|
25
|
+
texture?: ParticleTexture;
|
|
26
|
+
}
|
|
27
|
+
/** A drifting leaf/weather field: a `@margoengine/vfx` preset that keeps `count` particles wrapping inside a 6 m cube. */
|
|
28
|
+
export declare class GPUParticles extends VFX {
|
|
29
|
+
readonly name = "gpu-particles";
|
|
30
|
+
/**
|
|
31
|
+
* Same as `engine.use(new GPUParticles(options))`; returns the GPUParticles (VFX.attach would add a plain VFX). Pass
|
|
32
|
+
* `ParticleOptions`: the type also allows `VFXOptions` only because `VFX.attach` does, and its `wind` (`[x, z]` m/s or a
|
|
33
|
+
* field) is not this class's `wind` (a 3-value drift acceleration), so it throws `RangeError`.
|
|
34
|
+
*/
|
|
35
|
+
static attach(engine: EngineKernel, options?: ParticleOptions | VFXOptions): GPUParticles;
|
|
36
|
+
constructor(options?: ParticleOptions);
|
|
37
|
+
get count(): number;
|
|
38
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { VFX as e } from "@margoengine/vfx/base";
|
|
5
|
+
import { obstacles as t } from "@margoengine/vfx/obstacles";
|
|
6
|
+
import { wrap as n } from "@margoengine/vfx/wrap";
|
|
7
|
+
import { sort as r } from "@margoengine/vfx/sort";
|
|
8
|
+
import { textureFeature as i } from "@margoengine/vfx/texture";
|
|
9
|
+
//#region src/particles.ts
|
|
10
|
+
var a = class a extends e {
|
|
11
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
12
|
+
let n = new a(t);
|
|
13
|
+
return e.use(n), n;
|
|
14
|
+
}
|
|
15
|
+
constructor(e = {}) {
|
|
16
|
+
let { count: a = 5e3, gravity: o = [
|
|
17
|
+
0,
|
|
18
|
+
-.5,
|
|
19
|
+
0
|
|
20
|
+
], wind: s = [
|
|
21
|
+
.4,
|
|
22
|
+
0,
|
|
23
|
+
0
|
|
24
|
+
], size: c = .035, curl: l = 0 } = e;
|
|
25
|
+
if (!Number.isFinite(c) || !(l >= 0 && l <= 10) || o.length !== 3 || s.length !== 3) throw RangeError("Invalid particle options; use a finite size, curl 0..10 and 3-value gravity and wind");
|
|
26
|
+
super({
|
|
27
|
+
capacity: a,
|
|
28
|
+
initialCount: a,
|
|
29
|
+
rate: 0,
|
|
30
|
+
position: [
|
|
31
|
+
0,
|
|
32
|
+
0,
|
|
33
|
+
0
|
|
34
|
+
],
|
|
35
|
+
emissionExtent: [
|
|
36
|
+
3,
|
|
37
|
+
3,
|
|
38
|
+
3
|
|
39
|
+
],
|
|
40
|
+
wrap: !0,
|
|
41
|
+
velocity: [
|
|
42
|
+
0,
|
|
43
|
+
0,
|
|
44
|
+
0
|
|
45
|
+
],
|
|
46
|
+
spread: 0,
|
|
47
|
+
gravity: [
|
|
48
|
+
o[0] + s[0],
|
|
49
|
+
o[1] + s[1],
|
|
50
|
+
o[2] + s[2]
|
|
51
|
+
],
|
|
52
|
+
turbulence: l,
|
|
53
|
+
size: c * 2,
|
|
54
|
+
lifetime: [1e6, 1e6],
|
|
55
|
+
ground: -1e30,
|
|
56
|
+
bounce: .5,
|
|
57
|
+
angularVelocity: 1,
|
|
58
|
+
color: [
|
|
59
|
+
.93,
|
|
60
|
+
.53,
|
|
61
|
+
.17,
|
|
62
|
+
.85
|
|
63
|
+
],
|
|
64
|
+
obstacles: e.obstacles,
|
|
65
|
+
sort: e.sort,
|
|
66
|
+
texture: e.texture
|
|
67
|
+
}, {
|
|
68
|
+
obstacles: t,
|
|
69
|
+
wrap: n,
|
|
70
|
+
sort: r,
|
|
71
|
+
texture: i
|
|
72
|
+
}), this.name = "gpu-particles";
|
|
73
|
+
}
|
|
74
|
+
get count() {
|
|
75
|
+
return this.capacity;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
//#endregion
|
|
79
|
+
export { a as GPUParticles };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region ../shared/pass.js
|
|
2
|
+
var e = () => ({
|
|
3
|
+
colorAttachments: [{
|
|
4
|
+
view: void 0,
|
|
5
|
+
loadOp: "load",
|
|
6
|
+
storeOp: "store"
|
|
7
|
+
}],
|
|
8
|
+
depthStencilAttachment: {
|
|
9
|
+
view: void 0,
|
|
10
|
+
depthReadOnly: !0,
|
|
11
|
+
stencilReadOnly: !0
|
|
12
|
+
}
|
|
13
|
+
}), t = () => ({
|
|
14
|
+
colorAttachments: [{
|
|
15
|
+
view: void 0,
|
|
16
|
+
loadOp: "load",
|
|
17
|
+
storeOp: "store"
|
|
18
|
+
}],
|
|
19
|
+
depthStencilAttachment: {
|
|
20
|
+
view: void 0,
|
|
21
|
+
depthLoadOp: "load",
|
|
22
|
+
depthStoreOp: "store",
|
|
23
|
+
stencilReadOnly: !0
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
//#endregion
|
|
27
|
+
export { e as n, t };
|