From 87ecf2d7e4865a43b7e5bd8299d7a276d7aec34c Mon Sep 17 00:00:00 2001 From: rewin Date: Fri, 22 Mar 2024 23:35:20 +0300 Subject: [PATCH] Save for material (#101) --- Cargo.lock | 16 +++++----- studio/Cargo.toml | 2 +- studio/src/ui/inspector.rs | 64 +++++++++++++++++++++++++++++++------- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2a4788a..0efdb6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4685,7 +4685,7 @@ dependencies = [ [[package]] name = "space_bevy_xpbd_plugin" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "bevy_xpbd_3d", @@ -4695,7 +4695,7 @@ dependencies = [ [[package]] name = "space_editor" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "space_bevy_xpbd_plugin", @@ -4706,7 +4706,7 @@ dependencies = [ [[package]] name = "space_editor_core" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "space_persistence", @@ -4718,7 +4718,7 @@ dependencies = [ [[package]] name = "space_editor_ui" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "bevy_debug_grid", @@ -4739,7 +4739,7 @@ dependencies = [ [[package]] name = "space_persistence" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "ron", @@ -4749,7 +4749,7 @@ dependencies = [ [[package]] name = "space_prefab" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "bevy-inspector-egui", @@ -4763,7 +4763,7 @@ dependencies = [ [[package]] name = "space_shared" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "bevy-inspector-egui", @@ -4773,7 +4773,7 @@ dependencies = [ [[package]] name = "space_undo" version = "0.3.1" -source = "git+https://github.com/rewin123/space_editor?rev=ce83f86dc902787ab16213f3209276a784654df6#ce83f86dc902787ab16213f3209276a784654df6" +source = "git+https://github.com/rewin123/space_editor?rev=0f2439ba8cd38ebf2db8d90ea6de736992bcb56e#0f2439ba8cd38ebf2db8d90ea6de736992bcb56e" dependencies = [ "bevy", "pretty-type-name", diff --git a/studio/Cargo.toml b/studio/Cargo.toml index 2872673..b5d9bc2 100644 --- a/studio/Cargo.toml +++ b/studio/Cargo.toml @@ -46,7 +46,7 @@ futures-lite = "2.2.0" futures = "0.3.30" image = "0.24.8" bytemuck = "1.14.1" -space_editor = {git = "https://github.com/rewin123/space_editor", rev = "ce83f86dc902787ab16213f3209276a784654df6"} +space_editor = {git = "https://github.com/rewin123/space_editor", rev = "0f2439ba8cd38ebf2db8d90ea6de736992bcb56e"} anyhow.workspace = true serde_repr = "0.1.18" bevy_tweening = "0.9" diff --git a/studio/src/ui/inspector.rs b/studio/src/ui/inspector.rs index 3972318..aa238bf 100644 --- a/studio/src/ui/inspector.rs +++ b/studio/src/ui/inspector.rs @@ -1,11 +1,7 @@ use std::{marker::PhantomData, sync::Arc}; use bevy::{ - ecs::world::unsafe_world_cell::UnsafeWorldCell, - prelude::*, - reflect::{serde::ReflectSerializer, GetTypeRegistration}, - utils::HashMap, - window::PrimaryWindow, + ecs::world::unsafe_world_cell::UnsafeWorldCell, prelude::*, reflect::{serde::ReflectSerializer, GetTypeRegistration}, transform::commands, utils::HashMap, window::PrimaryWindow }; use serde::{Deserialize, Serialize}; use serde_json::{self, json, Map as JsonMap, Value as JsonValue}; @@ -14,7 +10,7 @@ use space_editor::{ editor_tab::{EditorTab, EditorTabName}, EditorUiAppExt, }, - space_prefab::component::MeshPrimitivePrefab, + space_prefab::{component::MeshPrimitivePrefab, editor_registry::EditorRegistryExt, save::SaveState}, }; use wasm_bindgen::prelude::*; @@ -66,7 +62,8 @@ impl Plugin for InspectorPlugin { //register standart material { - app.register_type::() + + app.editor_registry::() .add_event::>>() .add_systems( Update, @@ -84,6 +81,14 @@ impl Plugin for InspectorPlugin { serialize_standart_materal.in_set(InspectorSystem::PerComponentCollect), ); + + + app.add_systems( + OnEnter(SaveState::Save), + (create_short_materials, apply_deferred).chain().before(space_editor::space_prefab::sub_scene::prepare_auto_scene) + ); + app.add_systems(PostUpdate, apply_short_materials); + app.world.resource_mut::().getters.insert( StandardMaterial::get_type_registration() .type_info() @@ -118,6 +123,40 @@ impl Plugin for InspectorPlugin { } } + + +fn create_short_materials( + mut commands : Commands, + q_mats: Query<(Entity, &Handle)>, + materials: Res>, +) { + for (entity, handle) in q_mats.iter() { + if let Some(material) = materials.get(handle) { + commands.entity(entity).insert(ShortMaterial::new(material)); + } + } +} + +fn apply_short_materials( + mut commands : Commands, + q_mats: Query<(Entity, &Handle, &ShortMaterial)>, + mut materials: ResMut>,) +{ + for (entity, handle, short) in q_mats.iter() { + let Some(mut material) = materials.get_mut(handle) else { + continue; + }; + + material.base_color = short.base_color; + material.perceptual_roughness = short.perceptual_roughness; + material.metallic = short.metallic; + material.reflectance = short.reflectance; + + commands.entity(entity).remove::(); + } +} + + #[derive(Resource, Default)] struct InspectGetters { getters: HashMap< @@ -538,8 +577,8 @@ fn auto_serialize_asset( ev_update_description.send(UpdateEntityDescription); } -#[derive(Reflect, Default)] -#[reflect(Default)] +#[derive(Reflect, Default, Clone, Component)] +#[reflect(Default, Component)] struct ShortMaterial { pub base_color: Color, pub perceptual_roughness: f32, @@ -558,6 +597,7 @@ impl ShortMaterial { } } + fn serialize_standart_materal( r_selection: Option>, q_m: Query<&Handle>, @@ -574,12 +614,12 @@ fn serialize_standart_materal( ev_inspect.clear(); - if let Some(handle) = r_selection + if let Some((target, handle)) = r_selection .as_ref() .and_then(|selection| find_handle_in_self_or_children(***selection, &q_children, &q_m)) - .and_then(|target| q_m.get(target).ok()) + .and_then(|target| q_m.get(target).ok().map(|h| (target, h.clone()))) { - if let Some(material) = assets.get(handle) { + if let Some(material) = assets.get(handle.clone()) { let registry = registry.read(); let short_material = ShortMaterial::new(material); let json =