Last active
September 20, 2023 03:52
-
-
Save ethereumdegen/37d160afd4f5e829c6143785d66bda38 to your computer and use it in GitHub Desktop.
Loading a zip containing GLTF files in bevy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive( Default )] | |
pub struct ModelPackLoader; | |
//publish an example for this !!! | |
impl AssetLoader for ModelPackLoader { | |
type Asset = ModelPack; | |
type Settings = (); | |
fn load<'a>( | |
&'a self, | |
reader: &'a mut Reader, | |
_settings: &'a (), | |
load_context: &'a mut LoadContext, | |
) -> BoxedFuture<'a, Result<ModelPack, anyhow::Error>> { | |
Box::pin(async move { | |
let mut bytes = Vec::new(); | |
reader.read_to_end(&mut bytes).await?; | |
let cursor = Cursor::new(bytes); | |
let mut archive = ZipArchive::new(cursor).unwrap(); | |
let mut index_content = String::new(); | |
{ | |
let mut index_zip_file = archive.by_name("index.json")? ; | |
index_zip_file.read_to_string(&mut index_content)?; | |
} | |
//parse the manifest file using serde | |
let index:ModelPackIndex = serde_json::from_str(&index_content)? ; | |
let name = index.name; | |
let models_in_index = index.models; | |
let mut models = HashMap::new(); | |
let gltf_loader= | |
GltfLoader { | |
supported_compressed_formats: CompressedImageFormats::all(), | |
custom_vertex_attributes: HashMap::new(), | |
}; | |
//load the models ... | |
for (model_name, model_index_record) in models_in_index.iter() { | |
println!("loading model {}", model_name); | |
let gltf_path = &model_index_record.path; | |
let mut gltf_reader = get_future_safe_reader_from_zip_file( | |
archive.by_name( &gltf_path )? | |
)?; | |
let loaded_gltf = gltf_loader.load( | |
&mut gltf_reader, | |
&(), | |
load_context | |
).await?; | |
let handle = load_context.add_labeled_asset( | |
model_name.clone(), loaded_gltf) ; | |
models.insert( model_name.clone(), handle ); | |
} | |
let model_pack = ModelPack{ | |
name, | |
models | |
}; | |
Ok(model_pack) | |
}) | |
} | |
//MAKE SURE THIS IS RIGHT | |
fn extensions(&self) -> &[&str] { | |
&["modelpack"] | |
} | |
} | |
fn get_future_safe_reader_from_zip_file( mut zip_file_reader: ZipFile ) | |
-> Result< futures::io::Cursor<Vec<u8>> , anyhow::Error>{ | |
let mut color_texture_contents = Vec::new(); | |
zip_file_reader.read_to_end(&mut color_texture_contents)?; | |
Ok( futures::io::Cursor::new(color_texture_contents) ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment