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,267 @@
|
|
|
1
|
+
// Surface detail applied to a part's finished mesh: `displace` roughens the
|
|
2
|
+
// surface with noise. See docs/pxmodel-format.md for the fields.
|
|
3
|
+
//
|
|
4
|
+
// Noise needs enough vertices to show, so a coarse geometry is first refined
|
|
5
|
+
// by splitting its long edges. An edge is split by the same rule in both
|
|
6
|
+
// triangles that share it, so no cracks open.
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { valueNoise } from './partgeometry';
|
|
10
|
+
|
|
11
|
+
// Refinement stops at this many triangles.
|
|
12
|
+
const MAX_TRIANGLES = 60000;
|
|
13
|
+
|
|
14
|
+
export function hasSurfaceDetail(part) {
|
|
15
|
+
return part.displace > 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Replaces the mesh's geometry with a displaced one, as the part asks. Edges shorter than `detail.edge` meters (optimize.js) are
|
|
19
|
+
// not split.
|
|
20
|
+
export function applySurfaceDetail(mesh, part, detail = null) {
|
|
21
|
+
let geometry = mesh.geometry;
|
|
22
|
+
|
|
23
|
+
// Edges are measured in the part's own units, which the part's scale
|
|
24
|
+
// stretches by about the mean of its three scales.
|
|
25
|
+
const [x, y, z] = part.scale.map((value) => Math.abs(value) || 1);
|
|
26
|
+
const shortest = (detail?.edge ?? 0) / Math.cbrt(x * y * z);
|
|
27
|
+
if (part.displace > 0) {
|
|
28
|
+
const scale = noiseScale(part);
|
|
29
|
+
geometry = refine(geometry, Math.max(shortest, 1 / (Math.max(...scale) * 2.5 * 2.5)));
|
|
30
|
+
displace(geometry, part, scale);
|
|
31
|
+
}
|
|
32
|
+
if (geometry !== mesh.geometry) {
|
|
33
|
+
mesh.geometry.dispose();
|
|
34
|
+
mesh.geometry = geometry;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//
|
|
39
|
+
// Refinement
|
|
40
|
+
//
|
|
41
|
+
|
|
42
|
+
// A copy of the geometry, unindexed, with every edge longer than `target`
|
|
43
|
+
// split at its middle, repeated until none is or the triangle limit is
|
|
44
|
+
// reached. A triangle with one split edge becomes two, with two becomes
|
|
45
|
+
// three, with three becomes four. Every attribute is carried to the new
|
|
46
|
+
// vertices, and material groups are kept.
|
|
47
|
+
function refine(geometry, target) {
|
|
48
|
+
let soup = toSoup(geometry);
|
|
49
|
+
for (let pass = 0; pass < 10 && soup.triangles < MAX_TRIANGLES; pass++) {
|
|
50
|
+
const next = splitLongEdges(soup, target);
|
|
51
|
+
if (next.triangles === soup.triangles) {
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
soup = next;
|
|
55
|
+
}
|
|
56
|
+
return fromSoup(soup, geometry);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// A triangle soup: per attribute, a flat array with three vertices per
|
|
60
|
+
// triangle, plus each triangle's material group.
|
|
61
|
+
function toSoup(geometry) {
|
|
62
|
+
const flat = geometry.index ? geometry.toNonIndexed() : geometry;
|
|
63
|
+
const attributes = {};
|
|
64
|
+
for (const [name, attribute] of Object.entries(flat.attributes)) {
|
|
65
|
+
const values = [];
|
|
66
|
+
for (let i = 0; i < attribute.count; i++) {
|
|
67
|
+
for (let k = 0; k < attribute.itemSize; k++) {
|
|
68
|
+
values.push(attribute.getComponent(i, k));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
attributes[name] = { itemSize: attribute.itemSize, values };
|
|
72
|
+
}
|
|
73
|
+
const triangles = flat.attributes.position.count / 3;
|
|
74
|
+
const groups = new Array(triangles).fill(0);
|
|
75
|
+
for (const group of flat.groups) {
|
|
76
|
+
for (let t = group.start / 3; t < (group.start + group.count) / 3 && t < triangles; t++) {
|
|
77
|
+
groups[t] = group.materialIndex ?? 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { attributes, groups, triangles };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function fromSoup(soup, original) {
|
|
84
|
+
const geometry = new THREE.BufferGeometry();
|
|
85
|
+
for (const [name, { itemSize, values }] of Object.entries(soup.attributes)) {
|
|
86
|
+
geometry.setAttribute(name, new THREE.Float32BufferAttribute(values, itemSize));
|
|
87
|
+
}
|
|
88
|
+
if (original.groups.length > 0) {
|
|
89
|
+
let start = 0;
|
|
90
|
+
for (let t = 1; t <= soup.triangles; t++) {
|
|
91
|
+
if (t === soup.triangles || soup.groups[t] !== soup.groups[start]) {
|
|
92
|
+
geometry.addGroup(start * 3, (t - start) * 3, soup.groups[start]);
|
|
93
|
+
start = t;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return geometry;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function splitLongEdges(soup, target) {
|
|
101
|
+
const names = Object.keys(soup.attributes);
|
|
102
|
+
const out = { attributes: {}, groups: [], triangles: 0 };
|
|
103
|
+
for (const name of names) {
|
|
104
|
+
out.attributes[name] = { itemSize: soup.attributes[name].itemSize, values: [] };
|
|
105
|
+
}
|
|
106
|
+
const position = soup.attributes.position.values;
|
|
107
|
+
|
|
108
|
+
// A vertex is one entry per attribute; a midpoint averages two of them.
|
|
109
|
+
const vertex = (index) =>
|
|
110
|
+
names.map((name) => {
|
|
111
|
+
const { itemSize, values } = soup.attributes[name];
|
|
112
|
+
return values.slice(index * itemSize, index * itemSize + itemSize);
|
|
113
|
+
});
|
|
114
|
+
const middle = (a, b) => a.map((item, n) => item.map((value, k) => (value + b[n][k]) / 2));
|
|
115
|
+
const emit = (group, ...corners) => {
|
|
116
|
+
for (const corner of corners) {
|
|
117
|
+
names.forEach((name, n) => out.attributes[name].values.push(...corner[n]));
|
|
118
|
+
}
|
|
119
|
+
out.groups.push(group);
|
|
120
|
+
out.triangles++;
|
|
121
|
+
};
|
|
122
|
+
const length = (i, j) =>
|
|
123
|
+
Math.hypot(
|
|
124
|
+
position[i * 3] - position[j * 3],
|
|
125
|
+
position[i * 3 + 1] - position[j * 3 + 1],
|
|
126
|
+
position[i * 3 + 2] - position[j * 3 + 2]
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
for (let t = 0; t < soup.triangles; t++) {
|
|
130
|
+
const group = soup.groups[t];
|
|
131
|
+
const index = [t * 3, t * 3 + 1, t * 3 + 2];
|
|
132
|
+
const split = [0, 1, 2].map((e) => length(index[e], index[(e + 1) % 3]) > target);
|
|
133
|
+
const count = split.filter(Boolean).length;
|
|
134
|
+
const corners = index.map(vertex);
|
|
135
|
+
if (count === 0) {
|
|
136
|
+
emit(group, ...corners);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Turn the corners so the pattern starts at corner 0: for one split,
|
|
141
|
+
// edge 0-1 is split; for two, edge 2-0 is the one left whole.
|
|
142
|
+
let turn = 0;
|
|
143
|
+
if (count === 1) {
|
|
144
|
+
turn = split.indexOf(true);
|
|
145
|
+
} else if (count === 2) {
|
|
146
|
+
turn = (split.indexOf(false) + 1) % 3;
|
|
147
|
+
}
|
|
148
|
+
const [a, b, c] = [0, 1, 2].map((k) => corners[(k + turn) % 3]);
|
|
149
|
+
const ab = middle(a, b);
|
|
150
|
+
const bc = middle(b, c);
|
|
151
|
+
const ca = middle(c, a);
|
|
152
|
+
if (count === 1) {
|
|
153
|
+
emit(group, a, ab, c);
|
|
154
|
+
emit(group, ab, b, c);
|
|
155
|
+
} else if (count === 2) {
|
|
156
|
+
emit(group, ab, b, bc);
|
|
157
|
+
emit(group, a, ab, bc);
|
|
158
|
+
emit(group, a, bc, c);
|
|
159
|
+
} else {
|
|
160
|
+
emit(group, a, ab, ca);
|
|
161
|
+
emit(group, ab, b, bc);
|
|
162
|
+
emit(group, ca, bc, c);
|
|
163
|
+
emit(group, ab, bc, ca);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
//
|
|
170
|
+
// Displacement
|
|
171
|
+
//
|
|
172
|
+
|
|
173
|
+
// `noiseScale` is how many bumps per unit, a number or [x, y, z]. A small
|
|
174
|
+
// value along one axis stretches the bumps along it, as in bark.
|
|
175
|
+
function noiseScale(part) {
|
|
176
|
+
const value = part.noiseScale;
|
|
177
|
+
if (Array.isArray(value)) {
|
|
178
|
+
return [0, 1, 2].map((i) => (value[i] > 0 ? value[i] : 5));
|
|
179
|
+
}
|
|
180
|
+
return [0, 1, 2].map(() => (value > 0 ? value : 5));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Vertices at the same place are one point of the surface, even where the
|
|
184
|
+
// geometry splits them for a sharp edge or a seam.
|
|
185
|
+
function samePlaceGroups(position) {
|
|
186
|
+
const groups = new Map();
|
|
187
|
+
for (let i = 0; i < position.count; i++) {
|
|
188
|
+
const key = [position.getX(i), position.getY(i), position.getZ(i)]
|
|
189
|
+
.map((value) => Math.round(value * 1e4))
|
|
190
|
+
.join(',');
|
|
191
|
+
if (!groups.has(key)) {
|
|
192
|
+
groups.set(key, []);
|
|
193
|
+
}
|
|
194
|
+
groups.get(key).push(i);
|
|
195
|
+
}
|
|
196
|
+
return [...groups.values()];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Each point moves along the average of its normals by `displace` times the
|
|
200
|
+
// noise at that point. Two octaves, as in the lumpy sphere. All vertices at
|
|
201
|
+
// one place move together, so sharp edges stay closed.
|
|
202
|
+
function displace(geometry, part, scale) {
|
|
203
|
+
if (!geometry.attributes.normal) {
|
|
204
|
+
geometry.computeVertexNormals();
|
|
205
|
+
}
|
|
206
|
+
const position = geometry.attributes.position;
|
|
207
|
+
const normal = geometry.attributes.normal;
|
|
208
|
+
const originalNormals = normal.clone();
|
|
209
|
+
const seed = Number.isFinite(part.seed) ? part.seed : 1;
|
|
210
|
+
const places = samePlaceGroups(position);
|
|
211
|
+
const direction = new THREE.Vector3();
|
|
212
|
+
const point = new THREE.Vector3();
|
|
213
|
+
for (const place of places) {
|
|
214
|
+
direction.set(0, 0, 0);
|
|
215
|
+
for (const i of place) {
|
|
216
|
+
direction.x += normal.getX(i);
|
|
217
|
+
direction.y += normal.getY(i);
|
|
218
|
+
direction.z += normal.getZ(i);
|
|
219
|
+
}
|
|
220
|
+
direction.normalize();
|
|
221
|
+
point.fromBufferAttribute(position, place[0]);
|
|
222
|
+
const [x, y, z] = [point.x * scale[0], point.y * scale[1], point.z * scale[2]];
|
|
223
|
+
const n =
|
|
224
|
+
valueNoise(x, y, z, seed) * 0.7 + valueNoise(x * 2.5, y * 2.5, z * 2.5, seed + 7) * 0.3;
|
|
225
|
+
point.addScaledVector(direction, part.displace * n);
|
|
226
|
+
for (const i of place) {
|
|
227
|
+
position.setXYZ(i, point.x, point.y, point.z);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
smoothNormals(geometry, places, originalNormals);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Normals of the displaced surface: each vertex averages the faces around its
|
|
234
|
+
// point that faced within 60 degrees of its own original normal, so round
|
|
235
|
+
// parts shade smoothly and the edges of a box stay sharp.
|
|
236
|
+
function smoothNormals(geometry, places, originalNormals) {
|
|
237
|
+
const position = geometry.attributes.position;
|
|
238
|
+
const faceNormals = [];
|
|
239
|
+
const a = new THREE.Vector3();
|
|
240
|
+
const b = new THREE.Vector3();
|
|
241
|
+
const c = new THREE.Vector3();
|
|
242
|
+
for (let t = 0; t < position.count / 3; t++) {
|
|
243
|
+
a.fromBufferAttribute(position, t * 3);
|
|
244
|
+
b.fromBufferAttribute(position, t * 3 + 1);
|
|
245
|
+
c.fromBufferAttribute(position, t * 3 + 2);
|
|
246
|
+
faceNormals.push(new THREE.Triangle(a, b, c).getNormal(new THREE.Vector3()));
|
|
247
|
+
}
|
|
248
|
+
const normal = geometry.attributes.normal;
|
|
249
|
+
const mine = new THREE.Vector3();
|
|
250
|
+
const other = new THREE.Vector3();
|
|
251
|
+
const sum = new THREE.Vector3();
|
|
252
|
+
for (const place of places) {
|
|
253
|
+
for (const i of place) {
|
|
254
|
+
mine.fromBufferAttribute(originalNormals, i);
|
|
255
|
+
sum.set(0, 0, 0);
|
|
256
|
+
for (const j of place) {
|
|
257
|
+
other.fromBufferAttribute(originalNormals, j);
|
|
258
|
+
if (mine.dot(other) > 0.5) {
|
|
259
|
+
sum.add(faceNormals[Math.floor(j / 3)]);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
sum.normalize();
|
|
263
|
+
normal.setXYZ(i, sum.x, sum.y, sum.z);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
normal.needsUpdate = true;
|
|
267
|
+
}
|