@chanmeng666/archlang-3d 0.0.0-stage → 0.1.1

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,29 @@
1
+ import { ReplayPayload } from './replay.js';
2
+ export { PREVIEW_LABEL, ReplayNode, ReplayOptions, replayPayload, viewBoxOf } from './replay.js';
3
+ import { S as StripItem } from './strip-BMxWrvYD.js';
4
+ export { R as ReplayFacts, s as stripAt } from './strip-BMxWrvYD.js';
5
+
6
+ interface ReplayHandle {
7
+ /** Render the frame at replay time `t` (clamped to [0, 1]) into the canvas, synchronously. */
8
+ seek: (t: number) => void;
9
+ /** SHA-256 (hex) of the canvas's composed RGBA pixels. */
10
+ frameHash: () => Promise<string>;
11
+ /** Settles once the core's drawing has loaded (or failed to); `seek` draws it from then on. */
12
+ ready: Promise<void>;
13
+ /** Release the WebGL context and the GPU buffers. The handle draws nothing afterwards. */
14
+ dispose: () => void;
15
+ }
16
+ /**
17
+ * Mount the replay `p` on `out`: sizes the canvas to `p.width`×`p.height` and draws nothing yet.
18
+ * `strip`, an element the host lays over the canvas, receives the metrics strip (card G4) as DOM
19
+ * text at every `seek`; it is never drawn into the canvas, so `frameHash` is the same with and
20
+ * without it.
21
+ */
22
+ declare function mountReplay(out: HTMLCanvasElement, p: ReplayPayload, strip?: HTMLElement): ReplayHandle;
23
+
24
+ /** The strip's DOM half, apart from `strip.ts` so that the types of `.` and `./node` need no DOM lib. */
25
+
26
+ /** Paint `items` into `el` (one span each); touches the DOM only when the text changed. */
27
+ declare function paintStrip(el: HTMLElement, items: readonly StripItem[]): void;
28
+
29
+ export { type ReplayHandle, ReplayPayload, StripItem, mountReplay, paintStrip };
@@ -0,0 +1,249 @@
1
+ import {
2
+ PREVIEW_LABEL,
3
+ replayPayload,
4
+ viewBoxOf
5
+ } from "./chunk-54YVSAVZ.js";
6
+ import {
7
+ END_AZIMUTH,
8
+ END_FOV,
9
+ START_FOV,
10
+ boxOfNodes,
11
+ cameraPosition,
12
+ cameraStops,
13
+ drawingFrame,
14
+ frameCamera,
15
+ stateAt,
16
+ stripAt
17
+ } from "./chunk-CTFMM64C.js";
18
+
19
+ // src/replay/mount.ts
20
+ import {
21
+ AmbientLight,
22
+ BufferAttribute,
23
+ BufferGeometry,
24
+ Color,
25
+ DirectionalLight,
26
+ EdgesGeometry,
27
+ Group,
28
+ LineBasicMaterial,
29
+ LineSegments,
30
+ Mesh,
31
+ MeshLambertMaterial,
32
+ OrthographicCamera,
33
+ PerspectiveCamera,
34
+ Scene,
35
+ Vector3,
36
+ WebGLRenderer
37
+ } from "three";
38
+
39
+ // src/replay/strip-dom.ts
40
+ function paintStrip(el, items) {
41
+ const key = items.map((i) => `${i.id}=${i.text}`).join("|");
42
+ if (el.dataset.stripKey === key) return;
43
+ el.dataset.stripKey = key;
44
+ el.hidden = items.length === 0;
45
+ const nodes = [];
46
+ for (const i of items) {
47
+ const s = el.ownerDocument.createElement("span");
48
+ s.dataset.fact = i.id;
49
+ s.textContent = i.text;
50
+ if (nodes.length > 0) nodes.push(el.ownerDocument.createTextNode(" "));
51
+ nodes.push(s);
52
+ }
53
+ el.replaceChildren(...nodes);
54
+ }
55
+
56
+ // src/replay/mount.ts
57
+ var THEMES = {
58
+ light: {
59
+ background: "#f7f5f0",
60
+ edge: "#5b5650",
61
+ colors: {
62
+ slab: "#cfc8bc",
63
+ floor: "#ece5d8",
64
+ column: "#bdb5a8",
65
+ wall: "#f4f1eb",
66
+ roof: "#9c958a",
67
+ door: "#b48a5c",
68
+ "window:frame": "#e9e6e0",
69
+ "window:glass": "#a9c7d8",
70
+ "wall:demolish": "#c8553d",
71
+ furniture: "#d9d1c3"
72
+ }
73
+ },
74
+ dark: {
75
+ background: "#0f2235",
76
+ edge: "#cfe3f5",
77
+ colors: {
78
+ slab: "#20384f",
79
+ floor: "#1b3248",
80
+ column: "#3b5b78",
81
+ wall: "#2e4d6a",
82
+ roof: "#14283c",
83
+ door: "#7aa6c9",
84
+ "window:frame": "#5d86a8",
85
+ "window:glass": "#86c5ee",
86
+ "wall:demolish": "#e07a5f",
87
+ furniture: "#3f6a8f"
88
+ }
89
+ }
90
+ };
91
+ var FOOTPRINT_MM = 12;
92
+ function mountReplay(out, p, strip) {
93
+ const theme = THEMES[p.theme];
94
+ const W = p.width;
95
+ const H = p.height;
96
+ const gl = document.createElement("canvas");
97
+ gl.width = W;
98
+ gl.height = H;
99
+ out.width = W;
100
+ out.height = H;
101
+ const ctx = out.getContext("2d", { willReadFrequently: true });
102
+ const renderer = new WebGLRenderer({ canvas: gl, antialias: false, preserveDrawingBuffer: true });
103
+ renderer.setPixelRatio(1);
104
+ renderer.setSize(W, H, false);
105
+ const scene = new Scene();
106
+ scene.background = new Color(theme.background);
107
+ scene.add(new AmbientLight(16777215, 1.6));
108
+ const sun = new DirectionalLight(16777215, 1.4);
109
+ sun.position.set(-0.6, 1.4, 0.9);
110
+ scene.add(sun);
111
+ const nodes = /* @__PURE__ */ new Map();
112
+ const owned = [];
113
+ for (const n of p.nodes) {
114
+ const pos = new Float32Array(n.positions.length);
115
+ let base = Infinity;
116
+ let topY = -Infinity;
117
+ for (let i = 1; i < n.positions.length; i += 3) {
118
+ base = Math.min(base, n.positions[i] / 1e3);
119
+ topY = Math.max(topY, n.positions[i] / 1e3);
120
+ }
121
+ for (let i = 0; i < n.positions.length; i += 3) {
122
+ const x = n.positions[i] / 1e3;
123
+ const y = n.positions[i + 1] / 1e3;
124
+ const z = n.positions[i + 2] / 1e3;
125
+ pos[i] = x;
126
+ pos[i + 1] = y - base;
127
+ pos[i + 2] = z;
128
+ }
129
+ const geo = new BufferGeometry();
130
+ geo.setAttribute("position", new BufferAttribute(pos, 3));
131
+ geo.setIndex(n.indices);
132
+ geo.computeVertexNormals();
133
+ const key = n.part ? `${n.kind}:${n.part}` : n.kind;
134
+ const glass = key === "window:glass";
135
+ const ghost = key === "wall:demolish";
136
+ const mat = new MeshLambertMaterial({
137
+ color: new Color(theme.colors[key] ?? theme.colors.furniture),
138
+ transparent: glass || ghost,
139
+ opacity: glass ? 0.45 : ghost ? 0.3 : 1,
140
+ depthWrite: !ghost,
141
+ flatShading: true,
142
+ // The floor finish sits a few millimetres under every footprint: pushed back in depth, so a
143
+ // flat footprint never fights it in perspective.
144
+ polygonOffset: n.kind === "floor" || n.kind === "slab",
145
+ polygonOffsetFactor: 1,
146
+ polygonOffsetUnits: 4
147
+ });
148
+ owned.push(geo, mat);
149
+ const g = new Group();
150
+ g.position.y = base + (n.lift ?? 0);
151
+ g.add(new Mesh(geo, mat));
152
+ if (n.kind !== "slab" && !glass) {
153
+ const edges = new EdgesGeometry(geo, 30);
154
+ const line = new LineBasicMaterial({ color: new Color(theme.edge) });
155
+ owned.push(edges, line);
156
+ g.add(new LineSegments(edges, line));
157
+ }
158
+ g.visible = false;
159
+ scene.add(g);
160
+ nodes.set(n.key, { g, flat: Math.min(1, FOOTPRINT_MM / Math.max(1e-6, topY - base)) });
161
+ }
162
+ const [vx, vy, vw, vh] = p.viewBox;
163
+ const aspect = W / H;
164
+ const { cx, cz, ow, oh } = drawingFrame(p.viewBox, aspect);
165
+ const box = boxOfNodes(p.nodes, { ghosts: true });
166
+ const lo = new Vector3(...box.lo);
167
+ const hi = new Vector3(...box.hi);
168
+ const top = hi.y + 1e3;
169
+ const ortho = new OrthographicCamera(-ow / 2, ow / 2, oh / 2, -oh / 2, 1, top + 1e5);
170
+ ortho.up.set(0, 0, -1);
171
+ ortho.position.set(cx, top + 5e4, cz);
172
+ ortho.lookAt(cx, 0, cz);
173
+ const persp = new PerspectiveCamera(START_FOV, aspect, 10, 1e9);
174
+ const orbit = { elevation: p.elevation, azimuth: END_AZIMUTH, fov: END_FOV, aspect };
175
+ const fits = cameraStops(p.nodes, p.events, orbit);
176
+ const size = hi.clone().sub(lo);
177
+ const mid = lo.clone().add(hi).multiplyScalar(0.5);
178
+ const reach = Math.max(...fits.map((f) => mid.distanceTo(new Vector3(...f.target))));
179
+ const radius = Math.max(
180
+ size.length() * 0.75 + reach + Math.hypot(mid.x - (vx + vw / 2), mid.z - (vy + vh / 2)),
181
+ 1e3
182
+ );
183
+ const rad = (d) => d * Math.PI / 180;
184
+ const img = new Image();
185
+ const ready = new Promise((resolve) => {
186
+ img.onload = () => resolve();
187
+ img.onerror = () => resolve();
188
+ });
189
+ img.src = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(p.svg)))}`;
190
+ let disposed = false;
191
+ function seek(t) {
192
+ if (disposed) return;
193
+ if (strip && p.facts) paintStrip(strip, stripAt(p.facts, p.events, Math.min(1, Math.max(0, t))));
194
+ const s = stateAt(p.events, t);
195
+ for (const [key, { g, flat }] of nodes) {
196
+ const st = s.nodes.get(key);
197
+ g.visible = st?.visible === true;
198
+ g.scale.y = Math.max(flat, st?.rise ?? 0);
199
+ }
200
+ let cam = ortho;
201
+ if (s.tilt > 0 || s.dolly > 0) {
202
+ const f = frameCamera(s, fits, { cx, cz, oh, elevation: p.elevation });
203
+ const { fov, dist, elevation, azimuth } = f;
204
+ const target = new Vector3(...f.target);
205
+ const el = rad(elevation);
206
+ persp.fov = fov;
207
+ persp.near = Math.max(10, dist - radius);
208
+ persp.far = dist + radius;
209
+ persp.updateProjectionMatrix();
210
+ persp.up.set(0, 0, -1);
211
+ if (el < rad(89.99)) persp.up.set(0, 1, 0);
212
+ persp.position.set(...cameraPosition([target.x, target.y, target.z], dist, { elevation, azimuth }));
213
+ persp.lookAt(target);
214
+ cam = persp;
215
+ }
216
+ renderer.render(scene, cam);
217
+ ctx.globalAlpha = 1;
218
+ ctx.drawImage(gl, 0, 0);
219
+ if (s.svg > 0 && img.complete && img.naturalWidth > 0) {
220
+ const k = Math.min(W / vw, H / vh);
221
+ const dw = vw * k;
222
+ const dh = vh * k;
223
+ ctx.globalAlpha = s.svg;
224
+ ctx.drawImage(img, (W - dw) / 2, (H - dh) / 2, dw, dh);
225
+ ctx.globalAlpha = 1;
226
+ }
227
+ }
228
+ async function frameHash() {
229
+ const data = ctx.getImageData(0, 0, W, H).data;
230
+ const digest = await crypto.subtle.digest("SHA-256", data);
231
+ return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
232
+ }
233
+ function dispose() {
234
+ if (disposed) return;
235
+ disposed = true;
236
+ for (const o of owned) o.dispose();
237
+ renderer.dispose();
238
+ renderer.forceContextLoss();
239
+ }
240
+ return { seek, frameHash, ready, dispose };
241
+ }
242
+ export {
243
+ PREVIEW_LABEL,
244
+ mountReplay,
245
+ paintStrip,
246
+ replayPayload,
247
+ stripAt,
248
+ viewBoxOf
249
+ };