This commit is contained in:
Thonon cedric 2021-11-09 20:57:40 -08:00 committed by GitHub
commit 7327c23252
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 31 deletions

View file

@ -1,5 +1,5 @@
def can_build(env, platform):
return env["tools"]
return True
def configure(env):

View file

@ -32,7 +32,9 @@
#include "core/math/convex_hull.h"
#include "core/os/thread.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_settings.h"
#endif
#include "scene/3d/collision_shape.h"
#include "scene/3d/mesh_instance.h"
#include "scene/3d/physics_body.h"
@ -294,11 +296,23 @@ void EditorNavigationMeshGenerator::_convert_detail_mesh_to_native_navigation_me
}
}
void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh, EditorProgress *ep,
#ifdef TOOLS_ENABLED
void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh,
rcHeightfield *hf, rcCompactHeightfield *chf, rcContourSet *cset, rcPolyMesh *poly_mesh, rcPolyMeshDetail *detail_mesh,
Vector<float> &vertices, Vector<int> &indices, EditorProgress *ep = nullptr) {
#else
void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh,
rcHeightfield *hf, rcCompactHeightfield *chf, rcContourSet *cset, rcPolyMesh *poly_mesh, rcPolyMeshDetail *detail_mesh,
Vector<float> &vertices, Vector<int> &indices) {
#endif
rcContext ctx;
ep->step(TTR("Setting up Configuration..."), 1);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Setting up Configuration..."), 1);
}
#endif
const float *verts = vertices.ptr();
const int nverts = vertices.size() / 3;
@ -332,16 +346,30 @@ void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<Navigation
cfg.bmax[1] = bmax[1];
cfg.bmax[2] = bmax[2];
ep->step(TTR("Calculating grid size..."), 2);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Calculating grid size..."), 2);
}
#endif
rcCalcGridSize(cfg.bmin, cfg.bmax, cfg.cs, &cfg.width, &cfg.height);
ep->step(TTR("Creating heightfield..."), 3);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Creating heightfield..."), 3);
}
#endif
hf = rcAllocHeightfield();
ERR_FAIL_COND(!hf);
ERR_FAIL_COND(!rcCreateHeightfield(&ctx, *hf, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs, cfg.ch));
ep->step(TTR("Marking walkable triangles..."), 4);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Marking walkable triangles..."), 4);
}
#endif
{
Vector<unsigned char> tri_areas;
tri_areas.resize(ntris);
@ -364,7 +392,11 @@ void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<Navigation
rcFilterWalkableLowHeightSpans(&ctx, cfg.walkableHeight, *hf);
}
ep->step(TTR("Constructing compact heightfield..."), 5);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Constructing compact heightfield..."), 5);
}
#endif
chf = rcAllocCompactHeightfield();
@ -374,10 +406,20 @@ void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<Navigation
rcFreeHeightField(hf);
hf = nullptr;
ep->step(TTR("Eroding walkable area..."), 6);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Eroding walkable area..."), 6);
}
#endif
ERR_FAIL_COND(!rcErodeWalkableArea(&ctx, cfg.walkableRadius, *chf));
ep->step(TTR("Partitioning..."), 7);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Partitioning..."), 7);
}
#endif
if (p_nav_mesh->get_sample_partition_type() == NavigationMesh::SAMPLE_PARTITION_WATERSHED) {
ERR_FAIL_COND(!rcBuildDistanceField(&ctx, *chf));
ERR_FAIL_COND(!rcBuildRegions(&ctx, *chf, 0, cfg.minRegionArea, cfg.mergeRegionArea));
@ -387,15 +429,22 @@ void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<Navigation
ERR_FAIL_COND(!rcBuildLayerRegions(&ctx, *chf, 0, cfg.minRegionArea));
}
ep->step(TTR("Creating contours..."), 8);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Creating contours..."), 8);
}
#endif
cset = rcAllocContourSet();
ERR_FAIL_COND(!cset);
ERR_FAIL_COND(!rcBuildContours(&ctx, *chf, cfg.maxSimplificationError, cfg.maxEdgeLen, *cset));
ep->step(TTR("Creating polymesh..."), 9);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Creating polymesh..."), 9);
}
#endif
poly_mesh = rcAllocPolyMesh();
ERR_FAIL_COND(!poly_mesh);
ERR_FAIL_COND(!rcBuildPolyMesh(&ctx, *cset, cfg.maxVertsPerPoly, *poly_mesh));
@ -409,7 +458,11 @@ void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<Navigation
rcFreeContourSet(cset);
cset = nullptr;
ep->step(TTR("Converting to native navigation mesh..."), 10);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Converting to native navigation mesh..."), 10);
}
#endif
_convert_detail_mesh_to_native_navigation_mesh(detail_mesh, p_nav_mesh);
@ -431,15 +484,14 @@ EditorNavigationMeshGenerator::~EditorNavigationMeshGenerator() {
}
void EditorNavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node) {
if (!Engine::get_singleton()->is_editor_hint()) {
ERR_PRINT("Invoking EditorNavigationMeshGenerator::bake(...) in-game is not supported in Godot 3.2 or below. Aborting bake...");
return;
#ifdef TOOLS_ENABLED
EditorProgress *ep = nullptr;
if (Engine::get_singleton()->is_editor_hint()) {
ep = memnew(EditorProgress("bake", TTR("Navigation Mesh Generator Setup:"), 11));
ep->step(TTR("Parsing Geometry..."), 0);
}
ERR_FAIL_COND(!p_nav_mesh.is_valid());
EditorProgress ep("bake", TTR("Navigation Mesh Generator Setup:"), 11);
ep.step(TTR("Parsing Geometry..."), 0);
#endif
Vector<float> vertices;
Vector<int> indices;
@ -467,7 +519,7 @@ void EditorNavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p
rcPolyMesh *poly_mesh = nullptr;
rcPolyMeshDetail *detail_mesh = nullptr;
_build_recast_navigation_mesh(p_nav_mesh, &ep, hf, chf, cset, poly_mesh, detail_mesh, vertices, indices);
_build_recast_navigation_mesh(p_nav_mesh, hf, chf, cset, poly_mesh, detail_mesh, vertices, indices /*, ep*/);
rcFreeHeightField(hf);
hf = nullptr;
@ -484,7 +536,13 @@ void EditorNavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p
rcFreePolyMeshDetail(detail_mesh);
detail_mesh = nullptr;
}
ep.step(TTR("Done!"), 11);
#ifdef TOOLS_ENABLED
if (ep) {
ep->step(TTR("Done!"), 11);
memdelete(ep);
}
#endif
}
void EditorNavigationMeshGenerator::clear(Ref<NavigationMesh> p_nav_mesh) {

View file

@ -31,7 +31,12 @@
#ifndef NAVIGATION_MESH_GENERATOR_H
#define NAVIGATION_MESH_GENERATOR_H
#ifdef TOOLS_ENABLED
#include "editor/editor_node.h"
#endif
#include "scene/3d/navigation_mesh.h"
#include <Recast.h>
@ -50,10 +55,23 @@ protected:
static void _parse_geometry(Transform p_accumulated_transform, Node *p_node, Vector<float> &p_verticies, Vector<int> &p_indices, NavigationMesh::ParsedGeometryType p_generate_from, uint32_t p_collision_mask, bool p_recurse_children);
static void _convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref<NavigationMesh> p_nav_mesh);
static void _build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh, EditorProgress *ep,
#ifdef TOOLS_ENABLED
static void _build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh,
rcHeightfield *hf, rcCompactHeightfield *chf, rcContourSet *cset, rcPolyMesh *poly_mesh,
rcPolyMeshDetail *detail_mesh, Vector<float> &vertices, Vector<int> &indices, EditorProgress *ep);
#else
static void _build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh,
rcHeightfield *hf, rcCompactHeightfield *chf, rcContourSet *cset, rcPolyMesh *poly_mesh,
rcPolyMeshDetail *detail_mesh, Vector<float> &vertices, Vector<int> &indices);
#endif
#endif
public:
static EditorNavigationMeshGenerator *get_singleton();

View file

@ -30,18 +30,23 @@
#include "register_types.h"
#include "navigation_mesh_editor_plugin.h"
#ifdef TOOLS_ENABLED
EditorNavigationMeshGenerator *_nav_mesh_generator = nullptr;
#include "navigation_mesh_editor_plugin.h"
#else
#include "core/engine.h"
#endif
#include "navigation_mesh_generator.h"
EditorNavigationMeshGenerator *_nav_mesh_generator = nullptr;
void register_recast_types() {
#ifdef TOOLS_ENABLED
ClassDB::APIType prev_api = ClassDB::get_current_api();
ClassDB::set_current_api(ClassDB::API_EDITOR);
#ifdef TOOLS_ENABLED
EditorPlugins::add_by_type<NavigationMeshEditorPlugin>();
#endif
_nav_mesh_generator = memnew(EditorNavigationMeshGenerator);
ClassDB::register_class<EditorNavigationMeshGenerator>();
@ -49,13 +54,10 @@ void register_recast_types() {
Engine::get_singleton()->add_singleton(Engine::Singleton("NavigationMeshGenerator", EditorNavigationMeshGenerator::get_singleton()));
ClassDB::set_current_api(prev_api);
#endif
}
void unregister_recast_types() {
#ifdef TOOLS_ENABLED
if (_nav_mesh_generator) {
memdelete(_nav_mesh_generator);
}
#endif
}