From 21ea3406d937595238afcbc0cb67f33a26b139db Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 4 Jul 2023 11:44:43 -0400 Subject: [PATCH] Merge pull request #7557 from smix8/diff_navagent_source_geometry_data_4.x Update navigation different actor types for new SourceGeometryData --- .../navigation_different_actor_types.rst | 75 +++++++++++-------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/tutorials/navigation/navigation_different_actor_types.rst b/tutorials/navigation/navigation_different_actor_types.rst index 19707a510f9..4b117a9b4ee 100644 --- a/tutorials/navigation/navigation_different_actor_types.rst +++ b/tutorials/navigation/navigation_different_actor_types.rst @@ -16,12 +16,12 @@ The same approach can be used to distinguish between e.g. landwalking, swimming .. tabs:: .. code-tab:: gdscript GDScript - # create navigation mesh resources for each actor size + # Create a navigation mesh resource for each actor size. var navigation_mesh_standard_size: NavigationMesh = NavigationMesh.new() var navigation_mesh_small_size: NavigationMesh = NavigationMesh.new() var navigation_mesh_huge_size: NavigationMesh = NavigationMesh.new() - # set appropriated agent parameters + # Set appropriated agent parameters. navigation_mesh_standard_size.agent_radius = 0.5 navigation_mesh_standard_size.agent_height = 1.8 navigation_mesh_small_size.agent_radius = 0.25 @@ -29,38 +29,53 @@ The same approach can be used to distinguish between e.g. landwalking, swimming navigation_mesh_huge_size.agent_radius = 1.5 navigation_mesh_huge_size.agent_height = 2.5 - # get the root node for the baking to parse geometry + # Get the root node to parse geometry for the baking. var root_node: Node3D = get_node("NavigationMeshBakingRootNode") - # bake the navigation geometry for each agent size - NavigationMeshGenerator.bake(navigation_mesh_standard_size, root_node) - NavigationMeshGenerator.bake(navigation_mesh_small_size, root_node) - NavigationMeshGenerator.bake(navigation_mesh_huge_size, root_node) + # Create the source geometry resource that will hold the parsed geometry data. + var source_geometry_data: NavigationMeshSourceGeometryData3D = NavigationMeshSourceGeometryData3D.new() - # create different navigation maps on the NavigationServer + # Parse the source geometry from the SceneTree on the main thread. + # The navigation mesh is only required for the parse settings so any of the three will do. + NavigationServer3D.parse_source_geometry_data(navigation_mesh_standard_size, source_geometry_data, root_node) + + # Bake the navigation geometry for each agent size from the same source geometry. + # If required for performance this baking step could also be done on background threads. + NavigationServer3D.bake_from_source_geometry_data(navigation_mesh_standard_size, source_geometry_data) + NavigationServer3D.bake_from_source_geometry_data(navigation_mesh_small_size, source_geometry_data) + NavigationServer3D.bake_from_source_geometry_data(navigation_mesh_huge_size, source_geometry_data) + + # Create different navigation maps on the NavigationServer. var navigation_map_standard: RID = NavigationServer3D.map_create() var navigation_map_small: RID = NavigationServer3D.map_create() var navigation_map_huge: RID = NavigationServer3D.map_create() - # create a region for each map - var navigation_map_standard_region: RID = NavigationServer3D.region_create() - var navigation_map_small_region: RID = NavigationServer3D.region_create() - var navigation_map_huge_region: RID = NavigationServer3D.region_create() - - # set navigation mesh for each region - NavigationServer3D.region_set_navigation_mesh(navigation_map_standard_region, navigation_mesh_standard_size) - NavigationServer3D.region_set_navigation_mesh(navigation_map_small_region, navigation_mesh_small_size) - NavigationServer3D.region_set_navigation_mesh(navigation_map_huge_region, navigation_mesh_huge_size) - - # add regions to maps - navigation_map_standard_region.region_set_map(navigation_map_standard_region, navigation_map_standard) - navigation_map_small_region.region_set_map(navigation_map_small_region, navigation_map_small) - navigation_map_huge_region.region_set_map(navigation_map_huge_region, navigation_map_huge) - - # wait a physics frame for sync - await get_tree().physics_frame - - # query paths for each size - var path_standard_agent = NavigationServer3D.map_get_path(navigation_map_standard, start_pos, end_pos, true) - var path_small_agent = NavigationServer3D.map_get_path(navigation_mesh_small_size, start_pos, end_pos, true) - var path_huge_agent = NavigationServer3D.map_get_path(navigation_map_huge, start_pos, end_pos, true) + # Set the new navigation maps as active. + NavigationServer3D.map_set_active(navigation_map_standard, true) + NavigationServer3D.map_set_active(navigation_map_small, true) + NavigationServer3D.map_set_active(navigation_map_huge, true) + + # Create a region for each map. + var navigation_region_standard: RID = NavigationServer3D.region_create() + var navigation_region_small: RID = NavigationServer3D.region_create() + var navigation_region_huge: RID = NavigationServer3D.region_create() + + # Add the regions to the maps. + NavigationServer3D.region_set_map(navigation_region_standard, navigation_map_standard) + NavigationServer3D.region_set_map(navigation_region_small, navigation_map_small) + NavigationServer3D.region_set_map(navigation_region_huge, navigation_map_huge) + + # Set navigation mesh for each region. + NavigationServer3D.region_set_navigation_mesh(navigation_region_standard, navigation_mesh_standard_size) + NavigationServer3D.region_set_navigation_mesh(navigation_region_small, navigation_mesh_small_size) + NavigationServer3D.region_set_navigation_mesh(navigation_region_huge, navigation_mesh_huge_size) + + # Create start and end position for the navigation path query. + var start_pos: Vector3 = Vector3(0.0, 0.0, 0.0) + var end_pos: Vector3 = Vector3(2.0, 0.0, 0.0) + var use_corridorfunnel: bool = true + + # Query paths for each agent size. + var path_standard_agent = NavigationServer3D.map_get_path(navigation_map_standard, start_pos, end_pos, use_corridorfunnel) + var path_small_agent = NavigationServer3D.map_get_path(navigation_map_small, start_pos, end_pos, use_corridorfunnel) + var path_huge_agent = NavigationServer3D.map_get_path(navigation_map_huge, start_pos, end_pos, use_corridorfunnel)