Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

View File

@@ -0,0 +1,166 @@
use std::ops::Deref;
use std::time::Duration;
use bevy::{
color::palettes::css as csscolors,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
math::Vec3Swizzles,
prelude::*,
window::PrimaryWindow,
};
use bevy_spatial::{AutomaticUpdate, SpatialStructure};
use bevy_spatial::{SpatialAccess, kdtree::KDTree2};
// marker for entities tracked by the KDTree
#[derive(Component, Default)]
struct NearestNeighbourComponent;
// marker for the "cursor" entity
#[derive(Component)]
struct Cursor;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
present_mode: bevy::window::PresentMode::AutoNoVsync,
..default()
}),
..default()
}))
// Add the plugin, which takes the tracked component as a generic.
.add_plugins(
AutomaticUpdate::<NearestNeighbourComponent>::new()
.with_spatial_ds(SpatialStructure::KDTree2)
.with_frequency(Duration::from_millis(1)),
)
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.insert_resource(Mouse2D { pos: Vec2::ZERO })
.add_systems(Startup, setup)
.add_systems(
Update,
(
update_mouse_pos,
(
mouse,
color,
reset_color.before(color),
collide_wall,
movement,
),
)
.chain(),
)
.run();
}
// type alias for easier usage later
type NNTree = KDTree2<NearestNeighbourComponent>;
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn((
Cursor,
Sprite {
color: Color::srgb(0.0, 0.0, 1.0),
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
Transform {
translation: Vec3::ZERO,
..default()
},
));
let sprite = Sprite {
color: csscolors::ORANGE_RED.into(),
custom_size: Some(Vec2::new(6.0, 6.0)),
..default()
};
for x in -100..100 {
for y in -100..100 {
commands.spawn((
NearestNeighbourComponent,
sprite.clone(),
Transform {
translation: Vec3::new((x * 4) as f32, (y * 4) as f32, 0.0),
..default()
},
));
}
}
}
#[derive(Copy, Clone, Resource)]
struct Mouse2D {
pos: Vec2,
}
fn update_mouse_pos(
window: Single<&Window, With<PrimaryWindow>>,
camera: Single<(&Camera, &GlobalTransform)>,
mut mouse: ResMut<Mouse2D>,
) {
let (cam, cam_t) = camera.deref();
if let Some(w_pos) = window.cursor_position() {
if let Ok(pos) = cam.viewport_to_world_2d(cam_t, w_pos) {
mouse.pos = pos;
}
}
}
fn mouse(
mut commands: Commands,
mouse: Res<Mouse2D>,
treeaccess: Res<NNTree>,
mut transform: Single<&mut Transform, With<Cursor>>,
ms_buttons: Res<ButtonInput<MouseButton>>,
) {
let use_mouse = ms_buttons.pressed(MouseButton::Left);
if let Some((_pos, entity)) = treeaccess.nearest_neighbour(mouse.pos) {
transform.translation = mouse.pos.extend(0.0); // I don't really know what this is here for
if use_mouse {
commands.entity(entity.unwrap()).despawn();
}
}
}
fn color(
treeaccess: Res<NNTree>,
mouse: Res<Mouse2D>,
mut query: Query<&mut Sprite, With<NearestNeighbourComponent>>,
) {
for (_, entity) in treeaccess.within_distance(mouse.pos, 50.0) {
if let Ok(mut sprite) = query.get_mut(entity.unwrap()) {
sprite.color = Color::BLACK;
}
}
}
fn reset_color(mut query: Query<&mut Sprite, With<NearestNeighbourComponent>>) {
for mut sprite in &mut query {
sprite.color = csscolors::ORANGE_RED.into();
}
}
fn movement(mut query: Query<&mut Transform, With<NearestNeighbourComponent>>) {
for mut pos in &mut query {
let goal = pos.translation - Vec3::ZERO;
pos.translation += goal.normalize_or_zero();
}
}
fn collide_wall(
window: Single<&Window, With<PrimaryWindow>>,
mut query: Query<&mut Transform, With<NearestNeighbourComponent>>,
) {
let w = window.width() / 2.0;
let h = window.height() / 2.0;
for mut pos in &mut query {
let [x, y] = pos.translation.xy().to_array();
if y < -h || x < -w || y > h || x > w {
pos.translation = pos.translation.normalize_or_zero();
}
}
}

View File

@@ -0,0 +1,125 @@
use bevy::{
color::palettes::css as csscolors,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
window::PrimaryWindow,
};
use bevy_spatial::{AutomaticUpdate, SpatialAccess, kdtree::KDTree3};
use std::ops::Deref;
#[derive(Component)]
struct NearestNeighbourComponent;
#[derive(Component)]
struct Cursor;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(AutomaticUpdate::<NearestNeighbourComponent>::new())
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.insert_resource(Mouse3D { pos: Vec3::ZERO })
.add_systems(Startup, setup)
.add_systems(
Update,
(update_mouse_pos, (mouse, color, reset_color.before(color))).chain(),
)
.run();
}
#[derive(Resource, Clone)]
struct MaterialHandles {
orange_red: Handle<StandardMaterial>,
black: Handle<StandardMaterial>,
blue: Handle<StandardMaterial>,
}
type NNTree = KDTree3<NearestNeighbourComponent>;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut ambient_light: ResMut<AmbientLight>,
) {
let handles = MaterialHandles {
orange_red: materials.add(Color::from(csscolors::ORANGE_RED)),
black: materials.add(Color::from(csscolors::BLACK)),
blue: materials.add(Color::from(csscolors::BLUE)),
};
commands.insert_resource(handles.clone());
ambient_light.color = Color::WHITE;
ambient_light.brightness = 500.;
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 100.0, 900.0).looking_at(Vec3::ZERO, Vec3::Y),
));
commands
.spawn((
Mesh3d(meshes.add(Cuboid::new(10., 10., 10.))),
MeshMaterial3d(handles.blue.clone()),
Transform::from_xyz(0.0, 0.5, 0.0),
))
.insert(Cursor);
let mesh = meshes.add(Cuboid::new(4., 4., 4.));
for x in -25..25 {
for y in -25..25 {
for z in -9..9 {
commands.spawn((
Mesh3d(mesh.clone()),
MeshMaterial3d(handles.orange_red.clone()),
Transform::from_xyz((x * 15) as f32, (y * 15) as f32, (z * 15) as f32),
NearestNeighbourComponent,
));
}
}
}
}
#[derive(Copy, Clone, Resource)]
struct Mouse3D {
pos: Vec3,
}
fn update_mouse_pos(
window: Single<&Window, With<PrimaryWindow>>,
camera: Single<(&Camera, &GlobalTransform)>,
mut mouse: ResMut<Mouse3D>,
) {
let (cam, cam_t) = camera.deref();
if let Some(w_pos) = window.cursor_position() {
if let Ok(pos) = cam.viewport_to_world(cam_t, w_pos) {
mouse.pos = pos.get_point(900.);
}
}
}
fn mouse(mouse: Res<Mouse3D>, mut transform: Single<&mut Transform, With<Cursor>>) {
transform.translation = mouse.pos;
}
fn color(
mouse: Res<Mouse3D>,
treeaccess: Res<NNTree>,
mut query: Query<&mut MeshMaterial3d<StandardMaterial>, With<NearestNeighbourComponent>>,
colors: Res<MaterialHandles>,
) {
for (_, entity) in treeaccess.within_distance(mouse.pos, 100.0) {
if let Ok(mut handle) = query.get_mut(entity.expect("No entity")) {
*handle = colors.black.clone().into();
}
}
}
fn reset_color(
colors: Res<MaterialHandles>,
mut query: Query<&mut MeshMaterial3d<StandardMaterial>, With<NearestNeighbourComponent>>,
) {
for mut handle in &mut query {
*handle = colors.orange_red.clone().into();
}
}

View File

@@ -0,0 +1,112 @@
use std::time::Duration;
use bevy::color::palettes::css as csscolors;
use bevy::prelude::*;
use bevy_spatial::{
AutomaticUpdate, SpatialAccess, SpatialStructure, TimestepLength, kdtree::KDTree2,
};
#[derive(Component, Default)]
struct NearestNeighbour;
#[derive(Component)]
struct Chaser;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(
AutomaticUpdate::<NearestNeighbour>::new()
.with_frequency(Duration::from_millis(305))
.with_spatial_ds(SpatialStructure::KDTree2),
)
.add_systems(Startup, setup)
.add_systems(Update, (move_to, rotate_around, mouseclick))
.run();
}
type NNTree = KDTree2<NearestNeighbour>;
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn((
Text("Click mouse to change rate".to_string()),
TextFont {
font_size: 30.0,
..default()
},
TextColor(Color::BLACK),
));
commands.spawn((
Chaser,
Sprite {
color: csscolors::BLUE.into(),
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
Transform::from_translation(Vec3::ZERO),
));
let neighbours = [
(csscolors::RED, Vec3::Y * 100.),
(csscolors::RED, Vec3::NEG_Y * 100.),
(csscolors::RED, Vec3::X * 100.),
(csscolors::RED, Vec3::NEG_X * 100.),
];
for (color, position) in neighbours {
commands.spawn((
NearestNeighbour,
Sprite {
color: Color::from(color),
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
Transform::from_translation(position),
));
}
}
fn rotate_around(mut query: Query<&mut Transform, With<NearestNeighbour>>) {
for mut transform in &mut query {
transform.rotate_around(
Vec3::ZERO,
Quat::from_axis_angle(Vec3::Z, 1.0f32.to_radians()),
);
}
}
fn move_to(
treeaccess: Res<NNTree>,
time: Res<Time>,
mut query: Query<&mut Transform, With<Chaser>>,
) {
for mut transform in &mut query {
if let Some(nearest) = treeaccess.nearest_neighbour(transform.translation.truncate()) {
let towards = nearest.0.extend(0.0) - transform.translation;
transform.translation += towards.normalize() * time.delta_secs() * 350.0;
}
}
}
/// Change the timestep for
fn mouseclick(
mouse_input: Res<ButtonInput<MouseButton>>,
mut text: Single<&mut Text>,
mut step: ResMut<TimestepLength<NearestNeighbour>>,
mut other_duration: Local<Duration>,
) {
if other_duration.is_zero() {
*other_duration = Duration::from_millis(1);
}
if mouse_input.just_pressed(MouseButton::Left) {
let duration = step.get_duration();
step.set_duration(*other_duration);
text.0 = format!("Spatial Update Rate: {} ms", other_duration.as_millis());
*other_duration = duration;
}
}

View File

@@ -0,0 +1,115 @@
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
math::vec2,
prelude::*,
time::common_conditions::on_timer,
window::PrimaryWindow,
};
use bevy_spatial::{
AutomaticUpdate, SpatialAccess, SpatialStructure, TransformMode, kdtree::KDTree3,
};
use std::ops::Deref;
use std::time::Duration;
#[derive(Component, Default)]
struct NearestNeighbour;
#[derive(Component)]
struct MoveTowards;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(
AutomaticUpdate::<NearestNeighbour>::new()
.with_spatial_ds(SpatialStructure::KDTree3)
.with_frequency(Duration::from_secs(1))
.with_transform(TransformMode::Transform),
)
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_plugins(LogDiagnosticsPlugin::default())
.add_systems(Startup, setup)
.add_systems(
Update,
(
mouseclick,
log_num.run_if(on_timer(Duration::from_secs_f32(0.5))),
move_to,
),
)
.run();
}
type NNTree = KDTree3<NearestNeighbour>;
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
for x in -6..6 {
for y in -6..6 {
commands.spawn((
NearestNeighbour,
Sprite {
color: Color::srgb(0.7, 0.3, 0.5),
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
Transform {
translation: Vec3::new((x * 100) as f32, (y * 100) as f32, 0.0),
..default()
},
));
}
}
}
fn mouseclick(
mut commands: Commands,
mouse_input: Res<ButtonInput<MouseButton>>,
window: Single<&Window, With<PrimaryWindow>>,
camera: Single<(&Camera, &GlobalTransform)>,
) {
let (cam, cam_t) = camera.deref();
if mouse_input.pressed(MouseButton::Left) {
if let Some(pos) = window.cursor_position() {
for xoff in -1..=1 {
for yoff in -1..=1 {
commands.spawn((
MoveTowards,
Sprite {
color: Color::srgb(0.15, 0.15, 1.0),
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
Transform {
translation: cam
.viewport_to_world_2d(
cam_t,
pos + vec2(xoff as f32 * 16., yoff as f32 * 16.),
)
.unwrap_or(Vec2::ZERO)
.extend(1.0),
..default()
},
));
}
}
}
}
}
fn move_to(
treeaccess: Res<NNTree>,
time: Res<Time>,
mut query: Query<&mut Transform, With<MoveTowards>>,
) {
for mut transform in &mut query {
if let Some(nearest) = treeaccess.nearest_neighbour(transform.translation) {
let towards = nearest.0 - transform.translation;
transform.translation += towards.normalize() * time.delta_secs() * 64.0;
}
}
}
fn log_num(query: Query<(), With<MoveTowards>>) {
info!(target: "blue", amount = query.iter().len())
}