*This PR description is an edited copy of #5007, written by @alice-i-cecile.* # Objective Follow-up to https://github.com/bevyengine/bevy/pull/2254. The `Resource` trait currently has a blanket implementation for all types that meet its bounds. While ergonomic, this results in several drawbacks: * it is possible to make confusing, silent mistakes such as inserting a function pointer (Foo) rather than a value (Foo::Bar) as a resource * it is challenging to discover if a type is intended to be used as a resource * we cannot later add customization options (see the [RFC](https://github.com/bevyengine/rfcs/blob/main/rfcs/27-derive-component.md) for the equivalent choice for Component). * dependencies can use the same Rust type as a resource in invisibly conflicting ways * raw Rust types used as resources cannot preserve privacy appropriately, as anyone able to access that type can read and write to internal values * we cannot capture a definitive list of possible resources to display to users in an editor ## Notes to reviewers * Review this commit-by-commit; there's effectively no back-tracking and there's a lot of churn in some of these commits. *ira: My commits are not as well organized :')* * I've relaxed the bound on Local to Send + Sync + 'static: I don't think these concerns apply there, so this can keep things simple. Storing e.g. a u32 in a Local is fine, because there's a variable name attached explaining what it does. * I think this is a bad place for the Resource trait to live, but I've left it in place to make reviewing easier. IMO that's best tackled with https://github.com/bevyengine/bevy/issues/4981. ## Changelog `Resource` is no longer automatically implemented for all matching types. Instead, use the new `#[derive(Resource)]` macro. ## Migration Guide Add `#[derive(Resource)]` to all types you are using as a resource. If you are using a third party type as a resource, wrap it in a tuple struct to bypass orphan rules. Consider deriving `Deref` and `DerefMut` to improve ergonomics. `ClearColor` no longer implements `Component`. Using `ClearColor` as a component in 0.8 did nothing. Use the `ClearColorConfig` in the `Camera3d` and `Camera2d` components instead. Co-authored-by: Alice <alice.i.cecile@gmail.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: devil-ira <justthecooldude@gmail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
177 lines
6.3 KiB
Rust
177 lines
6.3 KiB
Rust
use crate::MeshPipeline;
|
|
use crate::{DrawMesh, MeshPipelineKey, MeshUniform, SetMeshBindGroup, SetMeshViewBindGroup};
|
|
use bevy_app::Plugin;
|
|
use bevy_asset::{load_internal_asset, Handle, HandleUntyped};
|
|
use bevy_core_pipeline::core_3d::Opaque3d;
|
|
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
|
|
use bevy_reflect::std_traits::ReflectDefault;
|
|
use bevy_reflect::{Reflect, TypeUuid};
|
|
use bevy_render::Extract;
|
|
use bevy_render::{
|
|
extract_resource::{ExtractResource, ExtractResourcePlugin},
|
|
mesh::{Mesh, MeshVertexBufferLayout},
|
|
render_asset::RenderAssets,
|
|
render_phase::{AddRenderCommand, DrawFunctions, RenderPhase, SetItemPipeline},
|
|
render_resource::{
|
|
PipelineCache, PolygonMode, RenderPipelineDescriptor, Shader, SpecializedMeshPipeline,
|
|
SpecializedMeshPipelineError, SpecializedMeshPipelines,
|
|
},
|
|
view::{ExtractedView, Msaa, VisibleEntities},
|
|
RenderApp, RenderStage,
|
|
};
|
|
use bevy_utils::tracing::error;
|
|
|
|
pub const WIREFRAME_SHADER_HANDLE: HandleUntyped =
|
|
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 192598014480025766);
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct WireframePlugin;
|
|
|
|
impl Plugin for WireframePlugin {
|
|
fn build(&self, app: &mut bevy_app::App) {
|
|
load_internal_asset!(
|
|
app,
|
|
WIREFRAME_SHADER_HANDLE,
|
|
"render/wireframe.wgsl",
|
|
Shader::from_wgsl
|
|
);
|
|
|
|
app.register_type::<WireframeConfig>()
|
|
.init_resource::<WireframeConfig>()
|
|
.add_plugin(ExtractResourcePlugin::<WireframeConfig>::default());
|
|
|
|
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
|
|
render_app
|
|
.add_render_command::<Opaque3d, DrawWireframes>()
|
|
.init_resource::<WireframePipeline>()
|
|
.init_resource::<SpecializedMeshPipelines<WireframePipeline>>()
|
|
.add_system_to_stage(RenderStage::Extract, extract_wireframes)
|
|
.add_system_to_stage(RenderStage::Queue, queue_wireframes);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn extract_wireframes(mut commands: Commands, query: Extract<Query<Entity, With<Wireframe>>>) {
|
|
for entity in query.iter() {
|
|
commands.get_or_spawn(entity).insert(Wireframe);
|
|
}
|
|
}
|
|
|
|
/// Controls whether an entity should rendered in wireframe-mode if the [`WireframePlugin`] is enabled
|
|
#[derive(Component, Debug, Clone, Default, Reflect)]
|
|
#[reflect(Component, Default)]
|
|
pub struct Wireframe;
|
|
|
|
#[derive(Resource, Debug, Clone, Default, ExtractResource, Reflect)]
|
|
#[reflect(Resource)]
|
|
pub struct WireframeConfig {
|
|
/// Whether to show wireframes for all meshes. If `false`, only meshes with a [Wireframe] component will be rendered.
|
|
pub global: bool,
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
pub struct WireframePipeline {
|
|
mesh_pipeline: MeshPipeline,
|
|
shader: Handle<Shader>,
|
|
}
|
|
impl FromWorld for WireframePipeline {
|
|
fn from_world(render_world: &mut World) -> Self {
|
|
WireframePipeline {
|
|
mesh_pipeline: render_world.resource::<MeshPipeline>().clone(),
|
|
shader: WIREFRAME_SHADER_HANDLE.typed(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl SpecializedMeshPipeline for WireframePipeline {
|
|
type Key = MeshPipelineKey;
|
|
|
|
fn specialize(
|
|
&self,
|
|
key: Self::Key,
|
|
layout: &MeshVertexBufferLayout,
|
|
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
|
|
let mut descriptor = self.mesh_pipeline.specialize(key, layout)?;
|
|
descriptor.vertex.shader = self.shader.clone_weak();
|
|
descriptor.fragment.as_mut().unwrap().shader = self.shader.clone_weak();
|
|
descriptor.primitive.polygon_mode = PolygonMode::Line;
|
|
descriptor.depth_stencil.as_mut().unwrap().bias.slope_scale = 1.0;
|
|
Ok(descriptor)
|
|
}
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn queue_wireframes(
|
|
opaque_3d_draw_functions: Res<DrawFunctions<Opaque3d>>,
|
|
render_meshes: Res<RenderAssets<Mesh>>,
|
|
wireframe_config: Res<WireframeConfig>,
|
|
wireframe_pipeline: Res<WireframePipeline>,
|
|
mut pipelines: ResMut<SpecializedMeshPipelines<WireframePipeline>>,
|
|
mut pipeline_cache: ResMut<PipelineCache>,
|
|
msaa: Res<Msaa>,
|
|
mut material_meshes: ParamSet<(
|
|
Query<(Entity, &Handle<Mesh>, &MeshUniform)>,
|
|
Query<(Entity, &Handle<Mesh>, &MeshUniform), With<Wireframe>>,
|
|
)>,
|
|
mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase<Opaque3d>)>,
|
|
) {
|
|
let draw_custom = opaque_3d_draw_functions
|
|
.read()
|
|
.get_id::<DrawWireframes>()
|
|
.unwrap();
|
|
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
|
|
for (view, visible_entities, mut opaque_phase) in &mut views {
|
|
let rangefinder = view.rangefinder3d();
|
|
|
|
let add_render_phase =
|
|
|(entity, mesh_handle, mesh_uniform): (Entity, &Handle<Mesh>, &MeshUniform)| {
|
|
if let Some(mesh) = render_meshes.get(mesh_handle) {
|
|
let key = msaa_key
|
|
| MeshPipelineKey::from_primitive_topology(mesh.primitive_topology);
|
|
let pipeline_id = pipelines.specialize(
|
|
&mut pipeline_cache,
|
|
&wireframe_pipeline,
|
|
key,
|
|
&mesh.layout,
|
|
);
|
|
let pipeline_id = match pipeline_id {
|
|
Ok(id) => id,
|
|
Err(err) => {
|
|
error!("{}", err);
|
|
return;
|
|
}
|
|
};
|
|
opaque_phase.add(Opaque3d {
|
|
entity,
|
|
pipeline: pipeline_id,
|
|
draw_function: draw_custom,
|
|
distance: rangefinder.distance(&mesh_uniform.transform),
|
|
});
|
|
}
|
|
};
|
|
|
|
if wireframe_config.global {
|
|
let query = material_meshes.p0();
|
|
visible_entities
|
|
.entities
|
|
.iter()
|
|
.filter_map(|visible_entity| query.get(*visible_entity).ok())
|
|
.for_each(add_render_phase);
|
|
} else {
|
|
let query = material_meshes.p1();
|
|
visible_entities
|
|
.entities
|
|
.iter()
|
|
.filter_map(|visible_entity| query.get(*visible_entity).ok())
|
|
.for_each(add_render_phase);
|
|
}
|
|
}
|
|
}
|
|
|
|
type DrawWireframes = (
|
|
SetItemPipeline,
|
|
SetMeshViewBindGroup<0>,
|
|
SetMeshBindGroup<1>,
|
|
DrawMesh,
|
|
);
|