update inspector
added general render distance system
This commit is contained in:
75
game/main/src/utlis/render_distance_system.rs
Normal file
75
game/main/src/utlis/render_distance_system.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use bevy::prelude::*;
|
||||
use camera_system::prelude::PhosCamera;
|
||||
|
||||
pub struct RenderDistancePlugin;
|
||||
|
||||
impl Plugin for RenderDistancePlugin {
|
||||
fn build(&self, app: &mut bevy::prelude::App) {
|
||||
app.register_type::<RenderDistanceSettings>();
|
||||
app.add_systems(PostUpdate, render_distance_system)
|
||||
.insert_resource(RenderDistanceSettings::default());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
pub struct RenderDistanceSettings {
|
||||
pub render_distance: f32,
|
||||
}
|
||||
|
||||
impl RenderDistanceSettings {
|
||||
pub fn new(distance: f32) -> Self {
|
||||
return Self {
|
||||
render_distance: distance,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RenderDistanceSettings {
|
||||
fn default() -> Self {
|
||||
Self::new(500.)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct RenderDistanceVisibility {
|
||||
pub distance_multiplier: f32,
|
||||
pub offset: Vec3,
|
||||
}
|
||||
|
||||
impl RenderDistanceVisibility {
|
||||
pub fn with_offset(mut self, offset: Vec3) -> Self {
|
||||
self.offset = offset;
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn with_multiplier(mut self, distance_multiplier: f32) -> Self {
|
||||
self.distance_multiplier = distance_multiplier;
|
||||
return self;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RenderDistanceVisibility {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
distance_multiplier: 1.,
|
||||
offset: Vec3::ZERO,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_distance_system(
|
||||
mut objects: Query<(&Transform, &mut Visibility, &RenderDistanceVisibility)>,
|
||||
camera_query: Query<&Transform, With<PhosCamera>>,
|
||||
settings: Res<RenderDistanceSettings>,
|
||||
) {
|
||||
let camera = camera_query.single();
|
||||
for (t, mut vis, r) in objects.iter_mut() {
|
||||
let dist = (camera.translation - (t.translation + r.offset)).length() * r.distance_multiplier;
|
||||
if settings.render_distance < dist {
|
||||
*vis = Visibility::Hidden;
|
||||
} else {
|
||||
*vis = Visibility::Visible;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user