1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
pub use *;
/// Splits specified sprite textures into chunks, discards identical ones, joins unique
/// chunks into atlas textures and generates sprite meshes with texture coordinates mapped
/// to the atlas textures allowing to render original sprites without the source textures.
///
/// # Arguments
///
/// * `sprites`: Source sprite textures to dice.
/// * `prefs`: User preferences for the dicing operation.
///
/// returns: Generated atlas textures and diced sprite meshes or [Error].
///
/// # Examples
///
/// ```
/// use sprite_dicing::{Prefs, SourceSprite, Texture, Pixel};
///
/// // Fake function to load textures (images).
/// fn load (path: &str) -> Texture {
/// let red = Pixel::new(255, 0, 0, 255);
/// let blue = Pixel::new(0, 0, 255, 255);
/// Texture { width: 2, height: 2, pixels: vec![red, blue, blue, red] }
/// }
///
/// // Fake function to save textures.
/// fn save (path: &str, tex: &Texture) { }
///
/// // Collect source sprites to dice.
/// let sprites = vec![
/// SourceSprite { id: "1".to_owned(), texture: load("1.png"), pivot: None },
/// SourceSprite { id: "2".to_owned(), texture: load("2.png"), pivot: None },
/// // ...
/// ];
///
/// // Dice source sprites with default preferences.
/// let diced = sprite_dicing::dice(&sprites, &Prefs::default()).unwrap();
///
/// // Write generated atlas textures to file system.
/// for (index, atlas) in diced.atlases.iter().enumerate() {
/// save(&format!("atlas_{index}.png"), atlas);
/// }
///
/// // Build diced sprites using generated data.
/// for sprite in diced.sprites {
/// // Unique ID as set in the associated source sprite.
/// _ = sprite.id;
/// // Atlas texture containing all the unique pixels for the sprite.
/// _ = &diced.atlases[sprite.atlas_index];
/// // Mesh vertex positions in local space units.
/// _ = sprite.vertices;
/// // Atlas texture coordinates mapped to the mesh vertices.
/// _ = sprite.uvs;
/// // Mesh quad faces as indices to the vertices array.
/// _ = sprite.indices;
/// // Sprite rect in local space units.
/// _ = sprite.rect;
/// // ... (actual sprite asset building process is engine-specific)
/// }
/// ```