Skip to content

Commit 7125dcb

Browse files
authored
Customizable camera main texture usage (#11412)
# Objective - Some users want to change the default texture usage of the main camera but they are currently hardcoded ## Solution - Add a component that is used to configure the main texture usage field --- ## Changelog Added `CameraMainTextureUsage` Added `CameraMainTextureUsage` to `Camera3dBundle` and `Camera2dBundle` ## Migration Guide Add `main_texture_usages: Default::default()` to your camera bundle. # Notes Inspired by: #6815
1 parent 03ee959 commit 7125dcb

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

crates/bevy_core_pipeline/src/core_2d/camera_2d.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use crate::tonemapping::{DebandDither, Tonemapping};
22
use bevy_ecs::prelude::*;
33
use bevy_reflect::Reflect;
44
use bevy_render::{
5-
camera::{Camera, CameraProjection, CameraRenderGraph, OrthographicProjection},
5+
camera::{
6+
Camera, CameraMainTextureUsages, CameraProjection, CameraRenderGraph,
7+
OrthographicProjection,
8+
},
69
extract_component::ExtractComponent,
710
primitives::Frustum,
811
view::VisibleEntities,
@@ -26,6 +29,7 @@ pub struct Camera2dBundle {
2629
pub camera_2d: Camera2d,
2730
pub tonemapping: Tonemapping,
2831
pub deband_dither: DebandDither,
32+
pub main_texture_usages: CameraMainTextureUsages,
2933
}
3034

3135
impl Default for Camera2dBundle {
@@ -55,6 +59,7 @@ impl Default for Camera2dBundle {
5559
camera_2d: Camera2d,
5660
tonemapping: Tonemapping::None,
5761
deband_dither: DebandDither::Disabled,
62+
main_texture_usages: Default::default(),
5863
}
5964
}
6065
}
@@ -93,6 +98,7 @@ impl Camera2dBundle {
9398
camera_2d: Camera2d,
9499
tonemapping: Tonemapping::None,
95100
deband_dither: DebandDither::Disabled,
101+
main_texture_usages: Default::default(),
96102
}
97103
}
98104
}

crates/bevy_core_pipeline/src/core_3d/camera_3d.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::tonemapping::{DebandDither, Tonemapping};
22
use bevy_ecs::prelude::*;
33
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
44
use bevy_render::{
5-
camera::{Camera, CameraRenderGraph, Projection},
5+
camera::{Camera, CameraMainTextureUsages, CameraRenderGraph, Projection},
66
extract_component::ExtractComponent,
77
primitives::Frustum,
88
render_resource::{LoadOp, TextureUsages},
@@ -143,6 +143,7 @@ pub struct Camera3dBundle {
143143
pub tonemapping: Tonemapping,
144144
pub dither: DebandDither,
145145
pub color_grading: ColorGrading,
146+
pub main_texture_usages: CameraMainTextureUsages,
146147
}
147148

148149
// NOTE: ideally Perspective and Orthographic defaults can share the same impl, but sadly it breaks rust's type inference
@@ -160,6 +161,7 @@ impl Default for Camera3dBundle {
160161
tonemapping: Default::default(),
161162
dither: DebandDither::Enabled,
162163
color_grading: ColorGrading::default(),
164+
main_texture_usages: Default::default(),
163165
}
164166
}
165167
}

crates/bevy_render/src/camera/camera.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ use bevy_math::{
2424
primitives::Direction3d, vec2, Mat4, Ray3d, Rect, URect, UVec2, UVec4, Vec2, Vec3,
2525
};
2626
use bevy_reflect::prelude::*;
27+
use bevy_render_macros::ExtractComponent;
2728
use bevy_transform::components::GlobalTransform;
2829
use bevy_utils::{HashMap, HashSet};
2930
use bevy_window::{
3031
NormalizedWindowRef, PrimaryWindow, Window, WindowCreated, WindowRef, WindowResized,
3132
WindowScaleFactorChanged,
3233
};
3334
use std::{borrow::Cow, ops::Range};
34-
use wgpu::{BlendState, LoadOp, TextureFormat};
35+
use wgpu::{BlendState, LoadOp, TextureFormat, TextureUsages};
3536

3637
use super::{ClearColorConfig, Projection};
3738

@@ -750,6 +751,19 @@ pub fn camera_system<T: CameraProjection + Component>(
750751
}
751752
}
752753

754+
/// This component lets you control the [`TextureUsages`] field of the main texture generated for the camera
755+
#[derive(Component, ExtractComponent, Clone, Copy)]
756+
pub struct CameraMainTextureUsages(pub TextureUsages);
757+
impl Default for CameraMainTextureUsages {
758+
fn default() -> Self {
759+
Self(
760+
TextureUsages::RENDER_ATTACHMENT
761+
| TextureUsages::TEXTURE_BINDING
762+
| TextureUsages::COPY_SRC,
763+
)
764+
}
765+
}
766+
753767
#[derive(Component, Debug)]
754768
pub struct ExtractedCamera {
755769
pub target: Option<NormalizedRenderTarget>,

crates/bevy_render/src/camera/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub use manual_texture_view::*;
1212
pub use projection::*;
1313

1414
use crate::{
15-
extract_resource::ExtractResourcePlugin, render_graph::RenderGraph, ExtractSchedule, Render,
16-
RenderApp, RenderSet,
15+
extract_component::ExtractComponentPlugin, extract_resource::ExtractResourcePlugin,
16+
render_graph::RenderGraph, ExtractSchedule, Render, RenderApp, RenderSet,
1717
};
1818
use bevy_app::{App, Plugin};
1919
use bevy_ecs::schedule::IntoSystemConfigs;
@@ -39,6 +39,7 @@ impl Plugin for CameraPlugin {
3939
CameraProjectionPlugin::<PerspectiveProjection>::default(),
4040
ExtractResourcePlugin::<ManualTextureViews>::default(),
4141
ExtractResourcePlugin::<ClearColor>::default(),
42+
ExtractComponentPlugin::<CameraMainTextureUsages>::default(),
4243
));
4344

4445
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {

crates/bevy_render/src/view/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub use window::*;
77

88
use crate::{
99
camera::{
10-
ClearColor, ClearColorConfig, ExposureSettings, ExtractedCamera, ManualTextureViews,
11-
MipBias, TemporalJitter,
10+
CameraMainTextureUsages, ClearColor, ClearColorConfig, ExposureSettings, ExtractedCamera,
11+
ManualTextureViews, MipBias, TemporalJitter,
1212
},
1313
extract_resource::{ExtractResource, ExtractResourcePlugin},
1414
prelude::{Image, Shader},
@@ -465,11 +465,16 @@ fn prepare_view_targets(
465465
clear_color_global: Res<ClearColor>,
466466
render_device: Res<RenderDevice>,
467467
mut texture_cache: ResMut<TextureCache>,
468-
cameras: Query<(Entity, &ExtractedCamera, &ExtractedView)>,
468+
cameras: Query<(
469+
Entity,
470+
&ExtractedCamera,
471+
&ExtractedView,
472+
&CameraMainTextureUsages,
473+
)>,
469474
manual_texture_views: Res<ManualTextureViews>,
470475
) {
471476
let mut textures = HashMap::default();
472-
for (entity, camera, view) in cameras.iter() {
477+
for (entity, camera, view, texture_usage) in cameras.iter() {
473478
if let (Some(target_size), Some(target)) = (camera.physical_target_size, &camera.target) {
474479
if let (Some(out_texture_view), Some(out_texture_format)) = (
475480
target.get_texture_view(&windows, &images, &manual_texture_views),
@@ -502,9 +507,7 @@ fn prepare_view_targets(
502507
sample_count: 1,
503508
dimension: TextureDimension::D2,
504509
format: main_texture_format,
505-
usage: TextureUsages::RENDER_ATTACHMENT
506-
| TextureUsages::TEXTURE_BINDING
507-
| TextureUsages::COPY_SRC,
510+
usage: texture_usage.0,
508511
view_formats: match main_texture_format {
509512
TextureFormat::Bgra8Unorm => &[TextureFormat::Bgra8UnormSrgb],
510513
TextureFormat::Rgba8Unorm => &[TextureFormat::Rgba8UnormSrgb],

0 commit comments

Comments
 (0)