diff --git a/studio/src/lib.rs b/studio/src/lib.rs index 6eff445..aa0c9e1 100644 --- a/studio/src/lib.rs +++ b/studio/src/lib.rs @@ -21,6 +21,7 @@ mod hierarchy; mod input; mod inspector; mod interaction; +pub mod material; pub mod math; mod mixamo; mod path; @@ -33,6 +34,7 @@ mod ui; pub mod viewport; mod world_grid; +use crate::material::MaterialPlugin; pub use crate::{ anim::AnimPlugin, bvh::BvhPlugin, @@ -86,6 +88,7 @@ impl PluginGroup for CommonPlugins { .add(InspectorPlugin) .add(InterpolationPlugin) .add(ScenePlugin) + .add(MaterialPlugin) .add(SkyboxPlugin) .add(StudioInputPlugin) .add(UiLayerPlugin) diff --git a/studio/src/material/mod.rs b/studio/src/material/mod.rs new file mode 100644 index 0000000..3088b26 --- /dev/null +++ b/studio/src/material/mod.rs @@ -0,0 +1,37 @@ +use crate::material::short_material::ShortMaterial; +use bevy::app::{App, Plugin, PostUpdate}; +use bevy::asset::{Assets, Handle}; +use bevy::pbr::StandardMaterial; +use bevy::prelude::{Commands, Entity, Query, ResMut}; +use space_editor::prelude::EditorRegistryExt; + +pub mod short_material; + +pub struct MaterialPlugin; +impl Plugin for MaterialPlugin { + fn build(&self, app: &mut App) { + app.register_type::() + .editor_registry::(); + + app.add_systems(PostUpdate, apply_short_materials); + } +} + +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(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::(); + } +} diff --git a/studio/src/material/short_material.rs b/studio/src/material/short_material.rs new file mode 100644 index 0000000..9ba7706 --- /dev/null +++ b/studio/src/material/short_material.rs @@ -0,0 +1,25 @@ +use bevy::pbr::StandardMaterial; +use bevy::prelude::{Color, Component, Reflect}; + +use bevy::ecs::reflect::ReflectComponent; +use bevy::reflect::std_traits::ReflectDefault; + +#[derive(Reflect, Default, Clone, Component)] +#[reflect(Default, Component)] +pub struct ShortMaterial { + pub base_color: Color, + pub perceptual_roughness: f32, + pub metallic: f32, + pub reflectance: f32, +} + +impl ShortMaterial { + pub fn new(material: &StandardMaterial) -> Self { + Self { + base_color: material.base_color, + perceptual_roughness: material.perceptual_roughness, + metallic: material.metallic, + reflectance: material.reflectance, + } + } +} diff --git a/studio/src/ui/inspector.rs b/studio/src/ui/inspector.rs index 5d6f3df..7cac5f5 100644 --- a/studio/src/ui/inspector.rs +++ b/studio/src/ui/inspector.rs @@ -20,6 +20,7 @@ use space_editor::{ }; use wasm_bindgen::prelude::*; +use crate::material::short_material::ShortMaterial; use crate::{ interaction::Selection, wasm::{self, StaticBuffer, WasmMessageBuffer}, @@ -94,7 +95,6 @@ impl Plugin for InspectorPlugin { .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() @@ -142,25 +142,6 @@ fn create_short_materials( } } -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(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< @@ -591,26 +572,6 @@ fn auto_serialize_asset( ev_update_description.send(UpdateEntityDescription); } -#[derive(Reflect, Default, Clone, Component)] -#[reflect(Default, Component)] -struct ShortMaterial { - pub base_color: Color, - pub perceptual_roughness: f32, - pub metallic: f32, - pub reflectance: f32, -} - -impl ShortMaterial { - pub fn new(material: &StandardMaterial) -> Self { - Self { - base_color: material.base_color, - perceptual_roughness: material.perceptual_roughness, - metallic: material.metallic, - reflectance: material.reflectance, - } - } -} - fn serialize_standart_materal( r_selection: Option>, q_m: Query<&Handle>,