Aim the render camera at the picture's centre, include the contact shadow in the fit, and shoot the kit scene from the front.
This commit is contained in:
parent
b4aeccd2a2
commit
77b926312b
5 changed files with 91 additions and 18 deletions
BIN
media/cover.png
BIN
media/cover.png
Binary file not shown.
|
Before Width: | Height: | Size: 198 KiB After Width: | Height: | Size: 205 KiB |
BIN
media/kit.png
BIN
media/kit.png
Binary file not shown.
|
Before Width: | Height: | Size: 194 KiB After Width: | Height: | Size: 248 KiB |
BIN
media/models.png
BIN
media/models.png
Binary file not shown.
|
Before Width: | Height: | Size: 582 KiB After Width: | Height: | Size: 597 KiB |
|
|
@ -364,15 +364,16 @@ export function encodePng(frame: Frame): Uint8Array {
|
||||||
/* ------------------------------------------------------------------ framing */
|
/* ------------------------------------------------------------------ framing */
|
||||||
|
|
||||||
/** Camera that fits the model into frame. Same framing as in the viewer. */
|
/** Camera that fits the model into frame. Same framing as in the viewer. */
|
||||||
function frameCamera(root: Object3D, width: number, height: number): { camera: PerspectiveCamera; floor: number } {
|
function frameCamera(
|
||||||
|
root: Object3D,
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
towards: readonly [number, number, number],
|
||||||
|
): { camera: PerspectiveCamera; floor: number } {
|
||||||
const box = new Box3().setFromObject(root)
|
const box = new Box3().setFromObject(root)
|
||||||
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
|
const direction = new Vector3(towards[0], towards[1], towards[2]).normalize()
|
||||||
// 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()
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fit the BOX to the frame, not the sphere to the frame's short side.
|
* Fit the BOX to the frame, not the sphere to the frame's short side.
|
||||||
|
|
@ -415,6 +416,17 @@ function frameCamera(root: Object3D, width: number, height: number): { camera: P
|
||||||
points.push(new Vector3().fromBufferAttribute(position, i).applyMatrix4(object.matrixWorld))
|
points.push(new Vector3().fromBufferAttribute(position, i).applyMatrix4(object.matrixWorld))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
/**
|
||||||
|
* The contact shadow is part of the picture, so it is part of the fit.
|
||||||
|
*
|
||||||
|
* It is the geometry flattened onto the floor, which projects somewhere else
|
||||||
|
* entirely: a tall model throws its shadow well to one side, past everything
|
||||||
|
* the fit was measuring. Fourteen of the thirty-seven had a shadow running
|
||||||
|
* off the edge of its own cell, which reads as a crop even though no part of
|
||||||
|
* the model was touched.
|
||||||
|
*/
|
||||||
|
for (const p of points.slice()) points.push(new Vector3(p.x, box.min.y, p.z))
|
||||||
|
|
||||||
if (points.length === 0) for (let i = 0; i < 8; i += 1) {
|
if (points.length === 0) for (let i = 0; i < 8; i += 1) {
|
||||||
points.push(new Vector3(
|
points.push(new Vector3(
|
||||||
i & 1 ? box.max.x : box.min.x,
|
i & 1 ? box.max.x : box.min.x,
|
||||||
|
|
@ -423,25 +435,59 @@ function frameCamera(root: Object3D, width: number, height: number): { camera: P
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
let distance = (sphere.radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.12
|
/**
|
||||||
|
* Aimed at where the subject LANDS IN THE PICTURE, not at the middle of its
|
||||||
|
* bounding sphere.
|
||||||
|
*
|
||||||
|
* Those are the same point only for something symmetric about its centre.
|
||||||
|
* The whole-kit scene is a low wide plan with one tall mill standing off to
|
||||||
|
* one side, so its sphere centre sits well away from the middle of what you
|
||||||
|
* actually see, and the fit then padded one side to reach it. That is the
|
||||||
|
* margin down the right of the frame: not a fitting error, an aiming one.
|
||||||
|
*
|
||||||
|
* Centring and fitting run in the same loop because each one moves the other:
|
||||||
|
* re-aiming changes what the extremes project to, and pulling in changes
|
||||||
|
* where the centre falls. Four passes settles both inside a pixel here.
|
||||||
|
*/
|
||||||
|
const target = sphere.center.clone()
|
||||||
|
const right = new Vector3()
|
||||||
|
const up = new Vector3()
|
||||||
const at = new Vector3()
|
const at = new Vector3()
|
||||||
|
let distance = (sphere.radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.12
|
||||||
|
|
||||||
for (let pass = 0; pass < 4; pass += 1) {
|
for (let pass = 0; pass < 4; pass += 1) {
|
||||||
camera.position.copy(sphere.center).addScaledVector(direction, distance)
|
camera.position.copy(target).addScaledVector(direction, distance)
|
||||||
camera.lookAt(sphere.center)
|
camera.lookAt(target)
|
||||||
camera.updateMatrixWorld(true)
|
camera.updateMatrixWorld(true)
|
||||||
camera.updateProjectionMatrix()
|
camera.updateProjectionMatrix()
|
||||||
let worst = 0
|
|
||||||
|
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity
|
||||||
for (const p of points) {
|
for (const p of points) {
|
||||||
at.copy(p).project(camera)
|
at.copy(p).project(camera)
|
||||||
worst = Math.max(worst, Math.abs(at.x), Math.abs(at.y))
|
minX = Math.min(minX, at.x); maxX = Math.max(maxX, at.x)
|
||||||
|
minY = Math.min(minY, at.y); maxY = Math.max(maxY, at.y)
|
||||||
}
|
}
|
||||||
|
if (!Number.isFinite(minX)) break
|
||||||
|
|
||||||
|
// Slide the aim point across the image plane by however far off centre the
|
||||||
|
// content sits, in world units at this distance.
|
||||||
|
const halfHeight = Math.tan((camera.fov * Math.PI) / 360) * distance
|
||||||
|
const halfWidth = halfHeight * camera.aspect
|
||||||
|
right.setFromMatrixColumn(camera.matrixWorld, 0)
|
||||||
|
up.setFromMatrixColumn(camera.matrixWorld, 1)
|
||||||
|
target
|
||||||
|
.addScaledVector(right, ((minX + maxX) / 2) * halfWidth)
|
||||||
|
.addScaledVector(up, ((minY + maxY) / 2) * halfHeight)
|
||||||
|
|
||||||
|
// Then fit the half-extent, measured from that centre.
|
||||||
|
const worst = Math.max((maxX - minX) / 2, (maxY - minY) / 2)
|
||||||
if (worst <= 0) break
|
if (worst <= 0) break
|
||||||
// 0.94 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.94
|
distance *= worst / 0.94
|
||||||
}
|
}
|
||||||
|
|
||||||
camera.position.copy(sphere.center).addScaledVector(direction, distance)
|
camera.position.copy(target).addScaledVector(direction, distance)
|
||||||
camera.lookAt(sphere.center)
|
camera.lookAt(target)
|
||||||
camera.updateMatrixWorld(true)
|
camera.updateMatrixWorld(true)
|
||||||
camera.updateProjectionMatrix()
|
camera.updateProjectionMatrix()
|
||||||
return { camera, floor: box.min.y }
|
return { camera, floor: box.min.y }
|
||||||
|
|
@ -461,10 +507,20 @@ export interface RenderOptions {
|
||||||
/** Pre-rotation of the model around the Y axis (radians). For turntables. */
|
/** Pre-rotation of the model around the Y axis (radians). For turntables. */
|
||||||
readonly spin?: number
|
readonly spin?: number
|
||||||
readonly ground?: readonly number[]
|
readonly ground?: readonly number[]
|
||||||
|
/**
|
||||||
|
* Where the camera sits, as a direction from the subject.
|
||||||
|
*
|
||||||
|
* The default three-quarter view shows two faces of a single object at once,
|
||||||
|
* which is what a catalogue of single objects wants. A SCENE laid out in rows
|
||||||
|
* is a different subject: seen from the corner its rows run diagonally and
|
||||||
|
* two corners of the frame are left empty, so the whole-kit picture is taken
|
||||||
|
* from nearer the front, where the rows read as rows.
|
||||||
|
*/
|
||||||
|
readonly towards?: readonly [number, number, number]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderOne(id: string, options: RenderOptions): Frame {
|
export function renderOne(id: string, options: RenderOptions): Frame {
|
||||||
const { size, tall = options.size, patch, spin = 0, ground } = options
|
const { size, tall = options.size, patch, spin = 0, ground, towards = [0.78, 0.5, 1] } = options
|
||||||
const entry = CATALOG[id]
|
const entry = CATALOG[id]
|
||||||
if (!entry) throw new Error(`not in catalog: ${id}`)
|
if (!entry) throw new Error(`not in catalog: ${id}`)
|
||||||
const built = entry.build()
|
const built = entry.build()
|
||||||
|
|
@ -477,7 +533,7 @@ export function renderOne(id: string, options: RenderOptions): Frame {
|
||||||
built.root.rotation.y = spin
|
built.root.rotation.y = spin
|
||||||
const triangles = collect(built.root)
|
const triangles = collect(built.root)
|
||||||
const frame = newFrame(size, tall, ground)
|
const frame = newFrame(size, tall, ground)
|
||||||
const { camera, floor } = frameCamera(built.root, size, tall)
|
const { camera, floor } = frameCamera(built.root, size, tall, towards)
|
||||||
contactShadow(frame, camera, triangles, floor)
|
contactShadow(frame, camera, triangles, floor)
|
||||||
raster(frame, camera, triangles)
|
raster(frame, camera, triangles)
|
||||||
built.dispose()
|
built.dispose()
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,23 @@ const only = flag('ids')?.split(',')
|
||||||
const groundHex = flag('ground')
|
const groundHex = flag('ground')
|
||||||
const ground = groundHex ? toLinear(groundHex) : undefined
|
const ground = groundHex ? toLinear(groundHex) : undefined
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Where the camera stands, and why the whole-kit scene gets its own answer.
|
||||||
|
*
|
||||||
|
* A single model wants the three-quarter view: it shows two faces at once,
|
||||||
|
* where straight on hides depth completely. A SCENE of models laid out in rows
|
||||||
|
* is a different subject. From the corner its rows run diagonally across the
|
||||||
|
* frame and leave two corners empty; from nearer the front they read as rows,
|
||||||
|
* and the picture holds a quarter more of the kit at the same size.
|
||||||
|
*
|
||||||
|
* Applied here rather than left to the caller, because the alternative is a
|
||||||
|
* flag someone has to remember on every re-render, and the picture it produces
|
||||||
|
* is the one the README opens with.
|
||||||
|
*/
|
||||||
|
const towards = flag('towards')?.split(',').map(Number) as [number, number, number] | undefined
|
||||||
|
const cameraFor = (id: string): [number, number, number] | undefined =>
|
||||||
|
towards ?? (id === 'kit' ? [0.24, 0.5, 1] : undefined)
|
||||||
|
|
||||||
await mkdir(outDir, { recursive: true })
|
await mkdir(outDir, { recursive: true })
|
||||||
|
|
||||||
function ids0(): string {
|
function ids0(): string {
|
||||||
|
|
@ -52,7 +69,7 @@ if (angles > 1) {
|
||||||
const target = one ?? (only?.[0])
|
const target = one ?? (only?.[0])
|
||||||
if (!target) throw new Error('--angles needs --one <model> or --ids')
|
if (!target) throw new Error('--angles needs --one <model> or --ids')
|
||||||
const frames = Array.from({ length: angles }, (_, i) =>
|
const frames = Array.from({ length: angles }, (_, i) =>
|
||||||
renderOne(target, { size, ground, spin: (i / angles) * Math.PI * 2 }))
|
renderOne(target, { size, ground, towards: cameraFor(target), spin: (i / angles) * Math.PI * 2 }))
|
||||||
await writeFile(`${outDir}/_turntable.png`, encodePng(tile(frames, size, angles, ground)))
|
await writeFile(`${outDir}/_turntable.png`, encodePng(tile(frames, size, angles, ground)))
|
||||||
console.log(`${target} · ${angles} angles → ${outDir}/_turntable.png`)
|
console.log(`${target} · ${angles} angles → ${outDir}/_turntable.png`)
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
|
|
@ -63,7 +80,7 @@ if (sweep) {
|
||||||
const [key, values] = sweep.split('=')
|
const [key, values] = sweep.split('=')
|
||||||
const list = values!.split('|').map(Number)
|
const list = values!.split('|').map(Number)
|
||||||
const target = one ?? ids0()
|
const target = one ?? ids0()
|
||||||
const rendered = list.map((value) => renderOne(target, { size, ground, patch: { [key!]: value } }))
|
const rendered = list.map((value) => renderOne(target, { size, ground, towards: cameraFor(target), patch: { [key!]: value } }))
|
||||||
await writeFile(`${outDir}/_sweep.png`, encodePng(tile(rendered, size, list.length, ground)))
|
await writeFile(`${outDir}/_sweep.png`, encodePng(tile(rendered, size, list.length, ground)))
|
||||||
console.log(`${target} · ${key} = ${list.join(', ')} → ${outDir}/_sweep.png`)
|
console.log(`${target} · ${key} = ${list.join(', ')} → ${outDir}/_sweep.png`)
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
|
|
@ -78,7 +95,7 @@ const ids = one ? [one]
|
||||||
const frames = new Map<string, Frame>()
|
const frames = new Map<string, Frame>()
|
||||||
|
|
||||||
for (const id of ids) {
|
for (const id of ids) {
|
||||||
const frame = renderOne(id, { size, tall, ground })
|
const frame = renderOne(id, { size, tall, ground, towards: cameraFor(id) })
|
||||||
frames.set(id, frame)
|
frames.set(id, frame)
|
||||||
await writeFile(`${outDir}/${id}.png`, encodePng(frame))
|
await writeFile(`${outDir}/${id}.png`, encodePng(frame))
|
||||||
console.log(` ${id}`)
|
console.log(` ${id}`)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue