12.2 压缩与编码
构建清单(12.1)定了平台目标和策略,本节的 compress-encode.ts 把「格式/压缩」落成可查的表:三件事——
pickTextureFormat(platform):平台 → 纹理格式(web→webp、iOS→astc、Android→etc2、桌面→bc7)。pickAudioCodec(platform):平台 → 音频编码(web→opus、移动端→vorbis、桌面→pcm)。estimateCompressed(sizeBytes, method):方法 → 压缩后体积估计(gzip≈0.35、zstd≈0.28、brotli≈0.25)。compressChoice(assetType):资产类型 → 通用压缩方法(纹理/音频走平台格式、数据/JSON 走 zstd)。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 纹理设置 | 4.2 纹理设置与导入 | 导入时定参数,本节构建时定平台格式 |
| 音频设置 | 6.2 音频设置与导入 | 导入时定编码,本节构建时按平台换编码 |
| 平台矩阵 | 8.3 平台矩阵 | 选变体,本节选格式/编码 |
| 构建清单 | 12.1 平台目标与构建清单 | 清单定策略,本节执行压缩 |
压缩是「先想清楚,再动手」
压缩最容易犯的错是把通用压缩套到所有资产上——纹理用 gzip、音频用 zstd,结果是体积没小多少还拖垮解码。真实的规矩是分三层:
- 纹理/音频走平台专用格式(ASTC/ETC2/BC7、Opus/Vorbis)——GPU/硬件直接解码,不该再用通用压缩。
- 数据/JSON 走强压缩(zstd/brotli)——一次性解压,追求最小体积。
- 其他二进制走 gzip——折中。
compressChoice 的返回值里,纹理/音频是 'none'——不是「不压缩」,是压缩已由平台格式完成。体积估计 estimateCompressed 是构建前的预算工具:清单能不能进体积预算、超了就报警,先估后压。
对标资源编码与包压缩
Unity 的 Texture Importer 支持逐平台覆盖纹理格式等设置。单个资产的编码方式与分发包的整体压缩属于不同阶段。helio 示例根据平台和资产类型返回配置值,实际编码、目标设备能力验证和压缩收益测量由构建执行器负责。 Unity Texture Importer
可运行产物:6 条用例
下面 src/compress-encode.ts 层级 L1,完整可编译,命令 node --test test/12-2-compress-encode.test.ts:
// file: src/compress-encode.ts
// tier: L1
// verify: node --test test/12-2-compress-encode.test.ts
//
// 资产管线(第十二章):构建与导出——压缩与编码。
// 构建期的压缩是三件事:选纹理格式(平台 → 格式)、选音频编码(平台 →
// 编码)、估压缩后体积(方法 × 大小)。CompressEncode 把「平台 → 格式 →
// 体积」的决策做成可查的表,构建清单(12.1)据此把源资产压成目标格式。
// 纹理/音频走平台专用格式(不再通用压缩),数据/JSON 走 zstd。
export type CompressMethod = 'none' | 'gzip' | 'zstd' | 'brotli';
const RATIOS: Record<CompressMethod, number> = {
none: 1,
gzip: 0.35,
zstd: 0.28,
brotli: 0.25,
};
/** 估计某方法压缩后体积(取整字节)。 */
export function estimateCompressed(sizeBytes: number, method: CompressMethod): number {
return Math.round(sizeBytes * RATIOS[method]);
}
export interface TextureFormat {
name: string;
/** 是否支持透明通道。 */
alpha: boolean;
}
export const TEXTURE_FORMATS: Record<string, TextureFormat> = {
bc7: { name: 'bc7', alpha: true },
astc: { name: 'astc', alpha: true },
etc2: { name: 'etc2', alpha: true },
webp: { name: 'webp', alpha: true },
};
/** 某纹理格式是否支持透明通道。 */
export function alphaCapable(format: string): boolean {
return TEXTURE_FORMATS[format]?.alpha ?? false;
}
/** 平台默认纹理格式;未知平台兜底 webp。 */
export function pickTextureFormat(platform: string): string {
const map: Record<string, string> = {
web: 'webp',
ios: 'astc',
android: 'etc2',
desktop: 'bc7',
};
return map[platform] ?? 'webp';
}
/** 音频编码:web 用 opus,移动端用 vorbis,桌面兜底 pcm。 */
export function pickAudioCodec(platform: string): 'opus' | 'vorbis' | 'pcm' {
if (platform === 'web') return 'opus';
if (platform === 'ios' || platform === 'android') return 'vorbis';
return 'pcm';
}
/** 资产类型 → 通用压缩方法。 */
export function compressChoice(assetType: string): CompressMethod {
if (assetType === 'texture' || assetType === 'audio') return 'none';
if (assetType === 'data' || assetType === 'json') return 'zstd';
return 'gzip';
}
RATIOS 是压缩的先验表——真实构建里用实测压缩比替换,这里先给合理估算。alphaCapable 是纹理格式的能力查询:选格式前先确认它支持透明通道(UI 纹理带 alpha,格式选错就穿帮)。pickTextureFormat/pickAudioCodec 是平台映射,未知平台兜底 webp/pcm——新平台接入时给默认,而不是报错。
可运行产物:6 条用例
下面是 test/12-2-compress-encode.test.ts 层级 L1,命令 node --test test/12-2-compress-encode.test.ts:
// file: test/12-2-compress-encode.test.ts
// tier: L1
// verify: node --test test/12-2-compress-encode.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { estimateCompressed, pickTextureFormat, pickAudioCodec, compressChoice, alphaCapable } from '../src/compress-encode.ts';
test('estimateCompressed 方法排序', () => {
const none = estimateCompressed(1000, 'none');
const gzip = estimateCompressed(1000, 'gzip');
const zstd = estimateCompressed(1000, 'zstd');
const brotli = estimateCompressed(1000, 'brotli');
assert.ok(gzip < none);
assert.ok(zstd < gzip);
assert.ok(brotli < zstd);
});
test('estimateCompressed 精确数值', () => {
assert.equal(estimateCompressed(1000, 'none'), 1000);
assert.equal(estimateCompressed(1000, 'gzip'), 350);
assert.equal(estimateCompressed(1000, 'zstd'), 280);
});
test('pickTextureFormat 平台映射与未知兜底', () => {
assert.equal(pickTextureFormat('web'), 'webp');
assert.equal(pickTextureFormat('ios'), 'astc');
assert.equal(pickTextureFormat('android'), 'etc2');
assert.equal(pickTextureFormat('desktop'), 'bc7');
assert.equal(pickTextureFormat('unknown'), 'webp');
});
test('pickAudioCodec 平台映射', () => {
assert.equal(pickAudioCodec('web'), 'opus');
assert.equal(pickAudioCodec('ios'), 'vorbis');
assert.equal(pickAudioCodec('android'), 'vorbis');
assert.equal(pickAudioCodec('desktop'), 'pcm');
});
test('compressChoice 按资产类型路由', () => {
assert.equal(compressChoice('texture'), 'none');
assert.equal(compressChoice('audio'), 'none');
assert.equal(compressChoice('data'), 'zstd');
assert.equal(compressChoice('scene'), 'gzip');
});
test('alphaCapable 与零字节', () => {
assert.equal(alphaCapable('bc7'), true);
assert.equal(alphaCapable('etc2'), true);
assert.equal(alphaCapable('none'), false);
assert.equal(estimateCompressed(0, 'gzip'), 0);
});
本机实测(Node v26.5.0,node --test test/12-2-compress-encode.test.ts):tests 6、pass 6、fail 0,耗时约 40 ms。
第二条钉「精确数值」——gzip→350、zstd→280,体积预算是可核对的具体数,不是「大概小一点」;第四条钉「移动端 vorbis / web opus」——同样一首音频,不同平台编码不同,这是构建期编码存在的全部意义。
小结
- 压缩先想清楚再动手:纹理/音频走平台格式,数据/JSON 走 zstd,其他走 gzip。
estimateCompressed是体积预算工具:先估后压、超预算报警。alphaCapable提醒:选格式先确认透明通道能力。- 平台映射给默认、不报错:新平台接入即用。
- 下一节让构建产物「少而精」:LOD 生成——距离远就少画三角形。
阅读导航:上一节:12.1 平台目标与构建清单 · 下一节:12.3 LOD 生成 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。