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,496 @@
|
|
|
1
|
+
// Cages: a polygon mesh written by hand (or made by a loft) that is then
|
|
2
|
+
// mirrored and smoothed. A few dozen points make a smooth head, body or leaf.
|
|
3
|
+
//
|
|
4
|
+
// A cage is { vertices: [[x, y, z], ...], faces: [[i, j, k, ...], ...] }, faces
|
|
5
|
+
// counter-clockwise seen from outside, as in a `kind: "mesh"` part. The steps,
|
|
6
|
+
// in order: `from` starts a mesh from a box instead, `ops` extrude and move its
|
|
7
|
+
// faces (cageops.js), `crease` marks edges to keep sharp, `symmetric` mirrors
|
|
8
|
+
// the cage across x = 0 and welds the seam, `subdivide` smooths it with the
|
|
9
|
+
// Catmull-Clark rule that many times, and the result becomes an indexed
|
|
10
|
+
// geometry with shared, smoothly shaded vertices.
|
|
11
|
+
|
|
12
|
+
import * as THREE from 'three';
|
|
13
|
+
import { SEAM, applyOps, boxCage, creaseEdges, edgeKey } from './cageops';
|
|
14
|
+
import { subdivideSteps } from './optimize';
|
|
15
|
+
|
|
16
|
+
//
|
|
17
|
+
// Loft
|
|
18
|
+
//
|
|
19
|
+
|
|
20
|
+
// `sections` are rings of [x, z] points, each at its own `y` and shifted by
|
|
21
|
+
// `center: [x, z]`, bottom to top. Every ring has the same number of points,
|
|
22
|
+
// counter-clockwise seen from above. Neighbouring rings are joined by quads,
|
|
23
|
+
// and the bottom and top rings are closed with one face each.
|
|
24
|
+
export function loftCage(part) {
|
|
25
|
+
const sections = (Array.isArray(part.sections) ? part.sections : []).filter(
|
|
26
|
+
(section) =>
|
|
27
|
+
Array.isArray(section?.points) && section.points.length >= 3 && Number.isFinite(section.y)
|
|
28
|
+
);
|
|
29
|
+
if (sections.length < 2) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const count = Math.min(...sections.map((section) => section.points.length));
|
|
33
|
+
const vertices = [];
|
|
34
|
+
for (const section of sections) {
|
|
35
|
+
const [cx, cz] = Array.isArray(section.center) ? section.center : [0, 0];
|
|
36
|
+
for (let i = 0; i < count; i++) {
|
|
37
|
+
const [x, z] = section.points[i];
|
|
38
|
+
vertices.push([x + (cx ?? 0), section.y, z + (cz ?? 0)]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Side uvs: u runs once around the ring, v from the bottom ring to the top.
|
|
42
|
+
// The last column of quads ends at u = 1, so its corners differ from the
|
|
43
|
+
// first column's, and each face keeps its own uv per corner.
|
|
44
|
+
const bottom = sections[0].y;
|
|
45
|
+
const height = sections[sections.length - 1].y - bottom || 1;
|
|
46
|
+
const faces = [];
|
|
47
|
+
const faceUvs = [];
|
|
48
|
+
for (let ring = 0; ring + 1 < sections.length; ring++) {
|
|
49
|
+
const v0 = (sections[ring].y - bottom) / height;
|
|
50
|
+
const v1 = (sections[ring + 1].y - bottom) / height;
|
|
51
|
+
for (let i = 0; i < count; i++) {
|
|
52
|
+
const a = ring * count + i;
|
|
53
|
+
const b = ring * count + ((i + 1) % count);
|
|
54
|
+
const [u0, u1] = [i / count, (i + 1) / count];
|
|
55
|
+
faces.push([a, a + count, b + count, b]);
|
|
56
|
+
faceUvs.push(
|
|
57
|
+
new Map([
|
|
58
|
+
[a, [u0, v0]],
|
|
59
|
+
[a + count, [u0, v1]],
|
|
60
|
+
[b + count, [u1, v1]],
|
|
61
|
+
[b, [u1, v0]],
|
|
62
|
+
])
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const last = (sections.length - 1) * count;
|
|
67
|
+
faces.push(Array.from({ length: count }, (_, i) => i));
|
|
68
|
+
faces.push(Array.from({ length: count }, (_, i) => last + count - 1 - i));
|
|
69
|
+
faceUvs.push(capUvs(vertices, faces[faces.length - 2]), capUvs(vertices, faces[faces.length - 1]));
|
|
70
|
+
|
|
71
|
+
// Rings may wind either way; a closed cage whose faces point inward has a
|
|
72
|
+
// negative volume, and is turned inside out.
|
|
73
|
+
if (signedVolume(vertices, faces) < 0) {
|
|
74
|
+
faces.forEach((face) => face.reverse());
|
|
75
|
+
}
|
|
76
|
+
return { vertices, faces, faceUvs };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// An end cap's uvs are its points' x and z, fitted to 0..1 across the cap.
|
|
80
|
+
function capUvs(vertices, face) {
|
|
81
|
+
const xs = face.map((index) => vertices[index][0]);
|
|
82
|
+
const zs = face.map((index) => vertices[index][2]);
|
|
83
|
+
const [minX, minZ] = [Math.min(...xs), Math.min(...zs)];
|
|
84
|
+
const size = Math.max(Math.max(...xs) - minX, Math.max(...zs) - minZ) || 1;
|
|
85
|
+
return new Map(
|
|
86
|
+
face.map((index) => [
|
|
87
|
+
index,
|
|
88
|
+
[(vertices[index][0] - minX) / size, (vertices[index][2] - minZ) / size],
|
|
89
|
+
])
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function signedVolume(vertices, faces) {
|
|
94
|
+
let volume = 0;
|
|
95
|
+
for (const face of faces) {
|
|
96
|
+
for (let i = 1; i + 1 < face.length; i++) {
|
|
97
|
+
const [a, b, c] = [vertices[face[0]], vertices[face[i]], vertices[face[i + 1]]];
|
|
98
|
+
volume +=
|
|
99
|
+
a[0] * (b[1] * c[2] - b[2] * c[1]) -
|
|
100
|
+
a[1] * (b[0] * c[2] - b[2] * c[0]) +
|
|
101
|
+
a[2] * (b[0] * c[1] - b[1] * c[0]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return volume / 6;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//
|
|
108
|
+
// Symmetry
|
|
109
|
+
//
|
|
110
|
+
|
|
111
|
+
// Mirror across x = 0. Vertices on the seam are shared by both halves; the
|
|
112
|
+
// mirrored faces are reversed so they still face outward, and creases are
|
|
113
|
+
// mirrored with them.
|
|
114
|
+
export function mirrorCage(cage) {
|
|
115
|
+
const vertices = cage.vertices.map((v) => [...v]);
|
|
116
|
+
const mirrorIndex = cage.vertices.map((v, index) => {
|
|
117
|
+
if (Math.abs(v[0]) < SEAM) {
|
|
118
|
+
vertices[index][0] = 0;
|
|
119
|
+
return index;
|
|
120
|
+
}
|
|
121
|
+
vertices.push([-v[0], v[1], v[2]]);
|
|
122
|
+
return vertices.length - 1;
|
|
123
|
+
});
|
|
124
|
+
const faces = cage.faces.map((face) => [...face]);
|
|
125
|
+
for (const face of cage.faces) {
|
|
126
|
+
faces.push(face.map((index) => mirrorIndex[index]).reverse());
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const creases = new Map();
|
|
130
|
+
for (const [key, sharpness] of cage.creases ?? []) {
|
|
131
|
+
const [a, b] = key.split(',').map(Number);
|
|
132
|
+
creases.set(key, sharpness);
|
|
133
|
+
creases.set(edgeKey(mirrorIndex[a], mirrorIndex[b]), sharpness);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// A face lying on the seam would be inside the welded shape, and is dropped
|
|
137
|
+
// with its mirrored copy.
|
|
138
|
+
const onSeam = (face) => face.every((index) => mirrorIndex[index] === index);
|
|
139
|
+
return { vertices, faces: faces.filter((face) => !onSeam(face)), creases };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//
|
|
143
|
+
// Catmull-Clark subdivision
|
|
144
|
+
//
|
|
145
|
+
|
|
146
|
+
// One step. Every face gets a point at its middle, every edge a point near its
|
|
147
|
+
// middle, and every old vertex moves toward the average of its neighbours;
|
|
148
|
+
// each n-sided face becomes n quads. Edges with one face are a border: their
|
|
149
|
+
// points stay on the border, and a border vertex only follows the border.
|
|
150
|
+
//
|
|
151
|
+
// A creased edge is treated like a border for as many steps as its sharpness,
|
|
152
|
+
// and its two halves carry the sharpness less one to the next step. A
|
|
153
|
+
// sharpness between 0 and 1 blends the sharp and smooth results.
|
|
154
|
+
export function subdivideCage(cage) {
|
|
155
|
+
const { vertices, faces } = cage;
|
|
156
|
+
const creases = cage.creases ?? new Map();
|
|
157
|
+
const facePoints = faces.map((face) => average(face.map((index) => vertices[index])));
|
|
158
|
+
|
|
159
|
+
// Edges, keyed by their two vertex indices, with the faces on each side.
|
|
160
|
+
const edges = new Map();
|
|
161
|
+
faces.forEach((face, faceIndex) => {
|
|
162
|
+
face.forEach((a, i) => {
|
|
163
|
+
const b = face[(i + 1) % face.length];
|
|
164
|
+
const key = edgeKey(a, b);
|
|
165
|
+
if (!edges.has(key)) {
|
|
166
|
+
edges.set(key, { a, b, faces: [] });
|
|
167
|
+
}
|
|
168
|
+
edges.get(key).faces.push(faceIndex);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
for (const [key, edge] of edges) {
|
|
172
|
+
edge.sharpness = edge.faces.length < 2 ? Infinity : (creases.get(key) ?? 0);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Edge points: the average of the edge's ends and the two face points, or
|
|
176
|
+
// the plain middle on a border or a crease.
|
|
177
|
+
const out = vertices.map((v) => [...v]);
|
|
178
|
+
const edgePoint = new Map();
|
|
179
|
+
for (const [key, edge] of edges) {
|
|
180
|
+
const ends = [vertices[edge.a], vertices[edge.b]];
|
|
181
|
+
const middle = average(ends);
|
|
182
|
+
let point = middle;
|
|
183
|
+
if (edge.faces.length === 2 && edge.sharpness < 1) {
|
|
184
|
+
const smooth = average([...ends, facePoints[edge.faces[0]], facePoints[edge.faces[1]]]);
|
|
185
|
+
point = mix(smooth, middle, edge.sharpness);
|
|
186
|
+
}
|
|
187
|
+
out.push(point);
|
|
188
|
+
edgePoint.set(key, out.length - 1);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const touching = vertices.map(() => ({ faces: [], edges: [] }));
|
|
192
|
+
faces.forEach((face, faceIndex) =>
|
|
193
|
+
face.forEach((index) => touching[index].faces.push(faceIndex))
|
|
194
|
+
);
|
|
195
|
+
for (const edge of edges.values()) {
|
|
196
|
+
touching[edge.a].edges.push(edge);
|
|
197
|
+
touching[edge.b].edges.push(edge);
|
|
198
|
+
}
|
|
199
|
+
vertices.forEach((point, index) => {
|
|
200
|
+
out[index] = movedVertex(point, index, touching[index], vertices, facePoints);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// New faces: one quad per corner of each old face. Uvs, when the cage has
|
|
204
|
+
// them, are split the same way in straight lines.
|
|
205
|
+
const newFaces = [];
|
|
206
|
+
faces.forEach((face, faceIndex) => {
|
|
207
|
+
out.push(facePoints[faceIndex]);
|
|
208
|
+
const center = out.length - 1;
|
|
209
|
+
face.forEach((index, i) => {
|
|
210
|
+
const next = face[(i + 1) % face.length];
|
|
211
|
+
const previous = face[(i + face.length - 1) % face.length];
|
|
212
|
+
newFaces.push([
|
|
213
|
+
index,
|
|
214
|
+
edgePoint.get(edgeKey(index, next)),
|
|
215
|
+
center,
|
|
216
|
+
edgePoint.get(edgeKey(previous, index)),
|
|
217
|
+
]);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
const faceUvs = subdividedUvs(faces, cage.faceUvs, newFaces);
|
|
221
|
+
|
|
222
|
+
const newCreases = new Map();
|
|
223
|
+
for (const [key, edge] of edges) {
|
|
224
|
+
if (edge.faces.length === 2 && edge.sharpness > 1) {
|
|
225
|
+
const middle = edgePoint.get(key);
|
|
226
|
+
newCreases.set(edgeKey(edge.a, middle), edge.sharpness - 1);
|
|
227
|
+
newCreases.set(edgeKey(middle, edge.b), edge.sharpness - 1);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { vertices: out, faces: newFaces, creases: newCreases, faceUvs };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// The uvs of the new faces, which come one per corner of each old face, in
|
|
234
|
+
// order. A new face's corners get the old corner's uv, the middles of its two
|
|
235
|
+
// edges, and the face middle's.
|
|
236
|
+
function subdividedUvs(faces, faceUvs, newFaces) {
|
|
237
|
+
if (!faceUvs) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
const out = [];
|
|
241
|
+
faces.forEach((face, faceIndex) => {
|
|
242
|
+
const uvs = faceUvs[faceIndex];
|
|
243
|
+
const centerUv = average(face.map((index) => uvs.get(index))).slice(0, 2);
|
|
244
|
+
face.forEach((index, i) => {
|
|
245
|
+
const corner = uvs.get(index);
|
|
246
|
+
const next = uvs.get(face[(i + 1) % face.length]);
|
|
247
|
+
const previous = uvs.get(face[(i + face.length - 1) % face.length]);
|
|
248
|
+
const quad = newFaces[out.length];
|
|
249
|
+
out.push(
|
|
250
|
+
new Map([
|
|
251
|
+
[quad[0], corner],
|
|
252
|
+
[quad[1], mix(corner, next, 0.5)],
|
|
253
|
+
[quad[2], centerUv],
|
|
254
|
+
[quad[3], mix(previous, corner, 0.5)],
|
|
255
|
+
])
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
return out;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Where an old vertex moves. Inside: (F + 2R + (n - 3)P) / n, with F the
|
|
263
|
+
// average of the touching face points, R of the touching edge middles, n the
|
|
264
|
+
// edge count. On two sharp edges it follows them like a border; on more, or at
|
|
265
|
+
// the end of a border, it is a corner and stays put. A vertex left without
|
|
266
|
+
// faces, such as one only a dropped seam face used, stays put too.
|
|
267
|
+
function movedVertex(point, index, touching, vertices, facePoints) {
|
|
268
|
+
if (touching.edges.length === 0) {
|
|
269
|
+
return point;
|
|
270
|
+
}
|
|
271
|
+
const sharp = touching.edges.filter((edge) => edge.sharpness > 0);
|
|
272
|
+
const onBorder = sharp.some((edge) => edge.sharpness === Infinity);
|
|
273
|
+
let sharpPoint = null;
|
|
274
|
+
if (sharp.length === 2) {
|
|
275
|
+
const [left, right] = sharp.map((edge) => vertices[edge.a === index ? edge.b : edge.a]);
|
|
276
|
+
sharpPoint = point.map((value, axis) => 0.75 * value + 0.125 * (left[axis] + right[axis]));
|
|
277
|
+
} else if (sharp.length > 2 || onBorder) {
|
|
278
|
+
sharpPoint = point;
|
|
279
|
+
}
|
|
280
|
+
if (sharpPoint && onBorder) {
|
|
281
|
+
return sharpPoint;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const n = touching.edges.length;
|
|
285
|
+
const F = average(touching.faces.map((faceIndex) => facePoints[faceIndex]));
|
|
286
|
+
const R = average(touching.edges.map((edge) => average([vertices[edge.a], vertices[edge.b]])));
|
|
287
|
+
const smooth = point.map((value, axis) => (F[axis] + 2 * R[axis] + (n - 3) * value) / n);
|
|
288
|
+
if (!sharpPoint) {
|
|
289
|
+
return smooth;
|
|
290
|
+
}
|
|
291
|
+
const sharpness = sharp.reduce((sum, edge) => sum + edge.sharpness, 0) / sharp.length;
|
|
292
|
+
return mix(smooth, sharpPoint, Math.min(1, sharpness));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function mix(a, b, t) {
|
|
296
|
+
return a.map((value, axis) => value + (b[axis] - value) * t);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function average(points) {
|
|
300
|
+
const sum = [0, 0, 0];
|
|
301
|
+
for (const point of points) {
|
|
302
|
+
sum[0] += point[0];
|
|
303
|
+
sum[1] += point[1];
|
|
304
|
+
sum[2] += point[2] ?? 0;
|
|
305
|
+
}
|
|
306
|
+
return sum.map((value) => value / Math.max(1, points.length));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
//
|
|
310
|
+
// Orientation
|
|
311
|
+
//
|
|
312
|
+
|
|
313
|
+
// Makes every face of the cage wind the same way as its neighbours, then turns
|
|
314
|
+
// each closed piece so it faces outward. A hand-written cage often has a few
|
|
315
|
+
// faces wound backward, which shade dark or vanish.
|
|
316
|
+
//
|
|
317
|
+
// Neighbouring faces agree when their shared edge runs opposite ways in the
|
|
318
|
+
// two faces. Starting from one face, each neighbour that disagrees is reversed,
|
|
319
|
+
// spreading across the piece. A closed piece with a negative volume is inside
|
|
320
|
+
// out and is reversed whole. Returns true when some piece is open (has an edge
|
|
321
|
+
// with one face), which has no inside and outside to go by.
|
|
322
|
+
export function orientCage(cage) {
|
|
323
|
+
const { faces, vertices } = cage;
|
|
324
|
+
const byEdge = new Map();
|
|
325
|
+
faces.forEach((face, faceIndex) => {
|
|
326
|
+
face.forEach((a, i) => {
|
|
327
|
+
const b = face[(i + 1) % face.length];
|
|
328
|
+
const key = a < b ? `${a},${b}` : `${b},${a}`;
|
|
329
|
+
if (!byEdge.has(key)) {
|
|
330
|
+
byEdge.set(key, []);
|
|
331
|
+
}
|
|
332
|
+
byEdge.get(key).push(faceIndex);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
const piece = new Array(faces.length).fill(-1);
|
|
336
|
+
let open = false;
|
|
337
|
+
let pieces = 0;
|
|
338
|
+
for (let start = 0; start < faces.length; start++) {
|
|
339
|
+
if (piece[start] !== -1) {
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
const members = [start];
|
|
343
|
+
piece[start] = pieces;
|
|
344
|
+
let closed = true;
|
|
345
|
+
for (let at = 0; at < members.length; at++) {
|
|
346
|
+
const face = faces[members[at]];
|
|
347
|
+
face.forEach((a, i) => {
|
|
348
|
+
const b = face[(i + 1) % face.length];
|
|
349
|
+
const key = a < b ? `${a},${b}` : `${b},${a}`;
|
|
350
|
+
const uses = byEdge.get(key);
|
|
351
|
+
if (uses.length === 1) {
|
|
352
|
+
closed = false;
|
|
353
|
+
}
|
|
354
|
+
if (uses.length !== 2) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const other = uses.find((index) => index !== members[at]);
|
|
358
|
+
if (other === undefined || piece[other] !== -1) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// The neighbour must run this edge the other way; if not, reverse it.
|
|
363
|
+
const otherFace = faces[other];
|
|
364
|
+
const position = otherFace.indexOf(a);
|
|
365
|
+
if (otherFace[(position + 1) % otherFace.length] === b) {
|
|
366
|
+
otherFace.reverse();
|
|
367
|
+
}
|
|
368
|
+
piece[other] = piece[members[at]];
|
|
369
|
+
members.push(other);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
if (
|
|
373
|
+
closed &&
|
|
374
|
+
signedVolume(
|
|
375
|
+
vertices,
|
|
376
|
+
members.map((index) => faces[index])
|
|
377
|
+
) < 0
|
|
378
|
+
) {
|
|
379
|
+
for (const index of members) {
|
|
380
|
+
faces[index].reverse();
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
open ||= !closed;
|
|
384
|
+
pieces++;
|
|
385
|
+
}
|
|
386
|
+
return open;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
//
|
|
390
|
+
// Cage to geometry
|
|
391
|
+
//
|
|
392
|
+
|
|
393
|
+
function validCage(cage) {
|
|
394
|
+
if (!cage || !Array.isArray(cage.vertices) || !Array.isArray(cage.faces)) {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
// A malformed vertex becomes the origin rather than being dropped, so the
|
|
398
|
+
// face indices still point where they were written to.
|
|
399
|
+
const vertices = cage.vertices.map((v) =>
|
|
400
|
+
Array.isArray(v) ? [0, 1, 2].map((axis) => (Number.isFinite(v[axis]) ? v[axis] : 0)) : [0, 0, 0]
|
|
401
|
+
);
|
|
402
|
+
const faces = cage.faces.filter(
|
|
403
|
+
(face) =>
|
|
404
|
+
Array.isArray(face) &&
|
|
405
|
+
face.length >= 3 &&
|
|
406
|
+
face.every((index) => Number.isInteger(index) && index >= 0 && index < vertices.length)
|
|
407
|
+
);
|
|
408
|
+
return faces.length ? { vertices, faces } : null;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// The cage after `from`, `ops`, `crease`, `symmetric` and `subdivide` (at most
|
|
412
|
+
// 3 steps), as indexed geometry. Shared vertices shade smoothly; `flat` on the
|
|
413
|
+
// part flat-shades it.
|
|
414
|
+
export function cageGeometry(rawCage, part) {
|
|
415
|
+
const symmetric = part.symmetric === 'x';
|
|
416
|
+
const fromBox = part.kind === 'mesh' && Array.isArray(part.from?.box);
|
|
417
|
+
let cage = validCage(fromBox ? boxCage(part.from, symmetric) : rawCage);
|
|
418
|
+
if (!cage) {
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
const faceUvs = fromBox ? null : writtenUvs(rawCage, cage, part);
|
|
422
|
+
cage = applyOps(cage, part.ops, symmetric);
|
|
423
|
+
cage.faceUvs = faceUvs;
|
|
424
|
+
cage.creases = creaseEdges(cage, part.crease, symmetric);
|
|
425
|
+
let open = orientCage(cage);
|
|
426
|
+
if (symmetric) {
|
|
427
|
+
cage = mirrorCage(cage);
|
|
428
|
+
open = orientCage(cage);
|
|
429
|
+
}
|
|
430
|
+
const asked = Math.max(0, Math.min(3, Math.round(part.subdivide ?? 0)));
|
|
431
|
+
const steps = subdivideSteps(cage, part, asked);
|
|
432
|
+
for (let step = 0; step < steps; step++) {
|
|
433
|
+
cage = subdivideCage(cage);
|
|
434
|
+
}
|
|
435
|
+
const positions = cage.vertices.flat();
|
|
436
|
+
const indices = [];
|
|
437
|
+
for (const face of cage.faces) {
|
|
438
|
+
for (let i = 1; i + 1 < face.length; i++) {
|
|
439
|
+
indices.push(face[0], face[i], face[i + 1]);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
let geometry = new THREE.BufferGeometry();
|
|
443
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
|
|
444
|
+
geometry.setIndex(indices);
|
|
445
|
+
geometry.computeVertexNormals();
|
|
446
|
+
if (cage.faceUvs) {
|
|
447
|
+
geometry = splitByUv(geometry, cage);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// An open cage (a sheet) has no outside, so it is drawn from both sides.
|
|
451
|
+
geometry.userData.openCage = open;
|
|
452
|
+
return geometry;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Uvs written on the cage (a loft's) last only while its faces stay as
|
|
456
|
+
// written; `ops` and `symmetric` make new faces without them.
|
|
457
|
+
function writtenUvs(rawCage, cage, part) {
|
|
458
|
+
const changed = part.symmetric === 'x' || (Array.isArray(part.ops) && part.ops.length > 0);
|
|
459
|
+
const fits = Array.isArray(rawCage.faceUvs) && rawCage.faceUvs.length === cage.faces.length;
|
|
460
|
+
return fits && !changed ? rawCage.faceUvs : null;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// The smooth geometry again with a uv on every vertex. A vertex whose faces
|
|
464
|
+
// give it different uvs (on a seam) becomes one copy per uv, each with the
|
|
465
|
+
// shared normal, so the shading stays smooth across the seam.
|
|
466
|
+
function splitByUv(smooth, cage) {
|
|
467
|
+
const normals = smooth.getAttribute('normal');
|
|
468
|
+
const copies = new Map();
|
|
469
|
+
const positions = [];
|
|
470
|
+
const normalValues = [];
|
|
471
|
+
const uvs = [];
|
|
472
|
+
const copyOf = (index, uv) => {
|
|
473
|
+
const key = `${index},${uv[0]},${uv[1]}`;
|
|
474
|
+
if (!copies.has(key)) {
|
|
475
|
+
copies.set(key, positions.length / 3);
|
|
476
|
+
positions.push(...cage.vertices[index]);
|
|
477
|
+
normalValues.push(normals.getX(index), normals.getY(index), normals.getZ(index));
|
|
478
|
+
uvs.push(uv[0], uv[1]);
|
|
479
|
+
}
|
|
480
|
+
return copies.get(key);
|
|
481
|
+
};
|
|
482
|
+
const indices = [];
|
|
483
|
+
cage.faces.forEach((face, faceIndex) => {
|
|
484
|
+
const uvsOfFace = cage.faceUvs[faceIndex];
|
|
485
|
+
const corners = face.map((index) => copyOf(index, uvsOfFace.get(index)));
|
|
486
|
+
for (let i = 1; i + 1 < corners.length; i++) {
|
|
487
|
+
indices.push(corners[0], corners[i], corners[i + 1]);
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
const geometry = new THREE.BufferGeometry();
|
|
491
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
|
|
492
|
+
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normalValues, 3));
|
|
493
|
+
geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));
|
|
494
|
+
geometry.setIndex(indices);
|
|
495
|
+
return geometry;
|
|
496
|
+
}
|