From 0fe239a4cf136ac9d5bc713e6e0e8d5c5ef92eeb Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 1 Dec 2023 01:56:28 +0100 Subject: [PATCH] Load flat tiles if no glb is available --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 3 +++ src/tilemap.rs | 56 ++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3de6c0b..cdeede6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1282,7 +1282,7 @@ dependencies = [ [[package]] name = "bevy_web_asset" version = "0.7.1" -source = "git+https://github.com/oli-obk/bevy_web_asset.git?branch=android#dfe8f5b6e7bf1dbbde2c1a8a577f5a381c6a6e77" +source = "git+https://github.com/oli-obk/bevy_web_asset.git?branch=user-agent#bf804cc5a87b7d0a7aa90810d77c4b2279034390" dependencies = [ "bevy", "js-sys", diff --git a/Cargo.toml b/Cargo.toml index eecf4a2..d71622e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ bevy_screen_diagnostics = { git = "https://github.com/oli-obk/bevy_screen_diagno globe-rs = "0.1.8" directories = "5.0.1" async-fs = "2.1.0" -bevy_web_asset = { git = "https://github.com/oli-obk/bevy_web_asset.git", branch = "android" } +bevy_web_asset = { git = "https://github.com/oli-obk/bevy_web_asset.git", branch = "user-agent" } [target.'cfg(target_arch = "wasm32")'.dependencies] web-sys = { version = "0.3.22", default-features = false, features = [ diff --git a/src/lib.rs b/src/lib.rs index 78a93e7..e57e457 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,6 +72,9 @@ pub fn main() { app.add_plugins(HttpAssetReaderPlugin { base_url: "gltiles.osm2world.org/glb/".into(), }); + app.add_plugins(bevy_web_asset::WebAssetPlugin { + user_agent: Some("osmeta 0.1.0".into()), + }); if xr { #[cfg(all(feature = "xr", not(any(target_os = "macos", target_arch = "wasm32"))))] app.add_plugins(xr::Plugin); diff --git a/src/tilemap.rs b/src/tilemap.rs index cff0b03..26a65c9 100644 --- a/src/tilemap.rs +++ b/src/tilemap.rs @@ -119,6 +119,8 @@ impl TileMap { mut commands: Commands, server: Res, scenes: ResMut>, + mut meshes: ResMut>, + mut materials: ResMut>, mut tilemap: Query<(Entity, &mut Self)>, ) { for (id, mut tilemap) in &mut tilemap { @@ -129,7 +131,7 @@ impl TileMap { NotLoaded | Loading => { tilemap.loading = Some((pos, scene)); } - Loaded => { + state @ (Loaded | Failed) => { // FIXME: implement caching of downloaded assets by implementing something like // https://github.com/bevyengine/bevy/blob/main/examples/asset/processing/asset_processing.rs @@ -139,27 +141,51 @@ impl TileMap { continue; }; - let transform = Self::test_transform(pos); - let scene = scenes.get(scene).unwrap().scenes[0].clone(); - let tile = commands - .spawn(( - SceneBundle { - scene, // "models/17430_11371.glb#Scene0" - transform, + let tile = match state { + NotLoaded | Loading => unreachable!(), + Loaded => { + let transform = Self::test_transform(pos); + let scene = scenes.get(scene).unwrap().scenes[0].clone(); + commands + .spawn(( + SceneBundle { + scene, // "models/17430_11371.glb#Scene0" + transform, + ..default() + }, + Tile, + )) + .id() + } + Failed => { + warn!("failed to load tile {pos} from network, switching to flat tile"); + + let (coord, mesh) = flat_tile(pos); + let mesh = meshes.add(mesh); + let image: Handle = server.load(format!( + "https://a.tile.openstreetmap.org/{TILE_ZOOM}/{}/{}.png", + coord.0.x, coord.0.y + )); + let material = materials.add(StandardMaterial { + base_color_texture: Some(image), + perceptual_roughness: 1.0, ..default() - }, - Tile, - )) - .id(); + }); + commands + .spawn(PbrBundle { + mesh, + material, + ..default() + }) + .id() + } + }; commands.entity(id).add_child(tile); let dummy = std::mem::replace(entity, tile); if let Some(mut entity) = commands.get_entity(dummy) { entity.despawn(); } } - Failed => { - error!("failed to load tile {pos} from network"); - } } } }