Skip to content

Commit 251f72f

Browse files
committed
Fixes a bunch of examples
1 parent 3ede79a commit 251f72f

File tree

9 files changed

+90
-64
lines changed

9 files changed

+90
-64
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ pub fn camera_node_system(
159159
);
160160
}
161161

162-
let view = global_transform.compute_matrix();
162+
// TODO: camera centered RenderWorld fix
163+
let view = global_transform.compute_matrix().f32();
163164
let mut offset = 0;
164165

165166
if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW) {

crates/bevy_render/src/renderer/render_resource/render_resource.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_asset::Handle;
44

55
use bevy_core::{cast_slice, Bytes, Pod};
66
pub use bevy_derive::{RenderResource, RenderResources};
7-
use bevy_math::{Mat4, Vec2, Vec3, Vec4};
7+
use bevy_math::{Mat4, Vec2, Vec3, Vec4, F32Convert};
88
use bevy_transform::components::GlobalTransform;
99

1010
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -229,13 +229,16 @@ where
229229
}
230230
}
231231

232-
impl RenderResource for GlobalTransform {
232+
// FIXME: impl these for both precisions to allow cohabitation
233+
// of entitites with either type (and consider where else this'll matter)
234+
impl RenderResource for GlobalTransform{
233235
fn resource_type(&self) -> Option<RenderResourceType> {
234236
Some(RenderResourceType::Buffer)
235237
}
236238

237239
fn write_buffer_bytes(&self, buffer: &mut [u8]) {
238-
let mat4 = self.compute_matrix();
240+
// TODO: camera centered RenderWorld fix
241+
let mat4 = self.compute_matrix().f32();
239242
mat4.write_bytes(buffer);
240243
}
241244

crates/bevy_transform/src/components/transform.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,26 @@ mod xform_64 {
498498
}
499499
}
500500

501+
impl F64Convert for Transform64 {
502+
type F64Ver = Self;
503+
504+
#[inline]
505+
fn f64(&self) -> Self::F64Ver {
506+
*self
507+
}
508+
}
509+
impl F32Convert for Transform64 {
510+
type F32Ver = super::Transform32;
511+
512+
fn f32(&self) -> Self::F32Ver {
513+
super::Transform32 {
514+
translation: self.translation.f32(),
515+
rotation: self.rotation.f32(),
516+
scale: self.scale.f32(),
517+
}
518+
}
519+
}
520+
501521
/// Describe the position of an entity relative to the reference frame.
502522
///
503523
/// * To place or move an entity, you should set its [`Transform`].

examples/3d/load_gltf.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
1717
commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"));
1818
commands.spawn_bundle(PerspectiveCameraBundle {
19-
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
19+
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(TVec3::new(0.0, 0.3, 0.0), TVec3::Y),
2020
..Default::default()
2121
});
2222
commands
@@ -32,8 +32,8 @@ struct Rotates;
3232

3333
fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotates>>) {
3434
for mut transform in query.iter_mut() {
35-
*transform = Transform::from_rotation(Quat::from_rotation_y(
36-
(4.0 * std::f32::consts::PI / 20.0) * time.delta_seconds(),
35+
*transform = Transform::from_rotation(TQuat::from_rotation_y(
36+
(4.0 * bevy::math::real::consts::PI / 20.0) * time.delta_seconds().default_precision(),
3737
)) * *transform;
3838
}
3939
}

examples/3d/load_gltf_pipelined.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bevy::{
22
core::Time,
33
ecs::prelude::*,
4-
math::{EulerRot, Quat, Vec3},
4+
math::{EulerRot, TQuat, TVec3, TReal},
55
pbr2::{AmbientLight, DirectionalLight, DirectionalLightBundle},
66
prelude::{App, AssetServer, SpawnSceneCommands, Transform},
77
render2::{
@@ -26,7 +26,7 @@ fn main() {
2626
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2727
commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"));
2828
commands.spawn_bundle(PerspectiveCameraBundle {
29-
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
29+
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(TVec3::new(0.0, 0.3, 0.0), TVec3::Y),
3030
..Default::default()
3131
});
3232
const HALF_SIZE: f32 = 1.0;
@@ -52,11 +52,11 @@ fn animate_light_direction(
5252
mut query: Query<&mut Transform, With<DirectionalLight>>,
5353
) {
5454
for mut transform in query.iter_mut() {
55-
transform.rotation = Quat::from_euler(
55+
transform.rotation = TQuat::from_euler(
5656
EulerRot::ZYX,
5757
0.0,
58-
time.seconds_since_startup() as f32 * std::f32::consts::TAU / 10.0,
59-
-std::f32::consts::FRAC_PI_4,
58+
time.seconds_since_startup() as TReal * bevy::math::real::consts::TAU / 10.0,
59+
-bevy::math::real::consts::FRAC_PI_4,
6060
);
6161
}
6262
}

examples/game/alien_cake_addict.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use bevy::{
33
ecs::schedule::SystemSet,
44
prelude::*,
55
render::{camera::Camera, render_graph::base::camera::CAMERA_3D},
6+
math,
67
};
78
use rand::Rng;
89

@@ -45,7 +46,7 @@ fn main() {
4546
}
4647

4748
struct Cell {
48-
height: f32,
49+
height: TReal,
4950
}
5051

5152
#[derive(Default)]
@@ -70,29 +71,29 @@ struct Game {
7071
bonus: Bonus,
7172
score: i32,
7273
cake_eaten: u32,
73-
camera_should_focus: Vec3,
74-
camera_is_focus: Vec3,
74+
camera_should_focus: TVec3,
75+
camera_is_focus: TVec3,
7576
}
7677

7778
const BOARD_SIZE_I: usize = 14;
7879
const BOARD_SIZE_J: usize = 21;
7980

80-
const RESET_FOCUS: [f32; 3] = [
81-
BOARD_SIZE_I as f32 / 2.0,
81+
const RESET_FOCUS: [TReal; 3] = [
82+
BOARD_SIZE_I as TReal / 2.0,
8283
0.0,
83-
BOARD_SIZE_J as f32 / 2.0 - 0.5,
84+
BOARD_SIZE_J as TReal / 2.0 - 0.5,
8485
];
8586

8687
fn setup_cameras(mut commands: Commands, mut game: ResMut<Game>) {
87-
game.camera_should_focus = Vec3::from(RESET_FOCUS);
88+
game.camera_should_focus = TVec3::from(RESET_FOCUS);
8889
game.camera_is_focus = game.camera_should_focus;
8990
commands.spawn_bundle(PerspectiveCameraBundle {
9091
transform: Transform::from_xyz(
91-
-(BOARD_SIZE_I as f32 / 2.0),
92-
2.0 * BOARD_SIZE_J as f32 / 3.0,
93-
BOARD_SIZE_J as f32 / 2.0 - 0.5,
92+
-(BOARD_SIZE_I as TReal / 2.0),
93+
2.0 * BOARD_SIZE_J as TReal / 3.0,
94+
BOARD_SIZE_J as TReal / 2.0 - 0.5,
9495
)
95-
.looking_at(game.camera_is_focus, Vec3::Y),
96+
.looking_at(game.camera_is_focus, TVec3::Y),
9697
..Default::default()
9798
});
9899
commands.spawn_bundle(UiCameraBundle::default());
@@ -119,7 +120,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
119120
let height = rand::thread_rng().gen_range(-0.1..0.1);
120121
commands
121122
.spawn_bundle((
122-
Transform::from_xyz(i as f32, height - 0.2, j as f32),
123+
Transform::from_xyz(i as TReal, height - 0.2, j as TReal),
123124
GlobalTransform::identity(),
124125
))
125126
.with_children(|cell| {
@@ -136,12 +137,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
136137
commands
137138
.spawn_bundle((
138139
Transform {
139-
translation: Vec3::new(
140-
game.player.i as f32,
140+
translation: TVec3::new(
141+
game.player.i as TReal,
141142
game.board[game.player.j][game.player.i].height,
142-
game.player.j as f32,
143+
game.player.j as TReal,
143144
),
144-
rotation: Quat::from_rotation_y(-std::f32::consts::FRAC_PI_2),
145+
rotation: TQuat::from_rotation_y(-math::real::consts::FRAC_PI_2),
145146
..Default::default()
146147
},
147148
GlobalTransform::identity(),
@@ -199,21 +200,21 @@ fn move_player(
199200
if game.player.i < BOARD_SIZE_I - 1 {
200201
game.player.i += 1;
201202
}
202-
rotation = -std::f32::consts::FRAC_PI_2;
203+
rotation = -math::real::consts::FRAC_PI_2;
203204
moved = true;
204205
}
205206
if keyboard_input.just_pressed(KeyCode::Down) {
206207
if game.player.i > 0 {
207208
game.player.i -= 1;
208209
}
209-
rotation = std::f32::consts::FRAC_PI_2;
210+
rotation = math::real::consts::FRAC_PI_2;
210211
moved = true;
211212
}
212213
if keyboard_input.just_pressed(KeyCode::Right) {
213214
if game.player.j < BOARD_SIZE_J - 1 {
214215
game.player.j += 1;
215216
}
216-
rotation = std::f32::consts::PI;
217+
rotation = math::real::consts::PI;
217218
moved = true;
218219
}
219220
if keyboard_input.just_pressed(KeyCode::Left) {
@@ -227,12 +228,12 @@ fn move_player(
227228
// move on the board
228229
if moved {
229230
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
230-
translation: Vec3::new(
231-
game.player.i as f32,
231+
translation: TVec3::new(
232+
game.player.i as TReal,
232233
game.board[game.player.j][game.player.i].height,
233-
game.player.j as f32,
234+
game.player.j as TReal,
234235
),
235-
rotation: Quat::from_rotation_y(rotation),
236+
rotation: TQuat::from_rotation_y(rotation),
236237
..Default::default()
237238
};
238239
}
@@ -254,7 +255,7 @@ fn focus_camera(
254255
mut game: ResMut<Game>,
255256
mut transforms: QuerySet<(Query<(&mut Transform, &Camera)>, Query<&Transform>)>,
256257
) {
257-
const SPEED: f32 = 2.0;
258+
const SPEED: TReal = 2.0;
258259
// if there is both a player and a bonus, target the mid-point of them
259260
if let (Some(player_entity), Some(bonus_entity)) = (game.player.entity, game.bonus.entity) {
260261
if let (Ok(player_transform), Ok(bonus_transform)) = (
@@ -272,21 +273,21 @@ fn focus_camera(
272273
}
273274
// otherwise, target the middle
274275
} else {
275-
game.camera_should_focus = Vec3::from(RESET_FOCUS);
276+
game.camera_should_focus = TVec3::from(RESET_FOCUS);
276277
}
277278
// calculate the camera motion based on the difference between where the camera is looking
278279
// and where it should be looking; the greater the distance, the faster the motion;
279280
// smooth out the camera movement using the frame time
280281
let mut camera_motion = game.camera_should_focus - game.camera_is_focus;
281282
if camera_motion.length() > 0.2 {
282-
camera_motion *= SPEED * time.delta_seconds();
283+
camera_motion *= SPEED * time.delta_seconds().default_precision();
283284
// set the new camera's actual focus
284285
game.camera_is_focus += camera_motion;
285286
}
286287
// look at that new camera's actual focus
287288
for (mut transform, camera) in transforms.q0_mut().iter_mut() {
288289
if camera.name == Some(CAMERA_3D.to_string()) {
289-
*transform = transform.looking_at(game.camera_is_focus, Vec3::Y);
290+
*transform = transform.looking_at(game.camera_is_focus, TVec3::Y);
290291
}
291292
}
292293
}
@@ -322,10 +323,10 @@ fn spawn_bonus(
322323
commands
323324
.spawn_bundle((
324325
Transform {
325-
translation: Vec3::new(
326-
game.bonus.i as f32,
326+
translation: TVec3::new(
327+
game.bonus.i as TReal,
327328
game.board[game.bonus.j][game.bonus.i].height + 0.2,
328-
game.bonus.j as f32,
329+
game.bonus.j as TReal,
329330
),
330331
..Default::default()
331332
},
@@ -342,9 +343,9 @@ fn spawn_bonus(
342343
fn rotate_bonus(game: Res<Game>, time: Res<Time>, mut transforms: Query<&mut Transform>) {
343344
if let Some(entity) = game.bonus.entity {
344345
if let Ok(mut cake_transform) = transforms.get_mut(entity) {
345-
cake_transform.rotate(Quat::from_rotation_y(time.delta_seconds()));
346-
cake_transform.scale = Vec3::splat(
347-
1.0 + (game.score as f32 / 10.0 * time.seconds_since_startup().sin() as f32).abs(),
346+
cake_transform.rotate(TQuat::from_rotation_y(time.delta_seconds().default_precision()));
347+
cake_transform.scale = TVec3::splat(
348+
1.0 + (game.score as TReal / 10.0 * time.seconds_since_startup().sin() as TReal).abs(),
348349
);
349350
}
350351
}

examples/ios/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn setup_scene(
5252
});
5353
// camera
5454
commands.spawn_bundle(PerspectiveCameraBundle {
55-
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
55+
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(TVec3::ZERO, TVec3::Y),
5656
..Default::default()
5757
});
5858
}

examples/tools/bevymark.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ use rand::Rng;
66

77
const BIRDS_PER_SECOND: u32 = 1000;
88
const BASE_COLOR: Color = Color::rgb(5.0, 5.0, 5.0);
9-
const GRAVITY: f32 = -9.8 * 100.0;
10-
const MAX_VELOCITY: f32 = 750.;
11-
const BIRD_SCALE: f32 = 0.15;
12-
const HALF_BIRD_SIZE: f32 = 256. * BIRD_SCALE * 0.5;
9+
const GRAVITY: TReal = -9.8 * 100.0;
10+
const MAX_VELOCITY: TReal = 750.;
11+
const BIRD_SCALE: TReal = 0.15;
12+
const HALF_BIRD_SIZE: TReal = 256. * BIRD_SCALE * 0.5;
1313

1414
struct BevyCounter {
1515
pub count: u128,
1616
}
1717

1818
struct Bird {
19-
velocity: Vec3,
19+
velocity: TVec3,
2020
}
2121

2222
struct BirdMaterial(Handle<ColorMaterial>);
@@ -131,24 +131,24 @@ fn mouse_handler(
131131

132132
if mouse_button_input.pressed(MouseButton::Left) {
133133
let spawn_count = (BIRDS_PER_SECOND as f32 * time.delta_seconds()) as u128;
134-
let bird_x = (window.width / -2.) + HALF_BIRD_SIZE;
135-
let bird_y = (window.height / 2.) - HALF_BIRD_SIZE;
134+
let bird_x = (window.width / -2.).default_precision() + HALF_BIRD_SIZE;
135+
let bird_y = (window.height / 2.).default_precision() - HALF_BIRD_SIZE;
136136

137137
for count in 0..spawn_count {
138-
let bird_z = (counter.count + count) as f32 * 0.00001;
138+
let bird_z = (counter.count + count) as TReal * 0.00001;
139139
commands
140140
.spawn_bundle(SpriteBundle {
141141
material: bird_material.0.clone(),
142142
transform: Transform {
143-
translation: Vec3::new(bird_x, bird_y, bird_z),
144-
scale: Vec3::splat(BIRD_SCALE),
143+
translation: TVec3::new(bird_x, bird_y, bird_z),
144+
scale: TVec3::splat(BIRD_SCALE),
145145
..Default::default()
146146
},
147147
..Default::default()
148148
})
149149
.insert(Bird {
150-
velocity: Vec3::new(
151-
rand::random::<f32>() * MAX_VELOCITY - (MAX_VELOCITY * 0.5),
150+
velocity: TVec3::new(
151+
rand::random::<TReal>() * MAX_VELOCITY - (MAX_VELOCITY * 0.5),
152152
0.,
153153
0.,
154154
),
@@ -161,15 +161,15 @@ fn mouse_handler(
161161

162162
fn movement_system(time: Res<Time>, mut bird_query: Query<(&mut Bird, &mut Transform)>) {
163163
for (mut bird, mut transform) in bird_query.iter_mut() {
164-
transform.translation.x += bird.velocity.x * time.delta_seconds();
165-
transform.translation.y += bird.velocity.y * time.delta_seconds();
166-
bird.velocity.y += GRAVITY * time.delta_seconds();
164+
transform.translation.x += bird.velocity.x * time.delta_seconds().default_precision();
165+
transform.translation.y += bird.velocity.y * time.delta_seconds().default_precision();
166+
bird.velocity.y += GRAVITY * time.delta_seconds().default_precision();
167167
}
168168
}
169169

170170
fn collision_system(window: Res<WindowDescriptor>, mut bird_query: Query<(&mut Bird, &Transform)>) {
171-
let half_width = window.width as f32 * 0.5;
172-
let half_height = window.height as f32 * 0.5;
171+
let half_width = window.width as TReal * 0.5;
172+
let half_height = window.height as TReal * 0.5;
173173

174174
for (mut bird, transform) in bird_query.iter_mut() {
175175
let x_vel = bird.velocity.x;

pipelined/bevy_pbr2/src/render/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ pub fn queue_meshes(
588588
mesh_meta.mesh_draw_info.clear();
589589
mesh_meta.material_bind_groups.next_frame();
590590

591-
let view_matrix = view.transform.compute_matrix();
591+
// TODO: camera centered RenderWorld fix
592+
let view_matrix = view.transform.compute_matrix().f32();
592593
let view_row_2 = view_matrix.row(2);
593594
for (i, mesh) in extracted_meshes.meshes.iter_mut().enumerate() {
594595
let gpu_material = &render_materials

0 commit comments

Comments
 (0)