Kan*_*szM 5 geometry polygon gdscript godot
考虑以下场景:给定 n 个像“阴影”(具有减半 alpha 值的黑色)的 Polygon2D 节点,如何使用 merge_polygons_2d() 方法将所有这些节点组合成单个 Polygon2D 节点?
使用以下硬编码代码,我成功地将所有三个多边形合并为一个。但我无法弄清楚如何通过迭代来自动化该过程,以避免在有超过 10.. 20.. 多边形需要合并的情况下采用我的方法。
func Merge_Map_Shadows() -> void:
# Get all "shadow" polygons and store them in an array,
# then apply xform transform in order to retain it's position and rotation.
# After that, delete the original shadow.
if Map_Shadows.get_child_count() > 0:
var _detected_shadows: Array = []
for _shadow in Map_Shadows.get_children():
var _transformed_polygon: PoolVector2Array = []
for _vector in _shadow.polygon: _transformed_polygon.append(_shadow.transform.xform(_vector))
_detected_shadows.append(_transformed_polygon)
_shadow.call_deferred("queue_free")
# Create the "master shadow" node
var _master_shadow: Polygon2D = Polygon2D.new() ; _master_shadow.color = Color.black ; _master_shadow.color.a = 0.5
# Manually merge two "shadows" to the third and than apply the result to the master node
var _merged_shadow_1: Array = []
var _merged_shadow_2: Array = []
_merged_shadow_1 = Geometry.merge_polygons_2d(_detected_shadows[0], _detected_shadows[2])
_merged_shadow_2 = Geometry.merge_polygons_2d(_merged_shadow_1[0], _detected_shadows[1])
_master_shadow.set_polygon(_merged_shadow_2.front())
Map_Shadows.add_child(_master_shadow)
Run Code Online (Sandbox Code Playgroud)
谢谢你,迈克。
由于您要删除并添加为同一节点的子节点。我会利用孩子们的名单。
在开始之前,我们需要记住几件事:
Polygon2D.Polygon2D重叠。Polygon2D节点可能应用了非恒等变换。计划是将多边形合并到位。我将列出需要删除的多边形的列表。稍后会回来讨论这一点。
让我们首先迭代子列表。我使用整数索引的原因稍后会有意义,所以我有:
for child_index in Map_Shadows.get_child_count():
pass
Run Code Online (Sandbox Code Playgroud)
我们当然需要孩子,所以:
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
Run Code Online (Sandbox Code Playgroud)
但我们需要确保它是一个Polygon2D
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
var found_polygon:Polygon2D = child as Polygon2D
if found_polygon == null:
continue
Run Code Online (Sandbox Code Playgroud)
此外,由于我们将从同一个列表中删除,因此我们需要考虑它可能会排队等待删除:
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
var found_polygon:Polygon2D = child as Polygon2D
if found_polygon == null or found_polygon.is_queued_for_deletion():
continue
Run Code Online (Sandbox Code Playgroud)
接下来我们需要检查孩子是否应用了非身份转换,如果有,则撤消它:
if found_polygon.transform != Transform2D.IDENTITY:
var transformed_polygon = found_polygon.transform.xform(found_polygon.polygon)
found_polygon.transform = Transform2D.IDENTITY
found_polygon.polygon = transformed_polygon
Run Code Online (Sandbox Code Playgroud)
请注意,我们不需要迭代多边形的点。
现在我们将尝试将多边形与我们已经看到的所有多边形合并。为此,我们需要另一个循环。它看起来就像第一个,只是它上升到当前索引。这就是我使用索引进行迭代的原因。
for child_subindex in child_index:
var other_child = Map_Shadows.get_child(child_subindex)
var other_found_polygon:Polygon2D = other_child as Polygon2D
if other_found_polygon == null or other_found_polygon.is_queued_for_deletion():
continue
Run Code Online (Sandbox Code Playgroud)
现在我们尝试合并:
var merged_polygon = Geometry.merge_polygons_2d(found_polygon.polygon, other_found_polygon.polygon)
Run Code Online (Sandbox Code Playgroud)
如果它们合并,我们会得到一个包含单个项目(合并的多边形)的数组,如果不是这种情况,则它们不会合并。因此:
if merged_polygon.size() != 1:
continue
Run Code Online (Sandbox Code Playgroud)
最后,当它们合并时,我们将删除当前的多边形(好吧,我们将把它放入一个数组中以稍后删除它)并将另一个设置为合并的多边形:
other_found_polygon.polygon = merged_polygon[0]
polygons_to_remove.append(found_polygon)
break
Run Code Online (Sandbox Code Playgroud)
我在这里中断是因为我们已经找到了一个多边形来合并当前的多边形。无需继续寻找。
当然,一次遍历多边形可能无法完成所有合并。因此,将整个事情放入一个while(true)循环中,如下所示:
var polygons_to_remove:Array
while(true):
polygons_to_remove = []
# the rest of the code here
if polygons_to_remove.size() == 0:
break
for polygon_to_remove in polygons_to_remove:
polygon_to_remove.queue_free()
Run Code Online (Sandbox Code Playgroud)
正如我在开始时所说,我们保留需要删除的多边形的列表(好吧,一个数组)。
如果我们没有合并任何多边形,那么我们也不必删除任何多边形。这意味着我们完成了。这就是为什么当要删除的多边形列表为空时我们break退出循环。while(true)
当然,如果列表不为空,我们实际上需要删除这些节点。所以呼吁queue_free他们。不,我们不需要call_deferred,事实上,我们不想要,call_deferred因为我们正在检查is_queued_for_deletion,所以我们需要他们立即排队。
因为人们喜欢复制和粘贴,所以这是完整的相关代码:
var polygons_to_remove:Array
while(true):
polygons_to_remove = []
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
var found_polygon:Polygon2D = child as Polygon2D
if found_polygon == null or found_polygon.is_queued_for_deletion():
continue
if found_polygon.transform != Transform2D.IDENTITY:
var transformed_polygon = found_polygon.transform.xform(found_polygon.polygon)
found_polygon.transform = Transform2D.IDENTITY
found_polygon.polygon = transformed_polygon
for child_subindex in child_index:
var other_child = Map_Shadows.get_child(child_subindex)
var other_found_polygon:Polygon2D = other_child as Polygon2D
if other_found_polygon == null or other_found_polygon.is_queued_for_deletion():
continue
var merged_polygon = Geometry.merge_polygons_2d(found_polygon.polygon, other_found_polygon.polygon)
if merged_polygon.size() != 1:
continue
other_found_polygon.polygon = merged_polygon[0]
polygons_to_remove.append(found_polygon)
break
if polygons_to_remove.size() == 0:
break
for polygon_to_remove in polygons_to_remove:
polygon_to_remove.queue_free()
Run Code Online (Sandbox Code Playgroud)
是的,我测试过那个东西。有用。然而,我假设这些都是表现良好的多边形。可能存在边缘情况。特别是,我没有使用Polygon2D少于三个顶点的 a、Polygon2Dwithinvert_enable或Polygon2Dwithpolygons集进行测试(请参阅Polygon2D 的多边形属性有什么作用?)。
我还可以考虑进一步的优化:我们只需要检查在前一过程中合并的多边形之间的进一步合并。如果一个多边形完成了一次传递而没有合并,则意味着它是孤立的,我们不需要继续检查该多边形。