From f66746f9ad087de1cc3f740cb9f958cb4882c960 Mon Sep 17 00:00:00 2001 From: victor <52110451+cs50victor@users.noreply.github.com> Date: Mon, 19 Feb 2024 11:49:22 -0500 Subject: [PATCH] refactor: load remote splat file (#22) --- crates/bevy_remote_asset/src/lib.rs | 2 -- .../bevy_remote_asset/src/web_asset_source.rs | 22 +++++++++++++++ new_media/src/asset.rs | 28 ++++++++----------- new_media/src/main.rs | 9 +++--- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/crates/bevy_remote_asset/src/lib.rs b/crates/bevy_remote_asset/src/lib.rs index 79106de..bccd2b4 100644 --- a/crates/bevy_remote_asset/src/lib.rs +++ b/crates/bevy_remote_asset/src/lib.rs @@ -1,5 +1,3 @@ -// derived from https://github.com/johanhelsing/bevy_web_asset - mod web_asset_plugin; mod web_asset_source; diff --git a/crates/bevy_remote_asset/src/web_asset_source.rs b/crates/bevy_remote_asset/src/web_asset_source.rs index 8c9fe3c..402e616 100644 --- a/crates/bevy_remote_asset/src/web_asset_source.rs +++ b/crates/bevy_remote_asset/src/web_asset_source.rs @@ -29,6 +29,8 @@ impl WebAssetReader { } async fn get<'a>(path: PathBuf) -> Result>, AssetReaderError> { + println!("get"); + use std::{ future::Future, io, @@ -59,6 +61,7 @@ async fn get<'a>(path: PathBuf) -> Result>, AssetReaderError> { format!("non-utf8 path: {}", path.display()), )) })?; + let mut response = ContinuousPoll(surf::get(str_path)).await.map_err(|err| { AssetReaderError::Io(io::Error::new( io::ErrorKind::Other, @@ -71,6 +74,25 @@ async fn get<'a>(path: PathBuf) -> Result>, AssetReaderError> { )) })?; + if response.status() == StatusCode::Found { + if let Some(uri) = response.header("Location") { + let uri = uri.as_str(); + response = ContinuousPoll(surf::get(uri)).await.map_err(|err| { + AssetReaderError::Io(io::Error::new( + io::ErrorKind::Other, + format!( + "unexpected status code {} while loading {}: {}", + err.status(), + path.display(), + err.into_inner(), + ), + )) + })?; + }; + } + + println!("{}", response.status()); + match response.status() { StatusCode::Ok => Ok(Box::new(VecReader::new( ContinuousPoll(response.body_bytes()) diff --git a/new_media/src/asset.rs b/new_media/src/asset.rs index a388fd5..b43fc96 100644 --- a/new_media/src/asset.rs +++ b/new_media/src/asset.rs @@ -13,37 +13,33 @@ use bevy_panorbit_camera::PanOrbitCamera; use bevy_gaussian_splatting::{GaussianCloud, GaussianSplattingBundle}; -pub fn setup_gaussian_cloud( +pub fn setup_gaussian_cloud(mut commands: Commands, asset_server: Res) { + let cloud = asset_server.load::("https://huggingface.co/datasets/cs50victor/splats/resolve/main/train/point_cloud/iteration_7000/point_cloud.gcloud"); + + log::info!("loading {}", cloud.id()); + + let gs = GaussianSplattingBundle { cloud, ..default() }; + commands.spawn((gs, Name::new("gaussian_cloud"))); +} + +pub fn setup_camera( mut commands: Commands, - asset_server: Res, - mut gaussian_assets: ResMut>, mut scene_controller: ResMut, mut images: ResMut>, export_sources: ResMut>, ) { - // let remote_file = Some("https://huggingface.co/datasets/cs50victor/splats/resolve/main/train/point_cloud/iteration_7000/point_cloud.gcloud"); - // TODO: figure out how to load remote files later - let splat_file = "splats/garden/point_cloud/iteration_7000/point_cloud.gcloud"; - log::info!("loading {}", splat_file); - // let cloud = asset_server.load(splat_file.to_string()); - - let cloud = gaussian_assets.add(GaussianCloud::test_model()); - - let render_target = bevy_headless::setup_render_target( + let target = bevy_headless::setup_render_target( &mut commands, &mut images, &mut scene_controller, export_sources, ); - let gs = GaussianSplattingBundle { cloud, ..default() }; - commands.spawn((gs, Name::new("gaussian_cloud"))); - commands.spawn(( Camera3dBundle { transform: Transform::from_translation(Vec3::new(0.0, 1.5, 5.0)), tonemapping: Tonemapping::None, - camera: Camera { target: render_target, ..default() }, + camera: Camera { target, ..default() }, ..default() }, PanOrbitCamera { diff --git a/new_media/src/main.rs b/new_media/src/main.rs index e73392f..86b4476 100644 --- a/new_media/src/main.rs +++ b/new_media/src/main.rs @@ -2,7 +2,7 @@ mod asset; mod controls; mod server; -use asset::setup_gaussian_cloud; +use asset::{setup_camera, setup_gaussian_cloud}; use bevy::{ app::{App as Engine, ScheduleRunnerPlugin, Startup, Update}, core_pipeline::clear_color::ClearColor, @@ -29,6 +29,8 @@ fn main() { .filter_module("new_media", log::LevelFilter::Info) .filter_module("bevy", log::LevelFilter::Info) .filter_module("bevy_headless", log::LevelFilter::Info) + .filter_module("bevy_remote_asset", log::LevelFilter::Info) + .filter_module("bevy_remote_asset", log::LevelFilter::Debug) .filter_module("bevy_ws_server", log::LevelFilter::Info) .filter_module("bevy_ws_server", log::LevelFilter::Debug) .init(); @@ -39,7 +41,6 @@ fn main() { Engine::new() .insert_resource(bevy_headless::SceneInfo::new(config.width, config.height)) .init_resource::() - // .insert_resource(ClearColor(Color::rgb_u8(255, 255, 255))) .insert_resource(ClearColor(Color::rgb_u8(0, 0, 0))) .add_plugins(( WebAssetPlugin, @@ -49,7 +50,7 @@ fn main() { PanOrbitCameraPlugin, GaussianSplattingPlugin, )) - .add_systems(Startup, (start_ws, setup_gaussian_cloud)) - .add_systems(Update, (receive_message,update_world_from_input)) + .add_systems(Startup, (start_ws, setup_camera, setup_gaussian_cloud)) + .add_systems(Update, (receive_message, update_world_from_input)) .run(); }