mirror of
https://github.com/kaosat-dev/Blender_bevy_components_workflow.git
synced 2024-11-27 05:46:59 +00:00
Compare commits
No commits in common. "f5b063cd344d9ab8dc6141abab7c8958b5509512" and "10e85fce26cc956835239873734a6f22ae18731f" have entirely different histories.
f5b063cd34
...
10e85fce26
@ -144,29 +144,14 @@ impl Plugin for BlueprintsPlugin {
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
(
|
||||
prepare_blueprints,
|
||||
(spawn_from_blueprints,
|
||||
check_for_loaded,
|
||||
spawn_from_blueprints,
|
||||
apply_deferred
|
||||
)
|
||||
.chain(),
|
||||
(
|
||||
compute_scene_aabbs,
|
||||
apply_deferred
|
||||
)
|
||||
.chain()
|
||||
.run_if(aabbs_enabled),
|
||||
actually_spawn_stuff, apply_deferred).chain(),
|
||||
|
||||
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, check_for_material_loaded, materials_inject2).chain().run_if(materials_library_enabled),
|
||||
)
|
||||
.chain()
|
||||
.in_set(GltfBlueprintsSet::Spawn),
|
||||
|
@ -30,6 +30,8 @@ pub(crate) struct BlueprintMaterialAssetsLoaded;
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintMaterialAssetsNotLoaded;
|
||||
|
||||
|
||||
|
||||
/// system that injects / replaces materials from material library
|
||||
pub(crate) fn materials_inject(
|
||||
blueprints_config: ResMut<BluePrintsConfig>,
|
||||
@ -63,6 +65,7 @@ pub(crate) fn materials_inject(
|
||||
} 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 {
|
||||
@ -100,12 +103,14 @@ pub(crate) fn check_for_material_loaded(
|
||||
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 {
|
||||
@ -136,6 +141,7 @@ pub(crate) fn materials_inject2(
|
||||
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
|
||||
@ -151,7 +157,7 @@ pub(crate) fn materials_inject2(
|
||||
.material_library_cache
|
||||
.contains_key(&material_full_path)
|
||||
{
|
||||
debug!("material is cached, retrieving");
|
||||
info!("material is cached, retrieving");
|
||||
let material = blueprints_config
|
||||
.material_library_cache
|
||||
.get(&material_full_path)
|
||||
@ -159,6 +165,8 @@ pub(crate) fn materials_inject2(
|
||||
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())
|
||||
.expect("material should have been preloaded");
|
||||
|
@ -46,10 +46,25 @@ pub struct AddToGameWorld;
|
||||
/// helper component, just to transfer child data
|
||||
pub(crate) struct OriginalChildren(pub Vec<Entity>);
|
||||
|
||||
|
||||
#[derive(Component, Reflect, Default, Debug)]
|
||||
#[reflect(Component)]
|
||||
pub struct BlueprintsList(pub HashMap<String,Vec<String>>);
|
||||
|
||||
#[derive(Reflect, Default, Debug)]
|
||||
pub(crate) struct BlueprintLoadTracker{
|
||||
pub name: String,
|
||||
pub id: AssetId<Gltf>,
|
||||
pub loaded: bool,
|
||||
pub handle: Handle<Gltf>
|
||||
}
|
||||
#[derive(Component, Default, Debug)]
|
||||
pub(crate) struct BlueprintAssetsToLoad{
|
||||
pub all_loaded: bool,
|
||||
pub asset_infos: Vec<BlueprintLoadTracker>,
|
||||
pub progress: f32
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct AssetLoadTracker<T:bevy::prelude::Asset>{
|
||||
pub name: String,
|
||||
@ -74,7 +89,7 @@ pub(crate) struct BlueprintAssetsNotLoaded;
|
||||
|
||||
/// spawning prepare function,
|
||||
/// * also takes into account the already exisiting "override" components, ie "override components" > components from blueprint
|
||||
pub(crate) fn prepare_blueprints(
|
||||
pub(crate) fn spawn_from_blueprints(
|
||||
spawn_placeholders: Query<
|
||||
(
|
||||
Entity,
|
||||
@ -102,7 +117,7 @@ pub(crate) fn prepare_blueprints(
|
||||
) in spawn_placeholders.iter()
|
||||
{
|
||||
debug!(
|
||||
"requesting to spawn {:?} for entity {:?}, id: {:?}, parent:{:?}",
|
||||
"preparing to spawn {:?} for entity {:?}, id: {:?}, parent:{:?}",
|
||||
blupeprint_name.0, name, entity, original_parent
|
||||
);
|
||||
|
||||
@ -155,14 +170,10 @@ pub(crate) fn prepare_blueprints(
|
||||
.insert(BlueprintAssetsLoaded);
|
||||
}
|
||||
}
|
||||
else { // in case there are no blueprintsList
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(BlueprintAssetsLoaded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn check_for_loaded(
|
||||
mut blueprint_assets_to_load: Query<(Entity, &mut AssetsToLoad<Gltf>), With<BlueprintAssetsNotLoaded>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
@ -183,7 +194,7 @@ pub(crate) fn check_for_loaded(
|
||||
}
|
||||
}
|
||||
let progress:f32 = loaded_amount as f32 / total as f32;
|
||||
// println!("progress: {}",progress);
|
||||
println!("progress: {}",progress);
|
||||
assets_to_load.progress = progress;
|
||||
|
||||
if all_loaded {
|
||||
@ -195,7 +206,7 @@ pub(crate) fn check_for_loaded(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_from_blueprints(
|
||||
pub(crate) fn actually_spawn_stuff(
|
||||
spawn_placeholders: Query<
|
||||
(
|
||||
Entity,
|
||||
@ -231,7 +242,7 @@ pub(crate) fn spawn_from_blueprints(
|
||||
) in spawn_placeholders.iter()
|
||||
{
|
||||
info!(
|
||||
"attempting to spawn {:?} for entity {:?}, id: {:?}, parent:{:?}",
|
||||
"need to spawn {:?} for entity {:?}, id: {:?}, parent:{:?}",
|
||||
blupeprint_name.0, name, entity, original_parent
|
||||
);
|
||||
|
||||
@ -243,7 +254,7 @@ pub(crate) fn spawn_from_blueprints(
|
||||
library_override.map_or_else(|| &blueprints_config.library_folder, |l| &l.0);
|
||||
let model_path = Path::new(&library_path).join(Path::new(model_file_name.as_str()));
|
||||
|
||||
// info!("attempting to spawn {:?}", model_path);
|
||||
info!("attempting to spawn {:?}", model_path);
|
||||
let model_handle: Handle<Gltf> = asset_server.load(model_path);// FIXME: kinda weird now
|
||||
|
||||
let gltf = assets_gltf
|
||||
|
@ -1,13 +1,12 @@
|
||||
use std::any::TypeId;
|
||||
|
||||
use bevy::gltf::Gltf;
|
||||
use bevy::prelude::*;
|
||||
use bevy::scene::SceneInstance;
|
||||
// use bevy::utils::hashbrown::HashSet;
|
||||
|
||||
use super::{AnimationPlayerLink, Animations};
|
||||
use super::{SpawnHere, Spawned};
|
||||
use crate::{AssetsToLoad, BlueprintAssetsLoaded, CopyComponents, InBlueprint, NoInBlueprint, OriginalChildren};
|
||||
use crate::{BlueprintAssetsToLoad, CopyComponents, InBlueprint, NoInBlueprint, OriginalChildren};
|
||||
|
||||
/// this system is in charge of doing any necessary post processing after a blueprint scene has been spawned
|
||||
/// - it removes one level of useless nesting
|
||||
@ -90,8 +89,7 @@ pub(crate) fn spawned_blueprint_post_process(
|
||||
commands.entity(original).remove::<SpawnHere>();
|
||||
commands.entity(original).remove::<Spawned>();
|
||||
commands.entity(original).remove::<Handle<Scene>>();
|
||||
commands.entity(original).remove::<AssetsToLoad<Gltf>>();// also clear the sub assets tracker to free up handles, perhaps just freeing up the handles and leave the rest would be better ?
|
||||
commands.entity(original).remove::<BlueprintAssetsLoaded>();
|
||||
commands.entity(original).remove::<BlueprintAssetsToLoad>();// also clear the sub assets tracker to free up handles, perhaps just freeing up the handles and leave the rest would be better ?
|
||||
commands.entity(root_entity).despawn_recursive();
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -20,7 +20,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -20,7 +20,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -21,7 +21,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -21,7 +21,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -76,7 +76,7 @@ pub fn trigger_level_transition(
|
||||
} else if target_level == "Level2" {
|
||||
level = game_assets.level2.clone().unwrap();
|
||||
} else {
|
||||
level = game_assets.world.clone().unwrap();
|
||||
level = game_assets.world.clone();
|
||||
}
|
||||
info!("spawning new level");
|
||||
commands.spawn((
|
||||
|
@ -21,7 +21,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -5,8 +5,8 @@ use bevy_asset_loader::prelude::*;
|
||||
|
||||
#[derive(AssetCollection, Resource)]
|
||||
pub struct GameAssets {
|
||||
#[asset(key = "world", optional)]
|
||||
pub world: Option<Handle<Gltf>>,
|
||||
#[asset(key = "world")]
|
||||
pub world: Handle<Gltf>,
|
||||
|
||||
#[asset(key = "world_dynamic", optional)]
|
||||
pub world_dynamic: Option<Handle<Gltf>>,
|
||||
@ -16,8 +16,8 @@ pub struct GameAssets {
|
||||
#[asset(key = "level2", optional)]
|
||||
pub level2: Option<Handle<Gltf>>,
|
||||
|
||||
#[asset(key = "models", collection(typed, mapped), optional)]
|
||||
pub models: Option<HashMap<String, Handle<Gltf>>>,
|
||||
#[asset(key = "models", collection(typed, mapped))]
|
||||
pub models: HashMap<String, Handle<Gltf>>,
|
||||
|
||||
#[asset(key = "materials", collection(typed, mapped), optional)]
|
||||
pub materials: Option<HashMap<String, Handle<Gltf>>>,
|
||||
|
@ -1,6 +1,6 @@
|
||||
({
|
||||
/*"world":File (path: "models/World.glb"),
|
||||
"world":File (path: "models/World.glb"),
|
||||
"models": Folder (
|
||||
path: "models/library",
|
||||
),*/
|
||||
),
|
||||
})
|
@ -11,7 +11,6 @@ impl Plugin for CorePlugin {
|
||||
legacy_mode: false,
|
||||
library_folder: "models/library".into(),
|
||||
format: GltfFormat::GLB,
|
||||
material_library:true,
|
||||
aabbs: true,
|
||||
..Default::default()
|
||||
},
|
||||
|
@ -1,5 +1,5 @@
|
||||
use bevy::{prelude::*, utils::hashbrown::HashMap};
|
||||
use bevy_gltf_blueprints::{BluePrintBundle, BlueprintName, BlueprintsList, GameWorldTag, Library, SpawnHere};
|
||||
use bevy::prelude::*;
|
||||
use bevy_gltf_blueprints::{BluePrintBundle, BlueprintName, GameWorldTag};
|
||||
use bevy_gltf_worlflow_examples_common_rapier::{assets::GameAssets, GameState, InAppRunning};
|
||||
|
||||
use bevy_rapier3d::prelude::Velocity;
|
||||
@ -7,19 +7,31 @@ use rand::Rng;
|
||||
|
||||
pub fn setup_game(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
game_assets: Res<GameAssets>,
|
||||
models: Res<Assets<bevy::gltf::Gltf>>,
|
||||
mut next_game_state: ResMut<NextState<GameState>>,
|
||||
) {
|
||||
commands.insert_resource(AmbientLight {
|
||||
color: Color::WHITE,
|
||||
brightness: 0.2,
|
||||
});
|
||||
// here we actually spawn our game world/level
|
||||
|
||||
commands.spawn((
|
||||
SceneBundle {
|
||||
scene: asset_server.load("models/World.glb#Scene0"),
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
..default()
|
||||
},
|
||||
bevy::prelude::Name::from("world"),
|
||||
GameWorldTag,
|
||||
InAppRunning,
|
||||
));
|
||||
|
||||
next_game_state.set(GameState::InGame)
|
||||
}
|
||||
|
||||
|
@ -97,15 +97,13 @@ impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, (spawn_test).run_if(in_state(GameState::InGame)))
|
||||
.add_systems(Update, validate_export)
|
||||
.add_systems(Update, generate_screenshot.run_if(on_timer(Duration::from_secs_f32(0.2)))) // TODO: run once
|
||||
.add_systems(OnEnter(AppState::MenuRunning), start_game)
|
||||
.add_systems(OnEnter(AppState::AppRunning), setup_game)
|
||||
/* .add_systems(Update, generate_screenshot.run_if(on_timer(Duration::from_secs_f32(0.2)))) // TODO: run once
|
||||
|
||||
|
||||
.add_systems(
|
||||
Update,
|
||||
exit_game.run_if(on_timer(Duration::from_secs_f32(0.5))),
|
||||
) // shut down the app after this time*/
|
||||
) // shut down the app after this time
|
||||
;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user