Fit the render camera to the geometry and repack the kit scene, then regenerate the images.
This commit is contained in:
parent
5d793c83d0
commit
b4aeccd2a2
6 changed files with 73 additions and 17 deletions
BIN
media/cover.png
BIN
media/cover.png
Binary file not shown.
|
Before Width: | Height: | Size: 164 KiB After Width: | Height: | Size: 198 KiB |
BIN
media/kit.png
BIN
media/kit.png
Binary file not shown.
|
Before Width: | Height: | Size: 146 KiB After Width: | Height: | Size: 194 KiB |
BIN
media/models.png
BIN
media/models.png
Binary file not shown.
|
Before Width: | Height: | Size: 493 KiB After Width: | Height: | Size: 582 KiB |
|
|
@ -94,7 +94,7 @@ const left = MARGIN + 20
|
||||||
const right = WIDTH - MARGIN - 20
|
const right = WIDTH - MARGIN - 20
|
||||||
|
|
||||||
fillText(card, archivo, '@medieval-kit', { x: left - 2, y: 60, size: 52, colour: TEXT })
|
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,
|
x: left, y: 92, size: 16, colour: DIM,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -369,7 +369,9 @@ function frameCamera(root: Object3D, width: number, height: number): { camera: P
|
||||||
const sphere = box.getBoundingSphere(new Sphere())
|
const sphere = box.getBoundingSphere(new Sphere())
|
||||||
const camera = new PerspectiveCamera(32, width / height, 0.01, 100)
|
const camera = new PerspectiveCamera(32, width / height, 0.01, 100)
|
||||||
// Three-quarter view: the one angle that shows two faces at once. Looking
|
// 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()
|
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
|
* inside a pixel at these sizes, and it settles on whichever axis is
|
||||||
* actually tight rather than assuming it is the vertical one.
|
* 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
|
let distance = (sphere.radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.12
|
||||||
const corner = new Vector3()
|
const at = new Vector3()
|
||||||
for (let pass = 0; pass < 3; pass += 1) {
|
for (let pass = 0; pass < 4; pass += 1) {
|
||||||
camera.position.copy(sphere.center).addScaledVector(direction, distance)
|
camera.position.copy(sphere.center).addScaledVector(direction, distance)
|
||||||
camera.lookAt(sphere.center)
|
camera.lookAt(sphere.center)
|
||||||
camera.updateMatrixWorld(true)
|
camera.updateMatrixWorld(true)
|
||||||
camera.updateProjectionMatrix()
|
camera.updateProjectionMatrix()
|
||||||
let worst = 0
|
let worst = 0
|
||||||
for (let i = 0; i < 8; i += 1) {
|
for (const p of points) {
|
||||||
corner.set(
|
at.copy(p).project(camera)
|
||||||
i & 1 ? box.max.x : box.min.x,
|
worst = Math.max(worst, Math.abs(at.x), Math.abs(at.y))
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
if (worst <= 0) break
|
if (worst <= 0) break
|
||||||
// 0.9 of the frame, so nothing sits on the edge and a shadow has room.
|
// 0.94 of the frame, so nothing sits on the edge and a shadow has room.
|
||||||
distance *= worst / 0.9
|
distance *= worst / 0.94
|
||||||
}
|
}
|
||||||
|
|
||||||
camera.position.copy(sphere.center).addScaledVector(direction, distance)
|
camera.position.copy(sphere.center).addScaledVector(direction, distance)
|
||||||
|
|
|
||||||
|
|
@ -385,9 +385,23 @@ function kitEntry(): Entry {
|
||||||
const box = new Box3().setFromObject(model.root)
|
const box = new Box3().setFromObject(model.root)
|
||||||
return { id, model, box, size: box.getSize(new Vector3()) }
|
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.
|
* 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,
|
(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 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 cursorX = 0
|
||||||
let cursorZ = 0
|
let cursorZ = 0
|
||||||
let rowDepth = 0
|
let rowDepth = 0
|
||||||
|
|
||||||
for (const item of placed) {
|
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
|
cursorX = 0
|
||||||
cursorZ += rowDepth + gap
|
cursorZ += rowDepth + gap
|
||||||
rowDepth = 0
|
rowDepth = 0
|
||||||
|
|
@ -433,6 +453,11 @@ function kitEntry(): Entry {
|
||||||
root.add(item.model.root)
|
root.add(item.model.root)
|
||||||
cursorX += item.size.x + gap
|
cursorX += item.size.x + gap
|
||||||
rowDepth = Math.max(rowDepth, item.size.z)
|
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.
|
// Centre the whole kit on the ORIGIN, otherwise the camera framing looks at a corner.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue