-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - Camera Driven Rendering #4745
Closed
Closed
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5b62900
Camera Driven Rendering
cart 8620324
Apply suggestions from code review
cart 0c86620
UI camera far constants and docs
cart 414205c
world_to_screen -> world->viewport
cart 04482c3
multi-line warning
cart f2bfec2
Remove spurious logical_target_size assignment
cart b1ce968
clippy
cart 08cb5d3
clippy
cart 5cde9df
dont rely on undefined behavior for camera selection
cart 00dd8e3
add a new two_passes example
cart ab0af1a
remove notes.md
cart e4f398f
Remove newly added UiCameraBundle doc reference
cart 397b213
Merge remote-tracking branch 'upstream/main' into camera-driven
cart 64a9e74
adjust many_lights to use new FixedHorizontal constructor
cart 2d3ef0b
Fix headless
cart 773f6f6
resolve comments
cart bf11ff5
remove unused
cart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use bevy_derive::{Deref, DerefMut}; | ||
use bevy_ecs::prelude::*; | ||
use bevy_reflect::{Reflect, ReflectDeserialize}; | ||
use bevy_render::{color::Color, extract_resource::ExtractResource, RenderWorld}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Reflect, Serialize, Deserialize, Clone, Debug)] | ||
#[reflect_value(Serialize, Deserialize)] | ||
pub enum ClearColorConfig { | ||
Default, | ||
Custom(Color), | ||
None, | ||
} | ||
|
||
impl Default for ClearColorConfig { | ||
fn default() -> Self { | ||
ClearColorConfig::Default | ||
} | ||
} | ||
|
||
/// When used as a resource, sets the color that is used to clear the screen between frames. | ||
/// | ||
/// This color appears as the "background" color for simple apps, when | ||
/// there are portions of the screen with nothing rendered. | ||
#[derive(Component, Clone, Debug, Deref, DerefMut, ExtractResource)] | ||
pub struct ClearColor(pub Color); | ||
|
||
impl Default for ClearColor { | ||
fn default() -> Self { | ||
Self(Color::rgb(0.4, 0.4, 0.4)) | ||
} | ||
} | ||
|
||
pub fn extract_clear_color(clear_color: Res<ClearColor>, mut render_world: ResMut<RenderWorld>) { | ||
// If the clear color has changed | ||
if clear_color.is_changed() { | ||
// Update the clear color resource in the render world | ||
render_world.insert_resource(clear_color.clone()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::clear_color::ClearColorConfig; | ||
use bevy_ecs::{prelude::*, query::QueryItem}; | ||
use bevy_reflect::Reflect; | ||
use bevy_render::{ | ||
camera::{ | ||
Camera, CameraProjection, CameraRenderGraph, DepthCalculation, OrthographicProjection, | ||
}, | ||
extract_component::ExtractComponent, | ||
primitives::Frustum, | ||
view::VisibleEntities, | ||
}; | ||
use bevy_transform::prelude::{GlobalTransform, Transform}; | ||
|
||
#[derive(Component, Default, Reflect, Clone)] | ||
#[reflect(Component)] | ||
pub struct Camera2d { | ||
pub clear_color: ClearColorConfig, | ||
} | ||
|
||
impl ExtractComponent for Camera2d { | ||
type Query = &'static Self; | ||
type Filter = With<Camera>; | ||
|
||
fn extract_component(item: QueryItem<Self::Query>) -> Self { | ||
item.clone() | ||
} | ||
} | ||
|
||
#[derive(Bundle)] | ||
pub struct Camera2dBundle { | ||
pub camera: Camera, | ||
pub camera_render_graph: CameraRenderGraph, | ||
pub projection: OrthographicProjection, | ||
pub visible_entities: VisibleEntities, | ||
pub frustum: Frustum, | ||
pub transform: Transform, | ||
pub global_transform: GlobalTransform, | ||
pub camera_2d: Camera2d, | ||
} | ||
|
||
impl Default for Camera2dBundle { | ||
fn default() -> Self { | ||
Self::new_with_far(1000.0) | ||
} | ||
} | ||
|
||
impl Camera2dBundle { | ||
/// Create an orthographic projection camera with a custom Z position. | ||
/// | ||
/// The camera is placed at `Z=far-0.1`, looking toward the world origin `(0,0,0)`. | ||
/// Its orthographic projection extends from `0.0` to `-far` in camera view space, | ||
/// corresponding to `Z=far-0.1` (closest to camera) to `Z=-0.1` (furthest away from | ||
/// camera) in world space. | ||
cart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn new_with_far(far: f32) -> Self { | ||
// we want 0 to be "closest" and +far to be "farthest" in 2d, so we offset | ||
// the camera's translation by far and use a right handed coordinate system | ||
let projection = OrthographicProjection { | ||
far, | ||
depth_calculation: DepthCalculation::ZDifference, | ||
..Default::default() | ||
}; | ||
let transform = Transform::from_xyz(0.0, 0.0, far - 0.1); | ||
let view_projection = | ||
projection.get_projection_matrix() * transform.compute_matrix().inverse(); | ||
let frustum = Frustum::from_view_projection( | ||
&view_projection, | ||
&transform.translation, | ||
&transform.back(), | ||
projection.far(), | ||
); | ||
Self { | ||
camera_render_graph: CameraRenderGraph::new(crate::core_2d::graph::NAME), | ||
projection, | ||
visible_entities: VisibleEntities::default(), | ||
frustum, | ||
transform, | ||
global_transform: Default::default(), | ||
camera: Camera::default(), | ||
camera_2d: Camera2d::default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extract resource plugin is meant to do this now I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup this is no longer used. Nice catch!