Skip to main content

concinnity_asset/
texture.rs

1// 2D texture image schema.
2
3use crate::{AssetId, PayloadLocator};
4use alloc::string::String;
5
6/// A 2D texture image.
7///
8/// Use the `generator` field for built-in patterns or supply a `source` file path.
9///
10/// **Built-in generators:**
11///
12/// **Choosing a room texture**: for neutral indoor spaces prefer `plaster` (cream-white) or `concrete` (grey). `brick` is reddish-orange, only use it when you explicitly want that look. `stone` (dark grey-blue) suits dungeons or medieval rooms.
13///
14/// ```rust
15/// # use concinnity_asset::Texture;
16/// Texture {
17///     generator: "brick".into(),
18///     resolution: 512,
19///     ..Default::default()
20/// };
21/// ```
22#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
23#[serde(default)]
24pub struct Texture {
25    /// Asset identity; injected via `inject_name`. Not part of `args`.
26    #[serde(skip)]
27    pub asset_id: AssetId,
28    /// Procedural generator name. Empty or omitted means use `source` instead.
29    pub generator: String,
30    /// Path to the source image, relative to the project root.
31    /// Used only when `generator` is empty. A `.glb` path is allowed, use
32    /// `image_index` to pick which embedded image to use.
33    pub source: String,
34    /// When `source` points to a `.glb` file, which embedded image to import.
35    /// Ignored for regular image files.
36    pub image_index: u32,
37    /// Resolution hint for procedural generators (width = height). Defaults to
38    /// 512. Ignored for file-backed textures.
39    pub resolution: u32,
40    /// Optional ceiling on the longest edge of a file-backed image, in pixels.
41    /// `0` (the default) keeps the source resolution. When set and the source is
42    /// larger, the image is box-filtered down so its longest edge is at most this
43    /// value. Useful to keep very large source maps (4K+) from bloating the
44    /// compiled scene, which stores uncompressed pixels.
45    pub max_size: u32,
46    /// Injected at load time from the compiled blob payload.
47    #[serde(skip)]
48    pub locator: Option<PayloadLocator>,
49}
50
51impl Default for Texture {
52    fn default() -> Self {
53        Self {
54            asset_id: AssetId::default(),
55            generator: String::new(),
56            source: String::new(),
57            image_index: 0,
58            resolution: 512,
59            max_size: 0,
60            locator: None,
61        }
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn a_blank_texture_generates_at_the_default_resolution_and_is_uncapped() {
71        let t = Texture::default();
72        assert!(t.source.is_empty());
73        assert!(t.generator.is_empty());
74        assert_eq!(t.image_index, 0);
75        assert_eq!(t.resolution, 512);
76        // Zero means "no downscale cap", not a zero-sized image.
77        assert_eq!(t.max_size, 0);
78        assert!(t.locator.is_none());
79    }
80
81    #[test]
82    fn a_generated_texture_names_its_generator_instead_of_a_source() {
83        let t: Texture =
84            serde_json::from_str(r#"{"generator":"checker","resolution":128}"#).unwrap();
85        assert_eq!(t.generator, "checker");
86        assert!(t.source.is_empty());
87        assert_eq!(t.resolution, 128);
88    }
89
90    #[test]
91    fn an_imported_image_parses_and_round_trips_through_postcard() {
92        let t: Texture =
93            serde_json::from_str(r#"{"source":"bistro.fbx","image_index":7,"max_size":1024}"#)
94                .unwrap();
95        // The index picks one image out of a multi-image source archive.
96        assert_eq!(t.image_index, 7);
97
98        let bytes = postcard::to_allocvec(&t).unwrap();
99        let back: Texture = postcard::from_bytes(&bytes).unwrap();
100        assert_eq!(back.source, "bistro.fbx");
101        assert_eq!(back.image_index, 7);
102        assert_eq!(back.max_size, 1024);
103        assert_eq!(back.asset_id, AssetId::default());
104        assert!(back.locator.is_none());
105    }
106}