Skip to content

Commit 6c52a9b

Browse files
doonvZoomulator
authored andcommitted
Fix CameraProjection panic and improve CameraProjectionPlugin
1 parent b1ab036 commit 6c52a9b

File tree

3 files changed

+58
-48
lines changed

3 files changed

+58
-48
lines changed

crates/bevy_pbr/src/lib.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ mod prepass;
3535
mod render;
3636
mod ssao;
3737

38+
use std::marker::PhantomData;
39+
3840
use bevy_color::{Color, LinearRgba};
3941
pub use bundle::*;
4042
pub use extended_material::*;
@@ -91,7 +93,10 @@ use bevy_core_pipeline::core_3d::graph::{Core3d, Node3d};
9193
use bevy_ecs::prelude::*;
9294
use bevy_render::{
9395
alpha::AlphaMode,
94-
camera::{CameraUpdateSystem, Projection},
96+
camera::{
97+
CameraProjection, CameraUpdateSystem, OrthographicProjection, PerspectiveProjection,
98+
Projection,
99+
},
95100
extract_component::ExtractComponentPlugin,
96101
extract_resource::ExtractResourcePlugin,
97102
render_asset::prepare_assets,
@@ -306,6 +311,9 @@ impl Plugin for PbrPlugin {
306311
GpuMeshPreprocessPlugin {
307312
use_gpu_instance_buffer_builder: self.use_gpu_instance_buffer_builder,
308313
},
314+
PbrProjectionPlugin::<Projection>::default(),
315+
PbrProjectionPlugin::<PerspectiveProjection>::default(),
316+
PbrProjectionPlugin::<OrthographicProjection>::default(),
309317
))
310318
.configure_sets(
311319
PostUpdate,
@@ -324,11 +332,7 @@ impl Plugin for PbrPlugin {
324332
.after(TransformSystem::TransformPropagate)
325333
.after(VisibilitySystems::CheckVisibility)
326334
.after(CameraUpdateSystem),
327-
(
328-
clear_directional_light_cascades,
329-
build_directional_light_cascades::<Projection>,
330-
)
331-
.chain()
335+
clear_directional_light_cascades
332336
.in_set(SimulationLightSystems::UpdateDirectionalLightCascades)
333337
.after(TransformSystem::TransformPropagate)
334338
.after(CameraUpdateSystem),
@@ -414,3 +418,21 @@ impl Plugin for PbrPlugin {
414418
.init_resource::<GlobalLightMeta>();
415419
}
416420
}
421+
422+
/// [`CameraProjection`] specific PBR functionality.
423+
pub struct PbrProjectionPlugin<T: CameraProjection + Component>(PhantomData<T>);
424+
impl<T: CameraProjection + Component> Plugin for PbrProjectionPlugin<T> {
425+
fn build(&self, app: &mut App) {
426+
app.add_systems(
427+
PostUpdate,
428+
build_directional_light_cascades::<T>
429+
.in_set(SimulationLightSystems::UpdateDirectionalLightCascades)
430+
.after(clear_directional_light_cascades),
431+
);
432+
}
433+
}
434+
impl<T: CameraProjection + Component> Default for PbrProjectionPlugin<T> {
435+
fn default() -> Self {
436+
Self(Default::default())
437+
}
438+
}

crates/bevy_render/src/camera/projection.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ use std::marker::PhantomData;
22
use std::ops::{Div, DivAssign, Mul, MulAssign};
33

44
use crate::primitives::Frustum;
5+
use crate::view::VisibilitySystems;
56
use bevy_app::{App, Plugin, PostStartup, PostUpdate};
67
use bevy_ecs::prelude::*;
78
use bevy_math::{AspectRatio, Mat4, Rect, Vec2, Vec3A};
89
use bevy_reflect::{
910
std_traits::ReflectDefault, GetTypeRegistration, Reflect, ReflectDeserialize, ReflectSerialize,
1011
};
1112
use bevy_transform::components::GlobalTransform;
13+
use bevy_transform::TransformSystem;
1214
use serde::{Deserialize, Serialize};
1315

1416
/// Adds [`Camera`](crate::camera::Camera) driver systems for a given projection type.
17+
///
18+
/// If you are using `bevy_pbr`, then you need to add `PbrProjectionPlugin` along with this.
1519
pub struct CameraProjectionPlugin<T: CameraProjection + Component + GetTypeRegistration>(
1620
PhantomData<T>,
1721
);
@@ -29,12 +33,22 @@ impl<T: CameraProjection + Component + GetTypeRegistration> Plugin for CameraPro
2933
)
3034
.add_systems(
3135
PostUpdate,
32-
crate::camera::camera_system::<T>
33-
.in_set(CameraUpdateSystem)
34-
// We assume that each camera will only have one projection,
35-
// so we can ignore ambiguities with all other monomorphizations.
36-
// FIXME: Add an archetype invariant for this https://github.com/bevyengine/bevy/issues/1481.
37-
.ambiguous_with(CameraUpdateSystem),
36+
(
37+
crate::camera::camera_system::<T>
38+
.in_set(CameraUpdateSystem)
39+
// We assume that each camera will only have one projection,
40+
// so we can ignore ambiguities with all other monomorphizations.
41+
// FIXME: Add an archetype invariant for this https://github.com/bevyengine/bevy/issues/1481.
42+
.ambiguous_with(CameraUpdateSystem),
43+
crate::view::update_frusta::<T>
44+
.in_set(VisibilitySystems::UpdateFrusta)
45+
.after(crate::camera::camera_system::<T>)
46+
.after(TransformSystem::TransformPropagate)
47+
// We assume that no camera will have more than one projection component,
48+
// so these systems will run independently of one another.
49+
// FIXME: Add an archetype invariant for this https://github.com/bevyengine/bevy/issues/1481.
50+
.ambiguous_with(VisibilitySystems::UpdateFrusta),
51+
),
3852
);
3953
}
4054
}

crates/bevy_render/src/view/visibility/mod.rs

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ use bevy_transform::{components::GlobalTransform, TransformSystem};
1515
use bevy_utils::{Parallel, TypeIdMap};
1616

1717
use crate::{
18-
camera::{
19-
camera_system, Camera, CameraProjection, OrthographicProjection, PerspectiveProjection,
20-
Projection,
21-
},
18+
camera::{Camera, CameraProjection},
2219
mesh::Mesh,
2320
primitives::{Aabb, Frustum, Sphere},
2421
};
@@ -239,12 +236,8 @@ pub enum VisibilitySystems {
239236
/// Label for the [`calculate_bounds`], `calculate_bounds_2d` and `calculate_bounds_text2d` systems,
240237
/// calculating and inserting an [`Aabb`] to relevant entities.
241238
CalculateBounds,
242-
/// Label for the [`update_frusta<OrthographicProjection>`] system.
243-
UpdateOrthographicFrusta,
244-
/// Label for the [`update_frusta<PerspectiveProjection>`] system.
245-
UpdatePerspectiveFrusta,
246-
/// Label for the [`update_frusta<Projection>`] system.
247-
UpdateProjectionFrusta,
239+
/// Label for [`update_frusta`] in [`CameraProjectionPlugin`](crate::camera::CameraProjectionPlugin).
240+
UpdateFrusta,
248241
/// Label for the system propagating the [`InheritedVisibility`] in a
249242
/// [`hierarchy`](bevy_hierarchy).
250243
VisibilityPropagate,
@@ -261,40 +254,21 @@ impl Plugin for VisibilityPlugin {
261254

262255
app.configure_sets(
263256
PostUpdate,
264-
(
265-
CalculateBounds,
266-
UpdateOrthographicFrusta,
267-
UpdatePerspectiveFrusta,
268-
UpdateProjectionFrusta,
269-
VisibilityPropagate,
270-
)
257+
(CalculateBounds, VisibilityPropagate)
271258
.before(CheckVisibility)
272259
.after(TransformSystem::TransformPropagate),
273260
)
274261
.add_systems(
275262
PostUpdate,
276263
(
277264
calculate_bounds.in_set(CalculateBounds),
278-
update_frusta::<OrthographicProjection>
279-
.in_set(UpdateOrthographicFrusta)
280-
.after(camera_system::<OrthographicProjection>)
281-
// We assume that no camera will have more than one projection component,
282-
// so these systems will run independently of one another.
283-
// FIXME: Add an archetype invariant for this https://github.com/bevyengine/bevy/issues/1481.
284-
.ambiguous_with(update_frusta::<PerspectiveProjection>)
285-
.ambiguous_with(update_frusta::<Projection>),
286-
update_frusta::<PerspectiveProjection>
287-
.in_set(UpdatePerspectiveFrusta)
288-
.after(camera_system::<PerspectiveProjection>)
289-
// We assume that no camera will have more than one projection component,
290-
// so these systems will run independently of one another.
291-
// FIXME: Add an archetype invariant for this https://github.com/bevyengine/bevy/issues/1481.
292-
.ambiguous_with(update_frusta::<Projection>),
293-
update_frusta::<Projection>
294-
.in_set(UpdateProjectionFrusta)
295-
.after(camera_system::<Projection>),
296265
(visibility_propagate_system, reset_view_visibility).in_set(VisibilityPropagate),
297-
check_visibility::<WithMesh>.in_set(CheckVisibility),
266+
check_visibility::<WithMesh>
267+
.in_set(CheckVisibility)
268+
.after(CalculateBounds)
269+
.after(UpdateFrusta)
270+
.after(VisibilityPropagate)
271+
.after(TransformSystem::TransformPropagate),
298272
),
299273
);
300274
}

0 commit comments

Comments
 (0)