vmt_parser/data/
mod.rs

1pub mod texture_transform;
2
3use serde::de::Error;
4use serde::{Deserialize, Deserializer, Serialize};
5use serde_repr::{Deserialize_repr, Serialize_repr};
6use std::borrow::Cow;
7pub use texture_transform::TextureTransform;
8
9#[derive(Debug, Serialize, Deserialize, Copy, Clone, Default)]
10#[serde(from = "Vec2OrSingle<f32>")]
11pub struct Vec2(pub [f32; 2]);
12
13pub(crate) fn deserialize_bare_vec2<'de, D: Deserializer<'de>>(
14    deserializer: D,
15) -> Result<Vec2, D::Error> {
16    let str = Cow::<str>::deserialize(deserializer)?;
17    let (x, y) = str
18        .trim()
19        .split_once(' ')
20        .ok_or_else(|| D::Error::custom("doesn't look like a vec2"))?;
21    let x = x.trim().parse().map_err(D::Error::custom)?;
22    let y = y.trim().parse().map_err(D::Error::custom)?;
23
24    Ok(Vec2([x, y]))
25}
26
27impl From<Vec2OrSingle<f32>> for Vec2 {
28    fn from(value: Vec2OrSingle<f32>) -> Self {
29        match value {
30            Vec2OrSingle::Vec2(vec) => Vec2(vec),
31            Vec2OrSingle::Single(val) => Vec2([val; 2]),
32        }
33    }
34}
35
36#[derive(Debug, Serialize, Deserialize, Copy, Clone, Default)]
37#[serde(from = "Vec3OrSingle<f32>")]
38pub struct Vec3(pub [f32; 3]);
39
40impl From<Vec3OrSingle<f32>> for Vec3 {
41    fn from(value: Vec3OrSingle<f32>) -> Self {
42        match value {
43            Vec3OrSingle::Vec3(vec) => Vec3(vec),
44            Vec3OrSingle::Single(val) => Vec3([val; 3]),
45        }
46    }
47}
48
49#[derive(Debug, Serialize, Deserialize)]
50#[serde(untagged)]
51enum Vec3OrSingle<T> {
52    Vec3([T; 3]),
53    Single(T),
54}
55
56#[derive(Debug, Serialize, Deserialize)]
57#[serde(untagged)]
58enum Vec2OrSingle<T> {
59    Vec2([T; 2]),
60    Single(T),
61}
62
63pub(crate) fn default_scale3() -> Vec3 {
64    Vec3([1.0; 3])
65}
66
67#[allow(dead_code)]
68pub(crate) fn default_scale2() -> Vec2 {
69    Vec2([1.0; 2])
70}
71
72pub(crate) fn default_scale() -> f32 {
73    1.0
74}
75
76pub(crate) fn default_detail_scale() -> Vec2 {
77    Vec2([4.0; 2])
78}
79
80#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Copy, Clone, Default)]
81#[repr(u8)]
82pub enum BlendMode {
83    DecalModulate = 0,
84    #[default]
85    Additive = 1,
86    TranslucentOverlay = 2,
87    BlendFactorOverlay = 3,
88    TranslucentBase = 4,
89    UnlitAdditive = 5,
90    UnlitAdditiveThreshold = 6,
91    TwoPatternModulate = 7,
92    Multiply = 8,
93    BaseMaskAlpha = 9,
94    SelfShadowedBumpMap = 10,
95    SelfShadowedBumpAlbedo = 11,
96}