diff options
| author | Sébastien Crozet <developer@crozet.re> | 2022-01-09 17:25:17 +0100 |
|---|---|---|
| committer | Sébastien Crozet <developer@crozet.re> | 2022-01-09 20:41:41 +0100 |
| commit | a0d197e6919dda61ac2957e93ac72234fd6850e4 (patch) | |
| tree | b6459c5a9e26d6729f1d2955ac3ffde0e0ee9c79 /src_testbed | |
| parent | 6627f7193b3a110915b05839c7fb6d592ad24fd2 (diff) | |
| download | rapier-a0d197e6919dda61ac2957e93ac72234fd6850e4.tar.gz rapier-a0d197e6919dda61ac2957e93ac72234fd6850e4.tar.bz2 rapier-a0d197e6919dda61ac2957e93ac72234fd6850e4.zip | |
Update the testbed to use bevy 0.6
Diffstat (limited to 'src_testbed')
| -rw-r--r-- | src_testbed/camera2d.rs | 3 | ||||
| -rw-r--r-- | src_testbed/camera3d.rs | 3 | ||||
| -rw-r--r-- | src_testbed/graphics.rs | 17 | ||||
| -rw-r--r-- | src_testbed/objects/node.rs | 59 | ||||
| -rw-r--r-- | src_testbed/plugin.rs | 15 | ||||
| -rw-r--r-- | src_testbed/testbed.rs | 122 |
6 files changed, 128 insertions, 91 deletions
diff --git a/src_testbed/camera2d.rs b/src_testbed/camera2d.rs index 81b3263..46aacf1 100644 --- a/src_testbed/camera2d.rs +++ b/src_testbed/camera2d.rs @@ -9,6 +9,7 @@ use bevy::render::camera::Camera; const LINE_TO_PIXEL_RATIO: f32 = 0.1; +#[derive(Component)] pub struct OrbitCamera { pub zoom: f32, pub center: Vec3, @@ -87,7 +88,7 @@ impl OrbitCameraPlugin { } } impl Plugin for OrbitCameraPlugin { - fn build(&self, app: &mut AppBuilder) { + fn build(&self, app: &mut App) { app.add_system(Self::mouse_motion_system.system()) .add_system(Self::zoom_system.system()) .add_system(Self::update_transform_system.system()); diff --git a/src_testbed/camera3d.rs b/src_testbed/camera3d.rs index 602ef31..7df66da 100644 --- a/src_testbed/camera3d.rs +++ b/src_testbed/camera3d.rs @@ -11,6 +11,7 @@ use std::ops::RangeInclusive; const LINE_TO_PIXEL_RATIO: f32 = 0.1; +#[derive(Component)] pub struct OrbitCamera { pub x: f32, pub y: f32, @@ -113,7 +114,7 @@ impl OrbitCameraPlugin { } } impl Plugin for OrbitCameraPlugin { - fn build(&self, app: &mut AppBuilder) { + fn build(&self, app: &mut App) { app.add_system(Self::mouse_motion_system.system()) .add_system(Self::zoom_system.system()) .add_system(Self::update_transform_system.system()); diff --git a/src_testbed/graphics.rs b/src_testbed/graphics.rs index 706fc60..b623f26 100644 --- a/src_testbed/graphics.rs +++ b/src_testbed/graphics.rs @@ -17,6 +17,11 @@ use rand::{Rng, SeedableRng}; use rand_pcg::Pcg32; use std::collections::HashMap; +#[cfg(feature = "dim2")] +pub type BevyMaterial = ColorMaterial; +#[cfg(feature = "dim3")] +pub type BevyMaterial = StandardMaterial; + pub struct GraphicsManager { rand: Pcg32, b2sn: HashMap<RigidBodyHandle, Vec<EntityWithGraphics>>, @@ -84,7 +89,7 @@ impl GraphicsManager { pub fn set_body_color( &mut self, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, b: RigidBodyHandle, color: [f32; 3], ) { @@ -161,7 +166,7 @@ impl GraphicsManager { fn alloc_color( &mut self, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, handle: RigidBodyHandle, is_static: bool, ) -> Point3<f32> { @@ -183,7 +188,7 @@ impl GraphicsManager { &mut self, commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, components: &mut Query<(&mut Transform,)>, handle: RigidBodyHandle, bodies: &RigidBodySet, @@ -206,7 +211,7 @@ impl GraphicsManager { &mut self, commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, components: &mut Query<(&mut Transform,)>, handle: RigidBodyHandle, bodies: &RigidBodySet, @@ -257,7 +262,7 @@ impl GraphicsManager { &mut self, commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, handle: ColliderHandle, colliders: &ColliderSet, ) { @@ -292,7 +297,7 @@ impl GraphicsManager { &mut self, commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, handle: Option<ColliderHandle>, shape: &dyn Shape, sensor: bool, diff --git a/src_testbed/objects/node.rs b/src_testbed/objects/node.rs index aea11d8..1310415 100644 --- a/src_testbed/objects/node.rs +++ b/src_testbed/objects/node.rs @@ -5,15 +5,17 @@ use bevy::render::mesh::{Indices, VertexAttributeValues}; use na::{point, Point3, Vector3}; use std::collections::HashMap; -use bevy::render::pipeline::PrimitiveTopology; -use bevy::render::wireframe::Wireframe; +use bevy::pbr::wireframe::Wireframe; +use bevy::render::render_resource::PrimitiveTopology; use rapier::geometry::{ColliderHandle, ColliderSet, Shape, ShapeType}; #[cfg(feature = "dim3")] use rapier::geometry::{Cone, Cylinder}; use rapier::math::Isometry; +use crate::graphics::BevyMaterial; #[cfg(feature = "dim2")] use { + bevy::sprite::MaterialMesh2dBundle, na::{Point2, Vector2}, rapier::geometry::{Ball, Cuboid}, }; @@ -26,14 +28,14 @@ pub struct EntityWithGraphics { pub collider: Option<ColliderHandle>, pub delta: Isometry<f32>, pub opacity: f32, - material: Handle<StandardMaterial>, + material: Handle<BevyMaterial>, } impl EntityWithGraphics { pub fn spawn( commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, prefab_meshs: &HashMap<ShapeType, Handle<Mesh>>, shape: &dyn Shape, collider: Option<ColliderHandle>, @@ -74,9 +76,15 @@ impl EntityWithGraphics { transform.rotation = Quat::from_rotation_z(shape_pos.rotation.angle()); } + #[cfg(feature = "dim2")] + let material = ColorMaterial { + color: bevy_color, + texture: None, + }; + #[cfg(feature = "dim3")] let material = StandardMaterial { metallic: 0.5, - roughness: 0.5, + perceptual_roughness: 0.5, double_sided: true, // TODO: this doesn't do anything? ..StandardMaterial::from(bevy_color) }; @@ -84,7 +92,15 @@ impl EntityWithGraphics { let material_weak_handle = material_handle.clone_weak(); if let Some(mesh) = mesh { - let pbr = PbrBundle { + #[cfg(feature = "dim2")] + let bundle = MaterialMesh2dBundle { + mesh: mesh.into(), + material: material_handle, + transform, + ..Default::default() + }; + #[cfg(feature = "dim3")] + let bundle = PbrBundle { mesh, material: material_handle, transform, @@ -92,7 +108,7 @@ impl EntityWithGraphics { }; let mut entity_commands = commands.entity(entity); - entity_commands.insert_bundle(pbr); + entity_commands.insert_bundle(bundle); if sensor { entity_commands.insert(Wireframe); @@ -115,23 +131,38 @@ impl EntityWithGraphics { commands.entity(self.entity).despawn(); } - pub fn select(&mut self, materials: &mut Assets<StandardMaterial>) { + pub fn select(&mut self, materials: &mut Assets<BevyMaterial>) { // NOTE: we don't just call `self.set_color` because that would // overwrite self.base_color too. self.color = point![1.0, 0.0, 0.0]; if let Some(material) = materials.get_mut(&self.material) { - material.base_color = - Color::rgba(self.color.x, self.color.y, self.color.z, self.opacity); + #[cfg(feature = "dim2")] + { + material.color = + Color::rgba(self.color.x, self.color.y, self.color.z, self.opacity); + } + #[cfg(feature = "dim3")] + { + material.base_color = + Color::rgba(self.color.x, self.color.y, self.color.z, self.opacity); + } } } - pub fn unselect(&mut self, materials: &mut Assets<StandardMaterial>) { + pub fn unselect(&mut self, materials: &mut Assets<BevyMaterial>) { self.set_color(materials, self.base_color); } - pub fn set_color(&mut self, materials: &mut Assets<StandardMaterial>, color: Point3<f32>) { + pub fn set_color(&mut self, materials: &mut Assets<BevyMaterial>, color: Point3<f32>) { if let Some(material) = materials.get_mut(&self.material) { - material.base_color = Color::rgba(color.x, color.y, color.z, self.opacity); + #[cfg(feature = "dim2")] + { + material.color = Color::rgba(color.x, color.y, color.z, self.opacity); + } + #[cfg(feature = "dim3")] + { + material.base_color = Color::rgba(color.x, color.y, color.z, self.opacity); + } } self.color = color; self.base_color = color; @@ -336,7 +367,7 @@ fn collider_mesh_scale(co_shape: &dyn Shape) -> Vec3 { #[cfg(feature = "dim3")] ShapeType::Cuboid => { let c = co_shape.as_cuboid().unwrap(); - Vec3::from_slice_unaligned(c.half_extents.as_slice()) + Vec3::from_slice(c.half_extents.as_slice()) } #[cfg(feature = "dim3")] ShapeType::Cylinder => { diff --git a/src_testbed/plugin.rs b/src_testbed/plugin.rs index 8a9d98c..6eaf09e 100644 --- a/src_testbed/plugin.rs +++ b/src_testbed/plugin.rs @@ -1,22 +1,19 @@ +use crate::graphics::BevyMaterial; use crate::harness::Harness; use crate::physics::PhysicsState; use crate::GraphicsManager; use bevy::prelude::*; -use bevy::render::pipeline::PipelineDescriptor; +// use bevy::render::render_resource::RenderPipelineDescriptor; use bevy_egui::EguiContext; pub trait TestbedPlugin { - fn init_plugin( - &mut self, - pipelines: &mut Assets<PipelineDescriptor>, - shaders: &mut Assets<Shader>, - ); + fn init_plugin(&mut self); fn init_graphics( &mut self, graphics: &mut GraphicsManager, commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, components: &mut Query<(&mut Transform,)>, harness: &mut Harness, ); @@ -28,7 +25,7 @@ pub trait TestbedPlugin { graphics: &mut GraphicsManager, commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, components: &mut Query<(&mut Transform,)>, harness: &mut Harness, ); @@ -39,7 +36,7 @@ pub trait TestbedPlugin { graphics: &mut GraphicsManager, commands: &mut Commands, meshes: &mut Assets<Mesh>, - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, components: &mut Query<(&mut Transform,)>, ); fn profiling_string(&self) -> String; diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index ac53733..ce6544e 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -1,7 +1,6 @@ use std::env; use std::mem; -use bevy::pbr::Light; use bevy::prelude::*; use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState}; @@ -25,16 +24,17 @@ use crate::box2d_backend::Box2dWorld; use crate::harness::Harness; #[cfg(all(feature = "dim3", feature = "other-backends"))] use crate::physx_backend::PhysxWorld; +use bevy::pbr::wireframe::WireframePlugin; use bevy::render::camera::Camera; -use bevy::render::wireframe::WireframePlugin; -use bevy::wgpu::{WgpuFeature, WgpuFeatures, WgpuOptions}; +use bevy::render::options::{WgpuFeatures, WgpuOptions}; use bevy_egui::EguiContext; #[cfg(feature = "dim2")] use crate::camera2d::{OrbitCamera, OrbitCameraPlugin}; #[cfg(feature = "dim3")] use crate::camera3d::{OrbitCamera, OrbitCameraPlugin}; -use bevy::render::pipeline::PipelineDescriptor; +use crate::graphics::BevyMaterial; +// use bevy::render::render_resource::RenderPipelineDescriptor; const RAPIER_BACKEND: usize = 0; #[cfg(all(feature = "dim2", feature = "other-backends"))] @@ -128,19 +128,17 @@ struct OtherBackends { } struct Plugins(Vec<Box<dyn TestbedPlugin>>); -pub struct TestbedGraphics<'a, 'b, 'c, 'd> { +pub struct TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f> { graphics: &'a mut GraphicsManager, - commands: &'a mut Commands<'d>, + commands: &'a mut Commands<'d, 'e>, meshes: &'a mut Assets<Mesh>, - materials: &'a mut Assets<StandardMaterial>, - shaders: &'a mut Assets<Shader>, - pipelines: &'a mut Assets<PipelineDescriptor>, - components: &'a mut Query<'b, (&'c mut Transform,)>, + materials: &'a mut Assets<BevyMaterial>, + components: &'a mut Query<'b, 'f, (&'c mut Transform,)>, camera: &'a mut OrbitCamera, } -pub struct Testbed<'a, 'b, 'c, 'd> { - graphics: Option<TestbedGraphics<'a, 'b, 'c, 'd>>, +pub struct Testbed<'a, 'b, 'c, 'd, 'e, 'f> { + graphics: Option<TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f>>, harness: &'a mut Harness, state: &'a mut TestbedState, #[cfg(feature = "other-backends")] @@ -357,7 +355,7 @@ impl TestbedApp { "Rapier: 3D demos".to_string() }; - let mut app = App::build(); + let mut app = App::new(); app.insert_resource(WindowDescriptor { title, @@ -365,12 +363,14 @@ impl TestbedApp { ..Default::default() }) .insert_resource(ClearColor(Color::rgb(0.85, 0.85, 0.85))) - .insert_resource(Msaa { samples: 2 }) + .insert_resource(Msaa { samples: 4 }) .insert_resource(WgpuOptions { - features: WgpuFeatures { - // The Wireframe requires NonFillPolygonMode feature - features: vec![WgpuFeature::NonFillPolygonMode], - }, + // Required for wireframes. + features: WgpuFeatures::POLYGON_MODE_LINE, + ..Default::default() + }) + .insert_resource(AmbientLight { + brightness: 0.3, ..Default::default() }) .add_plugins(DefaultPlugins) @@ -398,7 +398,7 @@ impl TestbedApp { } } -impl<'a, 'b, 'c, 'd> TestbedGraphics<'a, 'b, 'c, 'd> { +impl<'a, 'b, 'c, 'd, 'e, 'f> TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f> { pub fn set_body_color(&mut self, body: RigidBodyHandle, color: [f32; 3]) { self.graphics .set_body_color(&mut self.materials, body, color); @@ -443,7 +443,7 @@ impl<'a, 'b, 'c, 'd> TestbedGraphics<'a, 'b, 'c, 'd> { } } -impl<'a, 'b, 'c, 'd> Testbed<'a, 'b, 'c, 'd> { +impl<'a, 'b, 'c, 'd, 'e, 'f> Testbed<'a, 'b, 'c, 'd, 'e, 'f> { pub fn set_number_of_steps_per_frame(&mut self, nsteps: usize) { self.state.nsteps = nsteps } @@ -603,9 +603,7 @@ impl<'a, 'b, 'c, 'd> Testbed<'a, 'b, 'c, 'd> { } pub fn add_plugin(&mut self, mut plugin: impl TestbedPlugin + 'static) { - if let Some(gfx) = &mut self.graphics { - plugin.init_plugin(gfx.pipelines, gfx.shaders); - } + plugin.init_plugin(); self.plugins.0.push(Box::new(plugin)); } @@ -815,24 +813,31 @@ fn draw_contacts(_nf: &NarrowPhase, _colliders: &ColliderSet) { #[cfg(feature = "dim3")] fn setup_graphics_environment(mut commands: Commands) { - let lights = [ - Vec3::new(100.0, 100.0, 100.0), - Vec3::new(100.0, 100.0, -100.0), - Vec3::new(-100.0, 100.0, -100.0), - Vec3::new(-100.0, 100.0, 100.0), - ]; - - for light in lights.iter() { - commands.spawn_bundle(LightBundle { - transform: Transform::from_translation(*light), - light: Light { - intensity: 30_000.0, - range: 3_000_000.0, + const HALF_SIZE: f32 = 100.0; + + commands.spawn_bundle(DirectionalLightBundle { + directional_light: DirectionalLight { + illuminance: 10000.0, + // Configure the projection to better fit the scene + shadow_projection: OrthographicProjection { + left: -HALF_SIZE, + right: HALF_SIZE, + bottom: -HALF_SIZE, + top: HALF_SIZE, + near: -10.0 * HALF_SIZE, + far: 100.0 * HALF_SIZE, ..Default::default() }, + shadows_enabled: true, ..Default::default() - }); - } + }, + transform: Transform { + translation: Vec3::new(10.0, 2.0, 10.0), + rotation: Quat::from_rotation_x(-std::f32::consts::FRAC_PI_4), + ..Default::default() + }, + ..Default::default() + }); commands .spawn_bundle(PerspectiveCameraBundle { @@ -851,15 +856,19 @@ fn setup_graphics_environment(mut commands: Commands) { #[cfg(feature = "dim2")] fn setup_graphics_environment(mut commands: Commands) { - commands.spawn_bundle(LightBundle { - transform: Transform::from_translation(Vec3::new(0.0, 0.0, 2000.0)), - light: Light { - intensity: 100_000_000.0, - range: 6000.0, - ..Default::default() - }, - ..Default::default() - }); + // commands.insert_resource(AmbientLight { + // brightness: 0.3, + // ..Default::default() + // }); + // commands.spawn_bundle(LightBundle { + // transform: Transform::from_translation(Vec3::new(0.0, 0.0, 2000.0)), + // light: Light { + // intensity: 100_000_000.0, + // range: 6000.0, + // ..Default::default() + // }, + // ..Default::default() + // }); commands .spawn_bundle(OrthographicCameraBundle { transform: Transform { @@ -889,10 +898,9 @@ fn egui_focus(ui_context: Res<EguiContext>, mut cameras: Query<&mut OrbitCamera> fn update_testbed( mut commands: Commands, windows: Res<Windows>, - mut pipelines: ResMut<Assets<PipelineDescriptor>>, - mut shaders: ResMut<Assets<Shader>>, + // mut pipelines: ResMut<Assets<RenderPipelineDescriptor>>, mut meshes: ResMut<Assets<Mesh>>, - mut materials: ResMut<Assets<StandardMaterial>>, + mut materials: ResMut<Assets<BevyMaterial>>, builders: NonSendMut<SceneBuilders>, mut graphics: NonSendMut<GraphicsManager>, mut state: ResMut<TestbedState>, @@ -911,8 +919,6 @@ fn update_testbed( // Handle inputs { let graphics_context = TestbedGraphics { - pipelines: &mut *pipelines, - shaders: &mut *shaders, graphics: &mut *graphics, commands: &mut commands, meshes: &mut *meshes, @@ -1000,8 +1006,6 @@ fn update_testbed( let meshes = &mut *meshes; let graphics_context = TestbedGraphics { - pipelines: &mut *pipelines, - shaders: &mut *shaders, graphics: &mut *graphics, commands: &mut commands, meshes: &mut *meshes, @@ -1149,8 +1153,6 @@ fn update_testbed( let graphics = &mut graphics; let mut testbed_graphics = TestbedGraphics { - pipelines: &mut *pipelines, - shaders: &mut *shaders, graphics: &mut *graphics, commands: &mut commands, meshes: &mut *meshes, @@ -1262,7 +1264,7 @@ fn clear( #[cfg(feature = "dim2")] fn highlight_hovered_body( - _materials: &mut Assets<StandardMaterial>, + _materials: &mut Assets<BevyMaterial>, _graphics_manager: &mut GraphicsManager, _testbed_state: &mut TestbedState, _physics: &PhysicsState, @@ -1275,7 +1277,7 @@ fn highlight_hovered_body( #[cfg(feature = "dim3")] fn highlight_hovered_body( - materials: &mut Assets<StandardMaterial>, + materials: &mut Assets<BevyMaterial>, graphics_manager: &mut GraphicsManager, testbed_state: &mut TestbedState, physics: &PhysicsState, @@ -1294,8 +1296,8 @@ fn highlight_hovered_body( if let Some(cursor) = window.cursor_position() { let ndc_cursor = (cursor / Vec2::new(window.width(), window.height()) * 2.0) - Vec2::ONE; let ndc_to_world = camera_transform.compute_matrix() * camera.projection_matrix.inverse(); - let ray_pt1 = ndc_to_world.project_point3(Vec3::new(ndc_cursor.x, ndc_cursor.y, 0.0)); - let ray_pt2 = ndc_to_world.project_point3(Vec3::new(ndc_cursor.x, ndc_cursor.y, 0.1)); + let ray_pt1 = ndc_to_world.project_point3(Vec3::new(ndc_cursor.x, ndc_cursor.y, -1.0)); + let ray_pt2 = ndc_to_world.project_point3(Vec3::new(ndc_cursor.x, ndc_cursor.y, 1.0)); let ray_dir = ray_pt2 - ray_pt1; let ray_origin = Point3::new(ray_pt1.x, ray_pt1.y, ray_pt1.z); let ray_dir = Vector3::new(ray_dir.x, ray_dir.y, ray_dir.z); |
