castle-web-cli 0.4.204 → 0.4.206
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.d.ts +1 -0
- package/dist/agent-prompts.js +7 -2
- package/dist/agent.d.ts +3 -2
- package/dist/agent.js +29 -18
- 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/imports.d.ts +1 -1
- package/dist/imports.js +59 -19
- package/dist/index.js +4 -3
- package/dist/init.d.ts +9 -1
- package/dist/init.js +64 -25
- package/dist/kitCatalog.d.ts +13 -0
- package/dist/kitCatalog.js +111 -0
- package/dist/platformDoc.js +7 -2
- 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 +5 -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 +5 -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-2d/castle.json +1 -0
- package/kits/physics-3d/CLAUDE.md +65 -7
- package/kits/physics-3d/castle.json +4 -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/kits/turn-based/castle.json +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
// Paths for `.pxmodel` parts: the `helix` that makes the path of a tube or a
|
|
2
|
+
// sweep, and `repeat.along`, copies of a part spaced evenly along a curve. See
|
|
3
|
+
// docs/pxmodel-format.md.
|
|
4
|
+
//
|
|
5
|
+
// A curve through a list of points is the Catmull-Rom curve a tube draws. To
|
|
6
|
+
// measure and walk it, it is sampled into a dense polyline, a "line" here.
|
|
7
|
+
// Copies and helix coils sit in frames that follow the line and turn only as
|
|
8
|
+
// much as it does, so they never flip.
|
|
9
|
+
|
|
10
|
+
import * as THREE from 'three';
|
|
11
|
+
|
|
12
|
+
const DEG = Math.PI / 180;
|
|
13
|
+
const MAX_COPIES = 200;
|
|
14
|
+
const MAX_HELIX_POINTS = 600;
|
|
15
|
+
const X = new THREE.Vector3(1, 0, 0);
|
|
16
|
+
const Z = new THREE.Vector3(0, 0, 1);
|
|
17
|
+
|
|
18
|
+
function points3(list) {
|
|
19
|
+
if (!Array.isArray(list)) {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
return list
|
|
23
|
+
.filter((p) => Array.isArray(p) && p.slice(0, 3).every(Number.isFinite))
|
|
24
|
+
.map((p) => new THREE.Vector3(p[0], p[1], p[2]));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function numberOr(value, fallback) {
|
|
28
|
+
return Number.isFinite(value) ? value : fallback;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// A path whose last point is its first is a loop.
|
|
32
|
+
function isLoop(points) {
|
|
33
|
+
return points.length >= 4 && points[0].distanceTo(points[points.length - 1]) < 1e-5;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//
|
|
37
|
+
// Lines
|
|
38
|
+
//
|
|
39
|
+
|
|
40
|
+
// Points along the path, close enough together that straight steps between
|
|
41
|
+
// them follow the curve. A closed path comes back to its first point.
|
|
42
|
+
function densePoints(points, closed, straight) {
|
|
43
|
+
if (straight) {
|
|
44
|
+
return closed ? [...points, points[0].clone()] : points.map((point) => point.clone());
|
|
45
|
+
}
|
|
46
|
+
const curve = new THREE.CatmullRomCurve3(points, closed, 'centripetal');
|
|
47
|
+
const count = Math.max(200, Math.min(2000, points.length * 16));
|
|
48
|
+
return curve.getSpacedPoints(count);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// A line is its `points`, the `tangents` at each point, and `lengths`, the
|
|
52
|
+
// distance along the line to each point. On a smooth line the tangent at a
|
|
53
|
+
// point is the direction from the point before it to the point after it.
|
|
54
|
+
function makeLine(points, closed, straight) {
|
|
55
|
+
const lengths = [0];
|
|
56
|
+
for (let i = 1; i < points.length; i++) {
|
|
57
|
+
lengths.push(lengths[i - 1] + points[i].distanceTo(points[i - 1]));
|
|
58
|
+
}
|
|
59
|
+
const last = points.length - 1;
|
|
60
|
+
const tangents = points.map((point, i) => {
|
|
61
|
+
let before = points[Math.max(0, i - 1)];
|
|
62
|
+
let after = points[Math.min(last, i + 1)];
|
|
63
|
+
if (closed && i === 0) {
|
|
64
|
+
before = points[last - 1];
|
|
65
|
+
}
|
|
66
|
+
if (closed && i === last) {
|
|
67
|
+
after = points[1];
|
|
68
|
+
}
|
|
69
|
+
return after.clone().sub(before).normalize();
|
|
70
|
+
});
|
|
71
|
+
return { points, tangents, lengths, closed, straight };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The position and tangent at `u`, the fraction of the line's length.
|
|
75
|
+
function sampleLine(line, u) {
|
|
76
|
+
const { points, tangents, lengths } = line;
|
|
77
|
+
const distance = Math.max(0, Math.min(1, u)) * lengths[lengths.length - 1];
|
|
78
|
+
let low = 0;
|
|
79
|
+
let high = lengths.length - 1;
|
|
80
|
+
while (high - low > 1) {
|
|
81
|
+
const middle = (low + high) >> 1;
|
|
82
|
+
if (lengths[middle] <= distance) {
|
|
83
|
+
low = middle;
|
|
84
|
+
} else {
|
|
85
|
+
high = middle;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const span = lengths[high] - lengths[low];
|
|
89
|
+
const blend = span > 0 ? (distance - lengths[low]) / span : 0;
|
|
90
|
+
const position = points[low].clone().lerp(points[high], blend);
|
|
91
|
+
|
|
92
|
+
// A straight line keeps each segment's direction up to its corner.
|
|
93
|
+
const tangent = line.straight
|
|
94
|
+
? points[high].clone().sub(points[low])
|
|
95
|
+
: tangents[low].clone().lerp(tangents[high], blend);
|
|
96
|
+
if (tangent.lengthSq() < 1e-12) {
|
|
97
|
+
tangent.copy(tangents[low]);
|
|
98
|
+
}
|
|
99
|
+
return { position, tangent: tangent.normalize() };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
//
|
|
103
|
+
// Frames
|
|
104
|
+
//
|
|
105
|
+
|
|
106
|
+
// A frame is a `position`, the `tangent` there and a `normal` across it. This
|
|
107
|
+
// normal is `prefer` made square to the tangent, or `fallback` when `prefer`
|
|
108
|
+
// runs along the tangent.
|
|
109
|
+
function squareNormal(tangent, prefer, fallback) {
|
|
110
|
+
const direction = Math.abs(prefer.dot(tangent)) > 0.99 ? fallback : prefer;
|
|
111
|
+
return direction.clone().addScaledVector(tangent, -direction.dot(tangent)).normalize();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// A normal pointing away from the middle of the line (the average of its
|
|
115
|
+
// points), so on a loop or an arc it points outward. A straight line has no
|
|
116
|
+
// outside, and its normal is +z (+x where the line runs along z).
|
|
117
|
+
function outwardNormal(line, frame) {
|
|
118
|
+
const middle = new THREE.Vector3();
|
|
119
|
+
line.points.forEach((point) => middle.add(point));
|
|
120
|
+
middle.divideScalar(line.points.length);
|
|
121
|
+
const away = frame.position.clone().sub(middle);
|
|
122
|
+
away.addScaledVector(frame.tangent, -away.dot(frame.tangent));
|
|
123
|
+
const length = line.lengths[line.lengths.length - 1];
|
|
124
|
+
if (away.length() < length * 1e-3) {
|
|
125
|
+
return squareNormal(frame.tangent, Z, X);
|
|
126
|
+
}
|
|
127
|
+
return away.normalize();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// The normal carried from one frame to the next by two reflections (Wang et
|
|
131
|
+
// al. 2008, "rotation minimizing frames"), so it turns only as much as the
|
|
132
|
+
// line does.
|
|
133
|
+
function carryNormal(previous, next) {
|
|
134
|
+
const step = next.position.clone().sub(previous.position);
|
|
135
|
+
const c1 = step.dot(step);
|
|
136
|
+
if (c1 < 1e-14) {
|
|
137
|
+
return previous.normal.clone();
|
|
138
|
+
}
|
|
139
|
+
const normal = previous.normal
|
|
140
|
+
.clone()
|
|
141
|
+
.addScaledVector(step, (-2 / c1) * step.dot(previous.normal));
|
|
142
|
+
const tangent = previous.tangent
|
|
143
|
+
.clone()
|
|
144
|
+
.addScaledVector(step, (-2 / c1) * step.dot(previous.tangent));
|
|
145
|
+
const second = next.tangent.clone().sub(tangent);
|
|
146
|
+
const c2 = second.dot(second);
|
|
147
|
+
if (c2 > 1e-14) {
|
|
148
|
+
normal.addScaledVector(second, (-2 / c2) * second.dot(normal));
|
|
149
|
+
}
|
|
150
|
+
return normal.addScaledVector(next.tangent, -normal.dot(next.tangent)).normalize();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Frames at each fraction in `fractions`, which run from low to high. The
|
|
154
|
+
// first normal comes from `firstNormal(frame)`. It is carried in small steps
|
|
155
|
+
// between fractions, so frames far apart still agree with the curve between.
|
|
156
|
+
function lineFrames(line, fractions, firstNormal) {
|
|
157
|
+
let frame = sampleLine(line, fractions[0]);
|
|
158
|
+
frame.normal = firstNormal(frame);
|
|
159
|
+
let reached = fractions[0];
|
|
160
|
+
const frames = [];
|
|
161
|
+
for (const fraction of fractions) {
|
|
162
|
+
const steps = Math.max(1, Math.ceil((fraction - reached) * 512));
|
|
163
|
+
for (let step = 1; step <= steps; step++) {
|
|
164
|
+
const next = sampleLine(line, reached + ((fraction - reached) * step) / steps);
|
|
165
|
+
next.normal = carryNormal(frame, next);
|
|
166
|
+
frame = next;
|
|
167
|
+
}
|
|
168
|
+
reached = fraction;
|
|
169
|
+
frames.push(frame);
|
|
170
|
+
}
|
|
171
|
+
return frames;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
//
|
|
175
|
+
// Helix
|
|
176
|
+
//
|
|
177
|
+
|
|
178
|
+
// `helix` is { radius, turns, height, phase }: a coil going up +y from the
|
|
179
|
+
// pivot, starting at +x when `phase` is 0 and turning from +x toward -z, like
|
|
180
|
+
// a common screw thread. With `around`, a list of points, the coil winds about
|
|
181
|
+
// a smooth curve through them instead, and `height` is not used.
|
|
182
|
+
export function helixPoints(helix) {
|
|
183
|
+
const radius = numberOr(helix.radius, 0.1);
|
|
184
|
+
const turns = Math.max(0, Math.min(50, numberOr(helix.turns, 3)));
|
|
185
|
+
const height = numberOr(helix.height, 0.3);
|
|
186
|
+
const phase = numberOr(helix.phase, 0) * DEG;
|
|
187
|
+
const count = Math.min(MAX_HELIX_POINTS, Math.max(24, Math.ceil(turns * 12)));
|
|
188
|
+
const angle = (i) => phase + (i / count) * turns * Math.PI * 2;
|
|
189
|
+
const around = points3(helix.around);
|
|
190
|
+
const points = [];
|
|
191
|
+
if (around.length < 2) {
|
|
192
|
+
for (let i = 0; i <= count; i++) {
|
|
193
|
+
const a = angle(i);
|
|
194
|
+
points.push(
|
|
195
|
+
new THREE.Vector3(radius * Math.cos(a), (height * i) / count, -radius * Math.sin(a))
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
return points;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Around a curve, +x and -z become the frame's normal and the side
|
|
202
|
+
// (tangent x normal); on a curve going up +y they are +x and -z again.
|
|
203
|
+
const line = makeLine(densePoints(around, false, false), false, false);
|
|
204
|
+
const fractions = Array.from({ length: count + 1 }, (_, i) => i / count);
|
|
205
|
+
lineFrames(line, fractions, (frame) => squareNormal(frame.tangent, X, Z)).forEach((frame, i) => {
|
|
206
|
+
const a = angle(i);
|
|
207
|
+
const side = frame.tangent.clone().cross(frame.normal);
|
|
208
|
+
points.push(
|
|
209
|
+
frame.position
|
|
210
|
+
.clone()
|
|
211
|
+
.addScaledVector(frame.normal, radius * Math.cos(a))
|
|
212
|
+
.addScaledVector(side, radius * Math.sin(a))
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
return points;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// The points a tube or sweep follows and whether they close into a loop: the
|
|
219
|
+
// `helix` if the part has one, else its `path`. A helix with no height and
|
|
220
|
+
// whole turns is a circle, which comes back as a loop.
|
|
221
|
+
export function partPath(part) {
|
|
222
|
+
if (part.helix && typeof part.helix === 'object') {
|
|
223
|
+
const points = helixPoints(part.helix);
|
|
224
|
+
if (isLoop(points)) {
|
|
225
|
+
return { points: points.slice(0, -1), closed: true };
|
|
226
|
+
}
|
|
227
|
+
return { points, closed: Boolean(part.closed) };
|
|
228
|
+
}
|
|
229
|
+
return { points: points3(part.path), closed: Boolean(part.closed) };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
//
|
|
233
|
+
// Repeat along
|
|
234
|
+
//
|
|
235
|
+
|
|
236
|
+
// The line copies follow, in the space of the repeated part's parent: a curve
|
|
237
|
+
// through a list of points measured from the part's position, as a tube's
|
|
238
|
+
// path is, or the path of another part, carried from that part's space.
|
|
239
|
+
function alongLine(along, part, parent, groups, parts) {
|
|
240
|
+
if (Array.isArray(along)) {
|
|
241
|
+
const start = new THREE.Vector3(...part.position);
|
|
242
|
+
const points = points3(along).map((point) => point.add(start));
|
|
243
|
+
if (points.length < 2) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
const closed = isLoop(points);
|
|
247
|
+
const path = closed ? points.slice(0, -1) : points;
|
|
248
|
+
return makeLine(densePoints(path, closed, false), closed, false);
|
|
249
|
+
}
|
|
250
|
+
const target = parts.get(along);
|
|
251
|
+
const mesh = groups.get(along)?.children.find((child) => child.userData.partMesh === along);
|
|
252
|
+
if (!target || !mesh || along === part.id) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
const { points, closed } = partPath(target);
|
|
256
|
+
if (points.length < 2) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
const straight = target.kind === 'sweep' && Boolean(target.straight);
|
|
260
|
+
mesh.updateWorldMatrix(true, false);
|
|
261
|
+
parent.updateWorldMatrix(true, false);
|
|
262
|
+
const toParent = parent.matrixWorld.clone().invert().multiply(mesh.matrixWorld);
|
|
263
|
+
const dense = densePoints(points, closed, straight).map((point) => point.applyMatrix4(toParent));
|
|
264
|
+
return makeLine(dense, closed, straight);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Where the copies go, as fractions of the line's length: from end to end on
|
|
268
|
+
// an open line, and evenly all the way round a loop.
|
|
269
|
+
function spacing(count, closed) {
|
|
270
|
+
if (closed) {
|
|
271
|
+
return Array.from({ length: count }, (_, n) => n / count);
|
|
272
|
+
}
|
|
273
|
+
if (count === 1) {
|
|
274
|
+
return [0.5];
|
|
275
|
+
}
|
|
276
|
+
return Array.from({ length: count }, (_, n) => n / (count - 1));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// `repeat.along` places `count` copies of the part evenly along a line, and
|
|
280
|
+
// the part itself is hidden as the pattern. Each copy sits in a frame at its
|
|
281
|
+
// point; with `align` the frame's +y is the line's direction and its +z starts
|
|
282
|
+
// away from the middle of the line. The part's own rotation turns
|
|
283
|
+
// each copy inside its frame. `alternate` turns every other copy by that many
|
|
284
|
+
// degrees about the frame's axes. `copyOf` and `jitterCopy` are the ones
|
|
285
|
+
// `repeat` uses.
|
|
286
|
+
export function repeatAlong(part, group, groups, parts, copyOf, jitterCopy) {
|
|
287
|
+
const settings = part.repeat;
|
|
288
|
+
const parent = group.parent;
|
|
289
|
+
const line = alongLine(settings.along, part, parent, groups, parts);
|
|
290
|
+
if (!line) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const count = Math.max(1, Math.min(MAX_COPIES, Math.round(numberOr(settings.count, 10))));
|
|
294
|
+
const frames = lineFrames(line, spacing(count, line.closed), (frame) =>
|
|
295
|
+
outwardNormal(line, frame)
|
|
296
|
+
);
|
|
297
|
+
const alternate = Array.isArray(settings.alternate)
|
|
298
|
+
? new THREE.Quaternion().setFromEuler(
|
|
299
|
+
new THREE.Euler(...[0, 1, 2].map((i) => numberOr(settings.alternate[i], 0) * DEG))
|
|
300
|
+
)
|
|
301
|
+
: null;
|
|
302
|
+
const basis = new THREE.Matrix4();
|
|
303
|
+
frames.forEach((frame, n) => {
|
|
304
|
+
const holder = new THREE.Group();
|
|
305
|
+
holder.userData.copyOf = part.id;
|
|
306
|
+
holder.position.copy(frame.position);
|
|
307
|
+
if (settings.align) {
|
|
308
|
+
const side = frame.tangent.clone().cross(frame.normal);
|
|
309
|
+
holder.quaternion.setFromRotationMatrix(basis.makeBasis(side, frame.tangent, frame.normal));
|
|
310
|
+
}
|
|
311
|
+
if (alternate && n % 2 === 1) {
|
|
312
|
+
holder.quaternion.multiply(alternate);
|
|
313
|
+
}
|
|
314
|
+
const copy = copyOf(group);
|
|
315
|
+
copy.visible = true;
|
|
316
|
+
copy.position.set(0, 0, 0);
|
|
317
|
+
if (settings.jitter) {
|
|
318
|
+
jitterCopy(copy, settings.jitter, n + 1);
|
|
319
|
+
}
|
|
320
|
+
holder.add(copy);
|
|
321
|
+
parent.add(holder);
|
|
322
|
+
});
|
|
323
|
+
group.visible = false;
|
|
324
|
+
}
|