-
-
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
3D OrthographicProjection improvements + new example #1361
Changes from 5 commits
5e05635
50c8a7f
8cee12d
a147b80
a4092e8
77803b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ pub struct OrthographicProjection { | |
pub window_origin: WindowOrigin, | ||
pub scaling_mode: ScalingMode, | ||
pub scale: f32, | ||
pub depth_calculation: DepthCalculation, | ||
} | ||
|
||
impl CameraProjection for OrthographicProjection { | ||
|
@@ -140,7 +141,7 @@ impl CameraProjection for OrthographicProjection { | |
} | ||
|
||
fn depth_calculation(&self) -> DepthCalculation { | ||
DepthCalculation::ZDifference | ||
self.depth_calculation | ||
} | ||
} | ||
|
||
|
@@ -156,6 +157,7 @@ impl Default for OrthographicProjection { | |
window_origin: WindowOrigin::Center, | ||
scaling_mode: ScalingMode::WindowSize, | ||
scale: 1.0, | ||
depth_calculation: DepthCalculation::Distance, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,7 +231,7 @@ pub fn visible_entities_system( | |
// smaller distances are sorted to lower indices by using the distance from the camera | ||
FloatOrd(match camera.depth_calculation { | ||
DepthCalculation::ZDifference => camera_position.z - position.z, | ||
DepthCalculation::Distance => (camera_position - position).length(), | ||
DepthCalculation::Distance => (camera_position - position).length_squared(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The square root operation isn't necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Because we only use this value for an ordering) |
||
}) | ||
} else { | ||
let order = FloatOrd(no_transform_order); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
use crate::{ | ||
camera::{Camera, OrthographicProjection, PerspectiveProjection, VisibleEntities}, | ||
camera::{ | ||
Camera, DepthCalculation, OrthographicProjection, PerspectiveProjection, ScalingMode, | ||
VisibleEntities, | ||
}, | ||
pipeline::RenderPipelines, | ||
prelude::Visible, | ||
render_graph::base, | ||
|
@@ -92,6 +95,7 @@ impl OrthographicCameraBundle { | |
}, | ||
orthographic_projection: OrthographicProjection { | ||
far, | ||
depth_calculation: DepthCalculation::ZDifference, | ||
..Default::default() | ||
}, | ||
visible_entities: Default::default(), | ||
|
@@ -106,7 +110,11 @@ impl OrthographicCameraBundle { | |
name: Some(base::camera::CAMERA_3D.to_string()), | ||
..Default::default() | ||
}, | ||
orthographic_projection: Default::default(), | ||
orthographic_projection: OrthographicProjection { | ||
scaling_mode: ScalingMode::FixedVertical, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For 3D applications, Edit: With If |
||
depth_calculation: DepthCalculation::Distance, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should already be the default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know. I added it explicitly, just to ensure that upon any possible future changes, it does not accidentally break. I don't think there is any harm in explicitly overriding it with the correct value. It just seems safer (and more sensible) to me that the camera bundle constructors should explicitly ensure that any relevant parameters are set to the correct values, rather than implicitly relying on the default just happening to be the right thing. |
||
..Default::default() | ||
}, | ||
visible_entities: Default::default(), | ||
transform: Default::default(), | ||
global_transform: Default::default(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use bevy::prelude::*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cargo.toml needs to be updated to include this example. |
||
|
||
fn main() { | ||
App::build() | ||
.add_resource(Msaa { samples: 4 }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will need to be updated to be |
||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup.system()) | ||
.run(); | ||
} | ||
|
||
/// set up a simple 3D scene | ||
fn setup( | ||
commands: &mut Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<StandardMaterial>>, | ||
) { | ||
// set up the camera | ||
let mut camera = OrthographicCameraBundle::new_3d(); | ||
camera.orthographic_projection.scale = 3.0; | ||
camera.transform = Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::zero(), Vec3::unit_y()); | ||
|
||
// add entities to the world | ||
commands | ||
// plane | ||
.spawn(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), | ||
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), | ||
..Default::default() | ||
}) | ||
// cubes | ||
.spawn(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), | ||
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), | ||
transform: Transform::from_xyz(1.5, 0.5, 1.5), | ||
..Default::default() | ||
}) | ||
.spawn(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), | ||
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), | ||
transform: Transform::from_xyz(1.5, 0.5, -1.5), | ||
..Default::default() | ||
}) | ||
.spawn(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), | ||
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), | ||
transform: Transform::from_xyz(-1.5, 0.5, 1.5), | ||
..Default::default() | ||
}) | ||
.spawn(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), | ||
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), | ||
transform: Transform::from_xyz(-1.5, 0.5, -1.5), | ||
..Default::default() | ||
}) | ||
// light | ||
.spawn(LightBundle { | ||
transform: Transform::from_xyz(3.0, 8.0, 5.0), | ||
..Default::default() | ||
}) | ||
// camera | ||
.spawn(camera); | ||
} |
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.
This is now a field in
OrthographicProjection
, so I had to add some extra derives, similar to theScalingMode
andWindowOrigin
enums.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.
Maybe a comment that
Distance
is generically applicable andZDifference
is an optimisation for an orthographic projection looking towards negative-z
(i.e. its forward vector is+z
because #1153, although don't include that in the comment)?