update to bevy 0.15-rc.3

This commit is contained in:
Joe Ardent 2024-11-27 11:14:24 -08:00
parent 5bb6d10558
commit c366dc3ec6
4 changed files with 2165 additions and 1199 deletions

3215
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
argh = "0.1.12" argh = "0.1"
bevy = "0.12.0" bevy = "0.15.0-rc.3"
bevy_spatial = "0.7.0" bevy_spatial = { git = "https://github.com/laundmo/bevy-spatial.git", rev = "9ac313ac6173b440f50954b696dc2063513b4f37" } #"0.10.0"
rand = "0.8.5" rand = "0.8"

View file

@ -8,7 +8,7 @@ use bevy_spatial::{kdtree::KDTree3, SpatialAccess};
pub type NNTree = KDTree3<Toid>; pub type NNTree = KDTree3<Toid>;
// toid stuff // toid stuff
const SPEED: f32 = 0.2; const SPEED: f32 = 10.0;
const SPEED_DIFF_RANGE: f32 = 0.1; // +/- 10% const SPEED_DIFF_RANGE: f32 = 0.1; // +/- 10%
const MAX_DELTA_V: f32 = std::f32::consts::PI * 2.0; // basically 360 degrees/sec const MAX_DELTA_V: f32 = std::f32::consts::PI * 2.0; // basically 360 degrees/sec
@ -59,23 +59,19 @@ pub fn turkey_time(
let z = r.gen_range(-10.0..=10.0); let z = r.gen_range(-10.0..=10.0);
let y = r.gen_range(0.1..=5.5); let y = r.gen_range(0.1..=5.5);
let pos = Vec3::new(x, MIN_ALTITUDE + y, z); let pos = Vec3::new(x, MIN_ALTITUDE + y, z);
let spatial_bundle = SpatialBundle { let xform = Transform::from_translation(pos);
transform: Transform::from_translation(pos), //dbg!(xform);
..Default::default() let spatial_bundle = (xform, Visibility::Visible);
};
commands commands
.spawn(spatial_bundle) .spawn(spatial_bundle)
.insert((Velocity(vel), Buddies::default(), Toid { speed, buddies })) .insert((Velocity(vel), Buddies::default(), Toid { speed, buddies }))
.with_children(|t| { .with_children(|t| {
t.spawn(SceneBundle { t.spawn(SceneRoot(scene.to_owned()))
scene: scene.to_owned(), .insert(Transform::default());
..Default::default() // .insert(Transform::from_rotation(Quat::from_axis_angle(
}) // Vec3::Y,
// .insert(Transform::default()); // -std::f32::consts::FRAC_PI_2,
.insert(Transform::from_rotation(Quat::from_axis_angle( // )));
Vec3::Y,
-std::f32::consts::FRAC_PI_2,
)));
}) })
.id() .id()
} }
@ -86,7 +82,7 @@ pub fn update_vel(
positions: Res<Positions>, positions: Res<Positions>,
index: Res<NNTree>, index: Res<NNTree>,
) { ) {
let dt = time.delta_seconds(); let dt = time.delta_secs();
let max_delta = MAX_DELTA_V * dt; let max_delta = MAX_DELTA_V * dt;
for (xform, mut vel, toid, buddies, entity) in toids.iter_mut() { for (xform, mut vel, toid, buddies, entity) in toids.iter_mut() {
let speed = toid.speed; let speed = toid.speed;
@ -160,13 +156,11 @@ pub fn update_pos(
time: Res<Time>, time: Res<Time>,
) { ) {
let mut new_look = Vec3::ZERO; let mut new_look = Vec3::ZERO;
let dt = time.delta_seconds(); let dt = time.delta_secs();
for (mut xform, vel, entity) in toids.iter_mut() { for (mut xform, vel, entity) in toids.iter_mut() {
xform.translation += **vel * dt; xform.translation += **vel * dt;
let look_at = xform.translation + **vel; let look_at = vel.normalize();
let right = look_at.cross(Vec3::Y).normalize(); xform.look_to(look_at, Vec3::Y);
let up = right.cross(look_at).normalize();
xform.look_at(look_at, up);
*positions.entry(entity).or_insert(Vec3::ZERO) = xform.translation; *positions.entry(entity).or_insert(Vec3::ZERO) = xform.translation;
new_look += xform.translation; new_look += xform.translation;
} }
@ -205,40 +199,41 @@ pub fn update_buddies(
pub fn update_gizmos(toids: Query<&Transform, With<Toid>>, mut gizmos: Gizmos) { pub fn update_gizmos(toids: Query<&Transform, With<Toid>>, mut gizmos: Gizmos) {
for toid in toids.iter() { for toid in toids.iter() {
let pos = toid.translation; let pos = toid.translation;
let up = toid.up(); //dbg!(toid);
let forward = toid.forward(); let up = toid.up().as_vec3();
let right = toid.right(); let forward = toid.forward().as_vec3();
gizmos.ray(pos, pos + forward, Color::RED); let right = toid.right().as_vec3();
gizmos.ray(pos, pos + up, Color::BLUE); gizmos.ray(pos, pos + forward, Color::srgb_u8(255, 0, 0));
gizmos.ray(pos, pos + right, Color::GREEN); //gizmos.ray(pos, pos + up, Color::srgb_u8(0, 0, 255));
//gizmos.ray(pos, pos + right, Color::srgb_u8(0, 255, 0));
} }
} }
pub fn rotate_camera( pub fn rotate_camera(
mut query: Query<&mut Transform, With<Camera>>, mut query: Query<&mut Transform, With<Camera>>,
keys: Res<Input<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
lookat: Res<LookAt>, lookat: Res<LookAt>,
) { ) {
let mut xform = query.single_mut(); let mut xform = query.single_mut();
let forward = xform.forward() * 0.7; let forward = xform.forward() * 0.7;
let right = xform.right(); let right = xform.right().as_vec3();
if keys.pressed(KeyCode::Right) { if keys.pressed(KeyCode::ArrowRight) {
xform.translation += right; xform.translation += right;
} }
if keys.pressed(KeyCode::Left) { if keys.pressed(KeyCode::ArrowLeft) {
xform.translation -= right; xform.translation -= right;
} }
if keys.pressed(KeyCode::Up) { if keys.pressed(KeyCode::ArrowUp) {
if keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight) { if keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight) {
xform.translation += Vec3::Y; xform.translation += Vec3::Y;
} else { } else {
xform.translation += forward; xform.translation += forward;
} }
} }
if keys.pressed(KeyCode::Down) { if keys.pressed(KeyCode::ArrowDown) {
if keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight) { if keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight) {
xform.translation -= Vec3::Y; xform.translation -= Vec3::Y;
} else { } else {
@ -256,7 +251,7 @@ pub fn rotate_camera(
pub fn unit_vec(r: &mut impl rand::Rng) -> Vec3 { pub fn unit_vec(r: &mut impl rand::Rng) -> Vec3 {
let mut x1: f32 = 0.0; let mut x1: f32 = 0.0;
let mut x2: f32 = 0.0; let mut x2: f32 = 0.0;
let mut ssum = std::f32::MAX; let mut ssum = f32::MAX;
while ssum >= 1.0 { while ssum >= 1.0 {
x1 = r.gen_range(-1.0..=1.0); x1 = r.gen_range(-1.0..=1.0);
x2 = r.gen_range(-1.0..=1.0); x2 = r.gen_range(-1.0..=1.0);

View file

@ -2,6 +2,7 @@ use std::time::Duration;
use audubon::*; use audubon::*;
use bevy::{ use bevy::{
color::palettes::css::BLACK,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*, prelude::*,
}; };
@ -20,16 +21,16 @@ fn main() {
)) ))
.insert_resource(config) .insert_resource(config)
.insert_resource(Positions::default()) .insert_resource(Positions::default())
.insert_resource(ClearColor(Color::rgb(0.64, 0.745, 0.937))) // a nice light blue .insert_resource(ClearColor(Color::srgb(0.64, 0.745, 0.937))) // a nice light blue
.insert_resource(AmbientLight { .insert_resource(AmbientLight {
color: Color::WHITE, color: Color::WHITE,
brightness: 1.0, brightness: 1.0,
}) })
.insert_resource(LookAt::default()) .insert_resource(LookAt::default())
.add_systems(Startup, setup) .add_systems(Startup, (setup, setup_ambient_light))
//.add_systems(Update, update_gizmos)
.add_systems(Update, (update_pos, update_buddies, update_vel).chain()) .add_systems(Update, (update_pos, update_buddies, update_vel).chain())
.add_systems(Update, (rotate_camera, bevy::window::close_on_esc)) .add_systems(Update, (rotate_camera, close_on_esc))
.add_systems(Update, update_gizmos)
.run(); .run();
} }
@ -41,38 +42,61 @@ fn setup(
models: Res<AssetServer>, models: Res<AssetServer>,
) { ) {
let rand = &mut rand::thread_rng(); let rand = &mut rand::thread_rng();
let cam = Camera3d::default();
commands.spawn(Camera3dBundle { let camera = commands
transform: Transform::from_xyz(0., 5.0, 25.).looking_at(Vec3::new(0.0, 5.0, 0.0), Vec3::Y), .spawn((
..default() Transform::from_xyz(0., 5.0, 25.).looking_at(Vec3::new(0.0, 5.0, 0.0), Vec3::Y),
}); cam,
Visibility::Hidden,
))
.id();
// plane // plane
commands.spawn(PbrBundle { commands
mesh: meshes.add(Mesh::from(shape::Plane::from_size(500.0))), .spawn(Mesh3d(
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), meshes.add(Mesh::from(Plane3d::default().mesh().size(500.0, 500.0))),
..default() ))
}); .insert(MeshMaterial3d(materials.add(Color::srgb(0.3, 1.0, 0.3))));
let toid_model = models.load("models/boid.glb#Scene0"); let toid_model = models.load("models/toid3.glb#Scene0");
for _ in 0..config.toids { for _ in 0..config.toids {
let _ = turkey_time(&mut commands, &toid_model, rand); let _ = turkey_time(&mut commands, &toid_model, rand);
} }
// instructions // instructions
commands.spawn( commands.spawn((
TextBundle::from_section( Text::new(
"Up and down for camera forward and back; hold shift to change height.\nLeft or right to move left or right.\nPress 'ESC' to quit.", "Up and down for camera forward and back; hold shift to change height.\nLeft or right to move left or right.\nPress 'ESC' to quit."), TextFont {
TextStyle { font_size: 20.0,
font_size: 20., ..Default::default()
..default() },
}, Node {
)
.with_style(Style {
position_type: PositionType::Absolute, position_type: PositionType::Absolute,
top: Val::Px(12.0), top: Val::Px(12.0),
left: Val::Px(12.0), left: Val::Px(12.0),
..default() ..default()
}), },
); TargetCamera(camera),
TextColor::WHITE));
}
fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}
fn setup_ambient_light(mut ambient_light: ResMut<AmbientLight>) {
ambient_light.brightness = 100.0;
} }