| 44 | * @returns Blickpunkt, Kameraposition und die Pixel je Welteinheit am Pfad |
| 45 | */ |
| 46 | export function pathFrame(points: readonly Vec3[], view: { direction: Vec3; up: Vec3 }, viewport: { width: number; height: number }, |
| 47 | fovDegrees: number, inset: { left: number; top: number }): { lookAt: Vec3; position: Vec3; pixelsPerUnit: number } { |
| 48 | const direction = unit(view.direction); |
| 49 | const right = unit(cross(direction, view.up)); |
| 50 | const up = cross(right, direction); |
| 51 | const center = scale(add(...points), 1 / Math.max(1, points.length)); |
| 52 | const tangent = Math.tan((fovDegrees * Math.PI) / 360); |
| 53 | const freeWidth = Math.max(1, viewport.width - inset.left), freeHeight = Math.max(1, viewport.height - inset.top); |
| 54 | const halfX = Math.max(1, Math.min(freeWidth / 2 - PATH_FRAME_MARGIN.x, freeWidth * PATH_FRAME_FILL)); |
| 55 | const halfY = Math.max(1, Math.min(freeHeight / 2 - PATH_FRAME_MARGIN.y, freeHeight * PATH_FRAME_FILL)); |
| 56 | let pixelsPerUnit = viewport.height / (2 * PATH_FRAME_MIN_DISTANCE * tangent); |
| 57 | for (const point of points) { |
| 58 | const offset = sub(point, center); |
| 59 | const x = Math.abs(dot(offset, right)), y = Math.abs(dot(offset, up)); |
| 60 | if (x > 1e-6) pixelsPerUnit = Math.min(pixelsPerUnit, halfX / x); |
| 61 | if (y > 1e-6) pixelsPerUnit = Math.min(pixelsPerUnit, halfY / y); |
| 62 | } |
| 63 | const distance = viewport.height / (2 * pixelsPerUnit * tangent); |
| 64 | // Der Pfad steht in der Mitte des FREIEN Teils, nicht der ganzen Flaeche. |
| 65 | const shiftX = inset.left + freeWidth / 2 - viewport.width / 2, shiftY = inset.top + freeHeight / 2 - viewport.height / 2; |
| 66 | const lookAt = add(center, scale(right, -shiftX / pixelsPerUnit), scale(up, shiftY / pixelsPerUnit)); |
| 67 | return { lookAt, position: sub(lookAt, scale(direction, distance)), pixelsPerUnit }; |
| 68 | } |
| 69 | |
| 70 | const LABEL_STEPS = [0.5, 0.4, 0.6, 0.3, 0.7, 0.2, 0.8]; |
| 71 | const LABEL_PAD = 2; |