Initial project import
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-06-05 21:44:45 -04:00
commit f29712d158
259 files changed
+52069

No files matched your search

+800
View File
@@ -0,0 +1,800 @@
//! Editor-side scene visualizers for authoring/runtime entities that have no mesh.
use avian3d::prelude::ColliderConstructor;
use bevy::prelude::*;
use shared::{
AuthoringLightKind, ColliderDesc, ColliderShapeDesc, LevelObject, LightDesc, ModelRef,
ObjectiveMarker, PhysicsBody, PlayerSpawn, PostProcessVolumeDesc, PrefabRef, ProjectSun,
RaytracingExcluded, TeamSpawn, TriggerVolume, WeaponSpawn,
};
use sim::tuning::{EYE_OFFSET_STAND, PLAYER_LENGTH, PLAYER_RADIUS};
use sim::{Player, PlayerCamera};
use crate::infra::EditorOnly;
use crate::selection::SelectedEntity;
use crate::state::scene_tools_active;
use crate::viewport::{EditorViewportMode, ViewportDisplayMode};
#[derive(Resource, Debug, Clone)]
pub struct EditorVisualizationSettings {
pub enabled: bool,
pub show_actor_icons: bool,
pub show_actor_icon_actor: bool,
pub show_actor_icon_mesh: bool,
pub show_actor_icon_light: bool,
pub show_actor_icon_physics: bool,
pub show_actor_icon_gameplay: bool,
pub show_actor_icon_volume: bool,
pub show_actor_icon_prefab_model: bool,
pub show_actor_icon_debug: bool,
pub show_colliders: bool,
pub show_lights: bool,
pub show_spawns: bool,
pub show_prefab_model_anchors: bool,
pub show_runtime: bool,
pub pickable_proxies: bool,
}
impl Default for EditorVisualizationSettings {
fn default() -> Self {
Self {
enabled: true,
show_actor_icons: true,
show_actor_icon_actor: true,
show_actor_icon_mesh: true,
show_actor_icon_light: true,
show_actor_icon_physics: true,
show_actor_icon_gameplay: true,
show_actor_icon_volume: true,
show_actor_icon_prefab_model: true,
show_actor_icon_debug: true,
show_colliders: true,
show_lights: true,
show_spawns: true,
show_prefab_model_anchors: true,
show_runtime: true,
pickable_proxies: true,
}
}
}
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EditorVisualizerProxy {
pub target: Entity,
pub kind: VisualizerKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VisualizerKind {
Collider,
Light,
PlayerSpawn,
Prefab,
Model,
Player,
Camera,
ProjectSun,
}
#[derive(Resource)]
struct VisualizerProxyAssets {
mesh: Handle<Mesh>,
material: Handle<StandardMaterial>,
}
pub struct EditorVisualizersPlugin;
impl Plugin for EditorVisualizersPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<EditorVisualizationSettings>()
.add_systems(Startup, init_visualizer_proxy_assets)
.add_systems(Update, sync_visualizer_proxies.run_if(scene_tools_active))
.add_systems(
Update,
(
draw_visualizers,
draw_split_collider_visualizers,
draw_post_process_volume_gizmos,
)
.chain()
.run_if(scene_tools_active)
.run_if(viewport_editor_overlays_visible),
);
}
}
fn viewport_editor_overlays_visible(display: Res<ViewportDisplayMode>) -> bool {
!display.clean_game_view
}
fn init_visualizer_proxy_assets(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.insert_resource(VisualizerProxyAssets {
mesh: meshes.add(Sphere::new(0.22).mesh().uv(16, 8)),
material: materials.add(StandardMaterial {
base_color: Color::srgba(0.2, 0.8, 1.0, 0.08),
alpha_mode: AlphaMode::Blend,
unlit: true,
..default()
}),
});
}
#[allow(clippy::too_many_arguments)]
fn sync_visualizer_proxies(
viewport_mode: Res<EditorViewportMode>,
display: Res<ViewportDisplayMode>,
settings: Res<EditorVisualizationSettings>,
assets: Res<VisualizerProxyAssets>,
mut commands: Commands,
mut proxies: Query<(Entity, &EditorVisualizerProxy, &mut Transform)>,
transforms: Query<&GlobalTransform>,
colliders: Query<
Entity,
(
With<LevelObject>,
Or<(With<PhysicsBody>, With<ColliderDesc>)>,
),
>,
lights: Query<Entity, (With<LevelObject>, With<LightDesc>)>,
spawns: Query<Entity, (With<LevelObject>, With<PlayerSpawn>)>,
prefabs: Query<Entity, (With<LevelObject>, With<PrefabRef>)>,
models: Query<Entity, (With<LevelObject>, With<ModelRef>)>,
players: Query<Entity, With<Player>>,
cameras: Query<Entity, With<PlayerCamera>>,
project_suns: Query<Entity, With<ProjectSun>>,
) {
let mut desired = std::collections::HashSet::new();
let collider_mode = *viewport_mode == EditorViewportMode::Collider;
let proxies_active = !display.clean_game_view
&& settings.enabled
&& (settings.pickable_proxies || collider_mode);
if proxies_active {
if settings.show_colliders || collider_mode {
desired.extend(
colliders
.iter()
.map(|target| (target, VisualizerKind::Collider)),
);
}
if settings.show_lights && !collider_mode {
desired.extend(lights.iter().map(|target| (target, VisualizerKind::Light)));
}
if settings.show_spawns && !collider_mode {
desired.extend(
spawns
.iter()
.map(|target| (target, VisualizerKind::PlayerSpawn)),
);
}
if settings.show_prefab_model_anchors && !collider_mode {
desired.extend(
prefabs
.iter()
.map(|target| (target, VisualizerKind::Prefab)),
);
desired.extend(models.iter().map(|target| (target, VisualizerKind::Model)));
}
if settings.show_runtime && !collider_mode {
desired.extend(
players
.iter()
.map(|target| (target, VisualizerKind::Player)),
);
desired.extend(
cameras
.iter()
.map(|target| (target, VisualizerKind::Camera)),
);
desired.extend(
project_suns
.iter()
.map(|target| (target, VisualizerKind::ProjectSun)),
);
}
}
let existing: std::collections::HashSet<(Entity, VisualizerKind)> = proxies
.iter()
.map(|(_, proxy, _)| (proxy.target, proxy.kind))
.collect();
for (entity, proxy, mut transform) in &mut proxies {
if !desired.contains(&(proxy.target, proxy.kind)) || transforms.get(proxy.target).is_err() {
commands.entity(entity).despawn();
} else if let Ok(global) = transforms.get(proxy.target) {
transform.translation = global.translation();
}
}
for (target, kind) in desired {
let Ok(global) = transforms.get(target) else {
continue;
};
if existing.contains(&(target, kind)) {
continue;
}
commands.spawn((
EditorOnly,
RaytracingExcluded,
Name::new(format!("{kind:?} Visualizer Proxy")),
EditorVisualizerProxy { target, kind },
Mesh3d(assets.mesh.clone()),
MeshMaterial3d(assets.material.clone()),
Transform::from_translation(global.translation()),
));
}
}
#[allow(clippy::too_many_arguments)]
fn draw_visualizers(
viewport_mode: Res<EditorViewportMode>,
settings: Res<EditorVisualizationSettings>,
selected: Res<SelectedEntity>,
mut gizmos: Gizmos,
colliders: Query<(Entity, &PhysicsBody, &GlobalTransform), With<LevelObject>>,
lights: Query<(Entity, &LightDesc, &GlobalTransform), With<LevelObject>>,
spawns: Query<(Entity, &GlobalTransform), (With<LevelObject>, With<PlayerSpawn>)>,
prefabs: Query<(Entity, &GlobalTransform), (With<LevelObject>, With<PrefabRef>)>,
models: Query<(Entity, &GlobalTransform), (With<LevelObject>, With<ModelRef>)>,
players: Query<(Entity, &GlobalTransform), With<Player>>,
cameras: Query<(Entity, &GlobalTransform), With<PlayerCamera>>,
project_suns: Query<(Entity, &GlobalTransform), With<ProjectSun>>,
triggers: Query<(Entity, &TriggerVolume, &GlobalTransform), With<LevelObject>>,
weapons: Query<(Entity, &GlobalTransform), (With<LevelObject>, With<WeaponSpawn>)>,
teams: Query<(Entity, &GlobalTransform), (With<LevelObject>, With<TeamSpawn>)>,
objectives: Query<(Entity, &GlobalTransform), (With<LevelObject>, With<ObjectiveMarker>)>,
) {
let collider_mode = *viewport_mode == EditorViewportMode::Collider;
if !settings.enabled && !collider_mode {
return;
}
if settings.show_colliders || collider_mode {
for (entity, body, global) in &colliders {
let color = if selected.0 == Some(entity) {
if collider_mode {
Color::srgba(0.65, 1.0, 0.55, 1.0)
} else {
Color::srgba(0.55, 1.0, 0.45, 0.95)
}
} else if collider_mode {
Color::srgba(0.35, 1.0, 0.45, 0.9)
} else {
Color::srgba(0.25, 0.9, 0.35, 0.55)
};
draw_collider(&mut gizmos, global, &body.collider, color);
}
}
if settings.show_lights && !collider_mode {
for (entity, light, global) in &lights {
let color = if selected.0 == Some(entity) {
Color::srgba(1.0, 0.9, 0.35, 1.0)
} else {
Color::srgba(1.0, 0.78, 0.25, 0.75)
};
draw_light(&mut gizmos, global, light, color, true);
}
}
if settings.show_spawns {
for (entity, global) in &spawns {
let color = if selected.0 == Some(entity) {
Color::srgba(0.2, 1.0, 1.0, 1.0)
} else {
Color::srgba(0.15, 0.85, 1.0, 0.75)
};
draw_spawn(&mut gizmos, global, color);
}
}
if settings.show_prefab_model_anchors {
for (entity, global) in &prefabs {
draw_anchor(
&mut gizmos,
global,
selected.0 == Some(entity),
Color::srgb(0.75, 0.45, 1.0),
);
}
for (entity, global) in &models {
draw_anchor(
&mut gizmos,
global,
selected.0 == Some(entity),
Color::srgb(0.5, 0.6, 1.0),
);
}
}
if settings.show_runtime {
for (entity, global) in &players {
let color = if selected.0 == Some(entity) {
Color::srgba(0.25, 0.65, 1.0, 1.0)
} else {
Color::srgba(0.25, 0.65, 1.0, 0.7)
};
draw_player(&mut gizmos, global, color);
}
for (entity, global) in &cameras {
let color = if selected.0 == Some(entity) {
Color::srgba(0.4, 0.85, 1.0, 1.0)
} else {
Color::srgba(0.4, 0.85, 1.0, 0.7)
};
draw_camera_frustum(&mut gizmos, global, color);
}
for (entity, global) in &project_suns {
let color = if selected.0 == Some(entity) {
Color::srgba(1.0, 0.88, 0.32, 1.0)
} else {
Color::srgba(1.0, 0.78, 0.18, 0.75)
};
draw_project_sun(&mut gizmos, global, color, true);
}
}
for (entity, trigger, global) in &triggers {
draw_oriented_box(
&mut gizmos,
global,
trigger.half_extents,
if selected.0 == Some(entity) {
Color::srgba(0.95, 0.35, 0.95, 0.85)
} else {
Color::srgba(0.9, 0.3, 0.9, 0.45)
},
);
}
for (entity, global) in &weapons {
draw_anchor(
&mut gizmos,
global,
selected.0 == Some(entity),
Color::srgb(1.0, 0.35, 0.2),
);
}
for (entity, global) in &teams {
draw_anchor(
&mut gizmos,
global,
selected.0 == Some(entity),
Color::srgb(0.2, 0.85, 0.35),
);
}
for (entity, global) in &objectives {
draw_anchor(
&mut gizmos,
global,
selected.0 == Some(entity),
Color::srgb(0.95, 0.85, 0.2),
);
}
}
fn draw_split_collider_visualizers(
viewport_mode: Res<EditorViewportMode>,
settings: Res<EditorVisualizationSettings>,
selected: Res<SelectedEntity>,
mut gizmos: Gizmos,
colliders: Query<
(Entity, &ColliderDesc, &GlobalTransform),
(With<LevelObject>, Without<PhysicsBody>),
>,
) {
let collider_mode = *viewport_mode == EditorViewportMode::Collider;
if !settings.enabled && !collider_mode {
return;
}
if !(settings.show_colliders || collider_mode) {
return;
}
for (entity, collider, global) in &colliders {
let color = if selected.0 == Some(entity) {
Color::srgba(0.65, 1.0, 0.55, 1.0)
} else if collider_mode {
Color::srgba(0.35, 1.0, 0.45, 0.9)
} else {
Color::srgba(0.25, 0.9, 0.35, 0.55)
};
draw_collider_desc(&mut gizmos, global, collider, color);
}
}
fn draw_post_process_volume_gizmos(
viewport_mode: Res<EditorViewportMode>,
settings: Res<EditorVisualizationSettings>,
selected: Res<SelectedEntity>,
mut gizmos: Gizmos,
volumes: Query<(Entity, &PostProcessVolumeDesc, &GlobalTransform), With<LevelObject>>,
) {
if *viewport_mode == EditorViewportMode::Collider || !settings.enabled {
return;
}
for (entity, volume, global) in &volumes {
let t = ((volume.priority + 10) as f32 / 110.0).clamp(0.0, 1.0);
let border = Color::srgba(0.2 + 0.8 * t, 0.45 + 0.2 * (1.0 - t), 1.0 - 0.65 * t, 0.95);
draw_oriented_box(
&mut gizmos,
global,
volume.half_extents,
if selected.0 == Some(entity) {
border.with_alpha(1.0)
} else {
border.with_alpha(0.55)
},
);
}
}
fn draw_collider(
gizmos: &mut Gizmos,
global: &GlobalTransform,
collider: &ColliderConstructor,
color: Color,
) {
match collider {
ColliderConstructor::Cuboid {
x_length,
y_length,
z_length,
} => draw_box(
gizmos,
global.translation(),
global.rotation(),
Vec3::new(*x_length, *y_length, *z_length) * 0.5,
color,
),
ColliderConstructor::Sphere { radius } => {
gizmos
.sphere(
Isometry3d::new(global.translation(), global.rotation()),
*radius,
color,
)
.resolution(24);
}
ColliderConstructor::Capsule { radius, height } => {
draw_capsule(
gizmos,
global.translation(),
global.rotation(),
*radius,
*height,
color,
);
}
_ => {
gizmos
.sphere(
Isometry3d::new(global.translation(), global.rotation()),
0.5,
color,
)
.resolution(16);
}
}
}
fn draw_collider_desc(
gizmos: &mut Gizmos,
global: &GlobalTransform,
collider: &ColliderDesc,
color: Color,
) {
if !collider.enabled {
return;
}
match &collider.shape {
ColliderShapeDesc::Cuboid {
x_length,
y_length,
z_length,
} => draw_box(
gizmos,
global.translation(),
global.rotation(),
Vec3::new(*x_length, *y_length, *z_length) * 0.5,
color,
),
ColliderShapeDesc::Sphere { radius } => {
gizmos
.sphere(
Isometry3d::new(global.translation(), global.rotation()),
*radius,
color,
)
.resolution(24);
}
ColliderShapeDesc::Capsule { radius, height } => {
draw_capsule(
gizmos,
global.translation(),
global.rotation(),
*radius,
*height,
color,
);
}
ColliderShapeDesc::StaticMesh { .. } => {
gizmos
.sphere(
Isometry3d::new(global.translation(), global.rotation()),
0.35,
color,
)
.resolution(16);
}
}
}
fn draw_light(
gizmos: &mut Gizmos,
global: &GlobalTransform,
light: &LightDesc,
color: Color,
emphasize: bool,
) {
let center = global.translation();
let forward = global.forward().as_vec3();
match light.kind {
AuthoringLightKind::Point => {
let radius = light.range.max(0.25);
gizmos
.sphere(Isometry3d::new(center, global.rotation()), radius, color)
.resolution(32);
if emphasize {
gizmos
.circle(
Isometry3d::from_translation(center),
radius,
color.with_alpha(0.45),
)
.resolution(32);
}
}
AuthoringLightKind::Spot => {
let range = light.range.max(1.0);
let end = center + forward * range;
gizmos.arrow(center, end, color);
let right = global.right().as_vec3();
let up = global.up().as_vec3();
let outer_deg = light.outer_angle_deg.max(light.inner_angle_deg);
let radius = range * (outer_deg.to_radians() * 0.5).tan().max(0.15);
for offset in [right, -right, up, -up] {
gizmos.line(center, end + offset * radius, color);
}
gizmos
.circle(Isometry3d::new(end, global.rotation()), radius, color)
.resolution(24);
if emphasize {
gizmos
.sphere(
Isometry3d::new(end, global.rotation()),
radius * 0.12,
color.with_alpha(0.5),
)
.resolution(12);
}
}
AuthoringLightKind::Directional => {
draw_directional_light_arrow(gizmos, global, color);
}
}
}
fn draw_directional_light_arrow(gizmos: &mut Gizmos, global: &GlobalTransform, color: Color) {
let center = global.translation();
let forward = global.forward().as_vec3();
let arrow_len = 5.0;
let tail = center - forward * arrow_len;
gizmos.arrow(tail, center, color);
gizmos
.sphere(Isometry3d::new(center, global.rotation()), 0.4, color)
.resolution(18);
let right = global.right().as_vec3();
let up = global.up().as_vec3();
for offset in [right, -right, up, -up] {
gizmos.line(center, center + offset * 0.75, color.with_alpha(0.65));
}
}
fn draw_spawn(gizmos: &mut Gizmos, global: &GlobalTransform, color: Color) {
let center = global.translation();
draw_capsule(
gizmos,
center,
global.rotation(),
PLAYER_RADIUS,
PLAYER_LENGTH,
color,
);
gizmos.arrow(center, center + global.forward().as_vec3() * 1.25, color);
gizmos
.sphere(
Isometry3d::new(center + Vec3::Y * EYE_OFFSET_STAND, global.rotation()),
0.08,
color,
)
.resolution(10);
}
fn draw_anchor(gizmos: &mut Gizmos, global: &GlobalTransform, selected: bool, base: Color) {
let center = global.translation();
let color = if selected {
Color::srgba(1.0, 0.85, 1.0, 1.0)
} else {
base.with_alpha(0.75)
};
gizmos.axes(Isometry3d::new(center, global.rotation()), 0.8);
draw_box(gizmos, center, global.rotation(), Vec3::splat(0.35), color);
}
fn draw_player(gizmos: &mut Gizmos, global: &GlobalTransform, color: Color) {
let center = global.translation();
draw_capsule(
gizmos,
center,
global.rotation(),
PLAYER_RADIUS,
PLAYER_LENGTH,
color,
);
gizmos.arrow(center, center + global.forward().as_vec3() * 1.25, color);
gizmos
.sphere(
Isometry3d::new(center + Vec3::Y * EYE_OFFSET_STAND, global.rotation()),
0.08,
color,
)
.resolution(10);
}
fn draw_project_sun(gizmos: &mut Gizmos, global: &GlobalTransform, color: Color, emphasize: bool) {
let center = global.translation();
gizmos
.sphere(Isometry3d::new(center, global.rotation()), 0.45, color)
.resolution(20);
draw_directional_light_arrow(gizmos, global, color);
if emphasize {
gizmos
.circle(
Isometry3d::new(center, global.rotation()),
1.0,
color.with_alpha(0.35),
)
.resolution(28);
} else {
gizmos
.circle(Isometry3d::new(center, global.rotation()), 0.75, color)
.resolution(24);
}
}
fn draw_camera_frustum(gizmos: &mut Gizmos, global: &GlobalTransform, color: Color) {
let origin = global.translation();
let forward = global.forward().as_vec3();
let right = global.right().as_vec3();
let up = global.up().as_vec3();
let near = origin + forward * 0.8;
let half_w = 0.45;
let half_h = 0.28;
let corners = [
near + right * half_w + up * half_h,
near - right * half_w + up * half_h,
near - right * half_w - up * half_h,
near + right * half_w - up * half_h,
];
for corner in corners {
gizmos.line(origin, corner, color);
}
gizmos.lineloop(corners, color);
}
fn draw_box(gizmos: &mut Gizmos, center: Vec3, rotation: Quat, half: Vec3, color: Color) {
let corners = [
Vec3::new(-half.x, -half.y, -half.z),
Vec3::new(half.x, -half.y, -half.z),
Vec3::new(half.x, -half.y, half.z),
Vec3::new(-half.x, -half.y, half.z),
Vec3::new(-half.x, half.y, -half.z),
Vec3::new(half.x, half.y, -half.z),
Vec3::new(half.x, half.y, half.z),
Vec3::new(-half.x, half.y, half.z),
]
.map(|corner| center + rotation * corner);
for (a, b) in [
(0, 1),
(1, 2),
(2, 3),
(3, 0),
(4, 5),
(5, 6),
(6, 7),
(7, 4),
(0, 4),
(1, 5),
(2, 6),
(3, 7),
] {
gizmos.line(corners[a], corners[b], color);
}
}
fn draw_capsule(
gizmos: &mut Gizmos,
center: Vec3,
rotation: Quat,
radius: f32,
length: f32,
color: Color,
) {
let half = length * 0.5;
let top = center + rotation * Vec3::Y * half;
let bottom = center - rotation * Vec3::Y * half;
gizmos
.sphere(Isometry3d::new(top, rotation), radius, color)
.resolution(16);
gizmos
.sphere(Isometry3d::new(bottom, rotation), radius, color)
.resolution(16);
for (x, z) in [(radius, 0.0), (-radius, 0.0), (0.0, radius), (0.0, -radius)] {
let offset = rotation * Vec3::new(x, 0.0, z);
gizmos.line(top + offset, bottom + offset, color);
}
}
fn draw_oriented_box(
gizmos: &mut Gizmos,
global: &GlobalTransform,
half_extents: Vec3,
color: Color,
) {
let rotation = global.rotation();
let center = global.translation();
let corners = box_corners(center, rotation, half_extents);
for (a, b) in box_edges() {
gizmos.line(corners[a], corners[b], color);
}
}
fn box_corners(center: Vec3, rotation: Quat, half_extents: Vec3) -> [Vec3; 8] {
let hx = half_extents.x;
let hy = half_extents.y;
let hz = half_extents.z;
[
center + rotation * Vec3::new(-hx, -hy, -hz),
center + rotation * Vec3::new(hx, -hy, -hz),
center + rotation * Vec3::new(hx, -hy, hz),
center + rotation * Vec3::new(-hx, -hy, hz),
center + rotation * Vec3::new(-hx, hy, -hz),
center + rotation * Vec3::new(hx, hy, -hz),
center + rotation * Vec3::new(hx, hy, hz),
center + rotation * Vec3::new(-hx, hy, hz),
]
}
fn box_edges() -> [(usize, usize); 12] {
[
(0, 1),
(1, 2),
(2, 3),
(3, 0),
(4, 5),
(5, 6),
(6, 7),
(7, 4),
(0, 4),
(1, 5),
(2, 6),
(3, 7),
]
}