Path: blob/main/crates/bevy_gltf/src/loader/gltf_ext/texture.rs
9418 views
use bevy_image::{ImageAddressMode, ImageFilterMode, ImageSamplerDescriptor};1use bevy_math::Affine2;23use gltf::texture::{MagFilter, MinFilter, Texture, TextureTransform, WrappingMode};45/// Extracts the texture sampler data from the glTF [`Texture`].6pub(crate) fn texture_sampler(7texture: &Texture<'_>,8default_sampler: &ImageSamplerDescriptor,9) -> ImageSamplerDescriptor {10let gltf_sampler = texture.sampler();11let mut sampler = default_sampler.clone();1213sampler.address_mode_u = address_mode(&gltf_sampler.wrap_s());14sampler.address_mode_v = address_mode(&gltf_sampler.wrap_t());1516// Shouldn't parse filters when anisotropic filtering is on, because trilinear is then required by wgpu.17// We also trust user to have provided a valid sampler.18if sampler.anisotropy_clamp == 1 {19if let Some(mag_filter) = gltf_sampler.mag_filter().map(|mf| match mf {20MagFilter::Nearest => ImageFilterMode::Nearest,21MagFilter::Linear => ImageFilterMode::Linear,22}) {23sampler.mag_filter = mag_filter;24}25if let Some(min_filter) = gltf_sampler.min_filter().map(|mf| match mf {26MinFilter::Nearest27| MinFilter::NearestMipmapNearest28| MinFilter::NearestMipmapLinear => ImageFilterMode::Nearest,29MinFilter::Linear | MinFilter::LinearMipmapNearest | MinFilter::LinearMipmapLinear => {30ImageFilterMode::Linear31}32}) {33sampler.min_filter = min_filter;34}35if let Some(mipmap_filter) = gltf_sampler.min_filter().map(|mf| match mf {36MinFilter::Nearest37| MinFilter::Linear38| MinFilter::NearestMipmapNearest39| MinFilter::LinearMipmapNearest => ImageFilterMode::Nearest,40MinFilter::NearestMipmapLinear | MinFilter::LinearMipmapLinear => {41ImageFilterMode::Linear42}43}) {44sampler.mipmap_filter = mipmap_filter;45}46}47sampler48}4950pub(crate) fn address_mode(wrapping_mode: &WrappingMode) -> ImageAddressMode {51match wrapping_mode {52WrappingMode::ClampToEdge => ImageAddressMode::ClampToEdge,53WrappingMode::Repeat => ImageAddressMode::Repeat,54WrappingMode::MirroredRepeat => ImageAddressMode::MirrorRepeat,55}56}5758pub(crate) fn texture_transform_to_affine2(texture_transform: TextureTransform) -> Affine2 {59Affine2::from_scale_angle_translation(60texture_transform.scale().into(),61-texture_transform.rotation(),62texture_transform.offset().into(),63)64}656667