feat: update to parry 0.21
This commit is contained in:
committed by
Sébastien Crozet
parent
b798e1942d
commit
ef47848fba
@@ -1,6 +1,6 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use na::{point, Point3};
|
||||
use na::{point, Point3, Point4};
|
||||
|
||||
use crate::objects::node::EntityWithGraphics;
|
||||
use rapier::dynamics::{RigidBodyHandle, RigidBodySet};
|
||||
@@ -27,8 +27,60 @@ pub type BevyMaterialComponent = MeshMaterial2d<BevyMaterial>;
|
||||
#[cfg(feature = "dim3")]
|
||||
pub type BevyMaterialComponent = MeshMaterial3d<BevyMaterial>;
|
||||
|
||||
pub type InstancedMaterials = HashMap<Point3<usize>, Handle<BevyMaterial>>;
|
||||
pub const SELECTED_OBJECT_MATERIAL_KEY: Point3<usize> = point![42, 42, 42];
|
||||
#[derive(Clone, Default)]
|
||||
pub struct InstancedMaterials {
|
||||
cache: HashMap<Point4<usize>, Handle<BevyMaterial>>,
|
||||
}
|
||||
|
||||
impl InstancedMaterials {
|
||||
pub fn insert(
|
||||
&mut self,
|
||||
materials: &mut Assets<BevyMaterial>,
|
||||
color: Point3<f32>,
|
||||
opacity: f32,
|
||||
) -> Handle<BevyMaterial> {
|
||||
let key = color
|
||||
.coords
|
||||
.push(opacity)
|
||||
.map(|c| (c * 255.0) as usize)
|
||||
.into();
|
||||
let bevy_color = Color::from(Srgba::new(color.x, color.y, color.z, opacity));
|
||||
|
||||
#[cfg(feature = "dim2")]
|
||||
let material = bevy_sprite::ColorMaterial {
|
||||
color: bevy_color,
|
||||
texture: None,
|
||||
..default()
|
||||
};
|
||||
#[cfg(feature = "dim3")]
|
||||
let material = StandardMaterial {
|
||||
metallic: 0.5,
|
||||
perceptual_roughness: 0.5,
|
||||
double_sided: true, // TODO: this doesn't do anything?
|
||||
..StandardMaterial::from(bevy_color)
|
||||
};
|
||||
|
||||
self.cache
|
||||
.entry(key)
|
||||
.or_insert_with(|| materials.add(material))
|
||||
.clone_weak()
|
||||
}
|
||||
|
||||
pub fn get(&self, color: &Point3<f32>, opacity: f32) -> Option<Handle<BevyMaterial>> {
|
||||
let key = color
|
||||
.coords
|
||||
.push(opacity)
|
||||
.map(|c| (c * 255.0) as usize)
|
||||
.into();
|
||||
self.cache.get(&key).map(|h| h.clone_weak())
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
pub const SELECTED_OBJECT_COLOR: Point3<f32> = point![1.0, 0.0, 0.0];
|
||||
|
||||
pub struct GraphicsManager {
|
||||
rand: Pcg32,
|
||||
@@ -52,13 +104,16 @@ impl GraphicsManager {
|
||||
ground_color: point![0.5, 0.5, 0.5],
|
||||
b2wireframe: HashMap::new(),
|
||||
prefab_meshes: HashMap::new(),
|
||||
instanced_materials: HashMap::new(),
|
||||
instanced_materials: Default::default(),
|
||||
gfx_shift: Vector::zeros(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selection_material(&self) -> Handle<BevyMaterial> {
|
||||
self.instanced_materials[&SELECTED_OBJECT_MATERIAL_KEY].clone_weak()
|
||||
self.instanced_materials
|
||||
.get(&SELECTED_OBJECT_COLOR, 1.0)
|
||||
.unwrap()
|
||||
.clone_weak()
|
||||
}
|
||||
|
||||
pub fn clear(&mut self, commands: &mut Commands) {
|
||||
@@ -110,6 +165,7 @@ impl GraphicsManager {
|
||||
pub fn set_body_color(
|
||||
&mut self,
|
||||
materials: &mut Assets<BevyMaterial>,
|
||||
material_handles: &mut Query<&mut BevyMaterialComponent>,
|
||||
b: RigidBodyHandle,
|
||||
color: [f32; 3],
|
||||
) {
|
||||
@@ -117,7 +173,11 @@ impl GraphicsManager {
|
||||
|
||||
if let Some(ns) = self.b2sn.get_mut(&b) {
|
||||
for n in ns.iter_mut() {
|
||||
n.set_color(materials, color.into())
|
||||
n.set_color(materials, &mut self.instanced_materials, color.into());
|
||||
|
||||
if let Ok(mut mat) = material_handles.get_mut(n.entity) {
|
||||
mat.0 = n.material.clone_weak();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -185,12 +245,7 @@ impl GraphicsManager {
|
||||
color
|
||||
}
|
||||
|
||||
fn alloc_color(
|
||||
&mut self,
|
||||
materials: &mut Assets<BevyMaterial>,
|
||||
handle: RigidBodyHandle,
|
||||
is_fixed: bool,
|
||||
) -> Point3<f32> {
|
||||
fn alloc_color(&mut self, handle: RigidBodyHandle, is_fixed: bool) -> Point3<f32> {
|
||||
let mut color = self.ground_color;
|
||||
|
||||
if !is_fixed {
|
||||
@@ -200,7 +255,7 @@ impl GraphicsManager {
|
||||
}
|
||||
}
|
||||
|
||||
self.set_body_color(materials, handle, color.into());
|
||||
self.b2color.insert(handle, color.into());
|
||||
|
||||
color
|
||||
}
|
||||
@@ -221,7 +276,7 @@ impl GraphicsManager {
|
||||
.b2color
|
||||
.get(&handle)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| self.alloc_color(materials, handle, !body.is_dynamic()));
|
||||
.unwrap_or_else(|| self.alloc_color(handle, !body.is_dynamic()));
|
||||
|
||||
let _ = self.add_body_colliders_with_color(
|
||||
commands, meshes, materials, components, handle, bodies, colliders, color,
|
||||
|
||||
@@ -179,7 +179,7 @@ impl Harness {
|
||||
self.physics.hooks = Box::new(hooks);
|
||||
|
||||
self.physics.islands = IslandManager::new();
|
||||
self.physics.broad_phase = DefaultBroadPhase::new();
|
||||
self.physics.broad_phase = DefaultBroadPhase::default();
|
||||
self.physics.narrow_phase = NarrowPhase::new();
|
||||
self.state.timestep_id = 0;
|
||||
self.state.time = 0.0;
|
||||
|
||||
@@ -14,7 +14,7 @@ use rapier::geometry::{ColliderHandle, ColliderSet, Shape, ShapeType};
|
||||
use rapier::geometry::{Cone, Cylinder};
|
||||
use rapier::math::{Isometry, Real, Vector};
|
||||
|
||||
use crate::graphics::{BevyMaterial, InstancedMaterials, SELECTED_OBJECT_MATERIAL_KEY};
|
||||
use crate::graphics::{BevyMaterial, InstancedMaterials, SELECTED_OBJECT_COLOR};
|
||||
#[cfg(feature = "dim2")]
|
||||
use {
|
||||
na::{vector, Point2, Vector2},
|
||||
@@ -37,28 +37,7 @@ impl EntityWithGraphics {
|
||||
materials: &mut Assets<BevyMaterial>,
|
||||
instanced_materials: &mut InstancedMaterials,
|
||||
) {
|
||||
if instanced_materials.contains_key(&SELECTED_OBJECT_MATERIAL_KEY) {
|
||||
return; // Already added.
|
||||
}
|
||||
|
||||
#[cfg(feature = "dim2")]
|
||||
let selection_material = bevy_sprite::ColorMaterial {
|
||||
color: Color::from(Srgba::rgb(1.0, 0.0, 0.0)),
|
||||
texture: None,
|
||||
..default()
|
||||
};
|
||||
#[cfg(feature = "dim3")]
|
||||
let selection_material = StandardMaterial {
|
||||
metallic: 0.5,
|
||||
perceptual_roughness: 0.5,
|
||||
double_sided: true, // TODO: this doesn't do anything?
|
||||
..StandardMaterial::from(Color::from(Srgba::rgb(1.0, 0.0, 0.0)))
|
||||
};
|
||||
|
||||
instanced_materials.insert(
|
||||
SELECTED_OBJECT_MATERIAL_KEY,
|
||||
materials.add(selection_material),
|
||||
);
|
||||
instanced_materials.insert(materials, SELECTED_OBJECT_COLOR, 1.0);
|
||||
}
|
||||
|
||||
pub fn spawn(
|
||||
@@ -84,8 +63,7 @@ impl EntityWithGraphics {
|
||||
.cloned()
|
||||
.or_else(|| generate_collider_mesh(shape).map(|m| meshes.add(m)));
|
||||
|
||||
let opacity = 1.0;
|
||||
let bevy_color = Color::from(Srgba::new(color.x, color.y, color.z, opacity));
|
||||
let opacity = if sensor { 0.25 } else { 1.0 };
|
||||
let shape_pos = collider_pos * delta;
|
||||
let mut transform = Transform::from_scale(scale);
|
||||
transform.translation.x = shape_pos.translation.vector.x as f32;
|
||||
@@ -108,23 +86,7 @@ impl EntityWithGraphics {
|
||||
transform.rotation = Quat::from_rotation_z(shape_pos.rotation.angle() as f32);
|
||||
}
|
||||
|
||||
#[cfg(feature = "dim2")]
|
||||
let material = bevy_sprite::ColorMaterial {
|
||||
color: bevy_color,
|
||||
texture: None,
|
||||
..default()
|
||||
};
|
||||
#[cfg(feature = "dim3")]
|
||||
let material = StandardMaterial {
|
||||
metallic: 0.5,
|
||||
perceptual_roughness: 0.5,
|
||||
double_sided: true, // TODO: this doesn't do anything?
|
||||
..StandardMaterial::from(bevy_color)
|
||||
};
|
||||
let material_handle = instanced_materials
|
||||
.entry(color.coords.map(|c| (c * 255.0) as usize).into())
|
||||
.or_insert_with(|| materials.add(material));
|
||||
let material_weak_handle = material_handle.clone_weak();
|
||||
let material_handle = instanced_materials.insert(materials, color, opacity);
|
||||
|
||||
if let Some(mesh) = mesh {
|
||||
#[cfg(feature = "dim2")]
|
||||
@@ -154,7 +116,7 @@ impl EntityWithGraphics {
|
||||
base_color: color,
|
||||
collider,
|
||||
delta,
|
||||
material: material_weak_handle,
|
||||
material: material_handle,
|
||||
opacity,
|
||||
}
|
||||
}
|
||||
@@ -164,18 +126,13 @@ impl EntityWithGraphics {
|
||||
commands.entity(self.entity).despawn();
|
||||
}
|
||||
|
||||
pub fn set_color(&mut self, materials: &mut Assets<BevyMaterial>, color: Point3<f32>) {
|
||||
if let Some(material) = materials.get_mut(&self.material) {
|
||||
#[cfg(feature = "dim2")]
|
||||
{
|
||||
material.color = Color::from(Srgba::new(color.x, color.y, color.z, self.opacity));
|
||||
}
|
||||
#[cfg(feature = "dim3")]
|
||||
{
|
||||
material.base_color =
|
||||
Color::from(Srgba::new(color.x, color.y, color.z, self.opacity));
|
||||
}
|
||||
}
|
||||
pub fn set_color(
|
||||
&mut self,
|
||||
materials: &mut Assets<BevyMaterial>,
|
||||
instanced_materials: &mut InstancedMaterials,
|
||||
color: Point3<f32>,
|
||||
) {
|
||||
self.material = instanced_materials.insert(materials, color, self.opacity);
|
||||
self.color = color;
|
||||
self.base_color = color;
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ impl PhysicsState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
islands: IslandManager::new(),
|
||||
broad_phase: DefaultBroadPhase::new(),
|
||||
broad_phase: DefaultBroadPhase::default(),
|
||||
narrow_phase: NarrowPhase::new(),
|
||||
bodies: RigidBodySet::new(),
|
||||
colliders: ColliderSet::new(),
|
||||
|
||||
@@ -178,11 +178,12 @@ struct OtherBackends {
|
||||
}
|
||||
struct Plugins(Vec<Box<dyn TestbedPlugin>>);
|
||||
|
||||
pub struct TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h> {
|
||||
pub struct TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k> {
|
||||
graphics: &'a mut GraphicsManager,
|
||||
commands: &'a mut Commands<'d, 'e>,
|
||||
meshes: &'a mut Assets<Mesh>,
|
||||
materials: &'a mut Assets<BevyMaterial>,
|
||||
material_handles: &'a mut Query<'i, 'j, &'k mut BevyMaterialComponent>,
|
||||
components: &'a mut Query<'b, 'f, &'c mut Transform>,
|
||||
#[allow(dead_code)] // Dead in 2D but not in 3D.
|
||||
camera_transform: GlobalTransform,
|
||||
@@ -192,8 +193,8 @@ pub struct TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h> {
|
||||
mouse: &'a SceneMouse,
|
||||
}
|
||||
|
||||
pub struct Testbed<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h> {
|
||||
graphics: Option<TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h>>,
|
||||
pub struct Testbed<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k> {
|
||||
graphics: Option<TestbedGraphics<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k>>,
|
||||
harness: &'a mut Harness,
|
||||
state: &'a mut TestbedState,
|
||||
#[cfg(feature = "other-backends")]
|
||||
@@ -508,9 +509,10 @@ impl TestbedApp {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'g, 'h> TestbedGraphics<'_, '_, '_, '_, '_, '_, 'g, 'h> {
|
||||
impl<'g, 'h> TestbedGraphics<'_, '_, '_, '_, '_, '_, 'g, 'h, '_, '_, '_> {
|
||||
pub fn set_body_color(&mut self, body: RigidBodyHandle, color: [f32; 3]) {
|
||||
self.graphics.set_body_color(self.materials, body, color);
|
||||
self.graphics
|
||||
.set_body_color(self.materials, self.material_handles, body, color);
|
||||
}
|
||||
|
||||
pub fn ui_context_mut(&mut self) -> &mut EguiContexts<'g, 'h> {
|
||||
@@ -585,7 +587,7 @@ impl<'g, 'h> TestbedGraphics<'_, '_, '_, '_, '_, '_, 'g, 'h> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Testbed<'_, '_, '_, '_, '_, '_, '_, '_> {
|
||||
impl Testbed<'_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_> {
|
||||
pub fn set_number_of_steps_per_frame(&mut self, nsteps: usize) {
|
||||
self.state.nsteps = nsteps
|
||||
}
|
||||
@@ -1131,6 +1133,7 @@ fn update_testbed(
|
||||
commands: &mut commands,
|
||||
meshes: &mut *meshes,
|
||||
materials: &mut *materials,
|
||||
material_handles: &mut material_handles,
|
||||
components: &mut gfx_components,
|
||||
camera_transform: *cameras.single().1,
|
||||
camera: &mut cameras.single_mut().2,
|
||||
@@ -1246,6 +1249,7 @@ fn update_testbed(
|
||||
commands: &mut commands,
|
||||
meshes: &mut *meshes,
|
||||
materials: &mut *materials,
|
||||
material_handles: &mut material_handles,
|
||||
components: &mut gfx_components,
|
||||
camera_transform: *cameras.single().1,
|
||||
camera: &mut cameras.single_mut().2,
|
||||
@@ -1421,6 +1425,7 @@ fn update_testbed(
|
||||
commands: &mut commands,
|
||||
meshes: &mut *meshes,
|
||||
materials: &mut *materials,
|
||||
material_handles: &mut material_handles,
|
||||
components: &mut gfx_components,
|
||||
camera_transform: *cameras.single().1,
|
||||
camera: &mut cameras.single_mut().2,
|
||||
|
||||
Reference in New Issue
Block a user