Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate OSM viewer with Bevy #1038

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"]
1,883 changes: 1,794 additions & 89 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]

resolver = "2"
members = [
"abstio",
"abstutil",
14 changes: 14 additions & 0 deletions apps/osm_viewer_bevy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "osm_viewer_bevy"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy_pancam = "0.7.0"
map_model = { path = "../../map_model" }
abstutil = { path = "../../abstutil" }
bevy-earcutr = "0.7.0"

[dependencies.bevy]
version = "0.9.1"
default-features = true
57 changes: 57 additions & 0 deletions apps/osm_viewer_bevy/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use abstutil;
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use bevy_earcutr::*;
use bevy_pancam::{PanCam, PanCamPlugin};
use map_model::Map;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
enum AppState {
Loading,
Running,
}

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(PanCamPlugin::default())
.add_startup_system(setup)
.run();
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let mut timer = abstutil::time::Timer::new("load_map");
let map_model = Map::load_synchronously(
"../../data/system/us/seattle/maps/montlake.bin".to_string(),
&mut timer,
);

for road in map_model.all_roads().iter() {
let poly = road.get_thick_polygon();

let mut builder = PolygonMeshBuilder::new();
// Call `add_earcutr_input` or each polygon you want in the mesh.
builder.add_earcutr_input(EarcutrInput {
vertices: poly
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case it's useful later for polygons with holes, there's a way to run earcutr and get vertices + indices already: geom::Tessellation::from(polygon).consume()

.get_outer_ring()
.points()
.iter()
.flat_map(|p| vec![p.x(), p.y()])
.collect::<Vec<f64>>(),
interior_indices: vec![],
});

if let Some(mesh) = builder.build() {
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(Mesh::from(mesh)).into(),
material: materials.add(ColorMaterial::from(Color::PURPLE)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if there's always one material per mesh in Bevy, or is there an easy way to specify material through per-vertex attributes? I could imagine cases where both could be useful

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..default()
});
};
}

commands.spawn((Camera2dBundle::default(), PanCam::default()));
}