Gltfpack.Net
1.0.0
dotnet add package Gltfpack.Net --version 1.0.0
NuGet\Install-Package Gltfpack.Net -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gltfpack.Net" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Gltfpack.Net" Version="1.0.0" />
<PackageReference Include="Gltfpack.Net" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Gltfpack.Net --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Gltfpack.Net, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Gltfpack.Net@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Gltfpack.Net&version=1.0.0
#tool nuget:?package=Gltfpack.Net&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Gltfpack.Net 使用说明
C# 直接调用 gltfpack 的托管封装,用于优化 glTF/GLB/OBJ 文件。
⚠️ 仅支持 Windows x64:此库使用 C++/CLI 技术,不支持 Linux、macOS 或其他平台。
安装
dotnet add package Gltfpack.Net
快速开始
using Gltf;
// 简单用法:高压缩
int result = GltfPack.compress("input.glb", "output.glb");
// 自定义配置
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.simplify_ratio = 0.5f;
result = GltfPack.gltfpack("input.glb", "output.glb", null, settings);
}
API
| 方法 | 说明 |
|---|---|
GltfPack.gltfpack(input, output, report, settings) |
完整配置处理 |
GltfPack.gltfpack(input, output, report, settings, logCallback) |
完整配置处理,带日志回调 |
GltfPack.gltfpack(input, output) |
默认配置处理 |
GltfPack.compress(input, output) |
高压缩配置 (-cc) |
返回值
| 值 | 说明 |
|---|---|
| 0 | 成功 |
| -1 | 无效参数 |
| 2 | 解析失败 |
| 3 | 纹理编码失败 |
| 4 | 写入失败 |
GltfSettings 参数详解
压缩选项
| 属性 | 类型 | 默认值 | 命令行 | 说明 |
|---|---|---|---|---|
compress |
bool | false | -c |
启用压缩 (EXT_meshopt_compression) |
compressmore |
bool | false | -cc |
高压缩模式 |
compresskhr |
bool | false | -cz |
使用 KHR_meshopt_compression |
fallback |
bool | false | -cf |
生成带回退的压缩文件 |
quantize |
bool | true | -noq |
启用量化(false=禁用) |
网格简化
| 属性 | 类型 | 默认值 | 命令行 | 说明 |
|---|---|---|---|---|
simplify_ratio |
float | 1.0 | -si R |
简化比例 0-1(1=不简化) |
simplify_error |
float | 0.01 | -se E |
误差阈值 |
simplify_aggressive |
bool | false | -sa |
激进模式 |
simplify_lock_borders |
bool | false | -slb |
锁定边界顶点 |
simplify_attributes |
bool | false | 属性感知简化 | |
simplify_scaled |
bool | false | 缩放感知简化 | |
simplify_permissive |
bool | false | -sp |
宽松模式 |
顶点量化
| 属性 | 类型 | 默认值 | 命令行 | 说明 |
|---|---|---|---|---|
pos_bits |
int | 14 | -vp N |
位置量化位数 (1-16) |
tex_bits |
int | 12 | -vt N |
UV量化位数 (1-16) |
nrm_bits |
int | 8 | -vn N |
法线量化位数 (1-16) |
col_bits |
int | 8 | -vc N |
颜色量化位数 (1-16) |
pos_normalized |
bool | false | -vpn |
位置归一化属性 |
pos_float |
bool | false | -vpf |
位置浮点属性 |
tex_float |
bool | false | -vtf |
UV浮点属性 |
nrm_float |
bool | false | -vnf |
法线浮点属性 |
纹理处理
| 属性 | 类型 | 默认值 | 命令行 | 说明 |
|---|---|---|---|---|
texture_ktx2 |
bool | false | -tc |
KTX2/Basis Universal 压缩 |
texture_webp |
bool | false | -tw |
WebP 压缩 |
texture_embed |
bool | auto | 嵌入纹理到 GLB | |
texture_ref |
bool | false | -tr |
保留原始纹理引用 |
texture_pow2 |
bool | false | -tp |
2的幂次尺寸 |
texture_flipy |
bool | false | -tfy |
Y轴翻转 |
texture_jobs |
int | 0 | -tj N |
压缩线程数 |
按纹理类型配置
// GltfTextureKind: Generic, Color, Normal, Attrib
// GltfTextureMode: Raw, ETC1S, UASTC, WebP
settings.SetTextureMode(GltfTextureKind.Color, GltfTextureMode.ETC1S);
settings.SetTextureQuality(GltfTextureKind.Color, 8); // 1-10
settings.SetTextureScale(GltfTextureKind.Normal, 0.5f); // 0-1
settings.SetTextureLimit(GltfTextureKind.Attrib, 1024); // 最大像素
场景选项
| 属性 | 类型 | 默认值 | 命令行 | 说明 |
|---|---|---|---|---|
keep_nodes |
bool | false | -kn |
保留命名节点 |
keep_materials |
bool | false | -km |
保留命名材质 |
keep_extras |
bool | false | -ke |
保留 extras 数据 |
keep_attributes |
bool | false | -kv |
保留顶点属性 |
mesh_dedup |
bool | false | 网格去重 | |
mesh_merge |
bool | false | -mm |
合并网格实例 |
mesh_instancing |
bool | false | -mi |
GPU实例化 |
mesh_interleaved |
bool | false | -vi |
交错顶点属性 |
动画选项
| 属性 | 类型 | 默认值 | 命令行 | 说明 |
|---|---|---|---|---|
anim_freq |
int | 30 | -af N |
采样频率 (0=禁用) |
trn_bits |
int | 16 | -at N |
平移量化 (1-24) |
rot_bits |
int | 12 | -ar N |
旋转量化 (4-16) |
scl_bits |
int | 16 | -as N |
缩放量化 (1-24) |
anim_const |
bool | false | -ac |
保留常量轨道 |
使用示例
基础压缩
// 等同于: gltfpack -i input.glb -o output.glb -cc
int result = GltfPack.compress("input.glb", "output.glb");
网格简化
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.simplify_ratio = 0.5f; // 简化到50%
settings.simplify_lock_borders = true;
int result = GltfPack.gltfpack("input.glb", "output.glb", null, settings);
}
KTX2 纹理压缩(高质量)
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.texture_ktx2 = true;
settings.SetTextureMode(GltfTextureKind.Color, GltfTextureMode.UASTC);
settings.SetTextureMode(GltfTextureKind.Normal, GltfTextureMode.UASTC);
settings.SetTextureQuality(GltfTextureKind.Color, 10);
settings.SetTextureQuality(GltfTextureKind.Normal, 10);
int result = GltfPack.gltfpack("input.glb", "output.glb", null, settings);
}
KTX2 纹理压缩(高压缩率)
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.texture_ktx2 = true;
settings.SetTextureMode(GltfTextureKind.Color, GltfTextureMode.ETC1S);
settings.SetTextureMode(GltfTextureKind.Normal, GltfTextureMode.ETC1S);
settings.SetTextureQuality(GltfTextureKind.Color, 6);
settings.SetTextureQuality(GltfTextureKind.Normal, 8);
settings.SetTextureLimit(GltfTextureKind.Color, 1024);
int result = GltfPack.gltfpack("input.glb", "output.glb", null, settings);
}
WebP 纹理压缩
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.texture_webp = true;
settings.SetTextureMode(GltfTextureKind.Color, GltfTextureMode.WebP);
settings.SetTextureMode(GltfTextureKind.Normal, GltfTextureMode.WebP);
settings.SetTextureQuality(GltfTextureKind.Color, 8);
int result = GltfPack.gltfpack("input.glb", "output.glb", null, settings);
}
混合纹理压缩
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.texture_ktx2 = true;
// 颜色: ETC1S (高压缩)
settings.SetTextureMode(GltfTextureKind.Color, GltfTextureMode.ETC1S);
settings.SetTextureQuality(GltfTextureKind.Color, 6);
// 法线: UASTC (高质量)
settings.SetTextureMode(GltfTextureKind.Normal, GltfTextureMode.UASTC);
settings.SetTextureQuality(GltfTextureKind.Normal, 10);
// 属性: UASTC
settings.SetTextureMode(GltfTextureKind.Attrib, GltfTextureMode.UASTC);
settings.SetTextureQuality(GltfTextureKind.Attrib, 8);
settings.texture_jobs = Environment.ProcessorCount;
int result = GltfPack.gltfpack("input.glb", "output.glb", null, settings);
}
完整优化流程
using (GltfSettings settings = GltfSettings.defaults())
{
// 压缩
settings.compress = true;
settings.compressmore = true;
// 简化
settings.simplify_ratio = 0.5f;
settings.simplify_error = 0.01f;
settings.simplify_lock_borders = true;
// 量化
settings.pos_bits = 14;
settings.tex_bits = 12;
settings.nrm_bits = 8;
// 纹理
settings.texture_ktx2 = true;
settings.SetTextureMode(GltfTextureKind.Color, GltfTextureMode.ETC1S);
settings.SetTextureMode(GltfTextureKind.Normal, GltfTextureMode.UASTC);
settings.SetTextureQuality(GltfTextureKind.Color, 7);
settings.SetTextureQuality(GltfTextureKind.Normal, 9);
settings.texture_jobs = Environment.ProcessorCount;
// 场景
settings.mesh_merge = true;
settings.mesh_instancing = true;
int result = GltfPack.gltfpack("input.glb", "output.glb", "report.json", settings);
Console.WriteLine(result == 0 ? "成功" : $"失败: {result}");
}
保留节点数据
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.keep_nodes = true;
settings.keep_extras = true;
settings.pos_float = true;
int result = GltfPack.gltfpack("input.glb", "output.glb", null, settings);
}
纹理格式选择
| 格式 | 压缩率 | 质量 | 推荐场景 |
|---|---|---|---|
| ETC1S | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | 移动端、Web |
| UASTC | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | 桌面端 |
| WebP | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | 通用Web |
功能支持
| 功能 | 状态 |
|---|---|
| 网格压缩 | ✅ |
| 网格简化 | ✅ |
| 顶点量化 | ✅ |
| 动画优化 | ✅ |
| KTX2/Basis 纹理压缩 | ✅ |
| WebP 纹理压缩 | ✅ |
日志回调
使用 GltfLogCallback 委托可以捕获 gltfpack 处理过程中的详细输出(需配合 verbose 属性使用)。
GltfLogCallback 委托
public delegate void GltfLogCallback(string message);
使用示例
using Gltf;
// 定义日志回调
void OnLog(string message)
{
Console.WriteLine($"[gltfpack] {message}");
}
// 使用回调捕获详细输出
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.compressmore = true;
settings.verbose = 1; // 启用详细输出
int result = GltfPack.gltfpack(
"input.glb",
"output.glb",
null,
settings,
OnLog // 日志回调
);
Console.WriteLine(result == 0 ? "成功" : $"失败: {result}");
}
使用 Lambda 表达式
using (GltfSettings settings = GltfSettings.defaults())
{
settings.compress = true;
settings.verbose = 1;
StringBuilder log = new StringBuilder();
int result = GltfPack.gltfpack(
"input.glb",
"output.glb",
null,
settings,
msg => log.AppendLine(msg) // Lambda 回调
);
// 处理完成后查看日志
Console.WriteLine(log.ToString());
}
verbose 级别
| 值 | 说明 |
|---|---|
| 0 | 无输出(默认) |
| 1 | 基本统计信息(-v) |
| 2 | 详细统计信息(-vv) |
注意事项
- 如果不需要捕获输出,使用不带
logCallback参数的重载方法 - 回调会在处理完成后一次性返回所有输出
- 输出内容与命令行工具使用
-v参数时相同
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 134 | 1/28/2026 |
v1.0.0: Initial release with full gltfpack functionality
- All mesh optimization features
- KTX2/Basis Universal texture compression
- WebP texture compression
- Ijwhost.dll is dynamically copied from user's .NET Runtime for version compatibility