Skip to content
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

Transform rewrite - initial draft #374

Merged
merged 25 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cfa64e8
initial draft
MarekLg Aug 27, 2020
6889524
replaced TransformBuilder, replaced e.g. Translation by Vec3
MarekLg Aug 28, 2020
dfced53
removed pass by reference in e.g. Transform::translate()
MarekLg Aug 28, 2020
03372ec
parenting working (see PR #374 for more info)
MarekLg Sep 1, 2020
8129f51
removed print_children system
MarekLg Sep 1, 2020
13b4f19
Merge branch 'master' into transform-rewrite
MarekLg Sep 4, 2020
f8a6d58
removed unused imports, kept commented code
MarekLg Sep 4, 2020
b75c2ab
added missing transformations
MarekLg Sep 11, 2020
86c8d63
removed unnecessary comments
MarekLg Sep 11, 2020
c757af2
removed unnecessary unit struct components
MarekLg Sep 11, 2020
fd439f2
removed unnecessary comments
MarekLg Sep 11, 2020
a949b70
removed registrations
MarekLg Sep 11, 2020
4025802
forgetting optimization by compute shader for now
MarekLg Sep 11, 2020
39e8b84
changed local_matrix().w_axis to local_transform
MarekLg Sep 11, 2020
4ea9cbd
removed unnecessary childs
MarekLg Sep 11, 2020
db6ab51
changed to set_local_translation()
MarekLg Sep 11, 2020
fe04ecf
removed unused imports
MarekLg Sep 11, 2020
e3936d3
changed to Vec3::new() constructor
MarekLg Sep 11, 2020
3e120cc
removed unnecessary asserts
MarekLg Sep 11, 2020
c0cccfc
Merge upstream/master into transform-rewrite
MarekLg Sep 11, 2020
7cc90b1
fix correct_children() test failing
MarekLg Sep 12, 2020
8329f96
split up local and global Transform
MarekLg Sep 12, 2020
79c898a
adapted 2D components to Transforms
MarekLg Sep 14, 2020
717f2b0
removed old Transform Components
MarekLg Sep 14, 2020
8a17481
forgot to add global_transform
MarekLg Sep 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Cargo.lock
.cargo/config
/.idea
/.vscode
/benches/target
/benches/target
13 changes: 4 additions & 9 deletions crates/bevy_pbr/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_render::{
pipeline::{DynamicBinding, PipelineSpecialization, RenderPipeline, RenderPipelines},
render_graph::base::MainPass,
};
use bevy_transform::prelude::{Rotation, Scale, Transform, Translation};
use bevy_transform::prelude::{GlobalTransform, Transform};

/// A component bundle for "pbr mesh" entities
#[derive(Bundle)]
Expand All @@ -18,9 +18,7 @@ pub struct PbrComponents {
pub draw: Draw,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub scale: Scale,
pub global_transform: GlobalTransform,
}

impl Default for PbrComponents {
Expand Down Expand Up @@ -49,9 +47,7 @@ impl Default for PbrComponents {
main_pass: Default::default(),
draw: Default::default(),
transform: Default::default(),
translation: Default::default(),
rotation: Default::default(),
scale: Default::default(),
global_transform: Default::default(),
}
}
}
Expand All @@ -61,6 +57,5 @@ impl Default for PbrComponents {
pub struct LightComponents {
pub light: Light,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub global_transform: GlobalTransform,
}
9 changes: 4 additions & 5 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use bevy_core::Byteable;
use bevy_math::Mat4;
use bevy_property::Properties;
use bevy_render::{
camera::{CameraProjection, PerspectiveProjection},
color::Color,
};
use bevy_transform::components::Translation;
use bevy_transform::components::GlobalTransform;
use std::ops::Range;

/// A point light
Expand Down Expand Up @@ -37,16 +36,16 @@ pub(crate) struct LightRaw {
unsafe impl Byteable for LightRaw {}

impl LightRaw {
pub fn from(light: &Light, transform: &Mat4, translation: &Translation) -> LightRaw {
pub fn from(light: &Light, global_transform: &GlobalTransform) -> LightRaw {
let perspective = PerspectiveProjection {
fov: light.fov,
aspect_ratio: 1.0,
near: light.depth.start,
far: light.depth.end,
};

let proj = perspective.get_projection_matrix() * *transform;
let (x, y, z) = translation.0.into();
let proj = perspective.get_projection_matrix() * *global_transform.value();
let (x, y, z) = global_transform.translation().into();
LightRaw {
proj: proj.to_cols_array_2d(),
pos: [x, y, z, 1.0],
Expand Down
8 changes: 3 additions & 5 deletions crates/bevy_pbr/src/render_graph/lights_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn lights_node_system(
render_resource_context: Res<Box<dyn RenderResourceContext>>,
// TODO: this write on RenderResourceBindings will prevent this system from running in parallel with other systems that do the same
mut render_resource_bindings: ResMut<RenderResourceBindings>,
mut query: Query<(&Light, &Transform, &Translation)>,
mut query: Query<(&Light, &GlobalTransform)>,
) {
let state = &mut state;
let render_resource_context = &**render_resource_context;
Expand Down Expand Up @@ -132,14 +132,12 @@ pub fn lights_node_system(
data[0..light_count_size].copy_from_slice([light_count as u32, 0, 0, 0].as_bytes());

// light array
for ((light, transform, translation), slot) in query
for ((light, global_transform), slot) in query
.iter()
.iter()
.zip(data[light_count_size..current_light_uniform_size].chunks_exact_mut(size))
{
slot.copy_from_slice(
LightRaw::from(&light, &transform.value, &translation).as_bytes(),
);
slot.copy_from_slice(LightRaw::from(&light, &global_transform).as_bytes());
}
},
);
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_pbr/src/render_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ use bevy_render::{
render_graph::{base, AssetRenderResourcesNode, RenderGraph, RenderResourcesNode},
shader::Shader,
};
use bevy_transform::prelude::Transform;
use bevy_transform::prelude::GlobalTransform;

pub(crate) fn add_pbr_graph(graph: &mut RenderGraph, resources: &Resources) {
graph.add_system_node(node::TRANSFORM, RenderResourcesNode::<Transform>::new(true));
graph.add_system_node(
node::TRANSFORM,
RenderResourcesNode::<GlobalTransform>::new(true),
);
graph.add_system_node(
node::STANDARD_MATERIAL,
AssetRenderResourcesNode::<StandardMaterial>::new(true),
Expand Down
35 changes: 18 additions & 17 deletions crates/bevy_render/src/camera/visible_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::Draw;
use bevy_core::FloatOrd;
use bevy_ecs::{Entity, Query};
use bevy_property::Properties;
use bevy_transform::prelude::Transform;
use bevy_transform::prelude::GlobalTransform;

#[derive(Debug)]
pub struct VisibleEntity {
Expand All @@ -24,13 +24,13 @@ impl VisibleEntities {
}

pub fn visible_entities_system(
mut camera_query: Query<(&Camera, &Transform, &mut VisibleEntities)>,
mut camera_query: Query<(&Camera, &GlobalTransform, &mut VisibleEntities)>,
mut draw_query: Query<(Entity, &Draw)>,
draw_transform_query: Query<(&Draw, &Transform)>,
draw_transform_query: Query<(&Draw, &GlobalTransform)>,
) {
for (camera, camera_transform, mut visible_entities) in &mut camera_query.iter() {
for (camera, camera_global_transform, mut visible_entities) in &mut camera_query.iter() {
visible_entities.value.clear();
let camera_position = camera_transform.value.w_axis().truncate();
let camera_position = camera_global_transform.translation();

let mut no_transform_order = 0.0;
let mut transparent_entities = Vec::new();
Expand All @@ -39,18 +39,19 @@ pub fn visible_entities_system(
continue;
}

let order = if let Ok(transform) = draw_transform_query.get::<Transform>(entity) {
let position = transform.value.w_axis().truncate();
// 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(),
})
} else {
let order = FloatOrd(no_transform_order);
no_transform_order += 0.1;
order
};
let order =
if let Ok(global_transform) = draw_transform_query.get::<GlobalTransform>(entity) {
let position = global_transform.translation();
// 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(),
})
} else {
let order = FloatOrd(no_transform_order);
no_transform_order += 0.1;
order
};

if draw.is_transparent {
transparent_entities.push(VisibleEntity { entity, order })
Expand Down
25 changes: 8 additions & 17 deletions crates/bevy_render/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
use base::MainPass;
use bevy_asset::Handle;
use bevy_ecs::Bundle;
use bevy_transform::components::{Rotation, Scale, Transform, Translation};
use bevy_math::Vec3;
use bevy_transform::components::{GlobalTransform, Transform};

/// A component bundle for "mesh" entities
#[derive(Bundle, Default)]
Expand All @@ -17,9 +18,7 @@ pub struct MeshComponents {
pub render_pipelines: RenderPipelines,
pub main_pass: MainPass,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub scale: Scale,
pub global_transform: GlobalTransform,
}

/// A component bundle for "3d camera" entities
Expand All @@ -29,9 +28,7 @@ pub struct Camera3dComponents {
pub perspective_projection: PerspectiveProjection,
pub visible_entities: VisibleEntities,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub scale: Scale,
pub global_transform: GlobalTransform,
}

impl Default for Camera3dComponents {
Expand All @@ -44,9 +41,7 @@ impl Default for Camera3dComponents {
perspective_projection: Default::default(),
visible_entities: Default::default(),
transform: Default::default(),
translation: Default::default(),
rotation: Default::default(),
scale: Default::default(),
global_transform: Default::default(),
}
}
}
Expand All @@ -58,9 +53,7 @@ pub struct Camera2dComponents {
pub orthographic_projection: OrthographicProjection,
pub visible_entities: VisibleEntities,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub scale: Scale,
pub global_transform: GlobalTransform,
}

impl Default for Camera2dComponents {
Expand All @@ -78,10 +71,8 @@ impl Default for Camera2dComponents {
..Default::default()
},
visible_entities: Default::default(),
transform: Default::default(),
translation: Translation::new(0.0, 0.0, far - 0.1),
rotation: Default::default(),
scale: Default::default(),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, far - 0.1)),
global_transform: Default::default(),
}
}
}
21 changes: 11 additions & 10 deletions crates/bevy_render/src/render_graph/nodes/camera_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,19 @@ pub fn camera_node_system(
// PERF: this write on RenderResourceAssignments will prevent this system from running in parallel
// with other systems that do the same
mut render_resource_bindings: ResMut<RenderResourceBindings>,
query: Query<(&Camera, &Transform)>,
query: Query<(&Camera, &GlobalTransform)>,
) {
let render_resource_context = &**render_resource_context;

let (camera, transform) = if let Some(camera_entity) = active_cameras.get(&state.camera_name) {
(
query.get::<Camera>(camera_entity).unwrap(),
query.get::<Transform>(camera_entity).unwrap(),
)
} else {
return;
};
let (camera, global_transform) =
if let Some(camera_entity) = active_cameras.get(&state.camera_name) {
(
query.get::<Camera>(camera_entity).unwrap(),
query.get::<GlobalTransform>(camera_entity).unwrap(),
)
} else {
return;
};

let staging_buffer = if let Some(staging_buffer) = state.staging_buffer {
render_resource_context.map_buffer(staging_buffer);
Expand Down Expand Up @@ -118,7 +119,7 @@ pub fn camera_node_system(

let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>();
let camera_matrix: [f32; 16] =
(camera.projection_matrix * transform.value.inverse()).to_cols_array();
(camera.projection_matrix * global_transform.value().inverse()).to_cols_array();

render_resource_context.write_mapped_buffer(
staging_buffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ where
}
}

impl RenderResources for bevy_transform::prelude::Transform {
impl RenderResources for bevy_transform::prelude::GlobalTransform {
fn render_resources_len(&self) -> usize {
1
}

fn get_render_resource(&self, index: usize) -> Option<&dyn RenderResource> {
if index == 0 {
Some(&self.value)
Some(self.value())
} else {
None
}
Expand Down
18 changes: 5 additions & 13 deletions crates/bevy_sprite/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_render::{
prelude::Draw,
render_graph::base::MainPass,
};
use bevy_transform::prelude::{Rotation, Scale, Transform, Translation};
use bevy_transform::prelude::{GlobalTransform, Transform};

#[derive(Bundle)]
pub struct SpriteComponents {
Expand All @@ -21,9 +21,7 @@ pub struct SpriteComponents {
pub draw: Draw,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub scale: Scale,
pub global_transform: GlobalTransform,
}

impl Default for SpriteComponents {
Expand Down Expand Up @@ -56,9 +54,7 @@ impl Default for SpriteComponents {
main_pass: MainPass,
material: Default::default(),
transform: Default::default(),
translation: Default::default(),
rotation: Default::default(),
scale: Default::default(),
global_transform: Default::default(),
}
}
}
Expand All @@ -77,9 +73,7 @@ pub struct SpriteSheetComponents {
pub main_pass: MainPass,
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub scale: Scale,
pub global_transform: GlobalTransform,
}

impl Default for SpriteSheetComponents {
Expand Down Expand Up @@ -112,9 +106,7 @@ impl Default for SpriteSheetComponents {
sprite: Default::default(),
texture_atlas: Default::default(),
transform: Default::default(),
translation: Default::default(),
rotation: Default::default(),
scale: Default::default(),
global_transform: Default::default(),
}
}
}
Loading