Skip to content

Commit 21a79c5

Browse files
committed
camera: add position and rotation components to Perspective camera. add "sync" toggle to LocalToWorld transform.
1 parent 19bf386 commit 21a79c5

File tree

18 files changed

+69
-35
lines changed

18 files changed

+69
-35
lines changed

crates/bevy_pbr/src/nodes/lights_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl SystemNode for LightsNode {
119119
query.iter(world).zip(data.chunks_exact_mut(size))
120120
{
121121
slot.copy_from_slice(
122-
LightRaw::from(&light, &local_to_world.0, &translation).as_bytes(),
122+
LightRaw::from(&light, &local_to_world.value, &translation).as_bytes(),
123123
);
124124
}
125125
},

crates/bevy_render/src/entity.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct PerspectiveCameraEntity {
1919
pub camera: Camera,
2020
pub perspective_projection: PerspectiveProjection,
2121
pub local_to_world: LocalToWorld,
22+
pub translation: Translation,
23+
pub rotation: Rotation,
2224
}
2325

2426
impl Default for PerspectiveCameraEntity {
@@ -30,6 +32,8 @@ impl Default for PerspectiveCameraEntity {
3032
},
3133
perspective_projection: Default::default(),
3234
local_to_world: Default::default(),
35+
translation: Default::default(),
36+
rotation: Default::default(),
3337
}
3438
}
3539

crates/bevy_render/src/render_graph/nodes/camera_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl SystemNode for CameraNode {
7777
.find(|(camera, _)| camera.name.as_ref().map(|n| n.as_str()) == Some(&uniform_name))
7878
{
7979
let camera_matrix: [[f32; 4]; 4] =
80-
(camera.view_matrix * local_to_world.0).to_cols_array_2d();
80+
(camera.view_matrix * local_to_world.value).to_cols_array_2d();
8181

8282
let tmp_buffer = render_resources.create_buffer_mapped(
8383
BufferInfo {

crates/bevy_render/src/shader/uniforms/local_to_world.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl AsUniforms for bevy_transform::prelude::LocalToWorld {
5555

5656
fn get_uniform_bytes(&self, name: &str) -> Option<Vec<u8>> {
5757
match name {
58-
"Object" => Some(self.0.get_bytes()),
58+
"Object" => Some(self.value.get_bytes()),
5959
_ => None,
6060
}
6161
}
@@ -65,7 +65,7 @@ impl AsUniforms for bevy_transform::prelude::LocalToWorld {
6565
}
6666
fn get_field_bind_type(&self, name: &str) -> Option<FieldBindType> {
6767
match name {
68-
"object" => self.0.get_bind_type(),
68+
"object" => self.value.get_bind_type(),
6969
_ => None,
7070
}
7171
}
@@ -75,7 +75,7 @@ impl AsUniforms for bevy_transform::prelude::LocalToWorld {
7575

7676
fn get_uniform_bytes_ref(&self, name: &str) -> Option<&[u8]> {
7777
match name {
78-
"Object" => self.0.get_bytes_ref(),
78+
"Object" => self.value.get_bytes_ref(),
7979
_ => None,
8080
}
8181
}
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
use crate::math::Mat4;
2-
use shrinkwraprs::Shrinkwrap;
3-
use std::fmt;
42
use bevy_property::Properties;
3+
use std::fmt;
54

6-
#[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy, Properties)]
7-
#[shrinkwrap(mutable)]
8-
pub struct LocalToWorld(pub Mat4);
5+
#[derive(Debug, PartialEq, Clone, Copy, Properties)]
6+
pub struct LocalToWorld {
7+
pub value: Mat4,
8+
pub sync: bool, // NOTE: this is hopefully a temporary measure to allow setting the transform directly.
9+
// ideally setting the transform automatically propagates back to position / translation / rotation,
10+
// but right now they are always considered the source of truth
11+
}
912

1013
impl LocalToWorld {
1114
#[inline(always)]
1215
pub fn identity() -> Self {
13-
Self(Mat4::identity())
16+
LocalToWorld {
17+
value: Mat4::identity(),
18+
sync: true,
19+
}
20+
}
21+
22+
#[inline(always)]
23+
pub fn new(value: Mat4) -> Self {
24+
LocalToWorld { value, sync: true }
25+
}
26+
27+
/// This creates a new `LocalToWorld` transform with the `sync` field set to `false`.
28+
/// While `sync` is false, position, rotation, and scale components will not be synced to the transform.
29+
/// This is helpful if you want to manually set the transform to a value (ex: Mat4::look_at_rh)
30+
#[inline(always)]
31+
pub fn new_sync_disabled(value: Mat4) -> Self {
32+
LocalToWorld { value, sync: false }
1433
}
1534
}
1635

@@ -22,6 +41,6 @@ impl Default for LocalToWorld {
2241

2342
impl fmt::Display for LocalToWorld {
2443
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25-
write!(f, "{}", self.0)
44+
write!(f, "{}", self.value)
2645
}
2746
}

crates/bevy_transform/src/local_to_world_propagate_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn propagate_recursive(
3939
}
4040
};
4141

42-
let new_local_to_world = LocalToWorld(parent_local_to_world.0 * local_to_parent.0);
42+
let new_local_to_world = LocalToWorld { value: parent_local_to_world.value * local_to_parent.0, sync: true };
4343
commands.add_component(entity, new_local_to_world);
4444

4545
// Collect children

crates/bevy_transform/src/local_to_world_system.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,36 @@ pub fn build(_: &mut World) -> Box<dyn Schedulable> {
132132
s.spawn(|_| unsafe {
133133
// Translation
134134
a.for_each_unchecked(world, |(mut ltw, translation)| {
135-
*ltw = LocalToWorld(Mat4::from_translation(translation.0));
135+
if !ltw.sync { return; }
136+
*ltw = LocalToWorld::new(Mat4::from_translation(translation.0));
136137
});
137138
});
138139
s.spawn(|_| unsafe {
139140
// Rotation
140141
b.for_each_unchecked(world, |(mut ltw, rotation)| {
141-
*ltw = LocalToWorld(Mat4::from_quat(rotation.0));
142+
if !ltw.sync { return; }
143+
*ltw = LocalToWorld::new(Mat4::from_quat(rotation.0));
142144
});
143145
});
144146
s.spawn(|_| unsafe {
145147
// Scale
146148
c.for_each_unchecked(world, |(mut ltw, scale)| {
147-
*ltw = LocalToWorld(Mat4::from_scale(Vec3::new(scale.0, scale.0, scale.0)));
149+
if !ltw.sync { return; }
150+
*ltw = LocalToWorld::new(Mat4::from_scale(Vec3::new(scale.0, scale.0, scale.0)));
148151
});
149152
});
150153
s.spawn(|_| unsafe {
151154
// NonUniformScale
152155
d.for_each_unchecked(world, |(mut ltw, non_uniform_scale)| {
153-
*ltw = LocalToWorld(Mat4::from_scale(non_uniform_scale.0));
156+
if !ltw.sync { return; }
157+
*ltw = LocalToWorld::new(Mat4::from_scale(non_uniform_scale.0));
154158
});
155159
});
156160
s.spawn(|_| unsafe {
157161
// Translation + Rotation
158162
e.for_each_unchecked(world, |(mut ltw, translation, rotation)| {
159-
*ltw = LocalToWorld(Mat4::from_rotation_translation(
163+
if !ltw.sync { return; }
164+
*ltw = LocalToWorld::new(Mat4::from_rotation_translation(
160165
rotation.0,
161166
translation.0,
162167
));
@@ -165,7 +170,8 @@ pub fn build(_: &mut World) -> Box<dyn Schedulable> {
165170
s.spawn(|_| unsafe {
166171
// Translation + Scale
167172
f.for_each_unchecked(world, |(mut ltw, translation, scale)| {
168-
*ltw = LocalToWorld(Mat4::from_scale_rotation_translation(
173+
if !ltw.sync { return; }
174+
*ltw = LocalToWorld::new(Mat4::from_scale_rotation_translation(
169175
Vec3::new(scale.0, scale.0, scale.0),
170176
Quat::default(),
171177
translation.0,
@@ -175,7 +181,8 @@ pub fn build(_: &mut World) -> Box<dyn Schedulable> {
175181
s.spawn(|_| unsafe {
176182
// Translation + NonUniformScale
177183
g.for_each_unchecked(world, |(mut ltw, translation, non_uniform_scale)| {
178-
*ltw = LocalToWorld(Mat4::from_scale_rotation_translation(
184+
if !ltw.sync { return; }
185+
*ltw = LocalToWorld::new(Mat4::from_scale_rotation_translation(
179186
non_uniform_scale.0,
180187
Quat::default(),
181188
translation.0,
@@ -185,7 +192,8 @@ pub fn build(_: &mut World) -> Box<dyn Schedulable> {
185192
s.spawn(|_| unsafe {
186193
// Rotation + Scale
187194
h.for_each_unchecked(world, |(mut ltw, rotation, scale)| {
188-
*ltw = LocalToWorld(Mat4::from_scale_rotation_translation(
195+
if !ltw.sync { return; }
196+
*ltw = LocalToWorld::new(Mat4::from_scale_rotation_translation(
189197
Vec3::new(scale.0, scale.0, scale.0),
190198
rotation.0,
191199
Vec3::default(),
@@ -195,7 +203,8 @@ pub fn build(_: &mut World) -> Box<dyn Schedulable> {
195203
s.spawn(|_| unsafe {
196204
// Rotation + NonUniformScale
197205
i.for_each_unchecked(world, |(mut ltw, rotation, non_uniform_scale)| {
198-
*ltw = LocalToWorld(Mat4::from_scale_rotation_translation(
206+
if !ltw.sync { return; }
207+
*ltw = LocalToWorld::new(Mat4::from_scale_rotation_translation(
199208
non_uniform_scale.0,
200209
rotation.0,
201210
Vec3::default(),
@@ -205,7 +214,8 @@ pub fn build(_: &mut World) -> Box<dyn Schedulable> {
205214
s.spawn(|_| unsafe {
206215
// Translation + Rotation + Scale
207216
j.for_each_unchecked(world, |(mut ltw, translation, rotation, scale)| {
208-
*ltw = LocalToWorld(Mat4::from_scale_rotation_translation(
217+
if !ltw.sync { return; }
218+
*ltw = LocalToWorld::new(Mat4::from_scale_rotation_translation(
209219
Vec3::new(scale.0, scale.0, scale.0),
210220
rotation.0,
211221
translation.0,
@@ -217,7 +227,8 @@ pub fn build(_: &mut World) -> Box<dyn Schedulable> {
217227
k.for_each_unchecked(
218228
world,
219229
|(mut ltw, translation, rotation, non_uniform_scale)| {
220-
*ltw = LocalToWorld(Mat4::from_scale_rotation_translation(
230+
if !ltw.sync { return; }
231+
*ltw = LocalToWorld::new(Mat4::from_scale_rotation_translation(
221232
non_uniform_scale.0,
222233
rotation.0,
223234
translation.0,

examples/3d/3d_scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn setup(
5050
})
5151
// camera
5252
.add_entity(PerspectiveCameraEntity {
53-
local_to_world: LocalToWorld(Mat4::look_at_rh(
53+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
5454
Vec3::new(3.0, 8.0, 5.0),
5555
Vec3::new(0.0, 0.0, 0.0),
5656
Vec3::new(0.0, 0.0, 1.0),

examples/3d/load_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn setup(
3939
})
4040
// camera
4141
.add_entity(PerspectiveCameraEntity {
42-
local_to_world: LocalToWorld(Mat4::look_at_rh(
42+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
4343
Vec3::new(2.0, -6.0, 2.0),
4444
Vec3::new(0.0, 0.0, 0.0),
4545
Vec3::new(0.0, 0.0, 1.0),

examples/3d/parenting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn setup(
5353
})
5454
// camera
5555
.add_entity(PerspectiveCameraEntity {
56-
local_to_world: LocalToWorld(Mat4::look_at_rh(
56+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
5757
Vec3::new(5.0, 10.0, 10.0),
5858
Vec3::new(0.0, 0.0, 0.0),
5959
Vec3::new(0.0, 0.0, 1.0),

examples/3d/spawner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn setup(
6363
})
6464
// camera
6565
.add_entity(PerspectiveCameraEntity {
66-
local_to_world: LocalToWorld(Mat4::look_at_rh(
66+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
6767
Vec3::new(3.0, 8.0, 5.0),
6868
Vec3::new(0.0, 0.0, 0.0),
6969
Vec3::new(0.0, 0.0, 1.0),

examples/3d/texture.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn setup(
6767
})
6868
// camera
6969
.add_entity(PerspectiveCameraEntity {
70-
local_to_world: LocalToWorld(Mat4::look_at_rh(
70+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
7171
Vec3::new(3.0, -8.0, 5.0),
7272
Vec3::new(0.0, 0.0, 0.0),
7373
Vec3::new(0.0, 0.0, 1.0),

examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn setup(
3535
})
3636
// camera
3737
.add_entity(PerspectiveCameraEntity {
38-
local_to_world: LocalToWorld(Mat4::look_at_rh(
38+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
3939
Vec3::new(3.0, 8.0, 5.0),
4040
Vec3::new(0.0, 0.0, 0.0),
4141
Vec3::new(0.0, 0.0, 1.0),

examples/asset/asset_loading.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn setup(
7070
})
7171
// camera
7272
.add_entity(PerspectiveCameraEntity {
73-
local_to_world: LocalToWorld(Mat4::look_at_rh(
73+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
7474
Vec3::new(0.0, -10.0, 3.0),
7575
Vec3::new(0.0, 0.0, 0.0),
7676
Vec3::new(0.0, 0.0, 1.0),

examples/asset/hot_asset_reloading.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn setup(
4747
})
4848
// camera
4949
.add_entity(PerspectiveCameraEntity {
50-
local_to_world: LocalToWorld(Mat4::look_at_rh(
50+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
5151
Vec3::new(2.0, -6.0, 2.0),
5252
Vec3::new(0.0, 0.0, 0.0),
5353
Vec3::new(0.0, 0.0, 1.0),

examples/input/input_keyboard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn setup(
8787
})
8888
// camera
8989
.add_entity(PerspectiveCameraEntity {
90-
local_to_world: LocalToWorld(Mat4::look_at_rh(
90+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
9191
Vec3::new(3.0, 8.0, 5.0),
9292
Vec3::new(0.0, 0.0, 0.0),
9393
Vec3::new(0.0, 0.0, 1.0),

examples/shader/shader_custom_material.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn setup(
8585
})
8686
// camera
8787
.add_entity(PerspectiveCameraEntity {
88-
local_to_world: LocalToWorld(Mat4::look_at_rh(
88+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
8989
Vec3::new(3.0, 8.0, 5.0),
9090
Vec3::new(0.0, 0.0, 0.0),
9191
Vec3::new(0.0, 0.0, 1.0),

examples/shader/shader_defs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn setup(
112112
})
113113
// camera
114114
.add_entity(PerspectiveCameraEntity {
115-
local_to_world: LocalToWorld(Mat4::look_at_rh(
115+
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
116116
Vec3::new(3.0, 8.0, 5.0),
117117
Vec3::new(0.0, 0.0, 0.0),
118118
Vec3::new(0.0, 0.0, 1.0),

0 commit comments

Comments
 (0)