Add collider authoring diagnostics
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-07-12 22:48:00 -04:00
1 parent 0e7aed40f1
commit 0ce43fc399
19 files changed
+1180 -127

No files matched your search

+194 -61
View File
@@ -1,14 +1,16 @@
//! Editor-side scene visualizers for authoring/runtime entities that have no mesh.
use avian3d::prelude::ColliderConstructor;
use std::f32::consts::FRAC_PI_2;
use avian3d::prelude::{ColliderAabb, ColliderConstructor};
use bevy::ecs::system::SystemParam;
use bevy::prelude::*;
use shared::{
AudioListenerDesc, AudioSourceDesc, AuthoringLightKind, ColliderDesc, ColliderShapeDesc,
LevelObject, LightDesc, ModelRef, NavigationArea, NavigationBounds, NavigationLink,
NavigationObstacle, ObjectiveMarker, PhysicsBody, PlayerSpawn, PostProcessVolumeDesc,
PrefabRef, ProjectSun, RaytracingExcluded, SkinnedMeshRenderer, TeamSpawn, TriggerVolume,
WeaponSpawn,
PrefabRef, ProjectSun, RaytracingExcluded, RigidBodyDesc, SkinnedMeshRenderer, TeamSpawn,
TriggerVolume, WeaponSpawn,
};
use sim::tuning::{EYE_OFFSET_STAND, PLAYER_LENGTH, PLAYER_RADIUS};
use sim::{Player, PlayerCamera};
@@ -16,6 +18,10 @@ use sim::{Player, PlayerCamera};
use crate::infra::EditorOnly;
use crate::selection::SelectedEntity;
use crate::state::{scene_tools_active, EditorMode, PlayPossession};
use crate::viewport::collider_diagnostics::{
refresh_collider_diagnostics, ColliderDiagnosticEntry, ColliderDiagnosticsState,
ColliderOverlayStatus,
};
use crate::viewport::{EditorViewportMode, ViewportDisplayMode};
#[derive(Resource, Debug, Clone)]
@@ -102,7 +108,7 @@ struct VisualizerTargetQueries<'w, 's> {
Entity,
(
With<LevelObject>,
Or<(With<PhysicsBody>, With<ColliderDesc>)>,
Or<(With<PhysicsBody>, With<ColliderDesc>, With<RigidBodyDesc>)>,
),
>,
lights: Query<'w, 's, Entity, (With<LevelObject>, With<LightDesc>)>,
@@ -127,7 +133,15 @@ pub struct EditorVisualizersPlugin;
impl Plugin for EditorVisualizersPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<EditorVisualizationSettings>()
.init_resource::<ColliderDiagnosticsState>()
.add_systems(Startup, init_visualizer_proxy_assets)
.add_systems(
Update,
refresh_collider_diagnostics
.before(sync_visualizer_proxies)
.before(draw_visualizers)
.before(draw_split_collider_visualizers),
)
.add_systems(Update, sync_visualizer_proxies)
.add_systems(
Update,
@@ -135,6 +149,7 @@ impl Plugin for EditorVisualizersPlugin {
draw_visualizers,
draw_audio_visualizers,
draw_split_collider_visualizers,
draw_missing_collider_visualizers,
draw_post_process_volume_gizmos,
draw_navigation_visualizers,
)
@@ -145,6 +160,52 @@ impl Plugin for EditorVisualizersPlugin {
}
}
fn draw_missing_collider_visualizers(
viewport_mode: Res<EditorViewportMode>,
settings: Res<EditorVisualizationSettings>,
diagnostics: Res<ColliderDiagnosticsState>,
selected: Res<SelectedEntity>,
mut gizmos: Gizmos,
missing: Query<
(Entity, &GlobalTransform),
(
With<LevelObject>,
With<RigidBodyDesc>,
Without<ColliderDesc>,
Without<PhysicsBody>,
),
>,
) {
let collider_mode = *viewport_mode == EditorViewportMode::Collider;
if !(collider_mode || settings.enabled && settings.show_colliders) {
return;
}
for (entity, global) in &missing {
let color = collider_overlay_color(
diagnostics.entry(entity),
selected.0 == Some(entity),
collider_mode,
);
let center = global.translation();
let radius = if selected.0 == Some(entity) {
0.48
} else {
0.34
};
draw_box(
&mut gizmos,
center,
global.rotation(),
Vec3::splat(radius),
color,
);
for axis in [Vec3::X, Vec3::Y, Vec3::Z] {
let direction = global.rotation() * axis * radius * 1.35;
gizmos.line(center - direction, center + direction, color);
}
}
}
fn draw_navigation_visualizers(
state: Res<crate::ui::navigation_inspector::NavigationEditorState>,
mut gizmos: Gizmos,
@@ -556,17 +617,7 @@ fn draw_visualizers(
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)
};
let color = collider_overlay_color(None, selected.0 == Some(entity), collider_mode);
draw_collider(&mut gizmos, global, &body.collider, color);
}
}
@@ -679,11 +730,18 @@ fn draw_visualizers(
}
}
#[expect(
clippy::too_many_arguments,
reason = "collider overlays combine authoring, runtime bounds, selection, and viewport state"
)]
fn draw_split_collider_visualizers(
viewport_mode: Res<EditorViewportMode>,
settings: Res<EditorVisualizationSettings>,
diagnostics: Res<ColliderDiagnosticsState>,
selected: Res<SelectedEntity>,
mut gizmos: Gizmos,
children: Query<&Children>,
runtime_aabbs: Query<&ColliderAabb>,
colliders: Query<
(Entity, &ColliderDesc, &GlobalTransform),
(With<LevelObject>, Without<PhysicsBody>),
@@ -697,17 +755,80 @@ fn draw_split_collider_visualizers(
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);
let color = collider_overlay_color(
diagnostics.entry(entity),
selected.0 == Some(entity),
collider_mode,
);
let drew_runtime_mesh = matches!(collider.shape, ColliderShapeDesc::StaticMesh { .. })
&& draw_runtime_collider_bounds(&mut gizmos, entity, &children, &runtime_aabbs, color);
if !drew_runtime_mesh {
draw_collider_desc(&mut gizmos, global, collider, color);
}
}
}
fn collider_overlay_color(
entry: Option<&ColliderDiagnosticEntry>,
selected: bool,
collider_mode: bool,
) -> Color {
let status = entry
.map(|entry| entry.overlay_status)
.unwrap_or(ColliderOverlayStatus::Valid);
let (rgb, base_alpha): (Vec3, f32) = match status {
ColliderOverlayStatus::Valid => (Vec3::new(0.25, 0.92, 0.43), 0.72),
ColliderOverlayStatus::Trigger => (Vec3::new(0.18, 0.82, 1.0), 0.78),
ColliderOverlayStatus::Disabled => (Vec3::new(0.52, 0.56, 0.62), 0.42),
ColliderOverlayStatus::Warning => (Vec3::new(1.0, 0.68, 0.18), 0.9),
ColliderOverlayStatus::Error => (Vec3::new(1.0, 0.24, 0.3), 0.96),
};
let alpha = if selected {
1.0
} else if collider_mode {
base_alpha.max(0.82)
} else {
base_alpha * 0.72
};
let rgb = if selected {
rgb.lerp(Vec3::ONE, 0.18)
} else {
rgb
};
Color::srgba(rgb.x, rgb.y, rgb.z, alpha)
}
fn draw_runtime_collider_bounds(
gizmos: &mut Gizmos,
root: Entity,
children: &Query<&Children>,
aabbs: &Query<&ColliderAabb>,
color: Color,
) -> bool {
let mut pending = vec![root];
let mut drew = false;
while let Some(entity) = pending.pop() {
if let Ok(aabb) = aabbs.get(entity) {
let min = aabb.min;
let max = aabb.max;
if min.is_finite() && max.is_finite() && min.cmplt(max).all() {
draw_box(
gizmos,
(min + max) * 0.5,
Quat::IDENTITY,
(max - min) * 0.5,
color,
);
drew = true;
}
}
if let Ok(entity_children) = children.get(entity) {
pending.extend(entity_children.iter());
}
}
drew
}
fn draw_post_process_volume_gizmos(
viewport_mode: Res<EditorViewportMode>,
settings: Res<EditorVisualizationSettings>,
@@ -741,6 +862,8 @@ fn draw_collider(
collider: &ColliderConstructor,
color: Color,
) {
let (scale, rotation, translation) = global.to_scale_rotation_translation();
let scale = scale.abs();
match collider {
ColliderConstructor::Cuboid {
x_length,
@@ -748,27 +871,21 @@ fn draw_collider(
z_length,
} => draw_box(
gizmos,
global.translation(),
global.rotation(),
Vec3::new(*x_length, *y_length, *z_length) * 0.5,
translation,
rotation,
Vec3::new(*x_length, *y_length, *z_length) * scale * 0.5,
color,
),
ColliderConstructor::Sphere { radius } => {
gizmos
.sphere(
Isometry3d::new(global.translation(), global.rotation()),
*radius,
color,
)
.resolution(24);
draw_ellipsoid(gizmos, translation, rotation, scale * *radius, color);
}
ColliderConstructor::Capsule { radius, height } => {
draw_capsule(
gizmos,
global.translation(),
global.rotation(),
*radius,
*height,
translation,
rotation,
*radius * scale.x.max(scale.z),
*height * scale.y,
color,
);
}
@@ -790,9 +907,8 @@ fn draw_collider_desc(
collider: &ColliderDesc,
color: Color,
) {
if !collider.enabled {
return;
}
let (scale, rotation, translation) = global.to_scale_rotation_translation();
let scale = scale.abs();
match &collider.shape {
ColliderShapeDesc::Cuboid {
x_length,
@@ -800,42 +916,59 @@ fn draw_collider_desc(
z_length,
} => draw_box(
gizmos,
global.translation(),
global.rotation(),
Vec3::new(*x_length, *y_length, *z_length) * 0.5,
translation,
rotation,
Vec3::new(*x_length, *y_length, *z_length) * scale * 0.5,
color,
),
ColliderShapeDesc::Sphere { radius } => {
gizmos
.sphere(
Isometry3d::new(global.translation(), global.rotation()),
*radius,
color,
)
.resolution(24);
draw_ellipsoid(gizmos, translation, rotation, scale * *radius, color);
}
ColliderShapeDesc::Capsule { radius, height } => {
draw_capsule(
gizmos,
global.translation(),
global.rotation(),
*radius,
*height,
translation,
rotation,
*radius * scale.x.max(scale.z),
*height * scale.y,
color,
);
}
ColliderShapeDesc::StaticMesh { .. } => {
gizmos
.sphere(
Isometry3d::new(global.translation(), global.rotation()),
0.35,
color,
)
.resolution(16);
draw_box(gizmos, translation, rotation, Vec3::splat(0.24), color);
gizmos.line(
translation - rotation * Vec3::splat(0.34),
translation + rotation * Vec3::splat(0.34),
color,
);
}
}
}
fn draw_ellipsoid(gizmos: &mut Gizmos, center: Vec3, rotation: Quat, radii: Vec3, color: Color) {
gizmos
.ellipse(
Isometry3d::new(center, rotation),
Vec2::new(radii.x, radii.z),
color,
)
.resolution(32);
gizmos
.ellipse(
Isometry3d::new(center, rotation * Quat::from_rotation_x(FRAC_PI_2)),
Vec2::new(radii.x, radii.y),
color,
)
.resolution(32);
gizmos
.ellipse(
Isometry3d::new(center, rotation * Quat::from_rotation_z(FRAC_PI_2)),
Vec2::new(radii.y, radii.z),
color,
)
.resolution(32);
}
fn draw_light(
gizmos: &mut Gizmos,
global: &GlobalTransform,