@entro314labs/iso-engine 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +165 -2
- package/THIRD_PARTY_NOTICES.md +39 -0
- package/dist/engine/anim.js +225 -0
- package/dist/engine/behaviors.js +105 -0
- package/dist/engine/diagram.js +80 -0
- package/dist/engine/draw.js +117 -0
- package/dist/engine/export.js +81 -0
- package/dist/engine/figure.js +35 -0
- package/dist/engine/global.js +4 -0
- package/dist/engine/index.d.ts +278 -0
- package/dist/engine/index.js +164 -0
- package/dist/engine/math.js +190 -0
- package/dist/engine/node.js +290 -0
- package/dist/engine/paths.js +76 -0
- package/dist/engine/prefabs/architecture.js +136 -0
- package/dist/engine/prefabs/basics.js +131 -0
- package/dist/engine/prefabs/devices.js +295 -0
- package/dist/engine/prefabs/icons.js +49 -0
- package/dist/engine/prefabs/index.js +12 -0
- package/dist/engine/prefabs/logistics.js +156 -0
- package/dist/engine/scene.js +466 -0
- package/dist/engine/shapes.js +781 -0
- package/dist/engine/sort.js +153 -0
- package/dist/engine/spec.js +81 -0
- package/dist/engine/style.js +87 -0
- package/dist/engine.css +1 -0
- package/dist/figure.css +93 -0
- package/dist/figure.d.ts +22 -0
- package/dist/figure.js +73 -0
- package/dist/filters.d.ts +9 -0
- package/dist/filters.js +22 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/iso.global.js +7990 -0
- package/dist/kernel.d.ts +102 -0
- package/dist/kernel.js +172 -0
- package/dist/loops.d.ts +49 -0
- package/dist/loops.js +64 -0
- package/dist/patterns.d.ts +44 -0
- package/dist/patterns.js +97 -0
- package/dist/theme.css +19 -0
- package/dist/types.d.ts +86 -0
- package/dist/types.js +1 -0
- package/package.json +50 -4
- package/skills/iso-engine/SKILL.md +143 -0
- package/skills/iso-engine/examples/engine.html +42 -0
- package/skills/iso-engine/examples/figure.html +51 -0
- package/skills/iso-engine/examples/loop.html +68 -0
- package/skills/iso-engine/examples/loop.js +36 -0
- package/skills/iso-engine/examples/matcher.js +134 -0
- package/skills/iso-engine/examples/spec.html +42 -0
- package/skills/iso-engine/references/animation.md +116 -0
- package/skills/iso-engine/references/api.md +207 -0
- package/skills/iso-engine/references/composition.md +66 -0
- package/skills/iso-engine/references/diagrams.md +68 -0
- package/skills/iso-engine/references/figures.md +359 -0
- package/skills/iso-engine/references/loops.md +126 -0
- package/skills/iso-engine/references/prefabs.md +103 -0
- package/skills/iso-engine/references/spec.md +66 -0
- package/skills/iso-engine/references/style.md +76 -0
- package/skills/iso-engine/references/troubleshooting.md +49 -0
- package/skills/iso-engine/scripts/build.mjs +35 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* World-space paths with arc-length parameterisation, for motion and for drawing.
|
|
2
|
+
Angles are measured in plan from +x towards +y (clockwise on screen). */
|
|
3
|
+
import { DEG, v3, rrect } from './math.js';
|
|
4
|
+
import { filletPolyline } from './shapes.js';
|
|
5
|
+
|
|
6
|
+
export class Path {
|
|
7
|
+
constructor(points, closed = false) {
|
|
8
|
+
this.points = points.map((p) => [p[0], p[1], p[2] || 0]);
|
|
9
|
+
this.closed = closed;
|
|
10
|
+
if (closed && this.points.length > 1) {
|
|
11
|
+
const a = this.points[0], b = this.points[this.points.length - 1];
|
|
12
|
+
if (v3.len(v3.sub(a, b)) > 1e-6) this.points.push(a.slice());
|
|
13
|
+
}
|
|
14
|
+
this.cum = [0];
|
|
15
|
+
for (let i = 1; i < this.points.length; i++) this.cum.push(this.cum[i - 1] + v3.len(v3.sub(this.points[i], this.points[i - 1])));
|
|
16
|
+
this.length = this.cum[this.cum.length - 1];
|
|
17
|
+
}
|
|
18
|
+
_seg(u) {
|
|
19
|
+
let s = this.closed ? ((u % 1) + 1) % 1 : Math.max(0, Math.min(1, u));
|
|
20
|
+
const L = s * this.length, c = this.cum;
|
|
21
|
+
let lo = 0, hi = c.length - 1;
|
|
22
|
+
while (hi - lo > 1) { const m = (lo + hi) >> 1; if (c[m] <= L) lo = m; else hi = m; }
|
|
23
|
+
const seg = c[hi] - c[lo] || 1;
|
|
24
|
+
return { i: lo, t: (L - c[lo]) / seg };
|
|
25
|
+
}
|
|
26
|
+
/* point at normalised arc length u ∈ [0, 1] (wraps when closed) */
|
|
27
|
+
at(u) { const { i, t } = this._seg(u), p = this.points; return v3.lerp(p[i], p[Math.min(i + 1, p.length - 1)], t); }
|
|
28
|
+
tangent(u) {
|
|
29
|
+
const { i } = this._seg(u), p = this.points;
|
|
30
|
+
const a = p[i], b = p[Math.min(i + 1, p.length - 1)];
|
|
31
|
+
return v3.norm(v3.sub(b, a));
|
|
32
|
+
}
|
|
33
|
+
/* heading in plan, degrees */
|
|
34
|
+
heading(u) { const t = this.tangent(u); return Math.atan2(t[1], t[0]) / DEG; }
|
|
35
|
+
reverse() { return new Path(this.points.slice().reverse(), false); }
|
|
36
|
+
offset(dx = 0, dy = 0, dz = 0) { return new Path(this.points.map((p) => [p[0] + dx, p[1] + dy, p[2] + dz]), false); }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const path = {
|
|
40
|
+
line: (a, b) => new Path([a, b]),
|
|
41
|
+
polyline: (pts, opt = {}) => new Path(opt.radius ? filletPolyline(pts.map((p) => [p[0], p[1], p[2] || 0]), opt.radius) : pts, !!opt.closed),
|
|
42
|
+
/* circle: {center:[x,y,z], r, start=0, ccw=false, n} */
|
|
43
|
+
circle(o) {
|
|
44
|
+
const [x, y, z] = o.center || [0, 0, 0], r = o.r || 10, n = o.n || 96, s = o.start || 0, dir = o.ccw ? -1 : 1;
|
|
45
|
+
const pts = [];
|
|
46
|
+
for (let i = 0; i <= n; i++) { const a = (s + dir * (360 * i) / n) * DEG; pts.push([x + r * Math.cos(a), y + r * Math.sin(a), z || 0]); }
|
|
47
|
+
return new Path(pts, true);
|
|
48
|
+
},
|
|
49
|
+
arc(o) {
|
|
50
|
+
const [x, y, z] = o.center || [0, 0, 0], r = o.r || 10, a0 = o.from ?? 0, a1 = o.to ?? 90;
|
|
51
|
+
const n = o.n || Math.max(8, Math.ceil(Math.abs(a1 - a0) / 4)), pts = [];
|
|
52
|
+
for (let i = 0; i <= n; i++) { const a = (a0 + ((a1 - a0) * i) / n) * DEG; pts.push([x + r * Math.cos(a), y + r * Math.sin(a), z || 0]); }
|
|
53
|
+
return new Path(pts);
|
|
54
|
+
},
|
|
55
|
+
/* cubic bezier in 3D */
|
|
56
|
+
bezier(p0, p1, p2, p3, n = 48) {
|
|
57
|
+
const pts = [];
|
|
58
|
+
for (let i = 0; i <= n; i++) {
|
|
59
|
+
const t = i / n, m = 1 - t;
|
|
60
|
+
pts.push([0, 1, 2].map((k) => m * m * m * p0[k] + 3 * m * m * t * p1[k] + 3 * m * t * t * p2[k] + t * t * t * p3[k]));
|
|
61
|
+
}
|
|
62
|
+
return new Path(pts);
|
|
63
|
+
},
|
|
64
|
+
/* a hop between two points: up, across, down (parcel arcs, data hops) */
|
|
65
|
+
hop(a, b, height = 20, n = 32) {
|
|
66
|
+
const pts = [];
|
|
67
|
+
for (let i = 0; i <= n; i++) { const t = i / n; const p = v3.lerp(a, b, t); p[2] += Math.sin(Math.PI * t) * height; pts.push(p); }
|
|
68
|
+
return new Path(pts);
|
|
69
|
+
},
|
|
70
|
+
/* rounded rectangle loop on a plane: {at:[x,y,z], size:[w,d], r} (clockwise from the back corner) */
|
|
71
|
+
rect(o) {
|
|
72
|
+
const [x, y, z] = o.at || [0, 0, 0], [w, d] = o.size || [100, 100];
|
|
73
|
+
return new Path(rrect(x, y, w, d, o.r || 0, 8).map((p) => [p[0], p[1], z || 0]), true);
|
|
74
|
+
},
|
|
75
|
+
join(...paths) { return new Path(paths.flatMap((p, i) => (i ? p.points.slice(1) : p.points))); },
|
|
76
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/* Architecture & diagram prefabs: building, tower, gate, conveyor, stack, block. */
|
|
2
|
+
import { rng, fmt, cssClass } from '../math.js';
|
|
3
|
+
import { iconPath } from './icons.js';
|
|
4
|
+
|
|
5
|
+
export default function install(define, Iso) {
|
|
6
|
+
/* Building with a window grid on both visible faces. lit: fraction of lit windows (seeded). */
|
|
7
|
+
define('building', (g, o) => {
|
|
8
|
+
const w = o.w ?? 40, d = o.d ?? 32, h = o.h ?? 60, floors = o.floors ?? Math.max(2, Math.round(h / 10));
|
|
9
|
+
const R = rng(o.seed ?? 11), lit = o.lit ?? 0.12, win = o.windows || 'grid';
|
|
10
|
+
const each = () => (R() < lit ? { fill: 'f-lit' } : null);
|
|
11
|
+
const facade = (f, cols) => {
|
|
12
|
+
const top = 5, bot = o.door ? 9 : 4;
|
|
13
|
+
if (win === 'bands') { for (let i = 0; i < floors; i++) { const v = top + (i * (f.h - top - bot)) / floors; f.rect(3, v + 1, f.w - 6, (f.h - top - bot) / floors - 3, { r: 0.6, fill: 'f-screen', line: 'ln-faint' }); } return; }
|
|
14
|
+
f.grid(3, top, f.w - 6, f.h - top - bot, cols, floors, { gap: 1.6, r: 0.4, fill: 'f-screen', line: 'ln-faint', each });
|
|
15
|
+
if (o.door) f.rect(f.w / 2 - 3, f.h - 8, 6, 8, { r: 0.8, fill: 'f-dark', line: 'ln-soft' });
|
|
16
|
+
};
|
|
17
|
+
g.box({
|
|
18
|
+
at: [-w / 2, -d / 2, 0], size: [w, d, h], r: o.r ?? 1, chamfer: o.chamfer ?? 1, material: o.material, color: o.color,
|
|
19
|
+
left: (f) => facade(f, Math.max(2, Math.round(w / 7))),
|
|
20
|
+
right: (f) => facade(f, Math.max(2, Math.round(d / 7))),
|
|
21
|
+
top: (f) => f.rect(2, 2, w - 4, d - 4, { r: 0.6, line: 'ln-faint' }),
|
|
22
|
+
});
|
|
23
|
+
if (o.roof !== false) {
|
|
24
|
+
g.box({ at: [-w / 2 + w * 0.15, -d / 2 + d * 0.2, h], size: [w * 0.3, d * 0.28, 4], r: 0.8 });
|
|
25
|
+
if (o.antenna) g.cylinder({ at: [w * 0.25, d * 0.2, h], r: 0.6, h: o.antenna });
|
|
26
|
+
}
|
|
27
|
+
}, { sort: 'none' });
|
|
28
|
+
|
|
29
|
+
/* Cylindrical tower (water tanks, silos, round offices) */
|
|
30
|
+
define('tower', (g, o) => {
|
|
31
|
+
const r = o.r ?? 14, h = o.h ?? 50;
|
|
32
|
+
g.cylinder({ at: [0, 0, 0], r, h, chamfer: 1, bands: o.bands || Array.from({ length: Math.max(1, Math.floor(h / 10) - 1) }, (_, i) => (i + 1) * 10), ribs: o.ribs ?? 24, material: o.material, color: o.color });
|
|
33
|
+
if (o.cap !== false) g.cone({ at: [0, 0, h], r: r * 0.92, h: r * 0.45 });
|
|
34
|
+
}, { sort: 'none' });
|
|
35
|
+
|
|
36
|
+
/* Glass portal: two posts, a beam, a glass pane; axis 'x' spans along x (pane faces +y) */
|
|
37
|
+
define('gate', (g, o) => {
|
|
38
|
+
const w = o.w ?? 40, h = o.h ?? 46, t = o.t ?? 4, axis = o.axis || 'x';
|
|
39
|
+
if (axis === 'x') {
|
|
40
|
+
g.box({ at: [-w / 2, -t / 2, 0], size: [t, t, h], r: 0.8 });
|
|
41
|
+
g.pane({ plane: 'left', at: [-w / 2 + t, 0, h - t], w: w - 2 * t, h: h - t, material: 'glass', fill: 'f-glass', line: 'ln-soft', draw: (f) => { f.line(f.w * 0.2, 4, f.w * 0.05, 18, 'ln-faint'); f.line(f.w * 0.32, 4, f.w * 0.12, 30, 'ln-faint'); } });
|
|
42
|
+
g.box({ at: [w / 2 - t, -t / 2, 0], size: [t, t, h], r: 0.8 });
|
|
43
|
+
g.box({ at: [-w / 2, -t / 2, h], size: [w, t, t], r: 0.8 });
|
|
44
|
+
} else {
|
|
45
|
+
g.box({ at: [-t / 2, w / 2 - t, 0], size: [t, t, h], r: 0.8 });
|
|
46
|
+
g.pane({ plane: 'right', at: [0, w / 2 - t, h - t], w: w - 2 * t, h: h - t, material: 'glass', fill: 'f-glass', line: 'ln-soft', draw: (f) => { f.line(f.w * 0.2, 4, f.w * 0.05, 18, 'ln-faint'); f.line(f.w * 0.32, 4, f.w * 0.12, 30, 'ln-faint'); } });
|
|
47
|
+
g.box({ at: [-t / 2, -w / 2, 0], size: [t, t, h], r: 0.8 });
|
|
48
|
+
g.box({ at: [-t / 2, -w / 2, h], size: [t, w, t], r: 0.8 });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/* Conveyor belt along x (or y). The belt surface is at z = h. track() → world Path along the belt. */
|
|
53
|
+
/* Conveyor belt along x (or y). Belt surface at z = h. Tall belts (h > 10) stand on legs with side
|
|
54
|
+
rails; the parts sort individually so boxes ride between the rails. track() → world path. */
|
|
55
|
+
define('conveyor', (g, o) => {
|
|
56
|
+
const L = o.length ?? 120, w = o.w ?? 18, h = o.h ?? 8, axis = o.axis || 'x', rollers = o.rollers ?? Math.round(L / 8);
|
|
57
|
+
const legs = o.legs ?? h > 10, rails = o.rails ?? legs;
|
|
58
|
+
const X = (a, b) => (axis === 'x' ? a : b); // pick by axis
|
|
59
|
+
const rollerFace = (f) => {
|
|
60
|
+
if (axis === 'x') for (let i = 0; i <= rollers; i++) { const u = 3 + (i * (L - 6)) / rollers; f.line(u, 2.5, u, w - 2.5, 'ln-faint'); }
|
|
61
|
+
else for (let i = 0; i <= rollers; i++) { const v = 3 + (i * (L - 6)) / rollers; f.line(2.5, v, w - 2.5, v, 'ln-faint'); }
|
|
62
|
+
if (axis === 'x') { f.line(0, 2.2, L, 2.2, 'ln-soft'); f.line(0, w - 2.2, L, w - 2.2, 'ln-soft'); }
|
|
63
|
+
else { f.line(2.2, 0, 2.2, L, 'ln-soft'); f.line(w - 2.2, 0, w - 2.2, L, 'ln-soft'); }
|
|
64
|
+
};
|
|
65
|
+
if (!legs) {
|
|
66
|
+
const size = X([L, w, h], [w, L, h]);
|
|
67
|
+
g.box({ at: [-size[0] / 2, -size[1] / 2, 0], size, r: 1.5, chamfer: 0.8, material: o.material, top: rollerFace,
|
|
68
|
+
left: (f) => f.hlines(4, f.h * 0.5, f.w - 8, 0, 1, 'ln-faint') });
|
|
69
|
+
} else {
|
|
70
|
+
const t = o.belt ?? 4, z0 = h - t, n = Math.max(2, Math.round(L / (o.legSpacing ?? 34)) + 1), p = 2;
|
|
71
|
+
for (let i = 0; i < n; i++) {
|
|
72
|
+
const s = -L / 2 + 3 + (i * (L - 6 - p)) / (n - 1);
|
|
73
|
+
for (const side of [-1, 1]) {
|
|
74
|
+
const q = side < 0 ? -w / 2 + 1 : w / 2 - 1 - p;
|
|
75
|
+
g.box({ at: X([s, q, 0], [q, s, 0]), size: [p, p, z0], material: o.material });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const size = X([L, w, t], [w, L, t]);
|
|
79
|
+
g.box({ at: [-size[0] / 2, -size[1] / 2, z0], size, r: 1, chamfer: 0.5, material: o.material, top: rollerFace,
|
|
80
|
+
left: axis === 'x' ? (f) => f.hlines(3, t / 2, f.w - 6, 0, 1, 'ln-faint') : undefined,
|
|
81
|
+
right: axis === 'y' ? (f) => f.hlines(3, t / 2, f.w - 6, 0, 1, 'ln-faint') : undefined });
|
|
82
|
+
if (rails) {
|
|
83
|
+
const rh = o.railH ?? 2.6, rt = 1.2;
|
|
84
|
+
for (const side of [-1, 1]) {
|
|
85
|
+
const q = side < 0 ? -w / 2 : w / 2 - rt;
|
|
86
|
+
g.box({ class: 'rail', at: X([-L / 2, q, h], [q, -L / 2, h]), size: X([L, rt, rh], [rt, L, rh]), r: 0.4, material: o.material });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
track(lift = 0) {
|
|
92
|
+
const at = this.opts.at || [0, 0, 0];
|
|
93
|
+
const a = axis === 'x' ? [at[0] - L / 2 + 4, at[1], at[2] + h + lift] : [at[0], at[1] - L / 2 + 4, at[2] + h + lift];
|
|
94
|
+
const b = axis === 'x' ? [at[0] + L / 2 - 4, at[1], at[2] + h + lift] : [at[0], at[1] + L / 2 - 4, at[2] + h + lift];
|
|
95
|
+
return Iso.path.line(a, b);
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}, { flat: true });
|
|
99
|
+
|
|
100
|
+
/* Layered stack (architecture "layer cake"). layers: [{label, material, color}] bottom → top */
|
|
101
|
+
define('stack', (g, o) => {
|
|
102
|
+
const [w, d] = o.size || [90, 70], t = o.t ?? 6, gap = o.gap ?? 14, layers = o.layers || [{ label: 'Layer' }];
|
|
103
|
+
layers.forEach((L, i) => {
|
|
104
|
+
const z = i * (t + gap);
|
|
105
|
+
g.box({
|
|
106
|
+
id: o.id ? o.id + '-' + i : undefined, class: 'layer', at: [-w / 2, -d / 2, z], size: [w, d, t], r: o.r ?? 6, chamfer: 1.2, material: L.material, color: L.color, glow: L.glow,
|
|
107
|
+
top: L.icon ? (f) => f.svg('<path class="ln-strong" d="' + iconPath(L.icon) + '"/>', { u: w / 2 - 9, v: d / 2 - 9, transform: 'scale(.75)' }) : undefined,
|
|
108
|
+
left: L.label && o.labels === 'face' ? (f) => f.text(L.label, 8, t / 2 + 2, { size: Math.min(5, t * 0.8), cls: 'tx tx-label', spacing: 0.8 }) : undefined,
|
|
109
|
+
});
|
|
110
|
+
if (L.label && o.labels !== 'face') {
|
|
111
|
+
/* callout to the left of the layer */
|
|
112
|
+
const ax = -w / 2, ay = d / 2, az = z + t / 2, side = o.labelSide === 'right';
|
|
113
|
+
const p0 = side ? [w / 2, -d / 2 + 4, az] : [ax + 4, ay, az];
|
|
114
|
+
const p1 = side ? [w / 2 + 16, -d / 2 + 4, az] : [ax + 4, ay + 16, az];
|
|
115
|
+
g.line({ points: [p0, p1], line: 'ln-soft' });
|
|
116
|
+
g.text({ text: L.label, at: p1, size: o.labelSize || 6, anchor: side ? 'start' : 'end', dx: side ? 4 : -4, dy: 2, cls: 'tx tx-label' });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
/* Diagram node: a rounded block with an icon on top and a label on the front face. */
|
|
122
|
+
define('block', (g, o) => {
|
|
123
|
+
const [w, d, h] = o.size || [36, 36, 12];
|
|
124
|
+
g.box({
|
|
125
|
+
at: [-w / 2, -d / 2, 0], size: [w, d, h], r: o.r ?? 5, chamfer: o.chamfer ?? 1.5, material: o.material, color: o.color, glow: o.glow,
|
|
126
|
+
top: o.icon ? (f) => {
|
|
127
|
+
const s = (o.iconSize ?? Math.min(w, d) * 0.5) / 24;
|
|
128
|
+
f.svg('<path class="' + cssClass(o.material === 'lit' ? 'ln-strong' : o.iconClass || 'ln-strong') + '" d="' + iconPath(o.icon) + '"/>', { u: (f.w - 24 * s) / 2, v: (f.h - 24 * s) / 2, transform: 'scale(' + fmt(s) + ')' });
|
|
129
|
+
} : undefined,
|
|
130
|
+
left: o.label ? (f) => {
|
|
131
|
+
const size = o.labelSize || Math.min(5.5, h * 0.42, (f.w - 6) / (String(o.label).length * 0.72));
|
|
132
|
+
f.text(o.label, f.w / 2, f.h / 2 + size * 0.36, { size, anchor: 'middle', cls: o.material === 'lit' || o.material === 'accent' ? 'tx tx-label tx-on-lit' : 'tx tx-label' });
|
|
133
|
+
} : undefined,
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/* Scenery prefabs: platforms, trees, plants, lamps, markers, zones, plaques, desks.
|
|
2
|
+
Local origin = centre of the footprint at the base; `at` and `rot` place the prefab. */
|
|
3
|
+
import { rng } from '../math.js';
|
|
4
|
+
|
|
5
|
+
export default function install(define) {
|
|
6
|
+
/* The slab every diorama sits on. Its top is z = 0, so objects at z = 0 stand on it. */
|
|
7
|
+
define('platform', (g, o) => {
|
|
8
|
+
const [w, d] = o.size || [240, 180], h = o.h ?? 10, r = o.r ?? 12, inset = o.inset ?? 10;
|
|
9
|
+
g.box({ at: [-w / 2, -d / 2, -h], size: [w, d, h], r, chamfer: o.chamfer ?? 3, material: o.material, color: o.color });
|
|
10
|
+
if (inset) g.rect({ at: [-w / 2 + inset, -d / 2 + inset, 0], size: [w - 2 * inset, d - 2 * inset], r: Math.max(r - inset / 2, 2), line: 'ln-faint' });
|
|
11
|
+
if (o.grid) g.grid({ at: [-w / 2 + inset, -d / 2 + inset, 0], size: [w - 2 * inset, d - 2 * inset], step: o.grid });
|
|
12
|
+
if (o.screws) {
|
|
13
|
+
const m = inset + 6;
|
|
14
|
+
for (const [x, y] of [[-w / 2 + m, -d / 2 + m], [w / 2 - m, -d / 2 + m], [-w / 2 + m, d / 2 - m], [w / 2 - m, d / 2 - m]]) g.circle({ at: [x, y, 0], r: 1.6, line: 'ln-soft' });
|
|
15
|
+
}
|
|
16
|
+
if (o.label) g.text({ text: o.label, at: [-w / 2 + inset + 6, d / 2 - inset / 2 + 2, 0], plane: 'top', size: o.labelSize || 6, cls: 'tx tx-label tx-floor', spacing: o.labelSpacing ?? 1.6 });
|
|
17
|
+
if (o.labelRight) g.text({ text: o.labelRight, at: [w / 2 - inset / 2 - 2, d / 2 - inset - 6, 0], plane: 'top-y', size: o.labelSize || 6, cls: 'tx tx-label tx-floor', spacing: o.labelSpacing ?? 1.6 });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/* Trees. kind: tiered (stacked discs) · pine · cone · round · bush · lollipop */
|
|
21
|
+
define('tree', (g, o) => {
|
|
22
|
+
const kind = o.kind || 'tiered', s = o.scale ?? 1, r = (o.r ?? 6) * s, h = (o.h ?? 20) * s;
|
|
23
|
+
const trunkH = o.trunk ?? (kind === 'bush' ? 0 : h * 0.22);
|
|
24
|
+
if (trunkH > 0) g.cylinder({ at: [0, 0, 0], r: Math.max(0.6, r * 0.13), h: trunkH + (kind === 'round' ? r * 0.5 : 0.5) });
|
|
25
|
+
if (kind === 'tiered') {
|
|
26
|
+
const n = o.tiers ?? 3, step = (h - trunkH) / (n + 0.4);
|
|
27
|
+
for (let i = 0; i < n; i++) {
|
|
28
|
+
const rr = r * (1 - i * (0.55 / n));
|
|
29
|
+
g.cylinder({ at: [0, 0, trunkH + i * step], r: rr, rTop: rr * 0.86, h: step * 0.62, chamfer: Math.min(0.8, step * 0.2) });
|
|
30
|
+
}
|
|
31
|
+
g.cone({ at: [0, 0, trunkH + n * step], r: r * 0.22, h: step * 0.5 });
|
|
32
|
+
} else if (kind === 'pine') {
|
|
33
|
+
const n = o.tiers ?? 3;
|
|
34
|
+
for (let i = 0; i < n; i++) g.cone({ at: [0, 0, trunkH + (i * (h - trunkH)) / (n + 1)], r: r * (1 - i * 0.22), h: ((h - trunkH) * 1.9) / (n + 1) });
|
|
35
|
+
} else if (kind === 'cone') {
|
|
36
|
+
g.cone({ at: [0, 0, trunkH], r, h: h - trunkH });
|
|
37
|
+
} else if (kind === 'round') {
|
|
38
|
+
g.sphere({ at: [0, 0, trunkH + r], r });
|
|
39
|
+
} else if (kind === 'bush') {
|
|
40
|
+
g.sphere({ at: [0, 0, r * 0.75], r });
|
|
41
|
+
} else if (kind === 'lollipop') {
|
|
42
|
+
g.cylinder({ at: [0, 0, trunkH], r, h: r * 0.35, chamfer: r * 0.08 });
|
|
43
|
+
}
|
|
44
|
+
}, { sort: 'none' });
|
|
45
|
+
|
|
46
|
+
/* Scatter helper: trees (or any prefab) on a ring or in a rectangle, seeded. */
|
|
47
|
+
define('forest', (g, o) => {
|
|
48
|
+
const R = rng(o.seed ?? 3), n = o.count ?? 30, kinds = o.kinds || ['tiered'], s0 = o.minScale ?? 0.7, s1 = o.maxScale ?? 1.15;
|
|
49
|
+
const pts = [];
|
|
50
|
+
const minD = o.spacing ?? 9;
|
|
51
|
+
for (let tries = 0; pts.length < n && tries < n * 40; tries++) {
|
|
52
|
+
let x, y;
|
|
53
|
+
if (o.ring) { const [ri, ro] = o.ring, a = R() * Math.PI * 2, rr = Math.sqrt(R() * (ro * ro - ri * ri) + ri * ri); x = Math.cos(a) * rr; y = Math.sin(a) * rr; }
|
|
54
|
+
else { const [w, d] = o.size || [100, 100]; x = (R() - 0.5) * w; y = (R() - 0.5) * d; }
|
|
55
|
+
if (o.avoid && o.avoid(x, y)) continue;
|
|
56
|
+
if (pts.some((p) => Math.hypot(p[0] - x, p[1] - y) < minD)) continue;
|
|
57
|
+
pts.push([x, y]);
|
|
58
|
+
}
|
|
59
|
+
for (const [x, y] of pts) g.tree({ at: [x, y, 0], kind: R.pick(kinds), scale: R.range(s0, s1), r: o.r, h: o.h, material: o.material, class: o.treeClass || 'tree' });
|
|
60
|
+
}, { flat: true });
|
|
61
|
+
|
|
62
|
+
define('plant', (g, o) => {
|
|
63
|
+
const r = o.r ?? 5, h = o.h ?? 8;
|
|
64
|
+
g.cylinder({ at: [0, 0, 0], r: r * 0.78, rTop: r, h, chamfer: 0.6 });
|
|
65
|
+
g.circle({ at: [0, 0, h], r: r - 1, fill: 'f-dark', line: null });
|
|
66
|
+
const R = rng(o.seed ?? 5);
|
|
67
|
+
for (let i = 0; i < (o.leaves ?? 4); i++) {
|
|
68
|
+
const a = R() * Math.PI * 2, rr = R() * r * 0.4;
|
|
69
|
+
g.sphere({ at: [Math.cos(a) * rr, Math.sin(a) * rr, h + r * (0.6 + i * 0.35)], r: r * (0.75 - i * 0.08) });
|
|
70
|
+
}
|
|
71
|
+
}, { sort: 'none' });
|
|
72
|
+
|
|
73
|
+
define('lamp', (g, o) => {
|
|
74
|
+
const h = o.h ?? 46;
|
|
75
|
+
g.cylinder({ at: [0, 0, 0], r: 4, h: 2, chamfer: 0.6 });
|
|
76
|
+
g.cylinder({ at: [0, 0, 2], r: 0.8, h: h - 6 });
|
|
77
|
+
g.box({ center: [0, 0, h - 4], size: [9, 5, 3], r: 1.2 });
|
|
78
|
+
g.box({ id: o.id ? o.id + '-bulb' : undefined, class: 'lamp-bulb', center: [0, 0, h - 4.6], size: [6, 3, 0.6], r: 0.8, material: o.on === false ? undefined : 'lit', glow: o.on !== false });
|
|
79
|
+
}, { sort: 'none' });
|
|
80
|
+
|
|
81
|
+
/* map marker: a bead on an inverted cone */
|
|
82
|
+
define('pin', (g, o) => {
|
|
83
|
+
const h = o.h ?? 18, r = o.r ?? 5;
|
|
84
|
+
if (o.shadow !== false) g.circle({ at: [0, 0, 0], r: r * 0.7, fill: 'f-dark', line: 'ln-faint' });
|
|
85
|
+
g.cylinder({ at: [0, 0, 0], r: 0.01, rTop: r * 0.62, h: h - r * 0.8 });
|
|
86
|
+
g.sphere({ at: [0, 0, h], r, material: o.material || (o.lit ? 'lit' : undefined), glow: !!o.lit });
|
|
87
|
+
}, { sort: 'none' });
|
|
88
|
+
|
|
89
|
+
/* flat dashed region with a caption (diagram zones, plots) */
|
|
90
|
+
define('zone', (g, o) => {
|
|
91
|
+
const [w, d] = o.size || [120, 80], r = o.r ?? 8;
|
|
92
|
+
g.rect({ at: [-w / 2, -d / 2, o.z || 0], size: [w, d], r, fill: o.fill === undefined ? null : o.fill, line: o.line || 'ln-soft', dash: o.dash ?? '4 4' });
|
|
93
|
+
if (o.label) g.text({ text: o.label, at: [-w / 2 + 6, d / 2 - 6, o.z || 0], plane: 'top', size: o.labelSize || 6, cls: 'tx tx-label tx-floor', spacing: 1.4 });
|
|
94
|
+
}, { sort: 'none', flatish: true });
|
|
95
|
+
|
|
96
|
+
/* small plate lying on the floor with text */
|
|
97
|
+
define('plaque', (g, o) => {
|
|
98
|
+
const text = String(o.text || ''), size = o.size || 6, w = o.w ?? text.length * size * 0.62 + 10, d = o.d ?? size + 7;
|
|
99
|
+
g.box({ at: [-w / 2, -d / 2, 0], size: [w, d, o.h ?? 1.5], r: 1.5, material: o.material });
|
|
100
|
+
g.text({ text, at: [-w / 2 + 5, d / 2 - (d - size * 0.72) / 2, o.h ?? 1.5], plane: 'top', size, cls: o.cls || 'tx tx-label', spacing: 1 });
|
|
101
|
+
}, { sort: 'none' });
|
|
102
|
+
|
|
103
|
+
define('desk', (g, o) => {
|
|
104
|
+
const [w, d] = o.size || [90, 46], h = o.h ?? 34, t = 3, leg = 2.4;
|
|
105
|
+
for (const [x, y] of [[-w / 2 + 3, -d / 2 + 3], [w / 2 - 3 - leg, -d / 2 + 3], [-w / 2 + 3, d / 2 - 3 - leg], [w / 2 - 3 - leg, d / 2 - 3 - leg]]) g.box({ at: [x, y, 0], size: [leg, leg, h - t] });
|
|
106
|
+
g.box({ at: [-w / 2, -d / 2, h - t], size: [w, d, t], r: 2, chamfer: 0.8, material: o.material });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
/* small car along x (facing +x); wheels are dark blocks */
|
|
110
|
+
define('car', (g, o) => {
|
|
111
|
+
const L = o.length ?? 22, w = o.w ?? 11;
|
|
112
|
+
for (const [x, y] of [[-L / 2 + 3, -w / 2 - 0.4], [L / 2 - 7, -w / 2 - 0.4], [-L / 2 + 3, w / 2 - 1.6], [L / 2 - 7, w / 2 - 1.6]]) g.box({ at: [x, y, 0], size: [4, 2, 3.4], r: 0.8, material: 'dark' });
|
|
113
|
+
g.box({ at: [-L / 2, -w / 2, 1.4], size: [L, w, 4], r: 2.5, chamfer: 0.6, material: o.material, color: o.color });
|
|
114
|
+
g.box({ at: [-L / 2 + 5, -w / 2 + 1, 5.4], size: [L * 0.5, w - 2, 3.4], r: 2, material: o.material, color: o.color,
|
|
115
|
+
left: (f) => f.rect(1, 0.6, f.w - 2, f.h - 1.4, { r: 0.6, fill: 'f-screen', line: null }), right: (f) => f.rect(1, 0.6, f.w - 2, f.h - 1.4, { r: 0.6, fill: 'f-screen', line: null }) });
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/* solar panel on a short post */
|
|
119
|
+
define('solar', (g, o) => {
|
|
120
|
+
const w = o.w ?? 22, d = o.d ?? 14;
|
|
121
|
+
g.box({ center: [0, 0, 0], size: [2, 2, 4] });
|
|
122
|
+
g.box({ at: [-w / 2, -d / 2, 4], size: [w, d, 1.2], r: 0.6, material: 'dark', top: (f) => f.grid(1, 1, w - 2, d - 2, 4, 3, { gap: 0.6, line: 'ln-faint' }) });
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
define('bench', (g, o) => {
|
|
126
|
+
const w = o.w ?? 30;
|
|
127
|
+
g.box({ at: [-w / 2 + 2, -3, 0], size: [2, 6, 5] });
|
|
128
|
+
g.box({ at: [w / 2 - 4, -3, 0], size: [2, 6, 5] });
|
|
129
|
+
g.box({ at: [-w / 2, -4, 5], size: [w, 8, 1.6], r: 0.8 });
|
|
130
|
+
});
|
|
131
|
+
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/* Device prefabs: phone, card, laptop, monitor, keyboard, desk computer, server, database, chip, router. */
|
|
2
|
+
import { MARK, ICONS } from './icons.js';
|
|
3
|
+
import { rng, fmt, pathData } from '../math.js';
|
|
4
|
+
import { Face } from '../shapes.js';
|
|
5
|
+
|
|
6
|
+
/* Screen content helper on a Face: mode off | ui | lit | dots | mark */
|
|
7
|
+
function screen(f, mode = 'off', o = {}) {
|
|
8
|
+
const pad = o.pad ?? 0.9, r = o.r ?? 1.6;
|
|
9
|
+
const w = f.w - 2 * pad, h = f.h - 2 * pad;
|
|
10
|
+
if (mode === 'lit') { f.rect(pad, pad, w, h, { r, fill: 'f-lit', line: 'ln-soft' }); return; }
|
|
11
|
+
f.rect(pad, pad, w, h, { r, fill: mode === 'off' ? 'f-screen' : 'f-screen', line: 'ln-soft' });
|
|
12
|
+
if (mode === 'ui') {
|
|
13
|
+
const cols = o.cols ?? 4, rows = o.rows ?? 5, gx = w * 0.14, top = h * 0.2;
|
|
14
|
+
f.grid(pad + gx, pad + top, w - 2 * gx, h * 0.62, cols, rows, { gap: Math.min(w, h) * 0.08, r: 0.6, fill: 'f-soft', line: null });
|
|
15
|
+
} else if (mode === 'dots') {
|
|
16
|
+
for (let j = 0; j < 7; j++) for (let i = 0; i < 4; i++) f.circle(pad + w * (0.2 + i * 0.2), pad + h * (0.2 + j * 0.1), 0.35, { fill: 'f-soft', line: null, n: 6 });
|
|
17
|
+
} else if (mode === 'mark') {
|
|
18
|
+
const s = Math.min(w, h) * 0.45 / 9;
|
|
19
|
+
f.svg('<path class="f-lit" d="' + MARK + '"/>', { u: pad + w / 2 - 4.5 * s, v: pad + h / 2 - 4.5 * s, transform: 'scale(' + fmt(s) + ')' });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default function install(define) {
|
|
24
|
+
/* Standing phone, screen toward +y (use facing:'right' to turn it). screen: off | ui | lit | dots | mark */
|
|
25
|
+
define('phone', (g, o) => {
|
|
26
|
+
const w = o.w ?? 13, h = o.h ?? 27, t = o.t ?? 1.6, r = o.r ?? 2.2, mode = o.screen || 'ui';
|
|
27
|
+
let z = 0;
|
|
28
|
+
if (o.stand) { g.box({ center: [0, 0, 0], size: [w * 0.75, 7, 2.2], r: 1.2, chamfer: 0.5 }); z = 2.2; }
|
|
29
|
+
if (o.lying) {
|
|
30
|
+
g.panel({ plane: 'top', at: [-w / 2, -h / 2, t], w, h, t, r, front: (f) => { screen(f, mode, { r: r - 0.6 }); f.rect(w / 2 - 2, 1.6, 4, 1, { r: 0.5, fill: 'f-ink', line: null }); } });
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
g.panel({
|
|
34
|
+
plane: 'left', at: [-w / 2, t / 2, z], w, h, t, r, material: o.material,
|
|
35
|
+
front: (f) => {
|
|
36
|
+
screen(f, mode, { r: r - 0.6 });
|
|
37
|
+
f.rect(w / 2 - 2, 1.7, 4, 1.1, { r: 0.55, fill: mode === 'lit' ? 'f-ink' : 'f-bg', line: null });
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}, { sort: 'none' });
|
|
41
|
+
|
|
42
|
+
/* Thin card lying flat; lit by default (the "one lit moment") */
|
|
43
|
+
define('card', (g, o) => {
|
|
44
|
+
const w = o.w ?? 16, d = o.d ?? 10, t = o.t ?? 1.2, lit = o.lit !== false;
|
|
45
|
+
g.box({
|
|
46
|
+
center: [0, 0, 0], size: [w, d, t], r: o.r ?? 1.2, material: lit ? 'lit' : o.material, glow: o.glow ?? lit,
|
|
47
|
+
top: (f) => { f.rect(2, 2.2, 3.4, 2.6, { r: 0.6, line: 'ln-soft' }); f.line(2, d - 2.4, w * 0.55, d - 2.4, 'ln-soft'); },
|
|
48
|
+
});
|
|
49
|
+
}, { sort: 'none' });
|
|
50
|
+
|
|
51
|
+
/* Open laptop facing +y. open: lid angle in degrees from the base (default 105). */
|
|
52
|
+
define('laptop', (g, o) => {
|
|
53
|
+
const w = o.w ?? 40, d = o.d ?? 28, t = 2.2, lt = 1.2, ang = ((o.open ?? 105) * Math.PI) / 180, L = d * 0.92;
|
|
54
|
+
g.box({ at: [-w / 2, -d / 2, 0], size: [w, d, t], r: 1.6, chamfer: 0.5, material: o.material,
|
|
55
|
+
top: (f) => { f.grid(4, 3.5, w - 8, d * 0.42, 12, 4, { gap: 0.8, r: 0.3, line: 'ln-faint' }); f.rect(w / 2 - 7, d * 0.58, 14, d * 0.3, { r: 1, line: 'ln-faint' }); } });
|
|
56
|
+
const dir = [0, Math.cos(ang), Math.sin(ang)], back = [0, -Math.sin(ang) * lt, Math.cos(ang) * lt];
|
|
57
|
+
const hinge = [-w / 2, -d / 2 + lt, t];
|
|
58
|
+
g.slab({ origin: hinge, u: [w, 0, 0], v: [dir[0] * L, dir[1] * L, dir[2] * L], w: [back[0], back[1], back[2]], material: o.material });
|
|
59
|
+
const mode = o.screen || 'ui';
|
|
60
|
+
g.custom({
|
|
61
|
+
bounds: [-w / 2, -d / 2, t, w / 2, -d / 2 + lt, t + L],
|
|
62
|
+
draw: (dd) => {
|
|
63
|
+
const top = [hinge[0], hinge[1] + dir[1] * L, hinge[2] + dir[2] * L];
|
|
64
|
+
const f = Face.local(dd, top, [1, 0, 0], [0, -dir[1], -dir[2]], w, L);
|
|
65
|
+
const s = f.sub(1.8, 1.8, w - 3.6, L - 3.6);
|
|
66
|
+
screen(s, mode, { r: 1, pad: 0, cols: 6, rows: 3 });
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}, { sort: 'none' });
|
|
70
|
+
|
|
71
|
+
/* Monitor on a stand facing +y */
|
|
72
|
+
define('monitor', (g, o) => {
|
|
73
|
+
const w = o.w ?? 48, h = o.h ?? 30, t = 2;
|
|
74
|
+
g.box({ center: [0, -2, 0], size: [16, 10, 1.6], r: 2, chamfer: 0.5 });
|
|
75
|
+
g.box({ center: [0, -3.5, 1.6], size: [4, 2, 10], r: 0.8 });
|
|
76
|
+
g.panel({ plane: 'left', at: [-w / 2, 0, 8], w, h, t, r: 1.6, material: o.material, front: (f) => screen(f, o.screen || 'ui', { r: 1, cols: 6, rows: 3, pad: 1.4 }) });
|
|
77
|
+
}, { sort: 'none' });
|
|
78
|
+
|
|
79
|
+
/* Keyboard; keys are nodes with id `${id}-key-<name>` so they can be pressed. */
|
|
80
|
+
define('keyboard', (g, o) => {
|
|
81
|
+
const w = o.w ?? 186, d = o.d ?? 52, h = o.h ?? 7;
|
|
82
|
+
buildKeyboard(g, { x: -w / 2, y: -d / 2, z: 0, w, d, h, id: o.id });
|
|
83
|
+
return keyboardAPI(g);
|
|
84
|
+
}, { sort: 'none' });
|
|
85
|
+
|
|
86
|
+
/* Fig. 02 · the reference desk computer: base plate, body with CRT screen, keyboard, coiled cable.
|
|
87
|
+
API: power(on?), type(key), press(key, down). Emits status through opts.onStatus(text). */
|
|
88
|
+
define('deskComputer', (g, o) => {
|
|
89
|
+
const ox = -138, oy = -114; // source coordinates → centred
|
|
90
|
+
const X = (x) => x + ox, Y = (y) => y + oy;
|
|
91
|
+
const BASE = { w: 276, d: 228, h: 14 }, BODY = { x: 70, y: 24, w: 130, d: 118, h: 168, z: 14 }, KB = { x: 40, y: 162, w: 186, d: 52, h: 7, z: 14 };
|
|
92
|
+
const YF = Y(BODY.y + BODY.d), XR = X(BODY.x + BODY.w), z0 = BODY.z;
|
|
93
|
+
if (o.base !== false) {
|
|
94
|
+
g.group({ id: o.id ? o.id + '-base' : undefined, sort: 'none' }, (b) => {
|
|
95
|
+
b.box({ at: [X(0), Y(0), 0], size: [BASE.w, BASE.d, BASE.h], r: 12, chamfer: 3 });
|
|
96
|
+
b.rect({ at: [X(11), Y(11), BASE.h], size: [BASE.w - 22, BASE.d - 22], r: 6, line: 'ln-soft' });
|
|
97
|
+
for (const [cx, cy] of [[18, 18], [BASE.w - 18, 18], [18, BASE.d - 18], [BASE.w - 18, BASE.d - 18]]) b.circle({ at: [X(cx), Y(cy), BASE.h], r: 1.7, line: 'ln-soft' });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const bx = X(BODY.x + 11), bz = z0 + 56;
|
|
101
|
+
const body = g.box({
|
|
102
|
+
id: o.id ? o.id + '-body' : undefined, at: [X(BODY.x), Y(BODY.y), z0], size: [BODY.w, BODY.d, BODY.h], r: 10, chamfer: 3,
|
|
103
|
+
top: (f) => f.rect(22, 50, BODY.w - 44, 8, { r: 4, line: 'ln-soft' }),
|
|
104
|
+
left: (f) => {
|
|
105
|
+
f.rect(11, BODY.h - 56 - 90, 108, 90, { r: 9, fill: 'f-bevel', line: 'ln-soft' });
|
|
106
|
+
f.vlines(13, BODY.h - 36, 8 * 3.3 - 3.3, 22, 8);
|
|
107
|
+
f.rect(72, BODY.h - 24 - 7, 48, 7, { r: 3.5, line: 'ln-soft' });
|
|
108
|
+
f.line(78, BODY.h - 27.5, 114, BODY.h - 27.5);
|
|
109
|
+
},
|
|
110
|
+
right: (f) => {
|
|
111
|
+
f.vlines(62, BODY.h - 148, 8 * 3.6, 44, 9);
|
|
112
|
+
f.rect(98, BODY.h - 38, 11, 20, { r: 2, line: 'ln-soft' });
|
|
113
|
+
f.line(103.5, BODY.h - 36, 103.5, BODY.h - 28);
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
/* the screen: its own node so it can animate without redrawing the body */
|
|
117
|
+
const scr = g.pane({
|
|
118
|
+
id: o.id ? o.id + '-screen' : undefined, plane: 'left', at: [bx + 6, YF, bz + 6 + 78], w: 96, h: 78, fill: false, line: false,
|
|
119
|
+
power: 0, text: '', cursor: true, logo: o.logo,
|
|
120
|
+
draw: (f, n) => drawCRT(f, n),
|
|
121
|
+
});
|
|
122
|
+
const led = g.pane({ id: o.id ? o.id + '-led' : undefined, plane: 'left', at: [X(BODY.x + 14), YF, z0 + 45], w: 3, h: 3, r: 1.5, fill: 'f-soft', line: null });
|
|
123
|
+
g.box({ at: [XR, YF - 52, z0 + 8], size: [7, 9, 7], r: 2, n: 3 });
|
|
124
|
+
const kb = g.group({ id: o.id ? o.id + '-keyboard' : undefined, sort: 'none' }, (k) => buildKeyboard(k, { x: X(KB.x), y: Y(KB.y), z: KB.z, w: KB.w, d: KB.d, h: KB.h, id: o.id }));
|
|
125
|
+
g.helix({ a: [XR + 7, YF - 47.5, z0 + 11.5], b: [X(KB.x + KB.w + 2), Y(KB.y + 12), KB.z + 5], r: 2.6, turns: 15 });
|
|
126
|
+
g.box({ at: [X(KB.x + KB.w), Y(KB.y + 9), KB.z + 2], size: [4, 6, 5], r: 1.6, n: 3 });
|
|
127
|
+
|
|
128
|
+
const state = { on: false, text: '', count: 0, last: '' };
|
|
129
|
+
const status = () => { if (o.onStatus) o.onStatus(state.on ? 'on · ' + state.count + ' chars' + (state.last ? ' · key ' + state.last : '') : 'off', state); };
|
|
130
|
+
const kapi = keyboardAPI(kb);
|
|
131
|
+
return {
|
|
132
|
+
state, screenNode: scr, body, keyboard: kb,
|
|
133
|
+
onMount(scene) {
|
|
134
|
+
scene.loop((t) => { if (scr.opts.power >= 1) scr.set('cursor', Math.floor(t * 1.9) % 2 === 0); });
|
|
135
|
+
status();
|
|
136
|
+
},
|
|
137
|
+
power(on = !state.on) {
|
|
138
|
+
const s = this.scene;
|
|
139
|
+
state.on = on;
|
|
140
|
+
if (!on) { state.text = ''; state.count = 0; state.last = ''; scr.set('text', ''); }
|
|
141
|
+
led.set('fill', on ? 'f-lit' : 'f-soft'); led.set('glow', on);
|
|
142
|
+
if (s) s.to(scr, { power: on ? 1 : 0 }, { duration: on ? 1.1 : 0.32, ease: 'linear' });
|
|
143
|
+
else scr.set('power', on ? 1 : 0);
|
|
144
|
+
this.set('pressed', on);
|
|
145
|
+
status();
|
|
146
|
+
return this;
|
|
147
|
+
},
|
|
148
|
+
type(key) {
|
|
149
|
+
if (!state.on) return this;
|
|
150
|
+
state.last = key === ' ' ? 'space' : key;
|
|
151
|
+
if (key.length === 1) { state.text += key; state.count++; }
|
|
152
|
+
else if (key === 'Backspace') state.text = state.text.slice(0, -1);
|
|
153
|
+
else if (key === 'Enter') state.text = '';
|
|
154
|
+
scr.set('text', state.text);
|
|
155
|
+
status();
|
|
156
|
+
return this;
|
|
157
|
+
},
|
|
158
|
+
press: kapi.press,
|
|
159
|
+
};
|
|
160
|
+
}, { sort: 'none' });
|
|
161
|
+
|
|
162
|
+
/* Server rack facing +y; LEDs have class "led" (blink them with scene.blink('.led')). */
|
|
163
|
+
define('server', (g, o) => {
|
|
164
|
+
const w = o.w ?? 28, d = o.d ?? 34, h = o.h ?? 64, units = o.units ?? 6, R = rng(o.seed ?? 2);
|
|
165
|
+
const uh = (h - 8) / units;
|
|
166
|
+
g.box({
|
|
167
|
+
at: [-w / 2, -d / 2, 0], size: [w, d, h], r: 2, chamfer: 1, material: o.material,
|
|
168
|
+
left: (f) => {
|
|
169
|
+
for (let i = 0; i < units; i++) {
|
|
170
|
+
const v = 4 + i * uh;
|
|
171
|
+
f.rect(3, v + 0.8, w - 6, uh - 1.6, { r: 0.8, fill: 'f-bevel', line: 'ln-soft' });
|
|
172
|
+
f.hlines(w * 0.45, v + uh * 0.3, w * 0.38, uh * 0.4, 3, 'ln-faint');
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
right: (f) => { f.vlines(5, 8, d - 10, h * 0.35, 10, 'ln-faint'); },
|
|
176
|
+
top: (f) => f.rect(3, 3, w - 6, d - 6, { r: 1, line: 'ln-faint' }),
|
|
177
|
+
});
|
|
178
|
+
for (let i = 0; i < units; i++) {
|
|
179
|
+
const v = 4 + i * uh;
|
|
180
|
+
for (let k = 0; k < 2; k++) {
|
|
181
|
+
g.pane({ id: o.id ? o.id + '-led-' + i + '-' + k : undefined, class: 'led', plane: 'left', at: [-w / 2 + 5.5 + k * 3.2, d / 2 + 0.05, h - v - uh * 0.5 + 0.8], w: 1.6, h: 1.6, r: 0.8, fill: R() < 0.6 ? 'f-lit' : 'f-soft', line: null });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}, { sort: 'none' });
|
|
185
|
+
|
|
186
|
+
/* Database: stacked discs */
|
|
187
|
+
define('database', (g, o) => {
|
|
188
|
+
const r = o.r ?? 16, tiers = o.tiers ?? 3, th = o.tierH ?? 8, gap = o.gap ?? 1.6;
|
|
189
|
+
for (let i = 0; i < tiers; i++) g.cylinder({ at: [0, 0, i * (th + gap)], r, h: th, chamfer: 0.8, material: o.material, bands: o.bands, rings: i === tiers - 1 ? [r * 0.55] : undefined });
|
|
190
|
+
}, { sort: 'none' });
|
|
191
|
+
|
|
192
|
+
/* Chip with pins and a die */
|
|
193
|
+
define('chip', (g, o) => {
|
|
194
|
+
const s = o.size ?? 30, n = o.pins ?? 6, t = o.h ?? 3;
|
|
195
|
+
const pitch = s / (n + 1);
|
|
196
|
+
for (let i = 1; i <= n; i++) {
|
|
197
|
+
const p = -s / 2 + i * pitch - 0.9;
|
|
198
|
+
g.box({ at: [p, -s / 2 - 2.6, 0.4], size: [1.8, 2.6, 0.8] });
|
|
199
|
+
g.box({ at: [-s / 2 - 2.6, p, 0.4], size: [2.6, 1.8, 0.8] });
|
|
200
|
+
g.box({ at: [p, s / 2, 0.4], size: [1.8, 2.6, 0.8] });
|
|
201
|
+
g.box({ at: [s / 2, p, 0.4], size: [2.6, 1.8, 0.8] });
|
|
202
|
+
}
|
|
203
|
+
g.box({ at: [-s / 2, -s / 2, 0], size: [s, s, t], r: 1.5, chamfer: 0.6, material: o.material });
|
|
204
|
+
const ds = s * 0.56;
|
|
205
|
+
g.box({ at: [-ds / 2, -ds / 2, t], size: [ds, ds, 1.4], r: 1, material: o.die || (o.lit ? 'lit' : 'dark'), glow: !!o.lit,
|
|
206
|
+
top: o.label ? (f) => f.text(o.label, ds / 2, ds / 2 + 1.6, { size: Math.min(4.5, ds / o.label.length * 1.4), anchor: 'middle', cls: o.lit ? 'tx-strong' : 'tx' }) : undefined });
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
/* Router / hub: box with antennas and LEDs */
|
|
210
|
+
define('router', (g, o) => {
|
|
211
|
+
const w = o.w ?? 34, d = o.d ?? 20, h = o.h ?? 6;
|
|
212
|
+
g.box({ at: [-w / 2, -d / 2, 0], size: [w, d, h], r: 2.5, chamfer: 0.8, material: o.material, top: (f) => f.hlines(w * 0.3, d * 0.3, w * 0.4, d * 0.4, 4, 'ln-faint') });
|
|
213
|
+
for (const x of [-w / 2 + 4, w / 2 - 4]) g.cylinder({ at: [x, -d / 2 + 3, h], r: 0.9, h: 16 });
|
|
214
|
+
for (let i = 0; i < 4; i++) g.pane({ class: 'led', plane: 'left', at: [-w / 2 + 6 + i * 4, d / 2 + 0.05, h * 0.62], w: 1.6, h: 1.2, r: 0.5, fill: 'f-lit', line: null });
|
|
215
|
+
}, { sort: 'none' });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/* ── keyboard (shared) ── */
|
|
219
|
+
function chars(s) { return s.split('').map((c) => [c, 1]); }
|
|
220
|
+
const ROWS = [
|
|
221
|
+
[['`', 1]].concat(chars('1234567890-='), [['backspace', 2]]),
|
|
222
|
+
[['tab', 1.5]].concat(chars('qwertyuiop[]'), [['\\', 1.5]]),
|
|
223
|
+
[['capslock', 1.75]].concat(chars("asdfghjkl;'"), [['enter', 2.25]]),
|
|
224
|
+
[['shift', 2.25]].concat(chars('zxcvbnm,./'), [['shift', 2.75]]),
|
|
225
|
+
[['control', 1.25], ['alt', 1.25], ['meta', 1.25], ['space', 7.5], ['meta', 1.25], ['alt', 1.25], ['control', 1.25]],
|
|
226
|
+
];
|
|
227
|
+
function buildKeyboard(g, K) {
|
|
228
|
+
if (K.w < 120) {
|
|
229
|
+
/* small keyboards: keys drawn on the top face */
|
|
230
|
+
g.box({ at: [K.x, K.y, K.z], size: [K.w, K.d, K.h], r: Math.min(3, K.d / 4), chamfer: 0.6,
|
|
231
|
+
top: (f) => { const p = Math.max(1.4, K.d * 0.12); f.grid(p, p, K.w - 2 * p, K.d - 2 * p, 14, 4, { gap: Math.max(0.4, K.d * 0.04), r: 0.4, fill: 'f-key', line: 'ln-faint' }); } });
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
g.box({ at: [K.x, K.y, K.z], size: [K.w, K.d, K.h], r: 6, chamfer: 1.5, top: (f) => f.rect(5, 5, K.w - 10, K.d - 10, { r: 3, line: 'ln-soft' }) });
|
|
235
|
+
const top = K.z + K.h, unit = (K.w - 12) / 15, pitch = (K.d - 12) / 5, k0x = K.x + 6, k0y = K.y + 6;
|
|
236
|
+
ROWS.forEach((row, ri) => {
|
|
237
|
+
let x = k0x;
|
|
238
|
+
row.forEach(([name, kw]) => {
|
|
239
|
+
const w = kw * unit - 1.8;
|
|
240
|
+
g.box({ id: (K.id ? K.id + '-' : '') + 'key-' + name + (g.find((n) => n.id === (K.id ? K.id + '-' : '') + 'key-' + name) ? '-2' : ''), class: 'key', key: name,
|
|
241
|
+
at: [x + 0.9, k0y + ri * pitch + 0.8, top], size: [w, pitch - 1.8, 3], r: 1.3, chamfer: 0.7, n: 2, tone: { top: 'f-key' } });
|
|
242
|
+
x += kw * unit;
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
function keyboardAPI(kb) {
|
|
247
|
+
return {
|
|
248
|
+
press(name, down = true) {
|
|
249
|
+
const s = kb.scene;
|
|
250
|
+
kb.findAll((n) => n.opts.key === name).forEach((k) => {
|
|
251
|
+
k.set('material', down ? 'paper' : undefined);
|
|
252
|
+
if (s) s.to(k, { z: down ? -1.4 : 0 }, { duration: 0.06, ease: 'outQuad' }); else k.set('z', down ? -1.4 : 0);
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/* ── CRT screen (power 0 → 1: line, open, glow) ── */
|
|
259
|
+
function drawCRT(f, n) {
|
|
260
|
+
const o = n.opts, w = f.w, h = f.h, p = o.power || 0, id = 'crt' + n.uid;
|
|
261
|
+
f.rect(0, 0, w, h, { r: 6, fill: 'f-screen', line: 'ln' });
|
|
262
|
+
if (p <= 0) return;
|
|
263
|
+
const lineP = Math.min(1, p / 0.16), openP = Math.max(0, Math.min(1, (p - 0.16) / 0.34)), easeO = 1 - Math.pow(1 - openP, 3);
|
|
264
|
+
const flash = openP < 1 ? 0.55 * (1 - openP) : Math.max(0, 0.25 - (p - 0.5) * 0.6);
|
|
265
|
+
const hh = Math.max(1.2, h * easeO), y0 = (h - hh) / 2;
|
|
266
|
+
let s = '<defs><clipPath id="' + id + 'c"><rect x="0" y="' + fmt(y0) + '" width="' + w + '" height="' + fmt(hh) + '" rx="6"/></clipPath>' +
|
|
267
|
+
'<radialGradient id="' + id + 'g" cx="50%" cy="42%" r="62%"><stop offset="0" style="stop-color:var(--iso-lit);stop-opacity:.16"/><stop offset="1" style="stop-color:var(--iso-lit);stop-opacity:0"/></radialGradient></defs>';
|
|
268
|
+
if (openP <= 0) {
|
|
269
|
+
const lw = w * (0.15 + 0.85 * lineP);
|
|
270
|
+
s += '<path class="ln-lit" d="M' + fmt((w - lw) / 2) + ' ' + fmt(h / 2) + 'H' + fmt((w + lw) / 2) + '" style="filter:drop-shadow(0 0 2px var(--iso-glow))"/>';
|
|
271
|
+
f.svg(s);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
s += '<g clip-path="url(#' + id + 'c)">';
|
|
275
|
+
s += '<rect class="f-screen-on" x="0" y="0" width="' + w + '" height="' + h + '"/>';
|
|
276
|
+
s += '<rect x="0" y="0" width="' + w + '" height="' + h + '" fill="url(#' + id + 'g)"/>';
|
|
277
|
+
if (openP > 0.4) {
|
|
278
|
+
const a = Math.min(1, (openP - 0.4) / 0.6);
|
|
279
|
+
const logo = (o.logo && pathData(o.logo)) || MARK; // path data in a 9 × 9 box
|
|
280
|
+
s += '<g style="opacity:' + fmt(a) + '">';
|
|
281
|
+
s += '<path class="f-lit" transform="translate(31.8 12) scale(3.6)" d="' + logo + '" style="filter:drop-shadow(0 0 2.5px var(--iso-glow))"/>';
|
|
282
|
+
const shown = (o.text || '').slice(-17);
|
|
283
|
+
s += '<text class="tx-lit" x="9" y="60" font-size="6" style="white-space:pre">> ' + esc(shown) + '</text>';
|
|
284
|
+
if (o.cursor && p >= 1) s += '<rect class="f-lit" x="' + fmt(9 + (2 + shown.length) * 3.62) + '" y="54.6" width="3.2" height="6.2"/>';
|
|
285
|
+
s += '</g>';
|
|
286
|
+
}
|
|
287
|
+
if (flash > 0) s += '<rect class="f-lit" x="0" y="0" width="' + w + '" height="' + h + '" style="opacity:' + fmt(flash) + '"/>';
|
|
288
|
+
s += '</g>';
|
|
289
|
+
if (openP < 1) {
|
|
290
|
+
s += '<path class="ln-lit" d="M0 ' + fmt(y0) + 'H' + w + 'M0 ' + fmt(y0 + hh) + 'H' + w + '" style="opacity:' + fmt(1 - openP) + '"/>';
|
|
291
|
+
}
|
|
292
|
+
f.svg(s);
|
|
293
|
+
}
|
|
294
|
+
function esc(s) { return String(s).replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c])); }
|
|
295
|
+
export { ICONS };
|