based off the bevy gizmos example

This commit is contained in:
Joe Ardent 2023-11-24 17:09:10 -08:00
commit 2ad953c75e
8 changed files with 4168 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

3985
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

12
Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "audubon"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
argh = "0.1.12"
bevy = "0.12.0"
rand = "0.8.5"
rstar = "0.11.0"

5
LICENSE.md Normal file
View file

@ -0,0 +1,5 @@
# The Chaos License (GLP)
This software is released under the terms of the Chaos License. In cases where the terms of the
license are unclear, refer to the [Fuck Around and Find Out
License](https://git.sr.ht/~boringcactus/fafol/tree/master/LICENSE-v0.2.md).

BIN
assets/models/boid.glb Normal file

Binary file not shown.

62
src/lib.rs Normal file
View file

@ -0,0 +1,62 @@
use argh::FromArgs;
use bevy::prelude::*;
#[derive(Debug, FromArgs, Resource)]
/// Toid Watching
pub struct Config {
/// how many Toids to spawn
#[argh(option, short = 't', default = "100")]
pub toids: usize,
}
pub type Point = (f32, f32, f32);
pub type Toint = rstar::primitives::GeomWithData<Point, Entity>;
pub trait Pointable {
fn to_point(&self) -> Point;
}
impl Pointable for Vec3 {
fn to_point(&self) -> Point {
(self.x, self.y, self.z)
}
}
#[derive(Resource, Deref, DerefMut)]
pub struct Index(pub rstar::RTree<Toint>);
#[derive(Component, Debug, Clone, Deref, DerefMut, Default)]
pub struct Buddies(Vec<Entity>);
#[derive(Component, Debug, Clone, Deref, DerefMut, Default)]
pub struct Velocity(Vec3);
#[derive(Component)]
pub struct Toid;
pub fn turkey_time(commands: &mut Commands, scene: &Handle<Scene>) -> Entity {
commands
.spawn(SpatialBundle::default())
.insert((Velocity::default(), Buddies::default(), Toid))
.with_children(|t| {
t.spawn(SceneBundle {
scene: scene.to_owned(),
..Default::default()
})
.insert(Transform::from_rotation(Quat::from_axis_angle(
Vec3::Y,
-std::f32::consts::FRAC_PI_2,
)));
})
.id()
}
pub fn add_gizmos(mut gizmos: Gizmos, toids: Query<(&Transform, Entity), With<Toid>>) {
let gizmos = &mut gizmos;
for (pos, _entity) in toids.iter() {
let nudge = pos.up() * 0.15;
let rpos = pos.translation + nudge;
gizmos.ray(rpos, pos.forward(), Color::RED);
}
}

103
src/main.rs Normal file
View file

@ -0,0 +1,103 @@
//use std::f32::consts::PI;
use bevy::prelude::*;
use audubon::*;
fn main() {
let config: audubon::Config = argh::from_env();
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(config)
.add_systems(Startup, setup)
.add_systems(
Update,
(
add_gizmos,
rotate_camera,
update_config,
bevy::window::close_on_esc,
),
)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
config: Res<Config>,
models: Res<AssetServer>,
) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
let toid_model = models.load("models/boid.glb#Scene0");
for _ in 0..config.toids {
let _ = turkey_time(&mut commands, &toid_model);
}
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
// example instructions
commands.spawn(
TextBundle::from_section(
"Press 'D' to toggle drawing gizmos on top of everything else in the scene\n\
Press 'P' to toggle perspective for line gizmos\n\
Hold 'Left' or 'Right' to change the line width",
TextStyle {
font_size: 20.,
..default()
},
)
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
}),
);
}
fn rotate_camera(mut query: Query<&mut Transform, With<Camera>>, time: Res<Time>) {
let mut transform = query.single_mut();
transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(time.delta_seconds() / 2.));
}
fn update_config(mut config: ResMut<GizmoConfig>, keyboard: Res<Input<KeyCode>>, time: Res<Time>) {
if keyboard.just_pressed(KeyCode::D) {
config.depth_bias = if config.depth_bias == 0. { -1. } else { 0. };
}
if keyboard.just_pressed(KeyCode::P) {
// Toggle line_perspective
config.line_perspective ^= true;
// Increase the line width when line_perspective is on
config.line_width *= if config.line_perspective { 5. } else { 1. / 5. };
}
if keyboard.pressed(KeyCode::Right) {
config.line_width += 5. * time.delta_seconds();
}
if keyboard.pressed(KeyCode::Left) {
config.line_width -= 5. * time.delta_seconds();
}
}

0
src/systems.rs Normal file
View file