@chanmeng666/archlang-3d 0.1.2 → 0.3.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,137 @@
1
+ import {
2
+ FLOOR_FINISHES,
3
+ endElevation,
4
+ explodeLifts,
5
+ slotGroups,
6
+ storeysOf,
7
+ timeline
8
+ } from "./chunk-VPYTRKMK.js";
9
+
10
+ // src/replay/view-dom.ts
11
+ var PRESET_LABELS = [
12
+ ["iso", "Iso", "Iso view: the replay's end view"],
13
+ ["top", "Top", "Top view, the plan's top edge up"],
14
+ ["north", "N", "North elevation: from the plan's top edge"],
15
+ ["east", "E", "East elevation: from the plan's right edge"],
16
+ ["south", "S", "South elevation: from the plan's bottom edge"],
17
+ ["west", "W", "West elevation: from the plan's left edge"]
18
+ ];
19
+ var VIEW_HINT = "Drag to turn, right-drag to pan, scroll to zoom; on the focused view, arrow keys turn and + / \u2212 zoom.";
20
+ var VIEW_CONTROLS_LABEL = "an interactive 3D view: drag or use the arrow keys to turn it, scroll or + and \u2212 to zoom";
21
+ function viewToolbar(view, opts = {}) {
22
+ const bar = document.createElement("div");
23
+ bar.className = "archlang-views";
24
+ bar.setAttribute("role", "toolbar");
25
+ bar.setAttribute("aria-label", "3D view");
26
+ const button = (text, label, onClick) => {
27
+ const b = document.createElement("button");
28
+ b.type = "button";
29
+ if (opts.buttonClass) b.className = opts.buttonClass;
30
+ b.textContent = text;
31
+ b.setAttribute("aria-label", label);
32
+ b.addEventListener("click", onClick);
33
+ return b;
34
+ };
35
+ const presetButtons = [];
36
+ const storeyButtons = [];
37
+ const press = () => {
38
+ for (const [name, b] of presetButtons) b.setAttribute("aria-pressed", String(view.camera === name));
39
+ for (const [i, b] of storeyButtons.entries()) b.setAttribute("aria-pressed", String(i + 1 === view.shown));
40
+ };
41
+ for (const [name, text, label] of PRESET_LABELS) {
42
+ const b = button(text, label, () => view.preset(name));
43
+ b.dataset.preset = name;
44
+ presetButtons.push([name, b]);
45
+ bar.append(b);
46
+ }
47
+ bar.append(button("Reset", "Reset the view: the replay's own camera, every storey shown", () => view.reset()));
48
+ if (view.storeys > 1) {
49
+ const group = document.createElement("span");
50
+ group.className = "archlang-views-storeys";
51
+ group.setAttribute("role", "group");
52
+ group.setAttribute("aria-label", "Storeys shown");
53
+ group.append("Storeys ");
54
+ for (let k = 1; k <= view.storeys; k++) {
55
+ const all = k === view.storeys;
56
+ const b = button(
57
+ all ? "All" : k === 1 ? "1" : `1\u2013${k}`,
58
+ all ? "Show every storey" : `Show storeys 1 to ${k}, hide the ones above`,
59
+ () => view.showStoreys(k)
60
+ );
61
+ b.dataset.storeys = String(k);
62
+ storeyButtons.push(b);
63
+ group.append(b);
64
+ }
65
+ bar.append(group);
66
+ }
67
+ view.onChange(press);
68
+ press();
69
+ const hint = document.createElement("span");
70
+ hint.className = "archlang-views-hint";
71
+ if (opts.hintId) hint.id = opts.hintId;
72
+ hint.textContent = VIEW_HINT;
73
+ bar.append(hint);
74
+ return bar;
75
+ }
76
+
77
+ // src/replay/payload.ts
78
+ var PREVIEW_LABEL = "Preview \u2014 from the separate archlang-3d package. ArchLang itself draws plans.";
79
+ function viewBoxOf(svg) {
80
+ const m = /viewBox="([-\d.\s]+)"/.exec(svg);
81
+ const v = (m?.[1] ?? "").trim().split(/\s+/).map(Number);
82
+ if (v.length !== 4 || v.some((x) => !Number.isFinite(x))) throw new Error("the drawing has no numeric viewBox");
83
+ return v;
84
+ }
85
+ function replayPayload(mesh, opts) {
86
+ const lifts = explodeLifts(mesh);
87
+ return {
88
+ title: opts.title,
89
+ theme: opts.theme ?? "light",
90
+ width: opts.width ?? 1920,
91
+ height: opts.height ?? 1080,
92
+ seconds: opts.seconds ?? 15,
93
+ svg: opts.svg,
94
+ viewBox: viewBoxOf(opts.svg),
95
+ elevation: endElevation(storeysOf(mesh).length),
96
+ ...opts.facts ? { facts: opts.facts } : {},
97
+ nodes: mesh.nodes.filter((n) => !n.hidden).map((n) => {
98
+ const positions = [];
99
+ const indices = [];
100
+ const slotted = n.shells.some((s) => s.slot !== void 0);
101
+ const groups = slotted ? slotGroups(n.shells) : [{ slot: "body", shells: n.shells }];
102
+ const runs = [];
103
+ for (const g of groups) {
104
+ const start = indices.length;
105
+ for (const s of g.shells) {
106
+ const base = positions.length / 3;
107
+ positions.push(...s.positions);
108
+ for (const i of s.indices) indices.push(base + i);
109
+ }
110
+ runs.push({ slot: g.slot, count: indices.length - start });
111
+ }
112
+ const lift = lifts.get(n.level) ?? 0;
113
+ const finish = FLOOR_FINISHES.find((f) => f === n.extras.finish);
114
+ return {
115
+ key: n.key,
116
+ kind: n.kind,
117
+ ...n.part ? { part: n.part } : {},
118
+ ...lift !== 0 ? { lift } : {},
119
+ positions,
120
+ indices,
121
+ ...slotted ? { groups: runs } : {},
122
+ ...n.kind === "floor" && finish ? { finish } : {}
123
+ };
124
+ }),
125
+ events: timeline(mesh)
126
+ };
127
+ }
128
+
129
+ export {
130
+ PRESET_LABELS,
131
+ VIEW_HINT,
132
+ VIEW_CONTROLS_LABEL,
133
+ viewToolbar,
134
+ PREVIEW_LABEL,
135
+ viewBoxOf,
136
+ replayPayload
137
+ };
@@ -1,14 +1,21 @@
1
1
  import {
2
2
  ARCHLANG_3D_VERSION,
3
3
  validateScene3D
4
- } from "./chunk-PJMKP2ML.js";
4
+ } from "./chunk-PRZXP6FP.js";
5
5
  import {
6
6
  PREVIEW_LABEL,
7
+ VIEW_CONTROLS_LABEL,
7
8
  replayPayload
8
- } from "./chunk-54YVSAVZ.js";
9
+ } from "./chunk-WBA4LABI.js";
9
10
  import {
10
- keyFrames
11
- } from "./chunk-CTFMM64C.js";
11
+ FINISH_PALETTE,
12
+ FLOOR_FINISHES,
13
+ KIND_PALETTE,
14
+ PALETTE,
15
+ keyFrames,
16
+ linearRgb,
17
+ slotGroups
18
+ } from "./chunk-VPYTRKMK.js";
12
19
 
13
20
  // src/node.ts
14
21
  import { readFileSync as readFileSync2 } from "node:fs";
@@ -73,15 +80,22 @@ canvas{max-width:100%;height:auto;display:block}
73
80
  #archlang-replay-strip[hidden]{display:none}
74
81
  input[type=range]{width:min(720px,90vw)}
75
82
  p{margin:0;font-size:12px;opacity:.8}
83
+ canvas:focus-visible{outline:2px solid ${fg};outline-offset:2px}
84
+ .archlang-views{display:flex;flex-wrap:wrap;align-items:center;justify-content:center;gap:6px;max-width:min(720px,90vw)}
85
+ .archlang-views button{font:inherit;font-size:13px;padding:2px 10px;border:1px solid ${fg};border-radius:4px;background:transparent;color:${fg};cursor:pointer}
86
+ .archlang-views button[aria-pressed=true]{background:${fg};color:${bg}}
87
+ .archlang-views-storeys{display:inline-flex;align-items:center;gap:4px;margin-left:8px}
88
+ .archlang-views-hint{flex-basis:100%;text-align:center;font-size:12px;opacity:.8}
76
89
  </style>
77
90
  </head>
78
91
  <body>
79
92
  <main>
80
93
  <div id="archlang-replay-stage" style="width:${payload.width}px">
81
- <canvas id="archlang-replay" role="img" aria-label="${escapeHtml(`${opts.title}: the plan drawing itself in its authoring order, then rising`)}"></canvas>${payload.facts ? `
94
+ <canvas id="archlang-replay" role="application" aria-label="${escapeHtml(`${opts.title}: the plan drawing itself in its authoring order, then rising; ${VIEW_CONTROLS_LABEL}`)}"></canvas>${payload.facts ? `
82
95
  <div id="archlang-replay-strip" role="group" aria-label="Plan facts" hidden></div>` : ""}
83
96
  </div>
84
97
  <input id="archlang-replay-t" type="range" min="0" max="1" step="0.001" value="0" aria-label="Replay position">
98
+ <div id="archlang-replay-views"></div>
85
99
  <p>${escapeHtml(PREVIEW_LABEL)}</p>
86
100
  </main>
87
101
  <script type="application/json" id="archlang-replay-payload">${json}</script>
@@ -239,18 +253,16 @@ async function renderReplay(o) {
239
253
  // src/glb/write.ts
240
254
  import { Document, Logger, WebIO } from "@gltf-transform/core";
241
255
  var GLB_GENERATOR = `archlang-3d ${ARCHLANG_3D_VERSION}`;
242
- var COLOUR = {
243
- slab: [0.55, 0.55, 0.55, 1],
244
- floor: [0.8, 0.75, 0.66, 1],
245
- column: [0.7, 0.7, 0.7, 1],
246
- wall: [0.92, 0.91, 0.88, 1],
247
- door: [0.55, 0.4, 0.28, 1],
248
- window: [0.6, 0.75, 0.85, 1],
249
- opening: [0.6, 0.6, 0.6, 1],
250
- furniture: [0.62, 0.64, 0.66, 1],
251
- roof: [0.45, 0.42, 0.4, 1]
252
- };
253
- var DEMOLISH_COLOUR = [0.78, 0.33, 0.24, 0.3];
256
+ function bodyMaterialName(n) {
257
+ if (n.kind === "floor") return `floor:${FLOOR_FINISHES.find((f) => f === n.extras.finish) ?? "neutral"}`;
258
+ if (n.kind === "wall" && n.part === "demolish") return "wall:demolish";
259
+ if (n.kind === "window" && (n.part === "glass" || n.part === "frame")) return `window:${n.part}`;
260
+ return n.kind;
261
+ }
262
+ function bodyMaterial(name) {
263
+ if (name.startsWith("floor:")) return FINISH_PALETTE[name.slice("floor:".length)];
264
+ return KIND_PALETTE[name] ?? KIND_PALETTE.furniture;
265
+ }
254
266
  var glbNodeName = (n) => n.key.slice(n.key.indexOf("/") + 1);
255
267
  var levelNodeName = (level) => `level:L${level}`;
256
268
  function nodeExtras(n) {
@@ -276,12 +288,23 @@ async function writeGlb(mesh, opts = {}) {
276
288
  doc.getRoot().setDefaultScene(scene);
277
289
  const materials = /* @__PURE__ */ new Map();
278
290
  const material = (n) => {
279
- const ghost = n.kind === "wall" && n.part === "demolish";
280
- const key = ghost ? "wall:demolish" : n.kind;
291
+ const key = bodyMaterialName(n);
292
+ let m = materials.get(key);
293
+ if (!m) {
294
+ const p = bodyMaterial(key);
295
+ m = doc.createMaterial(key).setBaseColorFactor([...linearRgb(p.srgb), p.alpha]).setMetallicFactor(p.metallic).setRoughnessFactor(p.roughness);
296
+ if (p.alpha < 1) m.setAlphaMode("BLEND");
297
+ materials.set(key, m);
298
+ }
299
+ return m;
300
+ };
301
+ const slotMaterial = (slot) => {
302
+ const key = `fixture:${slot}`;
281
303
  let m = materials.get(key);
282
304
  if (!m) {
283
- m = doc.createMaterial(key).setBaseColorFactor(ghost ? DEMOLISH_COLOUR : COLOUR[n.kind]).setMetallicFactor(0).setRoughnessFactor(1);
284
- if (ghost) m.setAlphaMode("BLEND");
305
+ const p = PALETTE[slot];
306
+ m = doc.createMaterial(key).setBaseColorFactor([...linearRgb(p.srgb), p.alpha]).setMetallicFactor(p.metallic).setRoughnessFactor(p.roughness);
307
+ if (p.alpha < 1) m.setAlphaMode("BLEND");
285
308
  materials.set(key, m);
286
309
  }
287
310
  return m;
@@ -294,19 +317,23 @@ async function writeGlb(mesh, opts = {}) {
294
317
  scene.addChild(group);
295
318
  levels.set(n.level, group);
296
319
  }
297
- const positions = [];
298
- const indices = [];
299
- for (const s of n.shells) {
300
- const base = positions.length / 3;
301
- for (const v of s.positions) positions.push(v / 1e6);
302
- for (const i of s.indices) indices.push(base + i);
303
- }
304
- const count = positions.length / 3;
305
- const pos = doc.createAccessor().setType("VEC3").setArray(new Float32Array(positions)).setBuffer(buffer);
306
- const idx = doc.createAccessor().setType("SCALAR").setArray(count > 65535 ? new Uint32Array(indices) : new Uint16Array(indices)).setBuffer(buffer);
307
- const prim = doc.createPrimitive().setAttribute("POSITION", pos).setIndices(idx).setMaterial(material(n));
308
320
  const name = glbNodeName(n);
309
- const node = doc.createNode(name).setMesh(doc.createMesh(name).addPrimitive(prim)).setExtras({ archlang: nodeExtras(n) });
321
+ const gmesh = doc.createMesh(name);
322
+ for (const g of slotGroups(n.shells)) {
323
+ const positions = [];
324
+ const indices = [];
325
+ for (const s of g.shells) {
326
+ const base = positions.length / 3;
327
+ for (const v of s.positions) positions.push(v / 1e6);
328
+ for (const i of s.indices) indices.push(base + i);
329
+ }
330
+ const count = positions.length / 3;
331
+ const pos = doc.createAccessor().setType("VEC3").setArray(new Float32Array(positions)).setBuffer(buffer);
332
+ const idx = doc.createAccessor().setType("SCALAR").setArray(count > 65535 ? new Uint32Array(indices) : new Uint16Array(indices)).setBuffer(buffer);
333
+ const mat = g.slot === "body" ? material(n) : slotMaterial(g.slot);
334
+ gmesh.addPrimitive(doc.createPrimitive().setAttribute("POSITION", pos).setIndices(idx).setMaterial(mat));
335
+ }
336
+ const node = doc.createNode(name).setMesh(gmesh).setExtras({ archlang: nodeExtras(n) });
310
337
  group.addChild(node);
311
338
  }
312
339
  return new WebIO().writeBinary(doc);
@@ -477,6 +504,7 @@ export {
477
504
  SWIFTSHADER_ARGS,
478
505
  renderReplay,
479
506
  GLB_GENERATOR,
507
+ bodyMaterialName,
480
508
  glbNodeName,
481
509
  levelNodeName,
482
510
  nodeExtras,
package/dist/cli.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  DOOR_POSES,
4
4
  adaptPlan,
5
5
  buildMesh
6
- } from "./chunk-DLZMYNAR.js";
6
+ } from "./chunk-QUSG4UCO.js";
7
7
  import {
8
8
  SWIFTSHADER_ARGS,
9
9
  checkGlb,
@@ -13,18 +13,18 @@ import {
13
13
  renderReplay,
14
14
  replayHtml,
15
15
  writeGlb
16
- } from "./chunk-KIRWFAD6.js";
16
+ } from "./chunk-ZE4PL6KP.js";
17
17
  import {
18
18
  ARCHLANG_3D_VERSION,
19
19
  replayFacts
20
- } from "./chunk-PJMKP2ML.js";
21
- import "./chunk-54YVSAVZ.js";
20
+ } from "./chunk-PRZXP6FP.js";
21
+ import "./chunk-WBA4LABI.js";
22
22
  import {
23
23
  storeysOf,
24
24
  timeline,
25
25
  viewLabel,
26
26
  viewMesh
27
- } from "./chunk-CTFMM64C.js";
27
+ } from "./chunk-VPYTRKMK.js";
28
28
 
29
29
  // src/cli/run.ts
30
30
  import { readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "node:fs";
@@ -1,5 +1,5 @@
1
1
  import { World } from '@chanmeng666/archlang';
2
- import { p as ReplayView, R as ReplayFacts } from './strip-DElA8ZbJ.js';
2
+ import { y as ReplayView, R as ReplayFacts } from './strip-CN_dTKqy.js';
3
3
 
4
4
  /**
5
5
  * The metrics strip's numbers (card G4), read from the core's public `describe()` and `lint()` for