@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
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
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 ModelLike } from './shared/model.js';
|
|
5
|
+
export { positionRings } from './rings.js';
|
|
6
|
+
type Context = Extract<PassContext, {
|
|
7
|
+
backend: 'webgpu';
|
|
8
|
+
}>;
|
|
9
|
+
/** A material's outline: either field left out takes the {@link ModelOutlines} value. */
|
|
10
|
+
export interface OutlineStyle {
|
|
11
|
+
/** Thickness in world units, 0-10 (0: no line on this material). */
|
|
12
|
+
outline?: number;
|
|
13
|
+
/** Linear RGB 0..1. */
|
|
14
|
+
color?: readonly [number, number, number];
|
|
15
|
+
}
|
|
16
|
+
/** Options of {@link ModelOutlines}. */
|
|
17
|
+
export interface ModelOutlineOptions {
|
|
18
|
+
/** Outline thickness in world units, 0-10 (0 hides it). Default 0.025, as `Toon`. Live as `outline`. */
|
|
19
|
+
outline?: number;
|
|
20
|
+
/** Outline colour, linear RGB 0..1. Default [0.015, 0.015, 0.015]. Live as `color`. */
|
|
21
|
+
color?: readonly [number, number, number];
|
|
22
|
+
/**
|
|
23
|
+
* Seam-free: every copy of a vertex at one position pushes out along their average normal, so hard-edged meshes get
|
|
24
|
+
* an unbroken outline. Default true. Live as `smooth`. Needs vertex buffers with STORAGE usage (gltf/pbr's and
|
|
25
|
+
* animation's have it); a primitive without draws as `smooth: false`.
|
|
26
|
+
*/
|
|
27
|
+
smooth?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Width and colour by material name (the glTF material's `name`), over `outline` and `color`; an entry's `outline`
|
|
30
|
+
* 0 leaves that material without a line. Live as `materials`.
|
|
31
|
+
*/
|
|
32
|
+
materials?: Readonly<Record<string, OutlineStyle>>;
|
|
33
|
+
/**
|
|
34
|
+
* Vertex colours (COLOR_0) style the line: its colour is multiplied by the vertex's RGB and its width by the
|
|
35
|
+
* vertex's alpha, so one mesh can thin and tint its line where it is painted. Default false. Live as `vertexColors`.
|
|
36
|
+
*/
|
|
37
|
+
vertexColors?: boolean;
|
|
38
|
+
/** The models to outline (glTF PBR `PBRModel`s or materials meshes). Default: every model attached to the engine. */
|
|
39
|
+
models?: readonly ModelLike[];
|
|
40
|
+
/**
|
|
41
|
+
* Only the primitives whose node is one of these or a descendant of one (a model's `root` or `nodes` entries, or any
|
|
42
|
+
* engine mesh above them), as drei's `Outlines` inside one `skinnedMesh`. Default every primitive. Live as `nodes`.
|
|
43
|
+
*/
|
|
44
|
+
nodes?: readonly OutlineNode[];
|
|
45
|
+
}
|
|
46
|
+
/** A node {@link ModelOutlineOptions.nodes} names: an engine mesh, such as a model's `root` or one of its `nodes`. */
|
|
47
|
+
export interface OutlineNode {
|
|
48
|
+
readonly id: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Inverted-hull outlines on loaded and animated characters: every triangle primitive of the glTF PBR models (and
|
|
52
|
+
* materials meshes) on the engine, or of `models`, drawn again front-culled with its vertices pushed `outline` world
|
|
53
|
+
* units out along the normal, in `color`. Skinned and morphed primitives use their deformer's vertices of this frame;
|
|
54
|
+
* instanced primitives draw one hull per instance; hidden nodes and nodes outside a view's layers draw none.
|
|
55
|
+
* `ModelOutlines.attach(engine, options)`; WebGPU only.
|
|
56
|
+
* @throws RangeError when `outline` or `color` is out of range; TypeError for a `models` entry that is no model.
|
|
57
|
+
* @throws MargoError `'unsupported'` on a WebGL engine, `'disposed'` after `dispose()`.
|
|
58
|
+
*/
|
|
59
|
+
export declare class ModelOutlines implements EnginePlugin {
|
|
60
|
+
/** Width limit for optional outline projections. */
|
|
61
|
+
protected get maxOutline(): number;
|
|
62
|
+
/** Shader transform for optional outline projections. */
|
|
63
|
+
protected outlineShader(code: string): string;
|
|
64
|
+
/** Extra frame parameters for optional outline projections. */
|
|
65
|
+
protected outlineFrame(_values: Float32Array, _context: Context): void;
|
|
66
|
+
readonly name = "toon:models";
|
|
67
|
+
private engine?;
|
|
68
|
+
private width;
|
|
69
|
+
private rgb;
|
|
70
|
+
private readonly only?;
|
|
71
|
+
private under?;
|
|
72
|
+
private chosen?;
|
|
73
|
+
/** Seam-free outlines (see the option). */
|
|
74
|
+
smooth: boolean;
|
|
75
|
+
/** Vertex colours style the line (see the option). */
|
|
76
|
+
vertexColors: boolean;
|
|
77
|
+
private styles;
|
|
78
|
+
private readonly smoothing;
|
|
79
|
+
private frame;
|
|
80
|
+
/** The frame (`currentFrame`) the storage rows were last written in, and the end of the rows written in it. */
|
|
81
|
+
private rowFrame;
|
|
82
|
+
private rowEnd;
|
|
83
|
+
private ringLayout?;
|
|
84
|
+
private readonly values;
|
|
85
|
+
private rows;
|
|
86
|
+
private readonly runs;
|
|
87
|
+
private readonly scratch;
|
|
88
|
+
private uniform?;
|
|
89
|
+
private storage?;
|
|
90
|
+
private layout?;
|
|
91
|
+
private groups?;
|
|
92
|
+
/** Plain front-culling, plain mirrored, smooth, smooth mirrored. */
|
|
93
|
+
private pipelines?;
|
|
94
|
+
private key;
|
|
95
|
+
private readonly colorAttachment;
|
|
96
|
+
private readonly depthAttachment;
|
|
97
|
+
private disposed;
|
|
98
|
+
constructor({ outline, color, smooth, materials, vertexColors, models, nodes, }?: ModelOutlineOptions);
|
|
99
|
+
static attach(engine: EngineKernel, options?: ModelOutlineOptions): ModelOutlines;
|
|
100
|
+
/** Outline thickness in world units, 0-10 (0 hides it). */
|
|
101
|
+
get outline(): number;
|
|
102
|
+
set outline(v: number);
|
|
103
|
+
/** Outline colour, linear RGB 0..1. */
|
|
104
|
+
get color(): readonly [number, number, number];
|
|
105
|
+
set color(v: readonly [number, number, number]);
|
|
106
|
+
/** The nodes whose primitives are outlined (see the option); undefined outlines every primitive. */
|
|
107
|
+
get nodes(): readonly OutlineNode[] | undefined;
|
|
108
|
+
set nodes(v: readonly OutlineNode[] | undefined);
|
|
109
|
+
/** Width and colour by material name, over `outline` and `color`. */
|
|
110
|
+
get materials(): Readonly<Record<string, OutlineStyle>>;
|
|
111
|
+
set materials(v: Readonly<Record<string, OutlineStyle>>);
|
|
112
|
+
setup(engine: Engine): void;
|
|
113
|
+
registerPasses(graph: FrameGraph): void;
|
|
114
|
+
/** The models drawn this frame: `models`, or every model plugin on the engine. */
|
|
115
|
+
private models;
|
|
116
|
+
private add;
|
|
117
|
+
private collect;
|
|
118
|
+
private prepare;
|
|
119
|
+
private draw;
|
|
120
|
+
/** Group 1 of the smooth pipelines for `p`: its vertices of this frame and its rings, baked on first use. */
|
|
121
|
+
private ringGroup;
|
|
122
|
+
private release;
|
|
123
|
+
onDeviceRestored(engine: Engine): void;
|
|
124
|
+
dispose(): void;
|
|
125
|
+
[Symbol.dispose]: () => void;
|
|
126
|
+
}
|
package/dist/models.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel, Mesh as MeshHandle } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph, type Geometry } from '@margoengine/core/extension';
|
|
4
|
+
/** Options of {@link MeshOutline}. Every field but `mesh` is live as a property of the same name. */
|
|
5
|
+
export interface MeshOutlineOptions {
|
|
6
|
+
/** The mesh to outline; its world matrix and visibility are read every frame. Required. */
|
|
7
|
+
mesh: MeshHandle;
|
|
8
|
+
/**
|
|
9
|
+
* Triangles of the hull: a core geometry (24-byte vertices, position then normal). Default the mesh's own geometry;
|
|
10
|
+
* pass one when another package draws the mesh (a materials mesh keeps it on its React binding).
|
|
11
|
+
*/
|
|
12
|
+
geometry?: Geometry | null;
|
|
13
|
+
/** Line colour, linear RGB 0 or more. Default [0, 0, 0]. */
|
|
14
|
+
color?: readonly [number, number, number];
|
|
15
|
+
/** Line alpha 0..1, blended only when `transparent`. Default 1. */
|
|
16
|
+
opacity?: number;
|
|
17
|
+
/** Blend the line over the scene by `opacity` and leave depth unwritten. Default false. */
|
|
18
|
+
transparent?: boolean;
|
|
19
|
+
/** Width: world units, or drawing-buffer pixels when `screenspace`. 0 or more, at most 1024. Default 0.05. */
|
|
20
|
+
thickness?: number;
|
|
21
|
+
/** Measure `thickness` in drawing-buffer pixels, so the line keeps its width at every distance and zoom. Default false. */
|
|
22
|
+
screenspace?: boolean;
|
|
23
|
+
/** Tone map the colour like a lit surface (Khronos PBR Neutral); false clamps it like unlit output. Default true. */
|
|
24
|
+
toneMapped?: boolean;
|
|
25
|
+
/** Push the hull's depth back by `polygonOffsetFactor` times its depth slope. Default false. */
|
|
26
|
+
polygonOffset?: boolean;
|
|
27
|
+
/** Slope factor of `polygonOffset`, finite. Default 0. */
|
|
28
|
+
polygonOffsetFactor?: number;
|
|
29
|
+
/** Draw order among outlines: a higher value draws later, -1000..1000. Default 0. Fixed at construction. */
|
|
30
|
+
renderOrder?: number;
|
|
31
|
+
/** Crease angle in radians, 0..pi: copies of a vertex whose normals differ by less are averaged. Default pi. */
|
|
32
|
+
angle?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* For core vertices (6 floats: position, normal), the hull normal of each vertex: the sum of the normals of every copy
|
|
36
|
+
* at its position within `angle` radians of its own (itself included), or its own normal where that sum vanishes.
|
|
37
|
+
*/
|
|
38
|
+
export declare function creasedNormals(vertices: Float32Array, angle: number): Float32Array;
|
|
39
|
+
/** The colour written for `color`: tone mapped (or clamped), then sRGB encoded, as core writes a surface. */
|
|
40
|
+
export declare function outlineColor(color: readonly number[], toneMapped: boolean): [number, number, number];
|
|
41
|
+
/**
|
|
42
|
+
* drei-style `Outlines` for one mesh: an inverted hull in `color`, `thickness` world units (or pixels with
|
|
43
|
+
* `screenspace`) wide, drawn after the opaque scene. `MeshOutline.attach(engine, { mesh })`; WebGPU or the WebGL2
|
|
44
|
+
* compatibility layer.
|
|
45
|
+
* @throws RangeError when an option is out of range or `mesh` is missing.
|
|
46
|
+
* @throws MargoError `'unsupported'` on native WebGL2, `'disposed'` after `dispose()`.
|
|
47
|
+
*/
|
|
48
|
+
export declare class MeshOutline implements EnginePlugin {
|
|
49
|
+
readonly name: string;
|
|
50
|
+
readonly mesh: MeshHandle;
|
|
51
|
+
geometry: Geometry | null | undefined;
|
|
52
|
+
color: readonly [number, number, number];
|
|
53
|
+
opacity: number;
|
|
54
|
+
transparent: boolean;
|
|
55
|
+
thickness: number;
|
|
56
|
+
screenspace: boolean;
|
|
57
|
+
toneMapped: boolean;
|
|
58
|
+
polygonOffset: boolean;
|
|
59
|
+
polygonOffsetFactor: number;
|
|
60
|
+
angle: number;
|
|
61
|
+
readonly renderOrder: number;
|
|
62
|
+
private engine?;
|
|
63
|
+
private hull?;
|
|
64
|
+
private uniform?;
|
|
65
|
+
/** Bind groups by pipeline: layout 'auto' gives each pipeline its own layout. */
|
|
66
|
+
private readonly groups;
|
|
67
|
+
private readonly pipelines;
|
|
68
|
+
private readonly values;
|
|
69
|
+
private readonly colorAttachment;
|
|
70
|
+
private readonly depthAttachment;
|
|
71
|
+
private disposed;
|
|
72
|
+
constructor(options: MeshOutlineOptions);
|
|
73
|
+
static attach(engine: EngineKernel, options: MeshOutlineOptions): MeshOutline;
|
|
74
|
+
private validate;
|
|
75
|
+
setup(engine: Engine): void;
|
|
76
|
+
registerPasses(graph: FrameGraph): void;
|
|
77
|
+
/** The hull buffers of the current geometry and angle, rebuilt when either changes. */
|
|
78
|
+
private buffers;
|
|
79
|
+
private pipeline;
|
|
80
|
+
private draw;
|
|
81
|
+
private release;
|
|
82
|
+
onDeviceRestored(engine: Engine): void;
|
|
83
|
+
/** Removes the plugin and releases its buffers; repeat calls are safe. */
|
|
84
|
+
dispose(): void;
|
|
85
|
+
[Symbol.dispose]: () => void;
|
|
86
|
+
}
|
package/dist/outline.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const f=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { r as e, t } from "./errors-XvcwUnU4.js";
|
|
5
|
+
import { a as n, i as r, n as i, o as a, r as o, t as s } from "./rings-BmbstqyP.js";
|
|
6
|
+
import { i as c, n as l, r as u } from "./frame-resources-CRA1ZB6K.js";
|
|
7
|
+
import { t as d } from "./pixel-hull-BYQALJ3e.js";
|
|
8
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
9
|
+
//#region ../shared/display.js
|
|
10
|
+
var p = "fn pbrNeutral(input:vec3f)->vec3f {let x=min(input.r,min(input.g,input.b));let offset=select(0.04,x-6.25*x*x,x<0.08);var v=input-offset;let peak=max(v.r,max(v.g,v.b));if(peak<0.76){return v;}let newPeak=1.0-0.24*0.24/(peak+0.24-0.76);v*=newPeak/peak;return mix(v,vec3f(newPeak),1.0-1.0/(0.15*(peak-newPeak)+1.0));}\n";
|
|
11
|
+
function m(e, t = [
|
|
12
|
+
0,
|
|
13
|
+
0,
|
|
14
|
+
0
|
|
15
|
+
]) {
|
|
16
|
+
let n = Math.min(e[0], e[1], e[2]), r = n < .08 ? n - 6.25 * n * n : .04;
|
|
17
|
+
for (let n = 0; n < 3; n++) t[n] = e[n] - r;
|
|
18
|
+
let i = Math.max(t[0], t[1], t[2]);
|
|
19
|
+
if (i < .76) return t;
|
|
20
|
+
let a = 1 - .0576 / (i + .24 - .76), o = 1 / (.15 * (i - a) + 1);
|
|
21
|
+
for (let e = 0; e < 3; e++) t[e] = a + (t[e] * (a / i) - a) * o;
|
|
22
|
+
return t;
|
|
23
|
+
}
|
|
24
|
+
`${p}`;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/outline.ts
|
|
27
|
+
var h = `${d}
|
|
28
|
+
struct Frame {camera:mat4x4f,model:mat4x4f,color:vec4f,params:vec4f}
|
|
29
|
+
@group(0) @binding(0) var<uniform> u:Frame;
|
|
30
|
+
@vertex fn vertex(@location(0) position:vec3f,@location(1) hull:vec3f)->@builtin(position) vec4f{
|
|
31
|
+
let m=u.model;let a=m[0].xyz;let b=m[1].xyz;let c=m[2].xyz;
|
|
32
|
+
let n=mat3x3f(cross(b,c),cross(c,a),cross(a,b))*hull*select(-1.0,1.0,dot(a,cross(b,c))>=0.0);
|
|
33
|
+
let l=length(n);let dir=select(vec3f(0),n/l,l>0.0);
|
|
34
|
+
if(u.params.y>0.5){return pixelHull(u.camera*m*vec4f(position,1),u.camera*vec4f(dir,0),u.params.zw,u.params.x);}
|
|
35
|
+
return u.camera*(m*vec4f(position,1)+vec4f(dir*u.params.x,0));
|
|
36
|
+
}
|
|
37
|
+
@fragment fn fragment()->@location(0) vec4f{return u.color;}`;
|
|
38
|
+
function g(e, t) {
|
|
39
|
+
let n = Math.floor(e.length / 6), r = s(e, 6), i = new Float32Array(n * 3), a = (t) => {
|
|
40
|
+
let n = t * 6, r = e[n + 3], i = e[n + 4], a = e[n + 5], o = Math.hypot(r, i, a) || 1;
|
|
41
|
+
return [
|
|
42
|
+
r / o,
|
|
43
|
+
i / o,
|
|
44
|
+
a / o
|
|
45
|
+
];
|
|
46
|
+
}, o = t >= Math.PI ? -Infinity : Math.cos(t) - 1e-6;
|
|
47
|
+
for (let e = 0; e < n; e++) {
|
|
48
|
+
let t = a(e), s = t[0], c = t[1], l = t[2];
|
|
49
|
+
for (let i = r[e], u = 0; i !== e && u < n; i = r[i], u++) {
|
|
50
|
+
let e = a(i);
|
|
51
|
+
t[0] * e[0] + t[1] * e[1] + t[2] * e[2] < o || (s += e[0], c += e[1], l += e[2]);
|
|
52
|
+
}
|
|
53
|
+
let u = Math.hypot(s, c, l), d = u < 1e-6;
|
|
54
|
+
i[e * 3] = d ? t[0] : s / u, i[e * 3 + 1] = d ? t[1] : c / u, i[e * 3 + 2] = d ? t[2] : l / u;
|
|
55
|
+
}
|
|
56
|
+
return i;
|
|
57
|
+
}
|
|
58
|
+
function _(e, t) {
|
|
59
|
+
let n = t ? m([
|
|
60
|
+
e[0],
|
|
61
|
+
e[1],
|
|
62
|
+
e[2]
|
|
63
|
+
]) : e.map((e) => Math.min(e, 1));
|
|
64
|
+
return [
|
|
65
|
+
0,
|
|
66
|
+
1,
|
|
67
|
+
2
|
|
68
|
+
].map((e) => {
|
|
69
|
+
let t = Math.max(0, n[e]);
|
|
70
|
+
return t < .0031308 ? t * 12.92 : 1.055 * t ** (1 / 2.4) - .055;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
var v = 0, y = (e, t, n) => typeof e == "number" && Number.isFinite(e) && e >= t && e <= n, b = class s {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
74
|
+
constructor(t) {
|
|
75
|
+
if (this.name = "toon:outline:" + v++, this.groups = /* @__PURE__ */ new Map(), this.pipelines = /* @__PURE__ */ new Map(), this.values = /* @__PURE__ */ new Float32Array(40), this.colorAttachment = {
|
|
76
|
+
view: void 0,
|
|
77
|
+
loadOp: "load",
|
|
78
|
+
storeOp: "store"
|
|
79
|
+
}, this.depthAttachment = { view: void 0 }, this.disposed = !1, !t || typeof t != "object" || !t.mesh || typeof t.mesh != "object") throw e("MeshOutline needs { mesh }; pass the mesh to outline", "arguments");
|
|
80
|
+
this.mesh = t.mesh, this.geometry = t.geometry, this.color = t.color ?? [
|
|
81
|
+
0,
|
|
82
|
+
0,
|
|
83
|
+
0
|
|
84
|
+
], this.opacity = t.opacity ?? 1, this.transparent = t.transparent ?? !1, this.thickness = t.thickness ?? .05, this.screenspace = t.screenspace ?? !1, this.toneMapped = t.toneMapped ?? !0, this.polygonOffset = t.polygonOffset ?? !1, this.polygonOffsetFactor = t.polygonOffsetFactor ?? 0, this.renderOrder = t.renderOrder ?? 0, this.angle = t.angle ?? Math.PI, this.validate();
|
|
85
|
+
}
|
|
86
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
87
|
+
let n = new s(t);
|
|
88
|
+
return f(e).use(n), n;
|
|
89
|
+
}
|
|
90
|
+
validate() {
|
|
91
|
+
let t = this.color;
|
|
92
|
+
if (!Array.isArray(t) || t.length !== 3 || !t.every((e) => y(e, 0, 1e6)) || !y(this.opacity, 0, 1) || !y(this.thickness, 0, 1024) || !y(this.angle, 0, Math.PI) || !y(this.polygonOffsetFactor, -1e6, 1e6) || !y(this.renderOrder, -1e3, 1e3)) throw e("Invalid MeshOutline options; use color three finite numbers 0 or more, opacity 0..1, thickness 0..1024, angle 0..pi, a finite polygonOffsetFactor and renderOrder -1000..1000", "arguments");
|
|
93
|
+
}
|
|
94
|
+
setup(e) {
|
|
95
|
+
if (this.disposed || !e.gpu) throw t(this.disposed ? "disposed" : "unsupported", this.disposed ? "MeshOutline was disposed; attach a new MeshOutline" : "MeshOutline requires WebGPU or the WebGL2 compatibility layer; create the engine with one of those backends");
|
|
96
|
+
this.engine = e;
|
|
97
|
+
}
|
|
98
|
+
registerPasses(e) {
|
|
99
|
+
e.add({
|
|
100
|
+
name: this.name,
|
|
101
|
+
order: 1.25 + this.renderOrder * 1e-5,
|
|
102
|
+
joins: !0,
|
|
103
|
+
execute: (e) => {
|
|
104
|
+
e.backend === "webgpu" && this.draw(e);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
buffers(e, t) {
|
|
109
|
+
let n = this.hull;
|
|
110
|
+
if (n && n.geometry === t && n.angle === this.angle) return n;
|
|
111
|
+
n && (l(e, n.vertex), l(e, n.index));
|
|
112
|
+
let r = t.vertices.length / 6, i = g(t.vertices, this.angle), a = new Float32Array(r * 6);
|
|
113
|
+
for (let e = 0; e < r; e++) a.set(t.vertices.subarray(e * 6, e * 6 + 3), e * 6), a.set(i.subarray(e * 3, e * 3 + 3), e * 6 + 3);
|
|
114
|
+
let o = e.createBuffer({
|
|
115
|
+
size: Math.max(16, a.byteLength),
|
|
116
|
+
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
117
|
+
}), s = e.createBuffer({
|
|
118
|
+
size: Math.max(16, Math.ceil(t.indices.byteLength / 4) * 4),
|
|
119
|
+
usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST
|
|
120
|
+
});
|
|
121
|
+
return e.queue.writeBuffer(o, 0, a), e.queue.writeBuffer(s, 0, t.indices), this.hull = {
|
|
122
|
+
geometry: t,
|
|
123
|
+
angle: this.angle,
|
|
124
|
+
vertex: o,
|
|
125
|
+
index: s,
|
|
126
|
+
count: t.indices.length
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
pipeline(e, t) {
|
|
130
|
+
let n = e.descriptor.depthStencil, r = n.depthCompare?.startsWith("greater") ?? !1, i = this.polygonOffset ? this.polygonOffsetFactor * (r ? -1 : 1) : 0, a = [
|
|
131
|
+
e.format,
|
|
132
|
+
e.descriptor.multisample?.count ?? 1,
|
|
133
|
+
n.format,
|
|
134
|
+
n.depthCompare,
|
|
135
|
+
t,
|
|
136
|
+
this.transparent,
|
|
137
|
+
i
|
|
138
|
+
].join(":"), o = this.pipelines.get(a);
|
|
139
|
+
if (o) return o;
|
|
140
|
+
let s = e.device, c = s.createShaderModule({ code: h }), l = {
|
|
141
|
+
operation: "add",
|
|
142
|
+
srcFactor: "src-alpha",
|
|
143
|
+
dstFactor: "one-minus-src-alpha"
|
|
144
|
+
};
|
|
145
|
+
return o = s.createRenderPipeline({
|
|
146
|
+
layout: "auto",
|
|
147
|
+
vertex: {
|
|
148
|
+
module: c,
|
|
149
|
+
entryPoint: "vertex",
|
|
150
|
+
buffers: [{
|
|
151
|
+
arrayStride: 24,
|
|
152
|
+
attributes: [{
|
|
153
|
+
shaderLocation: 0,
|
|
154
|
+
offset: 0,
|
|
155
|
+
format: "float32x3"
|
|
156
|
+
}, {
|
|
157
|
+
shaderLocation: 1,
|
|
158
|
+
offset: 12,
|
|
159
|
+
format: "float32x3"
|
|
160
|
+
}]
|
|
161
|
+
}]
|
|
162
|
+
},
|
|
163
|
+
fragment: {
|
|
164
|
+
module: c,
|
|
165
|
+
entryPoint: "fragment",
|
|
166
|
+
targets: [{
|
|
167
|
+
format: e.format,
|
|
168
|
+
...this.transparent ? { blend: {
|
|
169
|
+
color: l,
|
|
170
|
+
alpha: l
|
|
171
|
+
} } : {}
|
|
172
|
+
}]
|
|
173
|
+
},
|
|
174
|
+
primitive: {
|
|
175
|
+
topology: "triangle-list",
|
|
176
|
+
cullMode: t ? "back" : "front"
|
|
177
|
+
},
|
|
178
|
+
depthStencil: {
|
|
179
|
+
format: n.format,
|
|
180
|
+
depthCompare: n.depthCompare,
|
|
181
|
+
depthWriteEnabled: !this.transparent,
|
|
182
|
+
depthBiasSlopeScale: i
|
|
183
|
+
},
|
|
184
|
+
multisample: e.descriptor.multisample
|
|
185
|
+
}), this.pipelines.set(a, o), o;
|
|
186
|
+
}
|
|
187
|
+
draw(e) {
|
|
188
|
+
let t = this.engine, s = this.mesh;
|
|
189
|
+
if (this.validate(), this.disposed || !(this.thickness > 0) || t.meshes.get(s.id) !== s || o(t.store, s.id) || !n(e, s)) return;
|
|
190
|
+
let l = this.geometry === void 0 ? s.geometry : this.geometry;
|
|
191
|
+
if (!l || !l.indices.length) return;
|
|
192
|
+
let d = e.device, f = this.buffers(d, l), p = s.matrix, m = p[0] * (p[5] * p[10] - p[6] * p[9]) - p[4] * (p[1] * p[10] - p[2] * p[9]) + p[8] * (p[1] * p[6] - p[2] * p[5]) < 0;
|
|
193
|
+
this.uniform || (this.uniform = d.createBuffer({
|
|
194
|
+
size: 160,
|
|
195
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
196
|
+
}), this.groups.clear());
|
|
197
|
+
let h = this.pipeline(e, m), g = this.values, v = _(this.color, this.toneMapped), y = a(e);
|
|
198
|
+
g.set(e.camera.matrix), g.set(p, 16), g.set(v, 32), g[35] = this.transparent ? this.opacity : 1, g[36] = this.thickness, g[37] = +this.screenspace, g[38] = y[0], g[39] = y[1];
|
|
199
|
+
let b = c(d, this.uniform, g, e.view ?? e), x = this.uniform, S = this.groups.get(h);
|
|
200
|
+
if (!S) {
|
|
201
|
+
let e = h.getBindGroupLayout(0);
|
|
202
|
+
S = u(x, (t) => d.createBindGroup({
|
|
203
|
+
layout: e,
|
|
204
|
+
entries: [{
|
|
205
|
+
binding: 0,
|
|
206
|
+
resource: t
|
|
207
|
+
}]
|
|
208
|
+
})), this.groups.set(h, S);
|
|
209
|
+
}
|
|
210
|
+
let C = i(e, this.colorAttachment, this.depthAttachment);
|
|
211
|
+
r(C, e), C.setPipeline(h), C.setBindGroup(0, S.get(b)), C.setVertexBuffer(0, f.vertex), C.setIndexBuffer(f.index, "uint32"), C.drawIndexed(f.count), C.end();
|
|
212
|
+
}
|
|
213
|
+
release() {
|
|
214
|
+
let e = this.engine?.gpu;
|
|
215
|
+
e && (l(e, this.uniform), l(e, this.hull?.vertex), l(e, this.hull?.index)), this.uniform = this.hull = void 0, this.groups.clear(), this.pipelines.clear();
|
|
216
|
+
}
|
|
217
|
+
onDeviceRestored(e) {
|
|
218
|
+
this.release(), this.setup(e);
|
|
219
|
+
}
|
|
220
|
+
dispose() {
|
|
221
|
+
this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.release(), this.engine = void 0);
|
|
222
|
+
}
|
|
223
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
224
|
+
this.dispose();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
//#endregion
|
|
228
|
+
export { b as MeshOutline, g as creasedNormals, _ as outlineColor };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
//#region src/pixel-hull.ts
|
|
2
|
+
var e = "\nfn pixelHull(p:vec4f,n:vec4f,size:vec2f,width:f32)->vec4f{\n let direction=(n.xy*p.w-p.xy*n.w)*size;\n let magnitude=length(direction);\n var result=p;\n if(magnitude>1e-8&&p.w>0.0){result.x+=direction.x/magnitude*2.0*width/size.x*p.w;result.y+=direction.y/magnitude*2.0*width/size.y*p.w;}\n return result;\n}";
|
|
3
|
+
//#endregion
|
|
4
|
+
export { e as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const pixelHull = "\nfn pixelHull(p:vec4f,n:vec4f,size:vec2f,width:f32)->vec4f{\n let direction=(n.xy*p.w-p.xy*n.w)*size;\n let magnitude=length(direction);\n var result=p;\n if(magnitude>1e-8&&p.w>0.0){result.x+=direction.x/magnitude*2.0*width/size.x*p.w;result.y+=direction.y/magnitude*2.0*width/size.y*p.w;}\n return result;\n}";
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//#region ../shared/view-rect.js
|
|
2
|
+
function e(e) {
|
|
3
|
+
return e?.view?.viewport;
|
|
4
|
+
}
|
|
5
|
+
function t(t) {
|
|
6
|
+
let n = e(t);
|
|
7
|
+
return n ? [n[2], n[3]] : [t.width, t.height];
|
|
8
|
+
}
|
|
9
|
+
function n(t, n) {
|
|
10
|
+
let r = e(n);
|
|
11
|
+
if (!r) return;
|
|
12
|
+
t.setViewport(r[0], r[1], r[2], r[3], 0, 1);
|
|
13
|
+
let i = n.view.scissor ?? r;
|
|
14
|
+
t.setScissorRect(i[0], i[1], i[2], i[3]);
|
|
15
|
+
}
|
|
16
|
+
function r() {
|
|
17
|
+
return globalThis[Symbol.for("margoengine.layers")];
|
|
18
|
+
}
|
|
19
|
+
function i(e) {
|
|
20
|
+
return r()?.maskOf(e) ?? 1;
|
|
21
|
+
}
|
|
22
|
+
function a(e, t) {
|
|
23
|
+
let n = e?.view?.layers;
|
|
24
|
+
return n === void 0 || (i(t) & n) !== 0;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region ../shared/visibility.js
|
|
28
|
+
function o(e, t) {
|
|
29
|
+
for (let n = t; n >= 0; n = e.parents?.[n] ?? -1) if (e.hidden[n]) return !0;
|
|
30
|
+
return !1;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region ../shared/scene-pass.js
|
|
34
|
+
function s(e, t, n) {
|
|
35
|
+
let r = e.descriptor.depthStencil.format.includes("stencil");
|
|
36
|
+
return t.view = e.color, t.loadOp = "load", t.storeOp = "store", t.resolveTarget = void 0, n.view = e.depth, n.depthLoadOp = "load", n.depthStoreOp = "store", n.stencilLoadOp = r ? "load" : void 0, n.stencilStoreOp = r ? "store" : void 0, e.attach?.(t, n, !0), e.encoder.beginRenderPass({
|
|
37
|
+
colorAttachments: [t],
|
|
38
|
+
depthStencilAttachment: n
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/rings.ts
|
|
43
|
+
function c(e, t = 20) {
|
|
44
|
+
let n = Math.floor(e.length / t), r = new Uint32Array(n), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
|
|
45
|
+
for (let o = 0; o < n; o++) {
|
|
46
|
+
let n = o * t, s = (e[n] || 0) + "," + (e[n + 1] || 0) + "," + (e[n + 2] || 0), c = i.get(s);
|
|
47
|
+
c === void 0 ? a.set(s, o) : r[c] = o, r[o] = a.get(s), i.set(s, o);
|
|
48
|
+
}
|
|
49
|
+
return r;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { a, n as i, s as n, t as o, o as r, c as t };
|
package/dist/rings.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For each vertex of `vertices` (`stride` floats a vertex, position first), the next vertex at exactly the same
|
|
3
|
+
* position, round to itself: a vertex alone at its position points at itself. Positions match bit for bit, with -0
|
|
4
|
+
* the same as 0.
|
|
5
|
+
*/
|
|
6
|
+
export declare function positionRings(vertices: Float32Array, stride?: number): Uint32Array;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import { type PassContext } from '@margoengine/core/extension';
|
|
3
|
+
import { Toon as WorldToon, type ToonPipelineOptions } from './index.js';
|
|
4
|
+
import { ModelOutlines as WorldModelOutlines, type ModelOutlineOptions } from './models.js';
|
|
5
|
+
export type { ToonPipelineOptions } from './index.js';
|
|
6
|
+
export type { ModelOutlineOptions, OutlineNode, OutlineStyle } from './models.js';
|
|
7
|
+
/** Core toon shading and inverted hulls in drawing-buffer pixels. Default outline 3; range 0-1024 pixels. */
|
|
8
|
+
export declare class Toon extends WorldToon {
|
|
9
|
+
private readonly pixelValues;
|
|
10
|
+
protected get maxOutline(): number;
|
|
11
|
+
constructor(options?: ToonPipelineOptions);
|
|
12
|
+
static attach(engine: EngineKernel, options?: ToonPipelineOptions): Toon;
|
|
13
|
+
protected outlineData(context?: PassContext): Float32Array;
|
|
14
|
+
protected outlineShader(code: string): string;
|
|
15
|
+
}
|
|
16
|
+
/** Model hulls in drawing-buffer pixels, including live per-material widths, vertex paint and deformed meshes. */
|
|
17
|
+
export declare class ModelOutlines extends WorldModelOutlines {
|
|
18
|
+
protected get maxOutline(): number;
|
|
19
|
+
constructor(options?: ModelOutlineOptions);
|
|
20
|
+
static attach(engine: EngineKernel, options?: ModelOutlineOptions): ModelOutlines;
|
|
21
|
+
protected outlineFrame(values: Float32Array, context: Extract<PassContext, {
|
|
22
|
+
backend: 'webgpu';
|
|
23
|
+
}>): void;
|
|
24
|
+
protected outlineShader(code: string): string;
|
|
25
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const i=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { o as e } from "./rings-BmbstqyP.js";
|
|
5
|
+
import { t } from "./src-npYQWEC-.js";
|
|
6
|
+
import { t as n } from "./models-CyFG_IC3.js";
|
|
7
|
+
import { t as r } from "./pixel-hull-BYQALJ3e.js";
|
|
8
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
9
|
+
//#region src/screen-outline.ts
|
|
10
|
+
var a = class n extends t {
|
|
11
|
+
get maxOutline() {
|
|
12
|
+
return 1024;
|
|
13
|
+
}
|
|
14
|
+
constructor(e = {}) {
|
|
15
|
+
super({
|
|
16
|
+
outline: 3,
|
|
17
|
+
...e
|
|
18
|
+
}), this.pixelValues = /* @__PURE__ */ new Float32Array(8);
|
|
19
|
+
}
|
|
20
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
21
|
+
let r = new n(t);
|
|
22
|
+
return i(e).use(r), r;
|
|
23
|
+
}
|
|
24
|
+
outlineData(t) {
|
|
25
|
+
return this.pixelValues.set(this.values), this.pixelValues.set(t ? e(t) : [1, 1], 4), this.pixelValues;
|
|
26
|
+
}
|
|
27
|
+
outlineShader(e) {
|
|
28
|
+
return r + e.replace("var<uniform> outline:vec4f;", "var<uniform> screen:array<vec4f,2>;").replaceAll("outline.", "screen[0].").replace("let p=model*vec4f(i.position,1)+vec4f(normal*screen[0].w,0);return Output(frame.camera*p,", "let p=frame.camera*model*vec4f(i.position,1);let clip=pixelHull(p,frame.camera*vec4f(normal,0),screen[1].xy,screen[0].w);return Output(clip,");
|
|
29
|
+
}
|
|
30
|
+
}, o = class t extends n {
|
|
31
|
+
get maxOutline() {
|
|
32
|
+
return 1024;
|
|
33
|
+
}
|
|
34
|
+
constructor(e = {}) {
|
|
35
|
+
super({
|
|
36
|
+
outline: 3,
|
|
37
|
+
...e
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
41
|
+
let r = new t(n);
|
|
42
|
+
return i(e).use(r), r;
|
|
43
|
+
}
|
|
44
|
+
outlineFrame(t, n) {
|
|
45
|
+
t.set(e(n), 17);
|
|
46
|
+
}
|
|
47
|
+
outlineShader(e) {
|
|
48
|
+
return r + e.replace("return Out(u.camera*p,d.style.rgb*paint.rgb);", "let clip=u.camera*m*vec4f(position,1);return Out(pixelHull(clip,u.camera*vec4f(n,0),u.flags.yz,d.style.w*paint.a),d.style.rgb*paint.rgb);");
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
//#endregion
|
|
52
|
+
export { o as ModelOutlines, a as Toon };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Mesh } from '@margoengine/core/extension';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Structural view of a gltf/pbr `PBRModel` primitive (also what @margoengine/materials meshes are): node-space
|
|
5
|
+
* vertices, 20 floats each (position at 0, normal at 4, TEXCOORD_0 at 12), triangle indices and the material's
|
|
6
|
+
* settings. `buffers` exist while its model is set up on a device; `deformed` replaces the vertices when skinned.
|
|
7
|
+
*/
|
|
8
|
+
export interface ModelPrimitiveLike {
|
|
9
|
+
readonly node: Mesh;
|
|
10
|
+
readonly vertices: Float32Array;
|
|
11
|
+
readonly indices: Uint32Array;
|
|
12
|
+
readonly material: { readonly settings: ArrayLike<number>; readonly side?: 'front' | 'back' | 'double' };
|
|
13
|
+
readonly nodeIndex?: number;
|
|
14
|
+
/** gltf\/pbr `PBRInstances`: drawn once per instance (24 floats each, node-space matrix first). */
|
|
15
|
+
readonly instances?: { readonly count: number; readonly data: Float32Array };
|
|
16
|
+
readonly buffers?: { vertex: GPUBuffer; index: GPUBuffer };
|
|
17
|
+
readonly deformed?: GPUBuffer;
|
|
18
|
+
/** Byte offset of the primitive's vertices in `deformed`. Default 0. */
|
|
19
|
+
readonly deformedOffset?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Structural view of a gltf/pbr `PBRModel`: its `root` node and `primitives`. */
|
|
22
|
+
export interface ModelLike {
|
|
23
|
+
readonly root: Mesh;
|
|
24
|
+
readonly primitives: readonly ModelPrimitiveLike[];
|
|
25
|
+
readonly disposed?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Structural view of a pick proxy: an object drawn without triangles (an @margoengine/lod imposter level), hit through
|
|
29
|
+
* its local bounding sphere `sphere` (x, y, z, radius) under `anchor`'s world matrix. `model` is reported with the hit.
|
|
30
|
+
*/
|
|
31
|
+
export interface PickProxy {
|
|
32
|
+
readonly anchor: Mesh;
|
|
33
|
+
readonly sphere: ArrayLike<number>;
|
|
34
|
+
readonly model?: object | null;
|
|
35
|
+
}
|
|
36
|
+
/** gltf/pbr shape: a `root` node and `primitives` with 20-float CPU vertices (the first one is checked). */
|
|
37
|
+
export function isModel(value: unknown): value is ModelLike;
|
|
38
|
+
/** Proxies from every engine plugin with a `pickProxies()` method (an @margoengine/lod `LODSystem`), in a new array. */
|
|
39
|
+
export function engineProxies(engine: object): PickProxy[];
|