topotopo 0.2.0 → 0.4.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/NOTICE +30 -0
- package/README.md +54 -16
- package/dist/cli.js +95 -14
- package/dist/core/analysis.d.ts +5 -1
- package/dist/core/analysis.js +8 -3
- package/dist/core/bake.d.ts +2 -5
- package/dist/core/bake.js +12 -25
- package/dist/core/bevel.js +165 -97
- package/dist/core/geometry.js +1 -1
- package/dist/core/imageops.d.ts +6 -0
- package/dist/core/imageops.js +51 -0
- package/dist/core/materials.d.ts +4 -0
- package/dist/core/materials.js +11 -0
- package/dist/core/packed.js +11 -0
- package/dist/core/particles.d.ts +1 -1
- package/dist/core/particles.js +1 -1
- package/dist/core/rbd.js +13 -1
- package/dist/core/scatter.d.ts +1 -1
- package/dist/core/scatter.js +1 -1
- package/dist/core/scene.d.ts +20 -0
- package/dist/core/scene.js +71 -0
- package/dist/core/snippet-lib.d.ts +1 -1
- package/dist/core/snippet-lib.js +1 -1
- package/dist/core/solidify.d.ts +38 -0
- package/dist/core/solidify.js +356 -0
- package/dist/core/tangents.d.ts +52 -0
- package/dist/core/tangents.js +214 -0
- package/dist/core/texcoord.d.ts +4 -0
- package/dist/core/texcoord.js +14 -0
- package/dist/core/topology.d.ts +22 -0
- package/dist/core/topology.js +70 -7
- package/dist/core/traceimage.d.ts +7 -0
- package/dist/core/traceimage.js +221 -0
- package/dist/core/tritri.d.ts +5 -0
- package/dist/core/tritri.js +120 -0
- package/dist/core/uv.d.ts +13 -0
- package/dist/core/uv.js +3 -3
- package/dist/core/uvreport.d.ts +66 -0
- package/dist/core/uvreport.js +308 -0
- package/dist/core/xatlas.js +32 -2
- package/dist/gallery/app/gallery.css +100 -0
- package/dist/gallery/app/gallery.js +80 -0
- package/dist/gallery/bake.d.ts +34 -0
- package/dist/gallery/bake.js +144 -0
- package/dist/gallery/cli.d.ts +4 -0
- package/dist/gallery/cli.js +157 -0
- package/dist/gallery/library.d.ts +124 -0
- package/dist/gallery/library.js +366 -0
- package/dist/gallery/pages.d.ts +94 -0
- package/dist/gallery/pages.js +148 -0
- package/dist/gallery/server.d.ts +21 -0
- package/dist/gallery/server.js +391 -0
- package/dist/graph/engine.js +14 -0
- package/dist/graph/types.d.ts +17 -0
- package/dist/host/artifacts.d.ts +21 -0
- package/dist/host/artifacts.js +58 -0
- package/dist/host/export-targets.d.ts +33 -0
- package/dist/host/export-targets.js +84 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/dist/inspect.d.ts +6 -1
- package/dist/inspect.js +13 -1
- package/dist/io/common.d.ts +8 -0
- package/dist/io/gltf.js +236 -19
- package/dist/io/targets.d.ts +31 -0
- package/dist/io/targets.js +75 -0
- package/dist/mcp/docs.d.ts +1 -1
- package/dist/mcp/docs.js +73 -6
- package/dist/mcp/library.d.ts +12 -0
- package/dist/mcp/library.js +159 -0
- package/dist/mcp/server.js +29 -12
- package/dist/mcp/tools.d.ts +1 -1
- package/dist/mcp/tools.js +102 -43
- package/dist/nodes/bake.js +56 -36
- package/dist/nodes/bevel.js +4 -3
- package/dist/nodes/code.js +33 -7
- package/dist/nodes/curves.js +7 -0
- package/dist/nodes/generate.js +1 -1
- package/dist/nodes/imageops.d.ts +1 -0
- package/dist/nodes/imageops.js +206 -0
- package/dist/nodes/index.d.ts +3 -0
- package/dist/nodes/index.js +3 -0
- package/dist/nodes/material.js +3 -0
- package/dist/nodes/measure.js +3 -2
- package/dist/nodes/rbd.js +1 -1
- package/dist/nodes/scene.d.ts +1 -0
- package/dist/nodes/scene.js +53 -0
- package/dist/nodes/solidify.d.ts +1 -0
- package/dist/nodes/solidify.js +52 -0
- package/dist/nodes/transform.js +5 -1
- package/dist/nodes/util.d.ts +1 -1
- package/dist/nodes/wasmcall.js +1 -1
- package/dist/render/options.js +1 -1
- package/dist/render/pbr.d.ts +28 -0
- package/dist/render/pbr.js +378 -0
- package/dist/render/preview.d.ts +58 -0
- package/dist/render/preview.js +24 -0
- package/dist/render/raster.d.ts +12 -2
- package/dist/render/raster.js +98 -11
- package/dist/render/texview.d.ts +1 -1
- package/dist/render/texview.js +7 -2
- package/dist/viewer/app/camera.js +27 -7
- package/dist/viewer/app/embed.js +80 -0
- package/dist/viewer/app/export-popover.js +68 -0
- package/dist/viewer/app/grid.js +1 -1
- package/dist/viewer/app/guides.js +1 -1
- package/dist/viewer/app/image-view.js +114 -0
- package/dist/viewer/app/keymap.js +15 -3
- package/dist/viewer/app/layers.js +24 -5
- package/dist/viewer/app/main.js +71 -15
- package/dist/viewer/app/markers.js +1 -1
- package/dist/viewer/app/outliner.js +56 -15
- package/dist/viewer/app/outputs.css +73 -0
- package/dist/viewer/app/outputs.js +240 -0
- package/dist/viewer/app/props.js +1 -1
- package/dist/viewer/app/scene.js +6 -2
- package/dist/viewer/app/select.js +14 -3
- package/dist/viewer/app/selection.js +1 -1
- package/dist/viewer/app/status.js +20 -0
- package/dist/viewer/app/style.css +2 -1
- package/dist/viewer/app/units.js +1 -1
- package/dist/viewer/app/vars.js +1 -1
- package/dist/viewer/index.html +20 -6
- package/dist/viewer/outputs.d.ts +54 -0
- package/dist/viewer/outputs.js +436 -0
- package/dist/viewer/server.d.ts +19 -2
- package/dist/viewer/server.js +77 -9
- package/dist/workspace.d.ts +21 -2
- package/dist/workspace.js +124 -4
- package/kernels/pkg/README.md +21 -28
- package/kernels/pkg-simd/README.md +21 -28
- package/package.json +11 -4
package/dist/render/raster.js
CHANGED
|
@@ -19,6 +19,7 @@ import { LabelPlacer, ambientOcclusion, dotOv, drawLabel, drawLegend, lineOv } f
|
|
|
19
19
|
import { withHeightfieldPreview } from '../core/heightfield.js';
|
|
20
20
|
import { withSdfGridPreview } from '../core/sdfgrid.js';
|
|
21
21
|
import { drawTextureView, textureSampler } from './texview.js';
|
|
22
|
+
import { pbrScene, preparePBR } from './pbr.js';
|
|
22
23
|
/** View presets accepted by parseViews (MCP render "views", CLI --views). */
|
|
23
24
|
export const VIEW_PRESETS = {
|
|
24
25
|
quad: ['persp', 'front', 'right', 'top'],
|
|
@@ -48,7 +49,7 @@ export function parseViews(spec) {
|
|
|
48
49
|
const AXES = ['x', 'y', 'z'];
|
|
49
50
|
const WIRE_MODES = ['none', 'all', 'hard', 'boundary', 'groups'];
|
|
50
51
|
const GLYPH_MODES = ['none', 'N', 'up', 'orient', 'v', 'custom'];
|
|
51
|
-
const SHADING_MODES = ['default', 'flat', 'matcap', 'ao'];
|
|
52
|
+
const SHADING_MODES = ['default', 'flat', 'matcap', 'ao', 'pbr'];
|
|
52
53
|
const CUT_C = [0.93, 0.4, 0.3], CUT_FLIP_C = [0.25, 0.38, 0.9];
|
|
53
54
|
const ORTHO = {
|
|
54
55
|
front: { f: [0, 0, -1], r: [1, 0, 0], u: [0, 1, 0], label: 'FRONT X> Y^' },
|
|
@@ -101,7 +102,16 @@ function makeCam(name, bmin, bmax, W, H, o, kind = name, azDeg, secAxis = 1) {
|
|
|
101
102
|
const s = (o.orthoScale && o.orthoScale > 0 ? W / o.orthoScale : Math.min((W / 2) * 0.86 / Math.max(halfW, 1e-6), (H / 2) * 0.82 / Math.max(halfH, 1e-6))) * zoom;
|
|
102
103
|
return { name, persp: false, eye: c, f: v.f, r: v.r, u: v.u, c, focal: 0, scale: s };
|
|
103
104
|
}
|
|
104
|
-
/**
|
|
105
|
+
/**
|
|
106
|
+
* Render with the asynchronous preparation shading 'pbr' needs (MikkTSpace, texture files through readFile, usually
|
|
107
|
+
* Host.readFile); every other mode renders exactly as renderGeometry does.
|
|
108
|
+
*/
|
|
109
|
+
export async function renderGeometryAsync(g, o = {}, readFile) {
|
|
110
|
+
if (o.shading === 'pbr' && !o.pbrTextures)
|
|
111
|
+
o = { ...o, pbrTextures: await preparePBR(g, readFile) };
|
|
112
|
+
return renderGeometry(g, o);
|
|
113
|
+
}
|
|
114
|
+
/** Render the geometry; returns RGBA pixels. Shading 'pbr' needs renderGeometryAsync first (or MikkTSpace loaded). */
|
|
105
115
|
export function renderGeometry(g, o = {}) {
|
|
106
116
|
// packed instances are drawn expanded (within a triangle budget), spline curves as polylines, see core/packed.ts
|
|
107
117
|
if (g.hasPacked() || g.hasSplines()) {
|
|
@@ -331,18 +341,24 @@ export function renderGeometry(g, o = {}) {
|
|
|
331
341
|
out[i + 2] = 0.13;
|
|
332
342
|
}
|
|
333
343
|
// --- uv data (uv view, checker shading)
|
|
334
|
-
const
|
|
344
|
+
const materialNames = primMaterials(g), materialTable = readMaterials(g);
|
|
345
|
+
const hasMasks = Object.values(materialTable).some((m) => m.alphaMode === 'MASK');
|
|
346
|
+
const uvd = views.includes('uv') || o.checker || o.textures || hasMasks ? readUV(g) : null;
|
|
335
347
|
const checkerOn = !!(o.checker && uvd);
|
|
336
348
|
if (o.checker && !uvd)
|
|
337
349
|
notes.push('checker: no uv attribute (run uv_project, uv_unwrap or uv_flatten first)');
|
|
338
350
|
// base colour maps (image textures) on the mesh: texture × Cd (as glTF multiplies COLOR_0), off under checker / colour-by
|
|
339
|
-
const
|
|
351
|
+
const showTextureColor = !!(o.textures && !checkerOn && !o.colorBy && !o.highlight);
|
|
352
|
+
const texS = uvd && (showTextureColor || hasMasks) ? textureSampler(g, notes) : null;
|
|
340
353
|
if (o.textures && !uvd && nTri)
|
|
341
354
|
notes.push('textures: no uv attribute, maps not shown');
|
|
342
355
|
else if (o.textures && uvd && !texS && !checkerOn)
|
|
343
356
|
notes.push('textures: no material with an image base colour (texture_to_material)');
|
|
344
357
|
const texCd = !!(texS && g.findAttrib('Cd', ['vertex', 'point', 'prim', 'detail']));
|
|
345
|
-
const texRGB = [0, 0, 0];
|
|
358
|
+
const texRGB = [0, 0, 0, 1];
|
|
359
|
+
const alphaAttr = resolveAttr('Alpha');
|
|
360
|
+
const alphaOf = (v) => alphaAttr && alphaAttr.attr.type !== 'string' && !alphaAttr.attr.isArray
|
|
361
|
+
? alphaAttr.attr.data[elemIndex(alphaAttr.cls, v) * alphaAttr.attr.size] : 1;
|
|
346
362
|
// ---------------------------------------------------------------- opt-in overlays, shared by all views
|
|
347
363
|
const ovDesc = []; // footer line 2: what is switched on
|
|
348
364
|
if (pscale)
|
|
@@ -376,7 +392,17 @@ export function renderGeometry(g, o = {}) {
|
|
|
376
392
|
// shading
|
|
377
393
|
const flatN = shadingMode === 'flat' && nTri ? primNormals(g) : null;
|
|
378
394
|
const matcap = shadingMode === 'matcap';
|
|
379
|
-
|
|
395
|
+
let pbrS = null;
|
|
396
|
+
if (shadingMode === 'pbr' && nTri) {
|
|
397
|
+
const ev = o.exposure ?? 0;
|
|
398
|
+
if (!Number.isFinite(ev))
|
|
399
|
+
throw new Error(`render: exposure expects a number of stops, got ${o.exposure}`);
|
|
400
|
+
pbrS = pbrScene(g, tris, triPrim, o.pbrTextures, ev, notes);
|
|
401
|
+
if (o.colorBy || o.highlight || o.checker)
|
|
402
|
+
notes.push('pbr: color_by, highlight and checker are not shown under shading "pbr"');
|
|
403
|
+
ovDesc.push(`SHADING PBR STUDIO EV ${fmtNum(ev)} (${pbrS.summary.toUpperCase()})`);
|
|
404
|
+
}
|
|
405
|
+
else if (shadingMode)
|
|
380
406
|
ovDesc.push(`SHADING ${shadingMode.toUpperCase()}`);
|
|
381
407
|
// section / clip planes
|
|
382
408
|
const planeAt = (k, off, abs) => (abs ? off ?? 0 : bmin[k] + (bmax[k] - bmin[k]) * (off ?? 0.5));
|
|
@@ -783,6 +809,8 @@ export function renderGeometry(g, o = {}) {
|
|
|
783
809
|
}
|
|
784
810
|
}
|
|
785
811
|
const cam = makeCam(vname, bmin, bmax, W, H, o, kind, azDeg, secK);
|
|
812
|
+
if (pbrS)
|
|
813
|
+
pbrS.camera(cam.r, cam.u, cam.f);
|
|
786
814
|
// project points
|
|
787
815
|
const sx = new Float32Array(np), sy = new Float32Array(np), cz = new Float32Array(np), vz = new Float32Array(np);
|
|
788
816
|
const project = (x, y, z, outp) => {
|
|
@@ -873,6 +901,7 @@ export function renderGeometry(g, o = {}) {
|
|
|
873
901
|
const na = [0, 0, 0], nb = [0, 0, 0], nc = [0, 0, 0];
|
|
874
902
|
const tn = [0, 0, 0];
|
|
875
903
|
const chk = [0, 0, 0];
|
|
904
|
+
const pbrUV0 = [0, 0], pbrUV1 = [0, 0], pbrUV2 = [0, 0], pbrV = [0, 0, 0], pbrRGB = [0, 0, 0];
|
|
876
905
|
const bf = vi === backView ? new Uint8Array(W * H) : null;
|
|
877
906
|
for (let t = 0; t < nTri; t++) {
|
|
878
907
|
const va = tris[t * 3], vb = tris[t * 3 + 1], vc = tris[t * 3 + 2];
|
|
@@ -903,7 +932,9 @@ export function renderGeometry(g, o = {}) {
|
|
|
903
932
|
const fnw = cross3(e1, e2);
|
|
904
933
|
const toCam = cam.persp ? [cam.eye[0] - Pd[a * 3], cam.eye[1] - Pd[a * 3 + 1], cam.eye[2] - Pd[a * 3 + 2]] : [-cam.f[0], -cam.f[1], -cam.f[2]];
|
|
905
934
|
const back = dot3(fnw, toCam) < 0;
|
|
906
|
-
const
|
|
935
|
+
const material = materialNames ? materialTable[materialNames[triPrim[t]]] : undefined;
|
|
936
|
+
const alphaMask = material?.alphaMode === 'MASK';
|
|
937
|
+
const suspicious = back && !material?.doubleSided && conn !== null && closedPiece[conn.primClass[triPrim[t]]] === 1;
|
|
907
938
|
if (flatN) {
|
|
908
939
|
const p = triPrim[t] * 3;
|
|
909
940
|
na[0] = nb[0] = nc[0] = flatN[p];
|
|
@@ -930,6 +961,8 @@ export function renderGeometry(g, o = {}) {
|
|
|
930
961
|
}
|
|
931
962
|
const za = cz[a], zb2 = cz[b], zc = cz[c];
|
|
932
963
|
const inv = 1 / area;
|
|
964
|
+
// shading 'pbr': screen-space gradients of the affine weights (uv derivatives for the texture level of detail)
|
|
965
|
+
const g0x = (by - cy) * inv, g0y = (cx - bx) * inv, g1x = (cy - ay) * inv, g1y = (ax - cx) * inv;
|
|
933
966
|
// base colour map: mip level from the triangle's texels per screen pixel (render/texview.ts)
|
|
934
967
|
const texP = texS && texS.has(triPrim[t]) ? triPrim[t] : -1;
|
|
935
968
|
let texLod = 0;
|
|
@@ -962,15 +995,69 @@ export function renderGeometry(g, o = {}) {
|
|
|
962
995
|
if ((p0 * qa + p1 * qb + p2 * qc - cC) * cS > 0)
|
|
963
996
|
continue;
|
|
964
997
|
}
|
|
965
|
-
|
|
966
|
-
if (bf)
|
|
967
|
-
bf[zi] = suspicious ? 1 : 0;
|
|
998
|
+
const aw0 = w0, aw1 = w1;
|
|
968
999
|
if (cam.persp) {
|
|
969
1000
|
const s = w0 * za + w1 * zb2 + w2 * zc;
|
|
970
1001
|
w0 = (w0 * za) / s;
|
|
971
1002
|
w1 = (w1 * zb2) / s;
|
|
972
1003
|
w2 = 1 - w0 - w1;
|
|
973
1004
|
}
|
|
1005
|
+
if (alphaMask) {
|
|
1006
|
+
let alpha = material.alpha * (alphaOf(va) * w0 + alphaOf(vb) * w1 + alphaOf(vc) * w2);
|
|
1007
|
+
if (texP >= 0) {
|
|
1008
|
+
texS.sample(texP, uvd[va * 2] * w0 + uvd[vb * 2] * w1 + uvd[vc * 2] * w2, uvd[va * 2 + 1] * w0 + uvd[vb * 2 + 1] * w1 + uvd[vc * 2 + 1] * w2, texLod, texRGB);
|
|
1009
|
+
alpha *= texRGB[3];
|
|
1010
|
+
}
|
|
1011
|
+
if (alpha < (material.alphaCutoff ?? 0.5))
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
zb[zi] = depth;
|
|
1015
|
+
if (bf)
|
|
1016
|
+
bf[zi] = suspicious ? 1 : 0;
|
|
1017
|
+
if (pbrS) {
|
|
1018
|
+
let dudx = 0, dvdx = 0, dudy = 0, dvdy = 0;
|
|
1019
|
+
if (pbrS.hasUV) {
|
|
1020
|
+
const at = (q0, q1, out) => {
|
|
1021
|
+
let p0 = q0, p1 = q1;
|
|
1022
|
+
if (cam.persp) {
|
|
1023
|
+
const s = q0 * za + q1 * zb2 + (1 - q0 - q1) * zc;
|
|
1024
|
+
p0 = (q0 * za) / s;
|
|
1025
|
+
p1 = (q1 * zb2) / s;
|
|
1026
|
+
}
|
|
1027
|
+
pbrS.uvAt(t, p0, p1, 1 - p0 - p1, out);
|
|
1028
|
+
};
|
|
1029
|
+
at(aw0, aw1, pbrUV0);
|
|
1030
|
+
at(aw0 + g0x, aw1 + g1x, pbrUV1);
|
|
1031
|
+
at(aw0 + g0y, aw1 + g1y, pbrUV2);
|
|
1032
|
+
dudx = pbrUV1[0] - pbrUV0[0];
|
|
1033
|
+
dvdx = pbrUV1[1] - pbrUV0[1];
|
|
1034
|
+
dudy = pbrUV2[0] - pbrUV0[0];
|
|
1035
|
+
dvdy = pbrUV2[1] - pbrUV0[1];
|
|
1036
|
+
}
|
|
1037
|
+
if (cam.persp) {
|
|
1038
|
+
const px3 = Pd[a * 3] * w0 + Pd[b * 3] * w1 + Pd[c * 3] * w2, py3 = Pd[a * 3 + 1] * w0 + Pd[b * 3 + 1] * w1 + Pd[c * 3 + 1] * w2, pz3 = Pd[a * 3 + 2] * w0 + Pd[b * 3 + 2] * w1 + Pd[c * 3 + 2] * w2;
|
|
1039
|
+
const vx = cam.eye[0] - px3, vy = cam.eye[1] - py3, vz2 = cam.eye[2] - pz3, vl = Math.hypot(vx, vy, vz2) || 1;
|
|
1040
|
+
pbrV[0] = vx / vl;
|
|
1041
|
+
pbrV[1] = vy / vl;
|
|
1042
|
+
pbrV[2] = vz2 / vl;
|
|
1043
|
+
}
|
|
1044
|
+
else {
|
|
1045
|
+
pbrV[0] = -cam.f[0];
|
|
1046
|
+
pbrV[1] = -cam.f[1];
|
|
1047
|
+
pbrV[2] = -cam.f[2];
|
|
1048
|
+
}
|
|
1049
|
+
pbrS.shade(t, w0, w1, w2, dudx, dvdx, dudy, dvdy, pbrV, back, pbrRGB);
|
|
1050
|
+
const ci = zi * 3;
|
|
1051
|
+
if (suspicious) {
|
|
1052
|
+
pbrRGB[0] = pbrRGB[0] * 0.35 + 0.12;
|
|
1053
|
+
pbrRGB[1] = pbrRGB[1] * 0.35 + 0.22;
|
|
1054
|
+
pbrRGB[2] = pbrRGB[2] * 0.35 + 0.6;
|
|
1055
|
+
}
|
|
1056
|
+
col[ci] = pbrRGB[0];
|
|
1057
|
+
col[ci + 1] = pbrRGB[1];
|
|
1058
|
+
col[ci + 2] = pbrRGB[2];
|
|
1059
|
+
continue;
|
|
1060
|
+
}
|
|
974
1061
|
tn[0] = ca[0] * w0 + cb[0] * w1 + cc[0] * w2;
|
|
975
1062
|
tn[1] = ca[1] * w0 + cb[1] * w1 + cc[1] * w2;
|
|
976
1063
|
tn[2] = ca[2] * w0 + cb[2] * w1 + cc[2] * w2;
|
|
@@ -1011,7 +1098,7 @@ export function renderGeometry(g, o = {}) {
|
|
|
1011
1098
|
fb = fb * 0.35 + 0.6;
|
|
1012
1099
|
}
|
|
1013
1100
|
}
|
|
1014
|
-
else if (texP >= 0) {
|
|
1101
|
+
else if (texP >= 0 && showTextureColor) {
|
|
1015
1102
|
texS.sample(texP, uvd[va * 2] * w0 + uvd[vb * 2] * w1 + uvd[vc * 2] * w2, uvd[va * 2 + 1] * w0 + uvd[vb * 2 + 1] * w1 + uvd[vc * 2 + 1] * w2, texLod, texRGB);
|
|
1016
1103
|
if (texCd) {
|
|
1017
1104
|
fr *= texRGB[0];
|
package/dist/render/texview.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare function drawTextureView(g: Geometry, col: Float32Array, W: numbe
|
|
|
12
12
|
* texture view does. Anything else (points, prims, other volumes) is returned as it is.
|
|
13
13
|
*/
|
|
14
14
|
export declare function withImagePreview(g: Geometry, max?: number): Geometry;
|
|
15
|
-
/** Base colour maps per prim for persp/ortho views: sample
|
|
15
|
+
/** Base colour maps per prim for persp/ortho views: sample fills out with RGBA (grey has alpha 1). */
|
|
16
16
|
export interface TextureSampler {
|
|
17
17
|
/** true when prim p shows a base colour map */
|
|
18
18
|
has(p: number): boolean;
|
package/dist/render/texview.js
CHANGED
|
@@ -7,6 +7,7 @@ import { Geometry } from '../core/geometry.js';
|
|
|
7
7
|
import { Image, channelsOf, imageRef, isImage, referenceableImages, resizeLayer } from '../core/image.js';
|
|
8
8
|
import { MATERIAL_ATTRIB, normalizeMaterial, primMaterials, readMaterials, writeMaterials } from '../core/materials.js';
|
|
9
9
|
import { drawText, textWidth } from './font.js';
|
|
10
|
+
import { imageRowOfV } from '../core/texcoord.js';
|
|
10
11
|
const SLOT_LABEL = { baseColor: 'BASE COLOR', normal: 'NORMAL', roughness: 'ROUGHNESS', orm: 'ORM' };
|
|
11
12
|
/**
|
|
12
13
|
* What the texture view shows for a spec: "" = every image (at most 16); an image name = that image (all its layers
|
|
@@ -221,11 +222,15 @@ export function textureSampler(g, notes) {
|
|
|
221
222
|
sample(p, u, v, lod, out) {
|
|
222
223
|
const chain = perPrim[p];
|
|
223
224
|
const m = chain[Math.max(0, Math.min(chain.length - 1, Math.round(lod)))];
|
|
224
|
-
const x = u * m.w - 0.5, y = (
|
|
225
|
+
const x = u * m.w - 0.5, y = imageRowOfV(v, m.h) - 0.5;
|
|
225
226
|
const x0 = Math.floor(x), y0 = Math.floor(y), tx = x - x0, ty = y - y0;
|
|
226
227
|
const i0 = wrap(x0, m.w), i1 = wrap(x0 + 1, m.w), j0 = wrap(y0, m.h), j1 = wrap(y0 + 1, m.h);
|
|
227
228
|
const d = m.data, ch = m.ch;
|
|
228
|
-
for (let c = 0; c <
|
|
229
|
+
for (let c = 0; c < 4; c++) {
|
|
230
|
+
if (ch === 1 && c === 3) {
|
|
231
|
+
out[c] = 1;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
229
234
|
const cc = ch === 4 ? c : 0;
|
|
230
235
|
const a = d[(j0 * m.w + i0) * ch + cc], b = d[(j0 * m.w + i1) * ch + cc], e = d[(j1 * m.w + i0) * ch + cc], f = d[(j1 * m.w + i1) * ch + cc];
|
|
231
236
|
const t = a + (b - a) * tx, s = e + (f - e) * tx;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Cameras and views
|
|
1
|
+
// Cameras and views: our own controller, no OrbitControls. Tumble around a pivot,
|
|
2
2
|
// track, dolly towards a point, no inertia: the camera moves only while the person drags, scrolls or frames. Seven
|
|
3
3
|
// named cameras (persp and the axis views); one view, or four in a 2×2 quad, each showing one of them with its own
|
|
4
4
|
// shading. Orthographic cameras cannot tumble. Near and far planes follow the scene size (the renderer keeps a
|
|
@@ -37,6 +37,7 @@ class Cam {
|
|
|
37
37
|
this.name = name;
|
|
38
38
|
this.ortho = name !== 'persp';
|
|
39
39
|
this.target = new THREE.Vector3();
|
|
40
|
+
this.pivot = new THREE.Vector3();
|
|
40
41
|
this.quat = name === 'persp' ? perspQuat() : quatOf(AXIS_VIEWS[name].f, AXIS_VIEWS[name].u);
|
|
41
42
|
this.dist = 12;
|
|
42
43
|
this.halfH = 6;
|
|
@@ -45,12 +46,14 @@ class Cam {
|
|
|
45
46
|
get right() { return new THREE.Vector3(1, 0, 0).applyQuaternion(this.quat); }
|
|
46
47
|
get up() { return new THREE.Vector3(0, 1, 0).applyQuaternion(this.quat); }
|
|
47
48
|
get eye() { return this.target.clone().addScaledVector(this.back, this.dist); }
|
|
48
|
-
toJSON() { return { ortho: this.ortho, target: this.target.toArray(), quat: this.quat.toArray(), dist: this.dist, halfH: this.halfH }; }
|
|
49
|
+
toJSON() { return { ortho: this.ortho, target: this.target.toArray(), pivot: this.pivot.toArray(), quat: this.quat.toArray(), dist: this.dist, halfH: this.halfH }; }
|
|
49
50
|
load(j) {
|
|
50
51
|
if (!j) return;
|
|
51
52
|
const ok = (a, n) => Array.isArray(a) && a.length === n && a.every(Number.isFinite);
|
|
52
53
|
if (typeof j.ortho === 'boolean') this.ortho = j.ortho;
|
|
53
54
|
if (ok(j.target, 3)) this.target.fromArray(j.target);
|
|
55
|
+
this.pivot.copy(this.target);
|
|
56
|
+
if (ok(j.pivot, 3)) this.pivot.fromArray(j.pivot);
|
|
54
57
|
if (ok(j.quat, 4)) this.quat.fromArray(j.quat).normalize();
|
|
55
58
|
if (j.dist > 0) this.dist = Math.min(MAX_D, Math.max(MIN_D, j.dist));
|
|
56
59
|
if (j.halfH > 0) this.halfH = Math.min(MAX_D, Math.max(MIN_D, j.halfH));
|
|
@@ -126,10 +129,10 @@ export function createViews(scene) {
|
|
|
126
129
|
return { x: r.x + ((q.x + 1) / 2) * r.w, y: r.y + ((1 - q.y) / 2) * r.h };
|
|
127
130
|
},
|
|
128
131
|
/** The point under a viewport point on the plane through the slot's target facing the camera. */
|
|
129
|
-
targetPlanePoint(i, x, y) {
|
|
132
|
+
targetPlanePoint(i, x, y, at) {
|
|
130
133
|
const c = v.cam(i), rc = v.ray(i, x, y), n = c.back;
|
|
131
134
|
const p = new THREE.Vector3();
|
|
132
|
-
return rc.ray.intersectPlane(new THREE.Plane().setFromNormalAndCoplanarPoint(n, c.target), p) ? p : c.target.clone();
|
|
135
|
+
return rc.ray.intersectPlane(new THREE.Plane().setFromNormalAndCoplanarPoint(n, at ?? c.target), p) ? p : (at ?? c.target).clone();
|
|
133
136
|
},
|
|
134
137
|
|
|
135
138
|
// ---------------------------------------------------------------- navigation
|
|
@@ -149,11 +152,26 @@ export function createViews(scene) {
|
|
|
149
152
|
return true;
|
|
150
153
|
},
|
|
151
154
|
/** Track: the point at the target's depth follows the mouse. */
|
|
152
|
-
track(i, dx, dy) {
|
|
153
|
-
const c = v.cam(i)
|
|
154
|
-
c.
|
|
155
|
+
track(i, dx, dy, at) {
|
|
156
|
+
const c = v.cam(i);
|
|
157
|
+
const depth = at && !c.ortho ? Math.max(MIN_D, c.eye.clone().sub(at).dot(c.back)) : c.dist;
|
|
158
|
+
const k = v.worldPerPixel(i) * (c.ortho ? 1 : depth / c.dist);
|
|
159
|
+
const delta = c.right.multiplyScalar(-dx * k).addScaledVector(c.up, dy * k);
|
|
160
|
+
c.target.add(delta); c.pivot.add(delta);
|
|
155
161
|
v.placed = true;
|
|
156
162
|
},
|
|
163
|
+
/** Pick a navigation pivot without changing the camera's eye, aim or distance. */
|
|
164
|
+
anchor(i, hit) {
|
|
165
|
+
const c = v.cam(i);
|
|
166
|
+
if (hit && hit.toArray().every(Number.isFinite) && c.eye.clone().sub(hit).dot(c.back) > MIN_D) c.pivot.copy(hit);
|
|
167
|
+
return c.pivot.clone();
|
|
168
|
+
},
|
|
169
|
+
zoomAt(i, factor, at) {
|
|
170
|
+
const c = v.cam(i), before = c.ortho ? c.halfH : c.dist;
|
|
171
|
+
v.dolly(i, factor, at);
|
|
172
|
+
const scale = (c.ortho ? c.halfH : c.dist) / before;
|
|
173
|
+
c.pivot.sub(at).multiplyScalar(scale).add(at);
|
|
174
|
+
},
|
|
157
175
|
/** Dolly by factor s (< 1 closer) towards `at` (a world point on the target plane; default the target). */
|
|
158
176
|
dolly(i, s, at) {
|
|
159
177
|
const c = v.cam(i);
|
|
@@ -171,6 +189,7 @@ export function createViews(scene) {
|
|
|
171
189
|
const center = box.getCenter(new THREE.Vector3()), size = box.getSize(new THREE.Vector3());
|
|
172
190
|
const radius = Math.max(size.length() / 2, 1e-3);
|
|
173
191
|
c.target.copy(center);
|
|
192
|
+
c.pivot.copy(center);
|
|
174
193
|
const half = (vfov(aspect) * Math.PI) / 360, halfW = Math.atan(Math.tan(half) * aspect);
|
|
175
194
|
c.dist = Math.min(MAX_D, Math.max(MIN_D, (radius / Math.sin(Math.min(half, halfW))) * 1.05));
|
|
176
195
|
// ortho: the box's extent across the view
|
|
@@ -201,6 +220,7 @@ export function createViews(scene) {
|
|
|
201
220
|
// the new view looks at the same place at the same scale
|
|
202
221
|
const n = cams.get(name);
|
|
203
222
|
n.target.copy(c.target); n.halfH = c.ortho ? c.halfH : c.dist * 0.3;
|
|
223
|
+
n.pivot.copy(c.pivot);
|
|
204
224
|
slots[i].cam = name;
|
|
205
225
|
}
|
|
206
226
|
},
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// The embed page (src/gallery/pages.ts embedPage): a baked preview.glb under an orbit camera, made to sit in an iframe
|
|
2
|
+
// on a board. Not the editor viewport: no panels, no diagnostic overlays, and three's
|
|
3
|
+
// OrbitControls (left drag orbits, right drag pans, wheel zooms, as model viewers do), slowly turning until touched.
|
|
4
|
+
// The thumbnail behind the canvas stays visible until the model is in, and when it cannot load.
|
|
5
|
+
import * as THREE from 'three';
|
|
6
|
+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
7
|
+
import { parseGLB, createLights } from './scene.js';
|
|
8
|
+
|
|
9
|
+
const canvas = document.getElementById('gl');
|
|
10
|
+
const note = document.getElementById('note');
|
|
11
|
+
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
|
|
12
|
+
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
|
|
13
|
+
renderer.setClearColor(0x2b2d31, 1);
|
|
14
|
+
const scene = new THREE.Scene();
|
|
15
|
+
const camera = new THREE.PerspectiveCamera(35, 1, 0.01, 1000);
|
|
16
|
+
const controls = new OrbitControls(camera, canvas);
|
|
17
|
+
controls.enableDamping = true;
|
|
18
|
+
controls.autoRotate = true;
|
|
19
|
+
controls.autoRotateSpeed = 0.6;
|
|
20
|
+
controls.addEventListener('start', () => { controls.autoRotate = false; });
|
|
21
|
+
const lights = createLights(scene);
|
|
22
|
+
|
|
23
|
+
let dirty = true;
|
|
24
|
+
controls.addEventListener('change', () => { dirty = true; });
|
|
25
|
+
const resize = () => {
|
|
26
|
+
const w = canvas.clientWidth, h = canvas.clientHeight;
|
|
27
|
+
if (!w || !h) return;
|
|
28
|
+
renderer.setSize(w, h, false);
|
|
29
|
+
camera.aspect = w / h;
|
|
30
|
+
camera.updateProjectionMatrix();
|
|
31
|
+
dirty = true;
|
|
32
|
+
};
|
|
33
|
+
new ResizeObserver(resize).observe(canvas);
|
|
34
|
+
|
|
35
|
+
/** Frame the bounding sphere from the thumbnail's angle (35° around, 25° up). */
|
|
36
|
+
function frame(root) {
|
|
37
|
+
const box = new THREE.Box3().setFromObject(root);
|
|
38
|
+
if (box.isEmpty()) return;
|
|
39
|
+
const sphere = box.getBoundingSphere(new THREE.Sphere());
|
|
40
|
+
const r = Math.max(sphere.radius, 1e-3);
|
|
41
|
+
const dist = r / Math.sin(THREE.MathUtils.degToRad(camera.fov / 2)) * 1.05;
|
|
42
|
+
const az = THREE.MathUtils.degToRad(35), el = THREE.MathUtils.degToRad(25);
|
|
43
|
+
camera.position.copy(sphere.center).add(new THREE.Vector3(Math.sin(az) * Math.cos(el), Math.sin(el), Math.cos(az) * Math.cos(el)).multiplyScalar(dist));
|
|
44
|
+
camera.near = dist / 1000;
|
|
45
|
+
camera.far = dist * 100;
|
|
46
|
+
camera.updateProjectionMatrix();
|
|
47
|
+
controls.target.copy(sphere.center);
|
|
48
|
+
controls.minDistance = r * 0.05;
|
|
49
|
+
controls.maxDistance = dist * 20;
|
|
50
|
+
controls.update();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function load() {
|
|
54
|
+
try {
|
|
55
|
+
const res = await fetch(canvas.dataset.src);
|
|
56
|
+
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
|
|
57
|
+
const root = await parseGLB(await res.arrayBuffer());
|
|
58
|
+
root.traverse((o) => {
|
|
59
|
+
// a point's size in pixels, as in the editor, not in metres
|
|
60
|
+
if (o.isPoints) { o.material.size = 3; o.material.sizeAttenuation = false; }
|
|
61
|
+
if (o.isMesh) for (const m of [o.material].flat()) m.side = THREE.DoubleSide;
|
|
62
|
+
});
|
|
63
|
+
scene.add(root);
|
|
64
|
+
resize();
|
|
65
|
+
frame(root);
|
|
66
|
+
document.getElementById('poster').hidden = true;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
canvas.hidden = true;
|
|
69
|
+
note.textContent = `3D preview unavailable (${e.message})`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
renderer.setAnimationLoop(() => {
|
|
74
|
+
const moving = controls.update() || controls.autoRotate;
|
|
75
|
+
if (!dirty && !moving) return;
|
|
76
|
+
lights.follow(camera, controls.target);
|
|
77
|
+
renderer.render(scene, camera);
|
|
78
|
+
dirty = false;
|
|
79
|
+
});
|
|
80
|
+
load();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The export target popover: a format and a file name inside exports/, opened by an output's Export button when it
|
|
2
|
+
// has no target yet, or to change it. Submitting stores the target in the graph and exports.
|
|
3
|
+
const el = (tag, cls, text) => { const e = document.createElement(tag); if (cls) e.className = cls; if (text !== undefined) e.textContent = text; return e; };
|
|
4
|
+
|
|
5
|
+
/** A file name stem like the server's (io/targets.ts fileStem). */
|
|
6
|
+
export const fileStem = (s) => String(s).replace(/[^A-Za-z0-9_.-]+/g, '_').replace(/^\.+|[. ]+$/g, '') || 'output';
|
|
7
|
+
|
|
8
|
+
/** The path a format writes to by default: <stem><extension>, a folder for formats without one. */
|
|
9
|
+
export function defaultPath(stem, format, formats) {
|
|
10
|
+
return fileStem(stem) + (formats.find((f) => f.format === format)?.extension ?? '');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The path with its extension swapped from one format's to another's (a custom name keeps its stem). */
|
|
14
|
+
export function swapExtension(p, from, to, formats) {
|
|
15
|
+
const ext = (f) => formats.find((x) => x.format === f)?.extension ?? '';
|
|
16
|
+
const a = ext(from), b = ext(to);
|
|
17
|
+
const stem = a && p.toLowerCase().endsWith(a.toLowerCase()) ? p.slice(0, -a.length) : p;
|
|
18
|
+
return stem + b;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Open the popover under `anchor`. item: an output entry (label, kind, graph, output, target); formats: [{format, label,
|
|
23
|
+
* extension}] from /api/outputs; directory: the exports folder. Resolves to {format, path} or null (cancelled).
|
|
24
|
+
*/
|
|
25
|
+
export function exportPopover({ anchor, item, formats, directory }) {
|
|
26
|
+
document.querySelector('.export-popover')?.dispatchEvent(new Event('cancel'));
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
const stem = item.output ?? item.graph ?? 'output';
|
|
29
|
+
const startFormat = item.target?.format ?? (item.kind === 'image' ? 'textures' : 'glb');
|
|
30
|
+
const box = el('form', 'export-popover');
|
|
31
|
+
box.setAttribute('role', 'dialog'); box.setAttribute('aria-label', `Export ${item.label}`);
|
|
32
|
+
const title = el('div', 'export-title', item.target ? `Export target of ${item.label}` : `Export ${item.label}`);
|
|
33
|
+
const format = el('select'); format.setAttribute('aria-label', 'Format');
|
|
34
|
+
for (const f of formats) format.append(new Option(f.label, f.format));
|
|
35
|
+
format.value = startFormat;
|
|
36
|
+
const file = el('input'); file.type = 'text'; file.spellcheck = false; file.setAttribute('aria-label', 'File in exports/');
|
|
37
|
+
file.value = item.target?.path ?? defaultPath(stem, startFormat, formats);
|
|
38
|
+
const where = el('div', 'export-where dim');
|
|
39
|
+
const error = el('div', 'export-error err');
|
|
40
|
+
const cancel = el('button', '', 'Cancel'), go = el('button', 'primary', 'Export');
|
|
41
|
+
cancel.type = 'button'; go.type = 'submit';
|
|
42
|
+
const field = (label, input) => { const l = el('label', 'export-field'); l.append(el('span', 'dim', label), input); return l; };
|
|
43
|
+
const actions = el('div', 'export-actions'); actions.append(cancel, go);
|
|
44
|
+
box.append(title, field('Format', format), field('File', file), where, error, actions);
|
|
45
|
+
let previous = format.value;
|
|
46
|
+
const sync = () => { where.textContent = `${directory}/${file.value.trim() || '…'}`; error.textContent = ''; };
|
|
47
|
+
format.onchange = () => { file.value = swapExtension(file.value.trim(), previous, format.value, formats); previous = format.value; sync(); };
|
|
48
|
+
file.oninput = sync;
|
|
49
|
+
const close = (value) => { box.remove(); removeEventListener('pointerdown', outside, true); anchor?.focus?.(); resolve(value); };
|
|
50
|
+
const outside = (e) => { if (!box.contains(e.target)) close(null); };
|
|
51
|
+
box.addEventListener('cancel', () => close(null));
|
|
52
|
+
cancel.onclick = () => close(null);
|
|
53
|
+
box.onkeydown = (e) => { if (e.key === 'Escape') { e.preventDefault(); close(null); } };
|
|
54
|
+
box.onsubmit = (e) => {
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
const p = file.value.trim().replace(/\\/g, '/');
|
|
57
|
+
if (!p || p.startsWith('/') || /^[A-Za-z]:/.test(p) || p.split('/').some((s) => !s || s === '.' || s === '..')) { error.textContent = 'a path inside exports/, without ".." or a drive'; file.focus(); return; }
|
|
58
|
+
close({ format: format.value, path: p });
|
|
59
|
+
};
|
|
60
|
+
sync();
|
|
61
|
+
document.body.append(box);
|
|
62
|
+
const r = anchor.getBoundingClientRect();
|
|
63
|
+
box.style.left = Math.max(6, Math.min(r.left, innerWidth - box.offsetWidth - 6)) + 'px';
|
|
64
|
+
box.style.top = (r.bottom + 4 + box.offsetHeight < innerHeight ? r.bottom + 4 : Math.max(6, r.top - box.offsetHeight - 4)) + 'px';
|
|
65
|
+
addEventListener('pointerdown', outside, true);
|
|
66
|
+
format.focus();
|
|
67
|
+
});
|
|
68
|
+
}
|
package/dist/viewer/app/grid.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The adaptive metre grid, the world axes, the axis gizmo and each view's readouts
|
|
1
|
+
// The adaptive metre grid, the world axes, the axis gizmo and each view's readouts.
|
|
2
2
|
// The grid lies on y = 0 under persp and the top / bottom views, and on each side view's own plane (front: XY,
|
|
3
3
|
// right: ZY), as Maya and Houdini draw them; minor lines every `step`, major every 10 steps, the axes in the
|
|
4
4
|
// standard colours. One plane is set up again before each view renders.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Guides
|
|
1
|
+
// Guides: nulls, curves, areas and volumes, the markers agents read as
|
|
2
2
|
// inputs/markers.json (roles site, path, zone, volume). Drawn as the standard objects they are, in one desaturated
|
|
3
3
|
// cyan, on top of the scene: a null is an axis cross (its radius circle only while hovered or selected), a curve a
|
|
4
4
|
// polyline with CV squares and a direction tick, an area a closed curve with a light diagonal hatch, a volume a wire
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// A 2D image inspector, independent of the 3D camera. View state is remembered per output.
|
|
2
|
+
export function channelPixels(source, mode) {
|
|
3
|
+
const pixels = new Uint8ClampedArray(source);
|
|
4
|
+
const c = ['R', 'G', 'B', 'A'].indexOf(mode);
|
|
5
|
+
for (let i = 0; i < pixels.length; i += 4) {
|
|
6
|
+
if (c >= 0) pixels[i] = pixels[i + 1] = pixels[i + 2] = source[i + c];
|
|
7
|
+
if (mode !== 'RGBA') pixels[i + 3] = 255;
|
|
8
|
+
}
|
|
9
|
+
return pixels;
|
|
10
|
+
}
|
|
11
|
+
export function zoomImage(state, x, y, factor) {
|
|
12
|
+
const scale = Math.max(0.01, Math.min(64, state.scale * factor)), ratio = scale / state.scale;
|
|
13
|
+
return { scale, x: x - (x - state.x) * ratio, y: y - (y - state.y) * ratio };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createImageView(host, message) {
|
|
17
|
+
const panel = document.createElement('section'); panel.id = 'image-view'; panel.hidden = true;
|
|
18
|
+
panel.innerHTML = `<div class="image-tools"><button data-act="fit">Fit</button><button data-act="actual">100%</button>
|
|
19
|
+
<select aria-label="Image channel">${['RGB', 'RGBA', 'R', 'G', 'B', 'A'].map((m) => `<option>${m}</option>`).join('')}</select>
|
|
20
|
+
<select aria-label="Image layer"></select><label><input type="checkbox" data-act="checker" checked> Checkerboard</label>
|
|
21
|
+
<label><input type="checkbox" data-act="uv"> UV</label><span class="image-dim"></span></div>
|
|
22
|
+
<div class="image-stage checker"><canvas aria-label="Image preview"></canvas><div class="image-error" hidden></div></div>`;
|
|
23
|
+
host.append(panel);
|
|
24
|
+
const stage = panel.querySelector('.image-stage'), canvas = panel.querySelector('canvas'), context = canvas.getContext('2d');
|
|
25
|
+
const mode = panel.querySelector('[aria-label="Image channel"]'), layers = panel.querySelector('[aria-label="Image layer"]');
|
|
26
|
+
const uvToggle = panel.querySelector('[data-act="uv"]'), dim = panel.querySelector('.image-dim'), error = panel.querySelector('.image-error');
|
|
27
|
+
const memory = new Map();
|
|
28
|
+
let item = null, bitmap = null, data = null, raster = document.createElement('canvas'), segments = [], sequence = 0;
|
|
29
|
+
let state = { scale: 1, x: 0, y: 0 }, drag = null, fitNew = true;
|
|
30
|
+
function remember() { if (item) memory.set(item.id, { ...state, mode: mode.value, layer: layers.value }); }
|
|
31
|
+
function draw() {
|
|
32
|
+
if (panel.hidden) return;
|
|
33
|
+
const dpr = Math.min(2, devicePixelRatio || 1), w = stage.clientWidth, h = stage.clientHeight;
|
|
34
|
+
canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr);
|
|
35
|
+
context.setTransform(dpr, 0, 0, dpr, 0, 0); context.clearRect(0, 0, w, h);
|
|
36
|
+
if (!bitmap) return;
|
|
37
|
+
context.save(); context.translate(state.x, state.y); context.scale(state.scale, state.scale);
|
|
38
|
+
context.imageSmoothingEnabled = state.scale < 1;
|
|
39
|
+
context.drawImage(raster, 0, 0);
|
|
40
|
+
if (uvToggle.checked && segments.length) {
|
|
41
|
+
context.beginPath();
|
|
42
|
+
for (const [u, v, s, t] of segments) { context.moveTo(u * raster.width, (1 - v) * raster.height); context.lineTo(s * raster.width, (1 - t) * raster.height); }
|
|
43
|
+
context.strokeStyle = '#f0a030'; context.lineWidth = 1 / state.scale; context.stroke();
|
|
44
|
+
}
|
|
45
|
+
context.restore(); dim.textContent = `${raster.width} × ${raster.height} · ${Math.round(state.scale * 100)}%`;
|
|
46
|
+
}
|
|
47
|
+
function fit() {
|
|
48
|
+
if (!bitmap) return;
|
|
49
|
+
state.scale = Math.min((stage.clientWidth - 32) / raster.width, (stage.clientHeight - 32) / raster.height);
|
|
50
|
+
state.scale = Math.max(0.01, state.scale);
|
|
51
|
+
state.x = (stage.clientWidth - raster.width * state.scale) / 2; state.y = (stage.clientHeight - raster.height * state.scale) / 2;
|
|
52
|
+
remember(); draw();
|
|
53
|
+
}
|
|
54
|
+
function applyChannels() {
|
|
55
|
+
if (!data) return;
|
|
56
|
+
raster.getContext('2d').putImageData(new ImageData(channelPixels(data.data, item.generated ? 'RGBA' : mode.value), data.width, data.height), 0, 0);
|
|
57
|
+
draw();
|
|
58
|
+
}
|
|
59
|
+
async function load() {
|
|
60
|
+
if (!item) return;
|
|
61
|
+
const seq = ++sequence, current = item;
|
|
62
|
+
error.hidden = true;
|
|
63
|
+
const q = new URLSearchParams({ id: current.id, layer: layers.value, mode: mode.value, rev: current.revision });
|
|
64
|
+
const url = current.sourceURL ?? (current.generated ? '/api/output/image?' + q : '/api/output/file?' + q);
|
|
65
|
+
try {
|
|
66
|
+
const response = await fetch(url);
|
|
67
|
+
if (!response.ok) throw new Error((await response.json()).error);
|
|
68
|
+
const next = await createImageBitmap(await response.blob(), { premultiplyAlpha: 'none' });
|
|
69
|
+
if (seq !== sequence) { next.close(); return; }
|
|
70
|
+
bitmap?.close(); bitmap = next;
|
|
71
|
+
raster.width = next.width; raster.height = next.height;
|
|
72
|
+
const c = raster.getContext('2d', { willReadFrequently: true }); c.drawImage(next, 0, 0);
|
|
73
|
+
data = c.getImageData(0, 0, next.width, next.height); applyChannels();
|
|
74
|
+
if (fitNew) { fitNew = false; fit(); }
|
|
75
|
+
if (uvToggle.checked) await loadUV();
|
|
76
|
+
} catch (e) { if (seq === sequence) { error.textContent = e.message; error.hidden = false; message(e.message, 'err'); } }
|
|
77
|
+
}
|
|
78
|
+
async function loadUV() {
|
|
79
|
+
if (!item?.generated || !uvToggle.checked) { segments = []; draw(); return; }
|
|
80
|
+
const id = item.id;
|
|
81
|
+
try {
|
|
82
|
+
const r = await fetch('/api/output/uv?' + new URLSearchParams({ id })), j = await r.json();
|
|
83
|
+
if (item?.id !== id) return;
|
|
84
|
+
if (!r.ok) throw new Error(j.error);
|
|
85
|
+
segments = j.segments; draw();
|
|
86
|
+
} catch (e) { message(e.message, 'err'); }
|
|
87
|
+
}
|
|
88
|
+
panel.querySelector('[data-act="fit"]').onclick = fit;
|
|
89
|
+
panel.querySelector('[data-act="actual"]').onclick = () => { state = zoomImage(state, stage.clientWidth / 2, stage.clientHeight / 2, 1 / state.scale); remember(); draw(); };
|
|
90
|
+
panel.querySelector('[data-act="checker"]').onchange = (e) => stage.classList.toggle('checker', e.target.checked);
|
|
91
|
+
mode.onchange = () => { remember(); if (item?.generated) load(); else applyChannels(); };
|
|
92
|
+
layers.onchange = () => { remember(); load(); };
|
|
93
|
+
uvToggle.onchange = loadUV;
|
|
94
|
+
canvas.onpointerdown = (e) => { if (![0, 1, 2].includes(e.button)) return; e.preventDefault(); canvas.setPointerCapture(e.pointerId); drag = { id: e.pointerId, x: e.clientX, y: e.clientY }; };
|
|
95
|
+
canvas.onpointermove = (e) => { if (!drag || e.pointerId !== drag.id) return; state.x += e.clientX - drag.x; state.y += e.clientY - drag.y; drag.x = e.clientX; drag.y = e.clientY; draw(); };
|
|
96
|
+
const end = () => { drag = null; remember(); };
|
|
97
|
+
canvas.onpointerup = end; canvas.onpointercancel = end; canvas.onlostpointercapture = end; addEventListener('blur', end);
|
|
98
|
+
canvas.oncontextmenu = (e) => e.preventDefault();
|
|
99
|
+
canvas.addEventListener('wheel', (e) => { e.preventDefault(); const r = canvas.getBoundingClientRect(); const delta = e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? 400 : 1); state = zoomImage(state, e.clientX - r.left, e.clientY - r.top, Math.exp(Math.max(-1, Math.min(1, -delta * 0.0015)))); remember(); draw(); }, { passive: false });
|
|
100
|
+
new ResizeObserver(draw).observe(stage);
|
|
101
|
+
return {
|
|
102
|
+
show(next) {
|
|
103
|
+
const same = item?.id === next.id; if (!same) remember(); item = next; panel.hidden = false; host.classList.add('image-mode');
|
|
104
|
+
if (!same) { bitmap?.close(); bitmap = null; data = null; segments = []; uvToggle.checked = false; dim.textContent = 'Loading…'; }
|
|
105
|
+
const saved = memory.get(next.id); if (!same) { state = saved ? { scale: saved.scale, x: saved.x, y: saved.y } : { scale: 1, x: 0, y: 0 }; fitNew = !saved; }
|
|
106
|
+
layers.replaceChildren(...(next.layers ?? ['Image']).map((l) => new Option(l, l)));
|
|
107
|
+
layers.value = next.layers?.includes(saved?.layer) ? saved.layer : next.layer ?? 'Image'; layers.hidden = !next.layers?.length;
|
|
108
|
+
mode.value = saved?.mode ?? (next.generated ? 'RGB' : 'RGBA'); uvToggle.disabled = !next.generated;
|
|
109
|
+
load();
|
|
110
|
+
},
|
|
111
|
+
hide() { remember(); sequence++; item = null; bitmap?.close(); bitmap = null; data = null; panel.hidden = true; host.classList.remove('image-mode'); },
|
|
112
|
+
get visible() { return !panel.hidden; },
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
// The keymap
|
|
2
|
-
// Blender). Each row carries the text the overlay prints per preset and
|
|
1
|
+
// The keymap: one table of actions, four presets (TopoTopo, the default;
|
|
2
|
+
// Maya, Houdini, Blender). Each row carries the text the overlay prints per preset and the built actions' bindings:
|
|
3
3
|
// chord → action. Transform and snapping rows (phase 2) ship their text but no bindings, so a preset never changes
|
|
4
4
|
// shape later. Plain data and functions: the overlay is the only DOM code, and it runs only when called.
|
|
5
5
|
|
|
6
6
|
export const PRESETS = [
|
|
7
|
+
{ id: 'topotopo', label: 'TopoTopo' },
|
|
7
8
|
{ id: 'maya', label: 'Maya' },
|
|
8
9
|
{ id: 'houdini', label: 'Houdini' },
|
|
9
10
|
{ id: 'blender', label: 'Blender' },
|
|
10
11
|
];
|
|
11
|
-
export const DEFAULT_PRESET = '
|
|
12
|
+
export const DEFAULT_PRESET = 'topotopo';
|
|
12
13
|
|
|
13
14
|
const place = { 'Shift+1': 'placeNull', 'Shift+2': 'placeCurve', 'Shift+3': 'placeArea', 'Shift+4': 'placeVolume' };
|
|
14
15
|
const curve = { Enter: 'finish', Backspace: 'removeLast', Esc: 'cancel' };
|
|
@@ -57,6 +58,17 @@ export const ROWS = [
|
|
|
57
58
|
{ label: 'keymap overlay', maya: ['?', { keys: { '?': 'overlay' } }], houdini: ['?', { keys: { '?': 'overlay' } }], blender: ['?', { keys: { '?': 'overlay' } }] },
|
|
58
59
|
];
|
|
59
60
|
|
|
61
|
+
// Editing shortcuts follow Maya; navigation is available without held modifiers.
|
|
62
|
+
const topo = {
|
|
63
|
+
tumble: ['Right drag (pan in orthographic)', { drag: { R: 'tumble' } }],
|
|
64
|
+
track: ['Middle drag', { drag: { M: 'track' } }],
|
|
65
|
+
dolly: ['Wheel toward cursor'],
|
|
66
|
+
perspective: ['1', { keys: { 1: 'viewPersp' } }],
|
|
67
|
+
'top / front / right': ['2 / 3 / 4', { keys: { 2: 'viewTop', 3: 'viewFront', 4: 'viewRight' } }],
|
|
68
|
+
'wire / shaded / shaded + wire': ['5 / 6 / 7', { keys: { 5: 'displayWire', 6: 'displayShaded', 7: 'displayShadedWire' } }],
|
|
69
|
+
};
|
|
70
|
+
for (const row of ROWS) row.topotopo = topo[row.label] ?? row.maya;
|
|
71
|
+
|
|
60
72
|
/** Every action a binding may name (main.js handles each). */
|
|
61
73
|
export const ACTIONS = [
|
|
62
74
|
'tumble', 'track', 'dolly', 'frameSelection', 'frameAll', 'quad', 'viewPersp', 'viewTop', 'viewFront', 'viewRight', 'ortho',
|