Skip to content

Commit

Permalink
Advanced mipmap settings (replaces #41)
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Nov 20, 2024
1 parent 9f6897f commit 5a5c1ec
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
44 changes: 30 additions & 14 deletions src/io/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,22 +390,38 @@ fn parse_texture<'a>(
Some(::gltf::texture::MagFilter::Linear) => Interpolation::Linear,
None => tex.mag_filter,
};
(tex.min_filter, tex.mip_map_filter) = match sampler.min_filter() {
(tex.min_filter, tex.mipmap) = match sampler.min_filter() {
Some(::gltf::texture::MinFilter::Nearest) => (Interpolation::Nearest, None),
Some(::gltf::texture::MinFilter::Linear) => (Interpolation::Linear, None),
Some(::gltf::texture::MinFilter::NearestMipmapNearest) => {
(Interpolation::Nearest, Some(Interpolation::Nearest))
}
Some(::gltf::texture::MinFilter::LinearMipmapNearest) => {
(Interpolation::Linear, Some(Interpolation::Nearest))
}
Some(::gltf::texture::MinFilter::NearestMipmapLinear) => {
(Interpolation::Nearest, Some(Interpolation::Linear))
}
Some(::gltf::texture::MinFilter::LinearMipmapLinear) => {
(Interpolation::Linear, Some(Interpolation::Linear))
}
None => (tex.min_filter, tex.mip_map_filter),
Some(::gltf::texture::MinFilter::NearestMipmapNearest) => (
Interpolation::Nearest,
Some(Mipmap {
filter: Interpolation::Nearest,
..Default::default()
}),
),
Some(::gltf::texture::MinFilter::LinearMipmapNearest) => (
Interpolation::Linear,
Some(Mipmap {
filter: Interpolation::Nearest,
..Default::default()
}),
),
Some(::gltf::texture::MinFilter::NearestMipmapLinear) => (
Interpolation::Nearest,
Some(Mipmap {
filter: Interpolation::Linear,
..Default::default()
}),
),
Some(::gltf::texture::MinFilter::LinearMipmapLinear) => (
Interpolation::Linear,
Some(Mipmap {
filter: Interpolation::Linear,
..Default::default()
}),
),
None => (tex.min_filter, tex.mipmap),
};
tex.wrap_s = sampler.wrap_s().into();
tex.wrap_t = sampler.wrap_t().into();
Expand Down
24 changes: 24 additions & 0 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ impl Default for Interpolation {
}
}

/// Mipmap settings for a texture.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Mipmap {
/// Specifies what type of interpolation to use between the two closest mipmaps.
pub filter: Interpolation,
/// Specifies the maximum number of mipmap levels that should be created for the texture.
/// If this is 1, no mip maps will be created.
pub max_levels: u32,
/// Specifies the maximum ratio of anisotropy to be used when creating mipmaps for the texture.
/// If this is 1, only isotropic mipmaps will be created.
pub max_ratio: u32,
}

impl Default for Mipmap {
fn default() -> Self {
Self {
filter: Interpolation::Linear,
max_levels: u32::MAX,
max_ratio: 1,
}
}
}

///
/// Possible wrapping modes for a texture which determines how the texture is applied outside of the
/// [0..1] uv coordinate range.
Expand Down
8 changes: 4 additions & 4 deletions src/texture/texture2d.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[doc(inline)]
pub use crate::texture::{Interpolation, TextureData, Wrapping};
pub use super::{Interpolation, Mipmap, TextureData, Wrapping};

///
/// A CPU-side version of a 2D texture.
Expand All @@ -19,8 +19,8 @@ pub struct Texture2D {
pub min_filter: Interpolation,
/// The way the pixel data is interpolated when the texture is close
pub mag_filter: Interpolation,
/// Specifies whether mipmaps should be created for this texture and what type of interpolation to use between the two closest mipmaps.
pub mip_map_filter: Option<Interpolation>,
/// Specifies the [Mipmap] settings for this texture. If this is `None`, no mipmaps are created.
pub mipmap: Option<Mipmap>,
/// Determines how the texture is sampled outside the [0..1] s coordinate range (the first value of the uv coordinates).
pub wrap_s: Wrapping,
/// Determines how the texture is sampled outside the [0..1] t coordinate range (the second value of the uv coordinates).
Expand All @@ -36,7 +36,7 @@ impl Default for Texture2D {
height: 1,
min_filter: Interpolation::Linear,
mag_filter: Interpolation::Linear,
mip_map_filter: Some(Interpolation::Linear),
mipmap: Some(Mipmap::default()),
wrap_s: Wrapping::Repeat,
wrap_t: Wrapping::Repeat,
}
Expand Down
8 changes: 4 additions & 4 deletions src/texture/texture3d.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[doc(inline)]
pub use crate::texture::{Interpolation, TextureData, Wrapping};
pub use super::{Interpolation, Mipmap, TextureData, Wrapping};

///
/// A CPU-side version of a 3D texture.
Expand All @@ -21,8 +21,8 @@ pub struct Texture3D {
pub min_filter: Interpolation,
/// The way the pixel data is interpolated when the texture is close
pub mag_filter: Interpolation,
/// Specifies whether mipmaps should be created for this texture and what type of interpolation to use between the two closest mipmaps.
pub mip_map_filter: Option<Interpolation>,
/// Specifies the [Mipmap] settings for this texture. If this is `None`, no mipmaps are created.
pub mipmap: Option<Mipmap>,
/// Determines how the texture is sampled outside the [0..1] s coordinate range (the first value of the uvw coordinates).
pub wrap_s: Wrapping,
/// Determines how the texture is sampled outside the [0..1] t coordinate range (the second value of the uvw coordinates).
Expand All @@ -41,7 +41,7 @@ impl Default for Texture3D {
depth: 1,
min_filter: Interpolation::Linear,
mag_filter: Interpolation::Linear,
mip_map_filter: Some(Interpolation::Linear),
mipmap: None,
wrap_s: Wrapping::Repeat,
wrap_t: Wrapping::Repeat,
wrap_r: Wrapping::Repeat,
Expand Down

0 comments on commit 5a5c1ec

Please sign in to comment.