Update the testbed to use bevy 0.6

This commit is contained in:
Sébastien Crozet
2022-01-09 17:25:17 +01:00
parent 6627f7193b
commit a0d197e691
9 changed files with 144 additions and 105 deletions

View File

@@ -39,16 +39,17 @@ bincode = "1"
Inflector = "0.11" Inflector = "0.11"
md5 = "0.7" md5 = "0.7"
bevy_egui = "0.5" bevy_egui = "0.10"
bevy_ecs = "0.6"
# Dependencies for native only. # Dependencies for native only.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
bevy = {version = "0.5", default-features = false, features = ["bevy_wgpu", "bevy_winit", "render", "x11"]} bevy = {version = "0.6", default-features = false, features = ["bevy_winit", "render", "x11"]}
# Dependencies for WASM only. # Dependencies for WASM only.
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
bevy = {version = "0.5", default-features = false, features = ["bevy_winit", "render"]} bevy = {version = "0.6", default-features = false, features = ["bevy_winit", "render"]}
bevy_webgl2 = "0.5" #bevy_webgl2 = "0.5"
[dependencies.rapier2d] [dependencies.rapier2d]
path = "../rapier2d" path = "../rapier2d"

View File

@@ -41,16 +41,17 @@ md5 = "0.7"
Inflector = "0.11" Inflector = "0.11"
serde = { version = "1", features = [ "derive" ] } serde = { version = "1", features = [ "derive" ] }
bevy_egui = "0.5" bevy_egui = "0.10"
bevy_ecs = "0.6"
# Dependencies for native only. # Dependencies for native only.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
bevy = {version = "0.5", default-features = false, features = ["bevy_wgpu", "bevy_winit", "render", "x11"]} bevy = {version = "0.6", default-features = false, features = ["bevy_winit", "render", "x11"]}
# Dependencies for WASM only. # Dependencies for WASM only.
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
bevy = {version = "0.5", default-features = false, features = ["bevy_winit", "render"]} bevy = {version = "0.6", default-features = false, features = ["bevy_winit", "render"]}
bevy_webgl2 = "0.5" #bevy_webgl2 = "0.5"
[dependencies.rapier3d] [dependencies.rapier3d]
path = "../rapier3d" path = "../rapier3d"

View File

@@ -52,13 +52,13 @@ pub fn init_world(testbed: &mut Testbed) {
let handle = bodies.insert(rigid_body); let handle = bodies.insert(rigid_body);
let collider = match j % 5 { let collider = match j % 5 {
_ => ColliderBuilder::cuboid(rad, rad, rad).build(), // _ => ColliderBuilder::cuboid(rad, rad, rad).build(),
// 1 => ColliderBuilder::ball(rad).build(), 1 => ColliderBuilder::ball(rad).build(),
// // Rounded cylinders are much more efficient that cylinder, even if the // Rounded cylinders are much more efficient that cylinder, even if the
// // rounding margin is small. // rounding margin is small.
// 2 => ColliderBuilder::round_cylinder(rad, rad, rad / 10.0).build(), 2 => ColliderBuilder::round_cylinder(rad, rad, rad / 10.0).build(),
// 3 => ColliderBuilder::cone(rad, rad).build(), 3 => ColliderBuilder::cone(rad, rad).build(),
// _ => ColliderBuilder::capsule_y(rad, rad).build(), _ => ColliderBuilder::capsule_y(rad, rad).build(),
}; };
colliders.insert_with_parent(collider, handle, &mut bodies); colliders.insert_with_parent(collider, handle, &mut bodies);

View File

@@ -9,6 +9,7 @@ use bevy::render::camera::Camera;
const LINE_TO_PIXEL_RATIO: f32 = 0.1; const LINE_TO_PIXEL_RATIO: f32 = 0.1;
#[derive(Component)]
pub struct OrbitCamera { pub struct OrbitCamera {
pub zoom: f32, pub zoom: f32,
pub center: Vec3, pub center: Vec3,
@@ -87,7 +88,7 @@ impl OrbitCameraPlugin {
} }
} }
impl Plugin for 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()) app.add_system(Self::mouse_motion_system.system())
.add_system(Self::zoom_system.system()) .add_system(Self::zoom_system.system())
.add_system(Self::update_transform_system.system()); .add_system(Self::update_transform_system.system());

View File

@@ -11,6 +11,7 @@ use std::ops::RangeInclusive;
const LINE_TO_PIXEL_RATIO: f32 = 0.1; const LINE_TO_PIXEL_RATIO: f32 = 0.1;
#[derive(Component)]
pub struct OrbitCamera { pub struct OrbitCamera {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,
@@ -113,7 +114,7 @@ impl OrbitCameraPlugin {
} }
} }
impl Plugin for 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()) app.add_system(Self::mouse_motion_system.system())
.add_system(Self::zoom_system.system()) .add_system(Self::zoom_system.system())
.add_system(Self::update_transform_system.system()); .add_system(Self::update_transform_system.system());

View File

@@ -17,6 +17,11 @@ use rand::{Rng, SeedableRng};
use rand_pcg::Pcg32; use rand_pcg::Pcg32;
use std::collections::HashMap; use std::collections::HashMap;
#[cfg(feature = "dim2")]
pub type BevyMaterial = ColorMaterial;
#[cfg(feature = "dim3")]
pub type BevyMaterial = StandardMaterial;
pub struct GraphicsManager { pub struct GraphicsManager {
rand: Pcg32, rand: Pcg32,
b2sn: HashMap<RigidBodyHandle, Vec<EntityWithGraphics>>, b2sn: HashMap<RigidBodyHandle, Vec<EntityWithGraphics>>,
@@ -84,7 +89,7 @@ impl GraphicsManager {
pub fn set_body_color( pub fn set_body_color(
&mut self, &mut self,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
b: RigidBodyHandle, b: RigidBodyHandle,
color: [f32; 3], color: [f32; 3],
) { ) {
@@ -161,7 +166,7 @@ impl GraphicsManager {
fn alloc_color( fn alloc_color(
&mut self, &mut self,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
handle: RigidBodyHandle, handle: RigidBodyHandle,
is_static: bool, is_static: bool,
) -> Point3<f32> { ) -> Point3<f32> {
@@ -183,7 +188,7 @@ impl GraphicsManager {
&mut self, &mut self,
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
components: &mut Query<(&mut Transform,)>, components: &mut Query<(&mut Transform,)>,
handle: RigidBodyHandle, handle: RigidBodyHandle,
bodies: &RigidBodySet, bodies: &RigidBodySet,
@@ -206,7 +211,7 @@ impl GraphicsManager {
&mut self, &mut self,
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
components: &mut Query<(&mut Transform,)>, components: &mut Query<(&mut Transform,)>,
handle: RigidBodyHandle, handle: RigidBodyHandle,
bodies: &RigidBodySet, bodies: &RigidBodySet,
@@ -257,7 +262,7 @@ impl GraphicsManager {
&mut self, &mut self,
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
handle: ColliderHandle, handle: ColliderHandle,
colliders: &ColliderSet, colliders: &ColliderSet,
) { ) {
@@ -292,7 +297,7 @@ impl GraphicsManager {
&mut self, &mut self,
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
handle: Option<ColliderHandle>, handle: Option<ColliderHandle>,
shape: &dyn Shape, shape: &dyn Shape,
sensor: bool, sensor: bool,

View File

@@ -5,15 +5,17 @@ use bevy::render::mesh::{Indices, VertexAttributeValues};
use na::{point, Point3, Vector3}; use na::{point, Point3, Vector3};
use std::collections::HashMap; use std::collections::HashMap;
use bevy::render::pipeline::PrimitiveTopology; use bevy::pbr::wireframe::Wireframe;
use bevy::render::wireframe::Wireframe; use bevy::render::render_resource::PrimitiveTopology;
use rapier::geometry::{ColliderHandle, ColliderSet, Shape, ShapeType}; use rapier::geometry::{ColliderHandle, ColliderSet, Shape, ShapeType};
#[cfg(feature = "dim3")] #[cfg(feature = "dim3")]
use rapier::geometry::{Cone, Cylinder}; use rapier::geometry::{Cone, Cylinder};
use rapier::math::Isometry; use rapier::math::Isometry;
use crate::graphics::BevyMaterial;
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
use { use {
bevy::sprite::MaterialMesh2dBundle,
na::{Point2, Vector2}, na::{Point2, Vector2},
rapier::geometry::{Ball, Cuboid}, rapier::geometry::{Ball, Cuboid},
}; };
@@ -26,14 +28,14 @@ pub struct EntityWithGraphics {
pub collider: Option<ColliderHandle>, pub collider: Option<ColliderHandle>,
pub delta: Isometry<f32>, pub delta: Isometry<f32>,
pub opacity: f32, pub opacity: f32,
material: Handle<StandardMaterial>, material: Handle<BevyMaterial>,
} }
impl EntityWithGraphics { impl EntityWithGraphics {
pub fn spawn( pub fn spawn(
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
prefab_meshs: &HashMap<ShapeType, Handle<Mesh>>, prefab_meshs: &HashMap<ShapeType, Handle<Mesh>>,
shape: &dyn Shape, shape: &dyn Shape,
collider: Option<ColliderHandle>, collider: Option<ColliderHandle>,
@@ -74,9 +76,15 @@ impl EntityWithGraphics {
transform.rotation = Quat::from_rotation_z(shape_pos.rotation.angle()); 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 { let material = StandardMaterial {
metallic: 0.5, metallic: 0.5,
roughness: 0.5, perceptual_roughness: 0.5,
double_sided: true, // TODO: this doesn't do anything? double_sided: true, // TODO: this doesn't do anything?
..StandardMaterial::from(bevy_color) ..StandardMaterial::from(bevy_color)
}; };
@@ -84,7 +92,15 @@ impl EntityWithGraphics {
let material_weak_handle = material_handle.clone_weak(); let material_weak_handle = material_handle.clone_weak();
if let Some(mesh) = mesh { 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, mesh,
material: material_handle, material: material_handle,
transform, transform,
@@ -92,7 +108,7 @@ impl EntityWithGraphics {
}; };
let mut entity_commands = commands.entity(entity); let mut entity_commands = commands.entity(entity);
entity_commands.insert_bundle(pbr); entity_commands.insert_bundle(bundle);
if sensor { if sensor {
entity_commands.insert(Wireframe); entity_commands.insert(Wireframe);
@@ -115,23 +131,38 @@ impl EntityWithGraphics {
commands.entity(self.entity).despawn(); 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 // NOTE: we don't just call `self.set_color` because that would
// overwrite self.base_color too. // overwrite self.base_color too.
self.color = point![1.0, 0.0, 0.0]; self.color = point![1.0, 0.0, 0.0];
if let Some(material) = materials.get_mut(&self.material) { if let Some(material) = materials.get_mut(&self.material) {
material.base_color = #[cfg(feature = "dim2")]
Color::rgba(self.color.x, self.color.y, self.color.z, self.opacity); {
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); 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) { 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.color = color;
self.base_color = color; self.base_color = color;
@@ -336,7 +367,7 @@ fn collider_mesh_scale(co_shape: &dyn Shape) -> Vec3 {
#[cfg(feature = "dim3")] #[cfg(feature = "dim3")]
ShapeType::Cuboid => { ShapeType::Cuboid => {
let c = co_shape.as_cuboid().unwrap(); 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")] #[cfg(feature = "dim3")]
ShapeType::Cylinder => { ShapeType::Cylinder => {

View File

@@ -1,22 +1,19 @@
use crate::graphics::BevyMaterial;
use crate::harness::Harness; use crate::harness::Harness;
use crate::physics::PhysicsState; use crate::physics::PhysicsState;
use crate::GraphicsManager; use crate::GraphicsManager;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::render::pipeline::PipelineDescriptor; // use bevy::render::render_resource::RenderPipelineDescriptor;
use bevy_egui::EguiContext; use bevy_egui::EguiContext;
pub trait TestbedPlugin { pub trait TestbedPlugin {
fn init_plugin( fn init_plugin(&mut self);
&mut self,
pipelines: &mut Assets<PipelineDescriptor>,
shaders: &mut Assets<Shader>,
);
fn init_graphics( fn init_graphics(
&mut self, &mut self,
graphics: &mut GraphicsManager, graphics: &mut GraphicsManager,
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
components: &mut Query<(&mut Transform,)>, components: &mut Query<(&mut Transform,)>,
harness: &mut Harness, harness: &mut Harness,
); );
@@ -28,7 +25,7 @@ pub trait TestbedPlugin {
graphics: &mut GraphicsManager, graphics: &mut GraphicsManager,
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
components: &mut Query<(&mut Transform,)>, components: &mut Query<(&mut Transform,)>,
harness: &mut Harness, harness: &mut Harness,
); );
@@ -39,7 +36,7 @@ pub trait TestbedPlugin {
graphics: &mut GraphicsManager, graphics: &mut GraphicsManager,
commands: &mut Commands, commands: &mut Commands,
meshes: &mut Assets<Mesh>, meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
components: &mut Query<(&mut Transform,)>, components: &mut Query<(&mut Transform,)>,
); );
fn profiling_string(&self) -> String; fn profiling_string(&self) -> String;

View File

@@ -1,7 +1,6 @@
use std::env; use std::env;
use std::mem; use std::mem;
use bevy::pbr::Light;
use bevy::prelude::*; use bevy::prelude::*;
use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState}; use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState};
@@ -25,16 +24,17 @@ use crate::box2d_backend::Box2dWorld;
use crate::harness::Harness; use crate::harness::Harness;
#[cfg(all(feature = "dim3", feature = "other-backends"))] #[cfg(all(feature = "dim3", feature = "other-backends"))]
use crate::physx_backend::PhysxWorld; use crate::physx_backend::PhysxWorld;
use bevy::pbr::wireframe::WireframePlugin;
use bevy::render::camera::Camera; use bevy::render::camera::Camera;
use bevy::render::wireframe::WireframePlugin; use bevy::render::options::{WgpuFeatures, WgpuOptions};
use bevy::wgpu::{WgpuFeature, WgpuFeatures, WgpuOptions};
use bevy_egui::EguiContext; use bevy_egui::EguiContext;
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
use crate::camera2d::{OrbitCamera, OrbitCameraPlugin}; use crate::camera2d::{OrbitCamera, OrbitCameraPlugin};
#[cfg(feature = "dim3")] #[cfg(feature = "dim3")]
use crate::camera3d::{OrbitCamera, OrbitCameraPlugin}; 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; const RAPIER_BACKEND: usize = 0;
#[cfg(all(feature = "dim2", feature = "other-backends"))] #[cfg(all(feature = "dim2", feature = "other-backends"))]
@@ -128,19 +128,17 @@ struct OtherBackends {
} }
struct Plugins(Vec<Box<dyn TestbedPlugin>>); 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, graphics: &'a mut GraphicsManager,
commands: &'a mut Commands<'d>, commands: &'a mut Commands<'d, 'e>,
meshes: &'a mut Assets<Mesh>, meshes: &'a mut Assets<Mesh>,
materials: &'a mut Assets<StandardMaterial>, materials: &'a mut Assets<BevyMaterial>,
shaders: &'a mut Assets<Shader>, components: &'a mut Query<'b, 'f, (&'c mut Transform,)>,
pipelines: &'a mut Assets<PipelineDescriptor>,
components: &'a mut Query<'b, (&'c mut Transform,)>,
camera: &'a mut OrbitCamera, camera: &'a mut OrbitCamera,
} }
pub struct Testbed<'a, 'b, 'c, 'd> { pub struct Testbed<'a, 'b, 'c, 'd, 'e, 'f> {
graphics: Option<TestbedGraphics<'a, 'b, 'c, 'd>>, graphics: Option<TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f>>,
harness: &'a mut Harness, harness: &'a mut Harness,
state: &'a mut TestbedState, state: &'a mut TestbedState,
#[cfg(feature = "other-backends")] #[cfg(feature = "other-backends")]
@@ -357,7 +355,7 @@ impl TestbedApp {
"Rapier: 3D demos".to_string() "Rapier: 3D demos".to_string()
}; };
let mut app = App::build(); let mut app = App::new();
app.insert_resource(WindowDescriptor { app.insert_resource(WindowDescriptor {
title, title,
@@ -365,12 +363,14 @@ impl TestbedApp {
..Default::default() ..Default::default()
}) })
.insert_resource(ClearColor(Color::rgb(0.85, 0.85, 0.85))) .insert_resource(ClearColor(Color::rgb(0.85, 0.85, 0.85)))
.insert_resource(Msaa { samples: 2 }) .insert_resource(Msaa { samples: 4 })
.insert_resource(WgpuOptions { .insert_resource(WgpuOptions {
features: WgpuFeatures { // Required for wireframes.
// The Wireframe requires NonFillPolygonMode feature features: WgpuFeatures::POLYGON_MODE_LINE,
features: vec![WgpuFeature::NonFillPolygonMode], ..Default::default()
}, })
.insert_resource(AmbientLight {
brightness: 0.3,
..Default::default() ..Default::default()
}) })
.add_plugins(DefaultPlugins) .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]) { pub fn set_body_color(&mut self, body: RigidBodyHandle, color: [f32; 3]) {
self.graphics self.graphics
.set_body_color(&mut self.materials, body, color); .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) { pub fn set_number_of_steps_per_frame(&mut self, nsteps: usize) {
self.state.nsteps = nsteps 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) { pub fn add_plugin(&mut self, mut plugin: impl TestbedPlugin + 'static) {
if let Some(gfx) = &mut self.graphics { plugin.init_plugin();
plugin.init_plugin(gfx.pipelines, gfx.shaders);
}
self.plugins.0.push(Box::new(plugin)); self.plugins.0.push(Box::new(plugin));
} }
@@ -815,24 +813,31 @@ fn draw_contacts(_nf: &NarrowPhase, _colliders: &ColliderSet) {
#[cfg(feature = "dim3")] #[cfg(feature = "dim3")]
fn setup_graphics_environment(mut commands: Commands) { fn setup_graphics_environment(mut commands: Commands) {
let lights = [ const HALF_SIZE: f32 = 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),
Vec3::new(-100.0, 100.0, 100.0),
];
for light in lights.iter() { commands.spawn_bundle(DirectionalLightBundle {
commands.spawn_bundle(LightBundle { directional_light: DirectionalLight {
transform: Transform::from_translation(*light), illuminance: 10000.0,
light: Light { // Configure the projection to better fit the scene
intensity: 30_000.0, shadow_projection: OrthographicProjection {
range: 3_000_000.0, left: -HALF_SIZE,
right: HALF_SIZE,
bottom: -HALF_SIZE,
top: HALF_SIZE,
near: -10.0 * HALF_SIZE,
far: 100.0 * HALF_SIZE,
..Default::default() ..Default::default()
}, },
shadows_enabled: true,
..Default::default() ..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 commands
.spawn_bundle(PerspectiveCameraBundle { .spawn_bundle(PerspectiveCameraBundle {
@@ -851,15 +856,19 @@ fn setup_graphics_environment(mut commands: Commands) {
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
fn setup_graphics_environment(mut commands: Commands) { fn setup_graphics_environment(mut commands: Commands) {
commands.spawn_bundle(LightBundle { // commands.insert_resource(AmbientLight {
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 2000.0)), // brightness: 0.3,
light: Light { // ..Default::default()
intensity: 100_000_000.0, // });
range: 6000.0, // commands.spawn_bundle(LightBundle {
..Default::default() // transform: Transform::from_translation(Vec3::new(0.0, 0.0, 2000.0)),
}, // light: Light {
..Default::default() // intensity: 100_000_000.0,
}); // range: 6000.0,
// ..Default::default()
// },
// ..Default::default()
// });
commands commands
.spawn_bundle(OrthographicCameraBundle { .spawn_bundle(OrthographicCameraBundle {
transform: Transform { transform: Transform {
@@ -889,10 +898,9 @@ fn egui_focus(ui_context: Res<EguiContext>, mut cameras: Query<&mut OrbitCamera>
fn update_testbed( fn update_testbed(
mut commands: Commands, mut commands: Commands,
windows: Res<Windows>, windows: Res<Windows>,
mut pipelines: ResMut<Assets<PipelineDescriptor>>, // mut pipelines: ResMut<Assets<RenderPipelineDescriptor>>,
mut shaders: ResMut<Assets<Shader>>,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<BevyMaterial>>,
builders: NonSendMut<SceneBuilders>, builders: NonSendMut<SceneBuilders>,
mut graphics: NonSendMut<GraphicsManager>, mut graphics: NonSendMut<GraphicsManager>,
mut state: ResMut<TestbedState>, mut state: ResMut<TestbedState>,
@@ -911,8 +919,6 @@ fn update_testbed(
// Handle inputs // Handle inputs
{ {
let graphics_context = TestbedGraphics { let graphics_context = TestbedGraphics {
pipelines: &mut *pipelines,
shaders: &mut *shaders,
graphics: &mut *graphics, graphics: &mut *graphics,
commands: &mut commands, commands: &mut commands,
meshes: &mut *meshes, meshes: &mut *meshes,
@@ -1000,8 +1006,6 @@ fn update_testbed(
let meshes = &mut *meshes; let meshes = &mut *meshes;
let graphics_context = TestbedGraphics { let graphics_context = TestbedGraphics {
pipelines: &mut *pipelines,
shaders: &mut *shaders,
graphics: &mut *graphics, graphics: &mut *graphics,
commands: &mut commands, commands: &mut commands,
meshes: &mut *meshes, meshes: &mut *meshes,
@@ -1149,8 +1153,6 @@ fn update_testbed(
let graphics = &mut graphics; let graphics = &mut graphics;
let mut testbed_graphics = TestbedGraphics { let mut testbed_graphics = TestbedGraphics {
pipelines: &mut *pipelines,
shaders: &mut *shaders,
graphics: &mut *graphics, graphics: &mut *graphics,
commands: &mut commands, commands: &mut commands,
meshes: &mut *meshes, meshes: &mut *meshes,
@@ -1262,7 +1264,7 @@ fn clear(
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
fn highlight_hovered_body( fn highlight_hovered_body(
_materials: &mut Assets<StandardMaterial>, _materials: &mut Assets<BevyMaterial>,
_graphics_manager: &mut GraphicsManager, _graphics_manager: &mut GraphicsManager,
_testbed_state: &mut TestbedState, _testbed_state: &mut TestbedState,
_physics: &PhysicsState, _physics: &PhysicsState,
@@ -1275,7 +1277,7 @@ fn highlight_hovered_body(
#[cfg(feature = "dim3")] #[cfg(feature = "dim3")]
fn highlight_hovered_body( fn highlight_hovered_body(
materials: &mut Assets<StandardMaterial>, materials: &mut Assets<BevyMaterial>,
graphics_manager: &mut GraphicsManager, graphics_manager: &mut GraphicsManager,
testbed_state: &mut TestbedState, testbed_state: &mut TestbedState,
physics: &PhysicsState, physics: &PhysicsState,
@@ -1294,8 +1296,8 @@ fn highlight_hovered_body(
if let Some(cursor) = window.cursor_position() { if let Some(cursor) = window.cursor_position() {
let ndc_cursor = (cursor / Vec2::new(window.width(), window.height()) * 2.0) - Vec2::ONE; 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 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_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, 0.1)); 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_dir = ray_pt2 - ray_pt1;
let ray_origin = Point3::new(ray_pt1.x, ray_pt1.y, ray_pt1.z); 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); let ray_dir = Vector3::new(ray_dir.x, ray_dir.y, ray_dir.z);