Skip to content

Add image sampler configuration in GLTF loader #17875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 6, 2025
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ pub struct GltfLoaderSettings {
pub load_lights: bool,
/// If true, the loader will include the root of the gltf root node.
pub include_source: bool,
/// If some, the loader will ignore the gltf sampler data and generate materials with provided `ImageSamplerDescriptor`.
pub override_sampler: Option<ImageSamplerDescriptor>,
/// Anisotropic filtering level. Must be a power 1, 2, 4, 8 or 16.
/// Is passed into `ImageSamplerDescriptor` when parsing gltf sampler data.
pub anisotropy_clamp: u16,
}

impl Default for GltfLoaderSettings {
Expand All @@ -178,6 +183,8 @@ impl Default for GltfLoaderSettings {
load_cameras: true,
load_lights: true,
include_source: false,
override_sampler: None,
anisotropy_clamp: 1,
}
}
}
Expand Down Expand Up @@ -574,7 +581,7 @@ async fn load_gltf<'a, 'b, 'c>(
&linear_textures,
parent_path,
loader.supported_compressed_formats,
settings.load_materials,
settings,
)
.await?;
process_loaded_texture(load_context, &mut _texture_handles, image);
Expand All @@ -594,7 +601,7 @@ async fn load_gltf<'a, 'b, 'c>(
linear_textures,
parent_path,
loader.supported_compressed_formats,
settings.load_materials,
settings,
)
.await
});
Expand Down Expand Up @@ -1050,10 +1057,13 @@ async fn load_image<'a, 'b>(
linear_textures: &HashSet<usize>,
parent_path: &'b Path,
supported_compressed_formats: CompressedImageFormats,
render_asset_usages: RenderAssetUsages,
settings: &GltfLoaderSettings,
) -> Result<ImageOrPath, GltfError> {
let is_srgb = !linear_textures.contains(&gltf_texture.index());
let sampler_descriptor = texture_sampler(&gltf_texture);
let sampler_descriptor = settings
.override_sampler
.clone()
.unwrap_or(texture_sampler(&gltf_texture, settings.anisotropy_clamp));
#[cfg(all(debug_assertions, feature = "dds"))]
let name = gltf_texture
.name()
Expand All @@ -1071,7 +1081,7 @@ async fn load_image<'a, 'b>(
supported_compressed_formats,
is_srgb,
ImageSampler::Descriptor(sampler_descriptor),
render_asset_usages,
settings.load_materials,
)?;
Ok(ImageOrPath::Image {
image,
Expand All @@ -1095,7 +1105,7 @@ async fn load_image<'a, 'b>(
supported_compressed_formats,
is_srgb,
ImageSampler::Descriptor(sampler_descriptor),
render_asset_usages,
settings.load_materials,
)?,
label: GltfAssetLabel::Texture(gltf_texture.index()),
})
Expand Down Expand Up @@ -1810,7 +1820,7 @@ fn inverse_bind_matrices_label(skin: &gltf::Skin) -> String {
}

/// Extracts the texture sampler data from the glTF texture.
fn texture_sampler(texture: &gltf::Texture) -> ImageSamplerDescriptor {
fn texture_sampler(texture: &gltf::Texture, anisotropy_clamp: u16) -> ImageSamplerDescriptor {
let gltf_sampler = texture.sampler();

ImageSamplerDescriptor {
Expand All @@ -1823,7 +1833,10 @@ fn texture_sampler(texture: &gltf::Texture) -> ImageSamplerDescriptor {
MagFilter::Nearest => ImageFilterMode::Nearest,
MagFilter::Linear => ImageFilterMode::Linear,
})
.unwrap_or(ImageSamplerDescriptor::default().mag_filter),
.or(Some(ImageSamplerDescriptor::default().mag_filter))
// Enabling anisotropy with Nearest filters causes wgpu Validation Error
.filter(|_| anisotropy_clamp == 1)
.unwrap_or(ImageFilterMode::Linear),

min_filter: gltf_sampler
.min_filter()
Expand All @@ -1835,7 +1848,9 @@ fn texture_sampler(texture: &gltf::Texture) -> ImageSamplerDescriptor {
| MinFilter::LinearMipmapNearest
| MinFilter::LinearMipmapLinear => ImageFilterMode::Linear,
})
.unwrap_or(ImageSamplerDescriptor::default().min_filter),
.or(Some(ImageSamplerDescriptor::default().min_filter))
.filter(|_| anisotropy_clamp == 1)
.unwrap_or(ImageFilterMode::Linear),

mipmap_filter: gltf_sampler
.min_filter()
Expand All @@ -1848,7 +1863,11 @@ fn texture_sampler(texture: &gltf::Texture) -> ImageSamplerDescriptor {
ImageFilterMode::Linear
}
})
.unwrap_or(ImageSamplerDescriptor::default().mipmap_filter),
.or(Some(ImageSamplerDescriptor::default().mipmap_filter))
.filter(|_| anisotropy_clamp == 1)
.unwrap_or(ImageFilterMode::Linear),

anisotropy_clamp,

..Default::default()
}
Expand Down