Suppress the `clippy::type_complexity` lint (#8313)
# Objective
The clippy lint `type_complexity` is known not to play well with bevy.
It frequently triggers when writing complex queries, and taking the
lint's advice of using a type alias almost always just obfuscates the
code with no benefit. Because of this, this lint is currently ignored in
CI, but unfortunately it still shows up when viewing bevy code in an
IDE.
As someone who's made a fair amount of pull requests to this repo, I
will say that this issue has been a consistent thorn in my side. Since
bevy code is filled with spurious, ignorable warnings, it can be very
difficult to spot the *real* warnings that must be fixed -- most of the
time I just ignore all warnings, only to later find out that one of them
was real after I'm done when CI runs.
## Solution
Suppress this lint in all bevy crates. This was previously attempted in
#7050, but the review process ended up making it more complicated than
it needs to be and landed on a subpar solution.
The discussion in https://github.com/rust-lang/rust-clippy/pull/10571
explores some better long-term solutions to this problem. Since there is
no timeline on when these solutions may land, we should resolve this
issue in the meantime by locally suppressing these lints.
### Unresolved issues
Currently, these lints are not suppressed in our examples, since that
would require suppressing the lint in every single source file. They are
still ignored in CI.
2023-04-06 21:27:36 +00:00
|
|
|
#![allow(clippy::type_complexity)]
|
2023-03-28 20:58:02 +00:00
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
|
|
|
//! This crate adds an immediate mode drawing api to Bevy for visual debugging.
|
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//! ```
|
|
|
|
//! # use bevy_gizmos::prelude::*;
|
|
|
|
//! # use bevy_render::prelude::*;
|
|
|
|
//! # use bevy_math::prelude::*;
|
|
|
|
//! fn system(mut gizmos: Gizmos) {
|
|
|
|
//! gizmos.line(Vec3::ZERO, Vec3::X, Color::GREEN);
|
|
|
|
//! }
|
|
|
|
//! # bevy_ecs::system::assert_is_system(system);
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! See the documentation on [`Gizmos`](crate::gizmos::Gizmos) for more examples.
|
|
|
|
|
2023-03-20 20:57:54 +00:00
|
|
|
use std::mem;
|
|
|
|
|
2023-04-24 15:23:06 +00:00
|
|
|
use bevy_app::{Last, Plugin, Update};
|
2023-03-20 20:57:54 +00:00
|
|
|
use bevy_asset::{load_internal_asset, Assets, Handle, HandleUntyped};
|
|
|
|
use bevy_ecs::{
|
2023-04-24 15:23:06 +00:00
|
|
|
change_detection::DetectChanges,
|
|
|
|
component::Component,
|
|
|
|
entity::Entity,
|
|
|
|
query::Without,
|
|
|
|
reflect::ReflectComponent,
|
2023-03-20 20:57:54 +00:00
|
|
|
schedule::IntoSystemConfigs,
|
2023-04-24 15:23:06 +00:00
|
|
|
system::{Commands, Query, Res, ResMut, Resource},
|
2023-03-20 20:57:54 +00:00
|
|
|
world::{FromWorld, World},
|
|
|
|
};
|
|
|
|
use bevy_math::Mat4;
|
2023-04-24 15:23:06 +00:00
|
|
|
use bevy_reflect::{
|
|
|
|
std_traits::ReflectDefault, FromReflect, Reflect, ReflectFromReflect, TypeUuid,
|
|
|
|
};
|
2023-03-20 20:57:54 +00:00
|
|
|
use bevy_render::{
|
2023-04-24 15:23:06 +00:00
|
|
|
color::Color,
|
2023-03-20 20:57:54 +00:00
|
|
|
mesh::Mesh,
|
2023-04-24 15:23:06 +00:00
|
|
|
primitives::Aabb,
|
2023-03-20 20:57:54 +00:00
|
|
|
render_phase::AddRenderCommand,
|
|
|
|
render_resource::{PrimitiveTopology, Shader, SpecializedMeshPipelines},
|
|
|
|
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
|
|
|
|
};
|
2023-04-24 15:23:06 +00:00
|
|
|
use bevy_transform::components::{GlobalTransform, Transform};
|
2023-03-20 20:57:54 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "bevy_pbr")]
|
|
|
|
use bevy_pbr::MeshUniform;
|
|
|
|
#[cfg(feature = "bevy_sprite")]
|
|
|
|
use bevy_sprite::{Mesh2dHandle, Mesh2dUniform};
|
|
|
|
|
|
|
|
pub mod gizmos;
|
|
|
|
|
|
|
|
#[cfg(feature = "bevy_sprite")]
|
|
|
|
mod pipeline_2d;
|
|
|
|
#[cfg(feature = "bevy_pbr")]
|
|
|
|
mod pipeline_3d;
|
|
|
|
|
2023-04-24 15:23:06 +00:00
|
|
|
use gizmos::{GizmoStorage, Gizmos};
|
2023-03-20 20:57:54 +00:00
|
|
|
|
|
|
|
/// The `bevy_gizmos` prelude.
|
|
|
|
pub mod prelude {
|
|
|
|
#[doc(hidden)]
|
2023-04-24 15:23:06 +00:00
|
|
|
pub use crate::{gizmos::Gizmos, AabbGizmo, AabbGizmoConfig, GizmoConfig};
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const LINE_SHADER_HANDLE: HandleUntyped =
|
|
|
|
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 7414812689238026784);
|
|
|
|
|
2023-03-28 20:58:02 +00:00
|
|
|
/// A [`Plugin`] that provides an immediate mode drawing api for visual debugging.
|
2023-03-20 20:57:54 +00:00
|
|
|
pub struct GizmoPlugin;
|
|
|
|
|
|
|
|
impl Plugin for GizmoPlugin {
|
|
|
|
fn build(&self, app: &mut bevy_app::App) {
|
|
|
|
load_internal_asset!(app, LINE_SHADER_HANDLE, "lines.wgsl", Shader::from_wgsl);
|
|
|
|
|
|
|
|
app.init_resource::<MeshHandles>()
|
|
|
|
.init_resource::<GizmoConfig>()
|
|
|
|
.init_resource::<GizmoStorage>()
|
2023-04-24 15:23:06 +00:00
|
|
|
.add_systems(Last, update_gizmo_meshes)
|
|
|
|
.add_systems(
|
|
|
|
Update,
|
|
|
|
(
|
|
|
|
draw_aabbs,
|
|
|
|
draw_all_aabbs.run_if(|config: Res<GizmoConfig>| config.aabb.draw_all),
|
|
|
|
),
|
|
|
|
);
|
2023-03-20 20:57:54 +00:00
|
|
|
|
|
|
|
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { return; };
|
|
|
|
|
|
|
|
render_app.add_systems(ExtractSchedule, extract_gizmo_data);
|
|
|
|
|
|
|
|
#[cfg(feature = "bevy_sprite")]
|
|
|
|
{
|
|
|
|
use bevy_core_pipeline::core_2d::Transparent2d;
|
|
|
|
use pipeline_2d::*;
|
|
|
|
|
|
|
|
render_app
|
|
|
|
.add_render_command::<Transparent2d, DrawGizmoLines>()
|
|
|
|
.init_resource::<SpecializedMeshPipelines<GizmoLinePipeline>>()
|
|
|
|
.add_systems(Render, queue_gizmos_2d.in_set(RenderSet::Queue));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "bevy_pbr")]
|
|
|
|
{
|
|
|
|
use bevy_core_pipeline::core_3d::Opaque3d;
|
|
|
|
use pipeline_3d::*;
|
|
|
|
|
|
|
|
render_app
|
|
|
|
.add_render_command::<Opaque3d, DrawGizmoLines>()
|
|
|
|
.init_resource::<SpecializedMeshPipelines<GizmoPipeline>>()
|
|
|
|
.add_systems(Render, queue_gizmos_3d.in_set(RenderSet::Queue));
|
|
|
|
}
|
|
|
|
}
|
Webgpu support (#8336)
# Objective
- Support WebGPU
- alternative to #5027 that doesn't need any async / await
- fixes #8315
- Surprise fix #7318
## Solution
### For async renderer initialisation
- Update the plugin lifecycle:
- app builds the plugin
- calls `plugin.build`
- registers the plugin
- app starts the event loop
- event loop waits for `ready` of all registered plugins in the same
order
- returns `true` by default
- then call all `finish` then all `cleanup` in the same order as
registered
- then execute the schedule
In the case of the renderer, to avoid anything async:
- building the renderer plugin creates a detached task that will send
back the initialised renderer through a mutex in a resource
- `ready` will wait for the renderer to be present in the resource
- `finish` will take that renderer and place it in the expected
resources by other plugins
- other plugins (that expect the renderer to be available) `finish` are
called and they are able to set up their pipelines
- `cleanup` is called, only custom one is still for pipeline rendering
### For WebGPU support
- update the `build-wasm-example` script to support passing `--api
webgpu` that will build the example with WebGPU support
- feature for webgl2 was always enabled when building for wasm. it's now
in the default feature list and enabled on all platforms, so check for
this feature must also check that the target_arch is `wasm32`
---
## Migration Guide
- `Plugin::setup` has been renamed `Plugin::cleanup`
- `Plugin::finish` has been added, and plugins adding pipelines should
do it in this function instead of `Plugin::build`
```rust
// Before
impl Plugin for MyPlugin {
fn build(&self, app: &mut App) {
app.insert_resource::<MyResource>
.add_systems(Update, my_system);
let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Err(_) => return,
};
render_app
.init_resource::<RenderResourceNeedingDevice>()
.init_resource::<OtherRenderResource>();
}
}
// After
impl Plugin for MyPlugin {
fn build(&self, app: &mut App) {
app.insert_resource::<MyResource>
.add_systems(Update, my_system);
let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Err(_) => return,
};
render_app
.init_resource::<OtherRenderResource>();
}
fn finish(&self, app: &mut App) {
let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Err(_) => return,
};
render_app
.init_resource::<RenderResourceNeedingDevice>();
}
}
```
2023-05-04 22:07:57 +00:00
|
|
|
|
|
|
|
fn finish(&self, app: &mut bevy_app::App) {
|
|
|
|
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { return; };
|
|
|
|
|
|
|
|
#[cfg(feature = "bevy_sprite")]
|
|
|
|
{
|
|
|
|
use pipeline_2d::*;
|
|
|
|
|
|
|
|
render_app.init_resource::<GizmoLinePipeline>();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "bevy_pbr")]
|
|
|
|
{
|
|
|
|
use pipeline_3d::*;
|
|
|
|
|
|
|
|
render_app.init_resource::<GizmoPipeline>();
|
|
|
|
}
|
|
|
|
}
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:58:02 +00:00
|
|
|
/// A [`Resource`] that stores configuration for gizmos.
|
2023-04-24 15:23:06 +00:00
|
|
|
#[derive(Resource, Clone)]
|
2023-03-20 20:57:54 +00:00
|
|
|
pub struct GizmoConfig {
|
|
|
|
/// Set to `false` to stop drawing gizmos.
|
|
|
|
///
|
|
|
|
/// Defaults to `true`.
|
|
|
|
pub enabled: bool,
|
|
|
|
/// Draw gizmos on top of everything else, ignoring depth.
|
|
|
|
///
|
|
|
|
/// This setting only affects 3D. In 2D, gizmos are always drawn on top.
|
|
|
|
///
|
|
|
|
/// Defaults to `false`.
|
|
|
|
pub on_top: bool,
|
2023-04-24 15:23:06 +00:00
|
|
|
/// Configuration for the [`AabbGizmo`].
|
|
|
|
pub aabb: AabbGizmoConfig,
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GizmoConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
enabled: true,
|
|
|
|
on_top: false,
|
2023-04-24 15:23:06 +00:00
|
|
|
aabb: Default::default(),
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:23:06 +00:00
|
|
|
/// Configuration for drawing the [`Aabb`] component on entities.
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct AabbGizmoConfig {
|
|
|
|
/// Draws all bounding boxes in the scene when set to `true`.
|
|
|
|
///
|
|
|
|
/// To draw a specific entity's bounding box, you can add the [`AabbGizmo`] component.
|
|
|
|
///
|
|
|
|
/// Defaults to `false`.
|
|
|
|
pub draw_all: bool,
|
|
|
|
/// The default color for bounding box gizmos.
|
|
|
|
///
|
|
|
|
/// A random color is chosen per box if `None`.
|
|
|
|
///
|
|
|
|
/// Defaults to `None`.
|
|
|
|
pub default_color: Option<Color>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add this [`Component`] to an entity to draw its [`Aabb`] component.
|
|
|
|
#[derive(Component, Reflect, FromReflect, Default, Debug)]
|
|
|
|
#[reflect(Component, FromReflect, Default)]
|
|
|
|
pub struct AabbGizmo {
|
|
|
|
/// The color of the box.
|
|
|
|
///
|
|
|
|
/// The default color from the [`GizmoConfig`] resource is used if `None`,
|
|
|
|
pub color: Option<Color>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_aabbs(
|
|
|
|
query: Query<(Entity, &Aabb, &GlobalTransform, &AabbGizmo)>,
|
|
|
|
config: Res<GizmoConfig>,
|
|
|
|
mut gizmos: Gizmos,
|
|
|
|
) {
|
|
|
|
for (entity, &aabb, &transform, gizmo) in &query {
|
|
|
|
let color = gizmo
|
|
|
|
.color
|
|
|
|
.or(config.aabb.default_color)
|
|
|
|
.unwrap_or_else(|| color_from_entity(entity));
|
|
|
|
gizmos.cuboid(aabb_transform(aabb, transform), color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_all_aabbs(
|
|
|
|
query: Query<(Entity, &Aabb, &GlobalTransform), Without<AabbGizmo>>,
|
|
|
|
config: Res<GizmoConfig>,
|
|
|
|
mut gizmos: Gizmos,
|
|
|
|
) {
|
|
|
|
for (entity, &aabb, &transform) in &query {
|
|
|
|
let color = config
|
|
|
|
.aabb
|
|
|
|
.default_color
|
|
|
|
.unwrap_or_else(|| color_from_entity(entity));
|
|
|
|
gizmos.cuboid(aabb_transform(aabb, transform), color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn color_from_entity(entity: Entity) -> Color {
|
|
|
|
let hue = entity.to_bits() as f32 * 100_000. % 360.;
|
|
|
|
Color::hsl(hue, 1., 0.5)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn aabb_transform(aabb: Aabb, transform: GlobalTransform) -> GlobalTransform {
|
|
|
|
transform
|
|
|
|
* GlobalTransform::from(
|
|
|
|
Transform::from_translation(aabb.center.into())
|
|
|
|
.with_scale((aabb.half_extents * 2.).into()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-20 20:57:54 +00:00
|
|
|
#[derive(Resource)]
|
|
|
|
struct MeshHandles {
|
2023-04-18 16:31:55 +00:00
|
|
|
list: Option<Handle<Mesh>>,
|
|
|
|
strip: Option<Handle<Mesh>>,
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromWorld for MeshHandles {
|
2023-04-18 16:31:55 +00:00
|
|
|
fn from_world(_world: &mut World) -> Self {
|
2023-03-20 20:57:54 +00:00
|
|
|
MeshHandles {
|
2023-04-18 16:31:55 +00:00
|
|
|
list: None,
|
|
|
|
strip: None,
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct GizmoMesh;
|
|
|
|
|
|
|
|
fn update_gizmo_meshes(
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
2023-04-18 16:31:55 +00:00
|
|
|
mut handles: ResMut<MeshHandles>,
|
2023-03-20 20:57:54 +00:00
|
|
|
mut storage: ResMut<GizmoStorage>,
|
|
|
|
) {
|
2023-04-18 16:31:55 +00:00
|
|
|
if storage.list_positions.is_empty() {
|
|
|
|
handles.list = None;
|
|
|
|
} else if let Some(handle) = handles.list.as_ref() {
|
|
|
|
let list_mesh = meshes.get_mut(handle).unwrap();
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-04-18 16:31:55 +00:00
|
|
|
let positions = mem::take(&mut storage.list_positions);
|
|
|
|
list_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-04-18 16:31:55 +00:00
|
|
|
let colors = mem::take(&mut storage.list_colors);
|
|
|
|
list_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
|
|
|
|
} else {
|
|
|
|
let mut list_mesh = Mesh::new(PrimitiveTopology::LineList);
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-04-17 21:20:29 +00:00
|
|
|
let positions = mem::take(&mut storage.list_positions);
|
|
|
|
list_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-04-17 21:20:29 +00:00
|
|
|
let colors = mem::take(&mut storage.list_colors);
|
|
|
|
list_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
|
2023-04-18 16:31:55 +00:00
|
|
|
|
|
|
|
handles.list = Some(meshes.add(list_mesh));
|
2023-04-17 21:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:31:55 +00:00
|
|
|
if storage.strip_positions.is_empty() {
|
|
|
|
handles.strip = None;
|
|
|
|
} else if let Some(handle) = handles.strip.as_ref() {
|
|
|
|
let strip_mesh = meshes.get_mut(handle).unwrap();
|
2023-04-17 21:20:29 +00:00
|
|
|
|
2023-04-18 16:31:55 +00:00
|
|
|
let positions = mem::take(&mut storage.strip_positions);
|
|
|
|
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
|
|
|
|
|
|
|
|
let colors = mem::take(&mut storage.strip_colors);
|
|
|
|
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
|
|
|
|
} else {
|
|
|
|
let mut strip_mesh = Mesh::new(PrimitiveTopology::LineStrip);
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-04-17 21:20:29 +00:00
|
|
|
let positions = mem::take(&mut storage.strip_positions);
|
|
|
|
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
|
|
|
|
|
|
|
|
let colors = mem::take(&mut storage.strip_colors);
|
|
|
|
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
|
2023-04-18 16:31:55 +00:00
|
|
|
|
|
|
|
handles.strip = Some(meshes.add(strip_mesh));
|
2023-04-17 21:20:29 +00:00
|
|
|
}
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_gizmo_data(
|
|
|
|
mut commands: Commands,
|
|
|
|
handles: Extract<Res<MeshHandles>>,
|
|
|
|
config: Extract<Res<GizmoConfig>>,
|
|
|
|
) {
|
|
|
|
if config.is_changed() {
|
2023-04-24 15:23:06 +00:00
|
|
|
commands.insert_resource(config.clone());
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:31:55 +00:00
|
|
|
if !config.enabled {
|
2023-03-20 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let transform = Mat4::IDENTITY;
|
|
|
|
let inverse_transpose_model = transform.inverse().transpose();
|
2023-04-18 16:31:55 +00:00
|
|
|
commands.spawn_batch(
|
|
|
|
[handles.list.clone(), handles.strip.clone()]
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.map(move |handle| {
|
|
|
|
(
|
|
|
|
GizmoMesh,
|
|
|
|
#[cfg(feature = "bevy_pbr")]
|
|
|
|
(
|
|
|
|
handle.clone_weak(),
|
|
|
|
MeshUniform {
|
|
|
|
flags: 0,
|
|
|
|
transform,
|
|
|
|
previous_transform: transform,
|
|
|
|
inverse_transpose_model,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
#[cfg(feature = "bevy_sprite")]
|
|
|
|
(
|
|
|
|
Mesh2dHandle(handle),
|
|
|
|
Mesh2dUniform {
|
|
|
|
flags: 0,
|
|
|
|
transform,
|
|
|
|
inverse_transpose_model,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
);
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|