NavigationPolygon
实验性: This class may be changed or removed in future versions.
继承: Resource < RefCounted < Object
2D 导航网格,描述用于寻路的可穿越表面。
描述
导航网格可以通过在 NavigationServer2D 的帮助下烘焙它来创建,也可以通过手动添加顶点和凸多边形索引数组来创建。
要烘焙导航网格,至少需要添加一个轮廓来定义烘焙区域的外部边界。
var new_navigation_mesh = NavigationPolygon.new()
var bounding_outline = PackedVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
new_navigation_mesh.add_outline(bounding_outline)
NavigationServer2D.bake_from_source_geometry_data(new_navigation_mesh, NavigationMeshSourceGeometryData2D.new());
$NavigationRegion2D.navigation_polygon = new_navigation_mesh
var newNavigationMesh = new NavigationPolygon();
Vector2[] boundingOutline = [new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0)];
newNavigationMesh.AddOutline(boundingOutline);
NavigationServer2D.BakeFromSourceGeometryData(newNavigationMesh, new NavigationMeshSourceGeometryData2D());
GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = newNavigationMesh;
手动添加顶点和多边形索引。
var new_navigation_mesh = NavigationPolygon.new()
var new_vertices = PackedVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
new_navigation_mesh.vertices = new_vertices
var new_polygon_indices = PackedInt32Array([0, 1, 2, 3])
new_navigation_mesh.add_polygon(new_polygon_indices)
$NavigationRegion2D.navigation_polygon = new_navigation_mesh
var newNavigationMesh = new NavigationPolygon();
Vector2[] newVertices = [new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0)];
newNavigationMesh.Vertices = newVertices;
int[] newPolygonIndices = [0, 1, 2, 3];
newNavigationMesh.AddPolygon(newPolygonIndices);
GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = newNavigationMesh;
教程
属性
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
方法
void |
add_outline(outline: PackedVector2Array) |
void |
add_outline_at_index(outline: PackedVector2Array, index: int) |
void |
add_polygon(polygon: PackedInt32Array) |
void |
clear() |
void |
|
void |
|
get_outline(idx: int) const |
|
get_outline_count() const |
|
get_parsed_collision_mask_value(layer_number: int) const |
|
get_polygon(idx: int) |
|
get_polygon_count() const |
|
get_vertices() const |
|
void |
|
void |
remove_outline(idx: int) |
void |
set_outline(idx: int, outline: PackedVector2Array) |
void |
set_parsed_collision_mask_value(layer_number: int, value: bool) |
void |
set_vertices(vertices: PackedVector2Array) |
枚举
enum SamplePartitionType: 🔗
SamplePartitionType SAMPLE_PARTITION_CONVEX_PARTITION = 0
Convex partitioning that results in a navigation mesh with convex polygons.
SamplePartitionType SAMPLE_PARTITION_TRIANGULATE = 1
Triangulation partitioning that results in a navigation mesh with triangle polygons.
SamplePartitionType SAMPLE_PARTITION_MAX = 2
代表 SamplePartitionType 枚举的大小。
enum ParsedGeometryType: 🔗
ParsedGeometryType PARSED_GEOMETRY_MESH_INSTANCES = 0
将网格实例解析为障碍几何体。这包括 Polygon2D、MeshInstance2D、MultiMeshInstance2D 和 TileMap 节点。
仅当网格使用 2D 顶点表面格式时才会对其进行解析。
ParsedGeometryType PARSED_GEOMETRY_STATIC_COLLIDERS = 1
将 StaticBody2D 和 TileMap 碰撞器解析为障碍几何体。碰撞器应在由 parsed_collision_mask 指定的层中。
ParsedGeometryType PARSED_GEOMETRY_BOTH = 2
PARSED_GEOMETRY_MESH_INSTANCES 和 PARSED_GEOMETRY_STATIC_COLLIDERS.
ParsedGeometryType PARSED_GEOMETRY_MAX = 3
代表 ParsedGeometryType 枚举的大小。
enum SourceGeometryMode: 🔗
SourceGeometryMode SOURCE_GEOMETRY_ROOT_NODE_CHILDREN = 0
在根节点的子节点中递归扫描几何体。
SourceGeometryMode SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN = 1
以递归方式扫描组中的节点及其子节点以获取几何图形。该组由 source_geometry_group_name 指定。
SourceGeometryMode SOURCE_GEOMETRY_GROUPS_EXPLICIT = 2
将组中的节点用于几何。该组由 source_geometry_group_name 指定。
SourceGeometryMode SOURCE_GEOMETRY_MAX = 3
代表 SourceGeometryMode 枚举的大小。
属性说明
在烘焙导航网格时,用于侵蚀/缩小可行走表面的距离。
注意:半径必须大于等于 0.0。如果半径为0.0,则无法在烘焙过程中修复无效的轮廓重叠和其他精度错误。因此,某些障碍物可能会被错误地排除在最终导航网格之外,也有可能会删除导航网格的多边形。
Rect2 baking_rect = Rect2(0, 0, 0, 0) 🔗
如果烘焙的 Rect2 存在面积,则导航网格烘焙将被限制在其封闭区域内。
Vector2 baking_rect_offset = Vector2(0, 0) 🔗
应用于 baking_rect Rect2 的位置偏移量。
由 baking_rect Rect2 定义的烘焙边界区域周围的不可导航边框的大小。
与 baking_rect 结合使用,边框大小可用于烘焙图块对齐的导航网格,而图块边缘不会因 agent_radius 而缩小。
用于将导航网格顶点栅格化的单元格大小。必须与导航地图上的单元格大小相匹配。
int parsed_collision_mask = 4294967295 🔗
用于扫描静态碰撞器的物理层。
仅在 parsed_geometry_type 是 PARSED_GEOMETRY_STATIC_COLLIDERS 或 PARSED_GEOMETRY_BOTH 时才使用。
ParsedGeometryType parsed_geometry_type = 2 🔗
void set_parsed_geometry_type(value: ParsedGeometryType)
ParsedGeometryType get_parsed_geometry_type()
决定哪种类型的节点可解析为几何图形。
SamplePartitionType sample_partition_type = 0 🔗
void set_sample_partition_type(value: SamplePartitionType)
SamplePartitionType get_sample_partition_type()
创建导航网格多边形单元的分割算法。
StringName source_geometry_group_name = &"navigation_polygon_source_geometry_group" 🔗
void set_source_geometry_group_name(value: StringName)
StringName get_source_geometry_group_name()
应被解析以烘焙源几何体的节点的组名称。
只有当 source_geometry_mode 是 SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN 或 SOURCE_GEOMETRY_GROUPS_EXPLICIT 时才使用。
SourceGeometryMode source_geometry_mode = 0 🔗
void set_source_geometry_mode(value: SourceGeometryMode)
SourceGeometryMode get_source_geometry_mode()
烘焙时使用的几何体来源。
方法说明
void add_outline(outline: PackedVector2Array) 🔗
将包含轮廓顶点的 PackedVector2Array 追加到包含所有轮廓的内部数组。
void add_outline_at_index(outline: PackedVector2Array, index: int) 🔗
将一个包含轮廓顶点的 PackedVector2Array 添加到包含固定位置处的所有轮廓的内部数组。
void add_polygon(polygon: PackedInt32Array) 🔗
使用调用 get_vertices() 得到的顶点的索引添加一个多边形。
void clear() 🔗
清除顶点和多边形索引的内部数组。
void clear_outlines() 🔗
清除轮廓数组,但不清除顶点和由顶点创建的多边形。
void clear_polygons() 🔗
清除多边形数组,但不清除轮廓和顶点数组。
NavigationMesh get_navigation_mesh() 🔗
返回由该导航多边形产生的 NavigationMesh。该导航网格可用于使用 NavigationServer3D.region_set_navigation_mesh() API 直接更新区块的导航网格。
PackedVector2Array get_outline(idx: int) const 🔗
返回一个 PackedVector2Array,其中包含在编辑器中或通过脚本创建的轮廓的顶点。
int get_outline_count() const 🔗
返回在编辑器或脚本中创建的轮廓的数量。
bool get_parsed_collision_mask_value(layer_number: int) const 🔗
返回 parsed_collision_mask 中是否启用了指定的层,给定的 layer_number 应在 1 和 32 之间。
PackedInt32Array get_polygon(idx: int) 🔗
返回创建多边形的顶点索引,形式为 PackedInt32Array。
int get_polygon_count() const 🔗
返回多边形的数量。
PackedVector2Array get_vertices() const 🔗
返回一个 PackedVector2Array,其中包含用于创建多边形的所有顶点。
void make_polygons_from_outlines() 🔗
已弃用: Use NavigationServer2D.parse_source_geometry_data() and NavigationServer2D.bake_from_source_geometry_data() instead.
从编辑器中添加的轮廓或通过脚本创建多边形。
void remove_outline(idx: int) 🔗
删除在编辑器或脚本中创建的轮廓。你必须调用 make_polygons_from_outlines() 来更新多边形。
void set_outline(idx: int, outline: PackedVector2Array) 🔗
更改在编辑器或脚本中创建的轮廓。你必须调用 make_polygons_from_outlines() 来更新多边形。
void set_parsed_collision_mask_value(layer_number: int, value: bool) 🔗
根据 value,启用或禁用 parsed_collision_mask 中指定的层,给定的 layer_number 应在 1 和 32 之间。
void set_vertices(vertices: PackedVector2Array) 🔗
设置顶点,可以使用 add_polygon() 方法对其进行索引,创建多边形。