castle-web-cli 0.4.204 → 0.4.205
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/dist/agent-failures.d.ts +3 -1
- package/dist/agent-failures.js +19 -0
- package/dist/agent-prompts.js +2 -1
- package/dist/agent.d.ts +3 -2
- package/dist/agent.js +19 -10
- package/dist/castle-host/host.d.ts +17 -0
- package/dist/castle-host/host.js +551 -4
- package/dist/devSessionStorage.js +65 -4
- package/dist/effortSupport.d.ts +7 -0
- package/dist/effortSupport.js +31 -0
- package/dist/init.js +13 -3
- package/dist/platformDoc.js +6 -1
- package/dist/serve.js +35 -2
- package/dist/shell/assets/{index-DHzA0-Sj.js → index-hWQTC8q6.js} +5 -5
- package/dist/shell/index.html +1 -1
- package/dist/vitePlugins.js +19 -1
- package/kits/base/CLAUDE.md +2 -0
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +131 -0
- package/kits/base/sdk/agent.d.ts +6 -0
- package/kits/base/sdk/agent.js +70 -0
- package/kits/base/sdk/castle.d.ts +4 -0
- package/kits/base/sdk/castle.js +2 -0
- package/kits/base/sdk/commands.d.ts +106 -0
- package/kits/base/sdk/commands.js +21 -0
- package/kits/base/sdk/decks.d.ts +7 -0
- package/kits/base/sdk/decks.js +31 -0
- package/kits/base/sdk/runtime.d.ts +4 -2
- package/kits/base/sdk/runtime.js +51 -40
- package/kits/base/sdk/server/storage.d.ts +44 -0
- package/kits/base/sdk/server/storage.js +54 -0
- package/kits/base/sdk/transport.d.ts +5 -1
- package/kits/base/sdk/transport.js +106 -23
- package/kits/multiplayer-2d/castle.json +4 -4
- package/kits/multiplayer-2d/code/client/poses.js +7 -0
- package/kits/multiplayer-2d/code/server/limits.js +3 -0
- package/kits/multiplayer-2d/code/server/ownership.js +36 -8
- package/kits/multiplayer-2d/code/systems/multiplayer.js +23 -2
- package/kits/multiplayer-3d/CLAUDE.md +12 -1
- package/kits/multiplayer-3d/castle.json +4 -4
- package/kits/multiplayer-3d/code/client/avatars.js +7 -1
- package/kits/multiplayer-3d/code/client/poses.js +61 -1
- package/kits/multiplayer-3d/code/client/steer.js +383 -0
- package/kits/multiplayer-3d/code/server/limits.js +3 -0
- package/kits/multiplayer-3d/code/server/ownership.js +114 -18
- package/kits/multiplayer-3d/code/server/world.js +12 -4
- package/kits/multiplayer-3d/code/systems/multiplayer.js +315 -34
- package/kits/physics-3d/CLAUDE.md +65 -7
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/pxmodel-format.md +916 -15
- package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
- package/kits/physics-3d/engine3d/artobjects.js +218 -0
- package/kits/physics-3d/engine3d/blob.js +1058 -0
- package/kits/physics-3d/engine3d/branches.js +300 -0
- package/kits/physics-3d/engine3d/cage.js +496 -0
- package/kits/physics-3d/engine3d/cageops.js +516 -0
- package/kits/physics-3d/engine3d/frame.js +669 -0
- package/kits/physics-3d/engine3d/look.js +675 -0
- package/kits/physics-3d/engine3d/materialpresets.js +486 -0
- package/kits/physics-3d/engine3d/modelEditorWorld.js +3 -2
- package/kits/physics-3d/engine3d/modelrefs.js +120 -0
- package/kits/physics-3d/engine3d/motion.js +560 -0
- package/kits/physics-3d/engine3d/objtext.js +37 -0
- package/kits/physics-3d/engine3d/optimize.js +1083 -0
- package/kits/physics-3d/engine3d/paint.js +674 -0
- package/kits/physics-3d/engine3d/partgeometry.js +467 -0
- package/kits/physics-3d/engine3d/paths.js +324 -0
- package/kits/physics-3d/engine3d/pxmodel.js +429 -26
- package/kits/physics-3d/engine3d/scatter.js +102 -0
- package/kits/physics-3d/engine3d/sculpt.js +1148 -0
- package/kits/physics-3d/engine3d/surfacedetail.js +267 -0
- package/kits/physics-3d/engine3d/sweep.js +480 -0
- package/kits/physics-3d/engine3d/thumbnails.js +6 -4
- package/kits/physics-3d/engine3d/world3d.js +119 -17
- package/kits/physics-3d/eslint.config.js +13 -0
- package/kits/physics-3d/models/parts/axe.pxmodel +40 -0
- package/kits/physics-3d/models/parts/backpack.pxmodel +47 -0
- package/kits/physics-3d/models/parts/barrel.pxmodel +22 -0
- package/kits/physics-3d/models/parts/bedroll.pxmodel +28 -0
- package/kits/physics-3d/models/parts/belt-pouch.pxmodel +22 -0
- package/kits/physics-3d/models/parts/bolt.pxmodel +10 -0
- package/kits/physics-3d/models/parts/bracer.pxmodel +26 -0
- package/kits/physics-3d/models/parts/chain-span.pxmodel +12 -0
- package/kits/physics-3d/models/parts/chimney.pxmodel +11 -0
- package/kits/physics-3d/models/parts/crate.pxmodel +44 -0
- package/kits/physics-3d/models/parts/crystal.pxmodel +22 -0
- package/kits/physics-3d/models/parts/door-arched.pxmodel +12 -0
- package/kits/physics-3d/models/parts/engine-block.pxmodel +39 -0
- package/kits/physics-3d/models/parts/exhaust-pipe.pxmodel +30 -0
- package/kits/physics-3d/models/parts/fence-span.pxmodel +10 -0
- package/kits/physics-3d/models/parts/fender.pxmodel +19 -0
- package/kits/physics-3d/models/parts/headlight.pxmodel +24 -0
- package/kits/physics-3d/models/parts/hood.pxmodel +17 -0
- package/kits/physics-3d/models/parts/horned-helmet.pxmodel +37 -0
- package/kits/physics-3d/models/parts/jerry-can.pxmodel +26 -0
- package/kits/physics-3d/models/parts/ladder.pxmodel +10 -0
- package/kits/physics-3d/models/parts/lamp.pxmodel +20 -0
- package/kits/physics-3d/models/parts/lantern.pxmodel +30 -0
- package/kits/physics-3d/models/parts/leaf-cluster.pxmodel +21 -0
- package/kits/physics-3d/models/parts/offroad-tire.pxmodel +48 -0
- package/kits/physics-3d/models/parts/pauldron.pxmodel +26 -0
- package/kits/physics-3d/models/parts/rivet.pxmodel +8 -0
- package/kits/physics-3d/models/parts/rock.pxmodel +21 -0
- package/kits/physics-3d/models/parts/roll-bar-light.pxmodel +26 -0
- package/kits/physics-3d/models/parts/round-shield.pxmodel +31 -0
- package/kits/physics-3d/models/parts/saddlebag.pxmodel +30 -0
- package/kits/physics-3d/models/parts/spoked-wheel.pxmodel +39 -0
- package/kits/physics-3d/models/parts/staff.pxmodel +33 -0
- package/kits/physics-3d/models/parts/stepping-stones.pxmodel +8 -0
- package/kits/physics-3d/models/parts/stone-base.pxmodel +10 -0
- package/kits/physics-3d/models/parts/sword.pxmodel +33 -0
- package/kits/physics-3d/models/parts/window-lit.pxmodel +13 -0
- package/kits/physics-3d/models/parts/wizard-hat.pxmodel +28 -0
- package/kits/physics-3d/models/templates/biped-stout.pxmodel +59 -0
- package/kits/physics-3d/models/templates/biped-tall.pxmodel +63 -0
- package/kits/physics-3d/models/templates/quadruped.pxmodel +52 -0
- package/kits/physics-3d/scripts/render/filmstrip.js +217 -0
- package/kits/physics-3d/scripts/render/index.html +20 -0
- package/kits/physics-3d/scripts/render/sheet.js +411 -0
- package/kits/physics-3d/scripts/render.mjs +646 -0
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/client/ownership.js +14 -4
- package/kits/real-time/code/client/smooth.js +2 -2
- package/kits/real-time/code/server/session.js +20 -4
- package/package.json +2 -2
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
// The `frame` part kind: round tubes between named points, welded into one
|
|
2
|
+
// piece, and `net`, a grid of thin wires across faces of those points. Roll
|
|
3
|
+
// cages, bike frames, ladders, towers, fences. See docs/pxmodel-format.md for
|
|
4
|
+
// the fields.
|
|
5
|
+
//
|
|
6
|
+
// Edges that pass through a point are swept as one tube with a rounded bend
|
|
7
|
+
// there, so bent tubing has no seams. A tube that ends on the side of a
|
|
8
|
+
// through tube stops at its middle line, inside it, the way a welded tube
|
|
9
|
+
// meets another. Where edges meet with no pair to pass through, a ball a
|
|
10
|
+
// little wider than the tubes covers the joint.
|
|
11
|
+
|
|
12
|
+
import * as THREE from 'three';
|
|
13
|
+
import { circleSides, lengthSteps } from './optimize';
|
|
14
|
+
|
|
15
|
+
const DEG = Math.PI / 180;
|
|
16
|
+
const UP = new THREE.Vector3(0, 1, 0);
|
|
17
|
+
const MAX_EDGES = 400;
|
|
18
|
+
const MAX_WIRES = 800;
|
|
19
|
+
|
|
20
|
+
// A ball joint is this much wider than the widest tube it covers, so the
|
|
21
|
+
// surfaces cross instead of touching.
|
|
22
|
+
const BALL_SCALE = 1.08;
|
|
23
|
+
|
|
24
|
+
// At a point where three or more edges meet, the two closest to a straight
|
|
25
|
+
// line pass through as one tube when their bend is under this.
|
|
26
|
+
const THROUGH_BEND = 35 * DEG;
|
|
27
|
+
|
|
28
|
+
// One arc segment per this much turn at a bend.
|
|
29
|
+
const ARC_STEP = 12 * DEG;
|
|
30
|
+
|
|
31
|
+
// A frame is built into `mesh`, { positions, normals, indices }, flat arrays.
|
|
32
|
+
// An edge is { a, b, radius, line, length }: its two node names, its radius,
|
|
33
|
+
// and `line`, the points of its middle line from a to b.
|
|
34
|
+
|
|
35
|
+
//
|
|
36
|
+
// Reading
|
|
37
|
+
//
|
|
38
|
+
|
|
39
|
+
function number(value, fallback, min, max) {
|
|
40
|
+
if (!Number.isFinite(value)) {
|
|
41
|
+
return fallback;
|
|
42
|
+
}
|
|
43
|
+
return Math.max(min, Math.min(max, value));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function vector(value) {
|
|
47
|
+
if (!Array.isArray(value) || !value.slice(0, 3).every(Number.isFinite)) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return new THREE.Vector3(value[0], value[1], value[2]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function readNodes(part) {
|
|
54
|
+
const nodes = new Map();
|
|
55
|
+
if (!part.nodes || typeof part.nodes !== 'object' || Array.isArray(part.nodes)) {
|
|
56
|
+
return nodes;
|
|
57
|
+
}
|
|
58
|
+
for (const [name, value] of Object.entries(part.nodes)) {
|
|
59
|
+
const point = vector(value);
|
|
60
|
+
if (point) {
|
|
61
|
+
nodes.set(name, point);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return nodes;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// An edge is [a, b], with an optional radius and an optional object of
|
|
68
|
+
// options, { curve } or { via }, in either order after the names.
|
|
69
|
+
function readEdges(part, nodes, radius) {
|
|
70
|
+
const middle = new THREE.Vector3();
|
|
71
|
+
nodes.forEach((point) => middle.add(point));
|
|
72
|
+
middle.divideScalar(Math.max(1, nodes.size));
|
|
73
|
+
const edges = [];
|
|
74
|
+
const list = Array.isArray(part.edges) ? part.edges.slice(0, MAX_EDGES) : [];
|
|
75
|
+
for (const raw of list) {
|
|
76
|
+
if (!Array.isArray(raw) || !nodes.has(raw[0]) || !nodes.has(raw[1]) || raw[0] === raw[1]) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const extra = raw.slice(2);
|
|
80
|
+
const options = extra.find((item) => item && typeof item === 'object') ?? {};
|
|
81
|
+
const own = extra.find(Number.isFinite);
|
|
82
|
+
const line = edgeLine(nodes.get(raw[0]), nodes.get(raw[1]), options, middle);
|
|
83
|
+
edges.push({
|
|
84
|
+
a: raw[0],
|
|
85
|
+
b: raw[1],
|
|
86
|
+
radius: own > 0 ? own : radius,
|
|
87
|
+
line,
|
|
88
|
+
length: lengthOf(line),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return edges;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// A straight edge is its two end points. `via` points make a smooth curve
|
|
95
|
+
// through them. `curve` bows a straight edge by that fraction of its length,
|
|
96
|
+
// away from the middle of the frame, or toward it when negative.
|
|
97
|
+
function edgeLine(start, end, options, middle) {
|
|
98
|
+
const via = Array.isArray(options.via) ? options.via.map(vector).filter(Boolean) : [];
|
|
99
|
+
let curve = null;
|
|
100
|
+
if (via.length > 0) {
|
|
101
|
+
curve = new THREE.CatmullRomCurve3([start, ...via, end], false, 'centripetal');
|
|
102
|
+
} else if (Number.isFinite(options.curve) && Math.abs(options.curve) > 1e-4) {
|
|
103
|
+
const along = end.clone().sub(start);
|
|
104
|
+
const length = along.length();
|
|
105
|
+
along.normalize();
|
|
106
|
+
const center = start.clone().lerp(end, 0.5);
|
|
107
|
+
const out =
|
|
108
|
+
perpendicular(center.clone().sub(middle), along) ??
|
|
109
|
+
perpendicular(UP.clone(), along) ??
|
|
110
|
+
perpendicular(new THREE.Vector3(1, 0, 0), along);
|
|
111
|
+
|
|
112
|
+
// A quadratic curve's middle is halfway to its control point.
|
|
113
|
+
const control = center.addScaledVector(out, 2 * options.curve * length);
|
|
114
|
+
curve = new THREE.QuadraticBezierCurve3(start, control, end);
|
|
115
|
+
}
|
|
116
|
+
if (!curve) {
|
|
117
|
+
return [start.clone(), end.clone()];
|
|
118
|
+
}
|
|
119
|
+
const most = Math.min(24, 10 * Math.max(1, via.length));
|
|
120
|
+
const line = curve.getSpacedPoints(lengthSteps(null, curve.getLength(), most, 4));
|
|
121
|
+
line[0] = start.clone();
|
|
122
|
+
line[line.length - 1] = end.clone();
|
|
123
|
+
return line;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// The part of `value` square to the unit vector `along`, made unit length,
|
|
127
|
+
// or null when there is none.
|
|
128
|
+
function perpendicular(value, along) {
|
|
129
|
+
value.addScaledVector(along, -value.dot(along));
|
|
130
|
+
return value.lengthSq() > 1e-10 ? value.normalize() : null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function lengthOf(line) {
|
|
134
|
+
let total = 0;
|
|
135
|
+
for (let i = 1; i < line.length; i++) {
|
|
136
|
+
total += line[i].distanceTo(line[i - 1]);
|
|
137
|
+
}
|
|
138
|
+
return total;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
//
|
|
142
|
+
// Joints
|
|
143
|
+
//
|
|
144
|
+
|
|
145
|
+
// For each node, the edges that touch it, as { edge, away }: the edge's index
|
|
146
|
+
// and the direction the edge leaves the node.
|
|
147
|
+
function linkNodes(edges) {
|
|
148
|
+
const links = new Map();
|
|
149
|
+
edges.forEach((edge, index) => {
|
|
150
|
+
const line = edge.line;
|
|
151
|
+
const last = line.length - 1;
|
|
152
|
+
for (const [name, from, to] of [
|
|
153
|
+
[edge.a, line[0], line[1]],
|
|
154
|
+
[edge.b, line[last], line[last - 1]],
|
|
155
|
+
]) {
|
|
156
|
+
if (!links.has(name)) {
|
|
157
|
+
links.set(name, []);
|
|
158
|
+
}
|
|
159
|
+
links.get(name).push({ edge: index, away: to.clone().sub(from).normalize() });
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
return links;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// The pair of edges that pass through each node as one tube, as { edges,
|
|
166
|
+
// away, trim }: their indices, their directions away from the node, and how
|
|
167
|
+
// far back from the node each is cut for the bend. A bend is an arc of radius
|
|
168
|
+
// `round` touching both edges; on short edges it is made smaller, and when it
|
|
169
|
+
// would be tighter than the tube itself the corner gets a ball instead.
|
|
170
|
+
function findPairs(edges, links, round) {
|
|
171
|
+
const pairs = new Map();
|
|
172
|
+
for (const [name, list] of links) {
|
|
173
|
+
if (list.length < 2) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
let best = null;
|
|
177
|
+
for (let i = 0; i < list.length; i++) {
|
|
178
|
+
for (let j = i + 1; j < list.length; j++) {
|
|
179
|
+
const angle = list[i].away.angleTo(list[j].away);
|
|
180
|
+
if (!best || angle > best.angle) {
|
|
181
|
+
best = { first: list[i], second: list[j], angle };
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const bend = Math.PI - best.angle;
|
|
186
|
+
if (list.length > 2 && bend > THROUGH_BEND) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const pair = {
|
|
190
|
+
edges: [best.first.edge, best.second.edge],
|
|
191
|
+
away: [best.first.away, best.second.away],
|
|
192
|
+
trim: 0,
|
|
193
|
+
};
|
|
194
|
+
if (bend > 0.5 * DEG) {
|
|
195
|
+
const first = edges[best.first.edge];
|
|
196
|
+
const second = edges[best.second.edge];
|
|
197
|
+
const slope = Math.tan(best.angle / 2);
|
|
198
|
+
pair.trim = Math.min(round / slope, 0.45 * Math.min(first.length, second.length));
|
|
199
|
+
if (pair.trim * slope < 0.9 * Math.max(first.radius, second.radius)) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
pairs.set(name, pair);
|
|
204
|
+
}
|
|
205
|
+
return pairs;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A ball goes where edges meet without passing through, and where a branch
|
|
209
|
+
// is wider than the tube it ends on.
|
|
210
|
+
function addBalls(mesh, nodes, edges, links, pairs, sides) {
|
|
211
|
+
for (const [name, list] of links) {
|
|
212
|
+
if (list.length < 2) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const pair = pairs.get(name);
|
|
216
|
+
const widest = Math.max(...list.map((link) => edges[link.edge].radius));
|
|
217
|
+
const through = pair ? Math.max(...pair.edges.map((index) => edges[index].radius)) : 0;
|
|
218
|
+
if (widest > through + 1e-9) {
|
|
219
|
+
addBall(mesh, nodes.get(name), widest * BALL_SCALE, sides);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function addBall(mesh, center, radius, sides) {
|
|
225
|
+
const ball = new THREE.SphereGeometry(radius, sides, Math.max(4, Math.round(sides / 2)));
|
|
226
|
+
const first = mesh.positions.length / 3;
|
|
227
|
+
const position = ball.attributes.position.array;
|
|
228
|
+
for (let i = 0; i < position.length; i += 3) {
|
|
229
|
+
mesh.positions.push(
|
|
230
|
+
position[i] + center.x,
|
|
231
|
+
position[i + 1] + center.y,
|
|
232
|
+
position[i + 2] + center.z
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
mesh.normals.push(...ball.attributes.normal.array);
|
|
236
|
+
ball.index.array.forEach((index) => mesh.indices.push(first + index));
|
|
237
|
+
ball.dispose();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
//
|
|
241
|
+
// Chains
|
|
242
|
+
//
|
|
243
|
+
|
|
244
|
+
// Edges joined end to end through their pairs, as { steps, nodes, closed }:
|
|
245
|
+
// each step is { edge, forward }, `nodes` the names along the way. A chain
|
|
246
|
+
// starts at an edge end that does not pass through its node; the edges left
|
|
247
|
+
// over after those are closed loops.
|
|
248
|
+
function findChains(edges, links, pairs) {
|
|
249
|
+
const used = new Array(edges.length).fill(false);
|
|
250
|
+
const chains = [];
|
|
251
|
+
for (const [name, list] of links) {
|
|
252
|
+
for (const link of list) {
|
|
253
|
+
if (!used[link.edge] && !pairs.get(name)?.edges.includes(link.edge)) {
|
|
254
|
+
chains.push(followChain(link.edge, name, edges, pairs, used));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
for (let index = 0; index < edges.length; index++) {
|
|
259
|
+
if (!used[index]) {
|
|
260
|
+
chains.push(followChain(index, edges[index].a, edges, pairs, used));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return chains;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function followChain(start, from, edges, pairs, used) {
|
|
267
|
+
const chain = { steps: [], nodes: [from], closed: false };
|
|
268
|
+
let index = start;
|
|
269
|
+
let node = from;
|
|
270
|
+
for (;;) {
|
|
271
|
+
used[index] = true;
|
|
272
|
+
const edge = edges[index];
|
|
273
|
+
const forward = edge.a === node;
|
|
274
|
+
chain.steps.push({ edge: index, forward });
|
|
275
|
+
node = forward ? edge.b : edge.a;
|
|
276
|
+
chain.nodes.push(node);
|
|
277
|
+
const pair = pairs.get(node);
|
|
278
|
+
if (!pair || !pair.edges.includes(index)) {
|
|
279
|
+
return chain;
|
|
280
|
+
}
|
|
281
|
+
const next = pair.edges[0] === index ? pair.edges[1] : pair.edges[0];
|
|
282
|
+
if (next === start && node === from) {
|
|
283
|
+
chain.closed = true;
|
|
284
|
+
return chain;
|
|
285
|
+
}
|
|
286
|
+
if (used[next]) {
|
|
287
|
+
return chain;
|
|
288
|
+
}
|
|
289
|
+
index = next;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// The middle line of a chain as points with a radius at each: every edge cut
|
|
294
|
+
// back where it bends, with an arc between.
|
|
295
|
+
function chainLine(chain, edges, pairs, nodes) {
|
|
296
|
+
const points = [];
|
|
297
|
+
const radii = [];
|
|
298
|
+
const count = chain.steps.length;
|
|
299
|
+
for (let k = 0; k < count; k++) {
|
|
300
|
+
const { edge: index } = chain.steps[k];
|
|
301
|
+
const edge = edges[index];
|
|
302
|
+
const endPair = k < count - 1 || chain.closed ? pairs.get(chain.nodes[k + 1]) : null;
|
|
303
|
+
stepLine(chain, k, edge, pairs).forEach((point) => addPoint(points, radii, point, edge.radius));
|
|
304
|
+
if (endPair && endPair.trim > 0) {
|
|
305
|
+
const next = edges[chain.steps[(k + 1) % count].edge];
|
|
306
|
+
addArc(
|
|
307
|
+
points,
|
|
308
|
+
radii,
|
|
309
|
+
nodes.get(chain.nodes[k + 1]),
|
|
310
|
+
endPair,
|
|
311
|
+
index,
|
|
312
|
+
edge.radius,
|
|
313
|
+
next.radius
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (chain.closed && points.length > 2 && points[0].distanceTo(points[points.length - 1]) < 1e-6) {
|
|
318
|
+
points.pop();
|
|
319
|
+
radii.pop();
|
|
320
|
+
}
|
|
321
|
+
return { points, radii };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// The line of the k-th edge of a chain, in the chain's direction, cut back
|
|
325
|
+
// at each end that bends. The ends of an open chain do not bend.
|
|
326
|
+
function stepLine(chain, k, edge, pairs) {
|
|
327
|
+
const last = chain.steps.length - 1;
|
|
328
|
+
const startPair = k > 0 || chain.closed ? pairs.get(chain.nodes[k]) : null;
|
|
329
|
+
const endPair = k < last || chain.closed ? pairs.get(chain.nodes[k + 1]) : null;
|
|
330
|
+
const line = chain.steps[k].forward ? [...edge.line] : [...edge.line].reverse();
|
|
331
|
+
const cut = cutStart(line, startPair?.trim ?? 0);
|
|
332
|
+
return cutStart(cut.reverse(), endPair?.trim ?? 0).reverse();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function addPoint(points, radii, point, radius) {
|
|
336
|
+
if (points.length > 0 && points[points.length - 1].distanceTo(point) < 1e-6) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
points.push(point);
|
|
340
|
+
radii.push(radius);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// The line with `distance` of its length cut off the start.
|
|
344
|
+
function cutStart(line, distance) {
|
|
345
|
+
let left = distance;
|
|
346
|
+
for (let i = 1; i < line.length && left > 0; i++) {
|
|
347
|
+
const span = line[i].distanceTo(line[i - 1]);
|
|
348
|
+
if (span >= left) {
|
|
349
|
+
return [line[i - 1].clone().lerp(line[i], left / span), ...line.slice(i)];
|
|
350
|
+
}
|
|
351
|
+
left -= span;
|
|
352
|
+
}
|
|
353
|
+
return left > 0 ? [line[line.length - 1]] : line;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// The arc of a bend, from the cut end of the edge coming in to the cut start
|
|
357
|
+
// of the edge going out. Its center is on the line halfway between the two
|
|
358
|
+
// edges, far enough out that the arc touches both.
|
|
359
|
+
function addArc(points, radii, node, pair, incoming, radiusIn, radiusOut) {
|
|
360
|
+
const side = pair.edges[0] === incoming ? 0 : 1;
|
|
361
|
+
const from = pair.away[side];
|
|
362
|
+
const to = pair.away[1 - side];
|
|
363
|
+
const half = from.angleTo(to) / 2;
|
|
364
|
+
const radius = pair.trim * Math.tan(half);
|
|
365
|
+
const center = node
|
|
366
|
+
.clone()
|
|
367
|
+
.addScaledVector(from.clone().add(to).normalize(), radius / Math.sin(half));
|
|
368
|
+
const start = node.clone().addScaledVector(from, pair.trim).sub(center);
|
|
369
|
+
const end = node.clone().addScaledVector(to, pair.trim).sub(center);
|
|
370
|
+
const turn = start.angleTo(end);
|
|
371
|
+
const steps = lengthSteps(null, radius * turn, Math.max(2, Math.ceil(turn / ARC_STEP)));
|
|
372
|
+
for (let s = 1; s < steps; s++) {
|
|
373
|
+
const t = s / steps;
|
|
374
|
+
const point = start
|
|
375
|
+
.clone()
|
|
376
|
+
.multiplyScalar(Math.sin((1 - t) * turn))
|
|
377
|
+
.addScaledVector(end, Math.sin(t * turn))
|
|
378
|
+
.divideScalar(Math.sin(turn))
|
|
379
|
+
.add(center);
|
|
380
|
+
addPoint(points, radii, point, radiusIn + (radiusOut - radiusIn) * t);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
//
|
|
385
|
+
// Sweeping
|
|
386
|
+
//
|
|
387
|
+
|
|
388
|
+
// A tube along `points`, one ring of `sides` vertices at each. Each ring's
|
|
389
|
+
// frame is carried over from the one before, so the tube does not twist; on a
|
|
390
|
+
// closed loop the twist left over at the end is spread along it. `hint` is
|
|
391
|
+
// the direction the first ring's first vertex points, when it matters.
|
|
392
|
+
function sweep(mesh, points, radii, closed, sides, hint) {
|
|
393
|
+
const count = points.length;
|
|
394
|
+
const tangents = points.map((point, i) => {
|
|
395
|
+
const before = closed ? points[(i - 1 + count) % count] : points[Math.max(0, i - 1)];
|
|
396
|
+
const after = closed ? points[(i + 1) % count] : points[Math.min(count - 1, i + 1)];
|
|
397
|
+
return after.clone().sub(before).normalize();
|
|
398
|
+
});
|
|
399
|
+
const normals = [];
|
|
400
|
+
let normal = perpendicular((hint ?? UP).clone(), tangents[0]) ?? anySquare(tangents[0]);
|
|
401
|
+
for (let i = 0; i < count; i++) {
|
|
402
|
+
normal = perpendicular(normal.clone(), tangents[i]) ?? anySquare(tangents[i]);
|
|
403
|
+
normals.push(normal);
|
|
404
|
+
}
|
|
405
|
+
if (closed) {
|
|
406
|
+
const back = perpendicular(normal.clone(), tangents[0]) ?? normals[0];
|
|
407
|
+
const cross = new THREE.Vector3().crossVectors(back, normals[0]).dot(tangents[0]);
|
|
408
|
+
const twist = Math.atan2(cross, back.dot(normals[0]));
|
|
409
|
+
normals.forEach((n, i) => n.applyAxisAngle(tangents[i], (twist * i) / count));
|
|
410
|
+
}
|
|
411
|
+
const first = mesh.positions.length / 3;
|
|
412
|
+
const binormal = new THREE.Vector3();
|
|
413
|
+
const out = new THREE.Vector3();
|
|
414
|
+
for (let i = 0; i < count; i++) {
|
|
415
|
+
binormal.crossVectors(tangents[i], normals[i]);
|
|
416
|
+
for (let side = 0; side < sides; side++) {
|
|
417
|
+
const angle = (side / sides) * Math.PI * 2;
|
|
418
|
+
out
|
|
419
|
+
.copy(normals[i])
|
|
420
|
+
.multiplyScalar(Math.cos(angle))
|
|
421
|
+
.addScaledVector(binormal, Math.sin(angle));
|
|
422
|
+
const p = points[i];
|
|
423
|
+
mesh.positions.push(p.x + out.x * radii[i], p.y + out.y * radii[i], p.z + out.z * radii[i]);
|
|
424
|
+
mesh.normals.push(out.x, out.y, out.z);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
const rings = closed ? count : count - 1;
|
|
428
|
+
for (let i = 0; i < rings; i++) {
|
|
429
|
+
const here = first + i * sides;
|
|
430
|
+
const next = first + ((i + 1) % count) * sides;
|
|
431
|
+
for (let side = 0; side < sides; side++) {
|
|
432
|
+
const turn = (side + 1) % sides;
|
|
433
|
+
mesh.indices.push(
|
|
434
|
+
here + side,
|
|
435
|
+
here + turn,
|
|
436
|
+
next + side,
|
|
437
|
+
here + turn,
|
|
438
|
+
next + turn,
|
|
439
|
+
next + side
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return { tangents, first };
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function anySquare(direction) {
|
|
447
|
+
return perpendicular(new THREE.Vector3(1, 0, 0), direction) ?? new THREE.Vector3(0, 0, 1);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// A flat cap on the ring that starts at vertex `ring`, facing `facing`. It has
|
|
451
|
+
// its own copy of the ring so its edge stays sharp.
|
|
452
|
+
function addCap(mesh, center, ring, sides, facing, flip) {
|
|
453
|
+
const first = mesh.positions.length / 3;
|
|
454
|
+
for (let side = 0; side < sides; side++) {
|
|
455
|
+
const at = (ring + side) * 3;
|
|
456
|
+
mesh.positions.push(mesh.positions[at], mesh.positions[at + 1], mesh.positions[at + 2]);
|
|
457
|
+
mesh.normals.push(facing.x, facing.y, facing.z);
|
|
458
|
+
}
|
|
459
|
+
mesh.positions.push(center.x, center.y, center.z);
|
|
460
|
+
mesh.normals.push(facing.x, facing.y, facing.z);
|
|
461
|
+
const middle = first + sides;
|
|
462
|
+
for (let side = 0; side < sides; side++) {
|
|
463
|
+
const a = first + side;
|
|
464
|
+
const b = first + ((side + 1) % sides);
|
|
465
|
+
mesh.indices.push(...(flip ? [middle, b, a] : [middle, a, b]));
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// Each chain as one tube. An open end at a node with no other edge is capped;
|
|
470
|
+
// every other open end is inside a ball or a through tube.
|
|
471
|
+
function addChains(mesh, chains, edges, pairs, links, nodes, sides) {
|
|
472
|
+
for (const chain of chains) {
|
|
473
|
+
const { points, radii } = chainLine(chain, edges, pairs, nodes);
|
|
474
|
+
if (points.length < 2) {
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
const { tangents, first } = sweep(mesh, points, radii, chain.closed, sides, null);
|
|
478
|
+
if (chain.closed) {
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
const last = points.length - 1;
|
|
482
|
+
if (links.get(chain.nodes[0]).length === 1) {
|
|
483
|
+
addCap(mesh, points[0], first, sides, tangents[0].clone().negate(), true);
|
|
484
|
+
}
|
|
485
|
+
if (links.get(chain.nodes[chain.nodes.length - 1]).length === 1) {
|
|
486
|
+
addCap(mesh, points[last], first + last * sides, sides, tangents[last], false);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
//
|
|
492
|
+
// Nets
|
|
493
|
+
//
|
|
494
|
+
|
|
495
|
+
// Wires across each face of `net.faces`, three or four node names in order
|
|
496
|
+
// around the face. Each wire is a thin tube of four sides; a face side that is
|
|
497
|
+
// not already an edge gets a wire of its own.
|
|
498
|
+
function addNet(mesh, net, nodes, edges) {
|
|
499
|
+
const spacing = number(net.spacing, 0.06, 0.005, 10);
|
|
500
|
+
const wire = number(net.wire, 0.003, 0.0005, 0.2);
|
|
501
|
+
const edgeNames = new Set(
|
|
502
|
+
edges.flatMap((edge) => [`${edge.a}|${edge.b}`, `${edge.b}|${edge.a}`])
|
|
503
|
+
);
|
|
504
|
+
const lines = [];
|
|
505
|
+
for (const face of Array.isArray(net.faces) ? net.faces : []) {
|
|
506
|
+
const corners = Array.isArray(face) ? face.map((name) => nodes.get(name)) : [];
|
|
507
|
+
if ((corners.length !== 3 && corners.length !== 4) || corners.some((corner) => !corner)) {
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const facing = faceNormal(corners);
|
|
511
|
+
const faceLines =
|
|
512
|
+
corners.length === 4
|
|
513
|
+
? quadWires(corners, spacing, Boolean(net.diagonal))
|
|
514
|
+
: triangleWires(corners, spacing);
|
|
515
|
+
face.forEach((name, i) => {
|
|
516
|
+
const next = face[(i + 1) % face.length];
|
|
517
|
+
if (!edgeNames.has(`${name}|${next}`)) {
|
|
518
|
+
faceLines.push([corners[i], corners[(i + 1) % face.length]]);
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
faceLines.forEach((line) => lines.push({ line, facing }));
|
|
522
|
+
}
|
|
523
|
+
for (const { line, facing } of lines.slice(0, MAX_WIRES)) {
|
|
524
|
+
sweep(
|
|
525
|
+
mesh,
|
|
526
|
+
line,
|
|
527
|
+
line.map(() => wire),
|
|
528
|
+
false,
|
|
529
|
+
4,
|
|
530
|
+
facing
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function faceNormal(corners) {
|
|
536
|
+
const [a, b, c] = corners;
|
|
537
|
+
const d = corners[3] ?? a;
|
|
538
|
+
const normal = c.clone().sub(a).cross(d.clone().sub(b));
|
|
539
|
+
return normal.lengthSq() > 1e-12 ? normal.normalize() : UP.clone();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// A quad a, b, c, d is filled by blending its sides: the point at (u, v) is
|
|
543
|
+
// u of the way from a to b (and from d to c), then v of the way between those.
|
|
544
|
+
// Wires run along u and along v, or with `diagonal` at 45 degrees to both, as
|
|
545
|
+
// chain-link does.
|
|
546
|
+
function quadWires(corners, spacing, diagonal) {
|
|
547
|
+
const [a, b, c, d] = corners;
|
|
548
|
+
const width = (a.distanceTo(b) + d.distanceTo(c)) / 2;
|
|
549
|
+
const height = (a.distanceTo(d) + b.distanceTo(c)) / 2;
|
|
550
|
+
const at = (u, v) => a.clone().lerp(b, u).lerp(d.clone().lerp(c, u), v);
|
|
551
|
+
const lines = [];
|
|
552
|
+
if (!diagonal) {
|
|
553
|
+
const across = Math.max(1, Math.round(width / spacing));
|
|
554
|
+
const down = Math.max(1, Math.round(height / spacing));
|
|
555
|
+
for (let i = 1; i < across; i++) {
|
|
556
|
+
lines.push([at(i / across, 0), at(i / across, 1)]);
|
|
557
|
+
}
|
|
558
|
+
for (let j = 1; j < down; j++) {
|
|
559
|
+
lines.push([at(0, j / down), at(1, j / down)]);
|
|
560
|
+
}
|
|
561
|
+
return lines;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Diagonal wires are the lines x + y = k and x - y = k, measured in meters
|
|
565
|
+
// across the face. On a face that is not a flat parallelogram they curve, so
|
|
566
|
+
// they are drawn in a few pieces.
|
|
567
|
+
const flat = a.clone().add(c).distanceTo(b.clone().add(d)) < 1e-4;
|
|
568
|
+
const pieces = flat ? 1 : 6;
|
|
569
|
+
const step = spacing * Math.SQRT2;
|
|
570
|
+
const addLine = (x0, y0, x1, y1) => {
|
|
571
|
+
if (x1 - x0 < 1e-6) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const line = [];
|
|
575
|
+
for (let s = 0; s <= pieces; s++) {
|
|
576
|
+
const t = s / pieces;
|
|
577
|
+
line.push(at((x0 + (x1 - x0) * t) / width, (y0 + (y1 - y0) * t) / height));
|
|
578
|
+
}
|
|
579
|
+
lines.push(line);
|
|
580
|
+
};
|
|
581
|
+
for (let k = step / 2; k < width + height; k += step) {
|
|
582
|
+
addLine(
|
|
583
|
+
Math.max(0, k - height),
|
|
584
|
+
k - Math.max(0, k - height),
|
|
585
|
+
Math.min(width, k),
|
|
586
|
+
k - Math.min(width, k)
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
for (let k = -height + step / 2; k < width; k += step) {
|
|
590
|
+
addLine(
|
|
591
|
+
Math.max(0, k),
|
|
592
|
+
Math.max(0, k) - k,
|
|
593
|
+
Math.min(width, k + height),
|
|
594
|
+
Math.min(width, k + height) - k
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
return lines;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// A triangle a, b, c has wires square to a along two sides: lines parallel to
|
|
601
|
+
// b-c spaced from a, and lines parallel to a-c spaced along a-b.
|
|
602
|
+
function triangleWires(corners, spacing) {
|
|
603
|
+
const [a, b, c] = corners;
|
|
604
|
+
const lines = [];
|
|
605
|
+
const first = Math.max(1, Math.round(Math.max(a.distanceTo(b), a.distanceTo(c)) / spacing));
|
|
606
|
+
for (let i = 1; i < first; i++) {
|
|
607
|
+
lines.push([a.clone().lerp(b, i / first), a.clone().lerp(c, i / first)]);
|
|
608
|
+
}
|
|
609
|
+
const second = Math.max(1, Math.round(Math.max(a.distanceTo(b), c.distanceTo(b)) / spacing));
|
|
610
|
+
for (let i = 1; i < second; i++) {
|
|
611
|
+
lines.push([a.clone().lerp(b, i / second), c.clone().lerp(b, i / second)]);
|
|
612
|
+
}
|
|
613
|
+
return lines;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
//
|
|
617
|
+
// The part
|
|
618
|
+
//
|
|
619
|
+
|
|
620
|
+
// The geometry of a `frame` part, or null when it has no edges and no net.
|
|
621
|
+
// With `net.color` the net carries its own color per vertex.
|
|
622
|
+
export function frameGeometry(part) {
|
|
623
|
+
const nodes = readNodes(part);
|
|
624
|
+
const radius = number(part.radius, 0.03, 0.0005, 5);
|
|
625
|
+
const edges = readEdges(part, nodes, radius);
|
|
626
|
+
const net = part.net && typeof part.net === 'object' ? part.net : null;
|
|
627
|
+
if (edges.length === 0 && !net) {
|
|
628
|
+
return null;
|
|
629
|
+
}
|
|
630
|
+
const round = number(part.round, radius * 3, 0, 100);
|
|
631
|
+
const sides = circleSides(part, radius, Math.round(number(part.segments, 10, 3, 32)), 4);
|
|
632
|
+
const mesh = { positions: [], normals: [], indices: [] };
|
|
633
|
+
const links = linkNodes(edges);
|
|
634
|
+
const pairs = findPairs(edges, links, round);
|
|
635
|
+
addChains(mesh, findChains(edges, links, pairs), edges, pairs, links, nodes, sides);
|
|
636
|
+
addBalls(mesh, nodes, edges, links, pairs, sides);
|
|
637
|
+
const netStart = mesh.positions.length;
|
|
638
|
+
if (net) {
|
|
639
|
+
addNet(mesh, net, nodes, edges);
|
|
640
|
+
}
|
|
641
|
+
if (mesh.indices.length === 0) {
|
|
642
|
+
return null;
|
|
643
|
+
}
|
|
644
|
+
const geometry = new THREE.BufferGeometry();
|
|
645
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute(mesh.positions, 3));
|
|
646
|
+
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(mesh.normals, 3));
|
|
647
|
+
geometry.setIndex(mesh.indices);
|
|
648
|
+
if (net && typeof net.color === 'string') {
|
|
649
|
+
geometry.setAttribute(
|
|
650
|
+
'color',
|
|
651
|
+
netColors(mesh.positions.length, netStart, part.color, net.color)
|
|
652
|
+
);
|
|
653
|
+
geometry.userData.vertexColors = true;
|
|
654
|
+
}
|
|
655
|
+
return geometry;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function netColors(length, netStart, frameColor, netColor) {
|
|
659
|
+
const colors = new Float32Array(length);
|
|
660
|
+
const frame = new THREE.Color(typeof frameColor === 'string' ? frameColor : '#c7cfdd');
|
|
661
|
+
const wires = new THREE.Color(netColor);
|
|
662
|
+
for (let i = 0; i < length; i += 3) {
|
|
663
|
+
const color = i < netStart ? frame : wires;
|
|
664
|
+
colors[i] = color.r;
|
|
665
|
+
colors[i + 1] = color.g;
|
|
666
|
+
colors[i + 2] = color.b;
|
|
667
|
+
}
|
|
668
|
+
return new THREE.Float32BufferAttribute(colors, 3);
|
|
669
|
+
}
|