paddle/graphics/texture/
texture_config.rs

1use web_sys::WebGlRenderingContext;
2
3#[derive(Clone, Debug, Eq, PartialEq, Hash)]
4pub struct TextureConfig {
5    pub(crate) magnification_filter: MagnificationFilter,
6    pub(crate) minification_filter: MinificationFilter,
7    pub(crate) mipmap_level: MipmapLevel,
8    pub(crate) color_format: ColorFormat,
9}
10
11impl TextureConfig {
12    pub fn with_pixellated_mangification(mut self) -> Self {
13        self.magnification_filter = MagnificationFilter::Nearest;
14        self
15    }
16    pub fn with_blurred_magnification(mut self) -> Self {
17        self.magnification_filter = MagnificationFilter::Linear;
18        self
19    }
20    pub fn without_filter(mut self) -> Self {
21        self.mipmap_level = MipmapLevel::Off;
22        self.minification_filter = MinificationFilter::Nearest;
23        self
24    }
25    pub fn with_bilinear_filtering_no_mipmaps(mut self) -> Self {
26        self.mipmap_level = MipmapLevel::Off;
27        self.minification_filter = MinificationFilter::Linear;
28        self
29    }
30    pub fn with_unfiltered_mipmap(mut self) -> Self {
31        self.mipmap_level = MipmapLevel::Single;
32        self.minification_filter = MinificationFilter::Nearest;
33        self
34    }
35    pub fn with_bilinear_filtering(mut self) -> Self {
36        self.mipmap_level = MipmapLevel::Single;
37        self.minification_filter = MinificationFilter::Linear;
38        self
39    }
40    pub fn with_trilinear_filtering(mut self) -> Self {
41        self.mipmap_level = MipmapLevel::Double;
42        self.minification_filter = MinificationFilter::Linear;
43        self
44    }
45    pub fn with_rgba(mut self) -> Self {
46        self.color_format = ColorFormat::RGBA;
47        self
48    }
49    pub fn with_rgb(mut self) -> Self {
50        self.color_format = ColorFormat::RGB;
51        self
52    }
53}
54
55#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
56pub(crate) enum MagnificationFilter {
57    /// Pixellated
58    Nearest,
59    /// Blurred
60    Linear,
61}
62#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
63pub(crate) enum MinificationFilter {
64    /// Single pixel color lookup
65    Nearest,
66    /// Mixes colors of pixels
67    Linear,
68}
69#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
70pub(crate) enum MipmapLevel {
71    /// Use no mipmaps,
72    Off,
73    /// Use single mipmap lookup, may create sharp transitions between different minification degrees.
74    Single,
75    /// Linear interpolation between two mipmap lookups
76    Double,
77}
78#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
79pub(crate) enum ColorFormat {
80    /// Red, Green, and Blue
81    RGB,
82    /// Red, Green, Blue, and Alpha
83    RGBA,
84}
85
86impl MipmapLevel {
87    pub fn on(&self) -> bool {
88        *self != MipmapLevel::Off
89    }
90}
91impl MagnificationFilter {
92    pub(crate) fn webgl_num(&self) -> i32 {
93        (match self {
94            MagnificationFilter::Nearest => WebGlRenderingContext::NEAREST,
95            MagnificationFilter::Linear => WebGlRenderingContext::LINEAR,
96        }) as i32
97    }
98}
99impl MinificationFilter {
100    pub(crate) fn webgl_num(&self, mipmap: &MipmapLevel) -> i32 {
101        (match (self, mipmap) {
102            (MinificationFilter::Nearest, MipmapLevel::Off) => WebGlRenderingContext::NEAREST,
103            (MinificationFilter::Linear, MipmapLevel::Off) => WebGlRenderingContext::LINEAR,
104            (MinificationFilter::Nearest, MipmapLevel::Single) => {
105                WebGlRenderingContext::NEAREST_MIPMAP_NEAREST
106            }
107            (MinificationFilter::Linear, MipmapLevel::Single) => {
108                WebGlRenderingContext::LINEAR_MIPMAP_NEAREST
109            }
110            (MinificationFilter::Nearest, MipmapLevel::Double) => {
111                WebGlRenderingContext::NEAREST_MIPMAP_LINEAR
112            }
113            (MinificationFilter::Linear, MipmapLevel::Double) => {
114                WebGlRenderingContext::LINEAR_MIPMAP_LINEAR
115            }
116        }) as i32
117    }
118}
119impl ColorFormat {
120    pub(crate) fn webgl_num(&self) -> i32 {
121        (match self {
122            ColorFormat::RGB => WebGlRenderingContext::RGB,
123            ColorFormat::RGBA => WebGlRenderingContext::RGBA,
124        }) as i32
125    }
126}
127
128impl Default for TextureConfig {
129    fn default() -> Self {
130        Self {
131            magnification_filter: MagnificationFilter::Linear,
132            minification_filter: MinificationFilter::Linear,
133            mipmap_level: MipmapLevel::Single,
134            color_format: ColorFormat::RGBA,
135        }
136    }
137}