Compare commits
1 Commits
1b8c3de6a9
...
7db0403408
Author | SHA1 | Date |
---|---|---|
Mark Moissette | 7db0403408 |
|
@ -151,7 +151,7 @@ impl Plugin for BlueprintsPlugin {
|
|||
compute_scene_aabbs.run_if(aabbs_enabled),
|
||||
apply_deferred.run_if(aabbs_enabled),
|
||||
apply_deferred,
|
||||
(materials_inject, check_for_material_loaded, materials_inject2).chain().run_if(materials_library_enabled),
|
||||
materials_inject.run_if(materials_library_enabled),
|
||||
)
|
||||
.chain()
|
||||
.in_set(GltfBlueprintsSet::Spawn),
|
||||
|
|
|
@ -1,19 +1,22 @@
|
|||
use std::path::Path;
|
||||
|
||||
use bevy::{
|
||||
asset::{AssetId, AssetServer, Assets, Handle},
|
||||
asset::{AssetServer, Assets, Handle},
|
||||
ecs::{
|
||||
component::Component, entity::Entity, query::{Added, With}, reflect::ReflectComponent, system::{Commands, Query, Res, ResMut}
|
||||
component::Component,
|
||||
query::{Added, With},
|
||||
reflect::ReflectComponent,
|
||||
system::{Commands, Query, Res, ResMut},
|
||||
},
|
||||
gltf::Gltf,
|
||||
hierarchy::{Children, Parent},
|
||||
log::{debug, info},
|
||||
log::debug,
|
||||
pbr::StandardMaterial,
|
||||
reflect::Reflect,
|
||||
render::mesh::Mesh,
|
||||
};
|
||||
|
||||
use crate::{AssetLoadTracker, AssetsToLoad, BluePrintsConfig};
|
||||
use crate::BluePrintsConfig;
|
||||
|
||||
#[derive(Component, Reflect, Default, Debug)]
|
||||
#[reflect(Component)]
|
||||
|
@ -23,110 +26,10 @@ pub struct MaterialInfo {
|
|||
pub source: String,
|
||||
}
|
||||
|
||||
/// flag component
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintMaterialAssetsLoaded;
|
||||
/// flag component
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintMaterialAssetsNotLoaded;
|
||||
|
||||
|
||||
|
||||
/// system that injects / replaces materials from material library
|
||||
pub(crate) fn materials_inject(
|
||||
blueprints_config: ResMut<BluePrintsConfig>,
|
||||
material_infos: Query<(Entity, &MaterialInfo), Added<MaterialInfo>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (entity, material_info) in material_infos.iter() {
|
||||
let model_file_name = format!(
|
||||
"{}_materials_library.{}",
|
||||
&material_info.source, &blueprints_config.format
|
||||
);
|
||||
let materials_path = Path::new(&blueprints_config.material_library_folder)
|
||||
.join(Path::new(model_file_name.as_str()));
|
||||
let material_name = &material_info.name;
|
||||
let material_full_path = materials_path.to_str().unwrap().to_string() + "#" + material_name; // TODO: yikes, cleanup
|
||||
|
||||
if blueprints_config
|
||||
.material_library_cache
|
||||
.contains_key(&material_full_path)
|
||||
{
|
||||
debug!("material is cached, retrieving");
|
||||
blueprints_config
|
||||
.material_library_cache
|
||||
.get(&material_full_path)
|
||||
.expect("we should have the material available");
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(BlueprintMaterialAssetsLoaded);
|
||||
|
||||
} else {
|
||||
let material_file_handle: Handle<Gltf> = asset_server.load(materials_path.clone());
|
||||
let material_file_id = material_file_handle.id();
|
||||
println!("loading material {} {}", material_full_path, material_file_id);
|
||||
let mut asset_infos:Vec<AssetLoadTracker<Gltf>> = vec![];
|
||||
|
||||
asset_infos.push(AssetLoadTracker {
|
||||
name: material_full_path,
|
||||
id: material_file_id,
|
||||
loaded: false,
|
||||
handle: material_file_handle.clone()
|
||||
});
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(AssetsToLoad{
|
||||
all_loaded: false,
|
||||
asset_infos: asset_infos,
|
||||
progress: 0.0
|
||||
//..Default::default()
|
||||
})
|
||||
.insert(BlueprintMaterialAssetsNotLoaded);
|
||||
/**/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO, merge with check_for_loaded, make generic ?
|
||||
pub(crate) fn check_for_material_loaded(
|
||||
mut blueprint_assets_to_load: Query<(Entity, &mut AssetsToLoad<Gltf>),With<BlueprintMaterialAssetsNotLoaded>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
){
|
||||
for (entity, mut assets_to_load) in blueprint_assets_to_load.iter_mut(){
|
||||
let mut all_loaded = true;
|
||||
let mut loaded_amount = 0;
|
||||
let total = assets_to_load.asset_infos.len();
|
||||
for tracker in assets_to_load.asset_infos.iter_mut(){
|
||||
let asset_id = tracker.id;
|
||||
let loaded = asset_server.is_loaded_with_dependencies(asset_id);
|
||||
tracker.loaded = loaded;
|
||||
if loaded {
|
||||
println!("loaded {} {}", tracker.name, tracker.id);
|
||||
loaded_amount += 1;
|
||||
}else{
|
||||
all_loaded = false;
|
||||
}
|
||||
}
|
||||
let progress:f32 = loaded_amount as f32 / total as f32;
|
||||
println!("progress (materials): {}",progress);
|
||||
assets_to_load.progress = progress;
|
||||
|
||||
if all_loaded {
|
||||
assets_to_load.all_loaded = true;
|
||||
commands.entity(entity)
|
||||
.insert(BlueprintMaterialAssetsLoaded)
|
||||
.remove::<BlueprintMaterialAssetsNotLoaded>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// system that injects / replaces materials from material library
|
||||
pub(crate) fn materials_inject2(
|
||||
mut blueprints_config: ResMut<BluePrintsConfig>,
|
||||
material_infos: Query<(&MaterialInfo, &Children, ), (Added<BlueprintMaterialAssetsLoaded>, With<BlueprintMaterialAssetsLoaded>)>,
|
||||
material_infos: Query<(&MaterialInfo, &Children), Added<MaterialInfo>>,
|
||||
with_materials_and_meshes: Query<
|
||||
(),
|
||||
(
|
||||
|
@ -135,13 +38,12 @@ pub(crate) fn materials_inject2(
|
|||
With<Handle<Mesh>>,
|
||||
),
|
||||
>,
|
||||
assets_gltf: Res<Assets<Gltf>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
models: Res<Assets<bevy::gltf::Gltf>>,
|
||||
|
||||
asset_server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (material_info, children) in material_infos.iter() {
|
||||
println!("here");
|
||||
let model_file_name = format!(
|
||||
"{}_materials_library.{}",
|
||||
&material_info.source, &blueprints_config.format
|
||||
|
@ -157,18 +59,16 @@ pub(crate) fn materials_inject2(
|
|||
.material_library_cache
|
||||
.contains_key(&material_full_path)
|
||||
{
|
||||
info!("material is cached, retrieving");
|
||||
debug!("material is cached, retrieving");
|
||||
let material = blueprints_config
|
||||
.material_library_cache
|
||||
.get(&material_full_path)
|
||||
.expect("we should have the material available");
|
||||
material_found = Some(material);
|
||||
} else {
|
||||
let model_handle: Handle<Gltf> = asset_server.load(materials_path.clone());// FIXME: kinda weird now
|
||||
println!("loading material {:?} {}", materials_path, model_handle.id());
|
||||
|
||||
let mat_gltf = assets_gltf
|
||||
.get(model_handle.id())
|
||||
let my_gltf: Handle<Gltf> = asset_server.load(materials_path.clone());
|
||||
let mat_gltf = models
|
||||
.get(my_gltf.id())
|
||||
.expect("material should have been preloaded");
|
||||
if mat_gltf.named_materials.contains_key(material_name) {
|
||||
let material = mat_gltf
|
||||
|
|
|
@ -51,6 +51,7 @@ pub(crate) struct OriginalChildren(pub Vec<Entity>);
|
|||
#[reflect(Component)]
|
||||
pub struct BlueprintsList(pub HashMap<String,Vec<String>>);
|
||||
|
||||
|
||||
#[derive(Reflect, Default, Debug)]
|
||||
pub(crate) struct BlueprintLoadTracker{
|
||||
pub name: String,
|
||||
|
@ -65,21 +66,6 @@ pub(crate) struct BlueprintAssetsToLoad{
|
|||
pub progress: f32
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct AssetLoadTracker<T:bevy::prelude::Asset>{
|
||||
pub name: String,
|
||||
pub id: AssetId<T>,
|
||||
pub loaded: bool,
|
||||
pub handle: Handle<T>
|
||||
}
|
||||
#[derive(Component, Default, Debug)]
|
||||
pub(crate) struct AssetsToLoad<T:bevy::prelude::Asset>{
|
||||
pub all_loaded: bool,
|
||||
pub asset_infos: Vec<AssetLoadTracker<T>>,
|
||||
pub progress: f32
|
||||
}
|
||||
|
||||
|
||||
/// flag component
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintAssetsLoaded;
|
||||
|
@ -97,7 +83,10 @@ pub(crate) fn spawn_from_blueprints(
|
|||
Option<&Parent>,
|
||||
Option<&Library>,
|
||||
Option<&Name>,
|
||||
|
||||
Option<&BlueprintsList>,
|
||||
// Option<&BlueprintAssetsPreloaded>
|
||||
|
||||
),
|
||||
(Added<BlueprintName>, Added<SpawnHere>, Without<Spawned>),
|
||||
>,
|
||||
|
@ -125,8 +114,8 @@ pub(crate) fn spawn_from_blueprints(
|
|||
if blueprints_list.is_some() {
|
||||
let blueprints_list = blueprints_list.unwrap();
|
||||
// println!("blueprints list {:?}", blueprints_list.0.keys());
|
||||
let mut asset_infos:Vec<AssetLoadTracker<Gltf>> = vec![];
|
||||
|
||||
let mut asset_infos:Vec<BlueprintLoadTracker> = vec![];
|
||||
for (blueprint_name, _) in blueprints_list.0.iter() {
|
||||
/*if blueprint_name == what {
|
||||
println!("WHOLY MOLLY !")
|
||||
|
@ -145,23 +134,22 @@ pub(crate) fn spawn_from_blueprints(
|
|||
let model_id = model_handle.id();
|
||||
let loaded = asset_server.is_loaded_with_dependencies(model_id);
|
||||
if !loaded {
|
||||
asset_infos.push(AssetLoadTracker {
|
||||
asset_infos.push(BlueprintLoadTracker{
|
||||
name: model_path.to_string_lossy().into(),
|
||||
id: model_id,
|
||||
loaded: false,
|
||||
handle: model_handle.clone()
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
// if not all assets are already loaded, inject a component to signal that we need them to be loaded
|
||||
if asset_infos.len() > 0 {
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(AssetsToLoad{
|
||||
.insert(BlueprintAssetsToLoad{
|
||||
all_loaded: false,
|
||||
asset_infos: asset_infos,
|
||||
progress: 0.0
|
||||
//..Default::default()
|
||||
..Default::default()
|
||||
})
|
||||
.insert(BlueprintAssetsNotLoaded);
|
||||
}else {
|
||||
|
@ -175,7 +163,7 @@ pub(crate) fn spawn_from_blueprints(
|
|||
|
||||
|
||||
pub(crate) fn check_for_loaded(
|
||||
mut blueprint_assets_to_load: Query<(Entity, &mut AssetsToLoad<Gltf>), With<BlueprintAssetsNotLoaded>>,
|
||||
mut blueprint_assets_to_load: Query<(Entity, &mut BlueprintAssetsToLoad),With<BlueprintAssetsNotLoaded>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
){
|
||||
|
@ -230,6 +218,7 @@ pub(crate) fn actually_spawn_stuff(
|
|||
children: Query<&Children>,
|
||||
){
|
||||
|
||||
|
||||
for (
|
||||
entity,
|
||||
blupeprint_name,
|
||||
|
@ -255,7 +244,7 @@ pub(crate) fn actually_spawn_stuff(
|
|||
let model_path = Path::new(&library_path).join(Path::new(model_file_name.as_str()));
|
||||
|
||||
info!("attempting to spawn {:?}", model_path);
|
||||
let model_handle: Handle<Gltf> = asset_server.load(model_path);// FIXME: kinda weird now
|
||||
let model_handle: Handle<Gltf> = asset_server.load(model_path);
|
||||
|
||||
let gltf = assets_gltf
|
||||
.get(&model_handle)
|
||||
|
|
Loading…
Reference in New Issue