1use super::scenes::{CutOptions, one, scene};
32use super::{AtlasScene, BuildError, Object, V, file, format, pair, pptr, s, sheet, st, texture};
33use crate::ngui::SceneBundle;
34
35pub const RECT: u32 = 2;
38pub const TIGHT: u32 = 0;
40
41#[must_use]
44pub fn packed(rotation: u32) -> u32 {
45 1 | RECT | rotation << 2
46}
47
48#[derive(Clone, Debug, Default, PartialEq)]
51pub struct Mesh {
52 pub vertices: Vec<[f32; 3]>,
54 pub submeshes: Vec<(u32, u32)>,
56 pub indices: Vec<u16>,
58}
59
60impl Mesh {
61 #[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#[derive(Clone, Debug, PartialEq)]
76pub struct SpriteSpec {
77 pub name: Vec<u8>,
79 pub rect: [f32; 4],
81 pub settings: u32,
83 pub ppu: f32,
85 pub texture: (i32, i64),
87 pub alpha: (i32, i64),
89 pub mesh: Mesh,
91 pub atlas: (i32, i64),
93 pub key: ([u32; 4], i64),
95 pub tags: Vec<Vec<u8>>,
97}
98
99impl SpriteSpec {
100 #[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 #[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 #[must_use]
132 pub fn settings(mut self, settings: u32) -> Self {
133 self.settings = settings;
134 self
135 }
136
137 #[must_use]
139 pub fn alpha(mut self, alpha: (i32, i64)) -> Self {
140 self.alpha = alpha;
141 self
142 }
143
144 #[must_use]
146 pub fn in_atlas(mut self, atlas: (i32, i64)) -> Self {
147 self.atlas = atlas;
148 self
149 }
150
151 #[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 #[must_use]
160 pub fn keyed(mut self, key: ([u32; 4], i64)) -> Self {
161 self.key = key;
162 self
163 }
164
165 #[must_use]
167 pub fn value(&self) -> V {
168 sprite_value(self)
169 }
170
171 #[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
201fn 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
234fn 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
314pub(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
329pub(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
341fn 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
353pub(super) fn sprites(key: &str) -> CutOptions {
355 CutOptions {
356 sprites_key: Some(key.to_owned()),
357 ..CutOptions::default()
358 }
359}
360
361pub(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
371pub 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
398pub 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
438pub 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
472pub 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
497pub 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
580fn 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
597pub 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
613pub 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}