diff --git a/media/cover.png b/media/cover.png index e7acb6c..c354dfe 100644 Binary files a/media/cover.png and b/media/cover.png differ diff --git a/media/kit.png b/media/kit.png index e7ae833..d79b01a 100644 Binary files a/media/kit.png and b/media/kit.png differ diff --git a/media/models.png b/media/models.png index 1c01579..a131677 100644 Binary files a/media/models.png and b/media/models.png differ diff --git a/scripts/build-cover.ts b/scripts/build-cover.ts index 92bcd17..c4af2d8 100644 --- a/scripts/build-cover.ts +++ b/scripts/build-cover.ts @@ -94,7 +94,7 @@ const left = MARGIN + 20 const right = WIDTH - MARGIN - 20 fillText(card, archivo, '@medieval-kit', { x: left - 2, y: 60, size: 52, colour: TEXT }) -fillText(card, mono, 'Procedural lowpoly props for three.js. Install the source, not the asset.', { +fillText(card, mono, 'Procedural lowpoly props for three.js. The source installs into your project.', { x: left, y: 92, size: 16, colour: DIM, }) diff --git a/scripts/raster.ts b/scripts/raster.ts index f612ddd..5980fbc 100644 --- a/scripts/raster.ts +++ b/scripts/raster.ts @@ -369,7 +369,9 @@ function frameCamera(root: Object3D, width: number, height: number): { camera: P const sphere = box.getBoundingSphere(new Sphere()) const camera = new PerspectiveCamera(32, width / height, 0.01, 100) // Three-quarter view: the one angle that shows two faces at once. Looking - // straight on hides depth completely. + // straight on hides depth completely. Raising it to look further down was + // measured on the whole-kit scene, the one subject with a reason to want it, + // and it made the frame emptier at every elevation tried. const direction = new Vector3(0.78, 0.5, 1).normalize() /** @@ -388,25 +390,54 @@ function frameCamera(root: Object3D, width: number, height: number): { camera: P * inside a pixel at these sizes, and it settles on whichever axis is * actually tight rather than assuming it is the vertical one. */ + /** + * Fitted to the GEOMETRY, not to the eight corners of its bounding box. + * + * A box fit is exact only for something that fills its box. The whole-kit + * scene is the opposite: a flat plan of small props with one 7 m mill in it, + * so four of the eight corners are pure air, and the frame was sized to hold + * air. That is what reads as being zoomed out. + * + * Sampling the real vertices costs one traversal and removes the slack + * exactly where there is slack, without ever cropping — a vertex outside the + * frame is what the loop is correcting for. + */ + const points: Vector3[] = [] + root.updateMatrixWorld(true) + root.traverse((object) => { + if (!(object instanceof Mesh)) return + const position = object.geometry.getAttribute('position') + if (!position) return + // Every vertex on a small model, a sample on a large one: 20k points is + // plenty to find the extremes and keeps this off the critical path. + const step = Math.max(1, Math.floor(position.count / 4096)) + for (let i = 0; i < position.count; i += step) { + points.push(new Vector3().fromBufferAttribute(position, i).applyMatrix4(object.matrixWorld)) + } + }) + if (points.length === 0) for (let i = 0; i < 8; i += 1) { + points.push(new Vector3( + i & 1 ? box.max.x : box.min.x, + i & 2 ? box.max.y : box.min.y, + i & 4 ? box.max.z : box.min.z, + )) + } + let distance = (sphere.radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.12 - const corner = new Vector3() - for (let pass = 0; pass < 3; pass += 1) { + const at = new Vector3() + for (let pass = 0; pass < 4; pass += 1) { camera.position.copy(sphere.center).addScaledVector(direction, distance) camera.lookAt(sphere.center) camera.updateMatrixWorld(true) camera.updateProjectionMatrix() let worst = 0 - for (let i = 0; i < 8; i += 1) { - corner.set( - i & 1 ? box.max.x : box.min.x, - i & 2 ? box.max.y : box.min.y, - i & 4 ? box.max.z : box.min.z, - ).project(camera) - worst = Math.max(worst, Math.abs(corner.x), Math.abs(corner.y)) + for (const p of points) { + at.copy(p).project(camera) + worst = Math.max(worst, Math.abs(at.x), Math.abs(at.y)) } if (worst <= 0) break - // 0.9 of the frame, so nothing sits on the edge and a shadow has room. - distance *= worst / 0.9 + // 0.94 of the frame, so nothing sits on the edge and a shadow has room. + distance *= worst / 0.94 } camera.position.copy(sphere.center).addScaledVector(direction, distance) diff --git a/src/catalog.ts b/src/catalog.ts index 86fc4b5..4a8b505 100644 --- a/src/catalog.ts +++ b/src/catalog.ts @@ -385,9 +385,23 @@ function kitEntry(): Entry { const box = new Box3().setFromObject(model.root) return { id, model, box, size: box.getSize(new Vector3()) } }) - placed.sort((a, b) => b.size.y - a.size.y) + /** + * Sorted by DEPTH, which is the dimension a shelf's height is set by. + * + * It used to sort by the models' own height, and the two are unrelated: + * the post mill is 6.98 m deep and the ladder 0.06 m, so a row that + * happened to hold both was 6.98 m deep for all of them and the ladder + * sat at the front of six metres of empty floor. Three models shared that + * row with the mill. The camera then had to frame a plan mostly made of + * gaps, which is what "zoomed out" actually was. + * + * Sorting by depth makes every row about as deep as the things standing + * in it. Tall still lands at the back, because among these models the + * deep ones are the tall ones. + */ + placed.sort((a, b) => b.size.z - a.size.z) - const gap = 0.42 + const gap = 0.26 /** * The row width is MEASURED from the models, not fixed at 5.6 m. * @@ -411,13 +425,19 @@ function kitEntry(): Entry { (sum, item) => sum + (item.size.x + gap) * (item.size.z + gap), 0, ) const widest = placed.reduce((most, item) => Math.max(most, item.size.x), 0) - const rowWidth = Math.max(widest, Math.sqrt(footprint) * 1.24) + const rowWidth = Math.max(widest, Math.sqrt(footprint) * 1.0) let cursorX = 0 let cursorZ = 0 let rowDepth = 0 for (const item of placed) { - if (cursorX > 0 && cursorX + item.size.x > rowWidth) { + // Anything as wide as most of a row takes the row to itself. The post + // mill is 6.6 m across and 7 m deep, so whatever shared its row stood + // at the front of seven metres of empty floor with the mill beside it + // and a gap between them the width of the mill. One item, one row, and + // the rest pack against each other instead of against it. + const oversized = item.size.x > rowWidth * 0.6 + if (cursorX > 0 && (oversized || cursorX + item.size.x > rowWidth)) { cursorX = 0 cursorZ += rowDepth + gap rowDepth = 0 @@ -433,6 +453,11 @@ function kitEntry(): Entry { root.add(item.model.root) cursorX += item.size.x + gap rowDepth = Math.max(rowDepth, item.size.z) + if (oversized) { + cursorX = 0 + cursorZ += rowDepth + gap + rowDepth = 0 + } } // Centre the whole kit on the ORIGIN, otherwise the camera framing looks at a corner.