Skip to content

Commit

Permalink
Merge pull request #83 from zzhgithub/feature-support-more-mesh
Browse files Browse the repository at this point in the history
Feature support more mesh
  • Loading branch information
zzhgithub authored Sep 17, 2023
2 parents f8956cd + 299c3fa commit 017daf1
Show file tree
Hide file tree
Showing 23 changed files with 597 additions and 114 deletions.
226 changes: 134 additions & 92 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ huffman-compress = { git = "https://github.com/zzhgithub/rust-huffman-compress",
"serde",
] }
noise = { version = "0.8.2" }
lazy_static = "1.4.0"
bevy_vox_mesh = { git = "https://github.com/zzhgithub/bevy_vox_mesh.git", branch = "fix" }

# 解决冲突
lock_api = "0.4.10"
codespan-reporting = "0.11.1"


[profile.dev.package.bevy_rapier3d]
opt-level = 3

Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cargo run --release --no-default-features --features headless --bin server
For Client
```shell
cargo run --release --bin client
# WGPU_BACKEND=opengl
```

# 控制
Expand Down
Binary file added assets/staff/工作方块.png
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/vox/工作台.vox
Binary file not shown.
2 changes: 2 additions & 0 deletions src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use just_join::{
},
staff::StaffInfoPlugin,
tools::inspector_egui::inspector_ui,
voxel_world::voxel_mesh::VoxelMeshPlugin,
CLIENT_DEBUG, CLIENT_FPS,
};
fn main() {
Expand Down Expand Up @@ -53,6 +54,7 @@ fn main() {
app.add_plugins(StaffInfoPlugin);
app.add_plugins(UiResourcePlugin);
app.add_plugins(Sprite3dPlugin);
app.add_plugins(VoxelMeshPlugin);

app.add_plugins((SplashPlugin, MenuPlugin, NotificationPlugin, GamePlugin));
// 调试工具
Expand Down
11 changes: 9 additions & 2 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use just_join::{
},
sky::ServerSkyPlugins,
staff::ServerStaffInfoPlugin,
voxel_world::biomes::OtherTreePlugin,
voxel_world::{biomes::OtherTreePlugin, voxel_mesh::VoxelMeshPlugin},
PROTOCOL_ID,
};
use renet_visualizer::RenetServerVisualizer;
Expand All @@ -43,7 +43,11 @@ use {
};

#[cfg(feature = "headless")]
use bevy::MinimalPlugins;
use bevy::{
asset::AssetPlugin,
prelude::{AddAsset, Mesh},
MinimalPlugins,
};

fn new_renet_server() -> (RenetServer, NetcodeServerTransport) {
let server = RenetServer::new(connection_config());
Expand Down Expand Up @@ -101,6 +105,8 @@ fn main() {
#[cfg(feature = "headless")]
{
app.add_plugins(MinimalPlugins);
app.add_plugins(AssetPlugin::default());
app.add_asset::<Mesh>();
}

app.add_plugins(RenetServerPlugin);
Expand All @@ -121,6 +127,7 @@ fn main() {
ServerStaffRulePlugin,
CossTroughCheckPlugin,
OtherTreePlugin,
VoxelMeshPlugin,
));

let (server, transport) = new_renet_server();
Expand Down
13 changes: 10 additions & 3 deletions src/client/mesh_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy::{
prelude::{
AlphaMode, AssetServer, Assets, Color, Commands, Component, Entity, Handle, IVec3,
IntoSystemConfigs, Last, MaterialMeshBundle, MaterialPlugin, Mesh, Plugin, PreUpdate, Res,
ResMut, Resource, StandardMaterial, Startup, Transform, Update,
ResMut, Resource, StandardMaterial, Startup, Transform, Update, Vec3,
},
tasks::{AsyncComputeTaskPool, Task},
time::{Time, Timer, TimerMode},
Expand Down Expand Up @@ -327,7 +327,14 @@ pub fn save_chunk_result(
}

#[derive(Debug, Component)]
pub struct TerrainMesh;
pub struct TerrainMesh(pub HitMeshType);

// 可以被识别的方式!
#[derive(Debug, Clone)]
pub enum HitMeshType {
Common,
Sp(Vec3),
}

#[derive(Debug, Component)]
pub struct WaterMesh;
Expand Down Expand Up @@ -372,7 +379,7 @@ pub fn update_mesh_system(
material: materials.0.clone(),
..Default::default()
},
TerrainMesh,
TerrainMesh(HitMeshType::Common),
RaycastMesh::<MyRaycastSet>::default(), // Make this mesh ray cast-able
))
.id(),
Expand Down
1 change: 1 addition & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod state_manager;
pub mod tool_bar_manager;
pub mod ui;
pub mod voxels;
pub mod sp_mesh_display;

// 同步创建或者删除角色
pub fn client_sync_players(
Expand Down
6 changes: 5 additions & 1 deletion src/client/player/mouse_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ pub fn mouse_button_system(
}

if mouse_button_input.just_pressed(MouseButton::Right) {
if let Some(crate::staff::StaffType::Voxel(voxel_type)) = tool_bar_data.staff_type() {
// Note: 这里放置时尝试转换成体素再传递
if let Some(crate::staff::StaffType::Voxel(voxel_type)) =
tool_bar_data.staff_type_try_to_voxel()
{
if let Some(pos) = choose_cube.out_center {
println!("放置物品{:?}", voxel_type);
// 判断当前这里是否和 其他的player的位置冲突
if check_player_put_object_available(pos.clone(), &player_query) {
// 还要发送当前生效的 tool_bar的 index
Expand Down
27 changes: 20 additions & 7 deletions src/client/ray_cast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy::{
prelude::{
AlphaMode, Assets, Color, Commands, Entity, Gizmos, GlobalTransform, Mesh, PbrBundle,
Plugin, Query, Res, ResMut, StandardMaterial, Startup, Transform, Update, Vec3, Visibility,
With, Without,
Plugin, Quat, Query, Res, ResMut, StandardMaterial, Startup, Transform, Update, Vec3,
Visibility, With, Without,
},
reflect::Reflect,
render::render_resource::PrimitiveTopology,
Expand Down Expand Up @@ -118,7 +118,7 @@ pub fn touth_mesh_ray_cast(
query: Query<&GlobalTransform, With<CameraTag>>,
mut choose_cube: ResMut<ChooseCube>,
mut gizmos: Gizmos,
query_mesh: Query<Entity, &TerrainMesh>,
query_mesh: Query<(Entity, &TerrainMesh)>,
mut query_help_cube: Query<
(&mut Transform, &mut Visibility),
(With<HelpCube>, Without<CameraTag>),
Expand Down Expand Up @@ -147,7 +147,8 @@ pub fn touth_mesh_ray_cast(
},
);

if let Some((_, hit)) = hits.first() {
if let Some((entity, hit)) = hits.first() {
let (_, mesh_data) = query_mesh.get(*entity).unwrap();
let hit_point = hit.position();
if ray_pos.distance(hit_point) <= TOUCH_RADIUS {
let normal = hit.normal();
Expand All @@ -159,15 +160,27 @@ pub fn touth_mesh_ray_cast(
let rate = timer.elapsed().as_millis() as f32 / timer.duration().as_millis() as f32;
gizmos.circle(hit_point, normal, 0.1 * rate, Color::BLUE);
}
let center_point = get_pos_chunk_center(hit_point, normal);
let out_center_point = get_pos_chunk_center(hit_point, -normal);

let center_point: Vec3;
match mesh_data.0 {
super::mesh_display::HitMeshType::Common => {
center_point = get_pos_chunk_center(hit_point, normal);
let out_center_point = get_pos_chunk_center(hit_point, -normal);
gizmos.sphere(out_center_point, Quat::IDENTITY, 0.5, Color::GREEN);
choose_cube.out_center = Some(out_center_point);
}
super::mesh_display::HitMeshType::Sp(center) => {
center_point = center;
choose_cube.out_center = None;
}
}

gizmos.sphere(center_point, Quat::IDENTITY, 0.5, Color::DARK_GRAY);
*visibility = Visibility::Visible;
*chue_pos = Transform::from_translation(center_point);

choose_cube.choose_on = Some(hit_point);
choose_cube.center = Some(center_point);
choose_cube.out_center = Some(out_center_point);
} else {
hidden_help_cube(choose_cube.as_mut(), &mut visibility);
}
Expand Down
Loading

0 comments on commit 017daf1

Please sign in to comment.