@margoengine/toon 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 +93 -2
- package/dist/errors-XvcwUnU4.js +7 -0
- package/dist/frame-resources-CRA1ZB6K.js +69 -0
- package/dist/hatch-models.d.ts +30 -0
- package/dist/hatch-models.js +171 -0
- package/dist/hatch-style-DlyIqk6B.js +121 -0
- package/dist/hatch-style.d.ts +39 -0
- package/dist/hatch.d.ts +28 -0
- package/dist/hatch.js +56 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +1 -0
- package/dist/models-CyFG_IC3.js +312 -0
- package/dist/models.d.ts +126 -0
- package/dist/models.js +3 -0
- package/dist/outline.d.ts +86 -0
- package/dist/outline.js +228 -0
- package/dist/pixel-hull-BYQALJ3e.js +4 -0
- package/dist/pixel-hull.d.ts +1 -0
- package/dist/rings-BmbstqyP.js +52 -0
- package/dist/rings.d.ts +6 -0
- package/dist/screen-outline.d.ts +25 -0
- package/dist/screen-outline.js +52 -0
- package/dist/shared/model.d.ts +39 -0
- package/dist/src-npYQWEC-.js +96 -0
- package/package.json +75 -4
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
//#region src/hatch-style.ts
|
|
2
|
+
var e = (e, t = 0) => `struct Hatch {shape:vec4f,angle:vec4f,ink:vec4f}
|
|
3
|
+
@group(${t}) @binding(${e}) var<uniform> hatch:Hatch;
|
|
4
|
+
fn hatchInk(pixel:vec2f,light:f32)->f32{
|
|
5
|
+
let s=hatch.shape.y;
|
|
6
|
+
if(hatch.shape.x>0.5){
|
|
7
|
+
let a=hatch.angle.x;let r=mat2x2f(cos(a),sin(a),-sin(a),cos(a))*pixel/s;let d=length(fract(r)-0.5)*s;
|
|
8
|
+
let c=clamp(1.0-light,0.0,1.0);
|
|
9
|
+
if(c>=1.0){return 1.0;}
|
|
10
|
+
var cell=sqrt(c/3.14159265);
|
|
11
|
+
if(c>0.78539816){
|
|
12
|
+
var lo=0.5;var hi=0.70710678;
|
|
13
|
+
for(var k=0;k<20;k++){let m=0.5*(lo+hi);let q=m*m;if(3.14159265*q-4.0*(q*acos(0.5/m)-0.5*sqrt(q-0.25))<c){lo=m;}else{hi=m;}}
|
|
14
|
+
cell=0.5*(lo+hi);
|
|
15
|
+
}
|
|
16
|
+
let radius=s*cell;
|
|
17
|
+
return clamp(radius-d+0.5,0.0,1.0);
|
|
18
|
+
}
|
|
19
|
+
let n=hatch.shape.w;var ink=0.0;
|
|
20
|
+
for(var k=0.0;k<n;k+=1.0){
|
|
21
|
+
if(light>=(n-k)/(n+1.0)){break;}
|
|
22
|
+
let a=hatch.angle.x+k*3.14159265/n;let t=dot(pixel,vec2f(cos(a),sin(a)))/s;let d=abs(fract(t)-0.5)*s;
|
|
23
|
+
ink=max(ink,clamp(hatch.shape.z*0.5-d+0.5,0.0,1.0));
|
|
24
|
+
}
|
|
25
|
+
return ink;
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
function t(e) {
|
|
29
|
+
if (e.style !== "hatch" && e.style !== "halftone") throw RangeError("Hatching style must be 'hatch' or 'halftone'");
|
|
30
|
+
if (!Number.isFinite(e.spacing) || e.spacing < 2 || e.spacing > 64 || !Number.isFinite(e.width) || e.width < .5 || e.width > e.spacing || !Number.isSafeInteger(e.layers) || e.layers < 1 || e.layers > 4 || !Number.isFinite(e.angle) || !Array.isArray(e.ink) || e.ink.length !== 3 || e.ink.some((e) => typeof e != "number" || !Number.isFinite(e) || e < 0 || e > 1)) throw RangeError("Invalid Hatching options; use spacing 2-64 px, width 0.5 px to spacing, integer layers 1-4, a finite angle and ink RGB 0..1");
|
|
31
|
+
}
|
|
32
|
+
var n = class {
|
|
33
|
+
constructor({ style: e = "hatch", spacing: n = 8, width: r = 1.5, layers: i = 3, angle: a = 45, ink: o = [
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
0
|
|
37
|
+
] } = {}) {
|
|
38
|
+
this.values = /* @__PURE__ */ new Float32Array(12), this.options = {
|
|
39
|
+
style: e,
|
|
40
|
+
spacing: n,
|
|
41
|
+
width: r,
|
|
42
|
+
layers: i,
|
|
43
|
+
angle: a,
|
|
44
|
+
ink: [
|
|
45
|
+
o[0],
|
|
46
|
+
o[1],
|
|
47
|
+
o[2]
|
|
48
|
+
]
|
|
49
|
+
}, t({
|
|
50
|
+
...this.options,
|
|
51
|
+
ink: o
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
set(e) {
|
|
55
|
+
let n = {
|
|
56
|
+
...this.options,
|
|
57
|
+
...e
|
|
58
|
+
};
|
|
59
|
+
t(n), this.options = {
|
|
60
|
+
...n,
|
|
61
|
+
ink: [
|
|
62
|
+
n.ink[0],
|
|
63
|
+
n.ink[1],
|
|
64
|
+
n.ink[2]
|
|
65
|
+
]
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
get style() {
|
|
69
|
+
return this.options.style;
|
|
70
|
+
}
|
|
71
|
+
set style(e) {
|
|
72
|
+
this.set({ style: e });
|
|
73
|
+
}
|
|
74
|
+
get spacing() {
|
|
75
|
+
return this.options.spacing;
|
|
76
|
+
}
|
|
77
|
+
set spacing(e) {
|
|
78
|
+
this.set({ spacing: e });
|
|
79
|
+
}
|
|
80
|
+
get width() {
|
|
81
|
+
return this.options.width;
|
|
82
|
+
}
|
|
83
|
+
set width(e) {
|
|
84
|
+
this.set({ width: e });
|
|
85
|
+
}
|
|
86
|
+
get layers() {
|
|
87
|
+
return this.options.layers;
|
|
88
|
+
}
|
|
89
|
+
set layers(e) {
|
|
90
|
+
this.set({ layers: e });
|
|
91
|
+
}
|
|
92
|
+
get angle() {
|
|
93
|
+
return this.options.angle;
|
|
94
|
+
}
|
|
95
|
+
set angle(e) {
|
|
96
|
+
this.set({ angle: e });
|
|
97
|
+
}
|
|
98
|
+
get ink() {
|
|
99
|
+
return this.options.ink;
|
|
100
|
+
}
|
|
101
|
+
set ink(e) {
|
|
102
|
+
this.set({ ink: e });
|
|
103
|
+
}
|
|
104
|
+
hatchValues() {
|
|
105
|
+
let e = this.options;
|
|
106
|
+
return this.values.set([
|
|
107
|
+
+(e.style === "halftone"),
|
|
108
|
+
e.spacing,
|
|
109
|
+
e.width,
|
|
110
|
+
e.layers,
|
|
111
|
+
e.angle * Math.PI / 180,
|
|
112
|
+
0,
|
|
113
|
+
0,
|
|
114
|
+
0,
|
|
115
|
+
...e.ink,
|
|
116
|
+
0
|
|
117
|
+
]), this.values;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
export { e as n, n as t };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Shared core/model hatching options; each is live as the property of the same name. */
|
|
2
|
+
export interface HatchingOptions {
|
|
3
|
+
/** `'hatch'`: pen strokes, a layer more for each step darker; `'halftone'`: one dot a cell. Default `'hatch'`. */
|
|
4
|
+
style?: 'hatch' | 'halftone';
|
|
5
|
+
/** Line or dot spacing in screen pixels, 2-64. Default 8. */
|
|
6
|
+
spacing?: number;
|
|
7
|
+
/** Stroke width in screen pixels for `'hatch'`, 0.5 up to `spacing`. Default 1.5. */
|
|
8
|
+
width?: number;
|
|
9
|
+
/** Stroke layers for `'hatch'`, an integer 1-4, each turned 180 / layers degrees from the last. Default 3. */
|
|
10
|
+
layers?: number;
|
|
11
|
+
/** Angle of the first strokes (or of the dot grid) in degrees. Default 45. */
|
|
12
|
+
angle?: number;
|
|
13
|
+
/** Ink colour, linear RGB 0..1, drawn unlit over the surface. Default [0, 0, 0]. */
|
|
14
|
+
ink?: readonly [number, number, number];
|
|
15
|
+
}
|
|
16
|
+
/** WGSL pattern with a selectable binding group. Shape, angle and ink occupy three vec4 uniform rows. */
|
|
17
|
+
export declare const hatchWGSL: (binding: number, group?: number) => string;
|
|
18
|
+
/** Shared validated, live pattern controls for core and model renderers. */
|
|
19
|
+
export declare class HatchStyle {
|
|
20
|
+
private options;
|
|
21
|
+
private readonly values;
|
|
22
|
+
constructor({ style, spacing, width, layers, angle, ink, }?: HatchingOptions);
|
|
23
|
+
/** Sets any of the options; the others keep their values. @throws RangeError as the constructor. */
|
|
24
|
+
set(options: HatchingOptions): void;
|
|
25
|
+
get style(): 'hatch' | 'halftone';
|
|
26
|
+
set style(style: 'hatch' | 'halftone');
|
|
27
|
+
get spacing(): number;
|
|
28
|
+
set spacing(spacing: number);
|
|
29
|
+
get width(): number;
|
|
30
|
+
set width(width: number);
|
|
31
|
+
get layers(): number;
|
|
32
|
+
set layers(layers: number);
|
|
33
|
+
get angle(): number;
|
|
34
|
+
set angle(angle: number);
|
|
35
|
+
get ink(): readonly [number, number, number];
|
|
36
|
+
set ink(ink: readonly [number, number, number]);
|
|
37
|
+
/** Current validated pattern parameters, packed for either renderer. */
|
|
38
|
+
protected hatchValues(): Float32Array;
|
|
39
|
+
}
|
package/dist/hatch.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
/** Group 0 binding of the hatching parameters on core meshes (docs/shader-contracts.md). */
|
|
5
|
+
export declare const HATCH_BINDING = 146;
|
|
6
|
+
import { HatchStyle, type HatchingOptions } from './hatch-style.js';
|
|
7
|
+
export type { HatchingOptions } from './hatch-style.js';
|
|
8
|
+
/**
|
|
9
|
+
* Pen hatching or halftone dots in place of flat bands on core meshes: the diffuse light level decides the inked share
|
|
10
|
+
* of each pixel, in a screen-space pattern. `Hatching.attach(engine, options)`; WebGPU only. With `Toon` (outlines and
|
|
11
|
+
* rim) attached too, the hatching replaces its bands.
|
|
12
|
+
* @throws RangeError when an option is out of range; MargoError `'unsupported'` on a WebGL engine, `'disposed'` after
|
|
13
|
+
* `dispose()`.
|
|
14
|
+
*/
|
|
15
|
+
export declare class Hatching extends HatchStyle implements EnginePlugin {
|
|
16
|
+
readonly name = "toon:hatch";
|
|
17
|
+
private buffer?;
|
|
18
|
+
private engine?;
|
|
19
|
+
private readonly shaders;
|
|
20
|
+
private disposed;
|
|
21
|
+
static attach(engine: EngineKernel, options?: HatchingOptions): Hatching;
|
|
22
|
+
setup(engine: Engine): void;
|
|
23
|
+
registerPasses(graph: FrameGraph): void;
|
|
24
|
+
onDeviceRestored(engine: Engine): void;
|
|
25
|
+
private release;
|
|
26
|
+
dispose(): void;
|
|
27
|
+
[Symbol.dispose]: () => void;
|
|
28
|
+
}
|
package/dist/hatch.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const n=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { n as e, t } from "./hatch-style-DlyIqk6B.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
|
+
import { MargoError as r } from "@margoengine/math/errors";
|
|
7
|
+
//#region src/hatch.ts
|
|
8
|
+
var i = 146, a = class i extends t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
9
|
+
constructor(...e) {
|
|
10
|
+
super(...e), this.name = "toon:hatch", this.shaders = /* @__PURE__ */ new Map(), this.disposed = !1;
|
|
11
|
+
}
|
|
12
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
13
|
+
let r = new i(t);
|
|
14
|
+
return n(e).use(r), r;
|
|
15
|
+
}
|
|
16
|
+
setup(e) {
|
|
17
|
+
if (this.disposed || !e.gpu) throw new r(this.disposed ? "disposed" : "unsupported", "Hatching requires WebGPU; attach a new Hatching to a WebGPU engine; see https://margoengine.com/errors#webgpu");
|
|
18
|
+
this.engine = e, this.buffer = e.gpu.createBuffer({
|
|
19
|
+
size: 48,
|
|
20
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
registerPasses(t) {
|
|
24
|
+
t.add({
|
|
25
|
+
name: "toon:hatch",
|
|
26
|
+
order: -2,
|
|
27
|
+
execute: (t) => {
|
|
28
|
+
if (t.backend !== "webgpu" || !this.buffer) return;
|
|
29
|
+
let n = this.shaders.get(t.shader);
|
|
30
|
+
if (n === void 0) {
|
|
31
|
+
let i = t.shader.replace(/let direct=[^;]+;/, "let light=max(dot(n,l),0.0);let ink=hatchInk(i.clip.xy,light);let direct=i.color;").replace(/let linear=([^;]+);/, "let linear=mix($1,hatch.ink.rgb,ink);");
|
|
32
|
+
if (i === t.shader || !i.includes("hatchInk(")) throw new r("unsupported", "Hatching could not patch this mesh shader; detach the effect that rewrote its lighting");
|
|
33
|
+
n = e(146) + i, this.shaders.size >= 32 && this.shaders.clear(), this.shaders.set(t.shader, n);
|
|
34
|
+
}
|
|
35
|
+
t.shader = n, t.device.queue.writeBuffer(this.buffer, 0, this.hatchValues()), t.bindings.push({
|
|
36
|
+
binding: 146,
|
|
37
|
+
resource: { buffer: this.buffer }
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
onDeviceRestored(e) {
|
|
43
|
+
this.release(), this.setup(e);
|
|
44
|
+
}
|
|
45
|
+
release() {
|
|
46
|
+
this.buffer?.destroy(), this.buffer = void 0, this.shaders.clear();
|
|
47
|
+
}
|
|
48
|
+
dispose() {
|
|
49
|
+
this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.release(), this.engine = void 0);
|
|
50
|
+
}
|
|
51
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
52
|
+
this.dispose();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { i as HATCH_BINDING, a as Hatching };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph, type PassContext } from '@margoengine/core/extension';
|
|
4
|
+
import { type ToonOptions } from '@margoengine/creative/toon';
|
|
5
|
+
/** `ToonShading`'s ramp options plus the inverted-hull outline. */
|
|
6
|
+
export interface ToonPipelineOptions extends ToonOptions {
|
|
7
|
+
/** Outline thickness in world units, 0-10 (0 hides it). Default 0.025. */
|
|
8
|
+
outline?: number;
|
|
9
|
+
/** Outline colour, linear RGB 0..1. Default [0.015, 0.015, 0.015]. */
|
|
10
|
+
color?: readonly [number, number, number];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Toon look for core meshes: `ToonShading`'s stepped diffuse ramp plus a front-culled inverted-hull pass for
|
|
14
|
+
* silhouette outlines (back faces pushed out: Raskar & Cohen, "Image Precision Silhouette Edges", I3D 1999; the
|
|
15
|
+
* default thickness and colour are Margo's). `Toon.attach(engine, options)`; WebGPU only.
|
|
16
|
+
* @throws RangeError when `outline` or `color` is out of range (and ToonShading's own option errors).
|
|
17
|
+
* @throws MargoError `'unsupported'` on a WebGL engine, `'disposed'` after `dispose()`.
|
|
18
|
+
*/
|
|
19
|
+
export declare class Toon implements EnginePlugin {
|
|
20
|
+
readonly name = "toon";
|
|
21
|
+
private readonly shading;
|
|
22
|
+
private engine?;
|
|
23
|
+
private buffer?;
|
|
24
|
+
private pipeline?;
|
|
25
|
+
private format;
|
|
26
|
+
protected readonly values: Float32Array;
|
|
27
|
+
/** Width limit for optional outline projections. */
|
|
28
|
+
protected get maxOutline(): number;
|
|
29
|
+
/** Parameters for the selected outline projection. */
|
|
30
|
+
protected outlineData(_context?: PassContext): Float32Array;
|
|
31
|
+
/** Shader transform for optional outline projections. */
|
|
32
|
+
protected outlineShader(code: string): string;
|
|
33
|
+
private disposed;
|
|
34
|
+
constructor({ outline, color, ...options }?: ToonPipelineOptions);
|
|
35
|
+
static attach(engine: EngineKernel, options?: ToonPipelineOptions): Toon;
|
|
36
|
+
setup(engine: Engine): void;
|
|
37
|
+
registerPasses(graph: FrameGraph): void;
|
|
38
|
+
private release;
|
|
39
|
+
onDeviceRestored(engine: Engine): void;
|
|
40
|
+
dispose(): void;
|
|
41
|
+
[Symbol.dispose]: () => void;
|
|
42
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as Toon } from "./src-npYQWEC-.js";
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const l=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { a as e, i as t, n, r, t as i } from "./rings-BmbstqyP.js";
|
|
5
|
+
import { i as a, n as o, r as s, t as c } from "./frame-resources-CRA1ZB6K.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
import { MargoError as u } from "@margoengine/math/errors";
|
|
8
|
+
[
|
|
9
|
+
5,
|
|
10
|
+
6,
|
|
11
|
+
7,
|
|
12
|
+
8,
|
|
13
|
+
9,
|
|
14
|
+
10
|
|
15
|
+
].map((e, t) => ({
|
|
16
|
+
shaderLocation: e,
|
|
17
|
+
offset: t * 16,
|
|
18
|
+
format: "float32x4"
|
|
19
|
+
}));
|
|
20
|
+
function d(e, t, n, r) {
|
|
21
|
+
let i = n * 24;
|
|
22
|
+
for (let n = 0; n < 4; n++) for (let a = 0; a < 4; a++) r[n * 4 + a] = e[a] * t[i + n * 4] + e[4 + a] * t[i + n * 4 + 1] + e[8 + a] * t[i + n * 4 + 2] + e[12 + a] * t[i + n * 4 + 3];
|
|
23
|
+
return r;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region ../shared/model.js
|
|
27
|
+
var f = (e) => !!e && typeof e == "object" && !!e.root && Array.isArray(e.primitives) && (!e.primitives.length || e.primitives[0].vertices instanceof Float32Array), p = "\nstruct Frame {camera:mat4x4f,flags:vec4f}\nstruct Draw {model:mat4x4f,style:vec4f}\nstruct Out {@builtin(position) clip:vec4f,@location(0) color:vec3f}\n@group(0) @binding(0) var<uniform> u:Frame;\n@group(0) @binding(1) var<storage,read> draws:array<Draw>;\n@vertex fn vertex(@location(0) position:vec3f,@location(1) own:vec3f,@location(2) tint:vec4f,@builtin(instance_index) slot:u32,@builtin(vertex_index) index:u32)->Out{\n let normal=NORMAL;let d=draws[slot];let m=d.model;let a=m[0].xyz;let b=m[1].xyz;let c=m[2].xyz;\n let paint=select(vec4f(1),tint,u.flags.x>0);\n let n=mat3x3f(cross(b,c),cross(c,a),cross(a,b))*normal*select(-1.0,1.0,dot(a,cross(b,c))>=0.0);\n let l=length(n);let p=m*vec4f(position,1)+vec4f(select(vec3f(0),n/l,l>0)*d.style.w*paint.a,0);\n return Out(u.camera*p,d.style.rgb*paint.rgb);\n}\n@fragment fn fragment(i:Out)->@location(0) vec4f{return vec4f(i.color,1);}", m = p.replace("NORMAL", "smoothed(index,own)") + "\n@group(1) @binding(0) var<storage,read> vertices:array<f32>;\n@group(1) @binding(1) var<storage,read> ring:array<u32>;\nfn smoothed(index:u32,own:vec3f)->vec3f{\n var sum=own;var k=ring[index];var steps=0u;\n loop{if(k==index||steps>=64u){break;}let o=k*20u;sum+=vec3f(vertices[o+4u],vertices[o+5u],vertices[o+6u]);k=ring[k];steps++;}\n return select(own,sum,dot(sum,sum)>1e-8);\n}", h = p.replace("NORMAL", "own");
|
|
28
|
+
function g(e, t, n = 10) {
|
|
29
|
+
if (typeof e != "number" || !Number.isFinite(e) || e < 0 || e > n || !Array.isArray(t) || t.length !== 3 || t.some((e) => typeof e != "number" || !Number.isFinite(e) || e < 0 || e > 1)) throw RangeError(`ModelOutlines outline must be between 0 and ${n} and color three channels between 0 and 1; pass finite values in range.`);
|
|
30
|
+
}
|
|
31
|
+
function _(e, t, n) {
|
|
32
|
+
for (let r = t; r >= 0; r = e.parents?.[r] ?? -1) if (n.has(r)) return !0;
|
|
33
|
+
return !1;
|
|
34
|
+
}
|
|
35
|
+
var v = (e) => e[0] * (e[5] * e[10] - e[6] * e[9]) - e[4] * (e[1] * e[10] - e[2] * e[9]) + e[8] * (e[1] * e[6] - e[2] * e[5]), y = class p {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
36
|
+
get maxOutline() {
|
|
37
|
+
return 10;
|
|
38
|
+
}
|
|
39
|
+
outlineShader(e) {
|
|
40
|
+
return e;
|
|
41
|
+
}
|
|
42
|
+
outlineFrame(e, t) {}
|
|
43
|
+
constructor({ outline: e = .025, color: t = [
|
|
44
|
+
.015,
|
|
45
|
+
.015,
|
|
46
|
+
.015
|
|
47
|
+
], smooth: n = !0, materials: r = {}, vertexColors: i = !1, models: a, nodes: o } = {}) {
|
|
48
|
+
if (this.name = "toon:models", this.styles = {}, this.smoothing = /* @__PURE__ */ new Map(), this.frame = 0, this.rowFrame = -1, this.rowEnd = 0, this.values = /* @__PURE__ */ new Float32Array(20), this.rows = /* @__PURE__ */ new Float32Array(320), this.runs = [], this.scratch = /* @__PURE__ */ new Float32Array(16), this.key = "", this.colorAttachment = {
|
|
49
|
+
view: void 0,
|
|
50
|
+
loadOp: "load",
|
|
51
|
+
storeOp: "store"
|
|
52
|
+
}, this.depthAttachment = { view: void 0 }, this.disposed = !1, g(e, t, this.maxOutline), a !== void 0 && (!Array.isArray(a) || !a.every(f))) throw TypeError("ModelOutlines models must be loaded models; pass the PBRModels loadGLTF returned");
|
|
53
|
+
this.width = e, this.rgb = [
|
|
54
|
+
t[0],
|
|
55
|
+
t[1],
|
|
56
|
+
t[2]
|
|
57
|
+
], this.only = a && [...a], this.smooth = !!n, this.vertexColors = !!i, this.materials = r, this.nodes = o;
|
|
58
|
+
}
|
|
59
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
60
|
+
let n = new p(t);
|
|
61
|
+
return l(e).use(n), n;
|
|
62
|
+
}
|
|
63
|
+
get outline() {
|
|
64
|
+
return this.width;
|
|
65
|
+
}
|
|
66
|
+
set outline(e) {
|
|
67
|
+
g(e, this.rgb, this.maxOutline), this.width = e;
|
|
68
|
+
}
|
|
69
|
+
get color() {
|
|
70
|
+
return this.rgb;
|
|
71
|
+
}
|
|
72
|
+
set color(e) {
|
|
73
|
+
g(this.width, e, this.maxOutline), this.rgb = [
|
|
74
|
+
e[0],
|
|
75
|
+
e[1],
|
|
76
|
+
e[2]
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
get nodes() {
|
|
80
|
+
return this.chosen;
|
|
81
|
+
}
|
|
82
|
+
set nodes(e) {
|
|
83
|
+
if (e !== void 0 && (!Array.isArray(e) || !e.every((e) => Number.isSafeInteger(e?.id) && e.id >= 0))) throw TypeError("ModelOutlines nodes must be engine meshes; pass model.root or entries of model.nodes");
|
|
84
|
+
this.chosen = e && [...e], this.under = e && new Set(e.map((e) => e.id));
|
|
85
|
+
}
|
|
86
|
+
get materials() {
|
|
87
|
+
return this.styles;
|
|
88
|
+
}
|
|
89
|
+
set materials(e) {
|
|
90
|
+
if (!e || typeof e != "object") throw TypeError("ModelOutlines materials must be an object of styles by name");
|
|
91
|
+
let t = {};
|
|
92
|
+
for (let [n, r] of Object.entries(e)) {
|
|
93
|
+
if (!r || typeof r != "object") throw TypeError(`ModelOutlines material '${n}' needs a style object; pass {outline, color}`);
|
|
94
|
+
g(r.outline ?? 0, r.color ?? [
|
|
95
|
+
0,
|
|
96
|
+
0,
|
|
97
|
+
0
|
|
98
|
+
], this.maxOutline), t[n] = {
|
|
99
|
+
outline: r.outline,
|
|
100
|
+
color: r.color && [
|
|
101
|
+
r.color[0],
|
|
102
|
+
r.color[1],
|
|
103
|
+
r.color[2]
|
|
104
|
+
]
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
this.styles = t;
|
|
108
|
+
}
|
|
109
|
+
setup(e) {
|
|
110
|
+
if (this.disposed || !e.gpu) throw new u(this.disposed ? "disposed" : "unsupported", "ModelOutlines requires WebGPU; attach a new ModelOutlines to a WebGPU engine; see https://margoengine.com/errors#webgpu");
|
|
111
|
+
this.engine = e;
|
|
112
|
+
}
|
|
113
|
+
registerPasses(e) {
|
|
114
|
+
e.add({
|
|
115
|
+
name: "toon:models",
|
|
116
|
+
order: 1.25,
|
|
117
|
+
joins: !0,
|
|
118
|
+
execute: (e) => {
|
|
119
|
+
e.backend === "webgpu" && this.draw(e);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
models() {
|
|
124
|
+
if (this.only) return this.only;
|
|
125
|
+
let e = [];
|
|
126
|
+
for (let t of this.engine.plugins.values()) f(t) && e.push(t);
|
|
127
|
+
return e;
|
|
128
|
+
}
|
|
129
|
+
add(e, t, n, r) {
|
|
130
|
+
let i = this.runs, a = i.length ? i[i.length - 1].first + i[i.length - 1].count : 0, o = v(t) < 0;
|
|
131
|
+
if (a * 20 + 20 > this.rows.length) {
|
|
132
|
+
let e = new Float32Array(this.rows.length * 2);
|
|
133
|
+
e.set(this.rows), this.rows = e;
|
|
134
|
+
}
|
|
135
|
+
this.rows.set(t, a * 20), this.rows.set(n, a * 20 + 16), this.rows[a * 20 + 19] = r;
|
|
136
|
+
let s = i[i.length - 1];
|
|
137
|
+
s && s.primitive === e && s.mirrored === o ? s.count++ : i.push({
|
|
138
|
+
primitive: e,
|
|
139
|
+
first: a,
|
|
140
|
+
count: 1,
|
|
141
|
+
mirrored: o
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
collect(t) {
|
|
145
|
+
let n = this.engine;
|
|
146
|
+
this.runs.length = 0;
|
|
147
|
+
for (let i of this.models()) if (!i.disposed) for (let a of i.primitives) {
|
|
148
|
+
let i = a.topology;
|
|
149
|
+
if (!a.buffers || i || r(n.store, a.node.id) || !e(t, a.node) || this.under && !_(n.store, a.node.id, this.under)) continue;
|
|
150
|
+
let o = a.material.name, s = o !== void 0 && Object.hasOwn(this.styles, o) ? this.styles[o] : void 0, c = s?.outline ?? this.width, l = s?.color ?? this.rgb;
|
|
151
|
+
if (!(c > 0)) continue;
|
|
152
|
+
let u = a.node.matrix;
|
|
153
|
+
if (!a.instances) this.add(a, u, l, c);
|
|
154
|
+
else for (let e = 0; e < a.instances.count; e++) this.add(a, d(u, a.instances.data, e, this.scratch), l, c);
|
|
155
|
+
}
|
|
156
|
+
let i = this.runs[this.runs.length - 1];
|
|
157
|
+
return i ? i.first + i.count : 0;
|
|
158
|
+
}
|
|
159
|
+
prepare(e, t) {
|
|
160
|
+
let n = e.device;
|
|
161
|
+
if (this.layout || (this.layout = n.createBindGroupLayout({ entries: [{
|
|
162
|
+
binding: 0,
|
|
163
|
+
visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
|
|
164
|
+
buffer: { type: "uniform" }
|
|
165
|
+
}, {
|
|
166
|
+
binding: 1,
|
|
167
|
+
visibility: GPUShaderStage.VERTEX,
|
|
168
|
+
buffer: { type: "read-only-storage" }
|
|
169
|
+
}] }), this.ringLayout = n.createBindGroupLayout({ entries: [{
|
|
170
|
+
binding: 0,
|
|
171
|
+
visibility: GPUShaderStage.VERTEX,
|
|
172
|
+
buffer: { type: "read-only-storage" }
|
|
173
|
+
}, {
|
|
174
|
+
binding: 1,
|
|
175
|
+
visibility: GPUShaderStage.VERTEX,
|
|
176
|
+
buffer: { type: "read-only-storage" }
|
|
177
|
+
}] }), this.uniform = n.createBuffer({
|
|
178
|
+
size: 80,
|
|
179
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
180
|
+
})), !this.storage || this.storage.size < t * 80) {
|
|
181
|
+
o(n, this.storage), this.storage = n.createBuffer({
|
|
182
|
+
size: Math.max(t, 16) * 80,
|
|
183
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
184
|
+
});
|
|
185
|
+
let e = this.storage, r = this.layout;
|
|
186
|
+
this.groups = s(this.uniform, (t) => n.createBindGroup({
|
|
187
|
+
layout: r,
|
|
188
|
+
entries: [{
|
|
189
|
+
binding: 0,
|
|
190
|
+
resource: t
|
|
191
|
+
}, {
|
|
192
|
+
binding: 1,
|
|
193
|
+
resource: { buffer: e }
|
|
194
|
+
}]
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
let r = e.descriptor.depthStencil, i = e.descriptor.multisample?.count ?? 1, a = e.format + ":" + i + ":" + r.format + ":" + r.depthCompare;
|
|
198
|
+
if (this.key !== a) {
|
|
199
|
+
let t = n.createShaderModule({ code: this.outlineShader(h) }), i = n.createShaderModule({ code: this.outlineShader(m) }), o = n.createPipelineLayout({ bindGroupLayouts: [this.layout] }), s = n.createPipelineLayout({ bindGroupLayouts: [this.layout, this.ringLayout] }), c = (t, i, a) => n.createRenderPipeline({
|
|
200
|
+
layout: a,
|
|
201
|
+
vertex: {
|
|
202
|
+
module: i,
|
|
203
|
+
entryPoint: "vertex",
|
|
204
|
+
buffers: [{
|
|
205
|
+
arrayStride: 80,
|
|
206
|
+
attributes: [
|
|
207
|
+
{
|
|
208
|
+
shaderLocation: 0,
|
|
209
|
+
offset: 0,
|
|
210
|
+
format: "float32x3"
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
shaderLocation: 1,
|
|
214
|
+
offset: 16,
|
|
215
|
+
format: "float32x3"
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
shaderLocation: 2,
|
|
219
|
+
offset: 64,
|
|
220
|
+
format: "float32x4"
|
|
221
|
+
}
|
|
222
|
+
]
|
|
223
|
+
}]
|
|
224
|
+
},
|
|
225
|
+
fragment: {
|
|
226
|
+
module: i,
|
|
227
|
+
entryPoint: "fragment",
|
|
228
|
+
targets: [{ format: e.format }]
|
|
229
|
+
},
|
|
230
|
+
primitive: {
|
|
231
|
+
topology: "triangle-list",
|
|
232
|
+
cullMode: t
|
|
233
|
+
},
|
|
234
|
+
depthStencil: {
|
|
235
|
+
format: r.format,
|
|
236
|
+
depthCompare: r.depthCompare,
|
|
237
|
+
depthWriteEnabled: !1
|
|
238
|
+
},
|
|
239
|
+
multisample: e.descriptor.multisample
|
|
240
|
+
});
|
|
241
|
+
this.pipelines = [
|
|
242
|
+
c("front", t, o),
|
|
243
|
+
c("back", t, o),
|
|
244
|
+
c("front", i, s),
|
|
245
|
+
c("back", i, s)
|
|
246
|
+
], this.key = a;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
draw(e) {
|
|
250
|
+
let r = this.collect(e);
|
|
251
|
+
if (this.frame++, !r) return;
|
|
252
|
+
let i = e.device, s = this.values, l = c(i, e.view ?? e), u = l === this.rowFrame ? this.rowEnd : 0;
|
|
253
|
+
this.rowFrame = l, this.rowEnd = u + r, this.prepare(e, u + r), s.set(e.camera.matrix), s[16] = +this.vertexColors, this.outlineFrame(s, e);
|
|
254
|
+
let d = a(i, this.uniform, s, e.view ?? e);
|
|
255
|
+
i.queue.writeBuffer(this.storage, u * 80, this.rows, 0, r * 20);
|
|
256
|
+
let f = n(e, this.colorAttachment, this.depthAttachment);
|
|
257
|
+
t(f, e), f.setBindGroup(0, this.groups.get(d));
|
|
258
|
+
let p;
|
|
259
|
+
for (let e of this.runs) {
|
|
260
|
+
let t = e.primitive, n = this.smooth ? this.ringGroup(i, t) : void 0, r = this.pipelines[(n ? 2 : 0) + +e.mirrored];
|
|
261
|
+
r !== p && f.setPipeline(p = r), n && f.setBindGroup(1, n), f.setVertexBuffer(0, t.deformed ?? t.buffers.vertex, t.deformed && t.deformedOffset || 0), f.setIndexBuffer(t.buffers.index, "uint32"), f.drawIndexed(t.indices.length, e.count, 0, 0, u + e.first);
|
|
262
|
+
}
|
|
263
|
+
if (f.end(), this.frame % 120 == 0) for (let [e, t] of this.smoothing) t.seen < this.frame - 120 && (o(i, t.ring), this.smoothing.delete(e));
|
|
264
|
+
}
|
|
265
|
+
ringGroup(e, t) {
|
|
266
|
+
let n = t.deformed ?? t.buffers.vertex, r = t.deformed && t.deformedOffset || 0;
|
|
267
|
+
if (!(n.usage & GPUBufferUsage.STORAGE)) return;
|
|
268
|
+
let a = this.smoothing.get(t);
|
|
269
|
+
if (!a || a.vertices !== t.vertices) {
|
|
270
|
+
a && o(e, a.ring);
|
|
271
|
+
let n = i(t.vertices), r = e.createBuffer({
|
|
272
|
+
size: Math.max(4, n.byteLength),
|
|
273
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
274
|
+
});
|
|
275
|
+
e.queue.writeBuffer(r, 0, n), this.smoothing.set(t, a = {
|
|
276
|
+
ring: r,
|
|
277
|
+
vertices: t.vertices,
|
|
278
|
+
offset: 0,
|
|
279
|
+
seen: 0
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
return a.seen = this.frame, (a.source !== n || a.offset !== r || !a.group) && (a.group = e.createBindGroup({
|
|
283
|
+
layout: this.ringLayout,
|
|
284
|
+
entries: [{
|
|
285
|
+
binding: 0,
|
|
286
|
+
resource: {
|
|
287
|
+
buffer: n,
|
|
288
|
+
offset: r,
|
|
289
|
+
size: t.vertices.byteLength
|
|
290
|
+
}
|
|
291
|
+
}, {
|
|
292
|
+
binding: 1,
|
|
293
|
+
resource: { buffer: a.ring }
|
|
294
|
+
}]
|
|
295
|
+
}), a.source = n, a.offset = r), a.group;
|
|
296
|
+
}
|
|
297
|
+
release() {
|
|
298
|
+
if (this.engine?.gpu && (o(this.engine.gpu, this.uniform), o(this.engine.gpu, this.storage)), this.engine?.gpu) for (let e of this.smoothing.values()) o(this.engine.gpu, e.ring);
|
|
299
|
+
this.smoothing.clear(), this.uniform = this.storage = this.layout = this.ringLayout = this.groups = this.pipelines = void 0, this.key = "";
|
|
300
|
+
}
|
|
301
|
+
onDeviceRestored(e) {
|
|
302
|
+
this.release(), this.setup(e);
|
|
303
|
+
}
|
|
304
|
+
dispose() {
|
|
305
|
+
this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.release(), this.engine = void 0);
|
|
306
|
+
}
|
|
307
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
308
|
+
this.dispose();
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
//#endregion
|
|
312
|
+
export { y as t };
|