From 8a8d01aa8805ebc5e3c779f84ddcf43b7be2d0a1 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 25 Jun 2020 15:24:27 -0700 Subject: [PATCH] render: add ClearColor resource --- Cargo.toml | 4 ++++ .../bevy_render/src/base_render_graph/mod.rs | 2 ++ crates/bevy_render/src/pass/pass.rs | 22 +++++++++++++++++++ .../src/render_graph/nodes/pass_node.rs | 13 ++++++++++- examples/window/clear_color.rs | 8 +++++++ src/prelude.rs | 1 + 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 examples/window/clear_color.rs diff --git a/Cargo.toml b/Cargo.toml index c4f68592f5..3479df334a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -187,6 +187,10 @@ path = "examples/ui/ui.rs" name = "ui_bench" path = "examples/ui/ui_bench.rs" +[[example]] +name = "clear_color" +path = "examples/window/clear_color.rs" + [[example]] name = "multiple_windows" path = "examples/window/multiple_windows.rs" \ No newline at end of file diff --git a/crates/bevy_render/src/base_render_graph/mod.rs b/crates/bevy_render/src/base_render_graph/mod.rs index f953453f0a..3a0d0a6f7f 100644 --- a/crates/bevy_render/src/base_render_graph/mod.rs +++ b/crates/bevy_render/src/base_render_graph/mod.rs @@ -114,6 +114,8 @@ impl BaseRenderGraphBuilder for RenderGraph { sample_count: 1, }); + main_pass_node.use_default_clear_color(0); + if config.add_3d_camera { main_pass_node.add_camera(camera::CAMERA); } diff --git a/crates/bevy_render/src/pass/pass.rs b/crates/bevy_render/src/pass/pass.rs index 94d12f25f5..afe3868ea0 100644 --- a/crates/bevy_render/src/pass/pass.rs +++ b/crates/bevy_render/src/pass/pass.rs @@ -18,6 +18,28 @@ impl TextureAttachment { } } +#[derive(Clone, Debug)] +pub struct ClearColor { + pub color: Color, +} + +impl ClearColor { + pub fn new(color: Color) -> Self { + ClearColor { + color, + } + } +} + +impl Default for ClearColor { + fn default() -> Self { + Self { + color: Color::rgb(0.1, 0.1, 0.1), + } + } + +} + #[derive(Debug, Clone)] pub struct RenderPassColorAttachmentDescriptor { /// The actual color attachment. diff --git a/crates/bevy_render/src/render_graph/nodes/pass_node.rs b/crates/bevy_render/src/render_graph/nodes/pass_node.rs index 75da79fd6e..5cd7b3e2d8 100644 --- a/crates/bevy_render/src/render_graph/nodes/pass_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/pass_node.rs @@ -1,6 +1,6 @@ use crate::{ draw::{Draw, RenderCommand}, - pass::{PassDescriptor, TextureAttachment}, + pass::{PassDescriptor, TextureAttachment, ClearColor}, pipeline::PipelineDescriptor, render_graph::{Node, ResourceSlotInfo, ResourceSlots}, render_resource::{BindGroupId, BufferId, RenderResourceBindings, RenderResourceType}, @@ -15,6 +15,7 @@ pub struct PassNode { cameras: Vec, color_attachment_input_indices: Vec>, depth_stencil_attachment_input_index: Option, + default_clear_color_inputs: Vec, } impl PassNode { @@ -50,12 +51,17 @@ impl PassNode { cameras: Vec::new(), color_attachment_input_indices, depth_stencil_attachment_input_index, + default_clear_color_inputs: Vec::new(), } } pub fn add_camera(&mut self, camera_name: &str) { self.cameras.push(camera_name.to_string()); } + + pub fn use_default_clear_color(&mut self, color_attachment_index: usize) { + self.default_clear_color_inputs.push(color_attachment_index); + } } impl Node for PassNode { @@ -76,6 +82,11 @@ impl Node for PassNode { let active_cameras= resources.get::().unwrap(); for (i, color_attachment) in self.descriptor.color_attachments.iter_mut().enumerate() { + if self.default_clear_color_inputs.contains(&i) { + if let Some(default_clear_color) = resources.get::() { + color_attachment.clear_color = default_clear_color.color; + } + } if let Some(input_index) = self.color_attachment_input_indices[i] { color_attachment.attachment = TextureAttachment::Id(input.get(input_index).unwrap().get_texture().unwrap()); diff --git a/examples/window/clear_color.rs b/examples/window/clear_color.rs new file mode 100644 index 0000000000..83b2199035 --- /dev/null +++ b/examples/window/clear_color.rs @@ -0,0 +1,8 @@ +use bevy::prelude::*; + +fn main() { + App::build() + .add_resource(ClearColor::new(Color::rgb(0.2, 0.2, 0.8))) + .add_default_plugins() + .run(); +} diff --git a/src/prelude.rs b/src/prelude.rs index 1c9ffac319..400e925ef2 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -17,6 +17,7 @@ pub use crate::{ draw::Draw, entity::*, mesh::{shape, Mesh}, + pass::ClearColor, pipeline::{PipelineDescriptor, RenderPipelines}, render_graph::{ nodes::{