15.2 端到端构建导出
装配(15.1)把模块接起来,本节让整条链路跑一趟:从一批源资产,到一张交付物清单。BuildExporter.export() 是端到端的预演——
- 按资产类型选格式与压缩:纹理走平台格式(web→webp)、音频走平台编码(web→opus)、数据走 zstd(json→280 字节)。
- 模型可选 LOD:开 LOD 取最远档三角面为输出规模,不开走 gzip。
- 产出交付物清单:路径 / 格式 / 体积 / 清单指纹,汇总总字节。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 平台目标 | 12.1 平台目标与构建清单 | 清单是契约;本节实际执行导出 |
| 压缩与编码 | 12.2 压缩与编码 | 格式/压缩表;本节把它用在链路上 |
| LOD 生成 | 12.3 LOD 生成 | LOD 档位;本节把它接进模型导出 |
| 装配 | 15.1 装配资产管线 | 模块装配好;本节串链路 |
端到端 = 一条链,不是一段段孤岛
每个模块单独测过不够——纹理的格式、音频的编码、数据的压缩、模型的 LOD,要在一批资产里同时发生、产出同一个清单。这就是端到端的意义:链路把「单点正确」变成「整体正确」。
链路四步:
- 判类型:
compressChoice(type)决定压缩方法(纹理/音频→none,数据→zstd,其他→gzip)。 - 定格式:
exportFormat按类型给交付格式——纹理查平台格式、音频查平台编码、模型 glb。 - 算规模:
estimateCompressed(sourceSize, method)估压缩后体积;模型开 LOD 则取最远档三角面。 - 产清单:路径
out/<id>.<format>+ 清单指纹<id>:<size>+ 总字节。
端到端暴露的常见问题就是「装配不齐」:纹理算了压缩、模型忘了 LOD、音频格式写死。BuildExporter 用一张表把决策收敛——新增资产类型只改 exportFormat 一行,链路其余部分不动。
对标导出计划与实际产物
Unreal 的 Cook 为目标平台准备资产,Package 则生成分发形式。helio 示例按类型选择格式、估算体积并生成计划清单。id:size 是清单指纹,内容变化但体积相同时指纹保持一致。验证分发文件的完整性需对实际生成的字节计算摘要。 Unreal Build Operations
可运行产物:6 条用例
下面 src/build-export.ts 层级 L1,完整可编译,命令 node --test test/15-2-build-export.test.ts:
// file: src/build-export.ts
// tier: L1
// verify: node --test test/15-2-build-export.test.ts
//
// 资产管线(第十五章):装配——端到端构建导出。
// 把构建期的三个模块(压缩 12.2、LOD 12.3、清单语义)串成一条导出链路:
// 一批源资产 → 按类型选格式与压缩 → 模型可选 LOD → 产出交付物清单
// (路径/格式/体积/清单指纹)。这是从资产到交付物的闭环预演。
import { estimateCompressed, pickTextureFormat, pickAudioCodec, compressChoice } from './compress-encode.ts';
import { LodGroup } from './lod-generate.ts';
export interface ExportItem {
id: string;
type: string;
sourceSize: number;
platform: string;
}
export interface ExportOutput {
id: string;
path: string;
format: string;
sizeBytes: number;
manifestFingerprint: string;
}
export interface ExportResult {
outputs: ExportOutput[];
totalBytes: number;
}
/** 按资产类型定交付格式:纹理/音频走平台格式,模型 glb,其余用类型名。 */
export function exportFormat(item: ExportItem): string {
if (item.type === 'texture') return pickTextureFormat(item.platform);
if (item.type === 'audio') return pickAudioCodec(item.platform);
if (item.type === 'model') return 'glb';
return item.type;
}
export class BuildExporter {
/** 把一批资产导出为交付物清单;模型开 LOD 时取最远档三角面为输出规模。 */
export(items: ExportItem[], opts: { lod: boolean; lodSteps: number }): ExportResult {
const outputs: ExportOutput[] = [];
let total = 0;
for (const it of items) {
const method = compressChoice(it.type);
const format = exportFormat(it);
let size = estimateCompressed(it.sourceSize, method);
if (opts.lod && it.type === 'model') {
const lod = new LodGroup();
const levels = lod.build(it.sourceSize, opts.lodSteps);
size = levels[levels.length - 1].triangles;
}
const path = `out/${it.id}.${format}`;
outputs.push({ id: it.id, path, format, sizeBytes: size, manifestFingerprint: `${it.id}:${size}` });
total += size;
}
return { outputs, totalBytes: total };
}
}
exportFormat 是扩展点:纹理/音频查平台表,模型 glb,其余用类型名——新增资产类型在这里加一行。export 的循环就是链路本体:判类型 → 定格式 → 算规模 → 产清单。模型开 LOD 时 size 从「压缩后体积」换成「最远档三角面」——构建产物取最小档,运行时远处就画最少面。
可运行产物:6 条用例
下面是 test/15-2-build-export.test.ts 层级 L1,命令 node --test test/15-2-build-export.test.ts:
// file: test/15-2-build-export.test.ts
// tier: L1
// verify: node --test test/15-2-build-export.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { BuildExporter, exportFormat, type ExportItem } from '../src/build-export.ts';
const NOLOD = { lod: false, lodSteps: 3 };
test('纹理导出用平台格式(web → webp)', () => {
const ex = new BuildExporter();
const r = ex.export([{ id: 'tex', type: 'texture', sourceSize: 1000, platform: 'web' }], NOLOD);
assert.equal(r.outputs[0].format, 'webp');
assert.equal(r.outputs[0].path, 'out/tex.webp');
assert.equal(r.outputs[0].sizeBytes, 1000); // texture 走平台格式,无通用压缩
});
test('音频导出用平台编码(web → opus)', () => {
const ex = new BuildExporter();
const r = ex.export([{ id: 'music', type: 'audio', sourceSize: 2000, platform: 'web' }], NOLOD);
assert.equal(r.outputs[0].format, 'opus');
assert.equal(r.outputs[0].path, 'out/music.opus');
});
test('数据导出 zstd 压缩', () => {
const ex = new BuildExporter();
const r = ex.export([{ id: 'cfg', type: 'json', sourceSize: 1000, platform: 'web' }], NOLOD);
assert.equal(r.outputs[0].format, 'json');
assert.equal(r.outputs[0].sizeBytes, 280); // estimateCompressed(1000, 'zstd')
});
test('模型开 LOD → 最远档三角面为输出规模', () => {
const ex = new BuildExporter();
const r = ex.export([{ id: 'm', type: 'model', sourceSize: 1000, platform: 'desktop' }], { lod: true, lodSteps: 3 });
assert.equal(r.outputs[0].format, 'glb');
assert.equal(r.outputs[0].path, 'out/m.glb');
assert.equal(r.outputs[0].sizeBytes, 250); // 1.0→0.5→0.25,最远档 250 面
});
test('模型不开 LOD → gzip 压缩后大小', () => {
const ex = new BuildExporter();
const r = ex.export([{ id: 'm', type: 'model', sourceSize: 1000, platform: 'desktop' }], NOLOD);
assert.equal(r.outputs[0].sizeBytes, 350); // estimateCompressed(1000, 'gzip')
});
test('输出唯一、清单指纹格式与 totalBytes 汇总', () => {
const ex = new BuildExporter();
const items: ExportItem[] = [
{ id: 'tex', type: 'texture', sourceSize: 1000, platform: 'web' },
{ id: 'cfg', type: 'json', sourceSize: 1000, platform: 'web' },
];
const r = ex.export(items, NOLOD);
assert.equal(r.outputs.length, 2);
assert.equal(r.outputs[0].path, 'out/tex.webp');
assert.equal(r.outputs[1].path, 'out/cfg.json');
assert.equal(r.outputs[0].manifestFingerprint, 'tex:1000');
assert.equal(r.outputs[1].manifestFingerprint, 'cfg:280');
assert.equal(r.totalBytes, 1280);
assert.equal(exportFormat({ id: 'm', type: 'model', sourceSize: 1, platform: 'web' }), 'glb');
});
本机实测(Node v26.5.0,node --test test/15-2-build-export.test.ts):tests 6、pass 6、fail 0,耗时约 40 ms。
第一、二条钉「纹理/音频走平台格式」——out/tex.webp、out/music.opus,不是通用压缩;第三条钉「数据 zstd」——cfg:280;第四、五条钉「LOD 开/关」——开则 250(最远档)、关则 350(gzip);第六条钉「汇总」——两项 1000 + 280 = 1280,清单指纹 id:size 逐项精确。
小结
- 端到端 = 一条链:判类型 → 定格式 → 算规模 → 产清单。
- 纹理/音频走平台格式编码,数据走 zstd,模型走 glb。
- 模型开 LOD 取最远档三角面,构建产物即运行时最省档。
- 清单指纹用于核对记录中的资产标识与估算体积。
- 下一节把整卷收口:卷二能力回顾——14 项能力可枚举。
阅读导航:上一节:15.1 装配资产管线 · 下一节:15.3 卷二能力回顾 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。