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.
Files changed (125) hide show
  1. package/dist/agent-failures.d.ts +3 -1
  2. package/dist/agent-failures.js +19 -0
  3. package/dist/agent-prompts.js +2 -1
  4. package/dist/agent.d.ts +3 -2
  5. package/dist/agent.js +19 -10
  6. package/dist/castle-host/host.d.ts +17 -0
  7. package/dist/castle-host/host.js +551 -4
  8. package/dist/devSessionStorage.js +65 -4
  9. package/dist/effortSupport.d.ts +7 -0
  10. package/dist/effortSupport.js +31 -0
  11. package/dist/init.js +13 -3
  12. package/dist/platformDoc.js +6 -1
  13. package/dist/serve.js +35 -2
  14. package/dist/shell/assets/{index-DHzA0-Sj.js → index-hWQTC8q6.js} +5 -5
  15. package/dist/shell/index.html +1 -1
  16. package/dist/vitePlugins.js +19 -1
  17. package/kits/base/CLAUDE.md +2 -0
  18. package/kits/base/castle.json +1 -1
  19. package/kits/base/sdk/README.md +131 -0
  20. package/kits/base/sdk/agent.d.ts +6 -0
  21. package/kits/base/sdk/agent.js +70 -0
  22. package/kits/base/sdk/castle.d.ts +4 -0
  23. package/kits/base/sdk/castle.js +2 -0
  24. package/kits/base/sdk/commands.d.ts +106 -0
  25. package/kits/base/sdk/commands.js +21 -0
  26. package/kits/base/sdk/decks.d.ts +7 -0
  27. package/kits/base/sdk/decks.js +31 -0
  28. package/kits/base/sdk/runtime.d.ts +4 -2
  29. package/kits/base/sdk/runtime.js +51 -40
  30. package/kits/base/sdk/server/storage.d.ts +44 -0
  31. package/kits/base/sdk/server/storage.js +54 -0
  32. package/kits/base/sdk/transport.d.ts +5 -1
  33. package/kits/base/sdk/transport.js +106 -23
  34. package/kits/multiplayer-2d/castle.json +4 -4
  35. package/kits/multiplayer-2d/code/client/poses.js +7 -0
  36. package/kits/multiplayer-2d/code/server/limits.js +3 -0
  37. package/kits/multiplayer-2d/code/server/ownership.js +36 -8
  38. package/kits/multiplayer-2d/code/systems/multiplayer.js +23 -2
  39. package/kits/multiplayer-3d/CLAUDE.md +12 -1
  40. package/kits/multiplayer-3d/castle.json +4 -4
  41. package/kits/multiplayer-3d/code/client/avatars.js +7 -1
  42. package/kits/multiplayer-3d/code/client/poses.js +61 -1
  43. package/kits/multiplayer-3d/code/client/steer.js +383 -0
  44. package/kits/multiplayer-3d/code/server/limits.js +3 -0
  45. package/kits/multiplayer-3d/code/server/ownership.js +114 -18
  46. package/kits/multiplayer-3d/code/server/world.js +12 -4
  47. package/kits/multiplayer-3d/code/systems/multiplayer.js +315 -34
  48. package/kits/physics-3d/CLAUDE.md +65 -7
  49. package/kits/physics-3d/castle.json +3 -3
  50. package/kits/physics-3d/docs/pxmodel-format.md +916 -15
  51. package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
  52. package/kits/physics-3d/engine3d/artobjects.js +218 -0
  53. package/kits/physics-3d/engine3d/blob.js +1058 -0
  54. package/kits/physics-3d/engine3d/branches.js +300 -0
  55. package/kits/physics-3d/engine3d/cage.js +496 -0
  56. package/kits/physics-3d/engine3d/cageops.js +516 -0
  57. package/kits/physics-3d/engine3d/frame.js +669 -0
  58. package/kits/physics-3d/engine3d/look.js +675 -0
  59. package/kits/physics-3d/engine3d/materialpresets.js +486 -0
  60. package/kits/physics-3d/engine3d/modelEditorWorld.js +3 -2
  61. package/kits/physics-3d/engine3d/modelrefs.js +120 -0
  62. package/kits/physics-3d/engine3d/motion.js +560 -0
  63. package/kits/physics-3d/engine3d/objtext.js +37 -0
  64. package/kits/physics-3d/engine3d/optimize.js +1083 -0
  65. package/kits/physics-3d/engine3d/paint.js +674 -0
  66. package/kits/physics-3d/engine3d/partgeometry.js +467 -0
  67. package/kits/physics-3d/engine3d/paths.js +324 -0
  68. package/kits/physics-3d/engine3d/pxmodel.js +429 -26
  69. package/kits/physics-3d/engine3d/scatter.js +102 -0
  70. package/kits/physics-3d/engine3d/sculpt.js +1148 -0
  71. package/kits/physics-3d/engine3d/surfacedetail.js +267 -0
  72. package/kits/physics-3d/engine3d/sweep.js +480 -0
  73. package/kits/physics-3d/engine3d/thumbnails.js +6 -4
  74. package/kits/physics-3d/engine3d/world3d.js +119 -17
  75. package/kits/physics-3d/eslint.config.js +13 -0
  76. package/kits/physics-3d/models/parts/axe.pxmodel +40 -0
  77. package/kits/physics-3d/models/parts/backpack.pxmodel +47 -0
  78. package/kits/physics-3d/models/parts/barrel.pxmodel +22 -0
  79. package/kits/physics-3d/models/parts/bedroll.pxmodel +28 -0
  80. package/kits/physics-3d/models/parts/belt-pouch.pxmodel +22 -0
  81. package/kits/physics-3d/models/parts/bolt.pxmodel +10 -0
  82. package/kits/physics-3d/models/parts/bracer.pxmodel +26 -0
  83. package/kits/physics-3d/models/parts/chain-span.pxmodel +12 -0
  84. package/kits/physics-3d/models/parts/chimney.pxmodel +11 -0
  85. package/kits/physics-3d/models/parts/crate.pxmodel +44 -0
  86. package/kits/physics-3d/models/parts/crystal.pxmodel +22 -0
  87. package/kits/physics-3d/models/parts/door-arched.pxmodel +12 -0
  88. package/kits/physics-3d/models/parts/engine-block.pxmodel +39 -0
  89. package/kits/physics-3d/models/parts/exhaust-pipe.pxmodel +30 -0
  90. package/kits/physics-3d/models/parts/fence-span.pxmodel +10 -0
  91. package/kits/physics-3d/models/parts/fender.pxmodel +19 -0
  92. package/kits/physics-3d/models/parts/headlight.pxmodel +24 -0
  93. package/kits/physics-3d/models/parts/hood.pxmodel +17 -0
  94. package/kits/physics-3d/models/parts/horned-helmet.pxmodel +37 -0
  95. package/kits/physics-3d/models/parts/jerry-can.pxmodel +26 -0
  96. package/kits/physics-3d/models/parts/ladder.pxmodel +10 -0
  97. package/kits/physics-3d/models/parts/lamp.pxmodel +20 -0
  98. package/kits/physics-3d/models/parts/lantern.pxmodel +30 -0
  99. package/kits/physics-3d/models/parts/leaf-cluster.pxmodel +21 -0
  100. package/kits/physics-3d/models/parts/offroad-tire.pxmodel +48 -0
  101. package/kits/physics-3d/models/parts/pauldron.pxmodel +26 -0
  102. package/kits/physics-3d/models/parts/rivet.pxmodel +8 -0
  103. package/kits/physics-3d/models/parts/rock.pxmodel +21 -0
  104. package/kits/physics-3d/models/parts/roll-bar-light.pxmodel +26 -0
  105. package/kits/physics-3d/models/parts/round-shield.pxmodel +31 -0
  106. package/kits/physics-3d/models/parts/saddlebag.pxmodel +30 -0
  107. package/kits/physics-3d/models/parts/spoked-wheel.pxmodel +39 -0
  108. package/kits/physics-3d/models/parts/staff.pxmodel +33 -0
  109. package/kits/physics-3d/models/parts/stepping-stones.pxmodel +8 -0
  110. package/kits/physics-3d/models/parts/stone-base.pxmodel +10 -0
  111. package/kits/physics-3d/models/parts/sword.pxmodel +33 -0
  112. package/kits/physics-3d/models/parts/window-lit.pxmodel +13 -0
  113. package/kits/physics-3d/models/parts/wizard-hat.pxmodel +28 -0
  114. package/kits/physics-3d/models/templates/biped-stout.pxmodel +59 -0
  115. package/kits/physics-3d/models/templates/biped-tall.pxmodel +63 -0
  116. package/kits/physics-3d/models/templates/quadruped.pxmodel +52 -0
  117. package/kits/physics-3d/scripts/render/filmstrip.js +217 -0
  118. package/kits/physics-3d/scripts/render/index.html +20 -0
  119. package/kits/physics-3d/scripts/render/sheet.js +411 -0
  120. package/kits/physics-3d/scripts/render.mjs +646 -0
  121. package/kits/real-time/castle.json +2 -2
  122. package/kits/real-time/code/client/ownership.js +14 -4
  123. package/kits/real-time/code/client/smooth.js +2 -2
  124. package/kits/real-time/code/server/session.js +20 -4
  125. package/package.json +2 -2
@@ -0,0 +1,467 @@
1
+ // Geometry for the `.pxmodel` part kinds that are not Shape kinds: lathe,
2
+ // extrude, tube, torus and cone, plus the model-only options on the shared
3
+ // kinds -- a rounded box, a tapered cylinder and a lumpy sphere. See
4
+ // docs/pxmodel-format.md for the fields.
5
+ //
6
+ // Every builder returns a THREE.BufferGeometry centered the way the format
7
+ // doc describes, or null when the part's fields cannot make one, in which case
8
+ // the caller falls back to the plain shape.
9
+
10
+ import { objCage } from './objtext';
11
+ import * as THREE from 'three';
12
+ import { RoundedBoxGeometry } from 'three/examples/jsm/geometries/RoundedBoxGeometry.js';
13
+ import { mergeVertices } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
14
+ import { cageGeometry, loftCage } from './cage';
15
+ import { surfaceGeometry, sweepGeometry } from './sweep';
16
+ import { branchesGeometry } from './branches';
17
+ import { partPath } from './paths';
18
+ import { blobGeometry } from './blob';
19
+ import { frameGeometry } from './frame';
20
+ import { hasSculpt, sculptGeometry } from './sculpt';
21
+ import { circleSides, lengthSteps, pixelsInPart } from './optimize';
22
+
23
+ const DEG = Math.PI / 180;
24
+
25
+ // Model-only kinds, and the options that make a shared kind model-only.
26
+ export const MODEL_KINDS = new Set([
27
+ 'lathe',
28
+ 'extrude',
29
+ 'tube',
30
+ 'torus',
31
+ 'cone',
32
+ 'loft',
33
+ 'sweep',
34
+ 'surface',
35
+ 'branches',
36
+ 'blob',
37
+ 'frame',
38
+ ]);
39
+
40
+ const DEFORMERS = ['taper', 'twist', 'fold', 'bend'];
41
+
42
+ export function hasDeformers(part) {
43
+ return DEFORMERS.some((key) => part[key] !== undefined && part[key] !== null && part[key] !== 0);
44
+ }
45
+
46
+ export function needsModelGeometry(part) {
47
+ if (MODEL_KINDS.has(part.kind) || hasDeformers(part) || part.displace > 0 || hasSculpt(part)) {
48
+ return true;
49
+ }
50
+ if (part.kind === 'mesh') {
51
+ return (
52
+ part.subdivide > 0 ||
53
+ part.symmetric === 'x' ||
54
+ Boolean(part.from) ||
55
+ Boolean(part.ops) ||
56
+ typeof part.obj === 'string'
57
+ );
58
+ }
59
+ if (part.kind === 'box') {
60
+ return part.round > 0;
61
+ }
62
+
63
+ // Round kinds are built here so their sides follow their size on screen.
64
+ return part.kind === 'cylinder' || part.kind === 'sphere' || part.kind === 'capsule';
65
+ }
66
+
67
+ function points2(list) {
68
+ if (!Array.isArray(list)) {
69
+ return [];
70
+ }
71
+ return list
72
+ .filter((p) => Array.isArray(p) && Number.isFinite(p[0]) && Number.isFinite(p[1]))
73
+ .map((p) => new THREE.Vector2(p[0], p[1]));
74
+ }
75
+
76
+ //
77
+ // Lathe
78
+ //
79
+
80
+ // `profile` is [radius, y] points from bottom to top, spun around the y axis.
81
+ // A radius of 0 at either end closes that end.
82
+ function latheGeometry(part) {
83
+ const profile = points2(part.profile).map((p) => new THREE.Vector2(Math.max(0, p.x), p.y));
84
+ if (profile.length < 2) {
85
+ return null;
86
+ }
87
+ const widest = Math.max(...profile.map((p) => p.x));
88
+ const segments = circleSides(part, widest, clampInt(part.segments, 3, 64, 24));
89
+ return new THREE.LatheGeometry(profile, segments);
90
+ }
91
+
92
+ //
93
+ // Extrude
94
+ //
95
+
96
+ // `outline` is [x, y] points of a flat shape, pushed `depth` along z and
97
+ // centered on z = 0. `holes` are outlines cut through it. With `smooth` the
98
+ // outline is a curve through its points rather than straight edges.
99
+ function extrudeGeometry(part) {
100
+ const outline = points2(part.outline);
101
+ if (outline.length < 3) {
102
+ return null;
103
+ }
104
+ const shape = new THREE.Shape();
105
+ if (part.smooth) {
106
+ shape.moveTo(outline[0].x, outline[0].y);
107
+ shape.splineThru([...outline.slice(1), outline[0]]);
108
+ } else {
109
+ shape.setFromPoints(outline);
110
+ }
111
+ for (const hole of Array.isArray(part.holes) ? part.holes : []) {
112
+ const points = points2(hole);
113
+ if (points.length >= 3) {
114
+ shape.holes.push(new THREE.Path(points));
115
+ }
116
+ }
117
+ const depth = Number.isFinite(part.depth) && part.depth > 0 ? part.depth : 0.1;
118
+ const bevel = Number.isFinite(part.bevel) && part.bevel > 0 ? part.bevel : 0;
119
+ const geometry = new THREE.ExtrudeGeometry(shape, {
120
+ depth,
121
+ bevelEnabled: bevel > 0,
122
+ bevelSize: bevel,
123
+ bevelThickness: bevel,
124
+ bevelSegments: 2,
125
+ curveSegments: 16,
126
+ });
127
+ geometry.translate(0, 0, -depth / 2);
128
+ return geometry;
129
+ }
130
+
131
+ //
132
+ // Tube
133
+ //
134
+
135
+ // A tube swept along a smooth curve through `path`, [x, y, z] points. The
136
+ // radius runs from `radius` at the first point to `radiusEnd` at the last, so
137
+ // branches, horns and tails taper. Ends are capped.
138
+ function tubeGeometry(part) {
139
+ const { points: path, closed } = partPath(part);
140
+ if (path.length < 2) {
141
+ return null;
142
+ }
143
+ const curve = new THREE.CatmullRomCurve3(path, closed, 'centripetal');
144
+
145
+ // A helix has many turns, each needing its own steps.
146
+ const mostSegments = part.helix ? 512 : 128;
147
+ const start = Number.isFinite(part.radius) ? part.radius : 0.05;
148
+ const end = Number.isFinite(part.radiusEnd) ? part.radiusEnd : start;
149
+ const most = clampInt(part.segments, 2, mostSegments, Math.max(8, path.length * 8));
150
+ const lengthSegments = lengthSteps(part, curve.getLength(), most, Math.min(most, path.length));
151
+ const thickest = Math.max(start, end, ...(Array.isArray(part.radii) ? part.radii : []));
152
+ const radialSegments = circleSides(part, thickest, 12);
153
+
154
+ // `radii`, one per path point, overrides radius and radiusEnd; between
155
+ // points the radius eases smoothly, so a tail can swell and come to a point.
156
+ // A helix writes no points, so its radii are spread evenly along it.
157
+ const radiiCount = part.helix ? part.radii?.length >= 2 : part.radii?.length === path.length;
158
+ const radii = Array.isArray(part.radii) && radiiCount && part.radii.every(Number.isFinite) ? part.radii : null;
159
+ const radiusAt = (t) => {
160
+ if (!radii) {
161
+ return start + (end - start) * t;
162
+ }
163
+ const at = t * (radii.length - 1);
164
+ const index = Math.min(radii.length - 2, Math.floor(at));
165
+ const blend = at - index;
166
+ const eased = blend * blend * (3 - 2 * blend);
167
+ return radii[index] + (radii[index + 1] - radii[index]) * eased;
168
+ };
169
+ const frames = curve.computeFrenetFrames(lengthSegments, closed);
170
+ const positions = [];
171
+ const indices = [];
172
+
173
+ // One ring of vertices per step along the curve.
174
+ for (let step = 0; step <= lengthSegments; step++) {
175
+ const t = step / lengthSegments;
176
+ const center = curve.getPointAt(t);
177
+ const radius = Math.max(0, radiusAt(t));
178
+ const normal = frames.normals[step];
179
+ const binormal = frames.binormals[step];
180
+ for (let side = 0; side < radialSegments; side++) {
181
+ const angle = (side / radialSegments) * Math.PI * 2;
182
+ const cos = Math.cos(angle);
183
+ const sin = Math.sin(angle);
184
+ positions.push(
185
+ center.x + radius * (cos * normal.x + sin * binormal.x),
186
+ center.y + radius * (cos * normal.y + sin * binormal.y),
187
+ center.z + radius * (cos * normal.z + sin * binormal.z)
188
+ );
189
+ }
190
+ }
191
+ for (let step = 0; step < lengthSegments; step++) {
192
+ for (let side = 0; side < radialSegments; side++) {
193
+ const a = step * radialSegments + side;
194
+ const b = step * radialSegments + ((side + 1) % radialSegments);
195
+ const c = a + radialSegments;
196
+ const d = b + radialSegments;
197
+ // Seen from outside, a -> b runs around the ring and a -> c along the
198
+ // path; this winding faces outward.
199
+ indices.push(a, b, c, b, d, c);
200
+ }
201
+ }
202
+
203
+ // Caps: a center vertex at each end, fanned to its ring.
204
+ if (!closed) {
205
+ for (const [step, flip] of [
206
+ [0, true],
207
+ [lengthSegments, false],
208
+ ]) {
209
+ const center = curve.getPointAt(step / lengthSegments);
210
+ const centerIndex = positions.length / 3;
211
+ positions.push(center.x, center.y, center.z);
212
+ for (let side = 0; side < radialSegments; side++) {
213
+ const a = step * radialSegments + side;
214
+ const b = step * radialSegments + ((side + 1) % radialSegments);
215
+ indices.push(...(flip ? [centerIndex, b, a] : [centerIndex, a, b]));
216
+ }
217
+ }
218
+ }
219
+ const geometry = new THREE.BufferGeometry();
220
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
221
+ geometry.setIndex(indices);
222
+ geometry.computeVertexNormals();
223
+ return geometry;
224
+ }
225
+
226
+ //
227
+ // Torus, cone, tapered cylinder, rounded box
228
+ //
229
+
230
+ // A ring in the x-y plane around the z axis: `radius` to the middle of the
231
+ // tube, `tube` the tube's own radius, `arc` in degrees for a partial ring.
232
+ function torusGeometry(part) {
233
+ const radius = Number.isFinite(part.radius) ? part.radius : 0.5;
234
+ const tube = Number.isFinite(part.tube) && part.tube > 0 ? part.tube : radius * 0.2;
235
+ const arc = Number.isFinite(part.arc) ? Math.max(1, Math.min(360, part.arc)) : 360;
236
+ const around = circleSides(part, radius + tube, clampInt(part.segments, 3, 64, 32)) * (arc / 360);
237
+ return new THREE.TorusGeometry(
238
+ radius,
239
+ tube,
240
+ circleSides(part, tube, 12),
241
+ Math.max(3, Math.round(around)),
242
+ arc * DEG
243
+ );
244
+ }
245
+
246
+ function cylinderGeometry(part, radiusTop) {
247
+ const radius = Number.isFinite(part.radius) ? part.radius : 0.5;
248
+ const height = Number.isFinite(part.height) ? part.height : 1;
249
+ const most = clampInt(part.segments, 3, 64, 24);
250
+ const segments = circleSides(part, Math.max(radius, radiusTop), most);
251
+ return new THREE.CylinderGeometry(Math.max(0, radiusTop), radius, height, segments);
252
+ }
253
+
254
+ function roundedBoxGeometry(part) {
255
+ const [width, height, depth] = part.size;
256
+ const radius = Math.min(part.round, width / 2, height / 2, depth / 2);
257
+
258
+ // `roundSegments` steps round each edge; by default as many as its size on
259
+ // screen shows, from 1 to 3. Rounding under a pixel wide is not drawn.
260
+ if (radius < pixelsInPart(part, 1, 2 * radius) && !Number.isFinite(part.roundSegments)) {
261
+ return new THREE.BoxGeometry(width, height, depth);
262
+ }
263
+ const most = clampInt(part.roundSegments, 1, 8, 3);
264
+ const steps = Math.max(1, Math.round(circleSides(part, radius, most * 4, 4) / 4));
265
+ return new RoundedBoxGeometry(width, height, depth, steps, radius);
266
+ }
267
+
268
+ //
269
+ // Lumpy sphere
270
+ //
271
+
272
+ // Smooth 3d value noise in [-1, 1]: random values on an integer grid, blended
273
+ // with a smoothstep. The seed picks a different grid.
274
+ function hash(x, y, z, seed) {
275
+ let h = (x * 374761393 + y * 668265263 + z * 2147483647 + seed * 144665) | 0;
276
+ h = Math.imul(h ^ (h >>> 13), 1274126177);
277
+ return ((h ^ (h >>> 16)) >>> 0) / 4294967295;
278
+ }
279
+
280
+ export function valueNoise(x, y, z, seed) {
281
+ const x0 = Math.floor(x);
282
+ const y0 = Math.floor(y);
283
+ const z0 = Math.floor(z);
284
+ const fade = (t) => t * t * (3 - 2 * t);
285
+ const u = fade(x - x0);
286
+ const v = fade(y - y0);
287
+ const w = fade(z - z0);
288
+ const lerp = (a, b, t) => a + (b - a) * t;
289
+ const corner = (dx, dy, dz) => hash(x0 + dx, y0 + dy, z0 + dz, seed);
290
+ const bottom = lerp(
291
+ lerp(corner(0, 0, 0), corner(1, 0, 0), u),
292
+ lerp(corner(0, 1, 0), corner(1, 1, 0), u),
293
+ v
294
+ );
295
+ const top = lerp(
296
+ lerp(corner(0, 0, 1), corner(1, 0, 1), u),
297
+ lerp(corner(0, 1, 1), corner(1, 1, 1), u),
298
+ v
299
+ );
300
+ return lerp(bottom, top, w) * 2 - 1;
301
+ }
302
+
303
+ // A sphere whose surface is pushed in and out by noise: `noise` is how far, as
304
+ // a fraction of the radius, and `seed` picks the pattern. Two octaves, so a
305
+ // bush or a rock has large lumps with smaller ones on them.
306
+ function lumpySphereGeometry(part) {
307
+ const radius = Number.isFinite(part.radius) ? part.radius : 0.5;
308
+ const amount = Math.min(part.noise, 1);
309
+ const seed = Number.isFinite(part.seed) ? part.seed : 1;
310
+
311
+ // `detail` (0 to 4) is the most times the icosahedron is split; a small
312
+ // sphere is split less, by its size on screen.
313
+ const most = clampInt(part.detail, 0, 4, 4);
314
+ const level = Math.min(most, Math.round(circleSides(part, radius, 25, 5) / 5) - 1);
315
+ const geometry = mergeVertices(new THREE.IcosahedronGeometry(radius, level));
316
+ const position = geometry.attributes.position;
317
+ const point = new THREE.Vector3();
318
+ for (let i = 0; i < position.count; i++) {
319
+ point.fromBufferAttribute(position, i);
320
+ const unit = point.clone().normalize();
321
+ const n =
322
+ valueNoise(unit.x * 2.2, unit.y * 2.2, unit.z * 2.2, seed) * 0.7 +
323
+ valueNoise(unit.x * 5, unit.y * 5, unit.z * 5, seed + 7) * 0.3;
324
+ point.copy(unit.multiplyScalar(radius * (1 + amount * n)));
325
+ position.setXYZ(i, point.x, point.y, point.z);
326
+ }
327
+ geometry.computeVertexNormals();
328
+ return geometry;
329
+ }
330
+
331
+ // A sphere with `segments` sides around, and half as many from pole to pole.
332
+ function sphereGeometry(part) {
333
+ const around = circleSides(part, part.radius, clampInt(part.segments, 3, 64, 24));
334
+ return new THREE.SphereGeometry(part.radius, around, Math.max(3, Math.round(around * 0.67)));
335
+ }
336
+
337
+ function clampInt(value, min, max, fallback) {
338
+ if (!Number.isFinite(value)) {
339
+ return fallback;
340
+ }
341
+ return Math.max(min, Math.min(max, Math.round(value)));
342
+ }
343
+
344
+ // Sculpt strokes (sculpt.js) apply to the finished shape before the
345
+ // deformers, in the same space as a cage's vertices.
346
+ export function buildPartGeometry(part) {
347
+ let geometry = baseGeometry(part);
348
+ if (geometry && hasSculpt(part)) {
349
+ geometry = sculptGeometry(geometry, part);
350
+ }
351
+ if (geometry && hasDeformers(part)) {
352
+ deform(geometry, part);
353
+ }
354
+ return geometry;
355
+ }
356
+
357
+ function baseGeometry(part) {
358
+ switch (part.kind) {
359
+ case 'mesh':
360
+ return cageGeometry(typeof part.obj === 'string' ? objCage(part.obj, part.unit) : part, part);
361
+ case 'loft':
362
+ return cageGeometry(loftCage(part), part);
363
+ case 'lathe':
364
+ return latheGeometry(part);
365
+ case 'extrude':
366
+ return extrudeGeometry(part);
367
+ case 'tube':
368
+ return tubeGeometry(part);
369
+ case 'sweep':
370
+ return sweepGeometry(part);
371
+ case 'surface':
372
+ return surfaceGeometry(part);
373
+ case 'branches':
374
+ return branchesGeometry(part);
375
+ case 'blob':
376
+ return blobGeometry(part);
377
+ case 'frame':
378
+ return frameGeometry(part);
379
+ case 'torus':
380
+ return torusGeometry(part);
381
+ case 'cone':
382
+ return cylinderGeometry(part, 0);
383
+ case 'cylinder':
384
+ return cylinderGeometry(part, Number.isFinite(part.radiusTop) ? part.radiusTop : part.radius);
385
+ case 'box':
386
+ return part.round > 0
387
+ ? roundedBoxGeometry(part)
388
+ : new THREE.BoxGeometry(...part.size, 4, 12, 4);
389
+ case 'sphere':
390
+ return part.noise > 0
391
+ ? lumpySphereGeometry(part)
392
+ : sphereGeometry(part);
393
+ case 'capsule':
394
+ return new THREE.CapsuleGeometry(
395
+ part.radius,
396
+ Math.max(0, part.height - 2 * part.radius),
397
+ Math.max(2, Math.round(circleSides(part, part.radius, 16, 8) / 4)),
398
+ circleSides(part, part.radius, clampInt(part.segments, 3, 64, 16), 8)
399
+ );
400
+ default:
401
+ return null;
402
+ }
403
+ }
404
+
405
+ //
406
+ // Deformers
407
+ //
408
+
409
+ // Deformers bend the finished shape along its own height, the y axis from its
410
+ // lowest point (t = 0) to its highest (t = 1), in this order:
411
+ //
412
+ // taper width at the top as a fraction of the bottom: a number, or [x, z]
413
+ // twist degrees turned about y at the top
414
+ // fold degrees each side of x = 0 lifts toward +z, a crease down the middle
415
+ // bend degrees the top is bent toward +z, along an even curve
416
+ function deform(geometry, part) {
417
+ geometry.computeBoundingBox();
418
+ const low = geometry.boundingBox.min.y;
419
+ const span = Math.max(1e-6, geometry.boundingBox.max.y - low);
420
+ const [taperX, taperZ] = Array.isArray(part.taper)
421
+ ? part.taper
422
+ : [part.taper ?? 1, part.taper ?? 1];
423
+ const twist = (part.twist ?? 0) * DEG;
424
+ const fold = Math.tan(Math.max(-80, Math.min(80, part.fold ?? 0)) * DEG);
425
+ const bend = (part.bend ?? 0) * DEG;
426
+ const position = geometry.attributes.position;
427
+ for (let i = 0; i < position.count; i++) {
428
+ let x = position.getX(i);
429
+ let y = position.getY(i);
430
+ let z = position.getZ(i);
431
+ const t = (y - low) / span;
432
+ x *= 1 + ((Number.isFinite(taperX) ? taperX : 1) - 1) * t;
433
+ z *= 1 + ((Number.isFinite(taperZ) ? taperZ : 1) - 1) * t;
434
+ if (twist) {
435
+ const angle = twist * t;
436
+ [x, z] = [
437
+ x * Math.cos(angle) - z * Math.sin(angle),
438
+ x * Math.sin(angle) + z * Math.cos(angle),
439
+ ];
440
+ }
441
+ z += Math.abs(x) * fold;
442
+
443
+ // Bend: the height becomes an arc of the same length around a center
444
+ // `span / bend` in front of the bottom.
445
+ if (Math.abs(bend) > 1e-4) {
446
+ const radius = span / bend;
447
+ const angle = t * bend;
448
+ const height = radius - z;
449
+ y = low + height * Math.sin(angle);
450
+ z = radius - height * Math.cos(angle);
451
+ }
452
+ position.setXYZ(i, x, y, z);
453
+ }
454
+ geometry.computeVertexNormals();
455
+ geometry.computeBoundingBox();
456
+ }
457
+
458
+ // Lathe profiles and tubes can be seen from inside (an open profile, the
459
+ // inside of a cup), so those draw both sides. So does a surface with no
460
+ // thickness, which is a single sheet.
461
+ export function partIsDoubleSided(part) {
462
+ return (
463
+ part.kind === 'lathe' ||
464
+ part.kind === 'extrude' ||
465
+ (part.kind === 'surface' && !(part.thickness > 0))
466
+ );
467
+ }