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,480 @@
1
+ // Geometry for two `.pxmodel` kinds: `sweep`, a flat outline carried along a
2
+ // 3d path, and `surface`, a smooth patch through a grid of control points. See
3
+ // docs/pxmodel-format.md for the fields.
4
+ //
5
+ // Both return a THREE.BufferGeometry, or null when the part's fields cannot
6
+ // make one.
7
+
8
+ import * as THREE from 'three';
9
+ import { partPath } from './paths';
10
+ import { circleSides, lengthSteps } from './optimize';
11
+
12
+ const DEG = Math.PI / 180;
13
+
14
+ function clampInt(value, min, max, fallback) {
15
+ if (!Number.isFinite(value)) {
16
+ return fallback;
17
+ }
18
+ return Math.max(min, Math.min(max, Math.round(value)));
19
+ }
20
+
21
+ function points2(list) {
22
+ if (!Array.isArray(list)) {
23
+ return [];
24
+ }
25
+ return list
26
+ .filter((p) => Array.isArray(p) && Number.isFinite(p[0]) && Number.isFinite(p[1]))
27
+ .map((p) => new THREE.Vector2(p[0], p[1]));
28
+ }
29
+
30
+ function points3(list) {
31
+ if (!Array.isArray(list)) {
32
+ return [];
33
+ }
34
+ return list
35
+ .filter((p) => Array.isArray(p) && p.slice(0, 3).every(Number.isFinite))
36
+ .map((p) => new THREE.Vector3(p[0], p[1], p[2]));
37
+ }
38
+
39
+ //
40
+ // Sweep
41
+ //
42
+
43
+ // A station is one place along the path where the profile is drawn: its
44
+ // `center`, `tangent`, the `up` direction the profile's y points along, and
45
+ // `along`, the fraction of the path's length reached there. At a corner of a
46
+ // straight path, `bend` is the direction the path turns and `miter` how far the
47
+ // profile stretches that way so the two sides meet.
48
+
49
+ // Rotation-minimizing frames by double reflection (Wang et al. 2008): each
50
+ // station's up is the previous one reflected twice, so it turns only as much
51
+ // as the path does and never flips.
52
+ function transportUp(stations, up) {
53
+ stations[0].up = up;
54
+ for (let i = 1; i < stations.length; i++) {
55
+ stations[i].up = reflectUp(stations[i - 1], stations[i]);
56
+ }
57
+ }
58
+
59
+ function reflectUp(previous, station) {
60
+ const step = station.center.clone().sub(previous.center);
61
+ const c1 = step.dot(step);
62
+ if (c1 < 1e-12) {
63
+ return previous.up.clone();
64
+ }
65
+ const upL = previous.up.clone().addScaledVector(step, (-2 / c1) * step.dot(previous.up));
66
+ const tangentL = previous.tangent
67
+ .clone()
68
+ .addScaledVector(step, (-2 / c1) * step.dot(previous.tangent));
69
+ const second = station.tangent.clone().sub(tangentL);
70
+ const c2 = second.dot(second);
71
+ const up = c2 < 1e-12 ? upL : upL.addScaledVector(second, (-2 / c2) * second.dot(upL));
72
+ return up.addScaledVector(station.tangent, -up.dot(station.tangent)).normalize();
73
+ }
74
+
75
+ // On a closed path the up direction may come back turned; the difference is
76
+ // spread evenly along the path so the seam matches.
77
+ function closeUp(stations) {
78
+ const first = stations[0];
79
+ const returned = reflectUp(stations[stations.length - 1], first);
80
+ const angle = Math.atan2(
81
+ returned.clone().cross(first.up).dot(first.tangent),
82
+ returned.dot(first.up)
83
+ );
84
+ const count = stations.length;
85
+ stations.forEach((station, i) => {
86
+ station.up.applyAxisAngle(station.tangent, (angle * i) / count);
87
+ });
88
+ }
89
+
90
+ // The profile's y at the start of the path: `up` if given, else +y, or +z
91
+ // when the path starts along y.
92
+ function startUp(part, tangent) {
93
+ const given = points3([part.up])[0];
94
+ let up = given && given.lengthSq() > 1e-12 ? given : new THREE.Vector3(0, 1, 0);
95
+ if (Math.abs(up.clone().normalize().dot(tangent)) > 0.99) {
96
+ up = Math.abs(tangent.y) > 0.9 ? new THREE.Vector3(0, 0, 1) : new THREE.Vector3(0, 1, 0);
97
+ }
98
+ return up.clone().addScaledVector(tangent, -up.dot(tangent)).normalize();
99
+ }
100
+
101
+ // Stations along a Catmull-Rom curve through the path, evenly spaced by length.
102
+ function curveStations(path, closed, part) {
103
+ const curve = new THREE.CatmullRomCurve3(path, closed, 'centripetal');
104
+ const most = part.helix ? 1024 : 256;
105
+ const written = clampInt(part.segments, 2, most, Math.max(16, path.length * 12));
106
+ const count = lengthSteps(part, curve.getLength(), written, Math.min(written, path.length));
107
+ const last = closed ? count - 1 : count;
108
+ const stations = [];
109
+ for (let i = 0; i <= last; i++) {
110
+ const along = i / count;
111
+ stations.push({
112
+ center: curve.getPointAt(along),
113
+ tangent: curve.getTangentAt(along).normalize(),
114
+ along,
115
+ split: false,
116
+ });
117
+ }
118
+ return stations;
119
+ }
120
+
121
+ // Stations at the points of a straight path. Each inner corner is split in
122
+ // two so the faces on either side shade flat.
123
+ function straightStations(path, closed) {
124
+ const count = path.length;
125
+ const segment = (i) => path[(i + 1) % count].clone().sub(path[i]);
126
+ const segmentCount = closed ? count : count - 1;
127
+ const lengths = Array.from({ length: segmentCount }, (_, i) => segment(i).length());
128
+ const total = lengths.reduce((sum, length) => sum + length, 0) || 1;
129
+ const stations = [];
130
+ let reached = 0;
131
+ for (let i = 0; i < count; i++) {
132
+ const into = i > 0 || closed ? segment((i - 1 + count) % count).normalize() : null;
133
+ const out = i < segmentCount ? segment(i).normalize() : null;
134
+ stations.push({ center: path[i].clone(), along: reached / total, ...corner(into, out) });
135
+ reached += lengths[i] ?? 0;
136
+ }
137
+ return stations;
138
+ }
139
+
140
+ // The tangent at a point between two segments is halfway between them. The
141
+ // profile there is stretched by 1 / cos of half the turn along the direction
142
+ // the path turns.
143
+ function corner(into, out) {
144
+ if (!into || !out) {
145
+ return { tangent: (into ?? out).clone(), split: false };
146
+ }
147
+ const tangent = into.clone().add(out);
148
+ if (tangent.lengthSq() < 1e-12) {
149
+ return { tangent: out.clone(), split: false };
150
+ }
151
+ tangent.normalize();
152
+ const bend = out.clone().sub(into);
153
+ if (bend.lengthSq() < 1e-12) {
154
+ return { tangent, split: false };
155
+ }
156
+ return {
157
+ tangent,
158
+ bend: bend.normalize(),
159
+ miter: 1 / Math.max(0.25, tangent.dot(out)),
160
+ split: true,
161
+ };
162
+ }
163
+
164
+ function uniquePath(path) {
165
+ return path.filter((point, i) => i === 0 || point.distanceTo(path[i - 1]) > 1e-6);
166
+ }
167
+
168
+ // The profile as columns around one ring and the edges between them. A sharp
169
+ // profile gives each edge its own two columns, so every corner is a crease; a
170
+ // `smooth` profile is a curve through the points, shared all the way round.
171
+ function profileRing(part) {
172
+ let outline = points2(part.profile);
173
+ if (outline.length < 3) {
174
+ return null;
175
+ }
176
+ if (THREE.ShapeUtils.isClockWise(outline)) {
177
+ outline = outline.reverse();
178
+ }
179
+ if (part.smooth) {
180
+ const curve = new THREE.SplineCurve([...outline, outline[0]]);
181
+ const widest = Math.max(...outline.map((point) => point.length()));
182
+ const sides = circleSides(part, widest, Math.max(24, outline.length * 6), outline.length);
183
+ outline = curve.getSpacedPoints(sides).slice(0, -1);
184
+ const edges = outline.map((_, i) => [i, (i + 1) % outline.length]);
185
+ return { outline, columns: outline, edges };
186
+ }
187
+ const columns = [];
188
+ const edges = [];
189
+ outline.forEach((point, i) => {
190
+ columns.push(point, outline[(i + 1) % outline.length]);
191
+ edges.push([2 * i, 2 * i + 1]);
192
+ });
193
+ return { outline, columns, edges };
194
+ }
195
+
196
+ // The profile point (x, y) placed at a station: scaled, turned, set in the
197
+ // plane across the path, and stretched along the corner's bend.
198
+ function placePoint(point, station, frame) {
199
+ const x = (point.x * frame.cos - point.y * frame.sin) * frame.scale;
200
+ const y = (point.x * frame.sin + point.y * frame.cos) * frame.scale;
201
+ const offset = frame.side.clone().multiplyScalar(x).addScaledVector(station.up, y);
202
+ if (station.bend && station.miter) {
203
+ offset.addScaledVector(station.bend, offset.dot(station.bend) * (station.miter - 1));
204
+ }
205
+ return offset.add(station.center);
206
+ }
207
+
208
+ function stationFrame(station, part) {
209
+ const scaleEnd = Number.isFinite(part.scaleEnd) ? part.scaleEnd : 1;
210
+ const turn = (Number.isFinite(part.turn) ? part.turn : 0) * DEG * station.along;
211
+ return {
212
+ side: station.up.clone().cross(station.tangent),
213
+ scale: 1 + (scaleEnd - 1) * station.along,
214
+ cos: Math.cos(turn),
215
+ sin: Math.sin(turn),
216
+ };
217
+ }
218
+
219
+ // `profile` is a closed [x, y] outline, carried along a smooth curve through
220
+ // `path` (or straight segments with `straight`). The profile's x points to the
221
+ // side of the path and its y along `up`. `scaleEnd` scales the profile at the
222
+ // end of the path, `turn` turns it by that many degrees from start to end.
223
+ // An open path has its ends capped.
224
+ export function sweepGeometry(part) {
225
+ const ring = profileRing(part);
226
+ const { points, closed: loop } = partPath(part);
227
+ const path = uniquePath(points);
228
+ if (!ring || path.length < 2) {
229
+ return null;
230
+ }
231
+ const closed = loop && path.length >= 3;
232
+ const stations = part.straight
233
+ ? straightStations(path, closed)
234
+ : curveStations(path, closed, part);
235
+ transportUp(stations, startUp(part, stations[0].tangent));
236
+ if (closed) {
237
+ closeUp(stations);
238
+ }
239
+
240
+ const positions = [];
241
+ const uvs = [];
242
+ const indices = [];
243
+ const columnCount = ring.columns.length;
244
+
245
+ // Rings of vertices; a split station has two, joined to the rings before
246
+ // and after it but not to each other.
247
+ const rings = [];
248
+ for (const station of stations) {
249
+ const frame = stationFrame(station, part);
250
+ const copies = station.split ? 2 : 1;
251
+ for (let copy = 0; copy < copies; copy++) {
252
+ rings.push({ start: positions.length / 3, joinPrevious: copy === 0 });
253
+ ring.columns.forEach((point, column) => {
254
+ const placed = placePoint(point, station, frame);
255
+ positions.push(placed.x, placed.y, placed.z);
256
+ uvs.push(column / columnCount, station.along);
257
+ });
258
+ }
259
+ }
260
+ const join = (from, to) => {
261
+ for (const [a, b] of ring.edges) {
262
+ const [a0, b0, a1, b1] = [from.start + a, from.start + b, to.start + a, to.start + b];
263
+ indices.push(a0, b0, a1, b0, b1, a1);
264
+ }
265
+ };
266
+ for (let i = 1; i < rings.length; i++) {
267
+ if (rings[i].joinPrevious) {
268
+ join(rings[i - 1], rings[i]);
269
+ }
270
+ }
271
+ if (closed) {
272
+ join(rings[rings.length - 1], rings[0]);
273
+ } else {
274
+ addCaps(ring, stations, part, { positions, uvs, indices });
275
+ }
276
+ const geometry = new THREE.BufferGeometry();
277
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
278
+ geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));
279
+ geometry.setIndex(indices);
280
+ geometry.computeVertexNormals();
281
+ return geometry;
282
+ }
283
+
284
+ // Caps: the outline filled in at each end, with its own vertices so the rim is
285
+ // a crease. The start cap faces back along the path.
286
+ function addCaps(ring, stations, part, buffers) {
287
+ const { positions, uvs, indices } = buffers;
288
+ const triangles = THREE.ShapeUtils.triangulateShape(ring.outline, []);
289
+ const ends = [
290
+ [stations[0], true],
291
+ [stations[stations.length - 1], false],
292
+ ];
293
+ for (const [station, flip] of ends) {
294
+ const frame = stationFrame(station, part);
295
+ const start = positions.length / 3;
296
+ for (const point of ring.outline) {
297
+ const placed = placePoint(point, station, frame);
298
+ positions.push(placed.x, placed.y, placed.z);
299
+ uvs.push(point.x, point.y);
300
+ }
301
+ for (const [a, b, c] of triangles) {
302
+ indices.push(
303
+ ...(flip ? [start + a, start + c, start + b] : [start + a, start + b, start + c])
304
+ );
305
+ }
306
+ }
307
+ }
308
+
309
+ //
310
+ // Surface
311
+ //
312
+
313
+ // The B-spline weights of `count` control points at t in [0, 1], for a curve
314
+ // of the given degree. The knots are clamped: the first and last are repeated
315
+ // degree + 1 times, so the curve starts and ends on the end points. See "The
316
+ // NURBS Book", algorithm A2.2.
317
+ function splineWeights(count, degree, t) {
318
+ const inner = count - degree;
319
+ const knots = [];
320
+ for (let i = 0; i <= count + degree; i++) {
321
+ knots.push(Math.max(0, Math.min(inner, i - degree)) / inner);
322
+ }
323
+ const span = Math.min(count - 1, degree + Math.floor(t * inner));
324
+ const basis = [1];
325
+ const left = [];
326
+ const right = [];
327
+ for (let j = 1; j <= degree; j++) {
328
+ left[j] = t - knots[span + 1 - j];
329
+ right[j] = knots[span + j] - t;
330
+ let saved = 0;
331
+ for (let r = 0; r < j; r++) {
332
+ const term = basis[r] / (right[r + 1] + left[j - r]);
333
+ basis[r] = saved + right[r + 1] * term;
334
+ saved = left[j - r] * term;
335
+ }
336
+ basis[j] = saved;
337
+ }
338
+ const weights = new Array(count).fill(0);
339
+ basis.forEach((value, r) => {
340
+ weights[span - degree + r] = value;
341
+ });
342
+ return weights;
343
+ }
344
+
345
+ function controlGrid(part) {
346
+ if (!Array.isArray(part.points)) {
347
+ return null;
348
+ }
349
+ const rows = part.points.map(points3).filter((row) => row.length >= 2);
350
+ if (rows.length < 2) {
351
+ return null;
352
+ }
353
+ const width = Math.min(...rows.map((row) => row.length));
354
+ return rows.map((row) => row.slice(0, width));
355
+ }
356
+
357
+ // The surface sampled on a (samples + 1) x (samples + 1) grid, row by row.
358
+ function sampleSurface(grid, samples) {
359
+ const rowCount = grid.length;
360
+ const columnCount = grid[0].length;
361
+ const rowDegree = Math.min(3, rowCount - 1);
362
+ const columnDegree = Math.min(3, columnCount - 1);
363
+ const columnWeights = [];
364
+ for (let b = 0; b <= samples; b++) {
365
+ columnWeights.push(splineWeights(columnCount, columnDegree, b / samples));
366
+ }
367
+ const points = [];
368
+ for (let a = 0; a <= samples; a++) {
369
+ const rowWeights = splineWeights(rowCount, rowDegree, a / samples);
370
+ for (let b = 0; b <= samples; b++) {
371
+ const point = new THREE.Vector3();
372
+ for (let i = 0; i < rowCount; i++) {
373
+ if (rowWeights[i] === 0) {
374
+ continue;
375
+ }
376
+ for (let j = 0; j < columnCount; j++) {
377
+ point.addScaledVector(grid[i][j], rowWeights[i] * columnWeights[b][j]);
378
+ }
379
+ }
380
+ points.push(point);
381
+ }
382
+ }
383
+ return points;
384
+ }
385
+
386
+ function sheetIndices(samples, start, flip) {
387
+ const indices = [];
388
+ const at = (a, b) => start + a * (samples + 1) + b;
389
+ for (let a = 0; a < samples; a++) {
390
+ for (let b = 0; b < samples; b++) {
391
+ const [p00, p10, p01, p11] = [at(a, b), at(a + 1, b), at(a, b + 1), at(a + 1, b + 1)];
392
+ indices.push(...(flip ? [p00, p01, p10, p10, p01, p11] : [p00, p10, p01, p10, p11, p01]));
393
+ }
394
+ }
395
+ return indices;
396
+ }
397
+
398
+ // The border of the sample grid, walked so the sheet is on its left seen from
399
+ // the front.
400
+ function borderLoop(samples) {
401
+ const at = (a, b) => a * (samples + 1) + b;
402
+ const loop = [];
403
+ for (let a = 0; a < samples; a++) {
404
+ loop.push(at(a, 0));
405
+ }
406
+ for (let b = 0; b < samples; b++) {
407
+ loop.push(at(samples, b));
408
+ }
409
+ for (let a = samples; a > 0; a--) {
410
+ loop.push(at(a, samples));
411
+ }
412
+ for (let b = samples; b > 0; b--) {
413
+ loop.push(at(0, b));
414
+ }
415
+ return loop;
416
+ }
417
+
418
+ // `points` is a grid of control points, rows of [x, y, z]. The surface is a
419
+ // smooth patch that starts and ends on the corner points and is pulled toward
420
+ // the others. With `thickness` 0 it is a single sheet; above 0 it is a closed
421
+ // shell, half the thickness to each side of the patch.
422
+ export function surfaceGeometry(part) {
423
+ const grid = controlGrid(part);
424
+ if (!grid) {
425
+ return null;
426
+ }
427
+ const samples = clampInt(part.resolution, 2, 64, 16);
428
+ const points = sampleSurface(grid, samples);
429
+ const uvs = [];
430
+ for (let a = 0; a <= samples; a++) {
431
+ for (let b = 0; b <= samples; b++) {
432
+ uvs.push(b / samples, a / samples);
433
+ }
434
+ }
435
+ const sheet = new THREE.BufferGeometry().setFromPoints(points);
436
+ sheet.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));
437
+ sheet.setIndex(sheetIndices(samples, 0, false));
438
+ sheet.computeVertexNormals();
439
+ const thickness = Number.isFinite(part.thickness) ? part.thickness : 0;
440
+ if (thickness <= 0) {
441
+ return sheet;
442
+ }
443
+ return shellGeometry(points, sheet.attributes.normal, uvs, samples, thickness / 2);
444
+ }
445
+
446
+ // Front and back sheets moved out along the normals, and a rim joining their
447
+ // borders. The rim has its own vertices, so its edges are creases.
448
+ function shellGeometry(points, normals, uvs, samples, half) {
449
+ const normal = new THREE.Vector3();
450
+ const front = [];
451
+ const back = [];
452
+ points.forEach((point, i) => {
453
+ normal.fromBufferAttribute(normals, i);
454
+ front.push(point.clone().addScaledVector(normal, half));
455
+ back.push(point.clone().addScaledVector(normal, -half));
456
+ });
457
+ const count = points.length;
458
+ const positions = [...front, ...back];
459
+ const allUvs = [...uvs, ...uvs];
460
+ const indices = [...sheetIndices(samples, 0, false), ...sheetIndices(samples, count, true)];
461
+
462
+ // Rim: two vertices per border point, front then back.
463
+ const loop = borderLoop(samples);
464
+ const rimStart = positions.length;
465
+ loop.forEach((index, i) => {
466
+ positions.push(front[index], back[index]);
467
+ allUvs.push(i / loop.length, 0, i / loop.length, 1);
468
+ });
469
+ loop.forEach((_, i) => {
470
+ const next = (i + 1) % loop.length;
471
+ const [aFront, aBack] = [rimStart + 2 * i, rimStart + 2 * i + 1];
472
+ const [bFront, bBack] = [rimStart + 2 * next, rimStart + 2 * next + 1];
473
+ indices.push(aFront, aBack, bFront, bFront, aBack, bBack);
474
+ });
475
+ const geometry = new THREE.BufferGeometry().setFromPoints(positions);
476
+ geometry.setAttribute('uv', new THREE.Float32BufferAttribute(allUvs, 2));
477
+ geometry.setIndex(indices);
478
+ geometry.computeVertexNormals();
479
+ return geometry;
480
+ }
@@ -7,7 +7,8 @@
7
7
  // template's node signature; one shared WebGL renderer serves all thumbs.
8
8
  import * as THREE from 'three';
9
9
  import { makeShapeMesh, nodeSignature, disposeNode } from './materials';
10
- import { parseModelCached, buildModelNode, modelArtSignature } from './pxmodel';
10
+ import { parseModelCached, buildModelNode, modelArtSignature, visibleBounds } from './pxmodel';
11
+ import { modelRefSignature } from './modelrefs';
11
12
  import { findBehaviorClass } from '@imports/castle.physics-2d/editors/behaviorRegistry';
12
13
 
13
14
  export const THUMB_PREFIX = '@thumb/';
@@ -65,11 +66,11 @@ function renderTemplate(template, sprites, files) {
65
66
  return { ...defaults, ...(components[name] ?? {}) };
66
67
  };
67
68
  const model = components.Model?.file ? parseModelCached(files?.[components.Model.file]) : null;
68
- if (model) node.add(buildModelNode(model, sprites));
69
+ if (model) node.add(buildModelNode(model, sprites, files, components.Model.file));
69
70
  else if (components.Shape) node.add(makeShapeMesh(merged('Shape'), sprites));
70
71
  scene.add(node);
71
72
 
72
- const bounds = new THREE.Box3().setFromObject(node);
73
+ const bounds = visibleBounds(node);
73
74
  const center = bounds.getCenter(new THREE.Vector3());
74
75
  const radius = Math.max(0.2, bounds.getSize(new THREE.Vector3()).length() / 2);
75
76
  const camera = new THREE.PerspectiveCamera(30, 1, 0.01, 100);
@@ -99,8 +100,9 @@ const cache = new Map(); // blueprint path -> { sig, art }
99
100
  export function thumbArtFor(path, template, sprites, files) {
100
101
  const modelFile = template.components?.Model?.file;
101
102
  const modelText = modelFile ? (files?.[modelFile] ?? '') : '';
103
+ const model = modelText ? parseModelCached(modelText) : null;
102
104
  const modelSig = modelText
103
- ? `${modelText.length}:${modelArtSignature(parseModelCached(modelText), sprites)}:${modelText}`
105
+ ? `${modelText.length}:${modelArtSignature(model, sprites)}:${modelText}:${modelRefSignature(model, sprites, files, modelFile)}`
104
106
  : '';
105
107
  const sig = nodeSignature({ components: template.components ?? {} }, sprites, modelSig);
106
108
  const cached = cache.get(path);