This commit is contained in:
1 parent
d847706b8f
commit
4dc22cd3b6
5 files changed
+213
-3
No files matched your search
@@ -2,6 +2,8 @@
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{BrushDesc, ComponentInstanceId};
|
||||
|
||||
const EPSILON: f32 = 0.0001;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -14,6 +16,107 @@ pub enum BrushPolygonError {
|
||||
NonConvex,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BrushDiagnosticSeverity {
|
||||
Warning,
|
||||
Error,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct BrushDiagnostic {
|
||||
pub severity: BrushDiagnosticSeverity,
|
||||
pub face: Option<ComponentInstanceId>,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct BrushValidationReport {
|
||||
pub diagnostics: Vec<BrushDiagnostic>,
|
||||
}
|
||||
|
||||
impl BrushValidationReport {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
!self
|
||||
.diagnostics
|
||||
.iter()
|
||||
.any(|diagnostic| diagnostic.severity == BrushDiagnosticSeverity::Error)
|
||||
}
|
||||
|
||||
pub fn push(
|
||||
&mut self,
|
||||
severity: BrushDiagnosticSeverity,
|
||||
face: Option<ComponentInstanceId>,
|
||||
message: impl Into<String>,
|
||||
) {
|
||||
self.diagnostics.push(BrushDiagnostic {
|
||||
severity,
|
||||
face,
|
||||
message: message.into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_brush(brush: &BrushDesc) -> BrushValidationReport {
|
||||
let mut report = BrushValidationReport::default();
|
||||
if brush.faces.is_empty() {
|
||||
report.push(BrushDiagnosticSeverity::Error, None, "Brush has no faces.");
|
||||
return report;
|
||||
}
|
||||
|
||||
for face in &brush.faces {
|
||||
if face.vertices.len() < 3 {
|
||||
report.push(
|
||||
BrushDiagnosticSeverity::Error,
|
||||
Some(face.id.clone()),
|
||||
"Face has fewer than three vertices.",
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if !face.vertices.iter().all(|vertex| vertex.is_finite()) {
|
||||
report.push(
|
||||
BrushDiagnosticSeverity::Error,
|
||||
Some(face.id.clone()),
|
||||
"Face contains non-finite vertex coordinates.",
|
||||
);
|
||||
}
|
||||
for i in 0..face.vertices.len() {
|
||||
for j in (i + 1)..face.vertices.len() {
|
||||
if face.vertices[i].distance_squared(face.vertices[j]) <= EPSILON * EPSILON {
|
||||
report.push(
|
||||
BrushDiagnosticSeverity::Error,
|
||||
Some(face.id.clone()),
|
||||
"Face contains duplicate vertices.",
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if face.plane.normal.try_normalize().is_none() {
|
||||
report.push(
|
||||
BrushDiagnosticSeverity::Error,
|
||||
Some(face.id.clone()),
|
||||
"Face plane normal is invalid.",
|
||||
);
|
||||
}
|
||||
if polygon_area_3d(&face.vertices) <= EPSILON {
|
||||
report.push(
|
||||
BrushDiagnosticSeverity::Error,
|
||||
Some(face.id.clone()),
|
||||
"Face area is zero or degenerate.",
|
||||
);
|
||||
}
|
||||
if !face.uv_scale.is_finite() || face.uv_scale.cmpeq(Vec2::ZERO).any() {
|
||||
report.push(
|
||||
BrushDiagnosticSeverity::Warning,
|
||||
Some(face.id.clone()),
|
||||
"Face UV scale is zero or non-finite.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
report
|
||||
}
|
||||
|
||||
pub fn signed_area_xz(vertices: &[Vec3]) -> f32 {
|
||||
if vertices.len() < 3 {
|
||||
return 0.0;
|
||||
@@ -94,6 +197,21 @@ fn orient_xz(a: Vec3, b: Vec3, c: Vec3) -> f32 {
|
||||
ab.x * ac.y - ab.y * ac.x
|
||||
}
|
||||
|
||||
fn polygon_area_3d(vertices: &[Vec3]) -> f32 {
|
||||
if vertices.len() < 3 {
|
||||
return 0.0;
|
||||
}
|
||||
let origin = vertices[0];
|
||||
let mut area = 0.0;
|
||||
for index in 1..(vertices.len() - 1) {
|
||||
area += (vertices[index] - origin)
|
||||
.cross(vertices[index + 1] - origin)
|
||||
.length()
|
||||
* 0.5;
|
||||
}
|
||||
area
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -128,4 +246,28 @@ mod tests {
|
||||
Err(BrushPolygonError::NonConvex)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_invalid_brush_faces() {
|
||||
let mut brush = BrushDesc::default();
|
||||
brush.faces[0].vertices.clear();
|
||||
let report = validate_brush(&brush);
|
||||
assert!(!report.is_valid());
|
||||
assert!(report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.any(|diagnostic| diagnostic.message.contains("fewer than three")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_uv_warnings_without_invalidating_brush() {
|
||||
let mut brush = BrushDesc::default();
|
||||
brush.faces[0].uv_scale = Vec2::ZERO;
|
||||
let report = validate_brush(&brush);
|
||||
assert!(report.is_valid());
|
||||
assert!(report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.any(|diagnostic| diagnostic.severity == BrushDiagnosticSeverity::Warning));
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ use bevy::mesh::{Indices, PrimitiveTopology};
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
inspector_component_active, BrushDesc, BrushFaceDesc, ColliderDesc, InspectorOrder,
|
||||
LevelObject, MaterialDesc, COMPONENT_BRUSH_DESC, COMPONENT_COLLIDER_DESC,
|
||||
brush_math::validate_brush, inspector_component_active, BrushDesc, BrushFaceDesc, ColliderDesc,
|
||||
InspectorOrder, LevelObject, MaterialDesc, COMPONENT_BRUSH_DESC, COMPONENT_COLLIDER_DESC,
|
||||
COMPONENT_MATERIAL_DESC,
|
||||
};
|
||||
|
||||
@@ -75,6 +75,16 @@ pub fn spawn_brush_mesh(
|
||||
material: Option<&MaterialDesc>,
|
||||
collider: Option<&ColliderDesc>,
|
||||
) {
|
||||
let report = validate_brush(brush);
|
||||
if !report.is_valid() {
|
||||
for diagnostic in report.diagnostics {
|
||||
warn!(
|
||||
"Brush hydration skipped invalid brush on {parent:?}: {}",
|
||||
diagnostic.message
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let Some(mesh) = brush_mesh(brush) else {
|
||||
warn!("Brush hydration skipped invalid brush on {parent:?}");
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user