11.3 3D 预览取景
3D 资产的缩略图不是截个图标——是把相机摆到能看见整个模型的位置拍一张。本节的 model-preview.ts 做取景数学:算出模型占多大、中心在哪、相机站多远刚好罩住。
boundsOf(positions):顶点列表的包围盒;空列表返回 null。center(bounds)/boundingRadius(bounds):包围盒中心与包围球半径。fitCameraDistance(bounds, fovDeg):把包围球放进画面所需距离,留 20% 余量。framing(bounds, fovDeg):取景框架——中心 + 距离。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 模型解析 | 5.1 glTF 解析与模型导入 | 预览消费 glTF 解析出的节点树(5.2 有骨架) |
| 骨骼蒙皮 | 5.2 骨骼与蒙皮 | 有蒙皮的角色,预览相机要罩住整个骨架 |
| 缩略图 | 11.1 缩略图生成与缓存 | 3D 预览是 model 类型资产的缩略图生成器 |
取景 = 包围盒 + 相机框架
「拍一张好缩略图」拆成三个数学问题:
- 模型在哪(
boundsOf):所有顶点取 min/max,得到轴对齐包围盒(AABB)。 - 中心在哪(
center):包围盒中点——相机看向这里。 - 站多远(
fitCameraDistance):把包围球放进画面。球半径r、视野fov下,相机距离 =r / sin(fov/2),再乘 1.2 留余量——不能让模型顶到画面边缘。
视锥边界与球面相切时,球心到边界的垂距等于半径。相机到球心的距离是直角三角形的斜边,因此最小距离为半径除以半视角的正弦。示例在此基础上增加 20% 距离。
取景公式的适用范围
本节按透视视锥与包围球相切计算距离。应使用水平和垂直视野中较小的半角。窄预览框如果只传垂直视野,模型仍可能在水平方向被裁剪。实际相机还需设置近远裁剪面,并处理半径为零的对象。
可运行产物:6 条用例
下面 src/model-preview.ts 层级 L1,完整可编译,命令 node --test test/11-3-model-preview.test.ts:
// file: src/model-preview.ts
// tier: L1
// verify: node --test test/11-3-model-preview.test.ts
//
// 资产管线(第十一章):缩略图与预览——3D 预览取景。
// 3D 资产的缩略图不是截个图标,是「把相机摆到能看见整个模型的位置拍一
// 张」。ModelPreview 做取景数学:boundOf 算包围盒、center 找中心、
// fitCameraDistance 按限制方向的视野把模型放进画面(留 20% 余量)。
// 3D 预览 = 包围盒 + 相机框架,其余是渲染器的事。
export interface Vec3 {
x: number;
y: number;
z: number;
}
export interface Bounds {
min: Vec3;
max: Vec3;
}
/** 顶点列表的包围盒;空列表返回 null。 */
export function boundsOf(positions: Vec3[]): Bounds | null {
if (positions.length === 0) return null;
let min = { ...positions[0] };
let max = { ...positions[0] };
for (const p of positions) {
if (p.x < min.x) min.x = p.x;
if (p.y < min.y) min.y = p.y;
if (p.z < min.z) min.z = p.z;
if (p.x > max.x) max.x = p.x;
if (p.y > max.y) max.y = p.y;
if (p.z > max.z) max.z = p.z;
}
return { min, max };
}
/** 包围盒中心。 */
export function center(bounds: Bounds): Vec3 {
return {
x: (bounds.min.x + bounds.max.x) / 2,
y: (bounds.min.y + bounds.max.y) / 2,
z: (bounds.min.z + bounds.max.z) / 2,
};
}
/** 包围球半径(半对角线)。 */
export function boundingRadius(bounds: Bounds): number {
const dx = bounds.max.x - bounds.min.x;
const dy = bounds.max.y - bounds.min.y;
const dz = bounds.max.z - bounds.min.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz) / 2;
}
/**
* 把包围球放进画面所需的最小相机距离:
* 半径 / sin(fov/2),留 20% 余量。
*/
export function fitCameraDistance(bounds: Bounds, fovDeg: number): number {
const r = boundingRadius(bounds);
if (!Number.isFinite(fovDeg) || fovDeg <= 0 || fovDeg >= 180) {
throw new RangeError("视野必须在 0 到 180 度之间");
}
const halfFov = (fovDeg * Math.PI) / 360;
return (r / Math.sin(halfFov)) * 1.2;
}
/** 取景框架:相机看向中心,距离刚好罩住模型。 */
export function framing(bounds: Bounds, fovDeg: number): { center: Vec3; distance: number } {
return { center: center(bounds), distance: fitCameraDistance(bounds, fovDeg) };
}
boundsOf 的退化情形:单顶点包围盒的半径为 0 → 相机距离 0——相机就贴在模型上。这在真实场景要钳制一个最小距离,但语义上「单点没有尺寸」是对的(测试第六条钉住)。
可运行产物:6 条用例
下面是 test/11-3-model-preview.test.ts 层级 L1,命令 node --test test/11-3-model-preview.test.ts:
// file: test/11-3-model-preview.test.ts
// tier: L1
// verify: node --test test/11-3-model-preview.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { boundsOf, center, boundingRadius, fitCameraDistance, framing, type Vec3 } from '../src/model-preview.ts';
test('boundsOf 算包围盒', () => {
const pts: Vec3[] = [
{ x: -2, y: -1, z: 0 },
{ x: 2, y: 3, z: 4 },
{ x: 0, y: 0, z: 0 },
];
const b = boundsOf(pts)!;
assert.deepEqual(b.min, { x: -2, y: -1, z: 0 });
assert.deepEqual(b.max, { x: 2, y: 3, z: 4 });
});
test('空顶点列表 → null', () => {
assert.equal(boundsOf([]), null);
});
test('center 与 boundingRadius', () => {
const b = boundsOf([{ x: -2, y: -1, z: 0 }, { x: 2, y: 3, z: 4 }])!;
assert.deepEqual(center(b), { x: 0, y: 1, z: 2 });
assert.equal(boundingRadius(b), Math.sqrt(16 + 16 + 16) / 2);
});
test('fitCameraDistance:视野越大距离越近,含 20% 余量', () => {
const b = boundsOf([{ x: 0, y: 0, z: 0 }, { x: 0, y: 2, z: 0 }])!; // 半径 1
const far = fitCameraDistance(b, 30);
const near = fitCameraDistance(b, 60);
assert.ok(near < far);
// 半径 1、30° 视野:sin(15°)≈0.259 → 距离约 4.64
assert.ok(Math.abs(far - 1 / Math.sin((15 * Math.PI) / 180) * 1.2) < 0.01);
});
test('framing 返回中心与距离', () => {
const b = boundsOf([{ x: -1, y: -1, z: -1 }, { x: 1, y: 1, z: 1 }])!;
const f = framing(b, 45);
assert.deepEqual(f.center, { x: 0, y: 0, z: 0 });
assert.ok(f.distance > 0);
});
test('单点包围盒:中心即点,半径为 0', () => {
const b = boundsOf([{ x: 5, y: 5, z: 5 }])!;
assert.deepEqual(center(b), { x: 5, y: 5, z: 5 });
assert.equal(boundingRadius(b), 0);
assert.equal(fitCameraDistance(b, 45), 0); // 半径为 0 → 距离 0
});
本机实测(Node v26.5.0,node --test test/11-3-model-preview.test.ts):tests 6、pass 6、fail 0,耗时约 50 ms。
第四条是最「取景」的一条:视野变大距离变近,且数值和手算公式(注释里的算式)逐位一致。第五条 framing 返回中心 + 距离的框架。第六条钉住单点退化(半径 0)。
小结
- 3D 预览 = 包围盒 + 相机框架。
boundsOf取 AABB、center取中心、fitCameraDistance=r / sin(fov/2) × 1.2。- 乘 1.2 是缩略图的边距,模型不顶画面边缘。
- 旋转/轨道是渲染层的事,取景数学是共同底座。
- 下一节离开编辑器内部,走向交付:构建与导出——平台目标、压缩、LOD。
阅读导航:上一节:11.2 后台生成队列 · 下一节:12.1 平台目标与构建清单 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。