@margoengine/toon 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.
@@ -0,0 +1,96 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const n=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { i as e, n as t } from "./frame-resources-CRA1ZB6K.js";
5
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
6
+ import { ToonShading as r } from "@margoengine/creative/toon";
7
+ import { MargoError as i } from "@margoengine/math/errors";
8
+ //#endregion
9
+ //#region ../shared/gpu.js
10
+ var a = "\nstruct Frame {camera:mat4x4f,eye:vec4f,light:vec4f,radiance:vec4f,ambient:vec4f}\n@group(0) @binding(0) var<uniform> frame:Frame;\nstruct Input {@location(0) position:vec3f,@location(1) normal:vec3f,@location(2) m0:vec4f,@location(3) m1:vec4f,@location(4) m2:vec4f,@location(5) m3:vec4f,@location(6) color:vec4f,@location(7) factors:vec4f}\nstruct Output {@builtin(position) @invariant clip:vec4f,@location(0) @interpolate(flat) id:u32}\n@vertex fn vertex(i:Input)->Output {let model=mat4x4f(i.m0,i.m1,i.m2,i.m3);let p=model*vec4f(i.position,1);return Output(frame.camera*p,u32(i.factors.w));}\n", o = class o {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
11
+ get maxOutline() {
12
+ return 10;
13
+ }
14
+ outlineData(e) {
15
+ return this.values;
16
+ }
17
+ outlineShader(e) {
18
+ return e;
19
+ }
20
+ constructor({ outline: e = .025, color: t = [
21
+ .015,
22
+ .015,
23
+ .015
24
+ ], ...n } = {}) {
25
+ if (this.name = "toon", this.format = "", this.disposed = !1, !Number.isFinite(e) || e < 0 || e > this.maxOutline || t.length !== 3 || t.some((e) => !Number.isFinite(e) || e < 0 || e > 1)) throw RangeError(`Toon outline must be between 0 and ${this.maxOutline} and color three channels between 0 and 1; pass finite values in range.`);
26
+ this.shading = new r(n), this.values = new Float32Array([...t, e]);
27
+ }
28
+ static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
29
+ let r = new o(t);
30
+ return n(e).use(r), r;
31
+ }
32
+ setup(e) {
33
+ if (this.disposed || !e.gpu) throw new i(this.disposed ? "disposed" : "unsupported", "Toon requires WebGPU; attach a new Toon to a WebGPU engine; see https://margoengine.com/errors#webgpu");
34
+ this.engine = e, this.shading.setup(e), this.buffer = e.gpu.createBuffer({
35
+ size: this.outlineData().byteLength,
36
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
37
+ });
38
+ }
39
+ registerPasses(t) {
40
+ this.shading.registerPasses(t), t.add({
41
+ name: "toon:hull",
42
+ order: 1,
43
+ execute: (t) => {
44
+ if (t.backend !== "webgpu" || !this.values[3]) return;
45
+ let n = t.format + ":" + (t.descriptor.multisample?.count ?? 1) + ":" + t.descriptor.depthStencil.format;
46
+ if (this.format !== n) {
47
+ let e = this.outlineShader(a.replace("let p=model*vec4f(i.position,1);", "let bent=mat3x3f(cross(i.m1.xyz,i.m2.xyz),cross(i.m2.xyz,i.m0.xyz),cross(i.m0.xyz,i.m1.xyz))*i.normal*select(-1.0,1.0,dot(i.m0.xyz,cross(i.m1.xyz,i.m2.xyz))>=0.0);let normal=normalize(select(vec3f(0,1,0),bent,dot(bent,bent)>0.0));let p=model*vec4f(i.position,1)+vec4f(normal*outline.w,0);") + "@group(0) @binding(1) var<uniform> outline:vec4f; @fragment fn fragment(i:Output)->@location(0) vec4f{return vec4f(outline.rgb,1);}"), r = t.device.createShaderModule({ code: e });
48
+ this.pipeline = t.device.createRenderPipeline({
49
+ ...t.descriptor,
50
+ layout: "auto",
51
+ vertex: {
52
+ ...t.descriptor.vertex,
53
+ module: r
54
+ },
55
+ fragment: {
56
+ module: r,
57
+ entryPoint: "fragment",
58
+ targets: [{ format: t.format }]
59
+ },
60
+ primitive: {
61
+ ...t.descriptor.primitive,
62
+ cullMode: "front"
63
+ },
64
+ depthStencil: {
65
+ ...t.descriptor.depthStencil,
66
+ depthWriteEnabled: !1
67
+ }
68
+ }), this.format = n;
69
+ }
70
+ let r = e(t.device, this.buffer, this.outlineData(t), t.view ?? t);
71
+ t.draw({
72
+ pipeline: this.pipeline,
73
+ clear: !1,
74
+ bindings: [{
75
+ binding: 1,
76
+ resource: r ?? { buffer: this.buffer }
77
+ }]
78
+ });
79
+ }
80
+ });
81
+ }
82
+ release() {
83
+ this.shading.dispose(), this.engine?.gpu && t(this.engine.gpu, this.buffer), this.buffer = void 0, this.pipeline = void 0, this.format = "";
84
+ }
85
+ onDeviceRestored(e) {
86
+ this.release(), this.setup(e);
87
+ }
88
+ dispose() {
89
+ this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.release(), this.engine = void 0);
90
+ }
91
+ [Symbol.dispose ?? Symbol()]() {
92
+ this.dispose();
93
+ }
94
+ };
95
+ //#endregion
96
+ export { o as t };
package/package.json CHANGED
@@ -1,6 +1,77 @@
1
- {
2
- "name": "@margoengine/toon",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@margoengine/toon",
3
+ "version": "0.1.0",
4
+ "description": "Stepped diffuse shading and inverted-hull outlines.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "webgpu",
8
+ "3d",
9
+ "toon"
10
+ ],
11
+ "homepage": "https://margoengine.com",
12
+ "bugs": {
13
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
18
+ "directory": "packages/toon"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "author": "Scott Campbell",
22
+ "type": "module",
23
+ "sideEffects": false,
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js"
28
+ },
29
+ "./models": {
30
+ "types": "./dist/models.d.ts",
31
+ "import": "./dist/models.js"
32
+ },
33
+ "./screen-outline": {
34
+ "types": "./dist/screen-outline.d.ts",
35
+ "import": "./dist/screen-outline.js"
36
+ },
37
+ "./hatch-models": {
38
+ "types": "./dist/hatch-models.d.ts",
39
+ "import": "./dist/hatch-models.js"
40
+ },
41
+ "./hatch": {
42
+ "types": "./dist/hatch.d.ts",
43
+ "import": "./dist/hatch.js"
44
+ },
45
+ "./outline": {
46
+ "types": "./dist/outline.d.ts",
47
+ "import": "./dist/outline.js"
48
+ }
49
+ },
50
+ "files": [
51
+ "dist",
52
+ "LICENSE",
53
+ "NOTICE"
54
+ ],
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "dependencies": {
59
+ "@margoengine/creative": "^0.1.0",
60
+ "@margoengine/math": "^0.1.0"
61
+ },
62
+ "peerDependencies": {
63
+ "@margoengine/core": "^0.1.0",
64
+ "@margoengine/gltf": "^0.1.0"
65
+ },
66
+ "peerDependenciesMeta": {
67
+ "@margoengine/gltf": {
68
+ "optional": true
69
+ }
70
+ },
71
+ "scripts": {
72
+ "build": "node ../../scripts/build-optional.js toon",
73
+ "test": "npm run build && node --test test/*.test.js",
74
+ "prepack": "npm run build",
75
+ "test:gpu": "cd ../.. && node packages/toon/test/models-check.mjs && node packages/toon/test/hatch-check.mjs && node packages/toon/test/screen-outline.browser.mjs && node packages/toon/test/hatch-models.browser.mjs && node packages/toon/test/outline.browser.mjs"
76
+ }
77
+ }