Group Holes with Hull Convex

This commit is contained in:
2025-11-24 20:29:46 -05:00
parent 0a3919ce4c
commit 1c8e3d7314
13 changed files with 77 additions and 39 deletions

View File

@@ -10,10 +10,11 @@ enum ExtrudeDirection {
@export_category("TrechBroom Generated Values")
@export var verts2d: PackedVector2Array
@export var meshInstance3d: MeshInstance3D
@export var extrusion_direction: ExtrudeDirection
@export var depth: float = 0.1
@export var depth_position_offset: Vector3 = Vector3.ZERO
@export_category("User Defined Values")
@export_category("TrechBroom User Values")
@export var extrusion_direction: ExtrudeDirection
@export var hole_proximity: float
@export var edge_non_fracture: float = 0.1
var parentNode
@@ -44,6 +45,8 @@ func _func_godot_apply_properties(entity_properties: Dictionary) -> void:
var verticies = meshInstance3d.mesh.surface_get_arrays(0)[Mesh.ARRAY_VERTEX]
extrusion_direction = entity_properties["extrude_direction"]
hole_proximity = entity_properties["hole_proximity"]
edge_non_fracture = entity_properties["edge_non_fractured"]
var depth_min: float
var depth_max: float
@@ -192,6 +195,24 @@ func _add_hole(new_hole: PackedVector2Array) -> bool:
var bound_rect = UTIL.get_bounding_rect(merged)
inner_rtree.add(bound_rect.position, bound_rect.end, hole.hole_id)
var hole_prox_boundary = bound_rect.grow(hole.area * 4)
var prox_holes_ids = inner_rtree.query_rect(hole_prox_boundary.position, hole_prox_boundary.end)
# any holes that are close in proximity convex hull
if len(prox_holes_ids) <= 1: # needs to be length 2 or more because it will always return the hole just created
return true
var proxmity_hole_vectors = []
for id in prox_holes_ids:
proxmity_hole_vectors.append_array(inner_polygons.get_hole(id).vectors)
inner_rtree.remove(id)
inner_polygons.remove_hole(id)
var convex_vectors = Geometry2D.convex_hull(proxmity_hole_vectors)
hole = inner_polygons.add_hole(convex_vectors)
bound_rect = UTIL.get_bounding_rect(convex_vectors)
inner_rtree.add(bound_rect.position, bound_rect.end, hole.hole_id)
return true
func _find_static_body():

View File

@@ -3,11 +3,15 @@ extends Resource
class_name Hole
var hole_id: int
var vectors: PackedVector2Array
var vectors: PackedVector2Array # verticies
var area: float
func _init(hole_id: int, vectors: PackedVector2Array):
self.hole_id = hole_id
self.vectors = vectors
self.area = UTIL.get_polygon_area(self.vectors)
func center():
var avg_vert = Vector2.ZERO

View File

@@ -25,3 +25,13 @@ static func get_bounding_rect(vectors: PackedVector2Array) -> Rect2:
# The 'position' of the Rect2 will be the minimum corner
return rect
static func get_polygon_area(vectors: PackedVector2Array) -> float:
var result := 0.0
var num_vertices := vectors.size()
for q in range(num_vertices):
var p = (q - 1 + num_vertices) % num_vertices
result += vectors[q].cross(vectors[p])
return abs(result * 0.5)