mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-22 03:48:21 -06:00
load block textures
This commit is contained in:
parent
35ff06a439
commit
260f4b4232
|
@ -5,9 +5,7 @@ use kubi_shared::block::BlockTexture;
|
|||
use crate::{filesystem::AssetManager, hui_integration::UiState, rendering::Renderer};
|
||||
|
||||
mod texture;
|
||||
mod shaders;
|
||||
|
||||
//use texture::load_texture2darray_prefab;
|
||||
use texture::load_texture2darray_prefab;
|
||||
|
||||
pub trait AssetPaths {
|
||||
fn file_name(self) -> &'static str;
|
||||
|
@ -39,19 +37,7 @@ impl AssetPaths for BlockTexture {
|
|||
|
||||
#[derive(Unique)]
|
||||
#[repr(transparent)]
|
||||
pub struct BlockTexturesPrefab(pub wgpu::Texture);
|
||||
|
||||
// #[derive(Unique)]
|
||||
// #[repr(transparent)]
|
||||
// pub struct ChunkShaderPrefab(pub Program);
|
||||
|
||||
// #[derive(Unique)]
|
||||
// #[repr(transparent)]
|
||||
// pub struct ColoredShaderPrefab(pub Program);
|
||||
|
||||
// #[derive(Unique)]
|
||||
// #[repr(transparent)]
|
||||
// pub struct Colored2ShaderPrefab(pub Program);
|
||||
pub struct BlockDiffuseTexture(pub wgpu::Texture);
|
||||
|
||||
#[derive(Unique)]
|
||||
#[repr(transparent)]
|
||||
|
@ -63,15 +49,14 @@ pub fn load_prefabs(
|
|||
mut ui: NonSendSync<UniqueViewMut<UiState>>,
|
||||
assman: UniqueView<AssetManager>
|
||||
) {
|
||||
// log::info!("Loading textures...");
|
||||
// storages.add_unique_non_send_sync(BlockTexturesPrefab(
|
||||
// load_texture2darray_prefab::<BlockTexture, _>(
|
||||
// &assman,
|
||||
// "blocks".into(),
|
||||
// &renderer.display,
|
||||
// MipmapsOption::AutoGeneratedMipmaps
|
||||
// )
|
||||
// ));
|
||||
log::info!("Loading textures...");
|
||||
storages.add_unique_non_send_sync(BlockDiffuseTexture(
|
||||
load_texture2darray_prefab::<BlockTexture>(
|
||||
&renderer,
|
||||
&assman,
|
||||
"blocks".into(),
|
||||
)
|
||||
));
|
||||
|
||||
log::info!("Loading the UI stuff...");
|
||||
{
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
macro_rules! include_shader_prefab {
|
||||
($name: literal, $vert: literal, $frag: literal, $facade: expr) => {
|
||||
{
|
||||
use ::glium::{Program, program::ProgramCreationInput};
|
||||
log::info!("compiling shader {}", $name);
|
||||
Program::new(&*$facade, ProgramCreationInput::SourceCode {
|
||||
vertex_shader: include_str!($vert),
|
||||
fragment_shader: include_str!($frag),
|
||||
geometry_shader: None,
|
||||
tessellation_control_shader: None,
|
||||
tessellation_evaluation_shader: None,
|
||||
transform_feedback_varyings: None,
|
||||
outputs_srgb: false,
|
||||
uses_point_size: false,
|
||||
}).expect("Failed to compile gpu program")
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use include_shader_prefab;
|
|
@ -1,45 +1,75 @@
|
|||
// use strum::IntoEnumIterator;
|
||||
// use rayon::prelude::*;
|
||||
// use std::{path::PathBuf, io::BufReader};
|
||||
// use crate::filesystem::AssetManager;
|
||||
// use super::AssetPaths;
|
||||
use glam::UVec2;
|
||||
use strum::IntoEnumIterator;
|
||||
use rayon::prelude::*;
|
||||
use wgpu::util::{DeviceExt, TextureDataOrder};
|
||||
use std::{io::BufReader, path::PathBuf};
|
||||
use crate::{filesystem::AssetManager, rendering::Renderer};
|
||||
use super::AssetPaths;
|
||||
|
||||
// pub fn load_texture2darray_prefab<
|
||||
// T: AssetPaths + IntoEnumIterator,
|
||||
// E: Facade
|
||||
// >(
|
||||
// assman: &AssetManager,
|
||||
// directory: PathBuf,
|
||||
// facade: &E,
|
||||
// mipmaps: MipmapsOption,
|
||||
// ) -> SrgbTexture2dArray {
|
||||
// log::info!("started loading {}", directory.as_os_str().to_str().unwrap());
|
||||
// //Load raw images
|
||||
// let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect();
|
||||
// let raw_images: Vec<RawImage2d<u8>> = tex_files.par_iter().map(|&file_name| {
|
||||
// log::info!("loading texture {}", file_name);
|
||||
// //Get path to the image and open the file
|
||||
// let reader = {
|
||||
// let path = directory.join(file_name);
|
||||
// BufReader::new(assman.open_asset(&path).expect("Failed to open texture file"))
|
||||
// };
|
||||
// //Parse image data
|
||||
// let (image_data, dimensions) = {
|
||||
// let image = image::load(
|
||||
// reader,
|
||||
// image::ImageFormat::Png
|
||||
// ).unwrap().to_rgba8();
|
||||
// let dimensions = image.dimensions();
|
||||
// (image.into_raw(), dimensions)
|
||||
// };
|
||||
// //Create a glium RawImage
|
||||
// RawImage2d::from_raw_rgba_reversed(
|
||||
// &image_data,
|
||||
// dimensions
|
||||
// )
|
||||
// }).collect();
|
||||
// log::info!("done loading texture files, uploading to the gpu");
|
||||
// //Upload images to the GPU
|
||||
// SrgbTexture2dArray::with_mipmaps(facade, raw_images, mipmaps)
|
||||
// .expect("Failed to upload texture array to GPU")
|
||||
// }
|
||||
pub fn load_texture2darray_prefab<T: AssetPaths + IntoEnumIterator>(
|
||||
renderer: &Renderer,
|
||||
assman: &AssetManager,
|
||||
directory: PathBuf,
|
||||
) -> wgpu::Texture {
|
||||
log::info!("started loading {}", directory.as_os_str().to_str().unwrap());
|
||||
|
||||
//Load raw images
|
||||
let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect();
|
||||
let raw_images: Vec<(Vec<u8>, UVec2)> = tex_files.par_iter().map(|&file_name| {
|
||||
log::info!("loading texture {}", file_name);
|
||||
|
||||
//Get path to the image and open the file
|
||||
let reader = {
|
||||
let path = directory.join(file_name);
|
||||
BufReader::new(assman.open_asset(&path).expect("Failed to open texture file"))
|
||||
};
|
||||
|
||||
//Parse image data
|
||||
let (image_data, dimensions) = {
|
||||
let image = image::load(
|
||||
reader,
|
||||
image::ImageFormat::Png
|
||||
).unwrap().to_rgba8();
|
||||
let dimensions = image.dimensions();
|
||||
(image.into_raw(), dimensions)
|
||||
};
|
||||
(image_data, UVec2::from(dimensions))
|
||||
}).collect();
|
||||
|
||||
assert!(!raw_images.is_empty(), "no images loaded");
|
||||
//TODO: check same size
|
||||
|
||||
log::info!("done loading texture files, uploading to the gpu");
|
||||
|
||||
let size = raw_images[0].1;
|
||||
let layers = raw_images.len() as u32;
|
||||
|
||||
//Concat data into a single vec
|
||||
let mut data = Vec::with_capacity((size.x * size.y * layers * 4) as usize);
|
||||
for (layer_data, _) in raw_images {
|
||||
data.extend_from_slice(&layer_data);
|
||||
}
|
||||
|
||||
//Upload images to the GPU
|
||||
let desc = &wgpu::TextureDescriptor {
|
||||
label: Some("block_diffuse_texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: size.x,
|
||||
height: size.y,
|
||||
depth_or_array_layers: layers,
|
||||
},
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
view_formats: &[],
|
||||
};
|
||||
|
||||
renderer.device().create_texture_with_data(
|
||||
renderer.queue(),
|
||||
desc,
|
||||
TextureDataOrder::MipMajor,
|
||||
&data
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue