Skip to content

Commit

Permalink
Merge pull request #35 from oli-obk/sphere
Browse files Browse the repository at this point in the history
 Place tiles on a globe
  • Loading branch information
Karl-Josef Adler authored Nov 28, 2023
2 parents 0a8a99a + 5540e8c commit afc2aca
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 74 deletions.
97 changes: 96 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ opt-level = 3

[dependencies]
bevy = { version = "0.12", features = ["jpeg"] }
bevy_flycam = { git = "https://github.com/sburris0/bevy_flycam" }
bevy_flycam = { git = "https://github.com/oli-obk/bevy_flycam", branch = "arbitrary_up" }
flate2 = "1.0.28"
futures-core = "0.3.29"
futures-io = "0.3.29"
glam = "0"
bevy_screen_diagnostics = { git = "https://github.com/oli-obk/bevy_screen_diagnostics.git" }
globe-rs = "0.1.8"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
surf = { version = "2.3.2", default-features = false, features = [
Expand Down
5 changes: 4 additions & 1 deletion src/flycam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
mut movement_settings: ResMut<MovementSettings>,
mut keys: ResMut<KeyBindings>,
pos: Res<crate::StartingPosition>,
) {
let pos = pos.0.normalize();
let transform =
Transform::from_xyz(0., 100., -300.).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y);
Transform::from_translation(pos * 300.0).looking_at(Vec3::new(0.0, 0.3, 0.0), pos);
movement_settings.up = pos;

let material = materials.add(StandardMaterial {
base_color_texture: Some(images.add(uv_debug_texture())),
Expand Down
24 changes: 23 additions & 1 deletion src/geopos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use globe_rs::{CartesianPoint, GeographicPoint};
use std::f32::consts::PI;

use crate::tilemap::TileCoord;
Expand Down Expand Up @@ -30,7 +31,6 @@ impl GeoPos {
pub fn to_tile_coordinates(self, zoom: u8) -> TileCoord {
let pow_zoom = 2_u32.pow(zoom.into()) as f32;

// return
TileCoord(Vec2 {
// Longitude, (Längengrad) West/East "index"
x: ((self.lon + 180.) / 360. * pow_zoom),
Expand All @@ -44,4 +44,26 @@ impl GeoPos {
// to compensate the stretching if the stretching of the West/East projection
})
}

pub fn to_cartesian(self) -> Vec3 {
let geo = GeographicPoint::new(
self.lon as f64 / 180.0 * std::f64::consts::PI,
self.lat as f64 / 180.0 * std::f64::consts::PI,
EARTH_RADIUS as f64,
);
let cart = CartesianPoint::from_geographic(&geo);
Vec3::new(cart.x() as f32, cart.y() as f32, cart.z() as f32)
}

pub fn from_cartesian(pos: Vec3) -> Self {
let pos = pos.as_dvec3();
let cart = CartesianPoint::new(pos.x, pos.y, pos.z);
let geo = GeographicPoint::from_cartesian(&cart);
GeoPos {
lat: geo.latitude() as f32 / PI * 180.0,
lon: geo.longitude() as f32 / PI * 180.0,
}
}
}

pub const EARTH_RADIUS: f32 = 6378000.;
102 changes: 52 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use bevy_screen_diagnostics::{
use geopos::GeoPos;
use http_assets::HttpAssetReaderPlugin;
use sun::Sky;
use tilemap::TileCoord;

type TileMap = tilemap::TileMap<8145>;
use tilemap::TileMap;

mod flycam;
mod geopos;
Expand All @@ -26,45 +24,11 @@ mod tilemap;
#[cfg(all(feature = "xr", not(any(target_os = "macos", target_arch = "wasm32"))))]
mod xr;

#[derive(Resource)]
struct StartingPosition(Vec3);

#[bevy_main]
pub fn main() {
let mut app = App::new();
app.add_plugins(HttpAssetReaderPlugin {
base_url: "https://gltiles.osm2world.org/glb/".into(),
});
if std::env::args().any(|arg| arg == "xr") {
#[cfg(all(feature = "xr", not(any(target_os = "macos", target_arch = "wasm32"))))]
app.add_plugins(xr::Plugin);
} else {
app.add_plugins(DefaultPlugins);
}
app.insert_resource(Msaa::Sample4) // Msaa::Sample4 Msaa::default() -- Todo: tut nichts?
.add_plugins(ScreenDiagnosticsPlugin {
timestep: 1.0,
..default()
})
.add_plugins(ScreenFrameDiagnosticsPlugin)
.add_plugins(ScreenEntityDiagnosticsPlugin)
.add_plugins(sun::Plugin)
.add_plugins(flycam::Plugin)
.add_systems(Startup, setup)
.add_systems(Update, (load_next_tile, TileMap::update))
.run();
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut diags: ResMut<ScreenDiagnostics>,
) {
diags.modify("fps").aggregate(Aggregate::Average);

// Just for testing:
#[allow(unused_mut)]
let mut x: f32 = 17437.0;
#[allow(unused_mut)]
let mut y: f32 = 11371.0;

let mut args: Vec<String> = vec![];

#[cfg(target_arch = "wasm32")]
Expand All @@ -84,29 +48,59 @@ fn setup(
args.extend(std::env::args().skip(1));
}

let mut lat = None;
let mut lon = None;
let mut pos = GeoPos {
lat: 48.14077,
lon: 11.55741,
};

for arg in args {
let (k, v) = arg
.split_once('=')
.expect("arguments must be `key=value` pairs");
match k {
"lat" => lat = Some(v.parse().unwrap()),
"lon" => lon = Some(v.parse().unwrap()),
"lat" => pos.lat = v.parse().unwrap(),
"lon" => pos.lon = v.parse().unwrap(),
other => panic!("unknown key `{other}`"),
}
}

if let Some((lat, lon)) = lat.zip(lon) {
let pos = GeoPos { lat, lon };
TileCoord(Vec2 { x, y }) = pos.to_tile_coordinates(15);
let mut app = App::new();
app.insert_resource(StartingPosition(pos.to_cartesian()));
app.add_plugins(HttpAssetReaderPlugin {
base_url: "https://gltiles.osm2world.org/glb/".into(),
});
if std::env::args().any(|arg| arg == "xr") {
#[cfg(all(feature = "xr", not(any(target_os = "macos", target_arch = "wasm32"))))]
app.add_plugins(xr::Plugin);
} else {
app.add_plugins(DefaultPlugins);
}
app.insert_resource(Msaa::Sample4) // Msaa::Sample4 Msaa::default() -- Todo: tut nichts?
.add_plugins(ScreenDiagnosticsPlugin {
timestep: 1.0,
..default()
})
.add_plugins(ScreenFrameDiagnosticsPlugin)
.add_plugins(ScreenEntityDiagnosticsPlugin)
.add_plugins(sun::Plugin)
.add_plugins(flycam::Plugin)
.add_systems(Startup, setup)
.add_systems(Update, (load_next_tile, TileMap::update))
.run();
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut diags: ResMut<ScreenDiagnostics>,
pos: Res<StartingPosition>,
) {
diags.modify("fps").aggregate(Aggregate::Average);

commands.spawn((
TileMap::new(&mut meshes),
SpatialBundle {
transform: Transform::from_xyz(-x * TileMap::TILE_SIZE, 0., -y * TileMap::TILE_SIZE),
transform: Transform::from_translation(-pos.0),
..default()
},
));
Expand Down Expand Up @@ -181,13 +175,21 @@ fn load_next_tile(
}
}

let origin = GeoPos::from_cartesian(pos - transform.translation).to_tile_coordinates(15);
let (x, y) = pos.any_orthonormal_pair();
let radius =
GeoPos::from_cartesian(pos - transform.translation + x * sky.scale.x + y * sky.scale.x)
.to_tile_coordinates(15)
.0
- origin.0;

tilemap.load_next(
id,
&mut commands,
&server,
// FIXME: Maybe use https://crates.io/crates/big_space in order to be able to remove
// the translation from the tilemap and instead just use its real coordinates.
pos - transform.translation,
sky.scale.x,
origin,
radius,
);
}
2 changes: 1 addition & 1 deletion src/sun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn animate_light_direction(
for mut transform in &mut query {
transform.rotation = Quat::from_euler(
EulerRot::ZYX,
0.0,
time.elapsed_seconds() * PI / 100.0,
0.0,
-FRAC_PI_4,
);
}
Expand Down
Loading

0 comments on commit afc2aca

Please sign in to comment.