Worked on server using matchbox
This commit is contained in:
parent
02c9ef06f5
commit
7df4e86afb
3 changed files with 2128 additions and 279 deletions
|
|
@ -9,6 +9,6 @@ rand = "0.9.1"
|
|||
# rusqlite = "0.37.0"
|
||||
statrs = "0.18.0"
|
||||
orthros_network = {path = "../orthros-network"}
|
||||
|
||||
bevy_matchbox = "0.13"
|
||||
[[bin]]
|
||||
name = "orthros-server"
|
||||
|
|
|
|||
|
|
@ -1,246 +1,45 @@
|
|||
pub mod temporal_management;
|
||||
use bevy::{
|
||||
asset::RenderAssetUsages,
|
||||
camera::RenderTarget,
|
||||
color::palettes::css::*,
|
||||
core_pipeline::tonemapping::Tonemapping,
|
||||
math::ops,
|
||||
pbr::wireframe::{Wireframe, WireframeMaterial, WireframePlugin},
|
||||
post_process::bloom::{Bloom, BloomCompositeMode},
|
||||
prelude::*,
|
||||
render::{render_resource::*, view::Hdr},
|
||||
render::{RenderPlugin, settings::WgpuSettings, view::Hdr},
|
||||
time::common_conditions::on_timer,
|
||||
};
|
||||
use bevy_matchbox::prelude::*;
|
||||
use orthros_network::starchart::star::{STARS, StarClass, StarInfo, star_diameter_range};
|
||||
use rand::{
|
||||
Rng,
|
||||
distr::{Distribution, Uniform},
|
||||
rng,
|
||||
};
|
||||
use std::{
|
||||
collections::hash_map::DefaultHasher,
|
||||
f32::consts::PI,
|
||||
hash::{Hash, Hasher},
|
||||
};
|
||||
// use temporal_management::database::{ObjectState, TemporalVector};
|
||||
// const SHADER_ASSET_PATH: &str = "shaders/g-type-star.wgsl";
|
||||
|
||||
// #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
||||
// struct WireframeMaterial {}
|
||||
|
||||
// impl Material for WireframeMaterial {
|
||||
// fn fragment_shader() -> ShaderRef {
|
||||
// SHADER_ASSET_PATH.into()
|
||||
// }
|
||||
// }
|
||||
|
||||
use std::time::Duration;
|
||||
const CHANNEL_ID: usize = 0;
|
||||
#[derive(Component)]
|
||||
struct StarInfoComp(StarInfo);
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins((DefaultPlugins, WireframePlugin::default()))
|
||||
.add_systems(Startup, setup_scene)
|
||||
// .add_systems(Update, (update_bloom_settings))
|
||||
.add_plugins((DefaultPlugins.set(RenderPlugin {
|
||||
render_creation: WgpuSettings {
|
||||
backends: None,
|
||||
..default()
|
||||
}
|
||||
.into(),
|
||||
..default()
|
||||
}),))
|
||||
.add_systems(Startup, (setup_scene, start_socket))
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
receive_messages,
|
||||
send_message.run_if(on_timer(Duration::from_secs(5))),
|
||||
),
|
||||
)
|
||||
.run();
|
||||
}
|
||||
fn create_star_system_annotation(
|
||||
commands: &mut Commands,
|
||||
asset_server: &Res<AssetServer>,
|
||||
images: &mut ResMut<Assets<Image>>,
|
||||
meshes: &mut ResMut<Assets<Mesh>>,
|
||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||
star_info: &StarInfo,
|
||||
) -> (Handle<Mesh>, Handle<StandardMaterial>) {
|
||||
let size = Extent3d {
|
||||
width: 1024,
|
||||
height: 1024,
|
||||
..default()
|
||||
};
|
||||
|
||||
// This is the texture that will be rendered to.
|
||||
let mut image = Image::new_fill(
|
||||
size,
|
||||
TextureDimension::D2,
|
||||
&[0, 0, 0, 0],
|
||||
TextureFormat::Bgra8UnormSrgb,
|
||||
RenderAssetUsages::default(),
|
||||
);
|
||||
// You need to set these texture usage flags in order to use the image as a render target
|
||||
image.texture_descriptor.usage =
|
||||
TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
|
||||
|
||||
let image_handle = images.add(image);
|
||||
|
||||
// Light
|
||||
|
||||
let texture_camera = commands
|
||||
.spawn((
|
||||
Camera2d,
|
||||
Camera {
|
||||
target: RenderTarget::Image(image_handle.clone().into()),
|
||||
clear_color: ClearColorConfig::None,
|
||||
..default()
|
||||
},
|
||||
))
|
||||
.id();
|
||||
let border = UiRect::all(Val::Px(40.));
|
||||
let non_zero = |x, y| x != Val::Px(0.) && y != Val::Px(0.);
|
||||
let border_size = |x, y| if non_zero(x, y) { f32::MAX } else { 0. };
|
||||
commands
|
||||
.spawn((
|
||||
Node {
|
||||
// Cover the whole image
|
||||
width: Val::Percent(100.),
|
||||
height: Val::Percent(100.),
|
||||
flex_direction: FlexDirection::Column,
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
..default()
|
||||
},
|
||||
BackgroundColor(Color::Srgba(Srgba {
|
||||
red: 0.0,
|
||||
green: 0.0,
|
||||
blue: 0.0,
|
||||
alpha: 0.0,
|
||||
})),
|
||||
Text::new(format!("{}", star_info.name)),
|
||||
TextFont {
|
||||
font: asset_server.load("fonts/quantum/quantflt.ttf"),
|
||||
// This font is loaded and will be used instead of the default font.
|
||||
font_size: 60.0,
|
||||
..default()
|
||||
},
|
||||
TextShadow::default(),
|
||||
// Set the justification of the Text
|
||||
TextLayout::new_with_justify(Justify::Center),
|
||||
UiTargetCamera(texture_camera),
|
||||
))
|
||||
.with_children(|parent| {
|
||||
// parent.spawn((
|
||||
// // Accepts a `String` or any type that converts into a `String`, such as `&str`
|
||||
// Text::new("hello\nbevy!"),
|
||||
// TextFont {
|
||||
// // This font is loaded and will be used instead of the default font.
|
||||
// font_size: 60.0,
|
||||
// ..default()
|
||||
// },
|
||||
// TextShadow::default(),
|
||||
// // Set the justification of the Text
|
||||
// TextLayout::new_with_justify(JustifyText::Center),
|
||||
// // Set the style of the Node itself.
|
||||
// Node {
|
||||
// // position_type: PositionType::Relative,
|
||||
// align_self: AlignSelf::FlexStart,
|
||||
|
||||
// // top: Val::Px(5.0),
|
||||
// // right: Val::Px(5.0),
|
||||
// ..default()
|
||||
// },
|
||||
// ));
|
||||
parent.spawn((
|
||||
Node {
|
||||
width: Val::Px(400.),
|
||||
height: Val::Px(400.),
|
||||
border,
|
||||
margin: UiRect::AUTO,
|
||||
align_self: AlignSelf::Center,
|
||||
// align_items: AlignItems::Center,
|
||||
// justify_content: JustifyContent::Center,
|
||||
// align_content: AlignContent::Center,
|
||||
..default()
|
||||
},
|
||||
BackgroundColor(Color::Srgba(Srgba {
|
||||
red: 0.0,
|
||||
green: 0.0,
|
||||
blue: 0.0,
|
||||
alpha: 0.0,
|
||||
})),
|
||||
BorderColor::all(RED),
|
||||
BorderRadius::px(
|
||||
border_size(border.left, border.top),
|
||||
border_size(border.right, border.top),
|
||||
border_size(border.right, border.bottom),
|
||||
border_size(border.left, border.bottom),
|
||||
),
|
||||
// Outline {
|
||||
// width: Val::Px(6.),
|
||||
// offset: Val::Px(6.),
|
||||
// color: Color::WHITE,
|
||||
// },
|
||||
));
|
||||
});
|
||||
let material_handle = materials.add(StandardMaterial {
|
||||
// base_color: Color::srgba(1.0, 1.0, 1.0, 0.0),
|
||||
base_color_texture: Some(image_handle),
|
||||
unlit: true,
|
||||
alpha_mode: AlphaMode::Blend,
|
||||
|
||||
..default()
|
||||
});
|
||||
let aspect = 1.0;
|
||||
|
||||
// create a new quad mesh. this is what we will apply the texture to
|
||||
let quad_width = 10.0;
|
||||
let quad_handle = meshes.add(Rectangle::new(quad_width, quad_width * aspect));
|
||||
return (quad_handle, material_handle);
|
||||
}
|
||||
fn setup_scene(
|
||||
mut commands: Commands,
|
||||
mut asset_server: Res<AssetServer>,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut custom_materials: ResMut<Assets<WireframeMaterial>>,
|
||||
mut images: ResMut<Assets<Image>>,
|
||||
mut time: ResMut<Time<Virtual>>,
|
||||
) {
|
||||
commands.spawn((
|
||||
Camera3d::default(),
|
||||
Camera {
|
||||
clear_color: ClearColorConfig::Custom(Color::from(BLACK)),
|
||||
..default()
|
||||
},
|
||||
Hdr,
|
||||
Tonemapping::TonyMcMapface, // 2. Using a tonemapper that desaturates to white is recommended
|
||||
Transform::from_xyz(0.0, 100.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
Bloom::OLD_SCHOOL, // 3. Enable bloom for the camera
|
||||
));
|
||||
|
||||
let material_emissive1 = materials.add(StandardMaterial {
|
||||
base_color: Color::srgba(0.0, 0.0, 1.0, 1.0),
|
||||
emissive: LinearRgba::rgb(0.0, 0.0, 2000.0), // 4. Put something bright in a dark environment to see the effect
|
||||
..default()
|
||||
});
|
||||
let material_emissive2 = materials.add(StandardMaterial {
|
||||
emissive: LinearRgba::rgb(1000.0, 1000.0, 1000.0),
|
||||
..default()
|
||||
});
|
||||
let material_emissive3 = materials.add(StandardMaterial {
|
||||
base_color: Color::srgba(1.0, 0.0, 0.0, 1.0),
|
||||
emissive: LinearRgba::rgb(2000.0, 0.0, 0.0),
|
||||
..default()
|
||||
});
|
||||
let material_non_emissive = materials.add(StandardMaterial {
|
||||
base_color: Color::BLACK,
|
||||
emissive: LinearRgba::rgb(500.0, 0.0, 2000.0),
|
||||
..default()
|
||||
});
|
||||
|
||||
let mesh = meshes.add(Sphere::new(0.4).mesh().ico(5).unwrap());
|
||||
fn setup_scene(mut commands: Commands) {
|
||||
for _ in 0..4 {
|
||||
let x = Uniform::try_from(-2000..2000).unwrap().sample(&mut rng());
|
||||
let z = Uniform::try_from(-8000..8000).unwrap().sample(&mut rng());
|
||||
let mut rand = rng().random::<i32>() % 6;
|
||||
if rand < 0 {
|
||||
rand *= -1;
|
||||
}
|
||||
// println!("{rand}");
|
||||
|
||||
let (material, scale) = match rand {
|
||||
0 => (material_emissive1.clone(), 1.5),
|
||||
1 => (material_emissive2.clone(), 1.8),
|
||||
2 => (material_emissive3.clone(), 3.0),
|
||||
3..=5 => (material_non_emissive.clone(), 1.5),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let star_class: StarClass = match rng().random_range(0..2) {
|
||||
0 => StarClass::MType,
|
||||
1 => StarClass::BType,
|
||||
|
|
@ -253,48 +52,40 @@ fn setup_scene(
|
|||
diameter: rng().random_range(star_diameter_range(&star_class)),
|
||||
class: star_class,
|
||||
};
|
||||
let (quad_handle, material_handle) = create_star_system_annotation(
|
||||
&mut commands,
|
||||
&asset_server,
|
||||
&mut images,
|
||||
&mut meshes,
|
||||
&mut materials,
|
||||
&star_info,
|
||||
);
|
||||
|
||||
commands
|
||||
.spawn((
|
||||
Mesh3d(mesh.clone()),
|
||||
MeshMaterial3d(material.clone()),
|
||||
Transform::from_xyz(x as f32 / 100.0, z as f32 / 20000.0, z as f32 / 100.0)
|
||||
.with_scale(Vec3::splat(scale)),
|
||||
StarInfoComp(star_info),
|
||||
Wireframe,
|
||||
// TemporalVector(vec![ObjectState {
|
||||
// object_id: rng().random::<u32>(),
|
||||
// time: time.elapsed_secs(),
|
||||
// }]),
|
||||
))
|
||||
.with_children(|parent| {
|
||||
parent.spawn((
|
||||
Mesh3d(quad_handle),
|
||||
MeshMaterial3d(material_handle.clone()),
|
||||
Transform::from_xyz(0.0, 0.0, 0.0)
|
||||
// .with_scale(Vec3::splat(scale))
|
||||
.with_rotation(Quat::from_euler(EulerRot::XYZ, -PI / 2.0, 0.0, PI / 2.0)),
|
||||
// Wireframe,
|
||||
));
|
||||
});
|
||||
commands.spawn((
|
||||
Transform::from_xyz(x as f32 / 100.0, z as f32 / 20000.0, z as f32 / 100.0),
|
||||
StarInfoComp(star_info),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn start_socket(mut commands: Commands) {
|
||||
let socket = MatchboxSocket::new_reliable("ws://localhost:3536/hello");
|
||||
commands.insert_resource(socket);
|
||||
}
|
||||
|
||||
fn send_message(mut socket: ResMut<MatchboxSocket>) {
|
||||
let peers: Vec<_> = socket.connected_peers().collect();
|
||||
|
||||
for peer in peers {
|
||||
let message = "Hello";
|
||||
info!("Sending message: {message:?} to {peer}");
|
||||
socket
|
||||
.channel_mut(CHANNEL_ID)
|
||||
.send(message.as_bytes().into(), peer);
|
||||
}
|
||||
}
|
||||
|
||||
fn receive_messages(mut socket: ResMut<MatchboxSocket>) {
|
||||
for (peer, state) in socket.update_peers() {
|
||||
info!("{peer}: {state:?}");
|
||||
}
|
||||
|
||||
// example instructions
|
||||
commands.spawn((
|
||||
Text::default(),
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
bottom: Val::Px(12.0),
|
||||
left: Val::Px(12.0),
|
||||
..default()
|
||||
},
|
||||
));
|
||||
for (_id, message) in socket.channel_mut(CHANNEL_ID).receive() {
|
||||
match std::str::from_utf8(&message) {
|
||||
Ok(message) => info!("Received message: {message:?}"),
|
||||
Err(e) => error!("Failed to convert message to string: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue