From 3a6c6c2adde98973828be10508031143dc8926da Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Sun, 15 Jan 2023 09:11:52 +0100 Subject: [PATCH] Picking support to alpha mask, transparency 3d Signed-off-by: Torstein Grindvik --- .../src/core_3d/main_pass_3d_node.rs | 29 ++++++++++++++----- crates/bevy_pbr/src/render/mesh.rs | 1 - examples/3d/transparency_3d.rs | 16 ++++++---- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs b/crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs index bf42a535713923..79826cc7641e60 100644 --- a/crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs +++ b/crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs @@ -94,6 +94,7 @@ impl Node for MainPass3dNode { if let Some(picking_textures) = picking_textures { color_attachments.push(Some(picking_textures.get_color_attachment(Operations { // If the wish is to not clear the screen, don't clear the picking buffer either. + // This may happen in situations such as a split screen game. load: match camera_3d.clear_color { ClearColorConfig::None => LoadOp::Load, _ => LoadOp::Clear(PickingTextures::clear_color()), @@ -132,13 +133,19 @@ impl Node for MainPass3dNode { #[cfg(feature = "trace")] let _main_alpha_mask_pass_3d_span = info_span!("main_alpha_mask_pass_3d").entered(); + let operations = Operations { + load: LoadOp::Load, + store: true, + }; + let mut color_attachments = vec![Some(target.get_color_attachment(operations))]; + if let Some(picking_textures) = picking_textures { + color_attachments.push(Some(picking_textures.get_color_attachment(operations))); + } + let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor { label: Some("main_alpha_mask_pass_3d"), // NOTE: The alpha_mask pass loads the color buffer as well as overwriting it where appropriate. - color_attachments: &[Some(target.get_color_attachment(Operations { - load: LoadOp::Load, - store: true, - }))], + color_attachments: &color_attachments, depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { view: &depth.view, // NOTE: The alpha mask pass loads the depth buffer and possibly overwrites it @@ -163,13 +170,19 @@ impl Node for MainPass3dNode { #[cfg(feature = "trace")] let _main_transparent_pass_3d_span = info_span!("main_transparent_pass_3d").entered(); + let operations = Operations { + load: LoadOp::Load, + store: true, + }; + let mut color_attachments = vec![Some(target.get_color_attachment(operations))]; + if let Some(picking_textures) = picking_textures { + color_attachments.push(Some(picking_textures.get_color_attachment(operations))); + } + let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor { label: Some("main_transparent_pass_3d"), // NOTE: The transparent pass loads the color buffer as well as overwriting it where appropriate. - color_attachments: &[Some(target.get_color_attachment(Operations { - load: LoadOp::Load, - store: true, - }))], + color_attachments: &color_attachments, depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { view: &depth.view, // NOTE: For the transparent pass we load the depth buffer. There should be no diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 96e2488a877e0f..61bf821a682867 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -666,7 +666,6 @@ impl SpecializedMeshPipeline for MeshPipeline { targets.push(Some(ColorTargetState { format: PICKING_TEXTURE_FORMAT, - // TODO: Check that we support both pipelines blend, write_mask: ColorWrites::ALL, })); diff --git a/examples/3d/transparency_3d.rs b/examples/3d/transparency_3d.rs index 437c291589f2d3..2879803b4181ee 100644 --- a/examples/3d/transparency_3d.rs +++ b/examples/3d/transparency_3d.rs @@ -2,11 +2,12 @@ //! Shows the effects of different blend modes. //! The `fade_transparency` system smoothly changes the transparency over time. -use bevy::prelude::*; +use bevy::{prelude::*, render::picking::Picking}; fn main() { App::new() - .insert_resource(Msaa { samples: 4 }) + // .insert_resource(Msaa { samples: 4 }) + .insert_resource(Msaa { samples: 1 }) .add_plugins(DefaultPlugins) .add_startup_system(setup) .add_system(fade_transparency) @@ -99,10 +100,13 @@ fn setup( ..default() }); // camera - commands.spawn(Camera3dBundle { - transform: Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), - ..default() - }); + commands.spawn(( + Camera3dBundle { + transform: Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), + ..default() + }, + Picking::default(), + )); } /// Fades the alpha channel of all materials between 0 and 1 over time.