Last active
May 14, 2024 19:55
-
-
Save rparrett/9cd3b145def1855f1baf87822e6a5f7a to your computer and use it in GitHub Desktop.
Bevy 0.13 adding Aabb to Text2d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Renders a lot of `Text2dBundle`s and adds `Aabb`s so they get automatically culled when out of view. | |
use bevy::{ | |
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, | |
math::Vec3A, | |
prelude::*, | |
render::primitives::Aabb, | |
text::TextLayoutInfo, | |
}; | |
use rand::Rng; | |
const CAMERA_SPEED: f32 = 1000.0; | |
fn main() { | |
App::new() | |
.add_plugins(( | |
FrameTimeDiagnosticsPlugin, | |
LogDiagnosticsPlugin::default(), | |
DefaultPlugins, | |
)) | |
.add_systems(Startup, setup) | |
.add_systems( | |
Update, | |
( | |
update_text_aabb, | |
print_sprite_count, | |
move_camera.after(print_sprite_count), | |
), | |
) | |
.run(); | |
} | |
fn setup(mut commands: Commands) { | |
let mut rng = rand::thread_rng(); | |
let tile_size = Vec2::splat(64.0); | |
let map_size = Vec2::splat(320.0); | |
let half_x = (map_size.x / 4.0) as i32; | |
let half_y = (map_size.y / 4.0) as i32; | |
// Spawns the camera | |
commands.spawn(Camera2dBundle::default()); | |
let text_style = TextStyle { | |
font_size: 60.0, | |
color: Color::WHITE, | |
..default() | |
}; | |
// Builds and spawns the sprites | |
let mut sprites = vec![]; | |
for y in -half_y..half_y { | |
for x in -half_x..half_x { | |
let position = Vec2::new(x as f32, y as f32); | |
let translation = (position * tile_size).extend(rng.gen::<f32>()); | |
let rotation = Quat::from_rotation_z(rng.gen::<f32>()); | |
let scale = Vec3::splat(rng.gen::<f32>() * 2.0); | |
sprites.push(Text2dBundle { | |
text: Text::from_section("test", text_style.clone()), | |
transform: Transform { | |
translation, | |
rotation, | |
scale, | |
}, | |
..default() | |
}); | |
} | |
} | |
commands.spawn_batch(sprites); | |
} | |
// System for rotating and translating the camera | |
fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) { | |
let mut camera_transform = camera_query.single_mut(); | |
camera_transform.rotate_z(time.delta_seconds() * 0.5); | |
*camera_transform = *camera_transform | |
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds()); | |
} | |
#[derive(Deref, DerefMut)] | |
struct PrintingTimer(Timer); | |
impl Default for PrintingTimer { | |
fn default() -> Self { | |
Self(Timer::from_seconds(1.0, TimerMode::Repeating)) | |
} | |
} | |
// System for printing the number of texts on every tick of the timer | |
fn print_sprite_count( | |
time: Res<Time>, | |
mut timer: Local<PrintingTimer>, | |
texts: Query<&ViewVisibility, With<Text>>, | |
) { | |
timer.tick(time.delta()); | |
if timer.just_finished() { | |
let visible_texts = texts.iter().filter(|visibility| visibility.get()).count(); | |
info!("Texts: {} Visible: {}", texts.iter().count(), visible_texts); | |
} | |
} | |
fn update_text_aabb( | |
mut commands: Commands, | |
query: Query<(Entity, &TextLayoutInfo), Changed<TextLayoutInfo>>, | |
) { | |
for (entity, info) in query.iter() { | |
commands.entity(entity).insert(Aabb { | |
center: Vec3A::ZERO, | |
half_extents: (info.logical_size / 2.).extend(0.).into(), | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment