Skip to main content

gltf_reader/
sampler.rs

1use alloc::borrow::Cow;
2use ownable::IntoOwned;
3use serde::Deserialize;
4
5use crate::{Extensions, Extras};
6
7/// glTF known sampling filters.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum FilterEnum {
10    Nearest,
11    Linear,
12    NearestMipmapNearest,
13    LinearMipmapNearest,
14    NearestMipmapLinear,
15    LinearMipmapLinear,
16}
17
18/// Sampling filter.
19#[derive(Clone, Copy, PartialEq, Eq, Deserialize, IntoOwned)]
20#[serde(transparent)]
21pub struct Filter(pub u64);
22
23impl core::fmt::Debug for Filter {
24    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25        if let Some(e) = self.to_enum() {
26            e.fmt(f)
27        } else {
28            self.0.fmt(f)
29        }
30    }
31}
32
33impl Filter {
34    pub const NEAREST: Self = Self(9728);
35    pub const LINEAR: Self = Self(9729);
36    pub const NEAREST_MIPMAP_NEAREST: Self = Self(9984);
37    pub const LINEAR_MIPMAP_NEAREST: Self = Self(9985);
38    pub const NEAREST_MIPMAP_LINEAR: Self = Self(9986);
39    pub const LINEAR_MIPMAP_LINEAR: Self = Self(9987);
40
41    pub fn to_enum(self) -> Option<FilterEnum> {
42        Some(match self {
43            Self::NEAREST => FilterEnum::Nearest,
44            Self::LINEAR => FilterEnum::Linear,
45            Self::NEAREST_MIPMAP_NEAREST => FilterEnum::NearestMipmapNearest,
46            Self::LINEAR_MIPMAP_NEAREST => FilterEnum::LinearMipmapNearest,
47            Self::NEAREST_MIPMAP_LINEAR => FilterEnum::NearestMipmapLinear,
48            Self::LINEAR_MIPMAP_LINEAR => FilterEnum::LinearMipmapLinear,
49            _ => return None,
50        })
51    }
52}
53
54/// glTF known wrapping modes.
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum WrapEnum {
57    ClampToEdge,
58    MirroredRepeat,
59    Repeat,
60}
61
62/// Wrapping mode.
63#[derive(Clone, Copy, PartialEq, Eq, Deserialize, IntoOwned)]
64#[serde(transparent)]
65pub struct Wrap(pub u64);
66
67impl Default for Wrap {
68    fn default() -> Self {
69        Self::REPEAT
70    }
71}
72
73impl core::fmt::Debug for Wrap {
74    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75        if let Some(e) = self.to_enum() {
76            e.fmt(f)
77        } else {
78            self.0.fmt(f)
79        }
80    }
81}
82
83impl Wrap {
84    pub const CLAMP_TO_EDGE: Self = Self(33071);
85    pub const MIRRORED_REPEAT: Self = Self(33648);
86    pub const REPEAT: Self = Self(10497);
87
88    pub fn to_enum(self) -> Option<WrapEnum> {
89        Some(match self {
90            Self::CLAMP_TO_EDGE => WrapEnum::ClampToEdge,
91            Self::MIRRORED_REPEAT => WrapEnum::MirroredRepeat,
92            Self::REPEAT => WrapEnum::Repeat,
93            _ => return None,
94        })
95    }
96}
97
98/// Texture sampler properties for filtering and wrapping modes.
99#[derive(Debug, Clone, Deserialize, IntoOwned)]
100pub struct Sampler<'a> {
101    /// The user-defined name of this object.
102    #[serde(borrow)]
103    pub name: Option<Cow<'a, str>>,
104
105    /// Magnification filter.
106    #[serde(rename = "magFilter")]
107    pub mag_filter: Option<Filter>,
108    /// Minification filter.
109    #[serde(rename = "minFilter")]
110    pub min_filter: Option<Filter>,
111    /// S (U) wrapping mode.
112    #[serde(rename = "wrapS")]
113    #[serde(default)]
114    pub wrap_s: Wrap,
115    /// T (V) wrapping mode.
116    #[serde(rename = "wrapT")]
117    #[serde(default)]
118    pub wrap_t: Wrap,
119
120    #[serde(borrow)]
121    pub extensions: Option<Extensions<'a>>,
122    #[serde(borrow)]
123    pub extras: Option<Extras<'a>>,
124}