topotopo 0.5.2 → 0.5.3

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.
@@ -25,7 +25,7 @@ import { heightfieldPreviewStep, isHeightfield, layerStats, withHeightfieldPrevi
25
25
  import { withSdfGridPreview } from '../core/sdfgrid.js';
26
26
  import { drawTextureView, textureSampler, withMaterialPreview } from './texview.js';
27
27
  import { EMPTY_ATTRIB } from '../core/scene.js';
28
- import { pbrScene, preparePBR } from './pbr.js';
28
+ import { ALPHA_BLEND, ALPHA_MASK, pbrScene, preparePBR } from './pbr.js';
29
29
  import { loadMikkTSpace } from '../core/tangents.js';
30
30
  import { decodePNG } from '../io/png-decode.js';
31
31
  /**
@@ -69,6 +69,8 @@ const SHADING_MODES = ['default', 'flat', 'matcap', 'ao', 'pbr'];
69
69
  /** The automatic wireframe draws polygon edges up to this many polygons. */
70
70
  export const AUTO_WIRE_MAX = 6000;
71
71
  const CUT_C = [0.93, 0.4, 0.3], CUT_FLIP_C = [0.25, 0.38, 0.9], SOCKET_C = [0.35, 1, 0.72];
72
+ /** Barycentric tolerance of the blend pass's shared-edge rule. */
73
+ const EDGE_EPS = 1e-9;
72
74
  /** Margins of the fit: the bounds fill this fraction of a view's width and height (room for the label and gizmo). */
73
75
  const FIT_W = 0.9, FIT_H = 0.84;
74
76
  /** Whether a zoom above 1 may crop the bounds: only when the person framed the view (look_at or ortho_scale). */
@@ -208,6 +210,7 @@ function makeCam(name, bmin, bmax, W, H, o, kind = name, azDeg, secAxis, pts) {
208
210
  * Host.readFile); every other mode renders exactly as renderGeometry does.
209
211
  */
210
212
  export async function renderGeometryAsync(g, o = {}, readFile) {
213
+ o = greyShading(o);
211
214
  if (o.shading === 'pbr' && !o.pbrTextures)
212
215
  o = { ...o, pbrTextures: await preparePBR(g, readFile) };
213
216
  if (o.underlay && !o.underlayImage)
@@ -250,8 +253,17 @@ export async function readUnderlay(p, readFile) {
250
253
  }
251
254
  return { width: img.width, height: img.height, rgba };
252
255
  }
256
+ /** grey is a pbr value check: it implies shading 'pbr' and refuses another shading. */
257
+ function greyShading(o) {
258
+ if (!o.grey || o.shading === 'pbr')
259
+ return o;
260
+ if (o.shading && o.shading !== 'default')
261
+ throw new Error(`render: grey is a value check of shading "pbr" (given shading "${o.shading}"); leave shading out or set it to "pbr"`);
262
+ return { ...o, shading: 'pbr' };
263
+ }
253
264
  /** Render the geometry; returns RGBA pixels. Shading 'pbr' needs renderGeometryAsync first (or MikkTSpace loaded). */
254
265
  export function renderGeometry(g, o = {}) {
266
+ o = greyShading(o);
255
267
  // packed instances are drawn expanded (within a triangle budget), spline curves as polylines, see core/packed.ts
256
268
  if (g.hasPacked() || g.hasSplines()) {
257
269
  const budget = o.maxTriangles !== undefined && o.maxTriangles > 0 ? Math.min(o.maxTriangles, 50_000_000) : undefined;
@@ -443,7 +455,8 @@ export function renderGeometry(g, o = {}) {
443
455
  if (f.cls === 'point')
444
456
  pointColorOf = (p) => get(p);
445
457
  }
446
- // material base colours where there is no colour (no Cd, or Houdini's default white), as glTF export does
458
+ // material base colours where there is no colour (no Cd, or Houdini's default white); an inspection view, so Cd
459
+ // stays visible here (shading 'pbr' follows the export rule: a named material wins)
447
460
  const pm = primMaterials(g), table = pm ? readMaterials(g) : null;
448
461
  if (pm && table && Object.keys(table).length) {
449
462
  const cd = colorOf, hasCd = !!(f && f.attr.type === 'float');
@@ -593,15 +606,29 @@ export function renderGeometry(g, o = {}) {
593
606
  const flatN = shadingMode === 'flat' && nTri ? primNormals(g) : null;
594
607
  const matcap = shadingMode === 'matcap';
595
608
  let pbrS = null;
609
+ const pbrBlend = [];
596
610
  if (shadingMode === 'pbr' && nTri) {
597
611
  const ev = o.exposure ?? 0;
598
612
  if (!Number.isFinite(ev))
599
613
  throw new Error(`render: exposure expects a number of stops, got ${o.exposure}`);
600
- pbrS = pbrScene(g, tris, triPrim, o.pbrTextures, ev, notes);
614
+ pbrS = pbrScene(g, tris, triPrim, o.pbrTextures, ev, notes, { vertexColors: o.vertexColors, grey: !!o.grey });
601
615
  // color_by and highlight tint the shaded material (their colour, lit by its brightness); the checker is not shown
602
616
  if (o.checker)
603
617
  notes.push('pbr: checker is not shown under shading "pbr" (color_by and highlight tint the material)');
604
- ovDesc.push(`SHADING PBR STUDIO EV ${fmtNum(ev)} (${pbrS.summary.toUpperCase()})`);
618
+ const cdMode = o.vertexColors && o.vertexColors !== 'auto' ? ` CD ${o.vertexColors.toUpperCase()}` : '';
619
+ ovDesc.push(`SHADING PBR${o.grey ? ' GREY 18%' : ''} STUDIO EV ${fmtNum(ev)}${cdMode} (${pbrS.summary.toUpperCase()})`);
620
+ // blend triangles grouped by prim, for the back-to-front pass of each view
621
+ const byPrim = new Map();
622
+ for (let t = 0; t < nTri; t++)
623
+ if (pbrS.triAlpha[t] === ALPHA_BLEND) {
624
+ const p = triPrim[t];
625
+ let l = byPrim.get(p);
626
+ if (!l)
627
+ byPrim.set(p, (l = []));
628
+ l.push(t);
629
+ }
630
+ for (const [prim, ts] of byPrim)
631
+ pbrBlend.push({ prim, tris: ts });
605
632
  }
606
633
  else if (shadingMode)
607
634
  ovDesc.push(`SHADING ${shadingMode.toUpperCase()}`);
@@ -994,7 +1021,7 @@ export function renderGeometry(g, o = {}) {
994
1021
  // downsample a finished tile into the output, draw the output-resolution overlays (post), label it. Transparent
995
1022
  // 3D views (zb given): alpha = the covered fraction of each pixel's samples and the colour of those samples alone;
996
1023
  // whatever post and the label draw is opaque
997
- const blit = (label, post, zb) => {
1024
+ const blit = (label, post, zb, bA) => {
998
1025
  const inv = 1 / (ss * ss);
999
1026
  const cover = !!(outA && zb);
1000
1027
  for (let y = 0; y < TH; y++)
@@ -1012,6 +1039,12 @@ export function renderGeometry(g, o = {}) {
1012
1039
  cg += col[k + 1];
1013
1040
  cb += col[k + 2];
1014
1041
  }
1042
+ else if (cover && bA && bA[zi] > 0) {
1043
+ n += bA[zi];
1044
+ cr += col[k];
1045
+ cg += col[k + 1];
1046
+ cb += col[k + 2];
1047
+ } // premultiplied
1015
1048
  }
1016
1049
  const o1 = (ty + y) * outW + tx + x, k = o1 * 3;
1017
1050
  if (!cover) {
@@ -1182,13 +1215,19 @@ export function renderGeometry(g, o = {}) {
1182
1215
  const na = [0, 0, 0], nb = [0, 0, 0], nc = [0, 0, 0];
1183
1216
  const tn = [0, 0, 0];
1184
1217
  const chk = [0, 0, 0];
1185
- const pbrUV0 = [0, 0], pbrUV1 = [0, 0], pbrUV2 = [0, 0], pbrV = [0, 0, 0], pbrRGB = [0, 0, 0];
1218
+ const pbrUV0 = [0, 0], pbrUV1 = [0, 0], pbrUV2 = [0, 0], pbrV = [0, 0, 0], pbrRGB = [0, 0, 0, 1];
1186
1219
  const bf = vi === backView ? new Uint8Array(W * H) : null, bfPrim = bf ? new Int32Array(W * H) : null;
1187
- for (let t = 0; t < nTri; t++) {
1220
+ // shading 'pbr' blend materials: drawn after the opaque pass (blendPass), depth-tested without writing depth and
1221
+ // composited over what is behind; on a transparent background, bA holds their coverage of empty samples (col
1222
+ // premultiplied there)
1223
+ const bA = pbrBlend.length && transparent ? new Float32Array(W * H) : null;
1224
+ const drawTri = (t, blendPass) => {
1225
+ if (pbrS && (pbrS.triAlpha[t] === ALPHA_BLEND) !== blendPass)
1226
+ return;
1188
1227
  const va = tris[t * 3], vb = tris[t * 3 + 1], vc = tris[t * 3 + 2];
1189
1228
  const a = g.vertexPoint[va], b = g.vertexPoint[vb], c = g.vertexPoint[vc];
1190
1229
  if (cam.persp && (vz[a] < 1e-4 || vz[b] < 1e-4 || vz[c] < 1e-4))
1191
- continue;
1230
+ return;
1192
1231
  let partial = false, qa = 0, qb = 0, qc = 0;
1193
1232
  if (clipOn) {
1194
1233
  qa = Pd[a * 3 + cK];
@@ -1196,17 +1235,17 @@ export function renderGeometry(g, o = {}) {
1196
1235
  qc = Pd[c * 3 + cK];
1197
1236
  const ra = (qa - cC) * cS > 0, rb = (qb - cC) * cS > 0, rc = (qc - cC) * cS > 0;
1198
1237
  if (ra && rb && rc)
1199
- continue;
1238
+ return;
1200
1239
  partial = ra || rb || rc;
1201
1240
  }
1202
1241
  const ax = sx[a], ay = sy[a], bx = sx[b], by = sy[b], cx = sx[c], cy = sy[c];
1203
1242
  const area = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
1204
1243
  if (Math.abs(area) < 1e-12)
1205
- continue;
1244
+ return;
1206
1245
  let minX = Math.max(0, Math.floor(Math.min(ax, bx, cx))), maxX = Math.min(W - 1, Math.ceil(Math.max(ax, bx, cx)));
1207
1246
  let minY = Math.max(0, Math.floor(Math.min(ay, by, cy))), maxY = Math.min(H - 1, Math.ceil(Math.max(ay, by, cy)));
1208
1247
  if (minX > maxX || minY > maxY)
1209
- continue;
1248
+ return;
1210
1249
  // facing (world space)
1211
1250
  const e1 = [Pd[b * 3] - Pd[a * 3], Pd[b * 3 + 1] - Pd[a * 3 + 1], Pd[b * 3 + 2] - Pd[a * 3 + 2]];
1212
1251
  const e2 = [Pd[c * 3] - Pd[a * 3], Pd[c * 3 + 1] - Pd[a * 3 + 1], Pd[c * 3 + 2] - Pd[a * 3 + 2]];
@@ -1214,7 +1253,9 @@ export function renderGeometry(g, o = {}) {
1214
1253
  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]];
1215
1254
  const back = dot3(fnw, toCam) < 0;
1216
1255
  const material = materialNames ? materialTable[materialNames[triPrim[t]]] : undefined;
1217
- const alphaMask = material?.alphaMode === 'MASK';
1256
+ // shading 'pbr' evaluates its own coverage (base colour maps of any source, vertex Alpha as exported)
1257
+ const alphaMask = !pbrS && material?.alphaMode === 'MASK';
1258
+ const pbrMask = !!pbrS && pbrS.triAlpha[t] === ALPHA_MASK, pbrCut = pbrMask ? pbrS.cutoff(t) : 0;
1218
1259
  const suspicious = back && !material?.doubleSided && solid !== null && closedPiece[pieceOf[triPrim[t]]] === 1;
1219
1260
  if (flatN) {
1220
1261
  const p = triPrim[t] * 3;
@@ -1242,6 +1283,17 @@ export function renderGeometry(g, o = {}) {
1242
1283
  colC = tint(colC);
1243
1284
  }
1244
1285
  const za = cz[a], zb2 = cz[b], zc = cz[c];
1286
+ // blend pass: a pixel centre on an edge shared by two triangles is drawn by exactly one of them (the one whose
1287
+ // opposite vertex lies on a fixed side of the edge), so shared edges are not composited twice
1288
+ const owns = (p0x, p0y, p1x, p1y, rx, ry) => {
1289
+ let nx = p0y - p1y, ny = p1x - p0x;
1290
+ if (nx < 0 || (nx === 0 && ny < 0)) {
1291
+ nx = -nx;
1292
+ ny = -ny;
1293
+ }
1294
+ return nx * (rx - p0x) + ny * (ry - p0y) > 0;
1295
+ };
1296
+ const own0 = blendPass && owns(bx, by, cx, cy, ax, ay), own1 = blendPass && owns(cx, cy, ax, ay, bx, by), own2 = blendPass && owns(ax, ay, bx, by, cx, cy);
1245
1297
  const inv = 1 / area;
1246
1298
  // shading 'pbr': screen-space gradients of the affine weights (uv derivatives for the texture level of detail)
1247
1299
  const g0x = (by - cy) * inv, g0y = (cx - bx) * inv, g1x = (cy - ay) * inv, g1y = (ax - cx) * inv;
@@ -1259,7 +1311,13 @@ export function renderGeometry(g, o = {}) {
1259
1311
  let w0 = ((bx - xx) * (cy - yy) - (by - yy) * (cx - xx)) * inv;
1260
1312
  let w1 = ((cx - xx) * (ay - yy) - (cy - yy) * (ax - xx)) * inv;
1261
1313
  let w2 = 1 - w0 - w1;
1262
- if (w0 < 0 || w1 < 0 || w2 < 0)
1314
+ if (blendPass) {
1315
+ if (w0 < -EDGE_EPS || w1 < -EDGE_EPS || w2 < -EDGE_EPS)
1316
+ continue;
1317
+ if ((w0 < EDGE_EPS && !own0) || (w1 < EDGE_EPS && !own1) || (w2 < EDGE_EPS && !own2))
1318
+ continue;
1319
+ }
1320
+ else if (w0 < 0 || w1 < 0 || w2 < 0)
1263
1321
  continue;
1264
1322
  const depth = w0 * za + w1 * zb2 + w2 * zc;
1265
1323
  const zi = py * W + px;
@@ -1293,11 +1351,6 @@ export function renderGeometry(g, o = {}) {
1293
1351
  if (alpha < (material.alphaCutoff ?? 0.5))
1294
1352
  continue;
1295
1353
  }
1296
- zb[zi] = depth;
1297
- if (bf) {
1298
- bf[zi] = suspicious ? 1 : 0;
1299
- bfPrim[zi] = triPrim[t];
1300
- }
1301
1354
  if (pbrS) {
1302
1355
  let dudx = 0, dvdx = 0, dudy = 0, dvdy = 0;
1303
1356
  if (pbrS.hasUV) {
@@ -1331,6 +1384,15 @@ export function renderGeometry(g, o = {}) {
1331
1384
  pbrV[2] = -cam.f[2];
1332
1385
  }
1333
1386
  pbrS.shade(t, w0, w1, w2, dudx, dvdx, dudy, dvdy, pbrV, back, pbrRGB);
1387
+ if (pbrMask && pbrRGB[3] < pbrCut)
1388
+ continue;
1389
+ if (!blendPass) {
1390
+ zb[zi] = depth;
1391
+ if (bf) {
1392
+ bf[zi] = suspicious ? 1 : 0;
1393
+ bfPrim[zi] = triPrim[t];
1394
+ }
1395
+ }
1334
1396
  const ci = zi * 3;
1335
1397
  if (pbrTint) {
1336
1398
  // the attribute colour lit by the material's brightness, over a third of the material itself
@@ -1343,11 +1405,27 @@ export function renderGeometry(g, o = {}) {
1343
1405
  pbrRGB[1] = pbrRGB[1] * 0.35 + 0.22;
1344
1406
  pbrRGB[2] = pbrRGB[2] * 0.35 + 0.6;
1345
1407
  }
1408
+ if (blendPass) {
1409
+ const al = pbrRGB[3];
1410
+ if (bA && zb[zi] === -Infinity) {
1411
+ if (bA[zi] === 0)
1412
+ col[ci] = col[ci + 1] = col[ci + 2] = 0;
1413
+ bA[zi] = al + bA[zi] * (1 - al);
1414
+ }
1415
+ for (let k = 0; k < 3; k++)
1416
+ col[ci + k] = pbrRGB[k] * al + col[ci + k] * (1 - al);
1417
+ continue;
1418
+ }
1346
1419
  col[ci] = pbrRGB[0];
1347
1420
  col[ci + 1] = pbrRGB[1];
1348
1421
  col[ci + 2] = pbrRGB[2];
1349
1422
  continue;
1350
1423
  }
1424
+ zb[zi] = depth;
1425
+ if (bf) {
1426
+ bf[zi] = suspicious ? 1 : 0;
1427
+ bfPrim[zi] = triPrim[t];
1428
+ }
1351
1429
  tn[0] = ca[0] * w0 + cb[0] * w1 + cc[0] * w2;
1352
1430
  tn[1] = ca[1] * w0 + cb[1] * w1 + cc[1] * w2;
1353
1431
  tn[2] = ca[2] * w0 + cb[2] * w1 + cc[2] * w2;
@@ -1411,6 +1489,17 @@ export function renderGeometry(g, o = {}) {
1411
1489
  col[ci + 2] = aces(fb * k + spec + rim);
1412
1490
  }
1413
1491
  }
1492
+ };
1493
+ for (let t = 0; t < nTri; t++)
1494
+ drawTri(t, false);
1495
+ if (pbrBlend.length) {
1496
+ // blend prims from back to front by their centroid's depth along the view axis
1497
+ const pc = centroids();
1498
+ const depthOf = (p) => (pc[p * 3] - cam.eye[0]) * cam.f[0] + (pc[p * 3 + 1] - cam.eye[1]) * cam.f[1] + (pc[p * 3 + 2] - cam.eye[2]) * cam.f[2];
1499
+ const order = pbrBlend.map((b) => ({ b, d: depthOf(b.prim) })).sort((x, y) => y.d - x.d || x.b.prim - y.b.prim);
1500
+ for (const { b } of order)
1501
+ for (const t of b.tris)
1502
+ drawTri(t, true);
1414
1503
  }
1415
1504
  if (bf)
1416
1505
  for (let i = 0; i < W * H; i++)
@@ -1781,7 +1870,7 @@ export function renderGeometry(g, o = {}) {
1781
1870
  for (let y = 0; y < TH; y++)
1782
1871
  out.set(tb.subarray(y * TW * 3, (y + 1) * TW * 3), ((ty + y) * outW + tx) * 3);
1783
1872
  };
1784
- blit(viewLabel, post, zb);
1873
+ blit(viewLabel, post, zb, bA);
1785
1874
  });
1786
1875
  // footer
1787
1876
  const fy = outH - footerH + 4 * ui;
package/dist/session.d.ts CHANGED
@@ -145,11 +145,24 @@ export interface SessionPackages {
145
145
  revision(): string;
146
146
  /**
147
147
  * while this session follows working folders (for looking): the scope exports cook in, resolving packages to their
148
- * published copies; null while nothing is followed
148
+ * published copies, those in `follow` (an export told to use their working folders) to their working folders; null
149
+ * while nothing is followed but those
149
150
  */
150
- published?(): Scope | null;
151
- /** what an export of the graph made while following says: the published versions it used (null: nothing to say) */
152
- publishedNote?(graph: GraphSpec): string | null;
151
+ published?(follow?: readonly string[]): Scope | null;
152
+ /**
153
+ * what an export of the graph made while following says: the working folders it used (`follow`) and the published
154
+ * versions it used for the others (null: nothing to say)
155
+ */
156
+ publishedNote?(graph: GraphSpec, follow?: readonly string[]): string | null;
157
+ /** the followed packages the graph names that an export not following them cannot cook (no published version in use) */
158
+ unpublished?(graph: GraphSpec, follow?: readonly string[]): {
159
+ package: string;
160
+ dir: string;
161
+ }[];
162
+ /** the packages this session follows */
163
+ following?(): {
164
+ package: string;
165
+ }[];
153
166
  }
154
167
  export declare class Session {
155
168
  dir: string;
@@ -287,11 +300,13 @@ export declare class Session {
287
300
  * Cook a graph (its output, or `target`) with its session values, as a cook of `kind`. Session.stop and `signal`
288
301
  * stop it at a node boundary; activity() reports it while it runs. A cook of the whole graph records its outcome
289
302
  * (lastCook) and, unless it was stopped, its report (lastReport), when no cook started after it has done so first.
290
- * An export cooks with the published copies of the packages the session shows live, and records nothing.
303
+ * An export cooks with the published copies of the packages the session shows live (the working folders of those
304
+ * `follow` names), and records nothing.
291
305
  */
292
306
  cook(name: string, target?: string, frame?: number, opts?: {
293
307
  kind?: CookKind;
294
308
  signal?: StopSignal;
309
+ follow?: readonly string[];
295
310
  }): Promise<CookReport>;
296
311
  /** the folder each watcher follows; a folder that is missing has none until it is made again */
297
312
  private watchers;
package/dist/session.js CHANGED
@@ -661,7 +661,8 @@ export class Session {
661
661
  * Cook a graph (its output, or `target`) with its session values, as a cook of `kind`. Session.stop and `signal`
662
662
  * stop it at a node boundary; activity() reports it while it runs. A cook of the whole graph records its outcome
663
663
  * (lastCook) and, unless it was stopped, its report (lastReport), when no cook started after it has done so first.
664
- * An export cooks with the published copies of the packages the session shows live, and records nothing.
664
+ * An export cooks with the published copies of the packages the session shows live (the working folders of those
665
+ * `follow` names), and records nothing.
665
666
  */
666
667
  async cook(name, target, frame, opts = {}) {
667
668
  const g = this.getGraph(name);
@@ -686,8 +687,9 @@ export class Session {
686
687
  this.notifyActivity(name);
687
688
  try {
688
689
  const s = this.sessions.get(name);
689
- // an export uses the published copies of the packages this session shows live (SessionPackages.published)
690
- const scope = run.kind === 'export' ? this.packages?.published?.() ?? null : null;
690
+ // an export uses the published copies of the packages this session shows live (SessionPackages.published), but
691
+ // the working folders of those it is told to follow
692
+ const scope = run.kind === 'export' ? this.packages?.published?.(opts.follow) ?? null : null;
691
693
  const r = await this.engine.cook(g, { target, frame, signal, onNode: (ev) => this.onNode(name, run, ev), ...(s?.size ? { vars: this.sessionVars(name) } : {}), ...(scope ? { scope } : {}) });
692
694
  if (seq && !scope && !(r.stopped && run.superseded) && seq > (this.recordedSeq.get(name) ?? 0)) {
693
695
  this.recordedSeq.set(name, seq);
@@ -3,5 +3,5 @@
3
3
  name="TopoTopo"
4
4
  description="Imports TopoTopo packages (<id>.topo): their published outputs are cooked into GLBs and PNGs that Godot imports itself, and kept current while the package changes."
5
5
  author="TopoTopo"
6
- version="0.5.2"
6
+ version="0.5.3"
7
7
  script="plugin.gd"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topotopo",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "An open-source procedural geometry engine: node graphs in JSON, cooked headless and deterministic, exported to Unreal, Blender and Houdini. Built for LLM agents through MCP, and for people through the CLI.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",