add opacity to EntityWithGraphics

This commit is contained in:
rezural
2021-07-06 07:38:50 +10:00
committed by Sébastien Crozet
parent 9f8d9769f8
commit 53700db860
2 changed files with 10 additions and 4 deletions

View File

@@ -27,7 +27,7 @@ pub use crate::graphics::GraphicsManager;
pub use crate::harness::plugin::HarnessPlugin; pub use crate::harness::plugin::HarnessPlugin;
pub use crate::physics::PhysicsState; pub use crate::physics::PhysicsState;
pub use crate::plugin::TestbedPlugin; pub use crate::plugin::TestbedPlugin;
pub use crate::testbed::{Testbed, TestbedApp, TestbedGraphics}; pub use crate::testbed::{Testbed, TestbedApp, TestbedGraphics, TestbedState};
#[cfg(all(feature = "dim2", feature = "other-backends"))] #[cfg(all(feature = "dim2", feature = "other-backends"))]
mod box2d_backend; mod box2d_backend;

View File

@@ -11,6 +11,7 @@ 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;
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
use { use {
na::{Point2, Vector2}, na::{Point2, Vector2},
@@ -24,6 +25,7 @@ pub struct EntityWithGraphics {
pub base_color: Point3<f32>, pub base_color: Point3<f32>,
pub collider: ColliderHandle, pub collider: ColliderHandle,
pub delta: Isometry<f32>, pub delta: Isometry<f32>,
pub opacity: f32,
material: Handle<StandardMaterial>, material: Handle<StandardMaterial>,
} }
@@ -49,7 +51,8 @@ impl EntityWithGraphics {
.or_else(|| generate_collider_mesh(shape).map(|m| meshes.add(m))) .or_else(|| generate_collider_mesh(shape).map(|m| meshes.add(m)))
.expect("Could not build the collider's render mesh"); .expect("Could not build the collider's render mesh");
let bevy_color = Color::rgb(color.x, color.y, color.z); let opacity = 1.0;
let bevy_color = Color::rgba(color.x, color.y, color.z, opacity);
let shape_pos = collider_pos * delta; let shape_pos = collider_pos * delta;
let mut transform = Transform::from_scale(scale); let mut transform = Transform::from_scale(scale);
transform.translation.x = shape_pos.translation.vector.x; transform.translation.x = shape_pos.translation.vector.x;
@@ -101,6 +104,7 @@ impl EntityWithGraphics {
collider, collider,
delta, delta,
material: material_weak_handle, material: material_weak_handle,
opacity,
} }
} }
@@ -108,12 +112,14 @@ impl EntityWithGraphics {
//FIXME: Should this be despawn_recursive? //FIXME: Should this be despawn_recursive?
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<StandardMaterial>) {
// 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 = Color::rgb(self.color.x, self.color.y, self.color.z); material.base_color =
Color::rgba(self.color.x, self.color.y, self.color.z, self.opacity);
} }
} }
@@ -123,7 +129,7 @@ impl EntityWithGraphics {
pub fn set_color(&mut self, materials: &mut Assets<StandardMaterial>, color: Point3<f32>) { pub fn set_color(&mut self, materials: &mut Assets<StandardMaterial>, 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::rgb(color.x, color.y, color.z); 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;