Skip to content

Commit

Permalink
Add SamplerDescriptor to Texture (#399) (#747)
Browse files Browse the repository at this point in the history
GLTF loader now grabs (some) sampler information from the respective
GLTF sampler struct.
  • Loading branch information
W4RH4WK authored Oct 29, 2020
1 parent fb2b19d commit 1b051d3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
48 changes: 46 additions & 2 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ use bevy_render::{
mesh::{Indices, Mesh, VertexAttribute},
pipeline::PrimitiveTopology,
prelude::{Color, Texture},
texture::TextureFormat,
texture::{AddressMode, FilterMode, SamplerDescriptor, TextureFormat},
};
use bevy_scene::Scene;
use bevy_transform::{
hierarchy::{BuildWorldChildren, WorldChildBuilder},
prelude::{GlobalTransform, Transform},
};
use gltf::{mesh::Mode, Primitive};
use gltf::{
mesh::Mode,
texture::{MagFilter, MinFilter, WrappingMode},
Primitive,
};
use image::{GenericImageView, ImageFormat};
use std::path::Path;
use thiserror::Error;
Expand All @@ -24,6 +28,8 @@ use thiserror::Error;
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 @@ -133,6 +139,7 @@ async fn load_gltf<'a, 'b>(
data: image.clone().into_vec(),
size: bevy_math::f32::vec2(size.0 as f32, size.1 as f32),
format: TextureFormat::Rgba8Unorm,
sampler: texture_sampler(&texture)?,
}),
);
}
Expand Down Expand Up @@ -263,6 +270,43 @@ fn texture_label(texture: &gltf::Texture) -> String {
format!("Texture{}", texture.index())
}

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

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

mag_filter: gltf_sampler
.mag_filter()
.map(|mf| match mf {
MagFilter::Nearest => FilterMode::Nearest,
MagFilter::Linear => FilterMode::Linear,
})
.unwrap_or(SamplerDescriptor::default().mag_filter),

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 }),
})
.transpose()?
.unwrap_or(SamplerDescriptor::default().min_filter),

..Default::default()
})
}

fn texture_address_mode(gltf_address_mode: &gltf::texture::WrappingMode) -> AddressMode {
match gltf_address_mode {
WrappingMode::ClampToEdge => AddressMode::ClampToEdge,
WrappingMode::Repeat => AddressMode::Repeat,
WrappingMode::MirroredRepeat => AddressMode::MirrorRepeat,
}
}

fn get_primitive_topology(mode: Mode) -> Result<PrimitiveTopology, GltfError> {
match mode {
Mode::Points => Ok(PrimitiveTopology::PointList),
Expand Down
20 changes: 1 addition & 19 deletions crates/bevy_render/src/texture/sampler_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::Texture;
use crate::pipeline::CompareFunction;
use std::num::NonZeroU8;

/// Describes a sampler
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub struct SamplerDescriptor {
pub address_mode_u: AddressMode,
pub address_mode_v: AddressMode,
Expand Down Expand Up @@ -34,23 +33,6 @@ impl Default for SamplerDescriptor {
}
}

impl From<&Texture> for SamplerDescriptor {
fn from(_texture: &Texture) -> Self {
SamplerDescriptor {
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
mag_filter: FilterMode::Nearest,
min_filter: FilterMode::Linear,
mipmap_filter: FilterMode::Nearest,
lod_min_clamp: 0.0,
lod_max_clamp: std::f32::MAX,
compare_function: None,
anisotropy_clamp: None,
}
}
}

/// How edges should be handled in texture addressing.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum AddressMode {
Expand Down
12 changes: 9 additions & 3 deletions crates/bevy_render/src/texture/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Texture {
pub data: Vec<u8>,
pub size: Vec2,
pub format: TextureFormat,
pub sampler: SamplerDescriptor,
}

impl Default for Texture {
Expand All @@ -26,6 +27,7 @@ impl Default for Texture {
data: Default::default(),
size: Default::default(),
format: TextureFormat::Rgba8UnormSrgb,
sampler: Default::default(),
}
}
}
Expand All @@ -37,7 +39,12 @@ impl Texture {
data.len(),
"Pixel data, size and format have to match",
);
Self { data, size, format }
Self {
data,
size,
format,
..Default::default()
}
}

pub fn new_fill(size: Vec2, pixel: &[u8], format: TextureFormat) -> Self {
Expand Down Expand Up @@ -104,8 +111,7 @@ impl Texture {
let texture_descriptor: TextureDescriptor = texture.into();
let texture_resource = render_resource_context.create_texture(texture_descriptor);

let sampler_descriptor: SamplerDescriptor = texture.into();
let sampler_resource = render_resource_context.create_sampler(&sampler_descriptor);
let sampler_resource = render_resource_context.create_sampler(&texture.sampler);

render_resource_context.set_asset_resource(
texture_handle,
Expand Down

0 comments on commit 1b051d3

Please sign in to comment.