Skip to content

Commit

Permalink
feat: bevy_args (#82)
Browse files Browse the repository at this point in the history
* feat: pre-pre-startup arguments

* feedback: replace viewer's println with log
  • Loading branch information
mosure authored Jan 27, 2024
1 parent b4d10da commit 923c77e
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 160 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ headless = [
]

viewer = [
"bevy_args",
"bevy-inspector-egui",
"bevy_panorbit_camera",
# "bevy_transform_gizmo",
"bevy/multi-threaded", # bevy screenshot functionality requires bevy/multi-threaded as of 0.12.1
"clap",
]

web = [
Expand All @@ -129,13 +131,15 @@ webgl2 = ["bevy/webgl2"]


[dependencies]
bevy_args = { version = "1.2.0", optional = true }
bevy-inspector-egui = { version = "0.22", optional = true }
bevy_mod_picking = { version = "0.17", optional = true }
bevy_panorbit_camera = { version = "0.10", optional = true }
bevy_transform_gizmo = { version = "0.9", optional = true }
bincode2 = { version = "2.0", optional = true }
byte-unit = { version = "5.0", optional = true }
bytemuck = "1.14"
clap = { version = "4.4", features = ["derive"], optional = true }
flate2 = { version = "1.0", optional = true }
flexbuffers = { version = "2.0", optional = true }
half = { version = "2.3.1", optional = true, features = ["serde"] }
Expand Down
43 changes: 25 additions & 18 deletions examples/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@
// c_rr --example headless --no-default-features --features "headless" -- [filename]

use bevy::{
app::ScheduleRunnerPlugin, core::Name, core_pipeline::tonemapping::Tonemapping, prelude::*, render::renderer::RenderDevice,
prelude::*,
app::ScheduleRunnerPlugin,
core::Name,
core_pipeline::tonemapping::Tonemapping,
render::renderer::RenderDevice,
};
use bevy_args::BevyArgsPlugin;
use bevy_panorbit_camera::{
PanOrbitCamera,
PanOrbitCameraPlugin,
};

use bevy_gaussian_splatting::{
random_gaussians, utils::get_arg, GaussianCloud, GaussianSplattingBundle,
GaussianCloud,
GaussianSplattingBundle,
GaussianSplattingPlugin,
random_gaussians,
utils::GaussianSplattingViewer,
};


/// Derived from: https://github.com/bevyengine/bevy/pull/5550
mod frame_capture {
pub mod image_copy {
Expand Down Expand Up @@ -325,7 +338,7 @@ mod frame_capture {
if n < 1 {
for image in images_to_save.iter() {
let img_bytes = images.get_mut(image.id()).unwrap();

let img = match img_bytes.clone().try_into_dynamic() {
Ok(img) => img.to_rgba8(),
Err(e) => panic!("Failed to create image buffer {e:?}"),
Expand Down Expand Up @@ -355,26 +368,20 @@ mod frame_capture {
fn setup_gaussian_cloud(
mut commands: Commands,
asset_server: Res<AssetServer>,
gaussian_splatting_viewer: Res<GaussianSplattingViewer>,
mut gaussian_assets: ResMut<Assets<GaussianCloud>>,
mut scene_controller: ResMut<frame_capture::scene::SceneController>,
mut images: ResMut<Assets<Image>>,
render_device: Res<RenderDevice>,
) {
let cloud: Handle<GaussianCloud>;

// TODO: add proper GaussianSplattingViewer argument parsing
let file_arg = get_arg(1);
if let Some(n) = file_arg.clone().and_then(|s| s.parse::<usize>().ok()) {
println!("generating {} gaussians", n);
cloud = gaussian_assets.add(random_gaussians(n));
} else if let Some(filename) = file_arg {
if filename == "--help" {
println!("usage: cargo run -- [filename | n]");
return;
}

println!("loading {}", filename);
cloud = asset_server.load(filename.to_string());
if gaussian_splatting_viewer.gaussian_count > 0 {
println!("generating {} gaussians", gaussian_splatting_viewer.gaussian_count);
cloud = gaussian_assets.add(random_gaussians(gaussian_splatting_viewer.gaussian_count));
} else if !gaussian_splatting_viewer.input_file.is_empty() {
println!("loading {}", gaussian_splatting_viewer.input_file);
cloud = asset_server.load(&gaussian_splatting_viewer.input_file);
} else {
cloud = gaussian_assets.add(GaussianCloud::test_model());
}
Expand Down Expand Up @@ -435,10 +442,10 @@ fn headless_app() {
close_when_requested: false,
}),
);

app.add_plugins(frame_capture::image_copy::ImageCopyPlugin);
app.add_plugins(BevyArgsPlugin::<GaussianSplattingViewer>::default());

// headless frame capture
app.add_plugins(frame_capture::image_copy::ImageCopyPlugin);
app.add_plugins(frame_capture::scene::CaptureFramePlugin);

app.add_plugins(ScheduleRunnerPlugin::run_loop(
Expand Down
91 changes: 71 additions & 20 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,66 @@ use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use std::collections::HashMap;

use bevy::prelude::*;
use bevy_args::{
Deserialize,
Parser,
Serialize,
};


#[derive(
Debug,
Resource,
Serialize,
Deserialize,
Parser,
)]
#[command(about = "bevy_gaussian_splatting viewer", version, long_about = None)]
pub struct GaussianSplattingViewer {
#[arg(long, default_value = "true")]
pub editor: bool,
#[arg(long, default_value = "true")]
pub press_esc_close: bool,
#[arg(long, default_value = "true")]
pub press_s_screenshot: bool,
#[arg(long, default_value = "true")]
pub show_fps: bool,
#[arg(long, default_value = "1920.0")]
pub width: f32,
#[arg(long, default_value = "1080.0")]
pub height: f32,
#[arg(long, default_value = "bevy_gaussian_splatting")]
pub name: String,
#[arg(long, default_value = "1")]
pub msaa_samples: u8,

#[arg(long, default_value = "")]
pub input_file: String,
#[arg(long, default_value = "0")]
pub gaussian_count: usize,
#[arg(long, default_value = "0")]
pub particle_count: usize,
}

impl Default for GaussianSplattingViewer {
fn default() -> GaussianSplattingViewer {
GaussianSplattingViewer {
editor: true,
press_esc_close: true,
press_s_screenshot: true,
show_fps: true,
width: 1920.0,
height: 1080.0,
name: "bevy_gaussian_splatting".to_string(),
msaa_samples: 1,
input_file: "".to_string(),
gaussian_count: 0,
particle_count: 0,
}
}
}


pub fn setup_hooks() {
#[cfg(debug_assertions)]
Expand All @@ -16,24 +76,15 @@ pub fn setup_hooks() {
}


#[cfg(not(target_arch = "wasm32"))]
pub fn get_arg(n: usize) -> Option<String> {
std::env::args().nth(n)
}

#[cfg(target_arch = "wasm32")]
pub fn get_arg(n: usize) -> Option<String> {
let window = web_sys::window()?;
let location = window.location();
let search = location.search().ok()?;

let args = search
.trim_start_matches('?')
.split('&')
.map(|s| s.splitn(2, '=').collect::<Vec<_>>())
.filter(|v| v.len() == 2)
.map(|v| (v[0].to_string(), v[1].to_string()))
.collect::<std::collections::HashMap<_, _>>();

args.get(&format!("arg{}", n)).cloned()
pub fn log(msg: &str) {
#[cfg(debug_assertions)]
#[cfg(target_arch = "wasm32")]
{
web_sys::console::log_1(&msg.into());
}
#[cfg(debug_assertions)]
#[cfg(not(target_arch = "wasm32"))]
{
println!("{}", msg);
}
}
43 changes: 13 additions & 30 deletions tools/compare_aabb_obb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use bevy::{
core::Name,
core_pipeline::tonemapping::Tonemapping,
};
use bevy_args::{
BevyArgsPlugin,
parse_args,
};
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_panorbit_camera::{
PanOrbitCamera,
Expand All @@ -16,34 +20,14 @@ use bevy_gaussian_splatting::{
GaussianCloudSettings,
GaussianSplattingBundle,
GaussianSplattingPlugin,
utils::setup_hooks, SphericalHarmonicCoefficients,
utils::{
setup_hooks,
GaussianSplattingViewer,
},
SphericalHarmonicCoefficients,
};


// TODO: move to editor crate
pub struct GaussianSplattingViewer {
pub editor: bool,
pub esc_close: bool,
pub show_fps: bool,
pub width: f32,
pub height: f32,
pub name: String,
}

impl Default for GaussianSplattingViewer {
fn default() -> GaussianSplattingViewer {
GaussianSplattingViewer {
editor: true,
esc_close: true,
show_fps: true,
width: 1920.0,
height: 1080.0,
name: "bevy_gaussian_splatting".to_string(),
}
}
}


pub fn setup_aabb_obb_compare(
mut commands: Commands,
mut gaussian_assets: ResMut<Assets<GaussianCloud>>,
Expand Down Expand Up @@ -118,7 +102,7 @@ pub fn setup_aabb_obb_compare(
}

fn compare_aabb_obb_app() {
let config = GaussianSplattingViewer::default();
let config = parse_args::<GaussianSplattingViewer>();
let mut app = App::new();

// setup for gaussian viewer app
Expand All @@ -139,15 +123,14 @@ fn compare_aabb_obb_app() {
..default()
}),
);
app.add_plugins((
PanOrbitCameraPlugin,
));
app.add_plugins(BevyArgsPlugin::<GaussianSplattingViewer>::default());
app.add_plugins(PanOrbitCameraPlugin);

if config.editor {
app.add_plugins(WorldInspectorPlugin::new());
}

if config.esc_close {
if config.press_esc_close {
app.add_systems(Update, esc_close);
}

Expand Down
Loading

0 comments on commit 923c77e

Please sign in to comment.