forked from DigitalExtinction/Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement markers of selected movable entities
Maximum number of units (which might all be selected) was decreased to 1023 so it fits into a 2D tree of depth 10 (used in the shader). Relates to DigitalExtinction#36.
- Loading branch information
Showing
8 changed files
with
504 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
mod collider; | ||
mod marker; | ||
mod plugin; | ||
mod shader; | ||
mod terrain; | ||
|
||
use bevy::{app::PluginGroupBuilder, prelude::*}; | ||
pub use collider::TerrainCollider; | ||
pub use marker::CircleMarker; | ||
use marker::MarkerPlugin; | ||
use plugin::TerrainPlugin; | ||
pub use terrain::TerrainBundle; | ||
|
||
pub struct TerrainPluginGroup; | ||
|
||
impl PluginGroup for TerrainPluginGroup { | ||
fn build(&mut self, group: &mut PluginGroupBuilder) { | ||
group.add(TerrainPlugin); | ||
group.add(TerrainPlugin).add(MarkerPlugin); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use bevy::{ | ||
prelude::*, | ||
render::{ | ||
primitives::{Aabb, Frustum, Sphere}, | ||
view::VisibilitySystems, | ||
}, | ||
utils::FloatOrd, | ||
}; | ||
use de_core::{objects::ObjectType, projection::ToFlat, state::GameState}; | ||
use de_objects::{ColliderCache, ObjectCache}; | ||
use glam::Vec3A; | ||
use iyes_loopless::prelude::*; | ||
|
||
use crate::shader::{Circle, TerrainMaterial, CIRCLE_CAPACITY}; | ||
|
||
pub(crate) struct MarkerPlugin; | ||
|
||
impl Plugin for MarkerPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_system_to_stage( | ||
CoreStage::PostUpdate, | ||
update_markers | ||
.run_in_state(GameState::Playing) | ||
.after(VisibilitySystems::CheckVisibility), | ||
); | ||
} | ||
} | ||
|
||
/// A semi-transparent circle is drawn on the terrain surface below every | ||
/// entity with this component. | ||
#[derive(Component)] | ||
pub struct CircleMarker { | ||
radius: f32, | ||
} | ||
|
||
impl CircleMarker { | ||
pub fn new(radius: f32) -> Self { | ||
Self { radius } | ||
} | ||
|
||
pub(crate) fn radius(&self) -> f32 { | ||
self.radius | ||
} | ||
} | ||
|
||
fn update_markers( | ||
mut materials: ResMut<Assets<TerrainMaterial>>, | ||
cache: Res<ObjectCache>, | ||
camera: Query<(&Transform, &Frustum), With<Camera3d>>, | ||
terrains: Query<(&ComputedVisibility, &Handle<TerrainMaterial>)>, | ||
markers: Query<( | ||
&ObjectType, | ||
&ComputedVisibility, | ||
&GlobalTransform, | ||
&CircleMarker, | ||
)>, | ||
) { | ||
let (eye, frustum) = match camera.get_single() { | ||
Ok((transform, frustum)) => (transform.translation, frustum), | ||
Err(_) => return, | ||
}; | ||
|
||
struct CircleWithDist { | ||
circle: Circle, | ||
distance_sq: FloatOrd, | ||
} | ||
|
||
let mut candidates = Vec::new(); | ||
for (&object_type, circle_visibility, transform, marker) in markers.iter() { | ||
if !circle_visibility.is_visible_in_hierarchy() { | ||
continue; | ||
} | ||
|
||
let aabb = cache.get_collider(object_type).aabb(); | ||
let aabb = Aabb { | ||
center: Vec3A::from(aabb.center()), | ||
half_extents: Vec3A::from(aabb.half_extents()), | ||
}; | ||
|
||
let translation = transform.translation(); | ||
|
||
if intersects_frustum(frustum, transform, &aabb) { | ||
candidates.push(CircleWithDist { | ||
circle: Circle::new(translation.to_flat(), marker.radius()), | ||
distance_sq: FloatOrd(eye.distance_squared(translation)), | ||
}); | ||
} | ||
} | ||
candidates.sort_unstable_by_key(|c| c.distance_sq); | ||
|
||
let circles: Vec<Circle> = candidates | ||
.iter() | ||
.take(CIRCLE_CAPACITY) | ||
.map(|c| c.circle) | ||
.collect(); | ||
|
||
for (terrain_visibility, material) in terrains.iter() { | ||
if !terrain_visibility.is_visible_in_hierarchy() { | ||
continue; | ||
} | ||
|
||
let material = materials.get_mut(material).unwrap(); | ||
material.set_markers(circles.clone()); | ||
} | ||
} | ||
|
||
fn intersects_frustum(frustum: &Frustum, transform: &GlobalTransform, aabb: &Aabb) -> bool { | ||
let model = transform.compute_matrix(); | ||
let model_sphere = Sphere { | ||
center: model.transform_point3a(aabb.center), | ||
radius: transform.radius_vec3a(aabb.half_extents), | ||
}; | ||
|
||
frustum.intersects_sphere(&model_sphere, false) && frustum.intersects_obb(aabb, &model, false) | ||
} |
Oops, something went wrong.