Skip to content

Commit

Permalink
Load flat tiles if no glb is available
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 1, 2023
1 parent 1a82e59 commit b3b52c6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 = [
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
56 changes: 41 additions & 15 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ impl TileMap {
mut commands: Commands,
server: Res<AssetServer>,
scenes: ResMut<Assets<Gltf>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut tilemap: Query<(Entity, &mut Self)>,
) {
for (id, mut tilemap) in &mut tilemap {
Expand All @@ -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

Expand All @@ -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<Image> = 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");
}
}
}
}
Expand Down

0 comments on commit b3b52c6

Please sign in to comment.