Skip to content

Commit

Permalink
Merge pull request #2 from oli-obk/main
Browse files Browse the repository at this point in the history
Some initial work
  • Loading branch information
oli-obk authored Nov 18, 2023
2 parents a06e5e2 + 2659c78 commit 1bd110d
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 71 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

env:
CARGO_TERM_COLOR: always

jobs:
# Run cargo test
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
name: Test `cargo test` on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 30
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ matrix.os }}-cargo-test-${{ hashFiles('**/Cargo.toml') }}
- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
- name: Install Dependencies
if: matrix.os == 'ubuntu-latest'
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libopenxr-loader1 libopenxr-dev
- name: Run cargo test
run: cargo test

# Run cargo clippy -- -D warnings
clippy:
name: Clippy
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.toml') }}
- name: Add clippy to rustup
run: rustup component add clippy
- name: Install Dependencies
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev
- name: Run clippy
run: cargo clippy -- -D warnings

# Run cargo fmt --all -- --check
format:
name: Format
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Add rustfmt to rustup
run: rustup component add rustfmt
- name: Run cargo fmt
run: cargo fmt --all -- --check
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

**/.DS_Store
assets/**
/assets
Cargo.lock
target/**
/target
20 changes: 12 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
[package]
name = "osmeta" # OSMeta ??? `#[warn(non_snake_case)]` on by default
name = "osmeta" # OSMeta ??? `#[warn(non_snake_case)]` on by default
version = "0.1.0"
edition = "2021"


# Enable a small amount of optimization in debug mode
#[profile.dev]
#opt-level = 1
[profile.dev]
opt-level = 1

# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
#[profile.dev.package."*"]
#opt-level = 3
[profile.dev.package."*"]
opt-level = 3


[dependencies]
bevy = { version = "0.12", features = ["jpeg"] } ## NOT the local file bevy, the one from the net
bevy_flycam = { git = "https://github.com/sburris0/bevy_flycam" } ## control
bevy = { version = "0.12", features = [
"jpeg",
] } ## NOT the local file bevy, the one from the net
bevy_flycam = { git = "https://github.com/sburris0/bevy_flycam" } ## control
bevy_oxr = { git = "https://github.com/awtterpip/bevy_openxr" }
## bevy_config_cam = { git = "https://github.com/BlackPhlox/bevy_config_cam" } ## { version = "0.3.0"}
glam = "0"


#[features]
[features]
#xr = ["bevy_oxr"]
#jpeg = ["bevy_internal/jpeg"]


Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.74.0
97 changes: 37 additions & 60 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
//! Loads and renders a glTF file as a scene.
use bevy::{
pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap},
prelude::*,
};
use std::f32::consts::*;
use tilemap::TileMap;
use xr::XRPlugin;

use bevy_flycam::prelude::*;
use bevy_oxr::DefaultXrPlugins;

//mod geopos;
//use geopos::*;


mod tilemap;
mod xr;

fn main() {

App::new()
.insert_resource(DirectionalLightShadowMap { size: 4096 })
let mut app = App::new();
if std::env::args().any(|arg| arg == "xr") {
app.add_plugins(DefaultXrPlugins).add_plugins(XRPlugin);
} else {
app.add_plugins(DefaultPlugins);
}
app.insert_resource(DirectionalLightShadowMap { size: 4096 })
.insert_resource(Msaa::Sample4) // Msaa::Sample4 Msaa::default() -- Todo: tut nichts?
.add_plugins(DefaultPlugins)

.add_plugins(bevy::diagnostic::LogDiagnosticsPlugin::default())
.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin::default())
.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
.add_systems(Startup, setup)
.add_systems(Update, animate_light_direction)
//.add_systems(Update, _animate_camera_position)

.add_systems(
Update,
(
animate_light_direction,
update_active_tile_zone,
tilemap::update,
),
)
.add_plugins(NoCameraPlayerPlugin) // https://github.com/sburris0/bevy_flycam (bevy_config_cam dies not work wiht Bevy 12)

.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut meshes: ResMut<Assets<Mesh>>) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(3., 100., 400.)
Expand All @@ -56,48 +64,31 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// only using a single cascade.
cascade_shadow_config: CascadeShadowConfigBuilder {
num_cascades: 1,
//maximum_distance: 1.6,
//maximum_distance: 1.6,
..default()
}
.into(),
..default()
});

add_tile(&mut commands, &asset_server,17429, 11369);
add_tile(&mut commands, &asset_server,17429, 11370);
add_tile(&mut commands, &asset_server,17429, 11371);

add_tile(&mut commands, &asset_server,17430, 11369);
add_tile(&mut commands, &asset_server,17430, 11370);
add_tile(&mut commands, &asset_server,17430, 11371);

add_tile(&mut commands, &asset_server,17431, 11369);
add_tile(&mut commands, &asset_server,17431, 11370);
add_tile(&mut commands, &asset_server,17431, 11371);

commands.spawn(TileMap::new(&mut meshes));
}

fn update_active_tile_zone(mut commands: Commands, mut tilemap: Query<&mut TileMap>) {
let mut tilemap = tilemap.single_mut();
tilemap.load(&mut commands, 17429, 11369);
tilemap.load(&mut commands, 17429, 11370);
tilemap.load(&mut commands, 17429, 11371);

fn add_tile(commands: &mut Commands, asset_server: &Res<AssetServer>,x: u32, y: u32) {
// https://gltiles.osm2world.org/glb/lod1/15/17388/11332.glb#Scene0"

// Just for testing:
const TILE_SIZE: f32 = 814.5;
const X0: u32 = 17430;
const Y0: u32 = 11370;

let name: String = format!( "models/{}_{}.glb#Scene0",x,y); //format!("hello {}", "world!");
commands.spawn(SceneBundle {
scene: asset_server.load(name), // "models/17430_11371.glb#Scene0"
transform: Transform::from_xyz(
(x-X0) as f32 * TILE_SIZE, 0.,
(y-Y0) as f32 * TILE_SIZE), // OSM y => GPU z
..default()
});
tilemap.load(&mut commands, 17430, 11369);
tilemap.load(&mut commands, 17430, 11370);
tilemap.load(&mut commands, 17430, 11371);

tilemap.load(&mut commands, 17431, 11369);
tilemap.load(&mut commands, 17431, 11370);
tilemap.load(&mut commands, 17431, 11371);
}


fn animate_light_direction(
time: Res<Time>,
mut query: Query<&mut Transform, With<DirectionalLight>>,
Expand All @@ -111,17 +102,3 @@ fn animate_light_direction(
);
}
}

fn _animate_camera_position(
time: Res<Time>,
mut query: Query<&mut Transform, With<FlyCam>>,
) {
for mut transform in &mut query {
transform.rotation = Quat::from_euler(
EulerRot::ZYX,
0.0, // Looping
time.elapsed_seconds() * PI / 10.0, // Komnpas
-35. / 180. * PI, // Auf-Ab
);
}
}
105 changes: 105 additions & 0 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::collections::{BTreeMap, VecDeque};

use bevy::prelude::*;

#[derive(Component, Default)]
pub struct TileMap {
tiles: BTreeMap<i32, BTreeMap<i32, Entity>>,
to_load: VecDeque<(i32, i32)>,
loading: Option<(i32, i32, Handle<Scene>)>,
dummy: Handle<Mesh>,
}

#[derive(Component)]
pub struct Tile;

impl TileMap {
pub fn load(&mut self, commands: &mut Commands, x: i32, y: i32) {
self.tiles
.entry(x)
.or_default()
.entry(y)
.or_insert_with(|| {
self.to_load.push_front((x, y));
let transform = test_transform(x, y);

commands
.spawn(PbrBundle {
mesh: self.dummy.clone(),
transform,
..default()
})
.id()
});
}
}

pub fn update(mut commands: Commands, server: Res<AssetServer>, mut tilemap: Query<&mut TileMap>) {
for mut tilemap in &mut tilemap {
if let Some((x, y, scene)) = tilemap.loading.take() {
use bevy::asset::LoadState::*;
match server.get_load_state(&scene).unwrap() {
NotLoaded | Loading => {
tilemap.loading = Some((x, y, scene));
return;
}
Loaded => {
let entity = tilemap.tiles.entry(x).or_default().get_mut(&y).unwrap();

let transform = test_transform(x, y);
let tile = commands
.spawn((
SceneBundle {
scene, // "models/17430_11371.glb#Scene0"
transform,
..default()
},
Tile,
))
.id();
let dummy = std::mem::replace(entity, tile);
commands.entity(dummy).despawn();
}
Failed => todo!(),
}
}

assert!(tilemap.loading.is_none());
let Some((x, y)) = tilemap.to_load.pop_back() else {
return;
};

// https://gltiles.osm2world.org/glb/lod1/15/17388/11332.glb#Scene0"
let name: String = format!("models/{}_{}.glb#Scene0", x, y);
tilemap.loading = Some((x, y, server.load(name))); // "models/17430_11371.glb#Scene0"
}
}

impl TileMap {
pub fn new(meshes: &mut Assets<Mesh>) -> Self {
Self {
dummy: meshes.add(
shape::Box {
min_x: 0.0,
max_x: 814.5,
min_y: 0.0,
max_y: 1.0,
min_z: 0.0,
max_z: 814.5,
}
.into(),
),
..default()
}
}
}

fn test_transform(x: i32, y: i32) -> Transform {
// Just for testing:
const TILE_SIZE: f32 = 814.5;
const X0: i32 = 17430;
const Y0: i32 = 11370;

// OSM y => GPU z
Transform::from_xyz((x - X0) as f32 * TILE_SIZE, 0., (y - Y0) as f32 * TILE_SIZE)
}
Loading

0 comments on commit 1bd110d

Please sign in to comment.