Skip to content

Commit

Permalink
Transform Rewrite (#374)
Browse files Browse the repository at this point in the history
Remove individual Translation / Rotation / Scale components in favor of a combined Transform component
  • Loading branch information
MarekLg authored Sep 14, 2020
1 parent 3bc5e4c commit 474bb54
Show file tree
Hide file tree
Showing 46 changed files with 478 additions and 1,386 deletions.
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

0 comments on commit 474bb54

Please sign in to comment.