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

Add compass rose and work around wasm crash #64

Merged
merged 8 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
**/.DS_Store
/assets
/target
/z_Usefull
/web/*
19 changes: 19 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ directories = "5.0.1"
async-fs = "2.1.0"
bevy_web_asset = { git = "https://github.com/oli-obk/bevy_web_asset.git", branch = "user-agent" }
big_space = "0.4.0"
bevy_embedded_assets = "0.9.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.22", default-features = false, features = [
Expand Down
1 change: 1 addition & 0 deletions assets/8k.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Source: https://www.solarsystemscope.com/textures/
Binary file added assets/8k_earth_clouds.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/8k_earth_daymap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/8k_moon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/8k_stars_milky_way.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/compass.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Source: https://openclipart.org/detail/233063/compass-rose-2
Binary file added assets/compass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 56 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use std::f32::consts::FRAC_PI_2;

use bevy::{
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
pbr::NotShadowCaster,
prelude::*,
};
use bevy_embedded_assets::EmbeddedAssetPlugin;
use bevy_flycam::{FlyCam, MovementSettings};
#[cfg(all(feature = "xr", not(any(target_os = "macos", target_arch = "wasm32"))))]
use bevy_oxr::xr_input::trackers::OpenXRTrackingRoot;
Expand Down Expand Up @@ -86,6 +88,8 @@ pub fn main() {
app.add_plugins(HttpAssetReaderPlugin {
base_url: "gltiles.osm2world.org/glb/".into(),
});
// Offer assets via `embedded://`
app.add_plugins(EmbeddedAssetPlugin::default());
app.add_plugins(bevy_web_asset::WebAssetPlugin {
user_agent: Some("osmeta 0.1.0".into()),
});
Expand Down Expand Up @@ -128,6 +132,7 @@ pub fn main() {
),
)
.add_systems(Update, update_camera_orientations)
.add_systems(PostUpdate, reposition_compass)
.run();
}

Expand Down Expand Up @@ -215,18 +220,12 @@ fn recompute_view_distance(
}

fn get_main_camera_position(
xr_pos: Query<(&Transform, &GalacticGrid), (With<OpenXRTrackingRoot>, Without<FlyCam>)>,
flycam_pos: Query<(&Transform, &GalacticGrid), (With<FlyCam>, Without<OpenXRTrackingRoot>)>,
player: player::Player,
view_distance: Res<ViewDistance>,
space: Res<FloatingOriginSettings>,
) -> (TileIndex, Vec2) {
let (pos, grid) = if let Ok(xr_pos) = xr_pos.get_single() {
xr_pos
} else {
flycam_pos.single()
};
let player = player.pos();

let pos = space.grid_position_double(grid, pos);
let pos = player.pos();
let origin = GeoPos::from_cartesian(pos);
let tile_size = origin.tile_size(TILE_ZOOM);
let radius = view_distance.0 / tile_size + 0.5;
Expand All @@ -235,6 +234,54 @@ fn get_main_camera_position(
(origin.as_tile_index(), radius)
}

#[derive(Component)]
struct Compass;

mod player;

fn reposition_compass(
mut compass: Query<
(&mut Transform, &mut GalacticGrid),
(With<Compass>, Without<FlyCam>, Without<OpenXRTrackingRoot>),
>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
server: Res<AssetServer>,
player: player::Player,
) {
if let Ok((mut pos, mut grid)) = compass.get_single_mut() {
let player = player.pos();
let directions = player.directions();
pos.translation = player.transform.translation - directions.up * 5.;
*grid = player.grid;
pos.look_to(directions.north, directions.up)
} else {
let mesh = shape::Plane::default();
let mesh = meshes.add(mesh.into());
let image = server.load("embedded://compass.png");
let material = materials.add(StandardMaterial {
base_color_texture: Some(image),
unlit: true,
cull_mode: None,
perceptual_roughness: 1.0,
fog_enabled: false,
alpha_mode: AlphaMode::Blend,
..default()
});
commands.spawn((
PbrBundle {
mesh,
material,
..default()
},
GalacticGrid::ZERO,
Compass,
NotShadowCaster,
));
}
}

fn update_camera_orientations(
mut movement_settings: ResMut<MovementSettings>,
fly_cam: Query<(&Transform, &GalacticGrid), With<FlyCam>>,
Expand Down
64 changes: 64 additions & 0 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::Compass;
use super::GalacticGrid;
use super::OpenXRTrackingRoot;
use bevy::ecs::system::SystemParam;
use bevy::prelude::*;
use bevy_flycam::FlyCam;
use big_space::FloatingOriginSettings;
use glam::DVec3;

#[derive(SystemParam)]
pub struct Player<'w, 's> {
pub(crate) xr_pos: Query<
'w,
's,
(&'static Transform, &'static GalacticGrid),
(With<OpenXRTrackingRoot>, Without<FlyCam>, Without<Compass>),
>,
pub(crate) flycam_pos: Query<
'w,
's,
(&'static Transform, &'static GalacticGrid),
(With<FlyCam>, Without<OpenXRTrackingRoot>, Without<Compass>),
>,
pub(crate) space: Res<'w, FloatingOriginSettings>,
}

pub struct PlayerPosition<'a> {
pub transform: Transform,
pub grid: GalacticGrid,
pub space: &'a FloatingOriginSettings,
}

impl PlayerPosition<'_> {
pub fn pos(&self) -> DVec3 {
self.space.grid_position_double(&self.grid, &self.transform)
}
pub fn directions(&self) -> Directions {
let up = self.pos().normalize().as_vec3();
let west = Vec3::Z.cross(up);
let north = up.cross(west);
Directions { up, north, west }
}
}

pub struct Directions {
pub up: Vec3,
pub north: Vec3,
pub west: Vec3,
}

impl<'w, 's> Player<'w, 's> {
pub fn pos(&self) -> PlayerPosition<'_> {
let (&transform, &grid) = if let Ok(xr_pos) = self.xr_pos.get_single() {
xr_pos
} else {
self.flycam_pos.single()
};
PlayerPosition {
transform,
grid,
space: &self.space,
}
}
}
61 changes: 35 additions & 26 deletions src/sun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn setup(
);

// Stars
let image = server.load("https://www.solarsystemscope.com/textures/download/2k_stars.jpg");
let image = server.load("embedded://8k_stars.jpg");
commands.spawn((
PbrBundle {
mesh: mesh.clone(),
Expand All @@ -111,8 +111,7 @@ fn setup(
));

// Clouds visible from earth and space
let image =
server.load("https://www.solarsystemscope.com/textures/download/2k_earth_clouds.jpg");
let image = server.load("embedded://8k_earth_clouds.jpg");
commands.spawn((
PbrBundle {
mesh: mesh.clone(),
Expand Down Expand Up @@ -150,8 +149,7 @@ fn setup(
GalacticGrid::ZERO,
));

let image =
server.load("https://www.solarsystemscope.com/textures/download/2k_earth_daymap.jpg");
let image = server.load("embedded://8k_earth_daymap.jpg");

// ground
commands.spawn((
Expand All @@ -174,7 +172,7 @@ fn setup(
NeedsTextureSetToRepeat(image),
));

let image = server.load("https://www.solarsystemscope.com/textures/download/2k_moon.jpg");
let image = server.load("embedded://8k_moon.jpg");

// moon
commands.spawn((
Expand Down Expand Up @@ -206,23 +204,29 @@ fn set_texture_transparent(
textures: Query<(Entity, &NeedsTextureTransparencyEqualToRed)>,
) {
for (entity, NeedsTextureTransparencyEqualToRed(handle)) in textures.iter() {
info!("making clouds transparent");
use bevy::asset::LoadState::*;
if let Loaded | Failed = server.get_load_state(handle).unwrap() {
let Some(image) = images.get_mut(handle) else {
unreachable!()
};
info!("clouds made transparent");
*image = image.convert(TextureFormat::Rgba8UnormSrgb).unwrap();
for chunk in image.data.chunks_exact_mut(4) {
let [r, _g, _b, a] = chunk else {
match server.get_load_state(handle).unwrap() {
Loaded => {
let Some(image) = images.get_mut(handle) else {
unreachable!()
};
*a = *r;
*image = image.convert(TextureFormat::Rgba8UnormSrgb).unwrap();
for chunk in image.data.chunks_exact_mut(4) {
let [r, _g, _b, a] = chunk else {
unreachable!()
};
*a = *r;
}
commands
.entity(entity)
.remove::<NeedsTextureTransparencyEqualToRed>();
}
commands
.entity(entity)
.remove::<NeedsTextureTransparencyEqualToRed>();
Failed => {
commands
.entity(entity)
.remove::<NeedsTextureTransparencyEqualToRed>();
}
_ => (),
}
}
}
Expand All @@ -235,13 +239,18 @@ fn set_texture_repeat(
) {
for (entity, NeedsTextureSetToRepeat(handle)) in textures.iter() {
use bevy::asset::LoadState::*;
if let Loaded | Failed = server.get_load_state(handle).unwrap() {
let Some(image) = images.get_mut(handle) else {
unreachable!()
};
info!("texture set to repeat");
image.sampler = repeat_sampler();
commands.entity(entity).remove::<NeedsTextureSetToRepeat>();
match server.get_load_state(handle).unwrap() {
Loaded => {
let Some(image) = images.get_mut(handle) else {
unreachable!()
};
image.sampler = repeat_sampler();
commands.entity(entity).remove::<NeedsTextureSetToRepeat>();
}
Failed => {
commands.entity(entity).remove::<NeedsTextureSetToRepeat>();
}
_ => (),
}
}
}
Expand Down