Scene in memory persistance and debug tools (#24)

![image](https://github.com/storytold/storyteller-bevy/assets/10954133/10eb5c33-2154-4e9d-95ce-1d8e8b822d22)
Debug tools example

---------

Co-authored-by: Danny McGee <dannymcgee@gmail.com>
This commit is contained in:
rewin
2024-02-02 22:51:18 +03:00
committed by GitHub
parent 2df86eaf58
commit 8f0b916eaa
15 changed files with 415 additions and 388 deletions
Generated
+32 -129
View File
@@ -177,9 +177,9 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.75" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca"
[[package]] [[package]]
name = "approx" name = "approx"
@@ -346,47 +346,19 @@ dependencies = [
"bevy_internal", "bevy_internal",
] ]
[[package]]
name = "bevy-inspector-egui"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d65b98d6fca1209c36c4d403c377f303aad22d940281fe1a9e431217516f0622"
dependencies = [
"bevy-inspector-egui-derive 0.21.0",
"bevy_app",
"bevy_asset",
"bevy_core",
"bevy_core_pipeline",
"bevy_ecs",
"bevy_egui 0.23.0",
"bevy_hierarchy",
"bevy_log",
"bevy_math",
"bevy_pbr",
"bevy_reflect",
"bevy_render",
"bevy_utils",
"bevy_window",
"egui 0.23.0",
"image",
"once_cell",
"pretty-type-name",
"smallvec",
]
[[package]] [[package]]
name = "bevy-inspector-egui" name = "bevy-inspector-egui"
version = "0.22.1" version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c3efee9165f9be9c6a180c6a9bbb4bd39ee0a98a594f4cb7127704e15415cdd" checksum = "1c3efee9165f9be9c6a180c6a9bbb4bd39ee0a98a594f4cb7127704e15415cdd"
dependencies = [ dependencies = [
"bevy-inspector-egui-derive 0.22.0", "bevy-inspector-egui-derive",
"bevy_app", "bevy_app",
"bevy_asset", "bevy_asset",
"bevy_core", "bevy_core",
"bevy_core_pipeline", "bevy_core_pipeline",
"bevy_ecs", "bevy_ecs",
"bevy_egui 0.24.0", "bevy_egui",
"bevy_hierarchy", "bevy_hierarchy",
"bevy_log", "bevy_log",
"bevy_math", "bevy_math",
@@ -396,24 +368,13 @@ dependencies = [
"bevy_time", "bevy_time",
"bevy_utils", "bevy_utils",
"bevy_window", "bevy_window",
"egui 0.24.1", "egui",
"image", "image",
"once_cell", "once_cell",
"pretty-type-name", "pretty-type-name",
"smallvec", "smallvec",
] ]
[[package]]
name = "bevy-inspector-egui-derive"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec800b7cf98151b5dbff80f0eb6dffcb4bcfceef6e457888b395ead4eb7e75ba"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.37",
]
[[package]] [[package]]
name = "bevy-inspector-egui-derive" name = "bevy-inspector-egui-derive"
version = "0.22.0" version = "0.22.0"
@@ -658,19 +619,6 @@ dependencies = [
"syn 2.0.37", "syn 2.0.37",
] ]
[[package]]
name = "bevy_egui"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85450af551b7e1cb766f710763b60a12a82ffd6323945a8f776c6334c59ccdc1"
dependencies = [
"arboard",
"bevy",
"egui 0.23.0",
"thread_local",
"webbrowser",
]
[[package]] [[package]]
name = "bevy_egui" name = "bevy_egui"
version = "0.24.0" version = "0.24.0"
@@ -679,7 +627,7 @@ checksum = "c90c01202dbcebc03315a01ea71553b35e1f20b0da6b1cc8c2605344032a3d96"
dependencies = [ dependencies = [
"arboard", "arboard",
"bevy", "bevy",
"egui 0.24.1", "egui",
"thread_local", "thread_local",
"webbrowser", "webbrowser",
] ]
@@ -2087,15 +2035,6 @@ dependencies = [
"proc-macro-error", "proc-macro-error",
] ]
[[package]]
name = "ecolor"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfdf4e52dbbb615cfd30cf5a5265335c217b5fd8d669593cea74a517d9c605af"
dependencies = [
"bytemuck",
]
[[package]] [[package]]
name = "ecolor" name = "ecolor"
version = "0.24.1" version = "0.24.1"
@@ -2105,17 +2044,6 @@ dependencies = [
"bytemuck", "bytemuck",
] ]
[[package]]
name = "egui"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bd69fed5fcf4fbb8225b24e80ea6193b61e17a625db105ef0c4d71dde6eb8b7"
dependencies = [
"ahash",
"epaint 0.23.0",
"nohash-hasher",
]
[[package]] [[package]]
name = "egui" name = "egui"
version = "0.24.1" version = "0.24.1"
@@ -2123,7 +2051,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c55bcb864b764eb889515a38b8924757657a250738ad15126637ee2df291ee6b" checksum = "c55bcb864b764eb889515a38b8924757657a250738ad15126637ee2df291ee6b"
dependencies = [ dependencies = [
"ahash", "ahash",
"epaint 0.24.1", "epaint",
"nohash-hasher", "nohash-hasher",
] ]
@@ -2133,7 +2061,7 @@ version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2880830f1b9a0ea618472c989e7ee758f9abc54352846e4feab1be278e69316b" checksum = "2880830f1b9a0ea618472c989e7ee758f9abc54352846e4feab1be278e69316b"
dependencies = [ dependencies = [
"egui 0.24.1", "egui",
"glam", "glam",
"mint", "mint",
] ]
@@ -2145,7 +2073,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "246a6cbf38560cdcf75f955b1f2f39be6339a3f2958d06997ce70b7b4aa3f57d" checksum = "246a6cbf38560cdcf75f955b1f2f39be6339a3f2958d06997ce70b7b4aa3f57d"
dependencies = [ dependencies = [
"duplicate", "duplicate",
"egui 0.24.1", "egui",
"paste", "paste",
] ]
@@ -2154,7 +2082,7 @@ name = "egui_file"
version = "0.13.0" version = "0.13.0"
source = "git+https://github.com/Barugon/egui_file.git?rev=de5f8f41df160d67be328ab50974966ceb74e70f#de5f8f41df160d67be328ab50974966ceb74e70f" source = "git+https://github.com/Barugon/egui_file.git?rev=de5f8f41df160d67be328ab50974966ceb74e70f#de5f8f41df160d67be328ab50974966ceb74e70f"
dependencies = [ dependencies = [
"egui 0.24.1", "egui",
] ]
[[package]] [[package]]
@@ -2163,15 +2091,6 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "emath"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ef2b29de53074e575c18b694167ccbe6e5191f7b25fe65175a0d905a32eeec0"
dependencies = [
"bytemuck",
]
[[package]] [[package]]
name = "emath" name = "emath"
version = "0.24.1" version = "0.24.1"
@@ -2222,21 +2141,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "epaint"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58067b840d009143934d91d8dcb8ded054d8301d7c11a517ace0a99bb1e1595e"
dependencies = [
"ab_glyph",
"ahash",
"bytemuck",
"ecolor 0.23.0",
"emath 0.23.0",
"nohash-hasher",
"parking_lot",
]
[[package]] [[package]]
name = "epaint" name = "epaint"
version = "0.24.1" version = "0.24.1"
@@ -2246,8 +2150,8 @@ dependencies = [
"ab_glyph", "ab_glyph",
"ahash", "ahash",
"bytemuck", "bytemuck",
"ecolor 0.24.1", "ecolor",
"emath 0.24.1", "emath",
"nohash-hasher", "nohash-hasher",
"parking_lot", "parking_lot",
] ]
@@ -3032,9 +2936,9 @@ dependencies = [
[[package]] [[package]]
name = "libz-sys" name = "libz-sys"
version = "1.1.14" version = "1.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "295c17e837573c8c821dbaeb3cceb3d745ad082f7572191409e69cbc1b3fd050" checksum = "037731f5d3aaa87a5675e895b63ddff1a87624bc29f77004ea829809654e48f6"
dependencies = [ dependencies = [
"cc", "cc",
"libc", "libc",
@@ -3562,9 +3466,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]] [[package]]
name = "openssl-sys" name = "openssl-sys"
version = "0.9.98" version = "0.9.99"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1665caf8ab2dc9aef43d1c0023bd904633a6a05cb30b0ad59bec2ae986e57a7" checksum = "22e1bf214306098e4832460f797824c05d25aacdf896f64a985fb0fd992454ae"
dependencies = [ dependencies = [
"cc", "cc",
"libc", "libc",
@@ -3676,18 +3580,18 @@ dependencies = [
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.1.3" version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422" checksum = "0302c4a0442c456bd56f841aee5c3bfd17967563f6fadc9ceb9f9c23cf3807e0"
dependencies = [ dependencies = [
"pin-project-internal", "pin-project-internal",
] ]
[[package]] [[package]]
name = "pin-project-internal" name = "pin-project-internal"
version = "1.1.3" version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -4176,7 +4080,7 @@ dependencies = [
[[package]] [[package]]
name = "space_bevy_xpbd_plugin" name = "space_bevy_xpbd_plugin"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy_xpbd_3d", "bevy_xpbd_3d",
@@ -4186,7 +4090,7 @@ dependencies = [
[[package]] [[package]]
name = "space_editor" name = "space_editor"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"space_bevy_xpbd_plugin", "space_bevy_xpbd_plugin",
@@ -4197,7 +4101,7 @@ dependencies = [
[[package]] [[package]]
name = "space_editor_core" name = "space_editor_core"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"space_persistence", "space_persistence",
@@ -4209,11 +4113,11 @@ dependencies = [
[[package]] [[package]]
name = "space_editor_ui" name = "space_editor_ui"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy_debug_grid", "bevy_debug_grid",
"bevy_egui 0.24.0", "bevy_egui",
"bevy_mod_picking", "bevy_mod_picking",
"bevy_panorbit_camera", "bevy_panorbit_camera",
"convert_case 0.6.0", "convert_case 0.6.0",
@@ -4230,7 +4134,7 @@ dependencies = [
[[package]] [[package]]
name = "space_persistence" name = "space_persistence"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"ron", "ron",
@@ -4240,10 +4144,10 @@ dependencies = [
[[package]] [[package]]
name = "space_prefab" name = "space_prefab"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy-inspector-egui 0.22.1", "bevy-inspector-egui",
"bevy-scene-hook", "bevy-scene-hook",
"ron", "ron",
"serde", "serde",
@@ -4254,21 +4158,20 @@ dependencies = [
[[package]] [[package]]
name = "space_shared" name = "space_shared"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy-inspector-egui 0.22.1", "bevy-inspector-egui",
"egui_file", "egui_file",
] ]
[[package]] [[package]]
name = "space_undo" name = "space_undo"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/rewin123/space_editor?rev=e150ecaf0647de6532d2967928982abf2558bfbc#e150ecaf0647de6532d2967928982abf2558bfbc" source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6"
dependencies = [ dependencies = [
"bevy", "bevy",
"pretty-type-name", "pretty-type-name",
"space_shared",
] ]
[[package]] [[package]]
@@ -4310,7 +4213,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bevy", "bevy",
"bevy-inspector-egui 0.21.0", "bevy-inspector-egui",
"bevy_mod_outline", "bevy_mod_outline",
"bevy_mod_picking", "bevy_mod_picking",
"bevy_mod_raycast", "bevy_mod_raycast",
+6
View File
@@ -0,0 +1,6 @@
{
"space_undo::ChangeChainSettings": "{\"space_undo::ChangeChainSettings\":(max_change_chain_size:200)}",
"space_persistence::PersistenceSettings": "{\"space_persistence::PersistenceSettings\":(load_on_startup:true,save_on_close:true)}",
"space_editor_core::hotkeys::HotkeySet<space_editor_ui::tools::gizmo::GizmoHotkey>": "{\"space_editor_core::hotkeys::HotkeySet<space_editor_ui::tools::gizmo::GizmoHotkey>\":(bindings:{Rotate:[R],Translate:[G],Scale:[S],Multiple:[ShiftLeft],Clone:[AltLeft],Delete:[X]},name:\"GizmoHotkey\")}",
"space_editor_ui::settings::NewWindowSettings": "{\"space_editor_ui::settings::NewWindowSettings\":(new_tab:SameNode)}",
}
+4 -4
View File
@@ -13,11 +13,11 @@ name = "studio-standalone"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
anyhow = { workspace = true } bevy-inspector-egui = "0.22.0"
bevy-inspector-egui = "0.21.0"
bevy_mod_raycast = "0.16.0" bevy_mod_raycast = "0.16.0"
bevy_rapier3d = "0.23.0" bevy_rapier3d = "0.23.0"
bevy_mod_outline = "0.6.0" bevy_mod_outline = "0.6.0"
bevy_web_asset = "0.7.0"
bitflags = "2.4.0" bitflags = "2.4.0"
console_error_panic_hook = { version = "0.1.7", optional = true } console_error_panic_hook = { version = "0.1.7", optional = true }
js-sys = { version = "0.3.65", optional = true } js-sys = { version = "0.3.65", optional = true }
@@ -34,8 +34,8 @@ web-sys = { version = "0.3.65", optional = true, features = [
"HtmlElement", "HtmlElement",
"Window", "Window",
] } ] }
space_editor = {git = "https://github.com/rewin123/space_editor", rev = "e150ecaf0647de6532d2967928982abf2558bfbc"} space_editor = {git = "https://github.com/rewin123/space_editor", rev = "ce83f86dc902787ab16213f3209276a784654df6"}
bevy_web_asset = "0.7.0" anyhow.workspace = true
[features] [features]
wasm = [ wasm = [
Binary file not shown.
+2
View File
@@ -179,6 +179,8 @@ fn process_focus_inputs(
// Zero out any existing camera velocity or interpolation // Zero out any existing camera velocity or interpolation
**velocity = Vec3::ZERO; **velocity = Vec3::ZERO;
info!("Setting focus to {:?}", target);
if q_interp_vel.get(ent).is_ok() { if q_interp_vel.get(ent).is_ok() {
cmd.entity(ent).remove::<InterpVelocity>(); cmd.entity(ent).remove::<InterpVelocity>();
} }
+55 -2
View File
@@ -1,8 +1,16 @@
use std::fmt; use std::fmt;
use bevy::{prelude::*, window::PrimaryWindow}; use bevy::{prelude::*, window::PrimaryWindow};
use bevy_inspector_egui::quick::WorldInspectorPlugin; use bevy_inspector_egui::{bevy_egui::EguiPlugin, DefaultInspectorConfigPlugin};
use bevy_mod_picking::debug::DebugPickingMode; use bevy_mod_picking::debug::DebugPickingMode;
use space_editor::{
prelude::EditorCameraMarker,
space_editor_ui::{
bottom_menu::BottomMenuPlugin, tools::gizmo::GizmoToolPlugin, EditorDefaultCameraPlugin,
EditorGizmoConfigPlugin, EditorPickingPlugin, EditorPluginGroup, EditorUiCore,
ShowEditorUi,
},
};
use crate::{ use crate::{
camera_controller::{CameraController, CameraMotionSystem}, camera_controller::{CameraController, CameraMotionSystem},
@@ -25,7 +33,6 @@ pub struct DebugPlugin;
impl Plugin for DebugPlugin { impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(DebugMode(false)) app.insert_resource(DebugMode(false))
.add_plugins(WorldInspectorPlugin::new().run_if(is_debug_mode))
.insert_resource(State::new(DebugPickingMode::Disabled)) .insert_resource(State::new(DebugPickingMode::Disabled))
.insert_resource(GizmoConfig { .insert_resource(GizmoConfig {
enabled: true, enabled: true,
@@ -47,6 +54,33 @@ impl Plugin for DebugPlugin {
.run_if(is_debug_mode), .run_if(is_debug_mode),
), ),
); );
//space_editor stuff
// app.add_plugins(UndoPlugin);
app.add_plugins(
EditorPluginGroup
.build()
.disable::<EditorUiCore>()
.add_before::<EditorDefaultCameraPlugin, _>(EditorUiCore {
disable_no_editor_cams: false,
})
.disable::<EguiPlugin>()
.disable::<DefaultInspectorConfigPlugin>()
.disable::<EditorPickingPlugin>()
.disable::<EditorDefaultCameraPlugin>()
.disable::<BottomMenuPlugin>()
.disable::<GizmoToolPlugin>()
.disable::<EditorGizmoConfigPlugin>()
.set(space_editor::prelude::SelectedPlugin {
show_selected_wireframe: false,
}),
);
app.add_systems(Update, sync_editor_camera);
app.add_systems(
Update,
update_editor_ui_mode.run_if(resource_changed::<DebugMode>()),
);
} }
} }
@@ -59,6 +93,16 @@ fn toggle_debug_mode(mut input: EventReader<InputAction>, mut r_debug: ResMut<De
} }
} }
fn update_editor_ui_mode(
r_debug: Res<DebugMode>,
mut s_show_editor_ui: ResMut<NextState<ShowEditorUi>>,
) {
s_show_editor_ui.set(match **r_debug {
true => ShowEditorUi::Show,
false => ShowEditorUi::Hide,
});
}
fn update_debug_picking_mode( fn update_debug_picking_mode(
r_debug: Res<DebugMode>, r_debug: Res<DebugMode>,
mut s_debug_picking: ResMut<NextState<DebugPickingMode>>, mut s_debug_picking: ResMut<NextState<DebugPickingMode>>,
@@ -157,3 +201,12 @@ pub fn print_mat3(m: &Mat3, label: impl fmt::Display) -> fmt::Result {
Ok(()) Ok(())
} }
fn sync_editor_camera(
mut cmd: Commands,
mut q_cam: Query<Entity, (With<MainCamera>, Without<EditorCameraMarker>)>,
) {
for ent in q_cam.iter_mut() {
cmd.entity(ent).insert(EditorCameraMarker);
}
}
+12 -1
View File
@@ -320,8 +320,19 @@ fn read_mouse_delta(
// TODO: Make it configurable // TODO: Make it configurable
fn send_undo_event(mut events: EventWriter<UndoRedo>, keyboard: Res<Input<KeyCode>>) { fn send_undo_event(mut events: EventWriter<UndoRedo>, keyboard: Res<Input<KeyCode>>) {
if keyboard.pressed(KeyCode::ControlLeft) && keyboard.just_pressed(KeyCode::Z) { if keyboard.pressed(KeyCode::ControlLeft)
&& keyboard.just_pressed(KeyCode::Z)
&& !keyboard.pressed(KeyCode::ShiftLeft)
{
events.send(UndoRedo::Undo); events.send(UndoRedo::Undo);
info!("Undo event sent"); info!("Undo event sent");
} }
if keyboard.pressed(KeyCode::ControlLeft)
&& keyboard.just_pressed(KeyCode::Z)
&& keyboard.pressed(KeyCode::ShiftLeft)
{
events.send(UndoRedo::Redo);
info!("Redo event sent");
}
} }
+18 -1
View File
@@ -3,13 +3,14 @@ use bevy::{
prelude::*, prelude::*,
render::view::RenderLayers, render::view::RenderLayers,
}; };
use space_editor::space_editor_ui::NotShowCamera;
use crate::MainCamera; use crate::MainCamera;
use super::GIZMO_RENDER_LAYER; use super::GIZMO_RENDER_LAYER;
#[derive(Component)] #[derive(Component)]
pub(super) struct GizmoCamera; pub struct GizmoCamera;
pub(super) fn spawn_gizmo_camera( pub(super) fn spawn_gizmo_camera(
mut cmd: Commands, mut cmd: Commands,
@@ -35,6 +36,22 @@ pub(super) fn spawn_gizmo_camera(
}, },
RenderLayers::layer(GIZMO_RENDER_LAYER), RenderLayers::layer(GIZMO_RENDER_LAYER),
Fxaa::default(), Fxaa::default(),
NotShowCamera,
)); ));
}); });
} }
pub(super) fn sync_gizmo_and_main_cameras(
q_main_camera: Query<&Camera, (With<MainCamera>, Without<GizmoCamera>)>,
mut q_gizmo_camera: Query<&mut Camera, (With<GizmoCamera>, Without<MainCamera>)>,
) {
let Ok(main_cam) = q_main_camera.get_single() else {
return;
};
let Ok(mut gizmo_cam) = q_gizmo_camera.get_single_mut() else {
return;
};
gizmo_cam.viewport = main_cam.viewport.clone();
}
+8 -4
View File
@@ -13,7 +13,7 @@ use self::{
use super::{Selection, TransformMode, TransformType}; use super::{Selection, TransformMode, TransformType};
mod camera; pub mod camera;
mod combo; mod combo;
mod common; mod common;
mod resources; mod resources;
@@ -62,6 +62,10 @@ impl Plugin for GizmoPlugin {
normalize_gizmo_scale.after(CameraMotionSystem), normalize_gizmo_scale.after(CameraMotionSystem),
), ),
); );
//sync camera viewports
app.add_systems(Update, camera::sync_gizmo_and_main_cameras);
app.add_systems( app.add_systems(
PostUpdate, PostUpdate,
update_ss_ring_rotation.after(TransformSystem::TransformPropagate), update_ss_ring_rotation.after(TransformSystem::TransformPropagate),
@@ -139,10 +143,10 @@ fn spawn_gizmo(
fn sync_gizmo_transform( fn sync_gizmo_transform(
mut cmd: Commands, mut cmd: Commands,
mut q_gizmo: Query<(&mut Transform, &TransformGizmo)>, mut q_gizmo: Query<(Entity, &mut Transform, &TransformGizmo)>,
q_targets: Query<&GlobalTransform>, q_targets: Query<&GlobalTransform>,
) { ) {
for (mut transform, gizmo) in q_gizmo.iter_mut() { for (ent, mut transform, gizmo) in q_gizmo.iter_mut() {
if let Ok(target) = q_targets.get(gizmo.target) { if let Ok(target) = q_targets.get(gizmo.target) {
match gizmo.transform_space { match gizmo.transform_space {
super::TransformSpace::Local => { super::TransformSpace::Local => {
@@ -155,7 +159,7 @@ fn sync_gizmo_transform(
} }
} }
} else { } else {
cmd.entity(gizmo.target).despawn_recursive(); cmd.entity(ent).despawn_recursive();
} }
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
mod gizmos; pub mod gizmos;
pub mod selection; pub mod selection;
pub mod transformation; pub mod transformation;
+32
View File
@@ -25,6 +25,8 @@ impl Plugin for SelectionPlugin {
.after(SceneSpawnerSystem), .after(SceneSpawnerSystem),
); );
app.add_systems(Update, tag_scene_elements);
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
app.add_systems( app.add_systems(
Update, Update,
@@ -36,6 +38,8 @@ impl Plugin for SelectionPlugin {
.map(bevy::utils::error) .map(bevy::utils::error)
.run_if(resource_changed_or_removed::<Selection>()), .run_if(resource_changed_or_removed::<Selection>()),
); );
app.add_systems(Update, sync_editor_selected);
} }
} }
@@ -48,6 +52,34 @@ pub struct Selected;
#[derive(Resource, Deref, DerefMut, Debug)] #[derive(Resource, Deref, DerefMut, Debug)]
pub struct Selection(Entity); pub struct Selection(Entity);
fn sync_editor_selected(
mut cmd: Commands,
q_new_selected: Query<Entity, (Added<Selected>, Without<space_editor::prelude::Selected>)>,
q_editor_selected: Query<Entity, With<space_editor::prelude::Selected>>,
q_selected: Query<Entity, With<Selected>>,
q_added_editor_selected: Query<
Entity,
(Without<Selected>, Added<space_editor::prelude::Selected>),
>,
) {
for ent in q_new_selected.iter() {
for ed_ent in q_editor_selected.iter() {
cmd.entity(ed_ent)
.remove::<space_editor::prelude::Selected>();
}
cmd.entity(ent).insert(space_editor::prelude::Selected);
}
for ent in q_added_editor_selected.iter() {
cmd.entity(ent).insert(Selected);
cmd.insert_resource(Selection(ent));
for ent in q_selected.iter() {
cmd.entity(ent).remove::<Selected>();
}
}
}
fn tag_scene_elements( fn tag_scene_elements(
mut cmd: Commands, mut cmd: Commands,
q_scene_elements: Query<(Entity, &Children), (With<SceneElement>, Without<Selectable>)>, q_scene_elements: Query<(Entity, &Children), (With<SceneElement>, Without<Selectable>)>,
+43 -44
View File
@@ -2,9 +2,9 @@
use bevy::{app::PluginGroupBuilder, prelude::*}; use bevy::{app::PluginGroupBuilder, prelude::*};
use bevy_mod_picking::DefaultPickingPlugins; use bevy_mod_picking::DefaultPickingPlugins;
use bevy_web_asset::WebAssetPlugin;
use space_editor::prelude::space_undo::UndoPlugin;
use bevy_web_asset::WebAssetPlugin;
use bvh::BvhPlugin;
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@@ -16,28 +16,27 @@ mod input;
mod inspector; mod inspector;
mod interaction; mod interaction;
mod math; mod math;
mod persistance;
mod scene; mod scene;
mod skybox; mod skybox;
mod ui; mod ui;
mod viewport; pub mod viewport;
mod world_grid; mod world_grid;
use crate::{ pub use crate::{
bvh::BvhPlugin,
camera_controller::CameraControllerPlugin, camera_controller::CameraControllerPlugin,
debug::DebugPlugin, debug::DebugPlugin,
input::StudioInputPlugin, input::StudioInputPlugin,
inspector::InspectorPlugin, inspector::InspectorPlugin,
interaction::InteractionPlugin, interaction::InteractionPlugin,
math::InterpolationPlugin, math::InterpolationPlugin,
scene::{LoadSceneEvent, ScenePlugin, TestBvh}, scene::{LoadSceneEvent, ScenePlugin},
skybox::{SkyboxId, SkyboxPlugin}, skybox::{SkyboxId, SkyboxPlugin},
ui::UiLayerPlugin, ui::UiLayerPlugin,
viewport::ViewportSize,
world_grid::WorldGridPlugin, world_grid::WorldGridPlugin,
}; };
pub use crate::viewport::ViewportSize;
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
mod wasm; mod wasm;
@@ -61,7 +60,6 @@ struct CommonPlugins;
impl PluginGroup for CommonPlugins { impl PluginGroup for CommonPlugins {
fn build(self) -> PluginGroupBuilder { fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>() PluginGroupBuilder::start::<Self>()
.add(BvhPlugin)
.add(CameraControllerPlugin) .add(CameraControllerPlugin)
.add(DebugPlugin) .add(DebugPlugin)
.add(InspectorPlugin) .add(InspectorPlugin)
@@ -71,6 +69,8 @@ impl PluginGroup for CommonPlugins {
.add(StudioInputPlugin) .add(StudioInputPlugin)
.add(UiLayerPlugin) .add(UiLayerPlugin)
.add(WorldGridPlugin) .add(WorldGridPlugin)
.add(persistance::PersistancePlugin)
.add(BvhPlugin)
} }
} }
@@ -79,41 +79,10 @@ impl PluginGroup for CommonPlugins {
struct EditorPlugins; struct EditorPlugins;
impl PluginGroup for EditorPlugins { impl PluginGroup for EditorPlugins {
fn build(self) -> PluginGroupBuilder { fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>() PluginGroupBuilder::start::<Self>().add(InteractionPlugin)
.add(InteractionPlugin)
.add(UndoPlugin)
} }
} }
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = startBvhViewer))]
pub fn start_bvh_viewer(
bvh_path: &str,
skybox_id: &str,
canvas_selector: &str,
canvas_alt: &str,
viewport_size: ViewportSize,
) {
let mut app = start_internal(
"Mannequin.gltf",
skybox_id,
canvas_selector,
canvas_alt,
viewport_size,
StudioMode::Viewer,
);
let bvh_path = bvh_path.to_string();
app.add_systems(
Startup,
move |mut cmd: Commands, r_assets: Res<AssetServer>| {
cmd.insert_resource(TestBvh(r_assets.load(bvh_path.clone())));
},
);
app.run();
}
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = startViewer))] #[cfg_attr(feature = "wasm", wasm_bindgen(js_name = startViewer))]
/// Start the application as a read-only viewer for a single library object. /// Start the application as a read-only viewer for a single library object.
/// ///
@@ -161,9 +130,35 @@ pub fn start_editor(
.run(); .run();
} }
// TODO: These `start_` functiions should be refactored to better support the #[cfg_attr(feature = "wasm", wasm_bindgen(js_name = startBvhViewer))]
// needs of the "reaodnly viewer" vs "full studio editor" use cases. See pub fn start_bvh_viewer(
// notes in `studio-web/src/lib/viewer.element.ts` for more info. bvh_path: &str,
skybox_id: &str,
canvas_selector: &str,
canvas_alt: &str,
viewport_size: ViewportSize,
) {
let mut app = start_internal(
"Mannequin.gltf",
skybox_id,
canvas_selector,
canvas_alt,
viewport_size,
StudioMode::Viewer,
);
let bvh_path = bvh_path.to_string();
app.add_systems(
Startup,
move |mut cmd: Commands, r_assets: Res<AssetServer>| {
cmd.insert_resource(scene::TestBvh(r_assets.load(bvh_path.clone())));
},
);
app.run();
}
#[cfg_attr(feature = "wasm", wasm_bindgen)] #[cfg_attr(feature = "wasm", wasm_bindgen)]
pub fn start( pub fn start(
object_id: &str, object_id: &str,
@@ -184,6 +179,10 @@ pub fn start(
.run(); .run();
} }
// TODO: These `start_` functiions should be refactored to better support the
// needs of the "reaodnly viewer" vs "full studio editor" use cases. See
// notes in `studio-web/src/lib/viewer.element.ts` for more info.
pub fn start_internal( pub fn start_internal(
object_id: &str, object_id: &str,
skybox_id: &str, skybox_id: &str,
+1 -1
View File
@@ -4,7 +4,7 @@ use studio::{StudioMode, ViewportSize};
fn main() { fn main() {
// Edit the arguments here to change the default startup scene or skybox // Edit the arguments here to change the default startup scene or skybox
let mut app = studio::start_internal( let mut app = studio::start_internal(
"Mannequin.gltf", "bookcase.gltf",
"gum_trees_4k", "gum_trees_4k",
"", // ignored for non-WASM "", // ignored for non-WASM
"Storyteller Studio", "Storyteller Studio",
+45
View File
@@ -0,0 +1,45 @@
use bevy::prelude::*;
use space_editor::prelude::*;
pub struct PersistancePlugin;
impl Plugin for PersistancePlugin {
fn build(&self, app: &mut App) {
info!("PersistancePlugin::build");
app.editor_tab_by_trait(
EditorTabName::Other("Persistance".to_string()),
DebugPersistanceTab::default(),
);
}
}
#[derive(Resource, Default)]
pub struct DebugPersistanceTab {}
impl EditorTab for DebugPersistanceTab {
fn ui(
&mut self,
ui: &mut bevy_inspector_egui::egui::Ui,
commands: &mut Commands,
world: &mut World,
) {
if ui.button("Save").clicked() {
world.send_event(EditorEvent::Save(EditorPrefabPath::MemoryCahce));
}
if ui.button("Load").clicked() {
world.send_event(EditorEvent::Load(EditorPrefabPath::MemoryCahce));
}
if ui.button("Clear level").clicked() {
let mut query = world.query_filtered::<Entity, With<PrefabMarker>>();
for e in query.iter(world) {
commands.entity(e).despawn_recursive();
}
}
}
fn title(&self) -> bevy_inspector_egui::egui::WidgetText {
"Persistance".into()
}
}
+156 -201
View File
@@ -1,19 +1,22 @@
use bevy::{ use bevy::{
asset::AssetPath, asset::{DependencyLoadState, LoadState, RecursiveDependencyLoadState},
core_pipeline::{ core_pipeline::{
bloom::{BloomCompositeMode, BloomPrefilterSettings, BloomSettings}, bloom::{BloomCompositeMode, BloomPrefilterSettings, BloomSettings},
fxaa::Fxaa, fxaa::Fxaa,
tonemapping::Tonemapping, tonemapping::Tonemapping,
}, },
gltf::Gltf, gltf::Gltf,
math::{vec2, vec3, Affine3A}, math::{vec2, Affine3A},
pbr::ShadowFilteringMethod, pbr::ShadowFilteringMethod,
prelude::*, prelude::*,
render::{mesh::skinning::SkinnedMesh, primitives::Aabb, view::NoFrustumCulling}, render::{mesh::skinning::SkinnedMesh, primitives::Aabb, view::NoFrustumCulling},
scene::InstanceId, scene::InstanceId,
}; };
use space_editor::prelude::{space_undo::OneFrameUndoIgnore, PrefabMarker}; use space_editor::{
prelude::PrefabMarker,
space_prefab::{component::SceneAutoChild, editor_registry::EditorRegistryExt},
};
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::prelude::wasm_bindgen;
@@ -42,29 +45,36 @@ impl Plugin for ScenePlugin {
( (
spawn_test_bvh.run_if(resource_exists::<TestBvh>()), // TODO spawn_test_bvh.run_if(resource_exists::<TestBvh>()), // TODO
init_scene_loading, init_scene_loading,
tick_scene_loading.run_if(in_state(SceneState::Loading)),
tick_scene_spawning.run_if(in_state(SceneState::Spawning)),
) )
.in_set(SceneSpawnerSystem), .in_set(SceneSpawnerSystem),
) )
.add_systems(
OnEnter(SceneState::Loading),
despawn_previous_scene.in_set(SceneSpawnerSystem),
)
.add_systems(
OnEnter(SceneState::Spawning),
spawn_next_scene.in_set(SceneSpawnerSystem),
)
.add_systems( .add_systems(
OnEnter(SceneState::Active), OnEnter(SceneState::Active),
( (tag_scene_elements, frame_scene_in_view).in_set(SceneSpawnerSystem),
tag_scene_elements,
spawn_main_scene_light,
frame_scene_in_view,
)
.in_set(SceneSpawnerSystem),
); );
app.add_systems(Update, tag_scene_elements);
//Auto loading screen
//Register handles on which we will look to show loading screen
app.init_resource::<LoadingQueue>();
app.add_systems(
PostUpdate,
(
check_loading,
auto_add_handle::<Scene>,
auto_add_handle::<Mesh>,
auto_add_handle::<StandardMaterial>,
),
);
//setup default light
app.add_systems(Startup, minimal_scene_setup);
app.editor_registry::<BindPoseLocal>();
app.editor_registry::<BindPoseGlobal>();
app.editor_registry::<InvBindPose>();
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]
app.add_systems( app.add_systems(
Update, Update,
@@ -78,19 +88,28 @@ impl Plugin for ScenePlugin {
} }
} }
fn minimal_scene_setup(mut cmd: Commands) {
cmd.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
shadows_enabled: false,
illuminance: 20000.0,
..default()
},
transform: Transform::default().looking_at(Vec3::NEG_ONE, Vec3::Y),
..default()
})
.insert(PrefabMarker);
}
#[cfg_attr(feature = "wasm", wasm_bindgen)] #[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(States, Clone, Copy, Default, Debug, PartialEq, Eq, Hash)] #[derive(States, Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
pub enum SceneState { pub enum SceneState {
#[default] #[default]
None, None,
Loading, Loading,
Spawning,
Active, Active,
} }
#[derive(Resource, Deref, DerefMut)]
struct PendingScene(pub Handle<Gltf>);
#[derive(Resource, Deref)] #[derive(Resource, Deref)]
pub struct ActiveScene { pub struct ActiveScene {
#[deref] #[deref]
@@ -98,6 +117,62 @@ pub struct ActiveScene {
pub source: Handle<Gltf>, pub source: Handle<Gltf>,
} }
#[derive(Resource, Default)]
pub struct LoadingQueue {
pub queue: Vec<UntypedHandle>,
}
fn check_loading(
mut loading_queue: ResMut<LoadingQueue>,
mut st_next: ResMut<NextState<SceneState>>,
asset_server: Res<AssetServer>,
) {
if !loading_queue.queue.is_empty() {
//filter queue to only include notloaded assets
let mut not_loaded_count = 0;
loading_queue.queue.retain(|h| {
if let Some((load_state, dep_load_state, rec_load_state)) =
asset_server.get_load_states(h.id())
{
let is_loaded = dep_load_state == DependencyLoadState::Loaded;
let is_failed = dep_load_state == DependencyLoadState::Failed
|| rec_load_state == RecursiveDependencyLoadState::Failed
|| load_state == LoadState::Failed;
if load_state == LoadState::NotLoaded {
not_loaded_count += 1;
}
!(is_failed || is_loaded)
} else {
false
}
});
info!("Loading {} assets", loading_queue.queue.len());
if loading_queue.queue.len() - not_loaded_count == 0 {
//not change state
} else {
st_next.set(SceneState::Loading);
}
} else {
st_next.set(SceneState::Active);
}
}
fn auto_add_handle<T: Asset>(
mut queue: ResMut<LoadingQueue>,
query: Query<&Handle<T>, Added<Handle<T>>>,
) {
for h in query.iter() {
queue.queue.push(h.clone().untyped());
}
}
#[derive(Event, Deref)] #[derive(Event, Deref)]
pub struct LoadSceneEvent(pub String); pub struct LoadSceneEvent(pub String);
@@ -110,25 +185,32 @@ pub struct LoadSceneEvent(pub String);
#[derive(Component)] #[derive(Component)]
pub struct SceneElement; pub struct SceneElement;
#[derive(Component)]
pub struct SceneData {
pub scene_path: String,
}
#[derive(Component)] #[derive(Component)]
pub struct SkeletalJoint(pub Color); pub struct SkeletalJoint(pub Color);
#[derive(Component)] #[derive(Component)]
pub struct SceneRoot; pub struct SceneRoot;
#[derive(Resource, Deref, DerefMut)] #[derive(Resource, Deref, DerefMut)]
pub struct TestBvh(pub Handle<Bvh>); // TODO pub struct TestBvh(pub Handle<Bvh>); // TODO
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct BindPoseLocal(pub Transform); pub struct BindPoseLocal(pub Transform);
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct BindPoseGlobal(pub Affine3A); pub struct BindPoseGlobal(pub Affine3A);
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct InvBindPose(pub Affine3A); pub struct InvBindPose(pub Affine3A);
fn initialize(mut cmd: Commands, r_assets: Res<AssetServer>) { fn initialize(mut cmd: Commands) {
// Camera, graphics settings // Camera, graphics settings
let focus = Vec3::ZERO; let focus = Vec3::ZERO;
let camera_xform = Transform::from_xyz(-2.5, 4.5, 5.0).looking_at(focus, Vec3::Y); let camera_xform = Transform::from_xyz(-2.5, 4.5, 5.0).looking_at(focus, Vec3::Y);
@@ -177,104 +259,29 @@ fn spawn_test_bvh(
} }
} }
fn init_scene_loading( fn init_scene_loading(mut cmd: Commands, mut er: EventReader<LoadSceneEvent>) {
mut cmd: Commands,
assets: Res<AssetServer>,
r_active_scene: Option<Res<ActiveScene>>,
mut er: EventReader<LoadSceneEvent>,
mut st_next: ResMut<NextState<SceneState>>,
) {
if let Some(id) = er.read().last() { if let Some(id) = er.read().last() {
let name = &**id; info!("Loading scene: {}", id.0);
let path = format!("gltf/{name}"); cmd.spawn((
// Avoid trying to re-load this scene if it's already active
if let Some(active_scene_path) =
r_active_scene.and_then(|scene| scene.source.path().cloned())
{
if active_scene_path == AssetPath::parse(&path) {
return;
}
}
cmd.insert_resource(PendingScene(assets.load(path)));
st_next.set(SceneState::Loading);
}
}
fn despawn_previous_scene(mut cmd: Commands, q_scene_root: Query<Entity, With<SceneRoot>>) {
if let Ok(scene_root) = q_scene_root.get_single() {
cmd.entity(scene_root).despawn_recursive();
}
}
fn tick_scene_loading(
r_pending_scene: Res<PendingScene>,
ra_gltfs: Res<Assets<Gltf>>,
mut er_scene_events: EventReader<AssetEvent<Scene>>,
mut st_next: ResMut<NextState<SceneState>>,
) {
for event in er_scene_events.read() {
if let AssetEvent::LoadedWithDependencies { id } = event {
let gltf = ra_gltfs.get(&**r_pending_scene).unwrap();
if gltf.default_scene.is_some() && gltf.default_scene.as_ref().unwrap().id() == *id {
st_next.set(SceneState::Spawning);
break;
}
}
}
}
fn spawn_next_scene(
mut cmd: Commands,
ra_gltfs: Res<Assets<Gltf>>,
r_pending_scene: Res<PendingScene>,
mut r_scene_spawner: ResMut<SceneSpawner>,
) {
let gltf = ra_gltfs.get(&**r_pending_scene).unwrap();
let scene = gltf
.default_scene
.as_ref()
.expect("Expected glTF to include a default scene");
let parent = cmd
.spawn((
Name::new("Scene"),
SceneRoot,
SceneElement,
SpatialBundle::default(), SpatialBundle::default(),
space_editor::prelude::GltfPrefab {
path: format!("gltf/{}", id.0),
scene: "Scene0".to_string(),
},
PrefabMarker, PrefabMarker,
OneFrameUndoIgnore::default(), //To prevert undo try to register add event SceneRoot,
)) ));
.id();
let instance = r_scene_spawner.spawn_as_child(scene.clone(), parent);
cmd.remove_resource::<PendingScene>();
cmd.insert_resource(ActiveScene {
instance,
source: r_pending_scene.clone(),
});
}
fn tick_scene_spawning(
r_scene_spawner: Res<SceneSpawner>,
r_active_scene: Res<ActiveScene>,
mut st_next: ResMut<NextState<SceneState>>,
) {
// TODO: The instance is considered "ready" even if shaders still need to be
// compiled, so this event isn't really sufficient for controlling a
// loading screen.
if r_scene_spawner.instance_is_ready(**r_active_scene) {
st_next.set(SceneState::Active);
} }
} }
#[derive(Component)]
pub struct NoMeshElement;
#[derive(Component)]
pub struct InitedKointSkinnedMesh;
fn tag_scene_elements( fn tag_scene_elements(
mut cmd: Commands, mut cmd: Commands,
r_scene_spawner: Res<SceneSpawner>,
r_active_scene: Res<ActiveScene>,
ra_gltfs: Res<Assets<Gltf>>,
// A "glTF mesh" is actually a container for one or more "glTF primitives", // A "glTF mesh" is actually a container for one or more "glTF primitives",
// which is basically a bundle of vertices paired with the material used to // which is basically a bundle of vertices paired with the material used to
// shade them. It's those "sub-meshes" that carry the actual mesh handles, // shade them. It's those "sub-meshes" that carry the actual mesh handles,
@@ -282,32 +289,39 @@ fn tag_scene_elements(
// because they often consist of stuff like a single eyeball, or just the // because they often consist of stuff like a single eyeball, or just the
// leafy parts of a plant. Here we only want to tag the elements that a user // leafy parts of a plant. Here we only want to tag the elements that a user
// might want to individually address when arranging or animating a scene. // might want to individually address when arranging or animating a scene.
q_named_non_primitives: Query<&Name, Without<Handle<Mesh>>>, q_named_non_primitives: Query<
q_skinned_meshes: Query<&SkinnedMesh>, (Entity, &Children),
(
With<SceneAutoChild>,
With<Name>,
Without<Handle<Mesh>>,
Without<SceneElement>,
Without<NoMeshElement>,
),
>,
q_skinned_meshes: Query<(Entity, &SkinnedMesh), Without<InitedKointSkinnedMesh>>,
mut q_xforms: ParamSet<(TransformHelper, Query<&Transform>)>, mut q_xforms: ParamSet<(TransformHelper, Query<&Transform>)>,
mesh_entities: Query<Entity, With<Handle<Mesh>>>,
) { ) {
let gltf = ra_gltfs.get(&r_active_scene.source).unwrap(); for (e, children) in q_named_non_primitives.iter() {
let mut has_mesh = false;
for (ent, name) in r_scene_spawner for child in children {
.iter_instance_entities(**r_active_scene) if mesh_entities.contains(*child) {
.filter_map(|ent| match q_named_non_primitives.get(ent) { has_mesh = true;
Ok(name) => Some((ent, name)), break;
_ => None, }
}) }
{ if has_mesh {
if gltf.named_nodes.contains_key(&**name) { cmd.entity(e).insert(SceneElement);
cmd.entity(ent).insert(SceneElement).insert(PrefabMarker); } else {
cmd.entity(e).insert(NoMeshElement);
} }
} }
for (ent, skinned_mesh) in r_scene_spawner for (ent, skinned_mesh) in q_skinned_meshes.iter() {
.iter_instance_entities(**r_active_scene) cmd.entity(ent)
.filter_map(|ent| match q_skinned_meshes.get(ent) { .insert(NoFrustumCulling)
Ok(skinned_mesh) => Some((ent, skinned_mesh)), .insert(InitedKointSkinnedMesh);
_ => None,
})
{
cmd.entity(ent).insert(NoFrustumCulling);
for &joint in skinned_mesh.joints.iter() { for &joint in skinned_mesh.joints.iter() {
let global_bind_pose = q_xforms.p0().compute_global_transform(joint).unwrap(); let global_bind_pose = q_xforms.p0().compute_global_transform(joint).unwrap();
@@ -323,76 +337,17 @@ fn tag_scene_elements(
} }
} }
fn spawn_main_scene_light(
mut cmd: Commands,
r_scene_spawner: Res<SceneSpawner>,
r_active_scene: Res<ActiveScene>,
q_aabbs: Query<(&Aabb, &GlobalTransform)>,
q_scene_root: Query<Entity, With<SceneRoot>>,
) {
let Ok(scene_root) = q_scene_root.get_single() else {
return;
};
let Some(aabb) = Aabb::combine(
r_scene_spawner
.iter_instance_entities(**r_active_scene)
.filter_map(|ent| q_aabbs.get(ent).ok()),
) else {
return;
};
let light_offset_base = vec3(0.5, 1., 1.).normalize();
let size = f32::max(
f32::max(aabb.half_extents.x, aabb.half_extents.y),
aabb.half_extents.z,
) * 2.;
let light_pos = Vec3::from(aabb.center) + light_offset_base * size * 2.;
cmd.spawn(DebugPoint {
point: light_pos,
color: Color::WHITE,
});
cmd.entity(scene_root).with_children(|cmd| {
cmd.spawn((
Name::new("Light"),
SceneElement,
SpotLightBundle {
spot_light: SpotLight {
color: Color::WHITE,
// TODO: This doesn't scale super well to very large objects
intensity: 200. * size,
radius: 0.1,
shadows_enabled: true,
shadow_normal_bias: 0.2 * size,
outer_angle: f32::to_radians(150.),
inner_angle: f32::to_radians(45.),
..default()
},
transform: Transform::from_translation(light_pos)
.looking_at(aabb.center.into(), Vec3::Y),
..default()
},
));
});
}
fn frame_scene_in_view( fn frame_scene_in_view(
mut cmd: Commands, mut cmd: Commands,
r_scene_spawner: Res<SceneSpawner>, q_aabbs: Query<(&Aabb, &GlobalTransform), With<SceneAutoChild>>,
r_active_scene: Res<ActiveScene>,
q_aabbs: Query<(&Aabb, &GlobalTransform)>,
mut q_camera: Query<(&mut CameraController, &mut Transform, &Projection)>, mut q_camera: Query<(&mut CameraController, &mut Transform, &Projection)>,
) { ) {
let Ok((mut cam, mut cam_xform, cam_proj)) = q_camera.get_single_mut() else { let Ok((mut cam, mut cam_xform, cam_proj)) = q_camera.get_single_mut() else {
error!("Failed to get camera controller");
return; return;
}; };
let Some(aabb) = Aabb::combine( let Some(aabb) = Aabb::combine(q_aabbs.iter()) else {
r_scene_spawner
.iter_instance_entities(**r_active_scene)
.filter_map(|ent| q_aabbs.get(ent).ok()),
) else {
return; return;
}; };