Skip to content

Commit

Permalink
[Vent-Assets] glTF Implement Wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 6, 2023
1 parent 878bb1e commit 82c997a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 39 deletions.
3 changes: 1 addition & 2 deletions crates/vent-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ pub mod pool;
pub mod shader;
pub mod texture;

pub trait Asset {
}
pub trait Asset {}

pub trait Vertex<'a> {
const LAYOUT: wgpu::VertexBufferLayout<'a>;
Expand Down
35 changes: 29 additions & 6 deletions crates/vent-assets/src/model/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ struct Sampler {
mag_filter: wgpu::FilterMode,
min_filter: wgpu::FilterMode,
mipmap_filter: wgpu::FilterMode,
address_mode_u: wgpu::AddressMode,
address_mode_v: wgpu::AddressMode,
}

pub(crate) struct GLTFLoader {}
Expand Down Expand Up @@ -103,15 +105,24 @@ impl GLTFLoader {
)
.unwrap(),
gltf::image::Source::Uri { uri, mime_type: _ } => {
let sampler = Self::convert_sampler(texture.texture().sampler());
let sampler = texture.texture().sampler();
let wgpu_sampler = Self::convert_sampler(&sampler);

let sampler_desc = &wgpu::SamplerDescriptor {
label: sampler.name(),
mag_filter: wgpu_sampler.mag_filter,
min_filter: wgpu_sampler.min_filter,
mipmap_filter: wgpu_sampler.mipmap_filter,
address_mode_u: wgpu_sampler.address_mode_u,
address_mode_v: wgpu_sampler.address_mode_v,
..Default::default()
};

Texture::from_image(
device,
queue,
&image::open(model_dir.join(uri)).unwrap(),
Some(sampler.mag_filter),
Some(sampler.min_filter),
Some(sampler.mipmap_filter),
texture.texture().sampler().name(),
Some(sampler_desc),
None,
)
.unwrap()
Expand All @@ -138,7 +149,7 @@ impl GLTFLoader {
}

/// Converts an gltf Texture Sampler into WGPU Filter Modes
fn convert_sampler(sampler: gltf::texture::Sampler) -> Sampler {
fn convert_sampler(sampler: &gltf::texture::Sampler) -> Sampler {
let mag_filter = if let Some(filter) = sampler.mag_filter() {
match filter {
gltf::texture::MagFilter::Nearest => wgpu::FilterMode::Nearest,
Expand Down Expand Up @@ -166,10 +177,22 @@ impl GLTFLoader {
Texture::DEFAULT_TEXTURE_FILTER,
)
};
let address_mode_u = match sampler.wrap_s() {
gltf::texture::WrappingMode::ClampToEdge => wgpu::AddressMode::ClampToEdge,
gltf::texture::WrappingMode::MirroredRepeat => wgpu::AddressMode::MirrorRepeat,
gltf::texture::WrappingMode::Repeat => wgpu::AddressMode::Repeat,
};
let address_mode_v = match sampler.wrap_t() {
gltf::texture::WrappingMode::ClampToEdge => wgpu::AddressMode::ClampToEdge,
gltf::texture::WrappingMode::MirroredRepeat => wgpu::AddressMode::MirrorRepeat,
gltf::texture::WrappingMode::Repeat => wgpu::AddressMode::Repeat,
};
Sampler {
mag_filter,
min_filter,
mipmap_filter,
address_mode_u,
address_mode_v,
}
}

Expand Down
12 changes: 10 additions & 2 deletions crates/vent-assets/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;

use vent_dev::utils::stopwatch::Stopwatch;
use wgpu::util::DeviceExt;
use wgpu::{BindGroupLayout, Device};

Expand All @@ -26,9 +27,16 @@ impl Model3D {
path: &Path,
texture_bind_group_layout: &BindGroupLayout,
) -> Self {
load_model_from_path(device, queue, path, texture_bind_group_layout)
let sw = Stopwatch::new_and_start();
let model = load_model_from_path(device, queue, path, texture_bind_group_layout)
.await
.expect("Failed to Load 3D Model")
.expect("Failed to Load 3D Model");
log::info!(
"Model {} took {}ms to Load",
path.to_str().unwrap(),
sw.elapsed_ms()
);
model
}

pub fn draw<'rp>(&'rp self, rpass: &mut wgpu::RenderPass<'rp>) {
Expand Down
3 changes: 0 additions & 3 deletions crates/vent-assets/src/model/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ impl OBJLoader {
queue,
&image::open(model_dir.join(&texture)).unwrap(),
None,
None,
None,
None,
Some(&texture),
)
.unwrap()
Expand Down
37 changes: 12 additions & 25 deletions crates/vent-assets/src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use image::{ImageError};
use image::ImageError;
use wgpu::util::DeviceExt;

use crate::Texture;
Expand Down Expand Up @@ -40,17 +40,14 @@ impl Texture {
label: Option<&str>,
) -> Result<Self, ImageError> {
let img = image::load_from_memory(bytes)?;
Self::from_image(device, queue, &img, None, None, None, None, label)
Self::from_image(device, queue, &img, None, label)
}

pub fn from_image(
device: &wgpu::Device,
queue: &wgpu::Queue,
img: &image::DynamicImage,
mag_filter: Option<wgpu::FilterMode>,
min_filter: Option<wgpu::FilterMode>,
mipmap_filter: Option<wgpu::FilterMode>,
sampler_label: Option<&str>,
sampler_desc: Option<&wgpu::SamplerDescriptor>,
texture_label: Option<&str>,
) -> Result<Self, ImageError> {
Self::create(
Expand All @@ -60,10 +57,7 @@ impl Texture {
img.width(),
img.height(),
Self::DEFAULT_TEXTURE_FORMAT,
mag_filter.unwrap_or(Self::DEFAULT_TEXTURE_FILTER),
min_filter.unwrap_or(Self::DEFAULT_TEXTURE_FILTER),
mipmap_filter.unwrap_or(Self::DEFAULT_TEXTURE_FILTER),
sampler_label,
sampler_desc.unwrap_or(&wgpu::SamplerDescriptor::default()),
texture_label,
)
}
Expand Down Expand Up @@ -92,10 +86,12 @@ impl Texture {
width,
height,
Self::DEFAULT_TEXTURE_FORMAT,
Self::DEFAULT_TEXTURE_FILTER,
Self::DEFAULT_TEXTURE_FILTER,
Self::DEFAULT_TEXTURE_FILTER,
None,
&wgpu::SamplerDescriptor {
mag_filter: Self::DEFAULT_TEXTURE_FILTER,
min_filter: Self::DEFAULT_TEXTURE_FILTER,
mipmap_filter: Self::DEFAULT_TEXTURE_FILTER,
..Default::default()
},
label,
)
}
Expand All @@ -107,10 +103,7 @@ impl Texture {
width: u32,
height: u32,
format: wgpu::TextureFormat,
mag_filter: wgpu::FilterMode,
min_filter: wgpu::FilterMode,
mipmap_filter: wgpu::FilterMode,
sampler_label: Option<&str>,
sampler_desc: &wgpu::SamplerDescriptor,
texture_label: Option<&str>,
) -> Result<Self, ImageError> {
let size = wgpu::Extent3d {
Expand All @@ -134,13 +127,7 @@ impl Texture {
);

let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: sampler_label,
mag_filter,
min_filter,
mipmap_filter,
..Default::default()
});
let sampler = device.create_sampler(sampler_desc);

Ok(Self {
texture,
Expand Down
5 changes: 4 additions & 1 deletion crates/vent-editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ fn main() {
init_panic_hook();
#[cfg(not(target_arch = "wasm32"))]
{
SimpleLogger::new().init().unwrap();
SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.init()
.unwrap();
};

let path = concat!(
Expand Down

0 comments on commit 82c997a

Please sign in to comment.