Skip to content

Commit

Permalink
No depth in 2D
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-blackbird committed Nov 11, 2022
1 parent f6262c6 commit ce474fd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_debug_draw/src/debuglines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn vertex(in: VertexInput) -> VertexOutput {
fn fragment(in: VertexOutput) -> FragmentOutput {
var out: FragmentOutput;

#ifdef DEPTH_TEST_ENABLED
#ifdef DEPTH_TEST
out.depth = in.pos.z;
#else
out.depth = 1.0;
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_debug_draw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Plugin for DebugDrawPlugin {

app.init_resource::<DebugDraw>()
.init_resource::<DebugDrawConfig>()
.add_system_to_stage(CoreStage::PostUpdate, update)
.add_system_to_stage(CoreStage::Last, update)
.sub_app_mut(RenderApp)
.add_system_to_stage(RenderStage::Extract, extract);

Expand Down Expand Up @@ -82,6 +82,8 @@ pub struct DebugDrawConfig {
/// Defaults to `true`.
pub enabled: bool,
/// Whether debug drawing should ignore depth and draw on top of everything else.
///
/// This setting only affects 3D. In 2D, debug drawing is always on top.
///
/// Defaults to `true`.
pub always_on_top: bool,
Expand Down
51 changes: 22 additions & 29 deletions crates/bevy_debug_draw/src/pipeline_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bevy_render::{
use bevy_sprite::*;
use bevy_utils::FloatOrd;

use crate::{DebugDrawConfig, DebugDrawMesh, SHADER_HANDLE};
use crate::{DebugDrawMesh, SHADER_HANDLE};

#[derive(Resource)]
pub(crate) struct DebugLinePipeline {
Expand Down Expand Up @@ -93,47 +93,40 @@ pub(crate) type DrawDebugLines = (

#[allow(clippy::too_many_arguments)]
pub(crate) fn queue(
config: Res<DebugDrawConfig>,
draw2d_functions: Res<DrawFunctions<Transparent2d>>,
debug_line_pipeline: Res<DebugLinePipeline>,
mut pipeline_cache: ResMut<PipelineCache>,
mut specialized_pipelines: ResMut<SpecializedMeshPipelines<DebugLinePipeline>>,
render_meshes: Res<RenderAssets<Mesh>>,
msaa: Res<Msaa>,
material_meshes: Query<(&Mesh2dUniform, &Mesh2dHandle), With<DebugDrawMesh>>,
material_meshes: Query<&Mesh2dHandle, With<DebugDrawMesh>>,
mut views: Query<(&VisibleEntities, &mut RenderPhase<Transparent2d>)>,
) {
for (view, mut phase) in &mut views {
let draw_mesh2d = draw2d_functions.read().get_id::<DrawDebugLines>().unwrap();
let msaa_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples);

for visible_entity in &view.entities {
if let Ok((uniform, mesh_handle)) = material_meshes.get(*visible_entity) {
if let Some(mesh) = render_meshes.get(&mesh_handle.0) {
let mesh_key = msaa_key
| Mesh2dPipelineKey::from_primitive_topology(PrimitiveTopology::LineList);
let mesh_z = if config.always_on_top {
f32::MAX
} else {
uniform.transform.w_axis.z
};
let pipeline = specialized_pipelines
.specialize(
&mut pipeline_cache,
&debug_line_pipeline,
mesh_key,
&mesh.layout,
)
.unwrap();
phase.add(Transparent2d {
entity: *visible_entity,
draw_function: draw_mesh2d,
pipeline,
sort_key: FloatOrd(mesh_z),
batch_range: None,
});
}
}
let Ok(mesh_handle) = material_meshes.get(*visible_entity) else { continue; };
let Some(mesh) = render_meshes.get(&mesh_handle.0) else { continue; };

let mesh_key =
msaa_key | Mesh2dPipelineKey::from_primitive_topology(PrimitiveTopology::LineList);
let pipeline = specialized_pipelines
.specialize(
&mut pipeline_cache,
&debug_line_pipeline,
mesh_key,
&mesh.layout,
)
.unwrap();
phase.add(Transparent2d {
entity: *visible_entity,
draw_function: draw_mesh2d,
pipeline,
sort_key: FloatOrd(f32::MAX),
batch_range: None,
});
}
}
}
2 changes: 1 addition & 1 deletion crates/bevy_debug_draw/src/pipeline_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl SpecializedMeshPipeline for DebugLinePipeline {
let mut shader_defs = Vec::new();
shader_defs.push("DEBUG_LINES_3D".to_string());
if depth_test {
shader_defs.push("DEPTH_TEST_ENABLED".to_string());
shader_defs.push("DEPTH_TEST".to_string());
}

let vertex_buffer_layout = layout.get_layout(&[
Expand Down

0 comments on commit ce474fd

Please sign in to comment.