castle-web-cli 0.4.203 → 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 (133) hide show
  1. package/dist/agent-failures.d.ts +3 -1
  2. package/dist/agent-failures.js +19 -0
  3. package/dist/agent-prompts.d.ts +1 -0
  4. package/dist/agent-prompts.js +20 -8
  5. package/dist/agent.d.ts +5 -2
  6. package/dist/agent.js +59 -125
  7. package/dist/castle-host/host.d.ts +17 -0
  8. package/dist/castle-host/host.js +551 -4
  9. package/dist/castleJson.d.ts +1 -0
  10. package/dist/devSessionStorage.js +65 -4
  11. package/dist/effortSupport.d.ts +7 -0
  12. package/dist/effortSupport.js +31 -0
  13. package/dist/init.js +13 -3
  14. package/dist/platformDoc.js +6 -1
  15. package/dist/routerFences.d.ts +22 -0
  16. package/dist/routerFences.js +139 -0
  17. package/dist/serve.js +35 -2
  18. package/dist/shell/assets/index-b_bebkRb.css +1 -0
  19. package/dist/shell/assets/{index-CxrtbzHf.js → index-hWQTC8q6.js} +5 -5
  20. package/dist/shell/index.html +2 -2
  21. package/dist/taskAccount.d.ts +13 -0
  22. package/dist/taskAccount.js +38 -0
  23. package/dist/vitePlugins.js +19 -1
  24. package/kits/base/CLAUDE.md +2 -0
  25. package/kits/base/castle.json +1 -1
  26. package/kits/base/sdk/README.md +131 -0
  27. package/kits/base/sdk/agent.d.ts +6 -0
  28. package/kits/base/sdk/agent.js +70 -0
  29. package/kits/base/sdk/castle.d.ts +4 -0
  30. package/kits/base/sdk/castle.js +2 -0
  31. package/kits/base/sdk/commands.d.ts +106 -0
  32. package/kits/base/sdk/commands.js +21 -0
  33. package/kits/base/sdk/decks.d.ts +7 -0
  34. package/kits/base/sdk/decks.js +31 -0
  35. package/kits/base/sdk/runtime.d.ts +4 -2
  36. package/kits/base/sdk/runtime.js +51 -40
  37. package/kits/base/sdk/server/storage.d.ts +44 -0
  38. package/kits/base/sdk/server/storage.js +54 -0
  39. package/kits/base/sdk/transport.d.ts +5 -1
  40. package/kits/base/sdk/transport.js +106 -23
  41. package/kits/multiplayer-2d/castle.json +4 -4
  42. package/kits/multiplayer-2d/code/client/poses.js +7 -0
  43. package/kits/multiplayer-2d/code/server/limits.js +3 -0
  44. package/kits/multiplayer-2d/code/server/ownership.js +36 -8
  45. package/kits/multiplayer-2d/code/systems/multiplayer.js +23 -2
  46. package/kits/multiplayer-3d/CLAUDE.md +12 -1
  47. package/kits/multiplayer-3d/castle.json +4 -4
  48. package/kits/multiplayer-3d/code/client/avatars.js +7 -1
  49. package/kits/multiplayer-3d/code/client/poses.js +61 -1
  50. package/kits/multiplayer-3d/code/client/steer.js +383 -0
  51. package/kits/multiplayer-3d/code/server/limits.js +3 -0
  52. package/kits/multiplayer-3d/code/server/ownership.js +114 -18
  53. package/kits/multiplayer-3d/code/server/world.js +12 -4
  54. package/kits/multiplayer-3d/code/systems/multiplayer.js +315 -34
  55. package/kits/physics-3d/CLAUDE.md +65 -7
  56. package/kits/physics-3d/castle.json +3 -3
  57. package/kits/physics-3d/docs/pxmodel-format.md +916 -15
  58. package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
  59. package/kits/physics-3d/engine3d/artobjects.js +218 -0
  60. package/kits/physics-3d/engine3d/blob.js +1058 -0
  61. package/kits/physics-3d/engine3d/branches.js +300 -0
  62. package/kits/physics-3d/engine3d/cage.js +496 -0
  63. package/kits/physics-3d/engine3d/cageops.js +516 -0
  64. package/kits/physics-3d/engine3d/frame.js +669 -0
  65. package/kits/physics-3d/engine3d/look.js +675 -0
  66. package/kits/physics-3d/engine3d/materialpresets.js +486 -0
  67. package/kits/physics-3d/engine3d/modelEditorWorld.js +3 -2
  68. package/kits/physics-3d/engine3d/modelrefs.js +120 -0
  69. package/kits/physics-3d/engine3d/motion.js +560 -0
  70. package/kits/physics-3d/engine3d/objtext.js +37 -0
  71. package/kits/physics-3d/engine3d/optimize.js +1083 -0
  72. package/kits/physics-3d/engine3d/paint.js +674 -0
  73. package/kits/physics-3d/engine3d/partgeometry.js +467 -0
  74. package/kits/physics-3d/engine3d/paths.js +324 -0
  75. package/kits/physics-3d/engine3d/pxmodel.js +429 -26
  76. package/kits/physics-3d/engine3d/scatter.js +102 -0
  77. package/kits/physics-3d/engine3d/sculpt.js +1148 -0
  78. package/kits/physics-3d/engine3d/surfacedetail.js +267 -0
  79. package/kits/physics-3d/engine3d/sweep.js +480 -0
  80. package/kits/physics-3d/engine3d/thumbnails.js +6 -4
  81. package/kits/physics-3d/engine3d/world3d.js +119 -17
  82. package/kits/physics-3d/eslint.config.js +13 -0
  83. package/kits/physics-3d/models/parts/axe.pxmodel +40 -0
  84. package/kits/physics-3d/models/parts/backpack.pxmodel +47 -0
  85. package/kits/physics-3d/models/parts/barrel.pxmodel +22 -0
  86. package/kits/physics-3d/models/parts/bedroll.pxmodel +28 -0
  87. package/kits/physics-3d/models/parts/belt-pouch.pxmodel +22 -0
  88. package/kits/physics-3d/models/parts/bolt.pxmodel +10 -0
  89. package/kits/physics-3d/models/parts/bracer.pxmodel +26 -0
  90. package/kits/physics-3d/models/parts/chain-span.pxmodel +12 -0
  91. package/kits/physics-3d/models/parts/chimney.pxmodel +11 -0
  92. package/kits/physics-3d/models/parts/crate.pxmodel +44 -0
  93. package/kits/physics-3d/models/parts/crystal.pxmodel +22 -0
  94. package/kits/physics-3d/models/parts/door-arched.pxmodel +12 -0
  95. package/kits/physics-3d/models/parts/engine-block.pxmodel +39 -0
  96. package/kits/physics-3d/models/parts/exhaust-pipe.pxmodel +30 -0
  97. package/kits/physics-3d/models/parts/fence-span.pxmodel +10 -0
  98. package/kits/physics-3d/models/parts/fender.pxmodel +19 -0
  99. package/kits/physics-3d/models/parts/headlight.pxmodel +24 -0
  100. package/kits/physics-3d/models/parts/hood.pxmodel +17 -0
  101. package/kits/physics-3d/models/parts/horned-helmet.pxmodel +37 -0
  102. package/kits/physics-3d/models/parts/jerry-can.pxmodel +26 -0
  103. package/kits/physics-3d/models/parts/ladder.pxmodel +10 -0
  104. package/kits/physics-3d/models/parts/lamp.pxmodel +20 -0
  105. package/kits/physics-3d/models/parts/lantern.pxmodel +30 -0
  106. package/kits/physics-3d/models/parts/leaf-cluster.pxmodel +21 -0
  107. package/kits/physics-3d/models/parts/offroad-tire.pxmodel +48 -0
  108. package/kits/physics-3d/models/parts/pauldron.pxmodel +26 -0
  109. package/kits/physics-3d/models/parts/rivet.pxmodel +8 -0
  110. package/kits/physics-3d/models/parts/rock.pxmodel +21 -0
  111. package/kits/physics-3d/models/parts/roll-bar-light.pxmodel +26 -0
  112. package/kits/physics-3d/models/parts/round-shield.pxmodel +31 -0
  113. package/kits/physics-3d/models/parts/saddlebag.pxmodel +30 -0
  114. package/kits/physics-3d/models/parts/spoked-wheel.pxmodel +39 -0
  115. package/kits/physics-3d/models/parts/staff.pxmodel +33 -0
  116. package/kits/physics-3d/models/parts/stepping-stones.pxmodel +8 -0
  117. package/kits/physics-3d/models/parts/stone-base.pxmodel +10 -0
  118. package/kits/physics-3d/models/parts/sword.pxmodel +33 -0
  119. package/kits/physics-3d/models/parts/window-lit.pxmodel +13 -0
  120. package/kits/physics-3d/models/parts/wizard-hat.pxmodel +28 -0
  121. package/kits/physics-3d/models/templates/biped-stout.pxmodel +59 -0
  122. package/kits/physics-3d/models/templates/biped-tall.pxmodel +63 -0
  123. package/kits/physics-3d/models/templates/quadruped.pxmodel +52 -0
  124. package/kits/physics-3d/scripts/render/filmstrip.js +217 -0
  125. package/kits/physics-3d/scripts/render/index.html +20 -0
  126. package/kits/physics-3d/scripts/render/sheet.js +411 -0
  127. package/kits/physics-3d/scripts/render.mjs +646 -0
  128. package/kits/real-time/castle.json +2 -2
  129. package/kits/real-time/code/client/ownership.js +14 -4
  130. package/kits/real-time/code/client/smooth.js +2 -2
  131. package/kits/real-time/code/server/session.js +20 -4
  132. package/package.json +2 -2
  133. package/dist/shell/assets/index-CHMcsJrW.css +0 -1
@@ -0,0 +1,486 @@
1
+ // Material presets: `"material": "rusted-steel"` on a part stands for a
2
+ // finish -- roughness, metalness, clearcoat and a stack of paint layers --
3
+ // tuned together. See docs/pxmodel-format.md for the list.
4
+ //
5
+ // A preset's colors come from one base color: `materialColor`, else the
6
+ // part's own `color` when written, else the preset's. Pattern sizes are
7
+ // fractions of the part's size, so grime on a drum and on a bolt both show a
8
+ // few patches. Fields written on the part win over the preset, and the part's
9
+ // own `paint` layers go on top of the preset's.
10
+
11
+ import * as THREE from 'three';
12
+ import { PART_DEFAULTS } from './pxmodel';
13
+
14
+ const FINISH_KEYS = ['roughness', 'metalness', 'clearcoat', 'emissive', 'emissiveIntensity'];
15
+
16
+ // Physical material fields that parts do not have, set only by a preset.
17
+ const EXTRA_KEYS = ['transmission', 'thickness', 'ior', 'sheen', 'sheenRoughness', 'sheenColor'];
18
+
19
+ // Sets the preset's finish on the mesh's materials and returns the part with
20
+ // the preset's color and paint filled in, for the surface detail
21
+ // and paint steps that follow. A part without a known preset is returned as
22
+ // it is.
23
+ export function applyMaterialPreset(mesh, part) {
24
+ if (typeof part.material !== 'string' || !Object.hasOwn(PRESETS, part.material)) {
25
+ return part;
26
+ }
27
+ const base = new THREE.Color(baseColor(part));
28
+ const preset = PRESETS[part.material](base, measure(mesh, part));
29
+ const expanded = { ...part, color: preset.color ?? hex(base) };
30
+ for (const key of FINISH_KEYS) {
31
+ if (part[key] === PART_DEFAULTS[key] && preset[key] !== undefined) {
32
+ expanded[key] = preset[key];
33
+ }
34
+ }
35
+ expanded.paint = [...(preset.paint ?? []), ...(Array.isArray(part.paint) ? part.paint : [])];
36
+ for (const material of [mesh.material].flat()) {
37
+ setFinish(material, expanded, preset);
38
+ }
39
+ return expanded;
40
+ }
41
+
42
+ function baseColor(part) {
43
+ if (typeof part.materialColor === 'string') {
44
+ return part.materialColor;
45
+ }
46
+ if (part.color !== PART_DEFAULTS.color) {
47
+ return part.color;
48
+ }
49
+ return PRESET_COLORS[part.material];
50
+ }
51
+
52
+ function setFinish(material, part, preset) {
53
+ if (!material.map && !material.vertexColors) {
54
+ material.color.set(part.color);
55
+ }
56
+ material.roughness = part.roughness;
57
+ material.metalness = part.metalness;
58
+ material.clearcoat = part.clearcoat;
59
+ material.emissive.set(part.emissive);
60
+ material.emissiveIntensity = part.emissiveIntensity;
61
+ for (const key of EXTRA_KEYS) {
62
+ if (preset[key] === undefined) {
63
+ continue;
64
+ }
65
+ if (material[key]?.isColor) {
66
+ material[key].set(preset[key]);
67
+ } else {
68
+ material[key] = preset[key];
69
+ }
70
+ }
71
+ if (preset.glowByColor) {
72
+ glowByColor(material);
73
+ }
74
+ }
75
+
76
+ // Emissive light is one color for the whole material. Here it is multiplied
77
+ // by the vertex color, so bright paint glows and dark paint barely does.
78
+ function glowByColor(material) {
79
+ material.onBeforeCompile = (shader) => {
80
+ shader.fragmentShader = shader.fragmentShader.replace(
81
+ '#include <emissivemap_fragment>',
82
+ '#include <emissivemap_fragment>\n#ifdef USE_COLOR\ntotalEmissiveRadiance *= vColor.rgb;\n#endif'
83
+ );
84
+ };
85
+ material.customProgramCacheKey = () => 'glow-by-color';
86
+ }
87
+
88
+ //
89
+ // Measuring
90
+ //
91
+
92
+ // The part's size in its own space: `length` is the middle one of its width,
93
+ // height and depth, so a pole is measured across and a plate along its face;
94
+ // `bottom` and `top` are its lowest and highest y from the pivot, and `long`
95
+ // the axis it is longest along.
96
+ function measure(mesh, part) {
97
+ mesh.geometry.computeBoundingBox();
98
+ const box = mesh.geometry.boundingBox;
99
+ const extent = [0, 1, 2].map(
100
+ (axis) => (box.max.getComponent(axis) - box.min.getComponent(axis)) * Math.abs(part.scale[axis])
101
+ );
102
+ const sorted = [...extent].sort((a, b) => a - b);
103
+ const low = box.min.y * part.scale[1] + part.offset[1];
104
+ const high = box.max.y * part.scale[1] + part.offset[1];
105
+ return {
106
+ length: Math.max(0.02, sorted[1]),
107
+ bottom: Math.min(low, high),
108
+ top: Math.max(low, high),
109
+ long: 'xyz'[extent.indexOf(sorted[2])],
110
+ };
111
+ }
112
+
113
+ // Patches per meter, so about `count` of them cross the part.
114
+ function per(size, count) {
115
+ return count / size.length;
116
+ }
117
+
118
+ // A fraction of the part's size in meters.
119
+ function across(size, fraction) {
120
+ return size.length * fraction;
121
+ }
122
+
123
+ // A height a fraction of the way up the part.
124
+ function upAt(size, fraction) {
125
+ return size.bottom + (size.top - size.bottom) * fraction;
126
+ }
127
+
128
+ //
129
+ // Colors
130
+ //
131
+
132
+ function hex(color) {
133
+ return `#${color.getHexString()}`;
134
+ }
135
+
136
+ // The base color moved in lightness, saturation and hue, each -1 to 1.
137
+ function shade(base, lightness, saturation = 0, hue = 0) {
138
+ return hex(base.clone().offsetHSL(hue, saturation, lightness));
139
+ }
140
+
141
+ // The base color blended toward another by `amount`.
142
+ function mix(base, other, amount) {
143
+ return hex(base.clone().lerp(new THREE.Color(other), amount));
144
+ }
145
+
146
+ // Grain as two sets of thin bands along the part's longest axis, of
147
+ // different widths, so the lines do not repeat evenly.
148
+ function grain(base, size, dark, light) {
149
+ const turn = { x: ['z', 0], y: ['z', 90], z: ['x', 0] }[size.long];
150
+ return [
151
+ {
152
+ pattern: 'stripes',
153
+ colors: [shade(base, dark)],
154
+ width: across(size, 0.05),
155
+ axis: turn[0],
156
+ angle: turn[1],
157
+ },
158
+ {
159
+ pattern: 'stripes',
160
+ colors: [shade(base, light)],
161
+ width: across(size, 0.083),
162
+ axis: turn[0],
163
+ angle: turn[1],
164
+ seed: 2,
165
+ },
166
+ ];
167
+ }
168
+
169
+ //
170
+ // Presets
171
+ //
172
+
173
+ const PRESET_COLORS = {
174
+ 'rusted-steel': '#7a7672',
175
+ 'worn-paint': '#3f78b5',
176
+ 'painted-metal': '#c0392b',
177
+ chrome: '#dfe3e8',
178
+ rubber: '#2b2b2d',
179
+ wood: '#a8733f',
180
+ 'weathered-wood': '#8e8474',
181
+ stone: '#8c8781',
182
+ 'mossy-stone': '#85817a',
183
+ bark: '#5e4636',
184
+ fur: '#b8692e',
185
+ skin: '#e2a887',
186
+ cloth: '#4d6f9e',
187
+ leather: '#74492b',
188
+ foliage: '#4c8a2e',
189
+ plastic: '#d8453a',
190
+ glass: '#d6ecf2',
191
+ gold: '#d9a83a',
192
+ clay: '#b8653f',
193
+ ceramic: '#ede7dc',
194
+ snow: '#f3f6fa',
195
+ lava: '#ff6a14',
196
+ };
197
+
198
+ // Each preset takes the base color and the part's size and returns its
199
+ // finish and paint.
200
+ const PRESETS = {
201
+ 'rusted-steel': (base, size) => ({
202
+ roughness: 0.7,
203
+ metalness: 0.55,
204
+ paint: [
205
+ { pattern: 'grime', color: '#8a4521', amount: 0.22, scale: per(size, 5) },
206
+ { pattern: 'grime', color: '#5c2c17', amount: 0.12, scale: per(size, 10), seed: 3 },
207
+ { pattern: 'spots', color: '#9c5424', size: across(size, 0.02), density: 0.5, seed: 4 },
208
+ { pattern: 'cavity', color: '#3a2016', amount: 0.7, width: across(size, 0.08) },
209
+ { pattern: 'edges', color: shade(base, 0.2), amount: 0.5, width: across(size, 0.03) },
210
+ ],
211
+ }),
212
+ 'worn-paint': (base, size) => ({
213
+ roughness: 0.55,
214
+ metalness: 0.15,
215
+ paint: [
216
+ { pattern: 'grime', color: shade(base, 0.04, -0.08), amount: 0.3, scale: per(size, 5) },
217
+ {
218
+ pattern: 'grime',
219
+ color: '#4a3c2e',
220
+ amount: 0.3,
221
+ scale: per(size, 4),
222
+ below: upAt(size, 0.3),
223
+ seed: 2,
224
+ },
225
+ { pattern: 'cavity', amount: 0.5, width: across(size, 0.08) },
226
+ { pattern: 'edges', color: '#9a968f', amount: 0.9, width: across(size, 0.12) },
227
+ ],
228
+ }),
229
+ 'painted-metal': (base, size) => ({
230
+ roughness: 0.35,
231
+ metalness: 0.3,
232
+ clearcoat: 0.5,
233
+ paint: [
234
+ { pattern: 'cavity', amount: 0.35, width: across(size, 0.06) },
235
+ { pattern: 'edges', amount: 0.3, width: across(size, 0.02) },
236
+ ],
237
+ }),
238
+ chrome: (base, size) => ({
239
+ roughness: 0.1,
240
+ metalness: 0.5,
241
+ clearcoat: 1,
242
+ paint: [{ pattern: 'cavity', amount: 0.5, width: across(size, 0.05) }],
243
+ }),
244
+ rubber: (base, size) => ({
245
+ roughness: 0.92,
246
+ paint: [
247
+ { pattern: 'grime', color: shade(base, 0.025), amount: 0.2, scale: per(size, 6) },
248
+ { pattern: 'edges', color: shade(base, 0.08), amount: 0.35, width: across(size, 0.03) },
249
+ ],
250
+ }),
251
+ wood: (base, size) => ({
252
+ roughness: 0.65,
253
+ paint: [
254
+ ...grain(base, size, -0.06, 0.05),
255
+ { pattern: 'grime', color: shade(base, -0.06), amount: 0.25, scale: per(size, 6), seed: 3 },
256
+ { pattern: 'cavity', amount: 0.5, width: across(size, 0.06) },
257
+ { pattern: 'edges', amount: 0.3, width: across(size, 0.025) },
258
+ ],
259
+ }),
260
+ 'weathered-wood': (base, size) => ({
261
+ roughness: 0.9,
262
+ paint: [
263
+ ...grain(base, size, -0.06, 0.05),
264
+ { pattern: 'grime', color: shade(base, -0.07), amount: 0.25, scale: per(size, 8), seed: 3 },
265
+ {
266
+ pattern: 'grime',
267
+ color: '#5d6b3a',
268
+ amount: 0.15,
269
+ scale: per(size, 4),
270
+ below: upAt(size, 0.35),
271
+ seed: 6,
272
+ },
273
+ { pattern: 'cavity', color: '#2e2820', amount: 0.6, width: across(size, 0.07) },
274
+ { pattern: 'edges', color: shade(base, 0.1, -0.1), amount: 0.55, width: across(size, 0.03) },
275
+ ],
276
+ }),
277
+ stone: (base, size) => ({
278
+ roughness: 0.9,
279
+ paint: stonePaint(base, size),
280
+ }),
281
+ 'mossy-stone': (base, size) => ({
282
+ roughness: 0.92,
283
+ paint: [
284
+ ...stonePaint(base, size),
285
+ {
286
+ pattern: 'grime',
287
+ color: '#4a5e2a',
288
+ amount: 0.5,
289
+ scale: per(size, 4),
290
+ above: upAt(size, 0.45),
291
+ seed: 7,
292
+ },
293
+ {
294
+ pattern: 'grime',
295
+ color: '#5f7535',
296
+ amount: 0.25,
297
+ scale: per(size, 8),
298
+ above: upAt(size, 0.55),
299
+ seed: 8,
300
+ },
301
+ ],
302
+ }),
303
+ bark: (base, size) => ({
304
+ roughness: 0.95,
305
+ paint: [
306
+ {
307
+ pattern: 'stripes',
308
+ colors: [shade(base, -0.05)],
309
+ width: across(size, 0.07),
310
+ axis: 'z',
311
+ angle: 90,
312
+ },
313
+ {
314
+ pattern: 'stripes',
315
+ colors: [shade(base, -0.04)],
316
+ width: across(size, 0.11),
317
+ axis: 'x',
318
+ angle: 90,
319
+ seed: 2,
320
+ },
321
+ {
322
+ pattern: 'grime',
323
+ color: shade(base, 0.04, -0.05),
324
+ amount: 0.25,
325
+ scale: per(size, 7),
326
+ seed: 3,
327
+ },
328
+ {
329
+ pattern: 'grime',
330
+ color: '#56682f',
331
+ amount: 0.15,
332
+ scale: per(size, 3),
333
+ below: upAt(size, 0.25),
334
+ seed: 5,
335
+ },
336
+ { pattern: 'cavity', amount: 0.5, width: across(size, 0.08) },
337
+ ],
338
+ }),
339
+ fur: (base, size) => ({
340
+ roughness: 1,
341
+ sheen: 0.4,
342
+ sheenRoughness: 0.6,
343
+ sheenColor: shade(base, 0.2),
344
+ paint: [
345
+ { pattern: 'grime', color: shade(base, 0.05), amount: 0.3, scale: per(size, 5) },
346
+ { pattern: 'cavity', color: shade(base, -0.18), amount: 0.4, width: across(size, 0.1) },
347
+ ],
348
+ }),
349
+ skin: (base, size) => ({
350
+ roughness: 0.55,
351
+ clearcoat: 0.1,
352
+ sheen: 0.2,
353
+ sheenColor: shade(base, 0.1, 0, -0.02),
354
+ paint: [
355
+ {
356
+ pattern: 'grime',
357
+ color: shade(base, -0.015, 0.04, -0.02),
358
+ amount: 0.2,
359
+ scale: per(size, 4),
360
+ },
361
+ {
362
+ pattern: 'cavity',
363
+ color: shade(base, -0.2, 0.1, -0.02),
364
+ amount: 0.4,
365
+ width: across(size, 0.08),
366
+ },
367
+ ],
368
+ }),
369
+ cloth: (base, size) => ({
370
+ roughness: 1,
371
+ sheen: 0.6,
372
+ sheenRoughness: 0.8,
373
+ sheenColor: shade(base, 0.25, -0.2),
374
+ paint: [
375
+ { pattern: 'grime', color: shade(base, -0.03), amount: 0.3, scale: per(size, 6) },
376
+ { pattern: 'cavity', amount: 0.55, width: across(size, 0.08) },
377
+ ],
378
+ }),
379
+ leather: (base, size) => ({
380
+ roughness: 0.6,
381
+ clearcoat: 0.2,
382
+ paint: [
383
+ { pattern: 'grime', color: shade(base, -0.04), amount: 0.3, scale: per(size, 6) },
384
+ { pattern: 'cavity', amount: 0.6, width: across(size, 0.06) },
385
+ { pattern: 'edges', color: shade(base, 0.12), amount: 0.45, width: across(size, 0.03) },
386
+ ],
387
+ }),
388
+ foliage: (base, size) => ({
389
+ roughness: 0.8,
390
+ paint: [
391
+ {
392
+ pattern: 'grime',
393
+ color: shade(base, 0.06, 0.03, -0.03),
394
+ amount: 0.25,
395
+ scale: per(size, 6),
396
+ above: upAt(size, 0.4),
397
+ },
398
+ { pattern: 'cavity', amount: 0.6, width: across(size, 0.08) },
399
+ ],
400
+ }),
401
+ plastic: (base, size) => ({
402
+ roughness: 0.35,
403
+ clearcoat: 0.3,
404
+ paint: [
405
+ { pattern: 'cavity', amount: 0.3, width: across(size, 0.05) },
406
+ { pattern: 'edges', amount: 0.2, width: across(size, 0.02) },
407
+ ],
408
+ }),
409
+ glass: () => ({
410
+ roughness: 0.05,
411
+ clearcoat: 1,
412
+ transmission: 0.92,
413
+ thickness: 0.2,
414
+ ior: 1.5,
415
+ }),
416
+ gold: (base, size) => ({
417
+ roughness: 0.25,
418
+ metalness: 0.5,
419
+ clearcoat: 0.5,
420
+ paint: [
421
+ { pattern: 'cavity', color: shade(base, -0.3), amount: 0.6, width: across(size, 0.06) },
422
+ { pattern: 'edges', color: shade(base, 0.15), amount: 0.4, width: across(size, 0.02) },
423
+ ],
424
+ }),
425
+ clay: (base, size) => ({
426
+ roughness: 0.95,
427
+ paint: [
428
+ { pattern: 'grime', color: shade(base, 0.06, -0.05), amount: 0.3, scale: per(size, 4) },
429
+ { pattern: 'cavity', amount: 0.45, width: across(size, 0.06) },
430
+ ],
431
+ }),
432
+ ceramic: (base, size) => ({
433
+ roughness: 0.2,
434
+ clearcoat: 0.8,
435
+ paint: [{ pattern: 'cavity', amount: 0.3, width: across(size, 0.05) }],
436
+ }),
437
+ snow: (base, size) => ({
438
+ roughness: 0.85,
439
+ sheen: 0.3,
440
+ sheenColor: '#ffffff',
441
+ paint: [
442
+ { pattern: 'grime', color: mix(base, '#c9d6e8', 0.6), amount: 0.3, scale: per(size, 3) },
443
+ {
444
+ pattern: 'cavity',
445
+ color: mix(base, '#7f97bd', 0.6),
446
+ amount: 0.5,
447
+ width: across(size, 0.08),
448
+ },
449
+ ],
450
+ }),
451
+ lava: (base, size) => ({
452
+ color: '#1f1612',
453
+ roughness: 0.9,
454
+ emissive: '#ffffff',
455
+ emissiveIntensity: 1.6,
456
+ glowByColor: true,
457
+ paint: [
458
+ { pattern: 'grime', color: '#34251d', amount: 0.35, scale: per(size, 6) },
459
+ { pattern: 'cracks', color: hex(base), amount: 0.5, scale: per(size, 2) },
460
+ { pattern: 'cracks', color: shade(base, -0.1), amount: 0.25, scale: per(size, 5), seed: 2 },
461
+ ],
462
+ }),
463
+ };
464
+
465
+ // Stone: large dark and light patches, fine speckle and dark cracks.
466
+ function stonePaint(base, size) {
467
+ return [
468
+ { pattern: 'grime', color: shade(base, -0.06), amount: 0.4, scale: per(size, 3) },
469
+ {
470
+ pattern: 'grime',
471
+ color: shade(base, 0.05, -0.05),
472
+ amount: 0.3,
473
+ scale: per(size, 7),
474
+ seed: 2,
475
+ },
476
+ {
477
+ pattern: 'spots',
478
+ color: shade(base, -0.1),
479
+ size: across(size, 0.015),
480
+ density: 0.3,
481
+ seed: 3,
482
+ },
483
+ { pattern: 'cracks', color: shade(base, -0.15), amount: 0.15, scale: per(size, 2), seed: 4 },
484
+ { pattern: 'cavity', amount: 0.75, width: across(size, 0.08) },
485
+ ];
486
+ }
@@ -79,7 +79,8 @@ export function createModelEditorWorld(canvas, callbacks) {
79
79
  return med;
80
80
  }
81
81
 
82
- export function setEditorModel(med, model, sprites) {
82
+ // `files` and `path` let `kind: "model"` parts find the files they reference.
83
+ export function setEditorModel(med, model, sprites, files = null, path = '') {
83
84
  clearEditVisuals(med);
84
85
  if (med.modelNode) {
85
86
  med.gizmo.detach();
@@ -89,7 +90,7 @@ export function setEditorModel(med, model, sprites) {
89
90
  // A private clone: edit-mode drags mutate this copy live (the parse cache
90
91
  // shares model objects with the play runtime, so never mutate those).
91
92
  med.model = model ? structuredClone(model) : null;
92
- med.modelNode = med.model ? buildModelNode(med.model, sprites) : null;
93
+ med.modelNode = med.model ? buildModelNode(med.model, sprites, files, path) : null;
93
94
  med.partGroups = med.modelNode?.userData.modelParts ?? null;
94
95
  if (med.modelNode) med.world.scene.add(med.modelNode);
95
96
  }
@@ -0,0 +1,120 @@
1
+ // Model references: a part with `kind: "model"` draws another `.pxmodel` file
2
+ // in its place, so a tire, a lamp or a crate is written once and reused.
3
+ //
4
+ // A reference names its file the way an art ref does: `models/parts/tire.pxmodel`
5
+ // is a file of the deck the referencing model belongs to, and
6
+ // `@imports/<alias>/...` (or `imports/<alias>/...`) is a file of an import.
7
+ // Callers hand the builder a map from deck path to file text -- the deck's file
8
+ // map works as it is -- and the referencing model's own deck path.
9
+
10
+ import * as THREE from 'three';
11
+ import { parseModelCached, buildModelNode, modelArtSignature } from './pxmodel';
12
+
13
+ // References nest at most this deep: a model, a part of it, a part of that.
14
+ export const MAX_MODEL_DEPTH = 4;
15
+
16
+ // The deck path a `src` names, written in the model at `fromPath`.
17
+ export function resolveModelRef(src, fromPath = '') {
18
+ const ref = String(src ?? '').replace(/^\.\//, '');
19
+ if (ref.startsWith('@imports/')) {
20
+ return ref.slice(1);
21
+ }
22
+ if (ref.startsWith('imports/')) {
23
+ return ref;
24
+ }
25
+ const deck = /^(imports\/[^/]+\/)/.exec(fromPath ?? '');
26
+ return (deck ? deck[1] : '') + ref;
27
+ }
28
+
29
+ // The model a reference names, with its deck path, or null when the file is
30
+ // missing, is not a model, is already being built above (a cycle), or is
31
+ // deeper than MAX_MODEL_DEPTH. `chain` is the paths of the models above.
32
+ function referencedModel(part, models, chain) {
33
+ if (typeof part.src !== 'string' || !part.src) {
34
+ return null;
35
+ }
36
+ const path = resolveModelRef(part.src, chain[chain.length - 1]);
37
+ if (chain.includes(path) || chain.length > MAX_MODEL_DEPTH) {
38
+ return null;
39
+ }
40
+ const model = parseModelCached(models?.[path]);
41
+ return model ? { model, path } : null;
42
+ }
43
+
44
+ // Build a `kind: "model"` part's node. A reference that cannot be built draws
45
+ // nothing. The inner model's parts lose their ids, so picking and animation
46
+ // see the reference as one part of the outer model.
47
+ //
48
+ // A game build (`options.references`, optimize.js) builds each referenced
49
+ // model once per tint and material change and hands out copies that share
50
+ // its geometry and materials. Copies after the first are marked
51
+ // `sharedReference`, since their materials are already changed.
52
+ export function buildModelRef(part, sprites, models, chain, options = {}) {
53
+ const found = referencedModel(part, models, chain);
54
+ if (!found) {
55
+ return new THREE.Group();
56
+ }
57
+ const changes = [part.tint, part.brightness, part.alphaTest, part.doubleSided, options.budget];
58
+ const key = JSON.stringify([found.path, ...changes]);
59
+ const shared = options.references?.get(key);
60
+ if (shared) {
61
+ const copy = shared.clone(true);
62
+ copy.userData.sharedReference = true;
63
+ return copy;
64
+ }
65
+ const inner = [...chain, found.path];
66
+ const node = buildModelNode(found.model, sprites, models, found.path, inner, options);
67
+ delete node.userData.modelParts;
68
+ node.traverse((child) => {
69
+ delete child.userData.partId;
70
+ delete child.userData.partMesh;
71
+ });
72
+ if (part.tint) {
73
+ tintNode(node, new THREE.Color(part.tint));
74
+ }
75
+ if (options.references) {
76
+ options.references.set(key, node);
77
+ return node.clone(true);
78
+ }
79
+ return node;
80
+ }
81
+
82
+ // `tint` multiplies every color in the model, so white leaves it as it is and
83
+ // dark parts stay dark. Copies made by `mirror` and `repeat` share materials;
84
+ // each is tinted once.
85
+ function tintNode(node, tint) {
86
+ const done = new Set();
87
+ node.traverse((child) => {
88
+ if (!child.isMesh) {
89
+ return;
90
+ }
91
+ for (const material of [child.material].flat()) {
92
+ if (!done.has(material)) {
93
+ done.add(material);
94
+ material.color.multiply(tint);
95
+ }
96
+ }
97
+ });
98
+ }
99
+
100
+ // The text and art of every model a model references, at any depth, for
101
+ // change signatures: editing a referenced file rebuilds what uses it.
102
+ export function modelRefSignature(model, sprites, models, path = '') {
103
+ const out = [];
104
+ const visit = (current, chain) => {
105
+ for (const part of current.parts) {
106
+ if (part.kind !== 'model') {
107
+ continue;
108
+ }
109
+ const found = referencedModel(part, models, chain);
110
+ if (found) {
111
+ out.push(`${found.path}:${models[found.path]}:${modelArtSignature(found.model, sprites)}`);
112
+ visit(found.model, [...chain, found.path]);
113
+ }
114
+ }
115
+ };
116
+ if (model) {
117
+ visit(model, [path]);
118
+ }
119
+ return out.join('|');
120
+ }