Skip to content

Commit

Permalink
add render layers option to viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Aug 2, 2024
1 parent 965cd5e commit c150355
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_vrm/src/layers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::sync::LazyLock;

use bevy::{render::view::RenderLayers, utils::HashMap};
use serde_vrm::vrm0::FirstPersonFlag;

pub use serde_vrm::vrm0::FirstPersonFlag;

pub const FIRST_PERSON_LAYER: usize = 7;
pub const THIRD_PERSON_LAYER: usize = 8;
Expand Down
39 changes: 36 additions & 3 deletions crates/vrm_viewer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

use std::f32::consts::PI;

use bevy::{asset::AssetMetaCheck, prelude::*};
use bevy::{asset::AssetMetaCheck, prelude::*, render::view::RenderLayers};
use bevy_egui::EguiPlugin;
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};
use bevy_vrm::{loader::Vrm, mtoon::MtoonSun, VrmBundle, VrmPlugins};
use bevy_vrm::{
layers::{FirstPersonFlag, RENDER_LAYERS},
loader::Vrm,
mtoon::MtoonSun,
VrmBundle, VrmPlugins,
};
use ui::RenderLayer;

mod draw_spring_bones;
mod move_leg;
Expand Down Expand Up @@ -39,6 +45,7 @@ impl Plugin for VrmViewerPlugin {
draw_spring_bones::move_avatar,
move_leg::move_leg,
read_dropped_files,
set_render_layers,
ui::update_ui,
),
);
Expand All @@ -48,8 +55,9 @@ impl Plugin for VrmViewerPlugin {
#[derive(Resource, Default)]
struct Settings {
pub draw_spring_bones: bool,
pub move_leg: bool,
pub move_avatar: bool,
pub move_leg: bool,
pub render_layer: RenderLayer,
}

const VRM_PATH: &str = "alicia.vrm";
Expand Down Expand Up @@ -92,6 +100,31 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
});
}

fn set_render_layers(
cameras: Query<Entity, With<Camera>>,
mut commands: Commands,
mut prev: Local<FirstPersonFlag>,
settings: Res<Settings>,
) {
for entity in cameras.iter() {
let flag = match settings.render_layer {
RenderLayer::Both => FirstPersonFlag::Both,
RenderLayer::FirstPerson => FirstPersonFlag::FirstPersonOnly,
RenderLayer::ThirdPerson => FirstPersonFlag::ThirdPersonOnly,
};

if flag != *prev {
*prev = flag;

let layers = RENDER_LAYERS[&flag].clone();

commands
.entity(entity)
.insert(layers.union(&RenderLayers::layer(0)));
}
}
}

fn read_dropped_files(
mut commands: Commands,
mut events: EventReader<FileDragAndDrop>,
Expand Down
45 changes: 44 additions & 1 deletion crates/vrm_viewer/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
use bevy::prelude::*;
use bevy_egui::EguiContexts;
use bevy_egui::{egui::ComboBox, EguiContexts};

use crate::Settings;

#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub enum RenderLayer {
#[default]
Both,
FirstPerson,
ThirdPerson,
}

impl RenderLayer {
fn as_str(&self) -> &str {
match self {
Self::Both => "Both",
Self::FirstPerson => "FirstPersonOnly",
Self::ThirdPerson => "ThirdPersonOnly",
}
}

fn from_str(s: &str) -> Option<Self> {
match s {
"Both" => Some(Self::Both),
"FirstPersonOnly" => Some(Self::FirstPerson),
"ThirdPersonOnly" => Some(Self::ThirdPerson),
_ => None,
}
}

fn variants() -> &'static [&'static str] {
&["Both", "FirstPersonOnly", "ThirdPersonOnly"]
}
}

pub fn update_ui(mut contexts: EguiContexts, mut settings: ResMut<Settings>) {
bevy_egui::egui::Window::new("VRM Viewer").show(contexts.ctx_mut(), |ui| {
ui.vertical(|ui| {
Expand All @@ -26,6 +57,18 @@ pub fn update_ui(mut contexts: EguiContexts, mut settings: ResMut<Settings>) {
ui.checkbox(&mut settings.move_leg, "Move leg bone");
ui.checkbox(&mut settings.move_avatar, "Move avatar");

ComboBox::from_label("Render layers")
.selected_text(settings.render_layer.as_str())
.show_ui(ui, |ui| {
for variant in RenderLayer::variants() {
ui.selectable_value(
&mut settings.render_layer,
RenderLayer::from_str(variant).unwrap(),
*variant,
);
}
});

ui.separator();

ui.vertical_centered(|ui| {
Expand Down

0 comments on commit c150355

Please sign in to comment.