castle-web-cli 0.4.204 → 0.4.206
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.d.ts +1 -0
- package/dist/agent-prompts.js +7 -2
- package/dist/agent.d.ts +3 -2
- package/dist/agent.js +29 -18
- 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/imports.d.ts +1 -1
- package/dist/imports.js +59 -19
- package/dist/index.js +4 -3
- package/dist/init.d.ts +9 -1
- package/dist/init.js +64 -25
- package/dist/kitCatalog.d.ts +13 -0
- package/dist/kitCatalog.js +111 -0
- package/dist/platformDoc.js +7 -2
- 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 +5 -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 +5 -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-2d/castle.json +1 -0
- package/kits/physics-3d/CLAUDE.md +65 -7
- package/kits/physics-3d/castle.json +4 -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/kits/turn-based/castle.json +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// Contact sheets of `.pxmodel` files, built with this kit's model builder and
|
|
2
|
+
// lit like the game, so a sheet shows what a deck would.
|
|
3
|
+
//
|
|
4
|
+
// `scripts/render.mjs` copies this page into a scratch deck, writes
|
|
5
|
+
// `target/sheet.json` there -- a list of models, each with the text of every
|
|
6
|
+
// art file and model file it references -- and takes a playtest screenshot of each page,
|
|
7
|
+
// pressing `n` in between. Every model has three card-sized pages, which the
|
|
8
|
+
// script lays side by side:
|
|
9
|
+
//
|
|
10
|
+
// 0 the game's own camera over the whole card, at game size
|
|
11
|
+
// 1 overhead yaw 45 pitch 45, front, back, right side
|
|
12
|
+
// 2 left side, top, and two 3/4 views from above
|
|
13
|
+
//
|
|
14
|
+
// A model with `motion` set (`--motion`) gets three filmstrip pages instead:
|
|
15
|
+
// idle, walking in place, and its first clip (filmstrip.js).
|
|
16
|
+
|
|
17
|
+
import * as THREE from 'three';
|
|
18
|
+
import { setup } from 'castle-web-sdk';
|
|
19
|
+
import {
|
|
20
|
+
parseModel,
|
|
21
|
+
buildModelNode,
|
|
22
|
+
visibleBounds,
|
|
23
|
+
} from '/imports/castle.physics-3d/engine3d/pxmodel.js';
|
|
24
|
+
import {
|
|
25
|
+
DEFAULT_TRIANGLES,
|
|
26
|
+
modelCost,
|
|
27
|
+
referenceBudget,
|
|
28
|
+
} from '/imports/castle.physics-3d/engine3d/optimize.js';
|
|
29
|
+
import { parseFull } from '@imports/castle.physics-2d/engine/pxart';
|
|
30
|
+
import { drawMotionPage } from './filmstrip.js';
|
|
31
|
+
import { imageArt } from '@imports/castle.physics-2d/engine/art';
|
|
32
|
+
|
|
33
|
+
const DEG = Math.PI / 180;
|
|
34
|
+
|
|
35
|
+
// The space decks' camera: a 15 degree lens looking down 45 degrees from yaw
|
|
36
|
+
// 45, framing 13 m of ground from top to bottom of the card.
|
|
37
|
+
const GAME_FOV = 15;
|
|
38
|
+
const GAME_VIEW_HEIGHT = 13;
|
|
39
|
+
|
|
40
|
+
const HEADER_PX = 82;
|
|
41
|
+
const LABEL_PX = 16;
|
|
42
|
+
const PAGES_PER_MODEL = 3;
|
|
43
|
+
|
|
44
|
+
// yaw is the camera's bearing around the model: 0 is in front of it (+z), 90
|
|
45
|
+
// is on its right (+x). The game camera sits at yaw -45.
|
|
46
|
+
const PAGES = [
|
|
47
|
+
[{ label: 'in game (iso45, game size)', yaw: -45, pitch: 45, game: true }],
|
|
48
|
+
[
|
|
49
|
+
{ label: 'overhead yaw 45 pitch 45', yaw: -45, pitch: 45 },
|
|
50
|
+
{ label: 'front', yaw: 0, pitch: 8 },
|
|
51
|
+
{ label: 'back', yaw: 180, pitch: 8 },
|
|
52
|
+
{ label: 'right side (+x)', yaw: 90, pitch: 8 },
|
|
53
|
+
],
|
|
54
|
+
[
|
|
55
|
+
{ label: 'left side (-x)', yaw: -90, pitch: 8 },
|
|
56
|
+
{ label: 'top', yaw: 0, pitch: 89.5 },
|
|
57
|
+
{ label: '3/4 above front-right', yaw: 40, pitch: 32 },
|
|
58
|
+
{ label: '3/4 above back-left', yaw: -140, pitch: 32 },
|
|
59
|
+
],
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
// Vite caches an imported module across the script's rewrites of this file, so
|
|
63
|
+
// it is fetched as a plain file on each load.
|
|
64
|
+
async function readTarget() {
|
|
65
|
+
const response = await fetch(`/target/sheet.json?at=${Date.now()}`, { cache: 'no-store' });
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
throw new Error(`target/sheet.json: ${response.status}`);
|
|
68
|
+
}
|
|
69
|
+
return response.json();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The sprites map the kit's material code looks refs up in, keyed by the ref
|
|
73
|
+
// exactly as the model writes it (without a `#frame` suffix).
|
|
74
|
+
async function loadSprites(arts) {
|
|
75
|
+
const sprites = {};
|
|
76
|
+
const waits = [];
|
|
77
|
+
for (const [ref, art] of Object.entries(arts ?? {})) {
|
|
78
|
+
if (art.kind === 'image') {
|
|
79
|
+
const image = imageArt(ref, art.dataUrl);
|
|
80
|
+
sprites[ref] = image;
|
|
81
|
+
waits.push(image.image.decode().catch(() => {}));
|
|
82
|
+
} else {
|
|
83
|
+
sprites[ref] = parseFull(art.text);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
await Promise.all(waits);
|
|
87
|
+
return sprites;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function addLights(scene, bounds) {
|
|
91
|
+
scene.add(new THREE.HemisphereLight(0xc8d0d8, 0x3a3530, 0.62 * Math.PI));
|
|
92
|
+
scene.add(new THREE.AmbientLight(0xffffff, 0.24));
|
|
93
|
+
|
|
94
|
+
// Same direction and strength as the game's sun. The shadow camera covers
|
|
95
|
+
// only this model's bounds; a smaller shadow area is a sharper shadow.
|
|
96
|
+
const center = bounds.getCenter(new THREE.Vector3());
|
|
97
|
+
const radius = Math.max(1, bounds.getSize(new THREE.Vector3()).length());
|
|
98
|
+
const sun = new THREE.DirectionalLight(0xffffff, 1.15);
|
|
99
|
+
sun.position
|
|
100
|
+
.copy(center)
|
|
101
|
+
.add(new THREE.Vector3(6, 18, -6).normalize().multiplyScalar(radius * 4));
|
|
102
|
+
sun.target.position.copy(center);
|
|
103
|
+
sun.castShadow = true;
|
|
104
|
+
sun.shadow.intensity = 0.65;
|
|
105
|
+
sun.shadow.mapSize.set(2048, 2048);
|
|
106
|
+
sun.shadow.camera.near = 0.1;
|
|
107
|
+
sun.shadow.camera.far = radius * 10;
|
|
108
|
+
sun.shadow.camera.left = -radius * 1.5;
|
|
109
|
+
sun.shadow.camera.right = radius * 1.5;
|
|
110
|
+
sun.shadow.camera.top = radius * 1.5;
|
|
111
|
+
sun.shadow.camera.bottom = -radius * 1.5;
|
|
112
|
+
sun.shadow.normalBias = 0.02;
|
|
113
|
+
scene.add(sun, sun.target);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function addGround(scene) {
|
|
117
|
+
const ground = new THREE.Mesh(
|
|
118
|
+
new THREE.PlaneGeometry(60, 60),
|
|
119
|
+
new THREE.MeshStandardMaterial({ color: 0x5f6b58, dithering: true })
|
|
120
|
+
);
|
|
121
|
+
ground.rotation.x = -Math.PI / 2;
|
|
122
|
+
ground.position.y = -0.005;
|
|
123
|
+
ground.receiveShadow = true;
|
|
124
|
+
scene.add(ground);
|
|
125
|
+
|
|
126
|
+
// One line per meter.
|
|
127
|
+
const grid = new THREE.GridHelper(60, 60, 0x7c8872, 0x7c8872);
|
|
128
|
+
grid.material.transparent = true;
|
|
129
|
+
grid.material.opacity = 0.5;
|
|
130
|
+
scene.add(grid);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Unit vector from the model toward the camera.
|
|
134
|
+
function viewDirection(view) {
|
|
135
|
+
const yaw = view.yaw * DEG;
|
|
136
|
+
const pitch = view.pitch * DEG;
|
|
137
|
+
return new THREE.Vector3(
|
|
138
|
+
Math.sin(yaw) * Math.cos(pitch),
|
|
139
|
+
Math.sin(pitch),
|
|
140
|
+
Math.cos(yaw) * Math.cos(pitch)
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// A camera that fits the model's bounding sphere in the tile.
|
|
145
|
+
function fitCamera(view, bounds, aspect) {
|
|
146
|
+
const center = bounds.getCenter(new THREE.Vector3());
|
|
147
|
+
const radius = Math.max(0.15, bounds.getSize(new THREE.Vector3()).length() / 2);
|
|
148
|
+
const fov = 24;
|
|
149
|
+
const fitVertical = radius / Math.sin((fov / 2) * DEG);
|
|
150
|
+
const fitHorizontal = radius / Math.sin(Math.atan(Math.tan((fov / 2) * DEG) * aspect));
|
|
151
|
+
const distance = Math.max(fitVertical, fitHorizontal) * 1.02;
|
|
152
|
+
const camera = new THREE.PerspectiveCamera(fov, aspect, distance / 50, distance * 4);
|
|
153
|
+
camera.position.copy(center).add(viewDirection(view).multiplyScalar(distance));
|
|
154
|
+
camera.lookAt(center);
|
|
155
|
+
return camera;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// The game's own camera and distance over the whole card, aimed at the
|
|
159
|
+
// model's footprint the way the game aims at the player's feet.
|
|
160
|
+
function gameCamera(view, bounds, aspect) {
|
|
161
|
+
const center = bounds.getCenter(new THREE.Vector3());
|
|
162
|
+
center.y = bounds.min.y;
|
|
163
|
+
const distance = GAME_VIEW_HEIGHT / 2 / Math.tan((GAME_FOV / 2) * DEG);
|
|
164
|
+
const camera = new THREE.PerspectiveCamera(GAME_FOV, aspect, 0.5, 400);
|
|
165
|
+
camera.position.copy(center).add(viewDirection(view).multiplyScalar(distance));
|
|
166
|
+
camera.lookAt(center);
|
|
167
|
+
return camera;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function addText(text, left, top, width, height, align) {
|
|
171
|
+
const node = document.createElement('div');
|
|
172
|
+
node.className = 'sheet-text';
|
|
173
|
+
node.textContent = text;
|
|
174
|
+
Object.assign(node.style, {
|
|
175
|
+
position: 'absolute',
|
|
176
|
+
left: `${left}px`,
|
|
177
|
+
top: `${top}px`,
|
|
178
|
+
width: `${width}px`,
|
|
179
|
+
height: `${height}px`,
|
|
180
|
+
font: '11px/16px ui-monospace, Menlo, monospace',
|
|
181
|
+
color: '#f8f8f2',
|
|
182
|
+
background: 'rgba(40, 42, 54, 0.92)',
|
|
183
|
+
padding: '0 6px',
|
|
184
|
+
boxSizing: 'border-box',
|
|
185
|
+
whiteSpace: 'pre',
|
|
186
|
+
overflow: 'hidden',
|
|
187
|
+
textAlign: align,
|
|
188
|
+
});
|
|
189
|
+
document.body.appendChild(node);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function clearText() {
|
|
193
|
+
for (const node of document.querySelectorAll('.sheet-text')) {
|
|
194
|
+
node.remove();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function describe(entry) {
|
|
199
|
+
const size = entry.bounds.getSize(new THREE.Vector3());
|
|
200
|
+
const dimensions = [size.x, size.y, size.z].map((value) => value.toFixed(2)).join(' x ');
|
|
201
|
+
return `${entry.source}\n${entry.model.parts.length} parts w x h x d ${dimensions} m min y ${entry.bounds.min.y.toFixed(2)}\n${describeCost(entry)}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// The game cost, from the same optimized build a game makes. Outlines draw
|
|
205
|
+
// their part's triangles a second time.
|
|
206
|
+
function describeCost(entry) {
|
|
207
|
+
const cost = entry.cost;
|
|
208
|
+
const budget = entry.model.triangles ?? DEFAULT_TRIANGLES;
|
|
209
|
+
const calls = [
|
|
210
|
+
`${cost.drawCalls} draw calls`,
|
|
211
|
+
`${cost.shadowCasters} shadow casters`,
|
|
212
|
+
`${cost.materials} materials`,
|
|
213
|
+
`built in ${Math.round(entry.buildMs)} ms`,
|
|
214
|
+
];
|
|
215
|
+
return `${cost.triangles} triangles of ${budget} budget, +${cost.outlineTriangles} in outlines\n${calls.join(' ')}\nheaviest: ${describeHeaviest(entry.partTriangles)}`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// The five parts with the most triangles, e.g. `sails 9.1k, tower 640`.
|
|
219
|
+
function describeHeaviest(partTriangles) {
|
|
220
|
+
const top = [...partTriangles].sort((a, b) => b[1] - a[1]).slice(0, 5);
|
|
221
|
+
const count = (n) => (n >= 1000 ? `${(n / 1000).toFixed(1)}k` : `${Math.round(n)}`);
|
|
222
|
+
return top.map(([id, triangles]) => `${id} ${count(triangles)}`).join(', ') || 'none';
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Triangles per top-level part, from a build without merging, where every
|
|
226
|
+
// mesh still sits under its part's group, or under a holder marked `copyOf`
|
|
227
|
+
// for a copy from `repeat` or `mirror`. A referenced model counts as the part
|
|
228
|
+
// that references it.
|
|
229
|
+
function trianglesPerPart(node) {
|
|
230
|
+
const totals = new Map();
|
|
231
|
+
const idOf = (object) => object.userData.partId ?? object.userData.copyOf;
|
|
232
|
+
node.traverseVisible((object) => {
|
|
233
|
+
if (!object.isMesh || object.userData.lookOutline) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
let owner = object.parent;
|
|
237
|
+
while (owner && owner !== node && idOf(owner) === undefined) {
|
|
238
|
+
owner = owner.parent;
|
|
239
|
+
}
|
|
240
|
+
const id = owner && idOf(owner);
|
|
241
|
+
if (id === undefined) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const index = object.geometry.getIndex();
|
|
245
|
+
const vertices = index ? index.count : object.geometry.getAttribute('position').count;
|
|
246
|
+
const instances = object.isInstancedMesh ? object.count : 1;
|
|
247
|
+
totals.set(id, (totals.get(id) ?? 0) + (vertices / 3) * instances);
|
|
248
|
+
});
|
|
249
|
+
return totals;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function makeRenderer(width, height) {
|
|
253
|
+
const canvas = document.createElement('canvas');
|
|
254
|
+
Object.assign(canvas.style, {
|
|
255
|
+
position: 'absolute',
|
|
256
|
+
left: '0px',
|
|
257
|
+
top: '0px',
|
|
258
|
+
width: `${width}px`,
|
|
259
|
+
height: `${height}px`,
|
|
260
|
+
});
|
|
261
|
+
document.body.appendChild(canvas);
|
|
262
|
+
const renderer = new THREE.WebGLRenderer({
|
|
263
|
+
canvas,
|
|
264
|
+
antialias: true,
|
|
265
|
+
preserveDrawingBuffer: true,
|
|
266
|
+
});
|
|
267
|
+
renderer.setPixelRatio(window.devicePixelRatio);
|
|
268
|
+
renderer.setSize(width, height, false);
|
|
269
|
+
renderer.shadowMap.enabled = true;
|
|
270
|
+
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
|
|
271
|
+
renderer.setScissorTest(true);
|
|
272
|
+
renderer.setClearColor(0x1d1f27);
|
|
273
|
+
return renderer;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function buildEntry(sheet) {
|
|
277
|
+
const model = parseModel(sheet.model);
|
|
278
|
+
if (!model) {
|
|
279
|
+
return { source: sheet.source, model: null };
|
|
280
|
+
}
|
|
281
|
+
const sprites = await loadSprites(sheet.arts);
|
|
282
|
+
const scene = new THREE.Scene();
|
|
283
|
+
const started = performance.now();
|
|
284
|
+
const node = buildModelNode(model, sprites, sheet.models, sheet.source, [sheet.source], {
|
|
285
|
+
optimize: true,
|
|
286
|
+
});
|
|
287
|
+
const buildMs = performance.now() - started;
|
|
288
|
+
scene.add(node);
|
|
289
|
+
node.updateMatrixWorld(true);
|
|
290
|
+
const bounds = visibleBounds(node);
|
|
291
|
+
addLights(scene, bounds);
|
|
292
|
+
addGround(scene);
|
|
293
|
+
const cost = modelCost(node);
|
|
294
|
+
|
|
295
|
+
// Merged batches hide which part a triangle came from. A game build gives
|
|
296
|
+
// referenced models an even share of the budget; this build is handed the
|
|
297
|
+
// same share.
|
|
298
|
+
const budgeted = { ...model, triangles: model.triangles ?? DEFAULT_TRIANGLES };
|
|
299
|
+
const plain = buildModelNode(budgeted, sprites, sheet.models, sheet.source, [sheet.source], {
|
|
300
|
+
budget: referenceBudget(budgeted),
|
|
301
|
+
});
|
|
302
|
+
plain.updateMatrixWorld(true);
|
|
303
|
+
const partTriangles = trianglesPerPart(plain);
|
|
304
|
+
return {
|
|
305
|
+
source: sheet.source,
|
|
306
|
+
model,
|
|
307
|
+
scene,
|
|
308
|
+
node,
|
|
309
|
+
bounds,
|
|
310
|
+
cost,
|
|
311
|
+
partTriangles,
|
|
312
|
+
buildMs,
|
|
313
|
+
motion: sheet.motion,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Draw one view into a rectangle given in CSS pixels from the top left.
|
|
318
|
+
function drawView(renderer, entry, view, rect, cardHeight) {
|
|
319
|
+
const aspect = rect.width / rect.height;
|
|
320
|
+
const camera = view.game
|
|
321
|
+
? gameCamera(view, entry.bounds, aspect)
|
|
322
|
+
: fitCamera(view, entry.bounds, aspect);
|
|
323
|
+
|
|
324
|
+
// WebGL viewports count from the bottom left.
|
|
325
|
+
const bottom = cardHeight - (rect.top + rect.height);
|
|
326
|
+
renderer.setViewport(rect.left, bottom, rect.width, rect.height);
|
|
327
|
+
renderer.setScissor(rect.left, bottom, rect.width, rect.height);
|
|
328
|
+
renderer.render(entry.scene, camera);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function drawPage(renderer, entries, pageNumber) {
|
|
332
|
+
const width = window.innerWidth;
|
|
333
|
+
const height = window.innerHeight;
|
|
334
|
+
const entry = entries[Math.floor(pageNumber / PAGES_PER_MODEL)];
|
|
335
|
+
const page = PAGES[pageNumber % PAGES_PER_MODEL];
|
|
336
|
+
clearText();
|
|
337
|
+
renderer.setViewport(0, 0, width, height);
|
|
338
|
+
renderer.setScissor(0, 0, width, height);
|
|
339
|
+
renderer.clear();
|
|
340
|
+
if (!entry) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if (!entry.model) {
|
|
344
|
+
addText(`not a pxmodel: ${entry.source}`, 0, 0, width, HEADER_PX, 'left');
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// `--motion` draws filmstrip pages (filmstrip.js) in place of the views.
|
|
349
|
+
if (entry.motion) {
|
|
350
|
+
const text = (content, left, top, w, h) => addText(content, left, top, w, h, 'left');
|
|
351
|
+
const header = describe(entry);
|
|
352
|
+
drawMotionPage(renderer, entry, pageNumber % PAGES_PER_MODEL, { width, height, header, text });
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (page.length === 1) {
|
|
356
|
+
drawView(renderer, entry, page[0], { left: 0, top: 0, width, height }, height);
|
|
357
|
+
addText(`${describe(entry)}\n${page[0].label}`, 0, 0, width, HEADER_PX + LABEL_PX, 'left');
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// A 2 x 2 grid with a one pixel gutter.
|
|
362
|
+
const tileWidth = Math.floor(width / 2);
|
|
363
|
+
const tileHeight = Math.floor(height / 2);
|
|
364
|
+
page.forEach((view, index) => {
|
|
365
|
+
const left = (index % 2) * tileWidth;
|
|
366
|
+
const top = Math.floor(index / 2) * tileHeight;
|
|
367
|
+
const rect = {
|
|
368
|
+
left: left + 1,
|
|
369
|
+
top: top + LABEL_PX,
|
|
370
|
+
width: tileWidth - 2,
|
|
371
|
+
height: tileHeight - LABEL_PX - 1,
|
|
372
|
+
};
|
|
373
|
+
drawView(renderer, entry, view, rect, height);
|
|
374
|
+
addText(view.label, left + 1, top, tileWidth - 2, LABEL_PX, 'center');
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async function main() {
|
|
379
|
+
setup();
|
|
380
|
+
document.body.dataset.castleScreenshotTarget = 'viewport';
|
|
381
|
+
Object.assign(document.body.style, { margin: '0', background: '#1d1f27', overflow: 'hidden' });
|
|
382
|
+
try {
|
|
383
|
+
const target = await readTarget();
|
|
384
|
+
const entries = [];
|
|
385
|
+
for (const sheet of target.sheets) {
|
|
386
|
+
entries.push(await buildEntry(sheet));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// The header also goes to the console, where render.mjs prints it.
|
|
390
|
+
for (const entry of entries) {
|
|
391
|
+
if (entry.model) {
|
|
392
|
+
const budget = entry.model.triangles ?? DEFAULT_TRIANGLES;
|
|
393
|
+
console.log(`render-sheet ${JSON.stringify({ source: entry.source, header: describe(entry), triangles: entry.cost.triangles, budget })}`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const renderer = makeRenderer(window.innerWidth, window.innerHeight);
|
|
397
|
+
let pageNumber = 0;
|
|
398
|
+
drawPage(renderer, entries, pageNumber);
|
|
399
|
+
window.addEventListener('keydown', (event) => {
|
|
400
|
+
if (event.key === 'n') {
|
|
401
|
+
pageNumber += 1;
|
|
402
|
+
drawPage(renderer, entries, pageNumber);
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
} catch (error) {
|
|
406
|
+
addText(`sheet failed: ${error.message}`, 0, 0, window.innerWidth, HEADER_PX, 'left');
|
|
407
|
+
console.error(error);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
void main();
|