75 lines
2.1 KiB
GDScript
75 lines
2.1 KiB
GDScript
extends SceneTree
|
|
|
|
const OUTPUT_PATH := "res://world/jajce/JajceNavigationMesh.tres"
|
|
const WORLD_SCENE_PATH := "res://world/jajce/JajceWorld.tscn"
|
|
const TERRAIN_PATH := "TerrainRoot/Terrain3D"
|
|
|
|
|
|
func _initialize() -> void:
|
|
call_deferred("_run")
|
|
|
|
|
|
func _run() -> void:
|
|
var world_scene := load(WORLD_SCENE_PATH) as PackedScene
|
|
if world_scene == null:
|
|
push_error("Could not load %s" % WORLD_SCENE_PATH)
|
|
quit(1)
|
|
return
|
|
|
|
var world := world_scene.instantiate() as Node3D
|
|
root.add_child(world)
|
|
await process_frame
|
|
for _frame in 10:
|
|
await physics_frame
|
|
|
|
var terrain := world.get_node(TERRAIN_PATH) as Terrain3D
|
|
if terrain == null or terrain.data == null:
|
|
push_error("Jajce Terrain3D data is not available")
|
|
quit(1)
|
|
return
|
|
|
|
var nav_mesh := _create_navigation_mesh_template()
|
|
var bake_aabb: AABB = nav_mesh.filter_baking_aabb
|
|
bake_aabb.position += nav_mesh.filter_baking_aabb_offset
|
|
|
|
var terrain_faces: PackedVector3Array = terrain.generate_nav_mesh_source_geometry(bake_aabb, false)
|
|
if terrain_faces.is_empty():
|
|
push_error("Terrain3D produced no navigation source faces")
|
|
quit(1)
|
|
return
|
|
|
|
var source_geometry := NavigationMeshSourceGeometryData3D.new()
|
|
source_geometry.add_faces(terrain_faces, Transform3D.IDENTITY)
|
|
NavigationServer3D.bake_from_source_geometry_data(nav_mesh, source_geometry)
|
|
|
|
if nav_mesh.get_polygon_count() == 0:
|
|
push_error("Baked Jajce navigation mesh contains no polygons")
|
|
quit(1)
|
|
return
|
|
|
|
var error := ResourceSaver.save(nav_mesh, OUTPUT_PATH)
|
|
if error != OK:
|
|
push_error("Could not save %s: %s" % [OUTPUT_PATH, error_string(error)])
|
|
quit(1)
|
|
return
|
|
|
|
print(
|
|
"[TOOL] Saved %s with %d vertices and %d polygons"
|
|
% [OUTPUT_PATH, nav_mesh.get_vertices().size(), nav_mesh.get_polygon_count()]
|
|
)
|
|
quit(0)
|
|
|
|
|
|
func _create_navigation_mesh_template() -> NavigationMesh:
|
|
var nav_mesh := NavigationMesh.new()
|
|
nav_mesh.agent_height = 1.7
|
|
nav_mesh.agent_radius = 0.4
|
|
nav_mesh.agent_max_climb = 0.55
|
|
nav_mesh.agent_max_slope = 35.0
|
|
nav_mesh.cell_size = 0.25
|
|
nav_mesh.cell_height = 0.1
|
|
nav_mesh.filter_baking_aabb = AABB(
|
|
Vector3(-36.0, -8.0, -36.0), Vector3(72.0, 32.0, 72.0)
|
|
)
|
|
return nav_mesh
|