From 1ca35bec8805126bdbf6353883594b2382a27ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Lescaudey=20de=20Maneville?= Date: Sun, 18 Feb 2024 11:25:45 +0100 Subject: [PATCH 1/8] Bevy 0.13 --- Cargo.toml | 9 ++++----- examples/anchors_example.rs | 8 ++++---- examples/balloon_example.rs | 4 ++-- examples/flag_example.rs | 2 +- examples/moving_example.rs | 4 ++-- examples/rapier_collision_example.rs | 6 +++--- examples/xpbd_collision_example.rs | 6 +++--- src/components/cloth_builder.rs | 7 +++++++ src/mesh.rs | 19 ++++++++++++------- 9 files changed, 38 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 419836f..b2fc2a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ xpbd_collisions = ["bevy_xpbd_3d"] thiserror = "1.0" [dependencies.bevy] -version = "0.12" +version = "0.13" default-features = false features = ["bevy_render", "bevy_asset"] @@ -44,7 +44,7 @@ bevy_rapier3d = "0.23" rand = "0.8" [dev-dependencies.bevy] -version = "0.12" +version = "0.13" features = [ "bevy_asset", "bevy_winit", @@ -54,10 +54,9 @@ features = [ "bevy_sprite", "png", "x11", - # The following features are required because of https://github.com/bevyengine/bevy/discussions/9100 - "ktx2", - "zstd", "tonemapping_luts", + # Faster compilation + "dynamic_linking" ] default-features = false diff --git a/examples/anchors_example.rs b/examples/anchors_example.rs index 6b151a8..72d5416 100644 --- a/examples/anchors_example.rs +++ b/examples/anchors_example.rs @@ -28,7 +28,7 @@ fn setup( transform: Transform::from_rotation(Quat::from_rotation_y(5.0)), ..Default::default() }); - let mesh_handle = meshes.add(shape::Cube::new(1.0).into()); + let mesh_handle = meshes.add(shape::Cube::new(1.0)); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), @@ -58,12 +58,12 @@ fn spawn_cloth( let flag_texture = asset_server.load("Bevy.png"); let (size_x, size_y) = (60, 40); - let anchor_mesh = meshes.add(shape::Cube::new(1.0).into()); + let anchor_mesh = meshes.add(shape::Cube::new(1.0)); let entity_a = commands .spawn(( PbrBundle { mesh: anchor_mesh.clone(), - material: materials.add(Color::RED.into()), + material: materials.add(Color::RED), transform: Transform::from_xyz(-15.0, 15.0, 15.0), ..Default::default() }, @@ -74,7 +74,7 @@ fn spawn_cloth( .spawn(( PbrBundle { mesh: anchor_mesh, - material: materials.add(Color::GREEN.into()), + material: materials.add(Color::GREEN), transform: Transform::from_xyz(15.0, 15.0, 15.0), ..Default::default() }, diff --git a/examples/balloon_example.rs b/examples/balloon_example.rs index 9f29e3b..a71b485 100644 --- a/examples/balloon_example.rs +++ b/examples/balloon_example.rs @@ -29,7 +29,7 @@ fn setup( mut meshes: ResMut>, ) { commands.spawn(DirectionalLightBundle::default()); - let mesh_handle = meshes.add(shape::Cube::new(1.0).into()); + let mesh_handle = meshes.add(shape::Cube::new(1.0)); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), @@ -65,7 +65,7 @@ fn spawn_cloth( .try_into() .unwrap(), ), - material: materials.add(Color::YELLOW.into()), + material: materials.add(Color::YELLOW), transform: Transform::from_xyz(0.0, 2.0, 0.0), ..Default::default() }, diff --git a/examples/flag_example.rs b/examples/flag_example.rs index 3c85e23..7f32f55 100644 --- a/examples/flag_example.rs +++ b/examples/flag_example.rs @@ -43,7 +43,7 @@ fn setup( transform: Transform::from_rotation(Quat::from_rotation_y(5.0)), ..Default::default() }); - let mesh_handle = meshes.add(shape::Cube::new(1.0).into()); + let mesh_handle = meshes.add(shape::Cube::new(1.0)); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), diff --git a/examples/moving_example.rs b/examples/moving_example.rs index 2c071b2..5c3228a 100644 --- a/examples/moving_example.rs +++ b/examples/moving_example.rs @@ -42,7 +42,7 @@ fn setup( mut materials: ResMut>, mut meshes: ResMut>, ) { - let mesh_handle = meshes.add(shape::Cube::new(1.0).into()); + let mesh_handle = meshes.add(shape::Cube::new(1.0)); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), @@ -86,7 +86,7 @@ fn spawn_cloth( b.spawn(( PbrBundle { mesh: meshes.add(shape::Cube::new(2.0).into()), - material: materials.add(Color::WHITE.into()), + material: materials.add(Color::WHITE), transform: Transform::from_xyz(10.0, 0.0, 0.0), ..Default::default() }, diff --git a/examples/rapier_collision_example.rs b/examples/rapier_collision_example.rs index a5adacc..b5cb049 100644 --- a/examples/rapier_collision_example.rs +++ b/examples/rapier_collision_example.rs @@ -47,7 +47,7 @@ fn setup( transform: Transform::from_rotation(Quat::from_rotation_y(5.0)), ..Default::default() }); - let mesh_handle = meshes.add(shape::Cube::new(2.0).into()); + let mesh_handle = meshes.add(shape::Cube::new(2.0)); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), @@ -72,7 +72,7 @@ fn setup( commands.spawn(( PbrBundle { mesh: meshes.add(shape::Cube { size: 24.0 }.into()), - material: materials.add(Color::WHITE.into()), + material: materials.add(Color::WHITE), transform: Transform::from_xyz(0.0, -12.0, 0.0), ..Default::default() }, @@ -146,7 +146,7 @@ fn shoot_balls( .try_into() .unwrap(), ), - material: materials.add(Color::WHITE.into()), + material: materials.add(Color::WHITE), transform: Transform::from_xyz(0.0, 0.0, -20.0), ..Default::default() }, diff --git a/examples/xpbd_collision_example.rs b/examples/xpbd_collision_example.rs index 8dba330..39f4045 100644 --- a/examples/xpbd_collision_example.rs +++ b/examples/xpbd_collision_example.rs @@ -47,7 +47,7 @@ fn setup( transform: Transform::from_rotation(Quat::from_rotation_y(5.0)), ..Default::default() }); - let mesh_handle = meshes.add(shape::Cube::new(2.0).into()); + let mesh_handle = meshes.add(shape::Cube::new(2.0)); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), @@ -73,7 +73,7 @@ fn setup( commands.spawn(( PbrBundle { mesh: meshes.add(shape::Cube { size: 24.0 }.into()), - material: materials.add(Color::WHITE.into()), + material: materials.add(Color::WHITE), transform: Transform::from_xyz(0.0, -12.0, 0.0), ..Default::default() }, @@ -148,7 +148,7 @@ fn shoot_balls( .try_into() .unwrap(), ), - material: materials.add(Color::WHITE.into()), + material: materials.add(Color::WHITE), transform: Transform::from_xyz(0.0, 0.0, -20.0), ..Default::default() }, diff --git a/src/components/cloth_builder.rs b/src/components/cloth_builder.rs index 028129a..8ded66d 100644 --- a/src/components/cloth_builder.rs +++ b/src/components/cloth_builder.rs @@ -19,6 +19,13 @@ type PinnedPosCondition = dyn Fn(Vec3) -> bool + Send + Sync; /// /// Add this component to an entity with at least a `GlobalTransform` and a /// `Handle` +/// +/// ## Note +/// +/// The associated `Mesh` must have its [`RenderAssetUsages`] set to `MAIN_WORLD` +/// for the cloth engine to access it +/// +/// [`RenderAssetUsages`]: bevy::render::render_asset::RenderAssetUsages, #[derive(Clone, Reflect, Default, Component)] #[must_use] pub struct ClothBuilder { diff --git a/src/mesh.rs b/src/mesh.rs index 13829d2..c1527de 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -2,7 +2,10 @@ use bevy::{ math::Vec3, - render::mesh::{Indices, Mesh, PrimitiveTopology}, + render::{ + mesh::{Indices, Mesh, PrimitiveTopology}, + render_asset::RenderAssetUsages, + }, }; #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)] @@ -58,12 +61,14 @@ pub fn rectangle_mesh( }) .collect(); - let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); - mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, points); - mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals); - mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs); - mesh.set_indices(Some(Indices::U32(indices))); - mesh + Mesh::new( + PrimitiveTopology::TriangleList, + RenderAssetUsages::MAIN_WORLD, + ) + .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points) + .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) + .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) + .with_inserted_indices(Indices::U32(indices)) } #[cfg(test)] From d8286c76afe859e084c2d45677424d7ae32ad2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Lescaudey=20de=20Maneville?= Date: Sun, 18 Feb 2024 11:39:25 +0100 Subject: [PATCH 2/8] Fixes --- src/components/cloth_builder.rs | 7 ++++--- src/components/cloth_rendering.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/components/cloth_builder.rs b/src/components/cloth_builder.rs index 8ded66d..f04e420 100644 --- a/src/components/cloth_builder.rs +++ b/src/components/cloth_builder.rs @@ -331,14 +331,15 @@ impl ClothBuilder { mesh.attribute(Mesh::ATTRIBUTE_COLOR) .and_then(|attr| match attr { VertexAttributeValues::Float32x3(v) => { - Some(v.iter().copied().map(Color::from).collect()) + Some(v.iter().copied().map(Color::rgb_from_array).collect()) } VertexAttributeValues::Float32x4(v) => { - Some(v.iter().copied().map(Color::from).collect()) + Some(v.iter().copied().map(Color::rgba_from_array).collect()) } VertexAttributeValues::Uint8x4(v) => Some( v.iter() - .map(|c| Color::rgba_u8(c[0], c[1], c[2], c[3])) + .copied() + .map(|[r, g, b, a]| Color::rgba_u8(r, g, b, a)) .collect(), ), _ => None, diff --git a/src/components/cloth_rendering.rs b/src/components/cloth_rendering.rs index 3672432..c7a3a08 100644 --- a/src/components/cloth_rendering.rs +++ b/src/components/cloth_rendering.rs @@ -265,7 +265,7 @@ impl ClothRendering { Mesh::ATTRIBUTE_NORMAL, Self::vec3_vertex_attr(&vertex_normals), ); - mesh.set_indices(Some(Indices::U32(new_self.indices))); + mesh.insert_indices(Indices::U32(new_self.indices)); } } } From 2ca165bf9f3df545ea00ed7b88138042aceb52e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Lescaudey=20de=20Maneville?= Date: Tue, 20 Feb 2024 09:35:05 +0100 Subject: [PATCH 3/8] examples --- examples/anchors_example.rs | 10 +++------- examples/balloon_example.rs | 11 ++--------- examples/camera_plugin.rs | 2 +- examples/flag_example.rs | 2 +- examples/moving_example.rs | 4 ++-- examples/rapier_collision_example.rs | 2 +- examples/xpbd_collision_example.rs | 2 +- 7 files changed, 11 insertions(+), 22 deletions(-) diff --git a/examples/anchors_example.rs b/examples/anchors_example.rs index 72d5416..e19a60e 100644 --- a/examples/anchors_example.rs +++ b/examples/anchors_example.rs @@ -28,7 +28,7 @@ fn setup( transform: Transform::from_rotation(Quat::from_rotation_y(5.0)), ..Default::default() }); - let mesh_handle = meshes.add(shape::Cube::new(1.0)); + let mesh_handle = meshes.add(Cuboid::default()); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), @@ -39,11 +39,7 @@ fn setup( commands.spawn(PbrBundle { mesh: mesh_handle.clone(), transform: Transform::from_xyz(x, 1.0, z), - material: materials.add(StandardMaterial { - base_color: color, - double_sided: true, - ..Default::default() - }), + material: materials.add(color), ..Default::default() }); }); @@ -58,7 +54,7 @@ fn spawn_cloth( let flag_texture = asset_server.load("Bevy.png"); let (size_x, size_y) = (60, 40); - let anchor_mesh = meshes.add(shape::Cube::new(1.0)); + let anchor_mesh = meshes.add(Cuboid::default()); let entity_a = commands .spawn(( PbrBundle { diff --git a/examples/balloon_example.rs b/examples/balloon_example.rs index a71b485..0db1e6a 100644 --- a/examples/balloon_example.rs +++ b/examples/balloon_example.rs @@ -29,7 +29,7 @@ fn setup( mut meshes: ResMut>, ) { commands.spawn(DirectionalLightBundle::default()); - let mesh_handle = meshes.add(shape::Cube::new(1.0)); + let mesh_handle = meshes.add(Cuboid::default()); [ (Color::BLUE, [-10.0, 0.0]), (Color::GREEN, [10.0, 0.0]), @@ -57,14 +57,7 @@ fn spawn_cloth( ) { commands.spawn(( PbrBundle { - mesh: meshes.add( - shape::Icosphere { - radius: 5.0, - subdivisions: 10, - } - .try_into() - .unwrap(), - ), + mesh: meshes.add(Sphere::new(5.).mesh().ico(10).unwrap()), material: materials.add(Color::YELLOW), transform: Transform::from_xyz(0.0, 2.0, 0.0), ..Default::default() diff --git a/examples/camera_plugin.rs b/examples/camera_plugin.rs index 21c21e8..8970329 100644 --- a/examples/camera_plugin.rs +++ b/examples/camera_plugin.rs @@ -31,7 +31,7 @@ pub fn handle_camera( mut cam_controls: Query<&mut Transform, With>, mut motion_evr: EventReader, mut scroll_evr: EventReader, - buttons: Res>, + buttons: Res>, time: Res