Skip to content

Commit

Permalink
update for Bevy 0.13 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Mar 9, 2024
1 parent 8bb0087 commit 81b9afe
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 41 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "bevy_pathmesh"
version = "0.5.0"
version = "0.6.0"
authors = ["François Mockers <mockersf@gmail.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -13,21 +13,21 @@ documentation = "https://docs.rs/bevy_pathmesh"
categories = ["game-development"]

[dependencies]
itertools = "0.11"
itertools = "0.12"

[dependencies.polyanya]
version = "0.4"
version = "0.5"

[dependencies.bevy]
version = "0.12"
version = "0.13"
features = ["bevy_render", "bevy_asset"]
default-features = false

[dev-dependencies]
rand = "0.8"

[dev-dependencies.bevy]
version = "0.12"
version = "0.13"
features = [
"bevy_ui",
"bevy_text",
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -85,5 +85,6 @@ fn get_path(

|Bevy|bevy_pathmesh|
|---|---|
|0.13|0.6|
|0.11|0.5|
|0.10|0.4|
45 changes: 28 additions & 17 deletions examples/gltf.rs
Expand Up @@ -20,14 +20,13 @@ fn main() {
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Navmesh with Polyanya".to_string(),
fit_canvas_to_parent: true,
..default()
}),
..default()
}),
PathMeshPlugin,
))
.add_state::<AppState>()
.init_state::<AppState>()
.add_systems(OnEnter(AppState::Setup), setup)
.add_systems(Update, check_textures.run_if(in_state(AppState::Setup)))
.add_systems(OnExit(AppState::Setup), setup_scene)
Expand Down Expand Up @@ -64,7 +63,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

commands.insert_resource(AmbientLight {
color: Color::SEA_GREEN,
brightness: 0.05,
brightness: 100.0,
});

commands.spawn(Camera3dBundle {
Expand Down Expand Up @@ -197,7 +196,7 @@ fn setup_scene(
commands.spawn((
SpotLightBundle {
spot_light: SpotLight {
intensity: 800.0,
intensity: 1000000.0,
color: Color::SEA_GREEN,
shadows_enabled: true,
inner_angle: 0.5,
Expand Down Expand Up @@ -254,8 +253,12 @@ fn setup_scene(
commands
.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule { ..default() })),
material: materials.add(Color::BLUE.into()),
mesh: meshes.add(Mesh::from(Capsule3d { ..default() })),
material: materials.add(StandardMaterial {
base_color: Color::BLUE,
emissive: Color::BLUE * 50.0,
..default()
}),
transform: Transform::from_xyz(-1.0, 0.0, -2.0),
..Default::default()
},
Expand All @@ -267,7 +270,7 @@ fn setup_scene(
point_light: PointLight {
color: Color::BLUE,
range: 500.0,
intensity: 2000.0,
intensity: 100000.0,
shadows_enabled: true,
..default()
},
Expand Down Expand Up @@ -309,11 +312,15 @@ fn give_target_auto(
let target_id = commands
.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
mesh: meshes.add(Mesh::from(Sphere {
radius: 0.5,
..default()
})),
material: materials.add(Color::RED.into()),
material: materials.add(StandardMaterial {
base_color: Color::RED,
emissive: Color::RED * 50.0,
..default()
}),
transform: Transform::from_xyz(x, 0.0, z),
..Default::default()
},
Expand All @@ -325,7 +332,7 @@ fn give_target_auto(
point_light: PointLight {
color: Color::RED,
shadows_enabled: true,
range: 3.0,
range: 10.0,
..default()
},
transform: Transform::from_xyz(0.0, 1.5, 0.0),
Expand All @@ -350,7 +357,7 @@ fn give_target_on_click(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
current_mesh: Res<CurrentMesh>,
mouse_buttons: Res<Input<MouseButton>>,
mouse_buttons: Res<ButtonInput<MouseButton>>,
primary_window: Query<&Window, With<PrimaryWindow>>,
camera: Query<(&Camera, &GlobalTransform)>,
) {
Expand All @@ -360,7 +367,7 @@ fn give_target_on_click(
let position = primary_window.single().cursor_position()?;
let (camera, transform) = camera.get_single().ok()?;
let ray = camera.viewport_to_world(transform, position)?;
let denom = Vec3::Y.dot(ray.direction);
let denom = Vec3::Y.dot(ray.direction.into());
let t = (Vec3::ZERO - ray.origin).dot(Vec3::Y) / denom;
let target = ray.origin + ray.direction * t;
navmesh.transformed_is_in_mesh(target).then_some(target)
Expand All @@ -378,11 +385,15 @@ fn give_target_on_click(
let target_id = commands
.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
mesh: meshes.add(Mesh::from(Sphere {
radius: 0.5,
..default()
})),
material: materials.add(Color::RED.into()),
material: materials.add(StandardMaterial {
base_color: Color::RED,
emissive: Color::RED * 50.0,
..default()
}),
transform: Transform::from_translation(target),
..Default::default()
},
Expand All @@ -394,7 +405,7 @@ fn give_target_on_click(
point_light: PointLight {
color: Color::RED,
shadows_enabled: true,
range: 3.0,
range: 10.0,
..default()
},
transform: Transform::from_xyz(0.0, 1.5, 0.0),
Expand Down Expand Up @@ -437,7 +448,7 @@ fn move_object(

fn trigger_navmesh_visibility(
mut query: Query<(&mut Visibility, &NavMeshDisp)>,
keyboard_input: ResMut<Input<KeyCode>>,
keyboard_input: ResMut<ButtonInput<KeyCode>>,
current_mesh: Res<CurrentMesh>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
Expand All @@ -460,7 +471,7 @@ fn target_activity(
) {
for children in &target {
point_light.get_mut(children[0]).unwrap().intensity =
(time.elapsed_seconds() * 10.0).sin().abs() * 100.0;
(time.elapsed_seconds() * 10.0).sin().abs() * 100000.0;
}
}

Expand Down
5 changes: 2 additions & 3 deletions examples/lines.rs
Expand Up @@ -12,7 +12,6 @@ fn main() {
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Navmesh with Polyanya".to_string(),
fit_canvas_to_parent: true,
..default()
}),
..default()
Expand Down Expand Up @@ -224,7 +223,7 @@ fn on_mesh_change(
});
}

fn mesh_change(mut mesh: ResMut<MeshDetails>, keyboard_input: Res<Input<KeyCode>>) {
fn mesh_change(mut mesh: ResMut<MeshDetails>, keyboard_input: Res<ButtonInput<KeyCode>>) {
if keyboard_input.just_pressed(KeyCode::Space) {
match mesh.mesh {
CurrentMesh::Simple => *mesh = ARENA,
Expand All @@ -239,7 +238,7 @@ struct NewPathStepEvent(Vec2);

fn on_click(
mut path_step_event: EventWriter<NewPathStepEvent>,
mouse_button_input: Res<Input<MouseButton>>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
primary_window: Query<&Window, With<PrimaryWindow>>,
camera_q: Query<(&Camera, &GlobalTransform)>,
mesh: Res<MeshDetails>,
Expand Down
7 changes: 3 additions & 4 deletions examples/many.rs
Expand Up @@ -26,7 +26,6 @@ fn main() {
.set(WindowPlugin {
primary_window: Some(Window {
title: "Navmesh with Polyanya".to_string(),
fit_canvas_to_parent: true,
present_mode: PresentMode::AutoNoVsync,
..default()
}),
Expand Down Expand Up @@ -507,7 +506,7 @@ fn update_ui(
text.sections[3].value = format!(
"{:.2}\n",
diagnostics
.get(FrameTimeDiagnosticsPlugin::FPS)
.get(&FrameTimeDiagnosticsPlugin::FPS)
.and_then(|d| d.average())
.unwrap_or_default()
);
Expand Down Expand Up @@ -537,7 +536,7 @@ fn update_ui(
}

fn mode_change(
keyboard_input: Res<Input<KeyCode>>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut task_mode: ResMut<TaskMode>,
mut display_mode: ResMut<DisplayMode>,
) {
Expand All @@ -547,7 +546,7 @@ fn mode_change(
TaskMode::Blocking => *task_mode = TaskMode::Async,
}
}
if keyboard_input.just_pressed(KeyCode::L) {
if keyboard_input.just_pressed(KeyCode::KeyL) {
match *display_mode {
DisplayMode::Line => *display_mode = DisplayMode::Nothing,
DisplayMode::Nothing => *display_mode = DisplayMode::Line,
Expand Down
7 changes: 3 additions & 4 deletions examples/moving.rs
Expand Up @@ -20,7 +20,6 @@ fn main() {
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Navmesh with Polyanya".to_string(),
fit_canvas_to_parent: true,
..default()
}),
..default()
Expand Down Expand Up @@ -226,8 +225,8 @@ fn on_mesh_change(

fn mesh_change(
mut mesh: ResMut<MeshDetails>,
keyboard_input: Res<Input<KeyCode>>,
mouse_input: Res<Input<MouseButton>>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mouse_input: Res<ButtonInput<MouseButton>>,
time: Res<Time>,
mut pressed_since: Local<Option<Duration>>,
) {
Expand Down Expand Up @@ -270,7 +269,7 @@ struct Path {
}

fn on_click(
mouse_button_input: Res<Input<MouseButton>>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
primary_window: Query<&Window, With<PrimaryWindow>>,
camera_q: Query<(&Camera, &GlobalTransform)>,
mesh: Res<MeshDetails>,
Expand Down
17 changes: 9 additions & 8 deletions src/lib.rs
Expand Up @@ -16,6 +16,7 @@ use std::sync::Arc;
use bevy::math::Vec3Swizzles;
use bevy::reflect::TypePath;
use bevy::render::mesh::{MeshVertexAttributeId, VertexAttributeValues};
use bevy::render::render_asset::RenderAssetUsages;
use bevy::{
prelude::*,
render::{mesh::Indices, render_resource::PrimitiveTopology},
Expand Down Expand Up @@ -183,7 +184,7 @@ impl PathMesh {

/// Creates a [`Mesh`] from this [`PathMesh`], suitable for rendering the surface
pub fn to_mesh(&self) -> Mesh {
let mut new_mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut new_mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::all());
let inverse_transform = self.inverse_transform();
new_mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
Expand All @@ -194,7 +195,7 @@ impl PathMesh {
.map(|coords| inverse_transform.transform_point(coords.into()).into())
.collect::<Vec<[f32; 3]>>(),
);
new_mesh.set_indices(Some(Indices::U32(
new_mesh.insert_indices(Indices::U32(
self.mesh
.polygons
.iter()
Expand All @@ -203,13 +204,13 @@ impl PathMesh {
.flat_map(|i| [p.vertices[0], p.vertices[i - 1], p.vertices[i]])
})
.collect(),
)));
));
new_mesh
}

/// Creates a [`Mesh`] from this [`PathMesh`], showing the wireframe of the polygons
pub fn to_wireframe_mesh(&self) -> Mesh {
let mut new_mesh = Mesh::new(PrimitiveTopology::LineList);
let mut new_mesh = Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::all());
let inverse_transform = self.inverse_transform();
new_mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
Expand All @@ -220,7 +221,7 @@ impl PathMesh {
.map(|coords| inverse_transform.transform_point(coords.into()).into())
.collect::<Vec<[f32; 3]>>(),
);
new_mesh.set_indices(Some(Indices::U32(
new_mesh.insert_indices(Indices::U32(
self.mesh
.polygons
.iter()
Expand All @@ -231,7 +232,7 @@ impl PathMesh {
.unique_by(|[a, b]| if a < b { (*a, *b) } else { (*b, *a) })
.flatten()
.collect(),
)));
));
new_mesh
}

Expand Down Expand Up @@ -304,7 +305,7 @@ mod tests {
}
.into(),
);
let mut bevy_mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut bevy_mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::all());
bevy_mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
Expand All @@ -323,7 +324,7 @@ mod tests {
[0.0, 1.0, -0.0],
],
);
bevy_mesh.set_indices(Some(Indices::U32(vec![0, 1, 3, 0, 3, 2])));
bevy_mesh.insert_indices(Indices::U32(vec![0, 1, 3, 0, 3, 2]));

let actual_path_mesh = PathMesh::from_bevy_mesh(&bevy_mesh);

Expand Down

0 comments on commit 81b9afe

Please sign in to comment.