Skip to content

Commit

Permalink
Add "depth_load_op" configuration to 3d Cameras
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jun 2, 2022
1 parent f487407 commit 437038b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
36 changes: 34 additions & 2 deletions crates/bevy_core_pipeline/src/core_3d/camera_3d.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
use crate::clear_color::ClearColorConfig;
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_render::{
camera::{Camera, CameraRenderGraph, Projection},
extract_component::ExtractComponent,
primitives::Frustum,
render_resource::LoadOp,
view::VisibleEntities,
};
use bevy_transform::prelude::{GlobalTransform, Transform};
use serde::{Deserialize, Serialize};

#[derive(Component, Default, Reflect, Clone)]
/// Configuration for the "main 3d render graph".
#[derive(Component, Reflect, Clone, Default)]
#[reflect(Component)]
pub struct Camera3d {
/// The clear color operation to perform for the main 3d pass.
pub clear_color: ClearColorConfig,
/// The depth clear operation to perform for the main 3d pass.
pub depth_load_op: Camera3dDepthLoadOp,
}

/// The depth clear operation to perform for the main 3d pass.
#[derive(Reflect, Serialize, Deserialize, Clone, Debug)]
#[reflect_value(Serialize, Deserialize)]
pub enum Camera3dDepthLoadOp {
/// Clear with a specified value.
/// Note that 0.0 is the far plane due to bevy's use of reverse-z projections.
Clear(f32),
/// Load from memory.
Load,
}

impl Default for Camera3dDepthLoadOp {
fn default() -> Self {
Camera3dDepthLoadOp::Clear(0.0)
}
}

impl From<Camera3dDepthLoadOp> for LoadOp<f32> {
fn from(config: Camera3dDepthLoadOp) -> Self {
match config {
Camera3dDepthLoadOp::Clear(x) => LoadOp::Clear(x),
Camera3dDepthLoadOp::Load => LoadOp::Load,
}
}
}

impl ExtractComponent for Camera3d {
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ impl Node for MainPass3dNode {
view: &depth.view,
// NOTE: The opaque main pass loads the depth buffer and possibly overwrites it
depth_ops: Some(Operations {
// NOTE: 0.0 is the far plane due to bevy's use of reverse-z projections
load: LoadOp::Clear(0.0),
load: camera_3d.depth_load_op.clone().into(),
store: true,
}),
stencil_ops: None,
Expand Down
1 change: 1 addition & 0 deletions examples/3d/render_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn setup(
.spawn_bundle(Camera3dBundle {
camera_3d: Camera3d {
clear_color: ClearColorConfig::Custom(Color::WHITE),
..default()
},
camera: Camera {
// render before the "main pass" camera
Expand Down
1 change: 1 addition & 0 deletions examples/3d/two_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn setup(
transform: Transform::from_xyz(10.0, 10., -5.0).looking_at(Vec3::ZERO, Vec3::Y),
camera_3d: Camera3d {
clear_color: ClearColorConfig::None,
..default()
},
camera: Camera {
// renders after / on top of the main camera
Expand Down

0 comments on commit 437038b

Please sign in to comment.