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;
|
|
|
|
|
|
|
|
use bevy_app::{Last, Plugin};
|
|
|
|
use bevy_asset::{load_internal_asset, Assets, Handle, HandleUntyped};
|
|
|
|
use bevy_ecs::{
|
|
|
|
prelude::{Component, DetectChanges},
|
|
|
|
schedule::IntoSystemConfigs,
|
|
|
|
system::{Commands, Res, ResMut, Resource},
|
|
|
|
world::{FromWorld, World},
|
|
|
|
};
|
|
|
|
use bevy_math::Mat4;
|
|
|
|
use bevy_reflect::TypeUuid;
|
|
|
|
use bevy_render::{
|
|
|
|
mesh::Mesh,
|
|
|
|
render_phase::AddRenderCommand,
|
|
|
|
render_resource::{PrimitiveTopology, Shader, SpecializedMeshPipelines},
|
|
|
|
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[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;
|
|
|
|
|
|
|
|
use crate::gizmos::GizmoStorage;
|
|
|
|
|
|
|
|
/// The `bevy_gizmos` prelude.
|
|
|
|
pub mod prelude {
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub use crate::{gizmos::Gizmos, GizmoConfig};
|
|
|
|
}
|
|
|
|
|
|
|
|
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>()
|
|
|
|
.add_systems(Last, update_gizmo_meshes);
|
|
|
|
|
|
|
|
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::<GizmoLinePipeline>()
|
|
|
|
.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::<GizmoPipeline>()
|
|
|
|
.init_resource::<SpecializedMeshPipelines<GizmoPipeline>>()
|
|
|
|
.add_systems(Render, queue_gizmos_3d.in_set(RenderSet::Queue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:58:02 +00:00
|
|
|
/// A [`Resource`] that stores configuration for gizmos.
|
2023-03-20 20:57:54 +00:00
|
|
|
#[derive(Resource, Clone, Copy)]
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GizmoConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
enabled: true,
|
|
|
|
on_top: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Resource)]
|
|
|
|
struct MeshHandles {
|
|
|
|
list: Handle<Mesh>,
|
|
|
|
strip: Handle<Mesh>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromWorld for MeshHandles {
|
|
|
|
fn from_world(world: &mut World) -> Self {
|
|
|
|
let mut meshes = world.resource_mut::<Assets<Mesh>>();
|
|
|
|
|
|
|
|
MeshHandles {
|
|
|
|
list: meshes.add(Mesh::new(PrimitiveTopology::LineList)),
|
|
|
|
strip: meshes.add(Mesh::new(PrimitiveTopology::LineStrip)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct GizmoMesh;
|
|
|
|
|
|
|
|
fn update_gizmo_meshes(
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
handles: Res<MeshHandles>,
|
|
|
|
mut storage: ResMut<GizmoStorage>,
|
|
|
|
) {
|
|
|
|
let list_mesh = meshes.get_mut(&handles.list).unwrap();
|
|
|
|
|
2023-04-17 21:20:29 +00:00
|
|
|
storage.in_use = false;
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-04-17 21:20:29 +00:00
|
|
|
if !storage.list_positions.is_empty() {
|
|
|
|
storage.in_use = true;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !storage.strip_positions.is_empty() {
|
|
|
|
storage.in_use = true;
|
|
|
|
|
|
|
|
let strip_mesh = meshes.get_mut(&handles.strip).unwrap();
|
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-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_gizmo_data(
|
|
|
|
mut commands: Commands,
|
|
|
|
handles: Extract<Res<MeshHandles>>,
|
|
|
|
config: Extract<Res<GizmoConfig>>,
|
2023-04-17 21:20:29 +00:00
|
|
|
storage: Extract<Res<GizmoStorage>>,
|
2023-03-20 20:57:54 +00:00
|
|
|
) {
|
|
|
|
if config.is_changed() {
|
|
|
|
commands.insert_resource(**config);
|
|
|
|
}
|
|
|
|
|
2023-04-17 21:20:29 +00:00
|
|
|
if !config.enabled || !storage.in_use {
|
2023-03-20 20:57:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let transform = Mat4::IDENTITY;
|
|
|
|
let inverse_transpose_model = transform.inverse().transpose();
|
|
|
|
commands.spawn_batch([&handles.list, &handles.strip].map(|handle| {
|
|
|
|
(
|
|
|
|
GizmoMesh,
|
|
|
|
#[cfg(feature = "bevy_pbr")]
|
|
|
|
(
|
2023-04-17 21:20:29 +00:00
|
|
|
handle.clone_weak(),
|
2023-03-20 20:57:54 +00:00
|
|
|
MeshUniform {
|
|
|
|
flags: 0,
|
|
|
|
transform,
|
Temporal Antialiasing (TAA) (#7291)
![image](https://user-images.githubusercontent.com/47158642/214374911-412f0986-3927-4f7a-9a6c-413bdee6b389.png)
# Objective
- Implement an alternative antialias technique
- TAA scales based off of view resolution, not geometry complexity
- TAA filters textures, firefly pixels, and other aliasing not covered
by MSAA
- TAA additionally will reduce noise / increase quality in future
stochastic rendering techniques
- Closes https://github.com/bevyengine/bevy/issues/3663
## Solution
- Add a temporal jitter component
- Add a motion vector prepass
- Add a TemporalAntialias component and plugin
- Combine existing MSAA and FXAA examples and add TAA
## Followup Work
- Prepass motion vector support for skinned meshes
- Move uniforms needed for motion vectors into a separate bind group,
instead of using different bind group layouts
- Reuse previous frame's GPU view buffer for motion vectors, instead of
recomputing
- Mip biasing for sharper textures, and or unjitter texture UVs
https://github.com/bevyengine/bevy/issues/7323
- Compute shader for better performance
- Investigate FSR techniques
- Historical depth based disocclusion tests, for geometry disocclusion
- Historical luminance/hue based tests, for shading disocclusion
- Pixel "locks" to reduce blending rate / revamp history confidence
mechanism
- Orthographic camera support for TemporalJitter
- Figure out COD's 1-tap bicubic filter
---
## Changelog
- Added MotionVectorPrepass and TemporalJitter
- Added TemporalAntialiasPlugin, TemporalAntialiasBundle, and
TemporalAntialiasSettings
---------
Co-authored-by: IceSentry <c.giguere42@gmail.com>
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
Co-authored-by: Robert Swain <robert.swain@gmail.com>
Co-authored-by: Daniel Chia <danstryder@gmail.com>
Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com>
Co-authored-by: Brandon Dyer <brandondyer64@gmail.com>
Co-authored-by: Edgar Geier <geieredgar@gmail.com>
2023-03-27 22:22:40 +00:00
|
|
|
previous_transform: transform,
|
2023-03-20 20:57:54 +00:00
|
|
|
inverse_transpose_model,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
#[cfg(feature = "bevy_sprite")]
|
|
|
|
(
|
2023-04-17 21:20:29 +00:00
|
|
|
Mesh2dHandle(handle.clone_weak()),
|
2023-03-20 20:57:54 +00:00
|
|
|
Mesh2dUniform {
|
|
|
|
flags: 0,
|
|
|
|
transform,
|
|
|
|
inverse_transpose_model,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}));
|
|
|
|
}
|