ShapeEditor icon indicating copy to clipboard operation
ShapeEditor copied to clipboard

feat: add CreateBrushFromPolygonMesh for direct half-edge brush creation

Open brainzhangdh opened this issue 6 months ago • 0 comments

Summary

Adds ExternalRealtimeCSG.CreateBrushFromPolygonMesh() which builds a RealtimeCSG brush directly from a PolygonMesh by constructing the half-edge connectivity data (ControlMesh) and matching Shape/Surface/TexGen data, bypassing the plane-intersection approach which can fail on certain mesh topologies.

All 6 RealtimeCSGTarget rebuild methods now try the direct PolygonMesh path first and fall back to CreateBrushFromPlanes if validation fails.

What changed

  • Added CreateBrushFromPolygonMesh() in ExternalRealtimeCSG.cs:
    • Builds unique vertex list from PolygonMesh
    • Constructs HalfEdge[] with proper twin associations
    • Creates ControlMesh with polygon/vertex/edge indices
    • Builds Shape with per-polygon Surface/TexGen/TexGenFlags
    • Validates via ControlMeshUtility.Validate before creating brush
    • Falls back to plane-based CreateBrushFromPlanes if validation fails
  • All 6 rebuild methods (FixedExtrude, ScaledExtrude, RevolveExtrude, LinearStaircase, SplineExtrude, RevolveChopped) now try direct path first

Files changed

  • Scripts/Utilities/ExternalRealtimeCSG.cs — new method
  • Scripts/Editor/Scene/RealtimeCSGTarget.*.cs — 6 rebuild methods updated

Validation

  • Code compiles against Unity 2023.2 (C# 9 / .NET 4.7.2)
  • Algorithm follows the half-edge construction pattern from the issue's reference prototype
  • Fallback path preserved for degenerate mesh cases

Related issue

Fixes #3.

brainzhangdh avatar May 14 '26 01:05 brainzhangdh