Skip to content

Commit

Permalink
GLTF loader: support mipmap filters (#1639)
Browse files Browse the repository at this point in the history
This removes the `GltfError::UnsupportedMinFilter` error.

I don't think this error should have existed in the first place, because it prevents users from using assets that bevy could totally render (without mipmap support as of yet).

It's much better to load the asset properly and then render it (even if it looks a little ugly), than to refuse to load the asset at all, giving users a confusing error.
  • Loading branch information
inodentry committed Mar 13, 2021
1 parent bbb9849 commit 2e72755
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ use crate::{Gltf, GltfNode};
pub enum GltfError {
#[error("unsupported primitive mode")]
UnsupportedPrimitive { mode: Mode },
#[error("unsupported min filter")]
UnsupportedMinFilter { filter: MinFilter },
#[error("invalid GLTF file")]
Gltf(#[from] gltf::Error),
#[error("binary blob is missing")]
Expand Down Expand Up @@ -199,7 +197,7 @@ async fn load_gltf<'a, 'b>(
let buffer = &buffer_data[view.buffer().index()][start..end];
let texture_label = texture_label(&gltf_texture);
let mut texture = Texture::from_buffer(buffer, ImageType::MimeType(mime_type))?;
texture.sampler = texture_sampler(&gltf_texture)?;
texture.sampler = texture_sampler(&gltf_texture);
load_context.set_labeled_asset::<Texture>(&texture_label, LoadedAsset::new(texture));
}
}
Expand Down Expand Up @@ -424,10 +422,10 @@ fn scene_label(scene: &gltf::Scene) -> String {
format!("Scene{}", scene.index())
}

fn texture_sampler(texture: &gltf::Texture) -> Result<SamplerDescriptor, GltfError> {
fn texture_sampler(texture: &gltf::Texture) -> SamplerDescriptor {
let gltf_sampler = texture.sampler();

Ok(SamplerDescriptor {
SamplerDescriptor {
address_mode_u: texture_address_mode(&gltf_sampler.wrap_s()),
address_mode_v: texture_address_mode(&gltf_sampler.wrap_t()),

Expand All @@ -442,15 +440,30 @@ fn texture_sampler(texture: &gltf::Texture) -> Result<SamplerDescriptor, GltfErr
min_filter: gltf_sampler
.min_filter()
.map(|mf| match mf {
MinFilter::Nearest => Ok(FilterMode::Nearest),
MinFilter::Linear => Ok(FilterMode::Linear),
filter => Err(GltfError::UnsupportedMinFilter { filter }),
MinFilter::Nearest
| MinFilter::NearestMipmapNearest
| MinFilter::NearestMipmapLinear => FilterMode::Nearest,
MinFilter::Linear
| MinFilter::LinearMipmapNearest
| MinFilter::LinearMipmapLinear => FilterMode::Linear,
})
.transpose()?
.unwrap_or(SamplerDescriptor::default().min_filter),

mipmap_filter: gltf_sampler
.min_filter()
.map(|mf| match mf {
MinFilter::Nearest
| MinFilter::Linear
| MinFilter::NearestMipmapNearest
| MinFilter::LinearMipmapNearest => FilterMode::Nearest,
MinFilter::NearestMipmapLinear | MinFilter::LinearMipmapLinear => {
FilterMode::Linear
}
})
.unwrap_or(SamplerDescriptor::default().mipmap_filter),

..Default::default()
})
}
}

fn texture_address_mode(gltf_address_mode: &gltf::texture::WrappingMode) -> AddressMode {
Expand Down

0 comments on commit 2e72755

Please sign in to comment.