Refactor gltf texture conversions
This commit is contained in:
parent
89c9b1e569
commit
47ebcf6fed
1 changed files with 20 additions and 24 deletions
|
@ -47,11 +47,10 @@ pub async fn load_model_gltf(
|
|||
|
||||
let diffuse_data = &mut images[diffuse_index];
|
||||
|
||||
if diffuse_data.format == gltf::image::Format::R8G8B8
|
||||
|| diffuse_data.format == gltf::image::Format::R16G16B16
|
||||
{
|
||||
diffuse_data.pixels =
|
||||
gltf_pixels_to_wgpu(diffuse_data.pixels.clone(), diffuse_data.format);
|
||||
let diffuse_channels = diffuse_data.pixels.len() / (diffuse_data.width * diffuse_data.height) as usize;
|
||||
assert!(diffuse_channels == 3 || diffuse_channels == 4, "gltf: diffuse tex unsupported channels count {:?}", diffuse_channels);
|
||||
if diffuse_channels == 3 {
|
||||
diffuse_data.pixels = gltf_pixels_to_wgpu(diffuse_data.pixels.clone(), diffuse_data.format);
|
||||
}
|
||||
|
||||
let diffuse_texture = Texture::from_pixels(
|
||||
|
@ -75,12 +74,10 @@ pub async fn load_model_gltf(
|
|||
.unwrap_or(0); // TODO default tex
|
||||
|
||||
let normal_data = &mut images[normal_index];
|
||||
|
||||
if normal_data.format == gltf::image::Format::R8G8B8
|
||||
|| normal_data.format == gltf::image::Format::R16G16B16
|
||||
{
|
||||
normal_data.pixels =
|
||||
gltf_pixels_to_wgpu(normal_data.pixels.clone(), normal_data.format);
|
||||
let normal_channels = normal_data.pixels.len() / (normal_data.width * normal_data.height) as usize;
|
||||
assert!(normal_channels == 3 || normal_channels == 4, "gltf: normal tex unsupported channels count {:?}", normal_channels);
|
||||
if normal_channels == 3 {
|
||||
normal_data.pixels = gltf_pixels_to_wgpu(normal_data.pixels.clone(), normal_data.format);
|
||||
}
|
||||
|
||||
let normal_texture = Texture::from_pixels(
|
||||
|
@ -104,13 +101,10 @@ pub async fn load_model_gltf(
|
|||
.unwrap_or(0); // TODO default tex
|
||||
|
||||
let rm_data = &mut images[rm_index];
|
||||
// dbg!(rm_data.format);
|
||||
|
||||
if rm_data.format == gltf::image::Format::R8G8B8
|
||||
|| rm_data.format == gltf::image::Format::R16G16B16
|
||||
{
|
||||
rm_data.pixels =
|
||||
gltf_pixels_to_wgpu(rm_data.pixels.clone(), rm_data.format);
|
||||
let rm_channels = rm_data.pixels.len() / (rm_data.width * rm_data.height) as usize;
|
||||
assert!(rm_channels == 3 || rm_channels == 4, "gltf: rm tex unsupported channels count {:?}", rm_channels);
|
||||
if rm_channels == 3 {
|
||||
rm_data.pixels = gltf_pixels_to_wgpu(rm_data.pixels.clone(), rm_data.format);
|
||||
}
|
||||
|
||||
let rm_texture = Texture::from_pixels(
|
||||
|
@ -146,11 +140,11 @@ fn gltf_image_format_to_wgpu(format: gltf::image::Format, srgb: bool) -> wgpu::T
|
|||
return match format {
|
||||
gltf::image::Format::R8 => panic!(),
|
||||
gltf::image::Format::R8G8 => panic!(),
|
||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8UnormSrgb, // converted
|
||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8UnormSrgb, // gltf_pixels_to_wgpu
|
||||
gltf::image::Format::R8G8B8A8 => wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
gltf::image::Format::R16 => panic!(),
|
||||
gltf::image::Format::R16G16 => panic!(),
|
||||
gltf::image::Format::R16G16B16 => panic!(), // converted
|
||||
gltf::image::Format::R16G16B16 => panic!(), // gltf_pixels_to_wgpu
|
||||
gltf::image::Format::R16G16B16A16 => panic!(),
|
||||
gltf::image::Format::R32G32B32FLOAT => panic!(),
|
||||
gltf::image::Format::R32G32B32A32FLOAT => panic!(),
|
||||
|
@ -160,11 +154,11 @@ fn gltf_image_format_to_wgpu(format: gltf::image::Format, srgb: bool) -> wgpu::T
|
|||
match format {
|
||||
gltf::image::Format::R8 => wgpu::TextureFormat::R8Unorm,
|
||||
gltf::image::Format::R8G8 => wgpu::TextureFormat::Rg8Unorm,
|
||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8Unorm, // converted
|
||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8Unorm, // gltf_pixels_to_wgpu
|
||||
gltf::image::Format::R8G8B8A8 => wgpu::TextureFormat::Rgba8Unorm,
|
||||
gltf::image::Format::R16 => wgpu::TextureFormat::R16Unorm,
|
||||
gltf::image::Format::R16G16 => wgpu::TextureFormat::Rg16Unorm,
|
||||
gltf::image::Format::R16G16B16 => wgpu::TextureFormat::Rgba16Unorm, // converted
|
||||
gltf::image::Format::R16G16B16 => wgpu::TextureFormat::Rgba16Unorm, // gltf_pixels_to_wgpu
|
||||
gltf::image::Format::R16G16B16A16 => wgpu::TextureFormat::Rgba16Unorm,
|
||||
gltf::image::Format::R32G32B32FLOAT => wgpu::TextureFormat::Rgba32Float,
|
||||
gltf::image::Format::R32G32B32A32FLOAT => wgpu::TextureFormat::Rgba32Float,
|
||||
|
@ -175,11 +169,11 @@ fn gltf_image_format_stride(format: gltf::image::Format) -> u32 {
|
|||
match format {
|
||||
gltf::image::Format::R8 => 1,
|
||||
gltf::image::Format::R8G8 => 2,
|
||||
gltf::image::Format::R8G8B8 => 4, // converted
|
||||
gltf::image::Format::R8G8B8 => 4, // gltf_pixels_to_wgpu
|
||||
gltf::image::Format::R8G8B8A8 => 4,
|
||||
gltf::image::Format::R16 => 2,
|
||||
gltf::image::Format::R16G16 => 4,
|
||||
gltf::image::Format::R16G16B16 => 8, // converted
|
||||
gltf::image::Format::R16G16B16 => 8, // gltf_pixels_to_wgpu
|
||||
gltf::image::Format::R16G16B16A16 => 8,
|
||||
gltf::image::Format::R32G32B32FLOAT => 12,
|
||||
gltf::image::Format::R32G32B32A32FLOAT => 16,
|
||||
|
@ -202,6 +196,8 @@ fn gltf_pixels_to_wgpu(mut bytes: Vec<u8>, format: gltf::image::Format) -> Vec<u
|
|||
.chunks_exact(6)
|
||||
.flat_map(|s| [s[0], s[1], s[2], s[3], s[4], s[5], 255, 255])
|
||||
.collect();
|
||||
} else {
|
||||
panic!("gltf_pixels_to_wgpu: Unsupported format {:?}", format);
|
||||
}
|
||||
|
||||
bytes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue