@chanmeng666/archlang-3d 0.4.0 → 0.6.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.
@@ -1,4 +1,4 @@
1
- import { y as ReplayEvent, z as ReplayState } from './strip-Nt7eJ-4E.js';
1
+ import { J as ReplayEvent, Q as ReplayState } from './strip-CA-IAT0N.js';
2
2
 
3
3
  /**
4
4
  * The replay's end camera as plain maths (card A3, look notes round 1): the viewer and the tests
@@ -3571,7 +3571,7 @@ function floorLifts(floors, overlaps) {
3571
3571
  }
3572
3572
 
3573
3573
  // package.json
3574
- var version = "0.4.0";
3574
+ var version = "0.6.0";
3575
3575
 
3576
3576
  // src/scene3d/build.ts
3577
3577
  import * as core2 from "@chanmeng666/archlang";
@@ -5,7 +5,7 @@ import {
5
5
  slotGroups,
6
6
  storeysOf,
7
7
  timeline
8
- } from "./chunk-EQBMPXB5.js";
8
+ } from "./chunk-TNNJ7CAO.js";
9
9
 
10
10
  // src/replay/view-dom.ts
11
11
  var PRESET_LABELS = [
@@ -38,14 +38,68 @@ function viewToolbar(view, opts = {}) {
38
38
  for (const [name, b] of presetButtons) b.setAttribute("aria-pressed", String(view.camera === name));
39
39
  for (const [i, b] of storeyButtons.entries()) b.setAttribute("aria-pressed", String(i + 1 === view.shown));
40
40
  };
41
+ let shut = (_refocus) => {
42
+ };
41
43
  for (const [name, text, label] of PRESET_LABELS) {
42
- const b = button(text, label, () => view.preset(name));
44
+ const b = button(text, label, () => {
45
+ view.preset(name);
46
+ shut(true);
47
+ });
43
48
  b.dataset.preset = name;
44
49
  presetButtons.push([name, b]);
45
- bar.append(b);
46
50
  }
47
- bar.append(button("Reset", "Reset the view: the replay's own camera, every storey shown", () => view.reset()));
48
- if (view.storeys > 1) {
51
+ const resetLabel = "Reset the view: the replay's own camera, every storey shown";
52
+ if (opts.compact) {
53
+ bar.classList.add("archlang-views-compact");
54
+ bar.append(button("\u27F2 Reset", resetLabel, () => view.reset()));
55
+ const wrap = document.createElement("span");
56
+ wrap.className = "archlang-views-menu";
57
+ const pop = document.createElement("span");
58
+ pop.className = "archlang-views-presets";
59
+ pop.id = `${opts.hintId ?? "archlang-views"}-presets`;
60
+ pop.setAttribute("role", "group");
61
+ pop.setAttribute("aria-label", "Preset views");
62
+ pop.hidden = true;
63
+ pop.append(...presetButtons.map(([, b]) => b));
64
+ const toggle = button(
65
+ "View \u25BE",
66
+ "View presets: iso, top, and from north, east, south and west",
67
+ () => pop.hidden ? open() : shut(false)
68
+ );
69
+ toggle.setAttribute("aria-expanded", "false");
70
+ toggle.setAttribute("aria-controls", pop.id);
71
+ const away = (e) => {
72
+ if (!wrap.contains(e.target)) shut(false);
73
+ };
74
+ const open = () => {
75
+ pop.hidden = false;
76
+ toggle.setAttribute("aria-expanded", "true");
77
+ document.addEventListener("pointerdown", away, true);
78
+ (presetButtons.find(([n]) => view.camera === n)?.[1] ?? presetButtons[0]?.[1])?.focus();
79
+ };
80
+ shut = (refocus) => {
81
+ if (pop.hidden) return;
82
+ pop.hidden = true;
83
+ toggle.setAttribute("aria-expanded", "false");
84
+ document.removeEventListener("pointerdown", away, true);
85
+ if (refocus) toggle.focus();
86
+ };
87
+ wrap.addEventListener("keydown", (e) => {
88
+ if (e.key !== "Escape" || pop.hidden) return;
89
+ e.preventDefault();
90
+ e.stopPropagation();
91
+ shut(true);
92
+ });
93
+ wrap.addEventListener("focusout", (e) => {
94
+ if (!wrap.contains(e.relatedTarget)) shut(false);
95
+ });
96
+ wrap.append(toggle, pop);
97
+ bar.append(wrap);
98
+ } else {
99
+ bar.append(...presetButtons.map(([, b]) => b));
100
+ bar.append(button("Reset", resetLabel, () => view.reset()));
101
+ }
102
+ if (view.storeys > 1 && opts.storeys !== false) {
49
103
  const group = document.createElement("span");
50
104
  group.className = "archlang-views-storeys";
51
105
  group.setAttribute("role", "group");
@@ -76,8 +130,8 @@ function viewToolbar(view, opts = {}) {
76
130
 
77
131
  // src/replay/payload.ts
78
132
  var PREVIEW_LEAD = "Same source, plan and 3D preview.";
79
- var PREVIEW_LABEL = "Same source, plan and 3D preview. The 3D preview is extruded from the same .arch file by the separate archlang-3d package, and checked against the plan.";
80
- var PREVIEW_LIMITS = "3D preview: walls, openings, floors, stairs and furniture in flat colours; no pitched roofs yet, and no sections, elevations or BIM.";
133
+ var PREVIEW_LABEL = "Same source, plan and 3D preview. The 3D preview is extruded from the same .arch file by the separate archlang-3d package, and checked against the plan: every 3D element comes from the plan's elements, and every solid is closed.";
134
+ var PREVIEW_LIMITS = "3D preview: walls, openings, floors, stairs, furniture, outdoor surfaces and fences in flat colours; the ground is a backdrop the viewer draws, not part of the building and not in the file; no pitched roofs or balcony railings yet, and no sections, elevations or BIM.";
81
135
  var PREVIEW_ABOUT_URL = "https://archlang.uk/3d";
82
136
  function viewBoxOf(svg) {
83
137
  const m = /viewBox="([-\d.\s]+)"/.exec(svg);
@@ -114,6 +168,7 @@ function replayPayload(mesh, opts) {
114
168
  }
115
169
  const lift = lifts.get(n.level) ?? 0;
116
170
  const finish = FLOOR_FINISHES.find((f) => f === n.extras.finish);
171
+ const colour = typeof n.extras.colour === "string" ? n.extras.colour : void 0;
117
172
  return {
118
173
  key: n.key,
119
174
  kind: n.kind,
@@ -122,12 +177,24 @@ function replayPayload(mesh, opts) {
122
177
  positions,
123
178
  indices,
124
179
  ...slotted ? { groups: runs } : {},
125
- ...n.kind === "floor" && finish ? { finish } : {}
180
+ ...n.kind === "floor" && finish ? { finish } : {},
181
+ ...(n.kind === "outdoor" || n.kind === "fence") && colour ? { colour } : {}
126
182
  };
127
183
  }),
128
- events: timeline(mesh)
184
+ events: timeline(mesh),
185
+ ...groundOf(mesh)
129
186
  };
130
187
  }
188
+ function groundOf(mesh) {
189
+ const row = mesh.assumptions.find((a) => a.rule === "ground-plane-viewer-only")?.value;
190
+ const lowest = typeof row?.level === "number" ? row.level : storeysOf(mesh)[0];
191
+ const slab = mesh.nodes.find((n) => n.level === lowest && n.kind === "slab" && !n.hidden);
192
+ if (!slab) return {};
193
+ let y = -Infinity;
194
+ for (const s of slab.shells)
195
+ for (let i = 1; i < s.positions.length; i += 3) y = Math.max(y, s.positions[i]);
196
+ return Number.isFinite(y) ? { ground: { y } } : {};
197
+ }
131
198
 
132
199
  export {
133
200
  PRESET_LABELS,
@@ -1,33 +1,117 @@
1
1
  import {
2
2
  ARCHLANG_3D_VERSION,
3
3
  validateScene3D
4
- } from "./chunk-EG7UT2HG.js";
4
+ } from "./chunk-6WCS4TAT.js";
5
5
  import {
6
6
  PREVIEW_LABEL,
7
7
  PREVIEW_LIMITS,
8
8
  VIEW_CONTROLS_LABEL,
9
9
  replayPayload
10
- } from "./chunk-KF5ME6KH.js";
10
+ } from "./chunk-CX6IVI7I.js";
11
11
  import {
12
+ FENCE_PALETTE,
12
13
  FINISH_PALETTE,
13
14
  FLOOR_FINISHES,
15
+ GROUND_ROUGHNESS,
16
+ GROUND_SPLITS,
17
+ GROUND_TINT,
14
18
  KIND_PALETTE,
15
19
  PALETTE,
16
20
  keyFrames,
17
21
  linearRgb,
18
22
  slotGroups
19
- } from "./chunk-EQBMPXB5.js";
23
+ } from "./chunk-TNNJ7CAO.js";
20
24
 
21
- // src/node.ts
22
- import { readFileSync as readFileSync2 } from "node:fs";
23
- import { resolve } from "node:path";
25
+ // src/world.ts
26
+ import { readFileSync, realpathSync, statSync } from "node:fs";
27
+ import { dirname, isAbsolute, relative, resolve } from "node:path";
28
+ import { utf8ByteLength } from "@chanmeng666/archlang";
29
+ var MODULE_CAP_BYTES = 1024 * 1024;
30
+ function isWithin(root, child) {
31
+ const rel = relative(root, child);
32
+ if (rel === "") return true;
33
+ return rel !== ".." && !rel.startsWith("../") && !rel.startsWith("..\\") && !isAbsolute(rel);
34
+ }
35
+ var realOrNull = (p) => {
36
+ try {
37
+ return realpathSync(p);
38
+ } catch {
39
+ return null;
40
+ }
41
+ };
42
+ function nearestRealAncestor(p) {
43
+ for (let at = dirname(p); ; at = dirname(at)) {
44
+ const real = realOrNull(at);
45
+ if (real !== null) return real;
46
+ if (dirname(at) === at) return null;
47
+ }
48
+ }
49
+ function makeNodeWorld(baseDir, root = process.cwd()) {
50
+ const roots = [resolve(root), resolve(baseDir)].flatMap((r) => {
51
+ const real = realOrNull(r);
52
+ return real === null ? [] : [{ lexical: r, real }];
53
+ });
54
+ const refusals = /* @__PURE__ */ new Map();
55
+ const outside = (path) => ({
56
+ code: "E_IMPORT_OUTSIDE_ROOT",
57
+ message: `Imported module "${path}" is outside the import root (the working directory) and the plan's own directory; run archlang3d from a directory that contains it`
58
+ });
59
+ const notArch = (path) => ({
60
+ code: "E_IMPORT_NOT_ARCH",
61
+ message: `Imported module "${path}" is not an .arch file`
62
+ });
63
+ const inRoot = (abs) => roots.some((r) => isWithin(r.lexical, abs) || isWithin(r.real, abs));
64
+ const inRealRoot = (real) => roots.some((r) => isWithin(r.real, real));
65
+ const isArch = (p) => /\.arch$/i.test(p);
66
+ const attempt = (path) => {
67
+ if (path.includes("\0")) return null;
68
+ const abs = resolve(baseDir, path);
69
+ if (!inRoot(abs)) return outside(path);
70
+ if (!isArch(abs)) return notArch(path);
71
+ const real = realOrNull(abs);
72
+ if (real === null) {
73
+ const ancestor = nearestRealAncestor(abs);
74
+ return ancestor !== null && inRealRoot(ancestor) ? null : outside(path);
75
+ }
76
+ if (!inRealRoot(real)) return outside(path);
77
+ if (!isArch(real)) return notArch(path);
78
+ const st = statSync(real);
79
+ if (!st.isFile()) return null;
80
+ const tooLarge = () => ({
81
+ code: "E_IMPORT_TOO_LARGE",
82
+ message: `Imported module "${path}" is larger than ${String(MODULE_CAP_BYTES)} bytes`
83
+ });
84
+ if (st.size > MODULE_CAP_BYTES) return tooLarge();
85
+ const text = readFileSync(real, "utf8");
86
+ return utf8ByteLength(text) > MODULE_CAP_BYTES ? tooLarge() : text;
87
+ };
88
+ const world = {
89
+ read(path) {
90
+ let r;
91
+ try {
92
+ r = attempt(path);
93
+ } catch {
94
+ r = null;
95
+ }
96
+ if (typeof r === "string") {
97
+ refusals.delete(path);
98
+ return r;
99
+ }
100
+ if (r) refusals.set(path, r);
101
+ else refusals.delete(path);
102
+ return null;
103
+ },
104
+ refusal: (path) => refusals.get(path)
105
+ };
106
+ return world;
107
+ }
24
108
 
25
109
  // src/replay/html.ts
26
- import { existsSync, readFileSync } from "node:fs";
110
+ import { existsSync, readFileSync as readFileSync2 } from "node:fs";
27
111
  import { createRequire } from "node:module";
28
- import { dirname, join } from "node:path";
112
+ import { dirname as dirname2, join } from "node:path";
29
113
  import { fileURLToPath } from "node:url";
30
- var HERE = dirname(fileURLToPath(import.meta.url));
114
+ var HERE = dirname2(fileURLToPath(import.meta.url));
31
115
  var require2 = createRequire(import.meta.url);
32
116
  var bundled;
33
117
  async function loadViewer() {
@@ -35,7 +119,7 @@ async function loadViewer() {
35
119
  const { bundleViewer } = await null;
36
120
  return bundleViewer(join(HERE, "viewer.ts"));
37
121
  }
38
- return readFileSync(join(HERE, "viewer.iife.js"), "utf8");
122
+ return readFileSync2(join(HERE, "viewer.iife.js"), "utf8");
39
123
  }
40
124
  function viewerScript() {
41
125
  bundled ??= loadViewer().catch((e) => {
@@ -45,15 +129,15 @@ function viewerScript() {
45
129
  return bundled;
46
130
  }
47
131
  function threeLicence() {
48
- let dir = dirname(require2.resolve("three"));
132
+ let dir = dirname2(require2.resolve("three"));
49
133
  while (!existsSync(join(dir, "package.json")) || !existsSync(join(dir, "LICENSE"))) {
50
- const up = dirname(dir);
134
+ const up = dirname2(dir);
51
135
  if (up === dir) throw new Error("three.js's LICENSE was not found beside its package.json");
52
136
  dir = up;
53
137
  }
54
- const version = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version;
138
+ const version = JSON.parse(readFileSync2(join(dir, "package.json"), "utf8")).version;
55
139
  return `three.js ${version}
56
- ${readFileSync(join(dir, "LICENSE"), "utf8")}`;
140
+ ${readFileSync2(join(dir, "LICENSE"), "utf8")}`;
57
141
  }
58
142
  var escapeHtml = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
59
143
  async function replayHtml(mesh, opts) {
@@ -130,13 +214,25 @@ function seekOnly(html) {
130
214
  '<style>html,body{overflow:hidden}</style><script>window.__replayAutoplay=false</script>\n<script type="application/json"'
131
215
  );
132
216
  }
217
+ var PAYLOAD_OPEN = '<script type="application/json" id="archlang-replay-payload">';
218
+ function withSupersample(html, k = 1) {
219
+ if (!Number.isInteger(k) || k < 1 || k > 4)
220
+ throw new Error(`supersample must be a whole number from 1 to 4 (got ${k})`);
221
+ if (k === 1) return html;
222
+ const at = html.indexOf(PAYLOAD_OPEN);
223
+ const end = at < 0 ? -1 : html.indexOf("</script>", at);
224
+ if (end < 0) throw new Error("the replay page carries no payload to supersample");
225
+ const p = JSON.parse(html.slice(at + PAYLOAD_OPEN.length, end));
226
+ const json = JSON.stringify({ ...p, supersample: k }).replace(/</g, "\\u003c");
227
+ return html.slice(0, at + PAYLOAD_OPEN.length) + json + html.slice(end);
228
+ }
133
229
  async function renderReplay(o) {
134
230
  const fps = o.fps ?? 30;
135
231
  const seconds = o.seconds ?? 15;
136
232
  const frames = Math.round(fps * seconds);
137
233
  const dir = mkdtempSync(join2(tmpdir(), "archlang3d-replay-"));
138
234
  const file = join2(dir, "replay.html");
139
- writeFileSync(file, seekOnly(o.html));
235
+ writeFileSync(file, seekOnly(withSupersample(o.html, o.supersample)));
140
236
  const { chromium } = await import("playwright-core");
141
237
  const browser = await chromium.launch({ args: SWIFTSHADER_ARGS });
142
238
  try {
@@ -259,9 +355,18 @@ function bodyMaterialName(n) {
259
355
  if (n.kind === "floor") return `floor:${FLOOR_FINISHES.find((f) => f === n.extras.finish) ?? "neutral"}`;
260
356
  if (n.kind === "wall" && n.part === "demolish") return "wall:demolish";
261
357
  if (n.kind === "window" && (n.part === "glass" || n.part === "frame")) return `window:${n.part}`;
358
+ if (n.kind === "outdoor") return `outdoor:${String(n.extras.surface)}`;
359
+ if (n.kind === "fence") return `fence:${String(n.extras.style)}`;
262
360
  return n.kind;
263
361
  }
264
- function bodyMaterial(name) {
362
+ function bodyMaterial(name, n) {
363
+ if (n.kind === "outdoor" || n.kind === "fence") {
364
+ const own = n.kind === "fence" ? FENCE_PALETTE[String(n.extras.style)] : GROUND_SPLITS[String(n.extras.surface)];
365
+ const tint = GROUND_TINT[String(n.extras.surface)] ?? "paving";
366
+ const roughness = own?.roughness ?? (n.kind === "fence" ? FENCE_PALETTE.picket.roughness : GROUND_ROUGHNESS[tint]);
367
+ const srgb = typeof n.extras.colour === "string" ? n.extras.colour : KIND_PALETTE.furniture.srgb;
368
+ return { srgb, alpha: 1, metallic: 0, roughness };
369
+ }
265
370
  if (name.startsWith("floor:")) return FINISH_PALETTE[name.slice("floor:".length)];
266
371
  return KIND_PALETTE[name] ?? KIND_PALETTE.furniture;
267
372
  }
@@ -293,7 +398,7 @@ async function writeGlb(mesh, opts = {}) {
293
398
  const key = bodyMaterialName(n);
294
399
  let m = materials.get(key);
295
400
  if (!m) {
296
- const p = bodyMaterial(key);
401
+ const p = bodyMaterial(key, n);
297
402
  m = doc.createMaterial(key).setBaseColorFactor([...linearRgb(p.srgb), p.alpha]).setMetallicFactor(p.metallic).setRoughnessFactor(p.roughness);
298
403
  if (p.alpha < 1) m.setAlphaMode("BLEND");
299
404
  materials.set(key, m);
@@ -487,20 +592,8 @@ async function checkGlb(glb, mesh) {
487
592
  return out;
488
593
  }
489
594
 
490
- // src/node.ts
491
- function makeNodeWorld(baseDir) {
492
- return {
493
- read: (p) => {
494
- try {
495
- return readFileSync2(resolve(baseDir, p), "utf8");
496
- } catch {
497
- return null;
498
- }
499
- }
500
- };
501
- }
502
-
503
595
  export {
596
+ makeNodeWorld,
504
597
  viewerScript,
505
598
  replayHtml,
506
599
  SWIFTSHADER_ARGS,
@@ -520,6 +613,5 @@ export {
520
613
  checkNaming,
521
614
  glbJson,
522
615
  checkValidator,
523
- checkGlb,
524
- makeNodeWorld
616
+ checkGlb
525
617
  };
@@ -78,6 +78,44 @@ function floorFinish(uses) {
78
78
  for (const [finish, kinds] of FLOOR_FINISH_RULES) if (uses?.some((u) => kinds.includes(u))) return finish;
79
79
  return "neutral";
80
80
  }
81
+ var GROUND_TINT = {
82
+ lawn: "lawn",
83
+ planting: "lawn",
84
+ paving: "paving",
85
+ deck: "paving",
86
+ gravel: "paving",
87
+ water: "water",
88
+ driveway: "paving",
89
+ patio: "paving",
90
+ balcony: "paving"
91
+ };
92
+ var GROUND_WASH = 6;
93
+ var SITE_PALETTE = {
94
+ gravel: m("#cbc3b4", 0.95),
95
+ tarmac: m("#8c8984", 0.85)
96
+ };
97
+ var GROUND_SPLITS = {
98
+ planting: PALETTE.foliage,
99
+ deck: PALETTE.wood,
100
+ gravel: SITE_PALETTE.gravel,
101
+ driveway: SITE_PALETTE.tarmac
102
+ };
103
+ var GROUND_ROUGHNESS = { lawn: 0.95, paving: 0.85, water: 0.1 };
104
+ var FENCE_PALETTE = {
105
+ picket: m("#eeebe4", 0.8),
106
+ panel: PALETTE.wood,
107
+ post: PALETTE.wood_dark
108
+ };
109
+ var GROUND_PLANE = { light: "#e2ddd3", dark: "#16293d", marginMm: 6e3 };
110
+ function siteColour(own, theme, toward) {
111
+ if (theme === "light") return own;
112
+ const mix = (i) => {
113
+ const a = Number.parseInt(own.slice(i, i + 2), 16);
114
+ const b = Number.parseInt(toward.slice(i, i + 2), 16);
115
+ return Math.round(a * 0.4 + b * 0.6).toString(16).padStart(2, "0");
116
+ };
117
+ return `#${mix(1)}${mix(3)}${mix(5)}`;
118
+ }
81
119
  function linearRgb(hex) {
82
120
  const ch = (i) => {
83
121
  const c = Number.parseInt(hex.slice(i, i + 2), 16) / 255;
@@ -174,10 +212,14 @@ function groupOf(n) {
174
212
  return 3;
175
213
  case "furniture":
176
214
  return FIXTURE_FAMILIES.has(String(n.extras.category)) ? 0 : 1;
215
+ case "outdoor":
216
+ return 1;
217
+ case "fence":
218
+ return 4;
177
219
  }
178
220
  }
179
221
  var isGhost = (n) => n.part === "demolish";
180
- var isFlat = (n) => n.kind === "slab" || n.kind === "floor";
222
+ var isFlat = (n) => n.kind === "slab" || n.kind === "floor" || n.kind === "outdoor";
181
223
  function authoringOrder(mesh) {
182
224
  return [...mesh.nodes].sort(
183
225
  (a, b) => STAGES.indexOf(a.stage) - STAGES.indexOf(b.stage) || groupOf(a) - groupOf(b) || a.level - b.level || a.order - b.order || (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)
@@ -325,6 +367,7 @@ function stateAt(events, t) {
325
367
  stop,
326
368
  settle,
327
369
  svg,
370
+ ground: u >= PHASE.rise ? 1 - svg : 0,
328
371
  nodes
329
372
  };
330
373
  }
@@ -575,6 +618,14 @@ export {
575
618
  FINISH_PALETTE,
576
619
  FLOOR_FINISH_RULES,
577
620
  floorFinish,
621
+ GROUND_TINT,
622
+ GROUND_WASH,
623
+ SITE_PALETTE,
624
+ GROUND_SPLITS,
625
+ GROUND_ROUGHNESS,
626
+ FENCE_PALETTE,
627
+ GROUND_PLANE,
628
+ siteColour,
578
629
  linearRgb,
579
630
  slotColour,
580
631
  finishColour,