2个不稳定版本
使用旧的Rust 2015
0.3.0 | 2019年8月18日 |
---|---|
0.2.0 | 2019年7月13日 |
#1804 在 游戏开发
48KB
1K SLoC
Sheep 🐑
sheep
(Spritesheet packer) 是一个轻量级和模块化的库,用于创建精灵图。它旨在对API的使用施加尽可能少的限制,以便可以在资源管道中使用。
该项目正在积极开发中,API可能会在达到稳定版本之前改变几次,但输入字节和接收元数据和字节向量的基本流程将保持不变。
用法
要使用命令行界面,只需使用cargo安装它
cargo install sheep_cli
提供使用提示。要查看所有选项,只需在没有参数的情况下运行命令。可以使用 --options
标志将选项传递给打包器,作为空格分隔的 key=value
对。默认情况下,将使用 maxrects
打包器,有关更多信息,请参阅打包器。
示例
sheep pack --options max_width=1024 max_height=1024 sprites/*.png
如果您想从源代码中使用CLI,只需克隆仓库并运行 cargo run -- ...
。有关如何直接使用库的示例,请参阅 simple_pack
示例,该示例位于 sheep/examples
目录中。
实现自己的 Packer
和 Format
Sheep 通过允许您选择用于打包精灵和编码元数据的实现来达到模块化。目前提供了两种常见的打包算法(SimplePacker
和 MaxrectsPacker
,请参阅打包器),以及 amethyst 引擎 使用的数据格式(AmethystFormat
)。未来还将提供更多内容,但是您也可以选择自己的打包算法和格式。
实现 Packer
pub struct MyPacker;
impl Packer for MyPacker {
fn pack(sprites: &[SpriteData]) -> PackerResult {
// Spritedata contains an id for the sprite to reference back
// to it, and the dimensions of the sprite.
// The expected output is the dimensions of the resulting spritesheet,
// as well as all the anchors for the sprites (i.e. their positions).
PackerResult { dimensions, anchors }
}
}
实现 Format
pub struct MyFormat;
// This is the format that will be output by encode, and you'll probably want
// to serialize later.
#[derive(Serialize)]
pub struct Foo {}
impl Format for AmethystFOrmat {
type Data = Foo;
fn encode(dimensions: (u32, u32), sprites: &[SpriteAnchor]) -> Self::Data {
// Encode the spritesheet dimensions and sprite positions into
// your chosen data format here.
Foo {}
}
}
使用您自定义的 impl
要使用自定义打包器或格式化程序,只需在调用函数时将它们作为类型参数传递
let sprite_sheet = sheep::pack::<MyPacker>(sprites, 4);
let meta = sheep::encode::<MyFormat>(&sprite_sheet);
打包器
目前有两种实现可供选择
- MAXRECTS (推荐)
最大矩形精灵打包算法的实现。本实现参考的论文和原始实现可以在此处找到:这里。此算法在大多数情况下应能产生最佳结果。
- 简单
一种简单的实现,将精灵按面积排序,然后全部打包到一个纹理中。这种方法在简单场景中可能比maxrects更快,但不能很好地扩展,因为无法限制最终精灵图的尺寸。
路线图
以下是sheep的规划功能
支持多个输出纹理(箱)智能输出纹理尺寸- 更多打包算法
MAXRECTS- 天际线
- 更多元数据格式
- 更多图像格式
许可证
sheep
采用MIT和Apache双许可,请参阅COPYING
。
依赖项
~9MB
~158K SLoC