Skip to main content

unityrs_testkit/ngui/
sprites.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/classes/generated.py. See THIRD_PARTY_LICENSES.
2//! The `sprites` kind's scenes (`docs/BATCH_SPEC.md` 1.5's `outputs.sprites`,
3//! `docs/plans/extract.md` E17): Unity `Sprite`s (class 213) beside the
4//! `Texture2D` sheets they render from, in bundles whose views join them,
5//! and [`sprite_scenes`], which lists them by name for
6//! `tools/parity/sprites_scenes.py` (the reference `tools/parity/sprites_kind.py`
7//! is recorded on them by `tools/fixtures/sprites.py`).
8//!
9//! A Sprite is a [`super::V`] of the 2022.3 layout the testkit's files
10//! carry, with the fields `tools/fixtures/sprite_build.py`'s Sprite tree
11//! holds, in its order, from the same sources (nothing from a TPK or a
12//! dump, `docs/plans/core.md` section 9, Q2): of the fields UnityPy 1.25.2's
13//! MIT class definitions (`classes/generated.py`) declare, those without a
14//! default and those `get_image_from_sprite` reads, each node's type the
15//! one a source names, `classes/generated.py` or the MIT readers of
16//! AssetStudio's release v0.16.47 (format facts only: no AssetStudio code is
17//! ported or kept here). `m_RD` is a `SpriteRenderData` of the layout from
18//! 5.6 (`m_SubMeshes`, `m_IndexBuffer`, `m_VertexData`), its `VertexData`
19//! the one from 2018 (no `m_CurrentChannels`, 14 `ChannelInfo`s), with
20//! `alphaTexture` last; then `m_SpriteAtlas`, `m_RenderDataKey` (a `pair`
21//! of a `GUID`, four `unsigned int`s `data[0]` to `data[3]`, and an
22//! `SInt64`) and `m_AtlasTags` (a vector of `string`).
23//!
24//! A vertex is three `float`s in channel 0 (`VertexFormat` 0, `float`, from
25//! 2019), the stream padded with zeros to a multiple of 16 bytes as
26//! `sprite_build.py` pads it, and the indices `UInt16`s, stored as the
27//! `UInt8`s of `m_IndexBuffer`. No scene has a UV channel: a tight Sprite
28//! here is masked by its triangles (`mask_sprite`), never drawn through an
29//! affine transform, whose last bits depend on the host's BLAS.
30
31use super::scenes::{CutOptions, one, scene};
32use super::{AtlasScene, BuildError, Object, V, file, format, pair, pptr, s, sheet, st, texture};
33use crate::ngui::SceneBundle;
34
35/// `settingsRaw` of a Sprite packed by its rectangle (`packingMode` 1),
36/// not rotated.
37pub const RECT: u32 = 2;
38/// `settingsRaw` of a tight Sprite (`packingMode` 0), its mesh's polygon.
39pub const TIGHT: u32 = 0;
40
41/// `settingsRaw` of a packed rectangle Sprite turned by `rotation`
42/// (`SpritePackingRotation`, bits 2 to 5).
43#[must_use]
44pub fn packed(rotation: u32) -> u32 {
45    1 | RECT | rotation << 2
46}
47
48/// A Sprite's mesh: its vertices (each in channel 0), its submeshes
49/// (`firstByte`, `indexCount`, topology 0) and its indices.
50#[derive(Clone, Debug, Default, PartialEq)]
51pub struct Mesh {
52    /// The vertices' positions.
53    pub vertices: Vec<[f32; 3]>,
54    /// The submeshes: `firstByte`, `indexCount`.
55    pub submeshes: Vec<(u32, u32)>,
56    /// The indices.
57    pub indices: Vec<u16>,
58}
59
60impl Mesh {
61    /// One triangle (0, 0), (`side`, 0), (0, `side`), as one submesh.
62    #[must_use]
63    pub fn triangle(side: f32) -> Self {
64        Self {
65            vertices: vec![[0.0, 0.0, 0.0], [side, 0.0, 0.0], [0.0, side, 0.0]],
66            submeshes: vec![(0, 3)],
67            indices: vec![0, 1, 2],
68        }
69    }
70}
71
72/// A Sprite: its name, `textureRect` and `settingsRaw`,
73/// `m_PixelsToUnits`, its texture's and alpha texture's pointers and its
74/// mesh.
75#[derive(Clone, Debug, PartialEq)]
76pub struct SpriteSpec {
77    /// `m_Name`.
78    pub name: Vec<u8>,
79    /// `m_RD.textureRect`: `x`, `y`, `width`, `height`.
80    pub rect: [f32; 4],
81    /// `m_RD.settingsRaw`.
82    pub settings: u32,
83    /// `m_PixelsToUnits`.
84    pub ppu: f32,
85    /// `m_RD.texture`.
86    pub texture: (i32, i64),
87    /// `m_RD.alphaTexture` (null: none).
88    pub alpha: (i32, i64),
89    /// `m_RD`'s mesh.
90    pub mesh: Mesh,
91    /// `m_SpriteAtlas` (null: none).
92    pub atlas: (i32, i64),
93    /// `m_RenderDataKey`: its `GUID`'s words and its id.
94    pub key: ([u32; 4], i64),
95    /// `m_AtlasTags`.
96    pub tags: Vec<Vec<u8>>,
97}
98
99impl SpriteSpec {
100    /// A rectangle Sprite `name` of `rect` on the texture at `texture`, with
101    /// no alpha texture and no mesh, `m_PixelsToUnits` 1.
102    #[must_use]
103    pub fn rect(name: &str, rect: [f32; 4], texture: (i32, i64)) -> Self {
104        Self {
105            name: name.as_bytes().to_vec(),
106            rect,
107            settings: RECT,
108            ppu: 1.0,
109            texture,
110            alpha: (0, 0),
111            mesh: Mesh::default(),
112            atlas: (0, 0),
113            key: ([1, 2, 3, 4], 7),
114            tags: Vec::new(),
115        }
116    }
117
118    /// A tight Sprite `name` of `rect` on `texture`, masked by `mesh`'s
119    /// triangles at `m_PixelsToUnits` 16.
120    #[must_use]
121    pub fn tight(name: &str, rect: [f32; 4], texture: (i32, i64), mesh: Mesh) -> Self {
122        Self {
123            settings: TIGHT,
124            ppu: 16.0,
125            mesh,
126            ..Self::rect(name, rect, texture)
127        }
128    }
129
130    /// The same Sprite with `settingsRaw` `settings`.
131    #[must_use]
132    pub fn settings(mut self, settings: u32) -> Self {
133        self.settings = settings;
134        self
135    }
136
137    /// The same Sprite with its alpha texture at `alpha`.
138    #[must_use]
139    pub fn alpha(mut self, alpha: (i32, i64)) -> Self {
140        self.alpha = alpha;
141        self
142    }
143
144    /// The same Sprite with `m_SpriteAtlas` at `atlas`.
145    #[must_use]
146    pub fn in_atlas(mut self, atlas: (i32, i64)) -> Self {
147        self.atlas = atlas;
148        self
149    }
150
151    /// The same Sprite with `m_AtlasTags` `tags`.
152    #[must_use]
153    pub fn tagged(mut self, tags: &[&str]) -> Self {
154        self.tags = tags.iter().map(|t| t.as_bytes().to_vec()).collect();
155        self
156    }
157
158    /// The same Sprite with `m_RenderDataKey` `key`.
159    #[must_use]
160    pub fn keyed(mut self, key: ([u32; 4], i64)) -> Self {
161        self.key = key;
162        self
163    }
164
165    /// The Sprite's value.
166    #[must_use]
167    pub fn value(&self) -> V {
168        sprite_value(self)
169    }
170
171    /// The Sprite as an object at `path_id`.
172    #[must_use]
173    pub fn object(&self, path_id: i64) -> Object {
174        Object::new(path_id, 213, sprite_value(self))
175    }
176}
177
178fn floats(ty: &str, names: &[&str], values: &[f32]) -> V {
179    V::Struct(
180        ty.to_owned(),
181        names
182            .iter()
183            .zip(values)
184            .map(|(n, v)| ((*n).to_owned(), V::Float(*v)))
185            .collect(),
186    )
187}
188
189pub(super) fn floats_of(ty: &str, names: &[&str], values: &[f32]) -> V {
190    floats(ty, names, values)
191}
192
193pub(super) fn rectf(r: [f32; 4]) -> V {
194    floats("Rectf", &["x", "y", "width", "height"], &r)
195}
196
197fn bytes_vector(data: &[u8]) -> V {
198    super::vector(V::UInt8(0), data.iter().map(|b| V::UInt8(*b)).collect())
199}
200
201/// `m_SubMeshes`: each `SubMesh` with `firstByte`, `indexCount`, topology
202/// 0, `firstVertex` 0, `vertexCount` all the vertices and a zero
203/// `localAABB`.
204fn submeshes(mesh: &Mesh) -> V {
205    let count = u32::try_from(mesh.vertices.len()).unwrap_or(u32::MAX);
206    let aabb = || {
207        st(
208            "AABB",
209            vec![
210                ("m_Center", floats("Vector3f", &["x", "y", "z"], &[0.0; 3])),
211                ("m_Extent", floats("Vector3f", &["x", "y", "z"], &[0.0; 3])),
212            ],
213        )
214    };
215    let sub = |first: u32, n: u32| {
216        st(
217            "SubMesh",
218            vec![
219                ("firstByte", V::UInt(first)),
220                ("indexCount", V::UInt(n)),
221                ("topology", V::Int(0)),
222                ("firstVertex", V::UInt(0)),
223                ("vertexCount", V::UInt(count)),
224                ("localAABB", aabb()),
225            ],
226        )
227    };
228    super::vector(
229        sub(0, 0),
230        mesh.submeshes.iter().map(|(f, n)| sub(*f, *n)).collect(),
231    )
232}
233
234/// `m_VertexData`: the vertex count, 14 channels (channel 0 three
235/// `float`s, the others empty) and the stream.
236fn vertex_data(mesh: &Mesh) -> V {
237    let channel = |dimension: u8| {
238        st(
239            "ChannelInfo",
240            vec![
241                ("stream", V::UInt8(0)),
242                ("offset", V::UInt8(0)),
243                ("format", V::UInt8(0)),
244                ("dimension", V::UInt8(dimension)),
245            ],
246        )
247    };
248    let channels = (0..14)
249        .map(|i| channel(if i == 0 { 3 } else { 0 }))
250        .collect();
251    let mut data: Vec<u8> = mesh
252        .vertices
253        .iter()
254        .flatten()
255        .flat_map(|f| f.to_le_bytes())
256        .collect();
257    data.resize(data.len().next_multiple_of(16), 0);
258    st(
259        "VertexData",
260        vec![
261            (
262                "m_VertexCount",
263                V::UInt(u32::try_from(mesh.vertices.len()).unwrap_or(u32::MAX)),
264            ),
265            ("m_Channels", super::vector(channel(0), channels)),
266            ("m_DataSize", V::Bytes(data)),
267        ],
268    )
269}
270
271fn sprite_value(spec: &SpriteSpec) -> V {
272    let indices: Vec<u8> = spec
273        .mesh
274        .indices
275        .iter()
276        .flat_map(|i| i.to_le_bytes())
277        .collect();
278    let rd = st(
279        "SpriteRenderData",
280        vec![
281            ("texture", pptr("Texture2D", spec.texture)),
282            ("m_SubMeshes", submeshes(&spec.mesh)),
283            ("m_IndexBuffer", bytes_vector(&indices)),
284            ("m_VertexData", vertex_data(&spec.mesh)),
285            ("textureRect", rectf(spec.rect)),
286            (
287                "textureRectOffset",
288                floats("Vector2f", &["x", "y"], &[0.0; 2]),
289            ),
290            ("settingsRaw", V::UInt(spec.settings)),
291            ("alphaTexture", pptr("Texture2D", spec.alpha)),
292        ],
293    );
294    let (words, id) = spec.key;
295    st(
296        "Sprite",
297        vec![
298            ("m_Name", V::Str(spec.name.clone())),
299            ("m_Rect", rectf(spec.rect)),
300            ("m_Offset", floats("Vector2f", &["x", "y"], &[0.0; 2])),
301            ("m_PixelsToUnits", V::Float(spec.ppu)),
302            ("m_Extrude", V::UInt(1)),
303            ("m_RD", rd),
304            ("m_SpriteAtlas", pptr("SpriteAtlas", spec.atlas)),
305            ("m_RenderDataKey", key(words, id)),
306            (
307                "m_AtlasTags",
308                super::vector(s(""), spec.tags.iter().map(|t| V::Str(t.clone())).collect()),
309            ),
310        ],
311    )
312}
313
314/// A `pair<GUID, SInt64>`: a `GUID` of `words` (`data[0]` to `data[3]`)
315/// and `id`.
316pub(super) fn key(words: [u32; 4], id: i64) -> V {
317    let names = ["data[0]", "data[1]", "data[2]", "data[3]"];
318    let guid = st(
319        "GUID",
320        names
321            .iter()
322            .zip(words)
323            .map(|(n, w)| (*n, V::UInt(w)))
324            .collect(),
325    );
326    pair(guid, V::Int64(id))
327}
328
329/// An RGBA32 sheet `name` at `path_id`, `w` x `h`, of base `base`.
330pub(super) fn rgba(path_id: i64, name: &str, base: u8, w: u8, h: u8) -> Object {
331    let size = (i32::from(w), i32::from(h));
332    texture(
333        path_id,
334        name.as_bytes(),
335        format::RGBA32,
336        size,
337        sheet(base, w, h, false),
338    )
339}
340
341/// An RGB24 sheet.
342fn rgb(path_id: i64, name: &str, base: u8, w: u8, h: u8) -> Object {
343    let size = (i32::from(w), i32::from(h));
344    texture(
345        path_id,
346        name.as_bytes(),
347        format::RGB24,
348        size,
349        sheet(base, w, h, true),
350    )
351}
352
353/// The options of a scene exporting Sprites under `key`.
354pub(super) fn sprites(key: &str) -> CutOptions {
355    CutOptions {
356        sprites_key: Some(key.to_owned()),
357        ..CutOptions::default()
358    }
359}
360
361/// One input `x.bundle` of the CAB `cab`, Sprites exported under `key`.
362pub(super) fn alone(cab: &str, objects: &[Object], key: &str) -> Result<AtlasScene, BuildError> {
363    let data = file(&[], objects, true)?;
364    Ok(scene(
365        vec![one("x.bundle", &[(cab, data)], &[])],
366        &["x.bundle"],
367        sprites(key),
368    ))
369}
370
371/// Sprites on sheets of their own file, in two groups: an RGBA32 sheet
372/// (`a`, `c` turned a quarter, `mask`, tight, and `flip`, mirrored) and an
373/// RGB24 one (`b`, and `mask_rgb`, whose mask becomes its alpha), the
374/// groups interleaved in object order, so the export runs `a`, `c`, `mask`,
375/// `flip`, then `b`, `mask_rgb`.
376pub fn own_sheets() -> Result<AtlasScene, BuildError> {
377    let tri = Mesh::triangle(0.25);
378    alone(
379        "CAB-s1",
380        &[
381            rgba(1, "sheet", 1, 8, 8),
382            SpriteSpec::rect("a", [1.0, 1.0, 4.0, 3.0], (0, 1)).object(2),
383            rgb(3, "other", 0x40, 4, 4),
384            SpriteSpec::rect("b", [0.0, 0.0, 4.0, 4.0], (0, 3)).object(4),
385            SpriteSpec::rect("c", [2.0, 1.0, 5.0, 3.0], (0, 1))
386                .settings(packed(4))
387                .object(5),
388            SpriteSpec::tight("mask", [0.0, 0.0, 4.0, 4.0], (0, 1), tri.clone()).object(6),
389            SpriteSpec::tight("mask_rgb", [0.0, 0.0, 4.0, 4.0], (0, 3), tri).object(7),
390            SpriteSpec::rect("flip", [1.0, 2.0, 3.0, 2.0], (0, 1))
391                .settings(packed(1))
392                .object(8),
393        ],
394        "{type}/{name}.png",
395    )
396}
397
398/// Sheets in a dependency and pointers that find none: `far` and `far2`
399/// share the dependency's sheet; `own` has its own texture and the
400/// dependency's RGB24 alpha texture; `gone` names a CAB outside the view
401/// (`outside_view`); `null`, `missing` and `past` a null pointer, a missing
402/// object and an `m_FileID` past the externals (`bad_pointer`).
403pub fn dependency_sheets() -> Result<AtlasScene, BuildError> {
404    let dep = file(
405        &[],
406        &[
407            rgba(10, "dep", 0x20, 4, 4),
408            rgb(11, "dep_alpha", 0x70, 2, 2),
409        ],
410        true,
411    )?;
412    let cab = file(
413        &["archive:/CAB-d1/CAB-d1", "archive:/CAB-gone/CAB-gone"],
414        &[
415            SpriteSpec::rect("far", [0.0, 0.0, 4.0, 4.0], (1, 10)).object(1),
416            SpriteSpec::rect("far2", [1.0, 1.0, 2.0, 2.0], (1, 10)).object(2),
417            SpriteSpec::rect("gone", [0.0, 0.0, 1.0, 1.0], (2, 5)).object(3),
418            SpriteSpec::rect("null", [0.0, 0.0, 1.0, 1.0], (0, 0)).object(4),
419            SpriteSpec::rect("missing", [0.0, 0.0, 1.0, 1.0], (0, 99)).object(5),
420            SpriteSpec::rect("past", [0.0, 0.0, 1.0, 1.0], (3, 1)).object(6),
421            rgba(7, "own", 0x30, 2, 2),
422            SpriteSpec::rect("own", [0.0, 0.0, 2.0, 2.0], (0, 7))
423                .alpha((1, 11))
424                .object(8),
425        ],
426        true,
427    )?;
428    Ok(scene(
429        vec![
430            one("d.bundle", &[("CAB-d1", dep)], &[]),
431            one("x.bundle", &[("CAB-s2", cab)], &["d.bundle"]),
432        ],
433        &["x.bundle"],
434        sprites("{safe_input}/{name}_{path_id}.png"),
435    ))
436}
437
438/// Alpha textures: an RGB24 sheet with an RGBA32 alpha texture of its size
439/// (`merged`, `merged2` and the tight `alpha_mask`, one group), the same
440/// sheet alone (`plain`, a group of its own), and with an alpha texture of
441/// another size (`mismatch`, `Image.merge`'s `ValueError`: `render`).
442pub fn alpha_textures() -> Result<AtlasScene, BuildError> {
443    alone(
444        "CAB-s3",
445        &[
446            rgb(1, "rgb", 0x10, 8, 8),
447            rgba(2, "alpha", 0x90, 8, 8),
448            rgba(3, "small_alpha", 0x50, 8, 4),
449            SpriteSpec::rect("merged", [1.0, 1.0, 4.0, 4.0], (0, 1))
450                .alpha((0, 2))
451                .object(4),
452            SpriteSpec::rect("plain", [1.0, 1.0, 4.0, 4.0], (0, 1)).object(5),
453            SpriteSpec::rect("merged2", [0.0, 0.0, 8.0, 8.0], (0, 1))
454                .alpha((0, 2))
455                .object(6),
456            SpriteSpec::rect("mismatch", [0.0, 0.0, 2.0, 2.0], (0, 1))
457                .alpha((0, 3))
458                .object(7),
459            SpriteSpec::tight(
460                "alpha_mask",
461                [2.0, 2.0, 4.0, 4.0],
462                (0, 1),
463                Mesh::triangle(0.25),
464            )
465            .alpha((0, 2))
466            .object(8),
467        ],
468        "{name}.png",
469    )
470}
471
472/// Names and keys (`{safe_name}.png`): a second `dup` (`duplicate_name`),
473/// `a_b` after `a b` (one safe name: `duplicate_key`), a name whose key
474/// has a segment past 255 bytes (`unwritable_key(segment_too_long)`), a
475/// Sprite with bytes after its value (`check_read`: `asset_bundle`) and one
476/// cut short after its name (`read`).
477pub fn names_and_keys() -> Result<AtlasScene, BuildError> {
478    let one_px = |name: &str| SpriteSpec::rect(name, [0.0, 0.0, 2.0, 2.0], (0, 1));
479    let long = "n".repeat(260);
480    alone(
481        "CAB-s4",
482        &[
483            rgba(1, "t", 0x60, 4, 4),
484            one_px("dup").object(2),
485            one_px("dup").object(3),
486            one_px("a b").object(4),
487            one_px("a_b").object(5),
488            one_px(&long).object(6),
489            one_px("trailing").object(7).with_trailing(4),
490            one_px("cut").object(8).cut_to(24),
491            one_px("last").object(9),
492        ],
493        "{safe_name}.png",
494    )
495}
496
497/// Every way a render or its sheet fails, each Sprite on its own: crops
498/// past Pillow's bounds (`decompression_bomb`, `crop_overflow` from a C
499/// `int` and a C `long`, `pillow_memory_error` at the width cap), images
500/// with a side of 0 (`empty_image`, one just below the cap), and the other
501/// exceptions (`render`: a NaN, an infinity, a right edge before the left,
502/// a rotation of 5, a tight Sprite with no vertices, an index past them);
503/// then a sheet that fails at the plan (`unsupported_format(Alpha8)`) and
504/// sheets that fail at the export, for every Sprite of their group
505/// (`no_image_data`, `not_enough_data`); and sheets with a side of 0, which
506/// UnityPy decodes to a 0 x 0 image and renders from (B3's review round 1,
507/// R1): one alone (`empty_sheet`, its crop padded), one as the alpha
508/// texture beside an 8 x 8 sheet (`Image.merge`'s size mismatch, `render`),
509/// and both a sheet's and its alpha texture's (a merge of two 0 x 0
510/// images, then a crop of it, and a crop of 0 x 0, `empty_image`), and an
511/// 8 x 0 sheet beside an 8 x 8 alpha texture (`alpha_zero_main`, `render`).
512/// `texture_first` is a second Sprite on the Alpha8 sheet (its skip's
513/// reason charged, shared with `bad_format`), with an alpha texture that
514/// names no object: the texture's answers come first (B3's review round 2,
515/// S5), so its skip is the texture's.
516pub fn render_failures() -> Result<AtlasScene, BuildError> {
517    let on = |name: &str, rect: [f32; 4]| SpriteSpec::rect(name, rect, (0, 1));
518    let cap = 536_870_912.0;
519    let past = Mesh {
520        indices: vec![0, 1, 3],
521        ..Mesh::triangle(0.25)
522    };
523    let no_vertices = Mesh {
524        submeshes: vec![(0, 0)],
525        ..Mesh::default()
526    };
527    let mut objects = vec![rgba(1, "sheet", 0x11, 8, 8)];
528    let sprites = [
529        on("bomb", [0.0, 0.0, 20000.0, 20000.0]),
530        on("c_int", [3.0e9, 0.0, 1.0, 1.0]),
531        on("c_int_negative", [-3.0e9, 0.0, 1.0, 1.0]),
532        on("c_long", [1.0e19, 0.0, 0.0, 1.0]),
533        on("width_cap", [0.0, 0.0, cap, 0.0]),
534        on("below_cap", [0.0, 0.0, 536_870_880.0, 0.0]),
535        on("zero_width", [1.0, 1.0, 0.0, 3.0]),
536        on("nan", [f32::NAN, 0.0, 1.0, 1.0]),
537        on("infinity", [0.0, 0.0, f32::INFINITY, 1.0]),
538        on("negative_width", [2.0, 1.0, -1.0, 3.0]),
539        on("rotation_5", [1.0, 0.0, 5.0, 3.0]).settings(packed(5)),
540        SpriteSpec::tight("no_vertices", [0.0, 0.0, 4.0, 4.0], (0, 1), no_vertices),
541        SpriteSpec::tight("index_past", [0.0, 0.0, 4.0, 4.0], (0, 1), past),
542        on("fine", [0.0, 0.0, 2.0, 2.0]),
543    ];
544    for (at, spec) in (2..).zip(sprites) {
545        objects.push(spec.object(at));
546    }
547    let size = (4, 4);
548    objects.extend([
549        texture(30, b"alpha8", format::ALPHA8, size, vec![0; 16]),
550        texture(31, b"empty", format::RGBA32, (0, 0), vec![0; 4]),
551        texture(34, b"empty_wide", format::RGBA32, (4, 0), vec![0; 4]),
552        texture(35, b"empty_alpha", format::RGBA32, (0, 4), vec![0; 4]),
553        texture(36, b"empty_rgb", format::RGB24, (8, 0), vec![0; 4]),
554        texture(32, b"no_data", format::RGBA32, size, Vec::new()),
555        texture(33, b"short", format::RGBA32, size, vec![0; 10]),
556        SpriteSpec::rect("bad_format", [0.0, 0.0, 1.0, 1.0], (0, 30)).object(40),
557        SpriteSpec::rect("empty_sheet", [0.0, 0.0, 1.0, 1.0], (0, 31)).object(41),
558        SpriteSpec::rect("no_data_1", [0.0, 0.0, 1.0, 1.0], (0, 32)).object(42),
559        SpriteSpec::rect("short_sheet", [0.0, 0.0, 1.0, 1.0], (0, 33)).object(43),
560        SpriteSpec::rect("no_data_2", [1.0, 1.0, 1.0, 1.0], (0, 32)).object(44),
561        SpriteSpec::rect("alpha_zero_beside", [0.0, 0.0, 1.0, 1.0], (0, 1))
562            .alpha((0, 35))
563            .object(45),
564        SpriteSpec::rect("alpha_zero_both", [0.0, 0.0, 1.0, 1.0], (0, 34))
565            .alpha((0, 35))
566            .object(46),
567        SpriteSpec::rect("alpha_zero_both_2", [0.0, 0.0, 0.0, 0.0], (0, 34))
568            .alpha((0, 35))
569            .object(47),
570        SpriteSpec::rect("alpha_zero_main", [0.0, 0.0, 1.0, 1.0], (0, 36))
571            .alpha((0, 1))
572            .object(48),
573        SpriteSpec::rect("texture_first", [0.0, 0.0, 1.0, 1.0], (0, 30))
574            .alpha((0, 99))
575            .object(49),
576    ]);
577    alone("CAB-s5", &objects, "{name}.png")
578}
579
580/// One input's bundle for [`sprite_winners`]: Sprites `shared` and `own_<id>`
581/// on a sheet of base `base`.
582fn winner_input(id: &str, base: u8) -> Result<SceneBundle, BuildError> {
583    let stem = id.trim_end_matches(".bundle");
584    let cab_name = format!("CAB-w{stem}");
585    let cab = file(
586        &[],
587        &[
588            rgba(1, "sheet", base, 2, 2),
589            SpriteSpec::rect("shared", [0.0, 0.0, 2.0, 2.0], (0, 1)).object(2),
590            SpriteSpec::rect(&format!("own_{stem}"), [0.0, 0.0, 1.0, 1.0], (0, 1)).object(3),
591        ],
592        true,
593    )?;
594    Ok(one(id, &[(cab_name.as_str(), cab)], &[]))
595}
596
597/// Several inputs exporting the Sprite key `shared.png` (`{name}.png`),
598/// for 4.5's winners: `a` and `b` alike, `c` with other pixels.
599pub fn sprite_winners() -> Result<AtlasScene, BuildError> {
600    let bundles = vec![
601        winner_input("a.bundle", 1)?,
602        winner_input("b.bundle", 1)?,
603        winner_input("c.bundle", 0x31)?,
604    ];
605    let inputs = bundles.iter().map(|b| b.id.clone()).collect();
606    Ok(AtlasScene {
607        bundles,
608        inputs,
609        options: sprites("{name}.png"),
610    })
611}
612
613/// Every `sprites` scene, by name.
614pub fn sprite_scenes() -> Result<Vec<(&'static str, AtlasScene)>, BuildError> {
615    let all: [(&'static str, super::scenes::Make); 18] = [
616        ("own_sheets", own_sheets),
617        ("dependency_sheets", dependency_sheets),
618        ("alpha_textures", alpha_textures),
619        ("names_and_keys", names_and_keys),
620        ("render_failures", render_failures),
621        ("sprite_winners", sprite_winners),
622        ("atlas_lookup", super::sprite_atlases::atlas_lookup),
623        ("atlas_dependency", super::sprite_atlases::atlas_dependency),
624        ("nested_cabs", super::sprite_atlases::nested_cabs),
625        ("sprite_fields", super::sprite_atlases::sprite_fields),
626        ("sprite_classes", super::sprite_objects::sprite_classes),
627        ("atlas_classes", super::sprite_objects::atlas_classes),
628        ("shadowed_rows", super::sprite_objects::shadowed_rows),
629        ("sprite_pointers", super::sprite_objects::sprite_pointers),
630        (
631            "sprite_attributes",
632            super::sprite_objects::sprite_attributes,
633        ),
634        ("int_tags", super::sprite_objects::int_tags),
635        ("raised_tags", super::sprite_atlases::raised_tags),
636        (
637            "raised_tags_swapped",
638            super::sprite_atlases::raised_tags_swapped,
639        ),
640    ];
641    all.into_iter().map(|(name, f)| Ok((name, f()?))).collect()
642}