Skip to content

Commit

Permalink
Fixes a bunch of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dman-os committed Aug 14, 2021
1 parent 3ede79a commit 251f72f
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 64 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_render/src/render_graph/nodes/camera_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ pub fn camera_node_system(
);
}

let view = global_transform.compute_matrix();
// TODO: camera centered RenderWorld fix
let view = global_transform.compute_matrix().f32();
let mut offset = 0;

if let Some(RenderResourceBinding::Buffer { buffer, .. }) = bindings.get(CAMERA_VIEW) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_asset::Handle;

use bevy_core::{cast_slice, Bytes, Pod};
pub use bevy_derive::{RenderResource, RenderResources};
use bevy_math::{Mat4, Vec2, Vec3, Vec4};
use bevy_math::{Mat4, Vec2, Vec3, Vec4, F32Convert};
use bevy_transform::components::GlobalTransform;

#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -229,13 +229,16 @@ where
}
}

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

fn write_buffer_bytes(&self, buffer: &mut [u8]) {
let mat4 = self.compute_matrix();
// TODO: camera centered RenderWorld fix
let mat4 = self.compute_matrix().f32();
mat4.write_bytes(buffer);
}

Expand Down
20 changes: 20 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,26 @@ mod xform_64 {
}
}

impl F64Convert for Transform64 {
type F64Ver = Self;

#[inline]
fn f64(&self) -> Self::F64Ver {
*self
}
}
impl F32Convert for Transform64 {
type F32Ver = super::Transform32;

fn f32(&self) -> Self::F32Ver {
super::Transform32 {
translation: self.translation.f32(),
rotation: self.rotation.f32(),
scale: self.scale.f32(),
}
}
}

/// Describe the position of an entity relative to the reference frame.
///
/// * To place or move an entity, you should set its [`Transform`].
Expand Down
6 changes: 3 additions & 3 deletions examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"));
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(TVec3::new(0.0, 0.3, 0.0), TVec3::Y),
..Default::default()
});
commands
Expand All @@ -32,8 +32,8 @@ struct Rotates;

fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotates>>) {
for mut transform in query.iter_mut() {
*transform = Transform::from_rotation(Quat::from_rotation_y(
(4.0 * std::f32::consts::PI / 20.0) * time.delta_seconds(),
*transform = Transform::from_rotation(TQuat::from_rotation_y(
(4.0 * bevy::math::real::consts::PI / 20.0) * time.delta_seconds().default_precision(),
)) * *transform;
}
}
10 changes: 5 additions & 5 deletions examples/3d/load_gltf_pipelined.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{
core::Time,
ecs::prelude::*,
math::{EulerRot, Quat, Vec3},
math::{EulerRot, TQuat, TVec3, TReal},
pbr2::{AmbientLight, DirectionalLight, DirectionalLightBundle},
prelude::{App, AssetServer, SpawnSceneCommands, Transform},
render2::{
Expand All @@ -26,7 +26,7 @@ fn main() {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"));
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(TVec3::new(0.0, 0.3, 0.0), TVec3::Y),
..Default::default()
});
const HALF_SIZE: f32 = 1.0;
Expand All @@ -52,11 +52,11 @@ fn animate_light_direction(
mut query: Query<&mut Transform, With<DirectionalLight>>,
) {
for mut transform in query.iter_mut() {
transform.rotation = Quat::from_euler(
transform.rotation = TQuat::from_euler(
EulerRot::ZYX,
0.0,
time.seconds_since_startup() as f32 * std::f32::consts::TAU / 10.0,
-std::f32::consts::FRAC_PI_4,
time.seconds_since_startup() as TReal * bevy::math::real::consts::TAU / 10.0,
-bevy::math::real::consts::FRAC_PI_4,
);
}
}
67 changes: 34 additions & 33 deletions examples/game/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy::{
ecs::schedule::SystemSet,
prelude::*,
render::{camera::Camera, render_graph::base::camera::CAMERA_3D},
math,
};
use rand::Rng;

Expand Down Expand Up @@ -45,7 +46,7 @@ fn main() {
}

struct Cell {
height: f32,
height: TReal,
}

#[derive(Default)]
Expand All @@ -70,29 +71,29 @@ struct Game {
bonus: Bonus,
score: i32,
cake_eaten: u32,
camera_should_focus: Vec3,
camera_is_focus: Vec3,
camera_should_focus: TVec3,
camera_is_focus: TVec3,
}

const BOARD_SIZE_I: usize = 14;
const BOARD_SIZE_J: usize = 21;

const RESET_FOCUS: [f32; 3] = [
BOARD_SIZE_I as f32 / 2.0,
const RESET_FOCUS: [TReal; 3] = [
BOARD_SIZE_I as TReal / 2.0,
0.0,
BOARD_SIZE_J as f32 / 2.0 - 0.5,
BOARD_SIZE_J as TReal / 2.0 - 0.5,
];

fn setup_cameras(mut commands: Commands, mut game: ResMut<Game>) {
game.camera_should_focus = Vec3::from(RESET_FOCUS);
game.camera_should_focus = TVec3::from(RESET_FOCUS);
game.camera_is_focus = game.camera_should_focus;
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(
-(BOARD_SIZE_I as f32 / 2.0),
2.0 * BOARD_SIZE_J as f32 / 3.0,
BOARD_SIZE_J as f32 / 2.0 - 0.5,
-(BOARD_SIZE_I as TReal / 2.0),
2.0 * BOARD_SIZE_J as TReal / 3.0,
BOARD_SIZE_J as TReal / 2.0 - 0.5,
)
.looking_at(game.camera_is_focus, Vec3::Y),
.looking_at(game.camera_is_focus, TVec3::Y),
..Default::default()
});
commands.spawn_bundle(UiCameraBundle::default());
Expand All @@ -119,7 +120,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
let height = rand::thread_rng().gen_range(-0.1..0.1);
commands
.spawn_bundle((
Transform::from_xyz(i as f32, height - 0.2, j as f32),
Transform::from_xyz(i as TReal, height - 0.2, j as TReal),
GlobalTransform::identity(),
))
.with_children(|cell| {
Expand All @@ -136,12 +137,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
commands
.spawn_bundle((
Transform {
translation: Vec3::new(
game.player.i as f32,
translation: TVec3::new(
game.player.i as TReal,
game.board[game.player.j][game.player.i].height,
game.player.j as f32,
game.player.j as TReal,
),
rotation: Quat::from_rotation_y(-std::f32::consts::FRAC_PI_2),
rotation: TQuat::from_rotation_y(-math::real::consts::FRAC_PI_2),
..Default::default()
},
GlobalTransform::identity(),
Expand Down Expand Up @@ -199,21 +200,21 @@ fn move_player(
if game.player.i < BOARD_SIZE_I - 1 {
game.player.i += 1;
}
rotation = -std::f32::consts::FRAC_PI_2;
rotation = -math::real::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Down) {
if game.player.i > 0 {
game.player.i -= 1;
}
rotation = std::f32::consts::FRAC_PI_2;
rotation = math::real::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Right) {
if game.player.j < BOARD_SIZE_J - 1 {
game.player.j += 1;
}
rotation = std::f32::consts::PI;
rotation = math::real::consts::PI;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Left) {
Expand All @@ -227,12 +228,12 @@ fn move_player(
// move on the board
if moved {
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
translation: Vec3::new(
game.player.i as f32,
translation: TVec3::new(
game.player.i as TReal,
game.board[game.player.j][game.player.i].height,
game.player.j as f32,
game.player.j as TReal,
),
rotation: Quat::from_rotation_y(rotation),
rotation: TQuat::from_rotation_y(rotation),
..Default::default()
};
}
Expand All @@ -254,7 +255,7 @@ fn focus_camera(
mut game: ResMut<Game>,
mut transforms: QuerySet<(Query<(&mut Transform, &Camera)>, Query<&Transform>)>,
) {
const SPEED: f32 = 2.0;
const SPEED: TReal = 2.0;
// if there is both a player and a bonus, target the mid-point of them
if let (Some(player_entity), Some(bonus_entity)) = (game.player.entity, game.bonus.entity) {
if let (Ok(player_transform), Ok(bonus_transform)) = (
Expand All @@ -272,21 +273,21 @@ fn focus_camera(
}
// otherwise, target the middle
} else {
game.camera_should_focus = Vec3::from(RESET_FOCUS);
game.camera_should_focus = TVec3::from(RESET_FOCUS);
}
// calculate the camera motion based on the difference between where the camera is looking
// and where it should be looking; the greater the distance, the faster the motion;
// smooth out the camera movement using the frame time
let mut camera_motion = game.camera_should_focus - game.camera_is_focus;
if camera_motion.length() > 0.2 {
camera_motion *= SPEED * time.delta_seconds();
camera_motion *= SPEED * time.delta_seconds().default_precision();
// set the new camera's actual focus
game.camera_is_focus += camera_motion;
}
// look at that new camera's actual focus
for (mut transform, camera) in transforms.q0_mut().iter_mut() {
if camera.name == Some(CAMERA_3D.to_string()) {
*transform = transform.looking_at(game.camera_is_focus, Vec3::Y);
*transform = transform.looking_at(game.camera_is_focus, TVec3::Y);
}
}
}
Expand Down Expand Up @@ -322,10 +323,10 @@ fn spawn_bonus(
commands
.spawn_bundle((
Transform {
translation: Vec3::new(
game.bonus.i as f32,
translation: TVec3::new(
game.bonus.i as TReal,
game.board[game.bonus.j][game.bonus.i].height + 0.2,
game.bonus.j as f32,
game.bonus.j as TReal,
),
..Default::default()
},
Expand All @@ -342,9 +343,9 @@ fn spawn_bonus(
fn rotate_bonus(game: Res<Game>, time: Res<Time>, mut transforms: Query<&mut Transform>) {
if let Some(entity) = game.bonus.entity {
if let Ok(mut cake_transform) = transforms.get_mut(entity) {
cake_transform.rotate(Quat::from_rotation_y(time.delta_seconds()));
cake_transform.scale = Vec3::splat(
1.0 + (game.score as f32 / 10.0 * time.seconds_since_startup().sin() as f32).abs(),
cake_transform.rotate(TQuat::from_rotation_y(time.delta_seconds().default_precision()));
cake_transform.scale = TVec3::splat(
1.0 + (game.score as TReal / 10.0 * time.seconds_since_startup().sin() as TReal).abs(),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ios/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn setup_scene(
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(TVec3::ZERO, TVec3::Y),
..Default::default()
});
}
Expand Down
34 changes: 17 additions & 17 deletions examples/tools/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use rand::Rng;

const BIRDS_PER_SECOND: u32 = 1000;
const BASE_COLOR: Color = Color::rgb(5.0, 5.0, 5.0);
const GRAVITY: f32 = -9.8 * 100.0;
const MAX_VELOCITY: f32 = 750.;
const BIRD_SCALE: f32 = 0.15;
const HALF_BIRD_SIZE: f32 = 256. * BIRD_SCALE * 0.5;
const GRAVITY: TReal = -9.8 * 100.0;
const MAX_VELOCITY: TReal = 750.;
const BIRD_SCALE: TReal = 0.15;
const HALF_BIRD_SIZE: TReal = 256. * BIRD_SCALE * 0.5;

struct BevyCounter {
pub count: u128,
}

struct Bird {
velocity: Vec3,
velocity: TVec3,
}

struct BirdMaterial(Handle<ColorMaterial>);
Expand Down Expand Up @@ -131,24 +131,24 @@ fn mouse_handler(

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

for count in 0..spawn_count {
let bird_z = (counter.count + count) as f32 * 0.00001;
let bird_z = (counter.count + count) as TReal * 0.00001;
commands
.spawn_bundle(SpriteBundle {
material: bird_material.0.clone(),
transform: Transform {
translation: Vec3::new(bird_x, bird_y, bird_z),
scale: Vec3::splat(BIRD_SCALE),
translation: TVec3::new(bird_x, bird_y, bird_z),
scale: TVec3::splat(BIRD_SCALE),
..Default::default()
},
..Default::default()
})
.insert(Bird {
velocity: Vec3::new(
rand::random::<f32>() * MAX_VELOCITY - (MAX_VELOCITY * 0.5),
velocity: TVec3::new(
rand::random::<TReal>() * MAX_VELOCITY - (MAX_VELOCITY * 0.5),
0.,
0.,
),
Expand All @@ -161,15 +161,15 @@ fn mouse_handler(

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

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

for (mut bird, transform) in bird_query.iter_mut() {
let x_vel = bird.velocity.x;
Expand Down
3 changes: 2 additions & 1 deletion pipelined/bevy_pbr2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ pub fn queue_meshes(
mesh_meta.mesh_draw_info.clear();
mesh_meta.material_bind_groups.next_frame();

let view_matrix = view.transform.compute_matrix();
// TODO: camera centered RenderWorld fix
let view_matrix = view.transform.compute_matrix().f32();
let view_row_2 = view_matrix.row(2);
for (i, mesh) in extracted_meshes.meshes.iter_mut().enumerate() {
let gpu_material = &render_materials
Expand Down

0 comments on commit 251f72f

Please sign in to comment.