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 7 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Cargo.lock
.cargo/config
/.idea
/.vscode

10 changes: 1 addition & 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::Transform;

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

impl Default for PbrComponents {
Expand Down Expand Up @@ -49,9 +46,6 @@ impl Default for PbrComponents {
main_pass: Default::default(),
draw: Default::default(),
transform: Default::default(),
translation: Default::default(),
rotation: Default::default(),
scale: Default::default(),
}
}
}
Expand All @@ -61,6 +55,4 @@ impl Default for PbrComponents {
pub struct LightComponents {
pub light: Light,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
}
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::Transform;
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, transform: &Transform) -> 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() * *transform.global_matrix();
let (x, y, z) = transform.global_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, &Transform)>,
) {
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, 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, &transform).as_bytes());
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/camera/visible_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn visible_entities_system(
) {
for (camera, camera_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_transform.global_matrix().w_axis().truncate();

let mut no_transform_order = 0.0;
let mut transparent_entities = Vec::new();
Expand All @@ -40,7 +40,7 @@ pub fn visible_entities_system(
}

let order = if let Ok(transform) = draw_transform_query.get::<Transform>(entity) {
let position = transform.value.w_axis().truncate();
let position = transform.global_matrix().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(),
Expand Down
9 changes: 0 additions & 9 deletions crates/bevy_render/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ pub struct MeshComponents {
pub render_pipelines: RenderPipelines,
pub main_pass: MainPass,
pub transform: Transform,
pub translation: Translation,
pub rotation: Rotation,
pub scale: Scale,
}

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

impl Default for Camera3dComponents {
Expand All @@ -44,9 +38,6 @@ impl Default for Camera3dComponents {
perspective_projection: Default::default(),
visible_entities: Default::default(),
transform: Default::default(),
translation: Default::default(),
rotation: Default::default(),
scale: Default::default(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/render_graph/nodes/camera_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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 * transform.global_matrix().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 @@ -186,7 +186,7 @@ impl RenderResources for bevy_transform::prelude::Transform {

fn get_render_resource(&self, index: usize) -> Option<&dyn RenderResource> {
if index == 0 {
Some(&self.value)
Some(self.global_matrix())
} else {
None
}
Expand Down
41 changes: 0 additions & 41 deletions crates/bevy_transform/src/components/local_transform.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/bevy_transform/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod children;
mod local_transform;
mod non_uniform_scale;
mod parent;
mod rotation;
Expand All @@ -8,7 +7,6 @@ mod transform;
mod translation;

pub use children::Children;
pub use local_transform::*;
pub use non_uniform_scale::*;
pub use parent::{Parent, PreviousParent};
pub use rotation::*;
Expand Down
124 changes: 107 additions & 17 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,126 @@
use bevy_math::Mat4;
use bevy_math::{Mat4, Quat, Vec3};
use bevy_property::Properties;
use std::fmt;

#[derive(Debug, PartialEq, Clone, Copy, Properties)]
pub struct Transform {
MarekLg marked this conversation as resolved.
Show resolved Hide resolved
pub value: Mat4,
pub sync: bool, // NOTE: this is hopefully a temporary measure to allow setting the transform directly.
// ideally setting the transform automatically propagates back to position / translation / rotation,
// but right now they are always considered the source of truth
local: Mat4,
global: Mat4,
MarekLg marked this conversation as resolved.
Show resolved Hide resolved
}

impl Transform {
Copy link
Member

@cart cart Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this api in general, but I don't think its a complete replacement for the current transform system until we have the following apis:

// sets the absolute local scale
set_local_scale(&mut self, scale: Vec3)

// gets the absolute local rotation
local_rotation(&self) -> Quat

// sets the absolute local rotation
set_local_rotation(&mut self, rotation: Quat)

#[inline(always)]
pub fn identity() -> Self {
pub fn new(local: Mat4) -> Self {
Transform {
value: Mat4::identity(),
sync: true,
local,
global: local,
}
}

#[inline(always)]
pub fn new(value: Mat4) -> Self {
Transform { value, sync: true }
pub fn new_with_parent(local: Mat4, parent: &Mat4) -> Self {
Transform {
local,
global: *parent * local,
}
}

pub fn from_parent(parent: &Mat4) -> Self {
Transform {
local: Mat4::default(),
global: *parent,
}
}

/// This creates a new `LocalToWorld` transform with the `sync` field set to `false`.
/// While `sync` is false, position, rotation, and scale components will not be synced to the transform.
/// This is helpful if you want to manually set the transform to a value (ex: Mat4::face_toward)
#[inline(always)]
pub fn new_sync_disabled(value: Mat4) -> Self {
Transform { value, sync: false }
pub fn identity() -> Self {
Transform {
local: Mat4::identity(),
global: Mat4::identity(),
}
}

pub fn from_translation(translation: Vec3) -> Self {
Transform::new(Mat4::from_translation(translation))
}

pub fn from_rotation(rotation: Quat) -> Self {
Transform::new(Mat4::from_quat(rotation))
}

pub fn from_scale(scale: Vec3) -> Self {
Transform::new(Mat4::from_scale(scale))
}

pub fn with_translation(mut self, translation: Vec3) -> Self {
self.translate(translation);
self
}

pub fn with_rotation(mut self, rotation: Quat) -> Self {
self.rotate(rotation);
self
}

// TODO: with_scale()

pub fn local_matrix(&self) -> &Mat4 {
&self.local
}

pub fn local_matrix_mut(&mut self) -> &mut Mat4 {
&mut self.local
}

pub fn global_matrix(&self) -> &Mat4 {
&self.global
}

pub fn local_translation(&self) -> Vec3 {
Vec3::from(self.local.w_axis().truncate())
}

// FIXME: only gets updated post update
pub fn global_translation(&self) -> Vec3 {
Vec3::from(self.global.w_axis().truncate())
}

pub fn local_scale(&self) -> Vec3 {
Vec3::new(
self.local.x_axis().truncate().length(),
self.local.y_axis().truncate().length(),
self.local.z_axis().truncate().length(),
)
}

// FIXME: only gets updated post update
pub fn global_scale(&self) -> Vec3 {
Vec3::new(
self.global.x_axis().truncate().length(),
self.global.y_axis().truncate().length(),
self.global.z_axis().truncate().length(),
)
}

pub fn set_local_translation(&mut self, translation: Vec3) {
*self.local.w_axis_mut() = translation.extend(1.0);
}

pub fn apply_parent_matrix(&mut self, parent: Option<Mat4>) {
match parent {
Some(parent) => self.global = parent * self.local,
None => self.global = self.local,
};
}

pub fn translate(&mut self, translation: Vec3) {
*self.local.w_axis_mut() += translation.extend(0.0);
}

pub fn rotate(&mut self, rotation: Quat) {
self.local = self.local.mul_mat4(&Mat4::from_quat(rotation));
}

// TODO: scale()
}

impl Default for Transform {
Expand All @@ -41,6 +131,6 @@ impl Default for Transform {

impl fmt::Display for Transform {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
write!(f, "{}", self.local)
}
}
Loading