2013-07-04 07:27:48 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-11-28 08:42:10 +00:00
|
|
|
// Copyright (c) 2014 Daniel Buckmaster
|
2013-07-04 07:27:48 +00:00
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include "navMesh.h"
|
2014-11-28 08:42:10 +00:00
|
|
|
#include "navContext.h"
|
2013-07-04 07:27:48 +00:00
|
|
|
#include <DetourDebugDraw.h>
|
|
|
|
|
#include <RecastDebugDraw.h>
|
|
|
|
|
|
|
|
|
|
#include "math/mathUtils.h"
|
|
|
|
|
#include "math/mRandom.h"
|
|
|
|
|
#include "console/consoleTypes.h"
|
|
|
|
|
#include "console/engineAPI.h"
|
|
|
|
|
#include "console/typeValidators.h"
|
|
|
|
|
|
|
|
|
|
#include "scene/sceneRenderState.h"
|
|
|
|
|
#include "gfx/gfxDrawUtil.h"
|
|
|
|
|
#include "renderInstance/renderPassManager.h"
|
|
|
|
|
#include "gfx/primBuilder.h"
|
|
|
|
|
|
|
|
|
|
#include "core/stream/bitStream.h"
|
|
|
|
|
#include "math/mathIO.h"
|
|
|
|
|
|
2021-01-11 09:51:21 +00:00
|
|
|
#include "core/stream/fileStream.h"
|
2025-07-23 20:02:44 +00:00
|
|
|
#include "T3D/assets/LevelAsset.h"
|
2014-11-28 09:24:11 +00:00
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
extern bool gEditingMission;
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_CO_NETOBJECT_V1(NavMesh);
|
|
|
|
|
|
2025-07-28 19:26:31 +00:00
|
|
|
const U32 NavMesh::mMaxVertsPerPoly = DT_VERTS_PER_POLYGON;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
SimObjectPtr<SimSet> NavMesh::smServerSet = NULL;
|
|
|
|
|
|
|
|
|
|
ImplementEnumType(NavMeshWaterMethod,
|
|
|
|
|
"The method used to include water surfaces in the NavMesh.\n")
|
|
|
|
|
{ NavMesh::Ignore, "Ignore", "Ignore all water surfaces.\n" },
|
|
|
|
|
{ NavMesh::Solid, "Solid", "Treat water surfaces as solid and walkable.\n" },
|
|
|
|
|
{ NavMesh::Impassable, "Impassable", "Treat water as an impassable obstacle.\n" },
|
|
|
|
|
EndImplementEnumType;
|
|
|
|
|
|
|
|
|
|
SimSet *NavMesh::getServerSet()
|
|
|
|
|
{
|
|
|
|
|
if(!smServerSet)
|
|
|
|
|
{
|
|
|
|
|
SimSet *set = NULL;
|
|
|
|
|
if(Sim::findObject("ServerNavMeshSet", set))
|
|
|
|
|
smServerSet = set;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
smServerSet = new SimSet();
|
|
|
|
|
smServerSet->registerObject("ServerNavMeshSet");
|
|
|
|
|
Sim::getRootGroup()->addObject(smServerSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return smServerSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimObjectPtr<EventManager> NavMesh::smEventManager = NULL;
|
|
|
|
|
|
|
|
|
|
EventManager *NavMesh::getEventManager()
|
|
|
|
|
{
|
|
|
|
|
if(!smEventManager)
|
|
|
|
|
{
|
|
|
|
|
smEventManager = new EventManager();
|
|
|
|
|
smEventManager->registerObject("NavEventManager");
|
|
|
|
|
Sim::getRootGroup()->addObject(smEventManager);
|
|
|
|
|
smEventManager->setMessageQueue("NavEventManagerQueue");
|
|
|
|
|
smEventManager->registerEvent("NavMeshCreated");
|
|
|
|
|
smEventManager->registerEvent("NavMeshRemoved");
|
|
|
|
|
smEventManager->registerEvent("NavMeshStartUpdate");
|
|
|
|
|
smEventManager->registerEvent("NavMeshUpdate");
|
|
|
|
|
smEventManager->registerEvent("NavMeshTileUpdate");
|
|
|
|
|
smEventManager->registerEvent("NavMeshUpdateBox");
|
|
|
|
|
smEventManager->registerEvent("NavMeshObstacleAdded");
|
|
|
|
|
smEventManager->registerEvent("NavMeshObstacleRemoved");
|
|
|
|
|
}
|
|
|
|
|
return smEventManager;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 14:33:56 +00:00
|
|
|
DefineEngineFunction(getNavMeshEventManager, S32, (),,
|
2014-11-28 08:42:10 +00:00
|
|
|
"@brief Get the EventManager object for all NavMesh updates.")
|
|
|
|
|
{
|
|
|
|
|
return NavMesh::getEventManager()->getId();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 14:33:56 +00:00
|
|
|
DefineEngineFunction(NavMeshUpdateAll, void, (S32 objid, bool remove), (0, false),
|
2014-11-28 08:42:10 +00:00
|
|
|
"@brief Update all NavMesh tiles that intersect the given object's world box.")
|
|
|
|
|
{
|
|
|
|
|
SceneObject *obj;
|
|
|
|
|
if(!Sim::findObject(objid, obj))
|
|
|
|
|
return;
|
2019-03-07 00:14:36 +00:00
|
|
|
obj->mPathfindingIgnore = remove;
|
2014-11-28 08:42:10 +00:00
|
|
|
SimSet *set = NavMesh::getServerSet();
|
|
|
|
|
for(U32 i = 0; i < set->size(); i++)
|
|
|
|
|
{
|
2015-12-04 00:34:53 +00:00
|
|
|
NavMesh *m = dynamic_cast<NavMesh*>(set->at(i));
|
2015-11-28 00:18:21 +00:00
|
|
|
if (m)
|
|
|
|
|
{
|
2015-11-27 23:43:08 +00:00
|
|
|
m->cancelBuild();
|
|
|
|
|
m->buildTiles(obj->getWorldBox());
|
|
|
|
|
}
|
2014-11-28 08:42:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 14:33:56 +00:00
|
|
|
DefineEngineFunction(NavMeshUpdateAroundObject, void, (S32 objid, bool remove), (0, false),
|
2016-02-19 23:34:27 +00:00
|
|
|
"@brief Update all NavMesh tiles that intersect the given object's world box.")
|
|
|
|
|
{
|
|
|
|
|
SceneObject *obj;
|
|
|
|
|
if (!Sim::findObject(objid, obj))
|
|
|
|
|
return;
|
2019-03-07 00:14:36 +00:00
|
|
|
obj->mPathfindingIgnore = remove;
|
2016-02-19 23:34:27 +00:00
|
|
|
SimSet *set = NavMesh::getServerSet();
|
|
|
|
|
for (U32 i = 0; i < set->size(); i++)
|
|
|
|
|
{
|
|
|
|
|
NavMesh *m = dynamic_cast<NavMesh*>(set->at(i));
|
|
|
|
|
if (m)
|
|
|
|
|
{
|
|
|
|
|
m->cancelBuild();
|
|
|
|
|
m->buildTiles(obj->getWorldBox());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 14:38:24 +00:00
|
|
|
|
2018-04-17 14:33:56 +00:00
|
|
|
DefineEngineFunction(NavMeshIgnore, void, (S32 objid, bool _ignore), (0, true),
|
2016-04-04 14:38:24 +00:00
|
|
|
"@brief Flag this object as not generating a navmesh result.")
|
|
|
|
|
{
|
|
|
|
|
SceneObject *obj;
|
|
|
|
|
if(!Sim::findObject(objid, obj))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
obj->mPathfindingIgnore = _ignore;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 14:33:56 +00:00
|
|
|
DefineEngineFunction(NavMeshUpdateOne, void, (S32 meshid, S32 objid, bool remove), (0, 0, false),
|
2014-11-28 08:42:10 +00:00
|
|
|
"@brief Update all tiles in a given NavMesh that intersect the given object's world box.")
|
|
|
|
|
{
|
|
|
|
|
NavMesh *mesh;
|
|
|
|
|
SceneObject *obj;
|
|
|
|
|
if(!Sim::findObject(meshid, mesh))
|
|
|
|
|
{
|
2015-01-29 10:09:32 +00:00
|
|
|
Con::errorf("NavMeshUpdateOne: cannot find NavMesh %d", meshid);
|
2014-11-28 08:42:10 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!Sim::findObject(objid, obj))
|
|
|
|
|
{
|
2015-01-29 10:09:32 +00:00
|
|
|
Con::errorf("NavMeshUpdateOne: cannot find SceneObject %d", objid);
|
2014-11-28 08:42:10 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(remove)
|
|
|
|
|
obj->disableCollision();
|
|
|
|
|
mesh->buildTiles(obj->getWorldBox());
|
|
|
|
|
if(remove)
|
|
|
|
|
obj->enableCollision();
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
NavMesh::NavMesh()
|
2025-07-22 13:39:36 +00:00
|
|
|
: m_triareas(0),
|
|
|
|
|
m_solid(0),
|
|
|
|
|
m_chf(0),
|
|
|
|
|
m_cset(0),
|
|
|
|
|
m_pmesh(0),
|
2025-07-24 13:25:02 +00:00
|
|
|
m_dmesh(0),
|
|
|
|
|
m_drawMode(DRAWMODE_NAVMESH)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
|
|
|
|
mTypeMask |= StaticShapeObjectType | MarkerObjectType;
|
2017-01-12 04:21:29 +00:00
|
|
|
mFileName = StringTable->EmptyString();
|
2013-07-04 07:27:48 +00:00
|
|
|
mNetFlags.clear(Ghostable);
|
|
|
|
|
|
2015-11-27 23:43:08 +00:00
|
|
|
mSaveIntermediates = false;
|
2013-07-04 07:27:48 +00:00
|
|
|
nm = NULL;
|
2014-11-28 08:42:10 +00:00
|
|
|
ctx = NULL;
|
2025-07-23 04:55:05 +00:00
|
|
|
m_geo = NULL;
|
2014-11-28 08:42:10 +00:00
|
|
|
|
|
|
|
|
mWaterMethod = Ignore;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2025-07-23 04:55:05 +00:00
|
|
|
mCellSize = mCellHeight = 0.2f;
|
2013-07-04 07:27:48 +00:00
|
|
|
mWalkableHeight = 2.0f;
|
|
|
|
|
mWalkableClimb = 0.3f;
|
|
|
|
|
mWalkableRadius = 0.5f;
|
|
|
|
|
mWalkableSlope = 40.0f;
|
|
|
|
|
mBorderSize = 1;
|
|
|
|
|
mDetailSampleDist = 6.0f;
|
|
|
|
|
mDetailSampleMaxError = 1.0f;
|
|
|
|
|
mMaxEdgeLen = 12;
|
|
|
|
|
mMaxSimplificationError = 1.3f;
|
|
|
|
|
mMinRegionArea = 8;
|
|
|
|
|
mMergeRegionArea = 20;
|
|
|
|
|
mTileSize = 10.0f;
|
|
|
|
|
mMaxPolysPerTile = 128;
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
mSmallCharacters = false;
|
|
|
|
|
mRegularCharacters = true;
|
|
|
|
|
mLargeCharacters = false;
|
|
|
|
|
mVehicles = false;
|
|
|
|
|
|
2017-01-12 04:21:29 +00:00
|
|
|
mCoverSet = StringTable->EmptyString();
|
2014-11-28 08:42:10 +00:00
|
|
|
mInnerCover = false;
|
|
|
|
|
mCoverDist = 1.0f;
|
|
|
|
|
mPeekDist = 0.7f;
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
mAlwaysRender = false;
|
|
|
|
|
|
|
|
|
|
mBuilding = false;
|
2020-05-11 20:08:57 +00:00
|
|
|
mCurLinkID = 0;
|
2025-07-25 11:41:59 +00:00
|
|
|
|
|
|
|
|
mWaterVertStart = 0;
|
|
|
|
|
mWaterTriStart = 0;
|
2025-07-26 09:34:19 +00:00
|
|
|
|
|
|
|
|
mQuery = NULL;
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NavMesh::~NavMesh()
|
|
|
|
|
{
|
2025-07-26 09:34:19 +00:00
|
|
|
dtFreeNavMeshQuery(mQuery);
|
2013-07-04 07:27:48 +00:00
|
|
|
dtFreeNavMesh(nm);
|
|
|
|
|
nm = NULL;
|
2014-11-28 08:42:10 +00:00
|
|
|
delete ctx;
|
|
|
|
|
ctx = NULL;
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NavMesh::setProtectedDetailSampleDist(void *obj, const char *index, const char *data)
|
|
|
|
|
{
|
|
|
|
|
F32 dist = dAtof(data);
|
|
|
|
|
if(dist == 0.0f || dist >= 0.9f)
|
|
|
|
|
return true;
|
2025-03-09 16:53:23 +00:00
|
|
|
if (dist > 0.0f && dist < 0.9f)
|
|
|
|
|
{
|
|
|
|
|
NavMesh* ptr = static_cast<NavMesh*>(obj);
|
|
|
|
|
ptr->mDetailSampleDist = 0.9f;
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
Con::errorf("NavMesh::detailSampleDist must be 0 or greater than 0.9!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NavMesh::setProtectedAlwaysRender(void *obj, const char *index, const char *data)
|
|
|
|
|
{
|
|
|
|
|
NavMesh *mesh = static_cast<NavMesh*>(obj);
|
|
|
|
|
bool always = dAtob(data);
|
|
|
|
|
if(always)
|
|
|
|
|
{
|
|
|
|
|
if(!gEditingMission)
|
|
|
|
|
mesh->mNetFlags.set(Ghostable);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(!gEditingMission)
|
|
|
|
|
mesh->mNetFlags.clear(Ghostable);
|
|
|
|
|
}
|
|
|
|
|
mesh->mAlwaysRender = always;
|
|
|
|
|
mesh->setMaskBits(LoadFlag);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FRangeValidator ValidCellSize(0.01f, 10.0f);
|
|
|
|
|
|
|
|
|
|
void NavMesh::initPersistFields()
|
|
|
|
|
{
|
2023-01-27 07:13:15 +00:00
|
|
|
docsURL;
|
2013-07-04 07:27:48 +00:00
|
|
|
addGroup("NavMesh Options");
|
|
|
|
|
|
|
|
|
|
addField("fileName", TypeString, Offset(mFileName, NavMesh),
|
|
|
|
|
"Name of the data file to store this navmesh in (relative to engine executable).");
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
addField("waterMethod", TYPEID<NavMeshWaterMethod>(), Offset(mWaterMethod, NavMesh),
|
|
|
|
|
"The method to use to handle water surfaces.");
|
|
|
|
|
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("cellSize", TypeRangedF32, Offset(mCellSize, NavMesh), &ValidCellSize,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Length/width of a voxel.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("cellHeight", TypeRangedF32, Offset(mCellHeight, NavMesh), &ValidCellSize,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Height of a voxel.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("tileSize", TypeRangedF32, Offset(mTileSize, NavMesh), &CommonValidators::PositiveNonZeroFloat,
|
2013-07-04 07:27:48 +00:00
|
|
|
"The horizontal size of tiles.");
|
|
|
|
|
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("actorHeight", TypeRangedF32, Offset(mWalkableHeight, NavMesh), &CommonValidators::PositiveFloat,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Height of an actor.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("actorClimb", TypeRangedF32, Offset(mWalkableClimb, NavMesh), &CommonValidators::PositiveFloat,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Maximum climbing height of an actor.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("actorRadius", TypeRangedF32, Offset(mWalkableRadius, NavMesh), &CommonValidators::PositiveFloat,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Radius of an actor.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("walkableSlope", TypeRangedF32, Offset(mWalkableSlope, NavMesh), &CommonValidators::ValidSlopeAngle,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Maximum walkable slope in degrees.");
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
addField("smallCharacters", TypeBool, Offset(mSmallCharacters, NavMesh),
|
|
|
|
|
"Is this NavMesh for smaller-than-usual characters?");
|
|
|
|
|
addField("regularCharacters", TypeBool, Offset(mRegularCharacters, NavMesh),
|
|
|
|
|
"Is this NavMesh for regular-sized characters?");
|
|
|
|
|
addField("largeCharacters", TypeBool, Offset(mLargeCharacters, NavMesh),
|
|
|
|
|
"Is this NavMesh for larger-than-usual characters?");
|
|
|
|
|
addField("vehicles", TypeBool, Offset(mVehicles, NavMesh),
|
|
|
|
|
"Is this NavMesh for characters driving vehicles?");
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
endGroup("NavMesh Options");
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
addGroup("NavMesh Annotations");
|
|
|
|
|
|
|
|
|
|
addField("coverGroup", TypeString, Offset(mCoverSet, NavMesh),
|
|
|
|
|
"Name of the SimGroup to store cover points in.");
|
|
|
|
|
|
|
|
|
|
addField("innerCover", TypeBool, Offset(mInnerCover, NavMesh),
|
|
|
|
|
"Add cover points everywhere, not just on corners?");
|
|
|
|
|
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("coverDist", TypeRangedF32, Offset(mCoverDist, NavMesh), &CommonValidators::PositiveFloat,
|
2014-11-28 08:42:10 +00:00
|
|
|
"Distance from the edge of the NavMesh to search for cover.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("peekDist", TypeRangedF32, Offset(mPeekDist, NavMesh), &CommonValidators::PositiveFloat,
|
2014-11-28 08:42:10 +00:00
|
|
|
"Distance to the side of each cover point that peeking happens.");
|
|
|
|
|
|
|
|
|
|
endGroup("NavMesh Annotations");
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
addGroup("NavMesh Rendering");
|
|
|
|
|
|
|
|
|
|
addProtectedField("alwaysRender", TypeBool, Offset(mAlwaysRender, NavMesh),
|
|
|
|
|
&setProtectedAlwaysRender, &defaultProtectedGetFn,
|
|
|
|
|
"Display this NavMesh even outside the editor.");
|
|
|
|
|
|
|
|
|
|
endGroup("NavMesh Rendering");
|
|
|
|
|
|
|
|
|
|
addGroup("NavMesh Advanced Options");
|
|
|
|
|
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("borderSize", TypeRangedS32, Offset(mBorderSize, NavMesh), &CommonValidators::PositiveInt,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Size of the non-walkable border around the navigation mesh (in voxels).");
|
2025-03-09 16:53:23 +00:00
|
|
|
addProtectedFieldV("detailSampleDist", TypeRangedF32, Offset(mDetailSampleDist, NavMesh),
|
|
|
|
|
&setProtectedDetailSampleDist, &defaultProtectedGetFn, &CommonValidators::PositiveFloat,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Sets the sampling distance to use when generating the detail mesh.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("detailSampleError", TypeRangedF32, Offset(mDetailSampleMaxError, NavMesh), &CommonValidators::PositiveFloat,
|
2013-07-04 07:27:48 +00:00
|
|
|
"The maximum distance the detail mesh surface should deviate from heightfield data.");
|
2025-07-23 14:08:29 +00:00
|
|
|
addFieldV("maxEdgeLen", TypeRangedS32, Offset(mMaxEdgeLen, NavMesh), &CommonValidators::PositiveInt,
|
2013-07-04 07:27:48 +00:00
|
|
|
"The maximum allowed length for contour edges along the border of the mesh.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("simplificationError", TypeRangedF32, Offset(mMaxSimplificationError, NavMesh), &CommonValidators::PositiveFloat,
|
2013-07-04 07:27:48 +00:00
|
|
|
"The maximum distance a simplfied contour's border edges should deviate from the original raw contour.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("minRegionArea", TypeRangedS32, Offset(mMinRegionArea, NavMesh), &CommonValidators::PositiveInt,
|
2013-07-04 07:27:48 +00:00
|
|
|
"The minimum number of cells allowed to form isolated island areas.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("mergeRegionArea", TypeRangedS32, Offset(mMergeRegionArea, NavMesh), &CommonValidators::PositiveInt,
|
2013-07-04 07:27:48 +00:00
|
|
|
"Any regions with a span count smaller than this value will, if possible, be merged with larger regions.");
|
2025-03-09 16:53:23 +00:00
|
|
|
addFieldV("maxPolysPerTile", TypeRangedS32, Offset(mMaxPolysPerTile, NavMesh), &CommonValidators::NaturalNumber,
|
2013-07-04 07:27:48 +00:00
|
|
|
"The maximum number of polygons allowed in a tile.");
|
|
|
|
|
|
|
|
|
|
endGroup("NavMesh Advanced Options");
|
|
|
|
|
|
|
|
|
|
Parent::initPersistFields();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NavMesh::onAdd()
|
|
|
|
|
{
|
|
|
|
|
if(!Parent::onAdd())
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-01-29 11:23:26 +00:00
|
|
|
mObjBox.set(Point3F(-0.5f, -0.5f, -0.5f),
|
|
|
|
|
Point3F( 0.5f, 0.5f, 0.5f));
|
2013-07-04 07:27:48 +00:00
|
|
|
resetWorldBox();
|
|
|
|
|
|
|
|
|
|
addToScene();
|
|
|
|
|
|
|
|
|
|
if(gEditingMission || mAlwaysRender)
|
|
|
|
|
{
|
|
|
|
|
mNetFlags.set(Ghostable);
|
2025-07-23 20:02:44 +00:00
|
|
|
if (isClientObject())
|
2013-07-04 07:27:48 +00:00
|
|
|
renderToDrawer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(isServerObject())
|
|
|
|
|
{
|
2014-11-28 08:42:10 +00:00
|
|
|
getServerSet()->addObject(this);
|
|
|
|
|
ctx = new NavContext();
|
2013-07-04 07:27:48 +00:00
|
|
|
setProcessTick(true);
|
2014-11-28 08:42:10 +00:00
|
|
|
if(getEventManager())
|
|
|
|
|
getEventManager()->postEvent("NavMeshCreated", getIdString());
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
load();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::onRemove()
|
|
|
|
|
{
|
2014-11-28 08:42:10 +00:00
|
|
|
if(getEventManager())
|
|
|
|
|
getEventManager()->postEvent("NavMeshRemoved", getIdString());
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
removeFromScene();
|
|
|
|
|
|
|
|
|
|
Parent::onRemove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::setTransform(const MatrixF &mat)
|
|
|
|
|
{
|
|
|
|
|
Parent::setTransform(mat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::setScale(const VectorF &scale)
|
|
|
|
|
{
|
|
|
|
|
Parent::setScale(scale);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 08:44:49 +00:00
|
|
|
S32 NavMesh::addLink(const Point3F &from, const Point3F &to, bool biDir, F32 rad, U32 flags)
|
2014-11-28 08:42:10 +00:00
|
|
|
{
|
|
|
|
|
Point3F rcFrom = DTStoRC(from), rcTo = DTStoRC(to);
|
|
|
|
|
mLinkVerts.push_back(rcFrom.x);
|
|
|
|
|
mLinkVerts.push_back(rcFrom.y);
|
|
|
|
|
mLinkVerts.push_back(rcFrom.z);
|
|
|
|
|
mLinkVerts.push_back(rcTo.x);
|
|
|
|
|
mLinkVerts.push_back(rcTo.y);
|
|
|
|
|
mLinkVerts.push_back(rcTo.z);
|
|
|
|
|
mLinksUnsynced.push_back(true);
|
2025-07-25 08:44:49 +00:00
|
|
|
mLinkRads.push_back(rad);
|
2025-07-24 22:43:35 +00:00
|
|
|
mLinkDirs.push_back(biDir ? 1 : 0);
|
2014-11-28 08:42:10 +00:00
|
|
|
mLinkAreas.push_back(OffMeshArea);
|
|
|
|
|
if (flags == 0) {
|
|
|
|
|
Point3F dir = to - from;
|
|
|
|
|
F32 drop = -dir.z;
|
|
|
|
|
dir.z = 0;
|
|
|
|
|
// If we drop more than we travel horizontally, we're a drop link.
|
|
|
|
|
if(drop > dir.len())
|
|
|
|
|
mLinkFlags.push_back(DropFlag);
|
|
|
|
|
else
|
|
|
|
|
mLinkFlags.push_back(JumpFlag);
|
|
|
|
|
}
|
|
|
|
|
mLinkIDs.push_back(1000 + mCurLinkID);
|
|
|
|
|
mLinkSelectStates.push_back(Unselected);
|
|
|
|
|
mDeleteLinks.push_back(false);
|
|
|
|
|
mCurLinkID++;
|
|
|
|
|
return mLinkIDs.size() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 22:43:35 +00:00
|
|
|
DefineEngineMethod(NavMesh, addLink, S32, (Point3F from, Point3F to, bool biDir, U32 flags), (0),
|
2014-11-28 08:42:10 +00:00
|
|
|
"Add a link to this NavMesh between two points.\n\n"
|
|
|
|
|
"")
|
|
|
|
|
{
|
2025-07-24 22:43:35 +00:00
|
|
|
return object->addLink(from, to, biDir, flags);
|
2014-11-28 08:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
S32 NavMesh::getLink(const Point3F &pos)
|
|
|
|
|
{
|
|
|
|
|
for(U32 i = 0; i < mLinkIDs.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
if(mDeleteLinks[i])
|
|
|
|
|
continue;
|
|
|
|
|
SphereF start(getLinkStart(i), mLinkRads[i]);
|
|
|
|
|
SphereF end(getLinkEnd(i), mLinkRads[i]);
|
|
|
|
|
if(start.isContained(pos) || end.isContained(pos))
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, getLink, S32, (Point3F pos),,
|
|
|
|
|
"Get the off-mesh link closest to a given world point.")
|
|
|
|
|
{
|
|
|
|
|
return object->getLink(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
S32 NavMesh::getLinkCount()
|
|
|
|
|
{
|
|
|
|
|
return mLinkIDs.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, getLinkCount, S32, (),,
|
|
|
|
|
"Return the number of links this mesh has.")
|
|
|
|
|
{
|
|
|
|
|
return object->getLinkCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinkData NavMesh::getLinkFlags(U32 idx)
|
|
|
|
|
{
|
|
|
|
|
if(idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
return LinkData(mLinkFlags[idx]);
|
|
|
|
|
}
|
|
|
|
|
return LinkData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 22:43:35 +00:00
|
|
|
bool NavMesh::getLinkDir(U32 idx)
|
|
|
|
|
{
|
|
|
|
|
if (idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
return mLinkDirs[idx];
|
|
|
|
|
}
|
2025-07-25 11:41:59 +00:00
|
|
|
|
|
|
|
|
return false;
|
2025-07-24 22:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 08:44:49 +00:00
|
|
|
F32 NavMesh::getLinkRadius(U32 idx)
|
|
|
|
|
{
|
|
|
|
|
if (idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
return mLinkRads[idx];
|
|
|
|
|
}
|
2025-07-25 11:41:59 +00:00
|
|
|
|
|
|
|
|
return -1.0f;
|
2025-07-25 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-24 22:43:35 +00:00
|
|
|
void NavMesh::setLinkDir(U32 idx, bool biDir)
|
|
|
|
|
{
|
|
|
|
|
if (idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
mLinkDirs[idx] = biDir ? 1 : 0;
|
|
|
|
|
mLinksUnsynced[idx] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 08:44:49 +00:00
|
|
|
void NavMesh::setLinkRadius(U32 idx, F32 rad)
|
|
|
|
|
{
|
|
|
|
|
if (idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
mLinkRads[idx] = rad;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
DefineEngineMethod(NavMesh, getLinkFlags, S32, (U32 id),,
|
|
|
|
|
"Get the flags set for a particular off-mesh link.")
|
|
|
|
|
{
|
|
|
|
|
return object->getLinkFlags(id).getFlags();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::setLinkFlags(U32 idx, const LinkData &d)
|
|
|
|
|
{
|
|
|
|
|
if(idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
mLinkFlags[idx] = d.getFlags();
|
|
|
|
|
mLinksUnsynced[idx] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, setLinkFlags, void, (U32 id, U32 flags),,
|
|
|
|
|
"Set the flags of a particular off-mesh link.")
|
|
|
|
|
{
|
|
|
|
|
LinkData d(flags);
|
|
|
|
|
object->setLinkFlags(id, d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Point3F NavMesh::getLinkStart(U32 idx)
|
|
|
|
|
{
|
|
|
|
|
return RCtoDTS(Point3F(
|
|
|
|
|
mLinkVerts[idx*6],
|
|
|
|
|
mLinkVerts[idx*6 + 1],
|
|
|
|
|
mLinkVerts[idx*6 + 2]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, getLinkStart, Point3F, (U32 id),,
|
|
|
|
|
"Get the starting point of an off-mesh link.")
|
|
|
|
|
{
|
|
|
|
|
return object->getLinkStart(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Point3F NavMesh::getLinkEnd(U32 idx)
|
|
|
|
|
{
|
|
|
|
|
return RCtoDTS(Point3F(
|
|
|
|
|
mLinkVerts[idx*6 + 3],
|
|
|
|
|
mLinkVerts[idx*6 + 4],
|
|
|
|
|
mLinkVerts[idx*6 + 5]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, getLinkEnd, Point3F, (U32 id),,
|
|
|
|
|
"Get the ending point of an off-mesh link.")
|
|
|
|
|
{
|
|
|
|
|
return object->getLinkEnd(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::selectLink(U32 idx, bool select, bool hover)
|
|
|
|
|
{
|
|
|
|
|
if(idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
if(!select)
|
|
|
|
|
mLinkSelectStates[idx] = Unselected;
|
|
|
|
|
else
|
|
|
|
|
mLinkSelectStates[idx] = hover ? Hovered : Selected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::eraseLink(U32 i)
|
|
|
|
|
{
|
|
|
|
|
mLinkVerts.erase(i*6, 6);
|
|
|
|
|
mLinksUnsynced.erase(i);
|
|
|
|
|
mLinkRads.erase(i);
|
|
|
|
|
mLinkDirs.erase(i);
|
|
|
|
|
mLinkAreas.erase(i);
|
|
|
|
|
mLinkFlags.erase(i);
|
|
|
|
|
mLinkIDs.erase(i);
|
|
|
|
|
mLinkSelectStates.erase(i);
|
|
|
|
|
mDeleteLinks.erase(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::eraseLinks()
|
|
|
|
|
{
|
|
|
|
|
mLinkVerts.clear();
|
|
|
|
|
mLinksUnsynced.clear();
|
|
|
|
|
mLinkRads.clear();
|
|
|
|
|
mLinkDirs.clear();
|
|
|
|
|
mLinkAreas.clear();
|
|
|
|
|
mLinkFlags.clear();
|
|
|
|
|
mLinkIDs.clear();
|
|
|
|
|
mLinkSelectStates.clear();
|
|
|
|
|
mDeleteLinks.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::setLinkCount(U32 c)
|
|
|
|
|
{
|
|
|
|
|
eraseLinks();
|
|
|
|
|
mLinkVerts.setSize(c * 6);
|
|
|
|
|
mLinksUnsynced.setSize(c);
|
|
|
|
|
mLinkRads.setSize(c);
|
|
|
|
|
mLinkDirs.setSize(c);
|
|
|
|
|
mLinkAreas.setSize(c);
|
|
|
|
|
mLinkFlags.setSize(c);
|
|
|
|
|
mLinkIDs.setSize(c);
|
|
|
|
|
mLinkSelectStates.setSize(c);
|
|
|
|
|
mDeleteLinks.setSize(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::deleteLink(U32 idx)
|
|
|
|
|
{
|
|
|
|
|
if(idx < mLinkIDs.size())
|
|
|
|
|
{
|
|
|
|
|
mDeleteLinks[idx] = true;
|
|
|
|
|
if(mLinksUnsynced[idx])
|
|
|
|
|
eraseLink(idx);
|
|
|
|
|
else
|
|
|
|
|
mLinksUnsynced[idx] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, deleteLink, void, (U32 id),,
|
|
|
|
|
"Delete a given off-mesh link.")
|
|
|
|
|
{
|
|
|
|
|
object->deleteLink(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, deleteLinks, void, (),,
|
|
|
|
|
"Deletes all off-mesh links on this NavMesh.")
|
|
|
|
|
{
|
|
|
|
|
//object->eraseLinks();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
static void buildCallback(SceneObject* object, void* key)
|
|
|
|
|
{
|
|
|
|
|
SceneContainer::CallbackInfo* info = reinterpret_cast<SceneContainer::CallbackInfo*>(key);
|
|
|
|
|
if (!object->mPathfindingIgnore)
|
|
|
|
|
object->buildPolyList(info->context, info->polyList, info->boundingBox, info->boundingSphere);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
bool NavMesh::build(bool background, bool saveIntermediates)
|
|
|
|
|
{
|
|
|
|
|
if(mBuilding)
|
|
|
|
|
cancelBuild();
|
2014-11-28 08:42:10 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(getEventManager())
|
|
|
|
|
getEventManager()->postEvent("NavMeshStartUpdate", getIdString());
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
mBuilding = true;
|
2025-07-27 11:43:19 +00:00
|
|
|
m_geo = NULL;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
ctx->startTimer(RC_TIMER_TOTAL);
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
dtFreeNavMesh(nm);
|
|
|
|
|
// Allocate a new navmesh.
|
|
|
|
|
nm = dtAllocNavMesh();
|
|
|
|
|
if(!nm)
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("Could not allocate dtNavMesh for NavMesh %s", getIdString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
// Needed for the recast config and generation params.
|
|
|
|
|
Box3F rc_box = DTStoRC(getWorldBox());
|
|
|
|
|
S32 gw = 0, gh = 0;
|
|
|
|
|
rcCalcGridSize(rc_box.minExtents, rc_box.maxExtents, mCellSize, &gw, &gh);
|
2025-07-23 04:55:05 +00:00
|
|
|
const S32 ts = (S32)(mTileSize / mCellSize);
|
2025-07-22 13:39:36 +00:00
|
|
|
const S32 tw = (gw + ts - 1) / ts;
|
|
|
|
|
const S32 th = (gh + ts - 1) / ts;
|
|
|
|
|
Con::printf("NavMesh::Build - Tiles %d x %d", tw, th);
|
|
|
|
|
|
2025-07-23 04:55:05 +00:00
|
|
|
U32 tileBits = mMin(getBinLog2(getNextPow2(tw * th)), 14);
|
2025-07-22 13:39:36 +00:00
|
|
|
if (tileBits > 14) tileBits = 14;
|
|
|
|
|
U32 maxTiles = 1 << tileBits;
|
|
|
|
|
U32 polyBits = 22 - tileBits;
|
|
|
|
|
mMaxPolysPerTile = 1 << polyBits;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
// Build navmesh parameters from console members.
|
|
|
|
|
dtNavMeshParams params;
|
2025-07-22 13:39:36 +00:00
|
|
|
rcVcopy(params.orig, rc_box.minExtents);
|
2025-07-23 04:55:05 +00:00
|
|
|
params.tileWidth = mTileSize;
|
|
|
|
|
params.tileHeight = mTileSize;
|
2025-07-22 13:39:36 +00:00
|
|
|
params.maxTiles = maxTiles;
|
2013-07-04 07:27:48 +00:00
|
|
|
params.maxPolys = mMaxPolysPerTile;
|
|
|
|
|
|
|
|
|
|
// Initialise our navmesh.
|
|
|
|
|
if(dtStatusFailed(nm->init(¶ms)))
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("Could not init dtNavMesh for NavMesh %s", getIdString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
// Update links to be deleted.
|
|
|
|
|
for(U32 i = 0; i < mLinkIDs.size();)
|
|
|
|
|
{
|
|
|
|
|
if(mDeleteLinks[i])
|
|
|
|
|
eraseLink(i);
|
|
|
|
|
else
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
mLinksUnsynced.fill(false);
|
|
|
|
|
mCurLinkID = 0;
|
|
|
|
|
|
|
|
|
|
mSaveIntermediates = saveIntermediates;
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
updateTiles(true);
|
|
|
|
|
|
|
|
|
|
if(!background)
|
|
|
|
|
{
|
2015-02-19 01:34:10 +00:00
|
|
|
while(!mDirtyTiles.empty())
|
2013-07-04 07:27:48 +00:00
|
|
|
buildNextTile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, build, bool, (bool background, bool save), (true, false),
|
|
|
|
|
"@brief Create a Recast nav mesh.")
|
|
|
|
|
{
|
|
|
|
|
return object->build(background, save);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::cancelBuild()
|
|
|
|
|
{
|
2015-11-27 22:17:30 +00:00
|
|
|
mDirtyTiles.clear();
|
2014-11-28 08:42:10 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_TOTAL);
|
2013-07-04 07:27:48 +00:00
|
|
|
mBuilding = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, cancelBuild, void, (),,
|
|
|
|
|
"@brief Cancel the current NavMesh build.")
|
|
|
|
|
{
|
|
|
|
|
object->cancelBuild();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::inspectPostApply()
|
|
|
|
|
{
|
|
|
|
|
if(mBuilding)
|
|
|
|
|
cancelBuild();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 20:02:44 +00:00
|
|
|
void NavMesh::createNewFile()
|
|
|
|
|
{
|
|
|
|
|
// We need to construct a default file name
|
|
|
|
|
String levelAssetId(Con::getVariable("$Client::LevelAsset"));
|
|
|
|
|
|
|
|
|
|
LevelAsset* levelAsset;
|
|
|
|
|
if (!Sim::findObject(levelAssetId.c_str(), levelAsset))
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("NavMesh::createNewFile() - Unable to find current level's LevelAsset. Unable to construct NavMesh filePath");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Torque::Path basePath(levelAsset->getNavmeshPath());
|
|
|
|
|
|
|
|
|
|
if (basePath.isEmpty())
|
|
|
|
|
basePath = (Torque::Path)(levelAsset->getLevelPath());
|
|
|
|
|
|
|
|
|
|
String fileName = Torque::FS::MakeUniquePath(basePath.getPath(), basePath.getFileName(), "nav");
|
|
|
|
|
mFileName = StringTable->insert(fileName.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 03:02:18 +00:00
|
|
|
S32 NavMesh::getTile(const Point3F& pos)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
|
|
|
|
if(mBuilding)
|
|
|
|
|
return -1;
|
|
|
|
|
for(U32 i = 0; i < mTiles.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
if(mTiles[i].box.isContained(pos))
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Box3F NavMesh::getTileBox(U32 id)
|
|
|
|
|
{
|
|
|
|
|
if(mBuilding || id >= mTiles.size())
|
|
|
|
|
return Box3F::Invalid;
|
|
|
|
|
return mTiles[id].box;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::updateTiles(bool dirty)
|
|
|
|
|
{
|
2017-04-05 18:04:25 +00:00
|
|
|
PROFILE_SCOPE(NavMesh_updateTiles);
|
2013-07-04 07:27:48 +00:00
|
|
|
if(!isProperlyAdded())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
mTiles.clear();
|
2015-11-27 22:17:30 +00:00
|
|
|
mDirtyTiles.clear();
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
const Box3F &box = DTStoRC(getWorldBox());
|
|
|
|
|
if(box.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Calculate tile dimensions.
|
2025-07-22 13:39:36 +00:00
|
|
|
const F32* bmin = box.minExtents;
|
|
|
|
|
const F32* bmax = box.maxExtents;
|
|
|
|
|
S32 gw = 0, gh = 0;
|
|
|
|
|
rcCalcGridSize(bmin, bmax, mCellSize, &gw, &gh);
|
2025-07-23 20:02:44 +00:00
|
|
|
const S32 ts = (S32)(mTileSize / mCellSize);
|
2025-07-22 13:39:36 +00:00
|
|
|
const S32 tw = (gw + ts - 1) / ts;
|
|
|
|
|
const S32 th = (gh + ts - 1) / ts;
|
2025-07-23 04:55:05 +00:00
|
|
|
const F32 tcs = mTileSize;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
// Iterate over tiles.
|
|
|
|
|
F32 tileBmin[3], tileBmax[3];
|
|
|
|
|
for(U32 y = 0; y < th; ++y)
|
|
|
|
|
{
|
|
|
|
|
for(U32 x = 0; x < tw; ++x)
|
|
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
tileBmin[0] = bmin[0] + x*tcs;
|
|
|
|
|
tileBmin[1] = bmin[1];
|
|
|
|
|
tileBmin[2] = bmin[2] + y*tcs;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
tileBmax[0] = bmin[0] + (x+1)*tcs;
|
|
|
|
|
tileBmax[1] = bmax[1];
|
|
|
|
|
tileBmax[2] = bmin[2] + (y+1)*tcs;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
mTiles.push_back(
|
|
|
|
|
Tile(RCtoDTS(tileBmin, tileBmax),
|
|
|
|
|
x, y,
|
|
|
|
|
tileBmin, tileBmax));
|
|
|
|
|
|
|
|
|
|
if(dirty)
|
2015-11-27 22:17:30 +00:00
|
|
|
mDirtyTiles.push_back_unique(mTiles.size() - 1);
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::processTick(const Move *move)
|
|
|
|
|
{
|
|
|
|
|
buildNextTile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::buildNextTile()
|
|
|
|
|
{
|
2017-04-05 18:04:25 +00:00
|
|
|
PROFILE_SCOPE(NavMesh_buildNextTile);
|
2025-07-23 20:02:44 +00:00
|
|
|
|
2015-02-19 01:34:10 +00:00
|
|
|
if(!mDirtyTiles.empty())
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-26 09:34:19 +00:00
|
|
|
dtFreeNavMeshQuery(mQuery);
|
|
|
|
|
mQuery = NULL;
|
2013-07-04 07:27:48 +00:00
|
|
|
// Pop a single dirty tile and process it.
|
|
|
|
|
U32 i = mDirtyTiles.front();
|
2015-11-27 22:17:30 +00:00
|
|
|
mDirtyTiles.pop_front();
|
2025-07-23 20:02:44 +00:00
|
|
|
Tile &tile = mTiles[i];
|
2025-07-23 04:55:05 +00:00
|
|
|
|
2015-11-27 23:43:08 +00:00
|
|
|
// Remove any previous data.
|
|
|
|
|
nm->removeTile(nm->getTileRefAt(tile.x, tile.y, 0), 0, 0);
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
// Generate navmesh for this tile.
|
|
|
|
|
U32 dataSize = 0;
|
2025-07-23 04:55:05 +00:00
|
|
|
unsigned char* data = buildTileData(tile, dataSize);
|
2025-07-23 20:02:44 +00:00
|
|
|
// cache our result (these only exist if keep intermediates is ticked)
|
|
|
|
|
if (m_chf)
|
|
|
|
|
{
|
|
|
|
|
tile.chf = m_chf;
|
|
|
|
|
m_chf = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_solid)
|
|
|
|
|
{
|
|
|
|
|
tile.solid = m_solid;
|
|
|
|
|
m_solid = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_cset)
|
|
|
|
|
{
|
|
|
|
|
tile.cset = m_cset;
|
|
|
|
|
m_cset = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_pmesh)
|
|
|
|
|
{
|
|
|
|
|
tile.pmesh = m_pmesh;
|
|
|
|
|
m_pmesh = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_dmesh)
|
|
|
|
|
{
|
|
|
|
|
tile.dmesh = m_dmesh;
|
|
|
|
|
m_dmesh = 0;
|
|
|
|
|
}
|
2025-07-28 19:26:31 +00:00
|
|
|
if (m_triareas)
|
|
|
|
|
{
|
|
|
|
|
tile.triareas = m_triareas;
|
|
|
|
|
m_triareas = nullptr;
|
|
|
|
|
}
|
2025-07-23 20:02:44 +00:00
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
if(data)
|
|
|
|
|
{
|
|
|
|
|
// Add new data (navmesh owns and deletes the data).
|
|
|
|
|
dtStatus status = nm->addTile(data, dataSize, DT_TILE_FREE_DATA, 0, 0);
|
2014-11-28 08:42:10 +00:00
|
|
|
int success = 1;
|
2013-07-04 07:27:48 +00:00
|
|
|
if(dtStatusFailed(status))
|
|
|
|
|
{
|
2014-11-28 08:42:10 +00:00
|
|
|
success = 0;
|
2013-07-04 07:27:48 +00:00
|
|
|
dtFree(data);
|
|
|
|
|
}
|
2014-11-28 08:42:10 +00:00
|
|
|
if(getEventManager())
|
|
|
|
|
{
|
|
|
|
|
String str = String::ToString("%d %d %d (%d, %d) %d %.3f %s",
|
|
|
|
|
getId(),
|
|
|
|
|
i, mTiles.size(),
|
|
|
|
|
tile.x, tile.y,
|
|
|
|
|
success,
|
|
|
|
|
ctx->getAccumulatedTime(RC_TIMER_TOTAL) / 1000.0f,
|
|
|
|
|
castConsoleTypeToString(tile.box));
|
|
|
|
|
getEventManager()->postEvent("NavMeshTileUpdate", str.c_str());
|
|
|
|
|
setMaskBits(LoadFlag);
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
// Did we just build the last tile?
|
2015-02-19 01:34:10 +00:00
|
|
|
if(mDirtyTiles.empty())
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-26 09:34:19 +00:00
|
|
|
mQuery = dtAllocNavMeshQuery();
|
|
|
|
|
if (dtStatusFailed(mQuery->init(nm, 2048)))
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("NavMesh - Failed to create navmesh query");
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_TOTAL);
|
|
|
|
|
if(getEventManager())
|
|
|
|
|
{
|
2016-11-24 18:09:00 +00:00
|
|
|
String str = String::ToString("%d", getId());
|
2014-11-28 08:42:10 +00:00
|
|
|
getEventManager()->postEvent("NavMeshUpdate", str.c_str());
|
|
|
|
|
setMaskBits(LoadFlag);
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
mBuilding = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-26 09:34:19 +00:00
|
|
|
|
|
|
|
|
if (mQuery == NULL)
|
|
|
|
|
{
|
|
|
|
|
mQuery = dtAllocNavMeshQuery();
|
|
|
|
|
if (dtStatusFailed(mQuery->init(nm, 2048)))
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("NavMesh - Failed to create navmesh query");
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-23 04:55:05 +00:00
|
|
|
unsigned char *NavMesh::buildTileData(const Tile &tile, U32 &dataSize)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
// Push out tile boundaries a bit.
|
|
|
|
|
F32 tileBmin[3], tileBmax[3];
|
|
|
|
|
rcVcopy(tileBmin, tile.bmin);
|
|
|
|
|
rcVcopy(tileBmax, tile.bmax);
|
2025-07-22 13:39:36 +00:00
|
|
|
// Setup our rcConfig
|
|
|
|
|
dMemset(&m_cfg, 0, sizeof(m_cfg));
|
|
|
|
|
m_cfg.cs = mCellSize;
|
|
|
|
|
m_cfg.ch = mCellHeight;
|
|
|
|
|
m_cfg.walkableSlopeAngle = mWalkableSlope;
|
|
|
|
|
m_cfg.walkableHeight = (S32)mCeil(mWalkableHeight / m_cfg.ch);
|
|
|
|
|
m_cfg.walkableClimb = (S32)mFloor(mWalkableClimb / m_cfg.ch);
|
|
|
|
|
m_cfg.walkableRadius = (S32)mCeil(mWalkableRadius / m_cfg.cs);
|
|
|
|
|
m_cfg.maxEdgeLen = (S32)(mMaxEdgeLen / mCellSize);
|
|
|
|
|
m_cfg.maxSimplificationError = mMaxSimplificationError;
|
|
|
|
|
m_cfg.minRegionArea = (S32)mSquared((F32)mMinRegionArea);
|
|
|
|
|
m_cfg.mergeRegionArea = (S32)mSquared((F32)mMergeRegionArea);
|
|
|
|
|
m_cfg.maxVertsPerPoly = (S32)mMaxVertsPerPoly;
|
2025-07-23 04:55:05 +00:00
|
|
|
m_cfg.tileSize = (S32)(mTileSize / mCellSize);
|
2025-07-22 13:39:36 +00:00
|
|
|
m_cfg.borderSize = mMax(m_cfg.walkableRadius + 3, mBorderSize); // use the border size if it is bigger.
|
|
|
|
|
m_cfg.width = m_cfg.tileSize + m_cfg.borderSize * 2;
|
|
|
|
|
m_cfg.height = m_cfg.tileSize + m_cfg.borderSize * 2;
|
|
|
|
|
m_cfg.detailSampleDist = mDetailSampleDist < 0.9f ? 0 : mCellSize * mDetailSampleDist;
|
|
|
|
|
m_cfg.detailSampleMaxError = mCellHeight * mDetailSampleMaxError;
|
|
|
|
|
rcVcopy(m_cfg.bmin, tileBmin);
|
|
|
|
|
rcVcopy(m_cfg.bmax, tileBmax);
|
|
|
|
|
m_cfg.bmin[0] -= m_cfg.borderSize * m_cfg.cs;
|
|
|
|
|
m_cfg.bmin[2] -= m_cfg.borderSize * m_cfg.cs;
|
|
|
|
|
m_cfg.bmax[0] += m_cfg.borderSize * m_cfg.cs;
|
|
|
|
|
m_cfg.bmax[2] += m_cfg.borderSize * m_cfg.cs;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2025-07-28 19:26:31 +00:00
|
|
|
Box3F worldBox = RCtoDTS(m_cfg.bmin, m_cfg.bmax);
|
|
|
|
|
SceneContainer::CallbackInfo info;
|
|
|
|
|
info.context = PLC_Navigation;
|
|
|
|
|
info.boundingBox = worldBox;
|
|
|
|
|
m_geo = new RecastPolyList;
|
|
|
|
|
info.polyList = m_geo;
|
|
|
|
|
info.key = this;
|
|
|
|
|
getContainer()->findObjects(worldBox, StaticObjectType | DynamicShapeObjectType, buildCallback, &info);
|
|
|
|
|
|
|
|
|
|
// Parse water objects into the same list, but remember how much geometry was /not/ water.
|
|
|
|
|
mWaterVertStart = m_geo->getVertCount();
|
|
|
|
|
mWaterTriStart = m_geo->getTriCount();
|
|
|
|
|
if (mWaterMethod != Ignore)
|
|
|
|
|
{
|
|
|
|
|
getContainer()->findObjects(worldBox, WaterObjectType, buildCallback, &info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for no geometry.
|
|
|
|
|
if (!m_geo->getVertCount())
|
|
|
|
|
{
|
|
|
|
|
m_geo->clear();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rcChunkyTriMesh* chunkyMesh = m_geo->getChunkyMesh();
|
|
|
|
|
|
|
|
|
|
// Create a heightfield to voxelize our input geometry.
|
2025-07-22 13:39:36 +00:00
|
|
|
m_solid = rcAllocHeightfield();
|
|
|
|
|
if(!m_solid)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
|
|
|
|
Con::errorf("Out of memory (rcHeightField) for NavMesh %s", getIdString());
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
if (!rcCreateHeightfield(ctx, *m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
|
|
|
|
Con::errorf("Could not generate rcHeightField for NavMesh %s", getIdString());
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
m_triareas = new unsigned char[chunkyMesh->maxTrisPerChunk];
|
|
|
|
|
if (!m_triareas)
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("NavMesh::buildTileData: Out of memory 'm_triareas' (%d).", chunkyMesh->maxTrisPerChunk);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
F32 tbmin[2], tbmax[2];
|
|
|
|
|
tbmin[0] = m_cfg.bmin[0];
|
|
|
|
|
tbmin[1] = m_cfg.bmin[2];
|
|
|
|
|
tbmax[0] = m_cfg.bmax[0];
|
|
|
|
|
tbmax[1] = m_cfg.bmax[2];
|
|
|
|
|
int cid[512];
|
|
|
|
|
const int ncid = rcGetChunksOverlappingRect(chunkyMesh, tbmin, tbmax, cid, 512);
|
|
|
|
|
if (!ncid)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ncid; ++i)
|
2014-11-28 08:42:10 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
const rcChunkyTriMeshNode& node = chunkyMesh->nodes[cid[i]];
|
|
|
|
|
const int* ctris = &chunkyMesh->tris[node.i * 3];
|
|
|
|
|
const int nctris = node.n;
|
|
|
|
|
|
|
|
|
|
memset(m_triareas, 0, nctris * sizeof(unsigned char));
|
|
|
|
|
rcMarkWalkableTriangles(ctx, m_cfg.walkableSlopeAngle,
|
|
|
|
|
m_geo->getVerts(), m_geo->getVertCount(), ctris, nctris, m_triareas);
|
|
|
|
|
|
2025-07-25 11:41:59 +00:00
|
|
|
// Post-process triangle areas if we need to capture water.
|
|
|
|
|
if (mWaterMethod != Ignore)
|
|
|
|
|
{
|
|
|
|
|
for (int t = 0; t < nctris; ++t)
|
|
|
|
|
{
|
|
|
|
|
const int* tri = &ctris[t * 3];
|
|
|
|
|
|
|
|
|
|
bool isWater = false;
|
|
|
|
|
for (int j = 0; j < 3; ++j)
|
|
|
|
|
{
|
|
|
|
|
if (tri[j] >= mWaterVertStart)
|
|
|
|
|
{
|
|
|
|
|
isWater = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isWater)
|
|
|
|
|
{
|
|
|
|
|
if (mWaterMethod == Solid)
|
|
|
|
|
{
|
|
|
|
|
m_triareas[t] = WaterArea; // Custom enum you define, like 64
|
|
|
|
|
}
|
|
|
|
|
else if (mWaterMethod == Impassable)
|
|
|
|
|
{
|
2025-07-27 11:43:19 +00:00
|
|
|
m_triareas[t] = NullArea;
|
2025-07-25 11:41:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
if (!rcRasterizeTriangles(ctx, m_geo->getVerts(), m_geo->getVertCount(), ctris, m_triareas, nctris, *m_solid, m_cfg.walkableClimb))
|
|
|
|
|
return NULL;
|
2014-11-28 08:42:10 +00:00
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
|
|
|
|
|
if (!mSaveIntermediates)
|
2014-11-28 08:42:10 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
delete[] m_triareas;
|
|
|
|
|
m_triareas = 0;
|
2014-11-28 08:42:10 +00:00
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
// these should be optional.
|
|
|
|
|
//if (m_filterLowHangingObstacles)
|
|
|
|
|
rcFilterLowHangingWalkableObstacles(ctx, m_cfg.walkableClimb, *m_solid);
|
|
|
|
|
//if (m_filterLedgeSpans)
|
|
|
|
|
rcFilterLedgeSpans(ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid);
|
|
|
|
|
//if (m_filterWalkableLowHeightSpans)
|
|
|
|
|
rcFilterWalkableLowHeightSpans(ctx, m_cfg.walkableHeight, *m_solid);
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
// Compact the heightfield so that it is faster to handle from now on.
|
|
|
|
|
// This will result more cache coherent data as well as the neighbours
|
|
|
|
|
// between walkable cells will be calculated.
|
|
|
|
|
m_chf = rcAllocCompactHeightfield();
|
|
|
|
|
if (!m_chf)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Out of memory 'chf'.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
if (!rcBuildCompactHeightfield(ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid, *m_chf))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could not build compact data.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
|
|
|
|
|
if (!mSaveIntermediates)
|
|
|
|
|
{
|
|
|
|
|
rcFreeHeightField(m_solid);
|
|
|
|
|
m_solid = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Erode the walkable area by agent radius.
|
|
|
|
|
if (!rcErodeWalkableArea(ctx, m_cfg.walkableRadius, *m_chf))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could not erode.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------
|
|
|
|
|
// Todo: mark areas here.
|
|
|
|
|
//const ConvexVolume* vols = m_geom->getConvexVolumes();
|
|
|
|
|
//for (int i = 0; i < m_geom->getConvexVolumeCount(); ++i)
|
2014-11-28 08:42:10 +00:00
|
|
|
//rcMarkConvexPolyArea(m_ctx, vols[i].verts, vols[i].nverts, vols[i].hmin, vols[i].hmax, (unsigned char)vols[i].area, *m_chf);
|
2013-07-04 07:27:48 +00:00
|
|
|
//--------------------------
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
// Partition the heightfield so that we can use simple algorithm later to triangulate the walkable areas.
|
|
|
|
|
// There are 3 martitioning methods, each with some pros and cons:
|
|
|
|
|
// These should be implemented.
|
|
|
|
|
// 1) Watershed partitioning
|
|
|
|
|
// - the classic Recast partitioning
|
|
|
|
|
// - creates the nicest tessellation
|
|
|
|
|
// - usually slowest
|
|
|
|
|
// - partitions the heightfield into nice regions without holes or overlaps
|
|
|
|
|
// - the are some corner cases where this method creates produces holes and overlaps
|
|
|
|
|
// - holes may appear when a small obstacles is close to large open area (triangulation can handle this)
|
|
|
|
|
// - overlaps may occur if you have narrow spiral corridors (i.e stairs), this make triangulation to fail
|
|
|
|
|
// * generally the best choice if you precompute the nacmesh, use this if you have large open areas
|
|
|
|
|
// 2) Monotone partioning
|
|
|
|
|
// - fastest
|
|
|
|
|
// - partitions the heightfield into regions without holes and overlaps (guaranteed)
|
|
|
|
|
// - creates long thin polygons, which sometimes causes paths with detours
|
|
|
|
|
// * use this if you want fast navmesh generation
|
|
|
|
|
// 3) Layer partitoining
|
|
|
|
|
// - quite fast
|
|
|
|
|
// - partitions the heighfield into non-overlapping regions
|
|
|
|
|
// - relies on the triangulation code to cope with holes (thus slower than monotone partitioning)
|
|
|
|
|
// - produces better triangles than monotone partitioning
|
|
|
|
|
// - does not have the corner cases of watershed partitioning
|
|
|
|
|
// - can be slow and create a bit ugly tessellation (still better than monotone)
|
|
|
|
|
// if you have large open areas with small obstacles (not a problem if you use tiles)
|
|
|
|
|
// * good choice to use for tiled navmesh with medium and small sized tiles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (/*m_partitionType == SAMPLE_PARTITION_WATERSHED*/ true)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
// Prepare for region partitioning, by calculating distance field along the walkable surface.
|
|
|
|
|
if (!rcBuildDistanceField(ctx, *m_chf))
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("NavMesh::buildTileData: Could not build distance field.");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Partition the walkable surface into simple regions without holes.
|
|
|
|
|
if (!rcBuildRegions(ctx, *m_chf, m_cfg.borderSize, m_cfg.minRegionArea, m_cfg.mergeRegionArea))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could not build watershed regions.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
else if (/*m_partitionType == SAMPLE_PARTITION_MONOTONE*/ false)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
// Partition the walkable surface into simple regions without holes.
|
|
|
|
|
// Monotone partitioning does not need distancefield.
|
|
|
|
|
if (!rcBuildRegionsMonotone(ctx, *m_chf, m_cfg.borderSize, m_cfg.minRegionArea, m_cfg.mergeRegionArea))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could not build monotone regions.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
}
|
|
|
|
|
else // SAMPLE_PARTITION_LAYERS
|
|
|
|
|
{
|
|
|
|
|
// Partition the walkable surface into simple regions without holes.
|
|
|
|
|
if (!rcBuildLayerRegions(ctx, *m_chf, m_cfg.borderSize, m_cfg.minRegionArea))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could not build layer regions.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
m_cset = rcAllocContourSet();
|
|
|
|
|
if (!m_cset)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Out of memory 'cset'");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
|
|
|
|
|
if (!rcBuildContours(ctx, *m_chf, m_cfg.maxSimplificationError, m_cfg.maxEdgeLen, *m_cset))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could not create contours");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
|
|
|
|
|
if (m_cset->nconts == 0)
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
// Build polygon navmesh from the contours.
|
|
|
|
|
m_pmesh = rcAllocPolyMesh();
|
|
|
|
|
if (!m_pmesh)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Out of memory 'pmesh'.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-07-22 13:39:36 +00:00
|
|
|
if (!rcBuildPolyMesh(ctx, *m_cset, m_cfg.maxVertsPerPoly, *m_pmesh))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could not triangulate contours.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
// Build detail mesh.
|
|
|
|
|
m_dmesh = rcAllocPolyMeshDetail();
|
|
|
|
|
if (!m_dmesh)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Out of memory 'dmesh'.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
if (!rcBuildPolyMeshDetail(ctx, *m_pmesh, *m_chf, m_cfg.detailSampleDist, m_cfg.detailSampleMaxError, *m_dmesh))
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
Con::errorf("NavMesh::buildTileData: Could build polymesh detail.");
|
2013-07-04 07:27:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
if (!mSaveIntermediates)
|
|
|
|
|
{
|
|
|
|
|
rcFreeCompactHeightfield(m_chf);
|
|
|
|
|
m_chf = 0;
|
|
|
|
|
rcFreeContourSet(m_cset);
|
|
|
|
|
m_cset = 0;
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned char* navData = 0;
|
|
|
|
|
int navDataSize = 0;
|
2025-07-22 13:39:36 +00:00
|
|
|
if (m_cfg.maxVertsPerPoly <= DT_VERTS_PER_POLYGON)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-22 13:39:36 +00:00
|
|
|
if (m_pmesh->nverts >= 0xffff)
|
|
|
|
|
{
|
|
|
|
|
// The vertex indices are ushorts, and cannot point to more than 0xffff vertices.
|
|
|
|
|
Con::errorf("NavMesh::buildTileData: Too many vertices per tile %d (max: %d).", m_pmesh->nverts, 0xffff);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (U32 i = 0; i < m_pmesh->npolys; i++)
|
|
|
|
|
{
|
|
|
|
|
if (m_pmesh->areas[i] == RC_WALKABLE_AREA)
|
|
|
|
|
m_pmesh->areas[i] = GroundArea;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
if (m_pmesh->areas[i] == GroundArea)
|
|
|
|
|
m_pmesh->flags[i] |= WalkFlag;
|
|
|
|
|
if (m_pmesh->areas[i] == WaterArea)
|
|
|
|
|
m_pmesh->flags[i] |= SwimFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dtNavMeshCreateParams params;
|
|
|
|
|
dMemset(¶ms, 0, sizeof(params));
|
|
|
|
|
|
|
|
|
|
params.verts = m_pmesh->verts;
|
|
|
|
|
params.vertCount = m_pmesh->nverts;
|
|
|
|
|
params.polys = m_pmesh->polys;
|
|
|
|
|
params.polyAreas = m_pmesh->areas;
|
|
|
|
|
params.polyFlags = m_pmesh->flags;
|
|
|
|
|
params.polyCount = m_pmesh->npolys;
|
|
|
|
|
params.nvp = m_pmesh->nvp;
|
|
|
|
|
|
|
|
|
|
params.detailMeshes = m_dmesh->meshes;
|
|
|
|
|
params.detailVerts = m_dmesh->verts;
|
|
|
|
|
params.detailVertsCount = m_dmesh->nverts;
|
|
|
|
|
params.detailTris = m_dmesh->tris;
|
|
|
|
|
params.detailTriCount = m_dmesh->ntris;
|
|
|
|
|
|
|
|
|
|
params.offMeshConVerts = mLinkVerts.address();
|
|
|
|
|
params.offMeshConRad = mLinkRads.address();
|
|
|
|
|
params.offMeshConDir = mLinkDirs.address();
|
|
|
|
|
params.offMeshConAreas = mLinkAreas.address();
|
|
|
|
|
params.offMeshConFlags = mLinkFlags.address();
|
|
|
|
|
params.offMeshConUserID = mLinkIDs.address();
|
|
|
|
|
params.offMeshConCount = mLinkIDs.size();
|
|
|
|
|
|
|
|
|
|
params.walkableHeight = mWalkableHeight;
|
|
|
|
|
params.walkableRadius = mWalkableRadius;
|
|
|
|
|
params.walkableClimb = mWalkableClimb;
|
|
|
|
|
params.tileX = tile.x;
|
|
|
|
|
params.tileY = tile.y;
|
|
|
|
|
params.tileLayer = 0;
|
|
|
|
|
rcVcopy(params.bmin, m_pmesh->bmin);
|
|
|
|
|
rcVcopy(params.bmax, m_pmesh->bmax);
|
|
|
|
|
params.cs = m_cfg.cs;
|
|
|
|
|
params.ch = m_cfg.ch;
|
|
|
|
|
params.buildBvTree = true;
|
|
|
|
|
|
|
|
|
|
if (!dtCreateNavMeshData(¶ms, &navData, &navDataSize))
|
|
|
|
|
{
|
|
|
|
|
Con::errorf("NavMesh::buildTileData: Could not build Detour navmesh.");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
dataSize = navDataSize;
|
|
|
|
|
|
|
|
|
|
return navData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This method should never be called in a separate thread to the rendering
|
|
|
|
|
/// or pathfinding logic. It directly replaces data in the dtNavMesh for
|
|
|
|
|
/// this NavMesh object.
|
|
|
|
|
void NavMesh::buildTiles(const Box3F &box)
|
|
|
|
|
{
|
2017-04-05 18:04:25 +00:00
|
|
|
PROFILE_SCOPE(NavMesh_buildTiles);
|
2013-07-04 07:27:48 +00:00
|
|
|
// Make sure we've already built or loaded.
|
|
|
|
|
if(!nm)
|
|
|
|
|
return;
|
|
|
|
|
// Iterate over tiles.
|
|
|
|
|
for(U32 i = 0; i < mTiles.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
const Tile &tile = mTiles[i];
|
|
|
|
|
// Check tile box.
|
|
|
|
|
if(!tile.box.isOverlapped(box))
|
|
|
|
|
continue;
|
|
|
|
|
// Mark as dirty.
|
2015-11-27 22:17:30 +00:00
|
|
|
mDirtyTiles.push_back_unique(i);
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
2014-11-28 08:42:10 +00:00
|
|
|
if(mDirtyTiles.size())
|
|
|
|
|
ctx->startTimer(RC_TIMER_TOTAL);
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, buildTiles, void, (Box3F box),,
|
|
|
|
|
"@brief Rebuild the tiles overlapped by the input box.")
|
|
|
|
|
{
|
|
|
|
|
return object->buildTiles(box);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::buildTile(const U32 &tile)
|
|
|
|
|
{
|
2017-04-05 18:04:25 +00:00
|
|
|
PROFILE_SCOPE(NavMesh_buildTile);
|
2013-07-04 07:27:48 +00:00
|
|
|
if(tile < mTiles.size())
|
|
|
|
|
{
|
2015-11-27 22:17:30 +00:00
|
|
|
mDirtyTiles.push_back_unique(tile);
|
2014-11-28 08:42:10 +00:00
|
|
|
ctx->startTimer(RC_TIMER_TOTAL);
|
2025-07-23 20:02:44 +00:00
|
|
|
m_geo = NULL;
|
2014-11-28 08:42:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::buildLinks()
|
|
|
|
|
{
|
|
|
|
|
// Make sure we've already built or loaded.
|
|
|
|
|
if(!nm)
|
|
|
|
|
return;
|
|
|
|
|
// Iterate over tiles.
|
|
|
|
|
for(U32 i = 0; i < mTiles.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
const Tile &tile = mTiles[i];
|
|
|
|
|
// Iterate over links
|
|
|
|
|
for(U32 j = 0; j < mLinkIDs.size(); j++)
|
|
|
|
|
{
|
2017-01-12 04:34:46 +00:00
|
|
|
if (mLinksUnsynced[j])
|
|
|
|
|
{
|
2014-11-28 08:42:10 +00:00
|
|
|
if(tile.box.isContained(getLinkStart(j)) ||
|
2017-01-12 04:34:46 +00:00
|
|
|
tile.box.isContained(getLinkEnd(j)))
|
2014-11-28 08:42:10 +00:00
|
|
|
{
|
|
|
|
|
// Mark tile for build.
|
2015-11-27 22:17:30 +00:00
|
|
|
mDirtyTiles.push_back_unique(i);
|
2014-11-28 08:42:10 +00:00
|
|
|
// Delete link if necessary
|
|
|
|
|
if(mDeleteLinks[j])
|
|
|
|
|
{
|
|
|
|
|
eraseLink(j);
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
mLinksUnsynced[j] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-12 04:34:46 +00:00
|
|
|
}
|
2014-11-28 08:42:10 +00:00
|
|
|
if(mDirtyTiles.size())
|
|
|
|
|
ctx->startTimer(RC_TIMER_TOTAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, buildLinks, void, (),,
|
|
|
|
|
"@brief Build tiles of this mesh where there are unsynchronised links.")
|
|
|
|
|
{
|
|
|
|
|
object->buildLinks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::deleteCoverPoints()
|
|
|
|
|
{
|
|
|
|
|
SimSet *set = NULL;
|
|
|
|
|
if(Sim::findObject(mCoverSet, set))
|
|
|
|
|
set->deleteAllObjects();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, deleteCoverPoints, void, (),,
|
|
|
|
|
"@brief Remove all cover points for this NavMesh.")
|
|
|
|
|
{
|
|
|
|
|
object->deleteCoverPoints();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NavMesh::createCoverPoints()
|
|
|
|
|
{
|
|
|
|
|
if(!nm || !isServerObject())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
SimSet *set = NULL;
|
|
|
|
|
if(Sim::findObject(mCoverSet, set))
|
|
|
|
|
{
|
|
|
|
|
set->deleteAllObjects();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
set = new SimGroup();
|
|
|
|
|
if(set->registerObject(mCoverSet))
|
|
|
|
|
{
|
|
|
|
|
getGroup()->addObject(set);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
delete set;
|
|
|
|
|
set = getGroup();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dtNavMeshQuery *query = dtAllocNavMeshQuery();
|
|
|
|
|
if(!query || dtStatusFailed(query->init(nm, 1)))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
dtQueryFilter f;
|
|
|
|
|
|
|
|
|
|
// Iterate over all polys in our navmesh.
|
|
|
|
|
const int MAX_SEGS = 6;
|
|
|
|
|
for(U32 i = 0; i < nm->getMaxTiles(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const dtMeshTile* tile = ((const dtNavMesh*)nm)->getTile(i);
|
|
|
|
|
if(!tile->header) continue;
|
|
|
|
|
const dtPolyRef base = nm->getPolyRefBase(tile);
|
|
|
|
|
for(U32 j = 0; j < tile->header->polyCount; ++j)
|
|
|
|
|
{
|
|
|
|
|
const dtPolyRef ref = base | j;
|
|
|
|
|
float segs[MAX_SEGS*6];
|
|
|
|
|
int nsegs = 0;
|
|
|
|
|
query->getPolyWallSegments(ref, &f, segs, NULL, &nsegs, MAX_SEGS);
|
2018-03-14 20:13:44 +00:00
|
|
|
for(int segIDx = 0; segIDx < nsegs; ++segIDx)
|
2014-11-28 08:42:10 +00:00
|
|
|
{
|
2018-03-14 20:13:44 +00:00
|
|
|
const float* sa = &segs[segIDx *6];
|
|
|
|
|
const float* sb = &segs[segIDx *6+3];
|
2014-11-28 08:42:10 +00:00
|
|
|
Point3F a = RCtoDTS(sa), b = RCtoDTS(sb);
|
|
|
|
|
F32 len = (b - a).len();
|
|
|
|
|
if(len < mWalkableRadius * 2)
|
|
|
|
|
continue;
|
|
|
|
|
Point3F edge = b - a;
|
|
|
|
|
edge.normalize();
|
|
|
|
|
// Number of points to try placing - for now, one at each end.
|
|
|
|
|
U32 pointCount = (len > mWalkableRadius * 4) ? 2 : 1;
|
2018-03-14 20:13:44 +00:00
|
|
|
for(U32 pointIDx = 0; pointIDx < pointCount; pointIDx++)
|
2014-11-28 08:42:10 +00:00
|
|
|
{
|
|
|
|
|
MatrixF mat;
|
|
|
|
|
Point3F pos;
|
|
|
|
|
// If we're only placing one point, put it in the middle.
|
|
|
|
|
if(pointCount == 1)
|
|
|
|
|
pos = a + edge * len / 2;
|
|
|
|
|
// Otherwise, stand off from edge ends.
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-03-14 20:13:44 +00:00
|
|
|
if(pointIDx % 2)
|
|
|
|
|
pos = a + edge * (pointIDx /2+1) * mWalkableRadius;
|
2014-11-28 08:42:10 +00:00
|
|
|
else
|
2018-03-14 20:13:44 +00:00
|
|
|
pos = b - edge * (pointIDx /2+1) * mWalkableRadius;
|
2014-11-28 08:42:10 +00:00
|
|
|
}
|
|
|
|
|
CoverPointData data;
|
|
|
|
|
if(testEdgeCover(pos, edge, data))
|
|
|
|
|
{
|
|
|
|
|
CoverPoint *m = new CoverPoint();
|
|
|
|
|
if(!m->registerObject())
|
|
|
|
|
delete m;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m->setTransform(data.trans);
|
|
|
|
|
m->setSize(data.size);
|
|
|
|
|
m->setPeek(data.peek[0], data.peek[1], data.peek[2]);
|
|
|
|
|
if(set)
|
|
|
|
|
set->addObject(m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, createCoverPoints, bool, (),,
|
|
|
|
|
"@brief Create cover points for this NavMesh.")
|
|
|
|
|
{
|
|
|
|
|
return object->createCoverPoints();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NavMesh::testEdgeCover(const Point3F &pos, const VectorF &dir, CoverPointData &data)
|
|
|
|
|
{
|
|
|
|
|
data.peek[0] = data.peek[1] = data.peek[2] = false;
|
|
|
|
|
// Get the edge normal.
|
|
|
|
|
Point3F norm;
|
|
|
|
|
mCross(dir, Point3F(0, 0, 1), &norm);
|
|
|
|
|
RayInfo ray;
|
|
|
|
|
U32 hits = 0;
|
|
|
|
|
for(U32 j = 0; j < CoverPoint::NumSizes; j++)
|
|
|
|
|
{
|
2023-04-27 03:27:35 +00:00
|
|
|
Point3F test = pos + Point3F(0.0f, 0.0f, mWalkableHeight * j / (F32)CoverPoint::NumSizes);
|
2014-11-28 08:42:10 +00:00
|
|
|
if(getContainer()->castRay(test, test + norm * mCoverDist, StaticObjectType, &ray))
|
|
|
|
|
{
|
|
|
|
|
// Test peeking.
|
|
|
|
|
Point3F left = test + dir * mPeekDist;
|
|
|
|
|
data.peek[0] = !getContainer()->castRay(test, left, StaticObjectType, &ray)
|
|
|
|
|
&& !getContainer()->castRay(left, left + norm * mCoverDist, StaticObjectType, &ray);
|
|
|
|
|
|
|
|
|
|
Point3F right = test - dir * mPeekDist;
|
|
|
|
|
data.peek[1] = !getContainer()->castRay(test, right, StaticObjectType, &ray)
|
|
|
|
|
&& !getContainer()->castRay(right, right + norm * mCoverDist, StaticObjectType, &ray);
|
|
|
|
|
|
|
|
|
|
Point3F over = test + Point3F(0, 0, 1) * 0.2f;
|
|
|
|
|
data.peek[2] = !getContainer()->castRay(test, over, StaticObjectType, &ray)
|
|
|
|
|
&& !getContainer()->castRay(over, over + norm * mCoverDist, StaticObjectType, &ray);
|
|
|
|
|
|
|
|
|
|
if(mInnerCover || data.peek[0] || data.peek[1] || data.peek[2])
|
|
|
|
|
hits++;
|
|
|
|
|
// If we couldn't peek here, we may be able to peek further up.
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// No cover at this height - break off.
|
|
|
|
|
break;
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
2014-11-28 08:42:10 +00:00
|
|
|
if(hits > 0)
|
|
|
|
|
{
|
|
|
|
|
data.size = (CoverPoint::Size)(hits - 1);
|
|
|
|
|
data.trans = MathUtils::createOrientFromDir(norm);
|
|
|
|
|
data.trans.setPosition(pos);
|
|
|
|
|
}
|
|
|
|
|
return hits > 0;
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::renderToDrawer()
|
|
|
|
|
{
|
2025-07-24 13:25:02 +00:00
|
|
|
mDbgDraw.clearCache();
|
|
|
|
|
// Recast debug draw
|
|
|
|
|
NetObject* no = getServerObject();
|
|
|
|
|
if (no)
|
|
|
|
|
{
|
|
|
|
|
NavMesh* n = static_cast<NavMesh*>(no);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true, true);
|
2025-07-24 13:25:02 +00:00
|
|
|
if (n->nm &&
|
|
|
|
|
(m_drawMode == DRAWMODE_NAVMESH ||
|
|
|
|
|
m_drawMode == DRAWMODE_NAVMESH_TRANS ||
|
|
|
|
|
m_drawMode == DRAWMODE_NAVMESH_BVTREE ||
|
|
|
|
|
m_drawMode == DRAWMODE_NAVMESH_NODES ||
|
|
|
|
|
m_drawMode == DRAWMODE_NAVMESH_PORTALS ||
|
|
|
|
|
m_drawMode == DRAWMODE_NAVMESH_INVIS))
|
|
|
|
|
{
|
2025-07-24 14:01:28 +00:00
|
|
|
if (m_drawMode != DRAWMODE_NAVMESH_INVIS)
|
2025-07-28 07:24:20 +00:00
|
|
|
{
|
|
|
|
|
if (m_drawMode == DRAWMODE_NAVMESH_TRANS)
|
|
|
|
|
mDbgDraw.blend(true);
|
2025-07-26 09:34:19 +00:00
|
|
|
duDebugDrawNavMeshWithClosedList(&mDbgDraw, *n->nm, *n->mQuery, 0);
|
2025-07-28 07:24:20 +00:00
|
|
|
mDbgDraw.blend(false);
|
|
|
|
|
}
|
2025-07-24 13:25:02 +00:00
|
|
|
if(m_drawMode == DRAWMODE_NAVMESH_BVTREE)
|
|
|
|
|
duDebugDrawNavMeshBVTree(&mDbgDraw, *n->nm);
|
|
|
|
|
if(m_drawMode == DRAWMODE_NAVMESH_PORTALS)
|
|
|
|
|
duDebugDrawNavMeshPortals(&mDbgDraw, *n->nm);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true, false);
|
|
|
|
|
|
2025-07-24 13:25:02 +00:00
|
|
|
for (Tile& tile : n->mTiles)
|
|
|
|
|
{
|
|
|
|
|
if (tile.chf && m_drawMode == DRAWMODE_COMPACT)
|
|
|
|
|
{
|
|
|
|
|
duDebugDrawCompactHeightfieldSolid(&mDbgDraw, *tile.chf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.chf && m_drawMode == DRAWMODE_COMPACT_DISTANCE)
|
|
|
|
|
{
|
|
|
|
|
duDebugDrawCompactHeightfieldDistance(&mDbgDraw, *tile.chf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.chf && m_drawMode == DRAWMODE_COMPACT_REGIONS)
|
|
|
|
|
{
|
|
|
|
|
duDebugDrawCompactHeightfieldRegions(&mDbgDraw, *tile.chf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.solid && m_drawMode == DRAWMODE_VOXELS)
|
|
|
|
|
{
|
|
|
|
|
duDebugDrawHeightfieldSolid(&mDbgDraw, *tile.solid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.solid && m_drawMode == DRAWMODE_VOXELS_WALKABLE)
|
|
|
|
|
{
|
|
|
|
|
duDebugDrawHeightfieldWalkable(&mDbgDraw, *tile.solid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.cset && m_drawMode == DRAWMODE_RAW_CONTOURS)
|
|
|
|
|
{
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(false);
|
2025-07-24 13:25:02 +00:00
|
|
|
duDebugDrawRawContours(&mDbgDraw, *tile.cset);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true);
|
2025-07-24 13:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.cset && m_drawMode == DRAWMODE_BOTH_CONTOURS)
|
|
|
|
|
{
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(false);
|
2025-07-24 13:25:02 +00:00
|
|
|
duDebugDrawRawContours(&mDbgDraw, *tile.cset);
|
|
|
|
|
duDebugDrawContours(&mDbgDraw, *tile.cset);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true);
|
2025-07-24 13:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.cset && m_drawMode == DRAWMODE_CONTOURS)
|
|
|
|
|
{
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(false);
|
2025-07-24 13:25:02 +00:00
|
|
|
duDebugDrawContours(&mDbgDraw, *tile.cset);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true);
|
2025-07-24 13:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.chf && tile.cset && m_drawMode == DRAWMODE_REGION_CONNECTIONS)
|
|
|
|
|
{
|
2025-07-25 10:11:58 +00:00
|
|
|
|
2025-07-24 13:25:02 +00:00
|
|
|
duDebugDrawCompactHeightfieldRegions(&mDbgDraw, *tile.chf);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(false);
|
2025-07-24 13:25:02 +00:00
|
|
|
duDebugDrawRegionConnections(&mDbgDraw, *tile.cset);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true);
|
2025-07-24 13:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.pmesh && m_drawMode == DRAWMODE_POLYMESH)
|
|
|
|
|
{
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(false);
|
2025-07-24 13:25:02 +00:00
|
|
|
duDebugDrawPolyMesh(&mDbgDraw, *tile.pmesh);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true);
|
2025-07-24 13:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tile.dmesh && m_drawMode == DRAWMODE_POLYMESH_DETAIL)
|
|
|
|
|
{
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(false);
|
2025-07-24 13:25:02 +00:00
|
|
|
duDebugDrawPolyMeshDetail(&mDbgDraw, *tile.dmesh);
|
2025-07-25 10:11:58 +00:00
|
|
|
mDbgDraw.depthMask(true);
|
2025-07-24 13:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 13:39:36 +00:00
|
|
|
void NavMesh::cleanup()
|
|
|
|
|
{
|
|
|
|
|
delete[] m_triareas;
|
|
|
|
|
m_triareas = 0;
|
|
|
|
|
rcFreeHeightField(m_solid);
|
|
|
|
|
m_solid = 0;
|
|
|
|
|
rcFreeCompactHeightfield(m_chf);
|
|
|
|
|
m_chf = 0;
|
|
|
|
|
rcFreeContourSet(m_cset);
|
|
|
|
|
m_cset = 0;
|
|
|
|
|
rcFreePolyMesh(m_pmesh);
|
|
|
|
|
m_pmesh = 0;
|
|
|
|
|
rcFreePolyMeshDetail(m_dmesh);
|
|
|
|
|
m_dmesh = 0;
|
2025-07-28 19:26:31 +00:00
|
|
|
SAFE_DELETE(m_geo);
|
2025-07-22 13:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
void NavMesh::prepRenderImage(SceneRenderState *state)
|
|
|
|
|
{
|
|
|
|
|
ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
|
|
|
|
|
ri->renderDelegate.bind(this, &NavMesh::render);
|
|
|
|
|
ri->type = RenderPassManager::RIT_Object;
|
|
|
|
|
ri->translucentSort = true;
|
|
|
|
|
ri->defaultKey = 1;
|
|
|
|
|
state->getRenderPass()->addInst(ri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat)
|
|
|
|
|
{
|
|
|
|
|
if(overrideMat)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(state->isReflectPass())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
PROFILE_SCOPE(NavMesh_Render);
|
|
|
|
|
|
|
|
|
|
// Recast debug draw
|
|
|
|
|
NetObject *no = getServerObject();
|
|
|
|
|
if(no)
|
|
|
|
|
{
|
|
|
|
|
NavMesh *n = static_cast<NavMesh*>(no);
|
|
|
|
|
|
2025-07-24 13:25:02 +00:00
|
|
|
if ((!gEditingMission && n->mAlwaysRender) || gEditingMission)
|
2013-07-04 07:27:48 +00:00
|
|
|
{
|
2025-07-24 13:25:02 +00:00
|
|
|
mDbgDraw.render(state);
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
void NavMesh::renderLinks(duDebugDraw &dd)
|
|
|
|
|
{
|
|
|
|
|
if(mBuilding)
|
|
|
|
|
return;
|
|
|
|
|
dd.begin(DU_DRAW_LINES);
|
2025-07-24 22:43:35 +00:00
|
|
|
dd.depthMask(false);
|
2014-11-28 08:42:10 +00:00
|
|
|
for(U32 i = 0; i < mLinkIDs.size(); i++)
|
|
|
|
|
{
|
2014-11-29 00:59:00 +00:00
|
|
|
U32 col = 0;
|
2014-11-28 08:42:10 +00:00
|
|
|
switch(mLinkSelectStates[i])
|
|
|
|
|
{
|
2014-11-29 00:59:00 +00:00
|
|
|
case Unselected: col = mLinksUnsynced[i] ? duRGBA(255, 0, 0, 200) : duRGBA(0, 0, 255, 255); break;
|
|
|
|
|
case Hovered: col = duRGBA(255, 255, 255, 255); break;
|
|
|
|
|
case Selected: col = duRGBA(0, 255, 0, 255); break;
|
2014-11-28 08:42:10 +00:00
|
|
|
}
|
|
|
|
|
F32 *s = &mLinkVerts[i*6];
|
|
|
|
|
F32 *e = &mLinkVerts[i*6 + 3];
|
|
|
|
|
if(!mDeleteLinks[i])
|
|
|
|
|
duAppendCircle(&dd, s[0], s[1], s[2], mLinkRads[i], col);
|
|
|
|
|
duAppendArc(&dd,
|
|
|
|
|
s[0], s[1], s[2],
|
|
|
|
|
e[0], e[1], e[2],
|
|
|
|
|
0.3f,
|
2025-07-24 22:43:35 +00:00
|
|
|
(mLinkDirs[i]&1) ? 0.6f : 0.0f, mLinkFlags[i] == DropFlag ? 0.0f : 0.6f,
|
2014-11-28 08:42:10 +00:00
|
|
|
col);
|
|
|
|
|
if(!mDeleteLinks[i])
|
|
|
|
|
duAppendCircle(&dd, e[0], e[1], e[2], mLinkRads[i], col);
|
|
|
|
|
}
|
|
|
|
|
dd.end();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 10:31:06 +00:00
|
|
|
void NavMesh::renderSearch(duDebugDraw& dd)
|
|
|
|
|
{
|
|
|
|
|
if (mQuery == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_drawMode == DRAWMODE_NAVMESH_NODES)
|
|
|
|
|
duDebugDrawNavMeshNodes(&dd, *mQuery);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
void NavMesh::renderTileData(duDebugDrawTorque &dd, U32 tile)
|
|
|
|
|
{
|
2025-07-24 13:25:02 +00:00
|
|
|
if (tile > mTiles.size())
|
|
|
|
|
return;
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
if(nm)
|
|
|
|
|
{
|
2025-07-24 13:25:02 +00:00
|
|
|
//duDebugDrawNavMesh(&dd, *nm, 0);
|
|
|
|
|
if(mTiles[tile].chf)
|
|
|
|
|
duDebugDrawCompactHeightfieldSolid(&dd, *mTiles[tile].chf);
|
2014-11-28 08:42:10 +00:00
|
|
|
|
2025-07-20 15:10:27 +00:00
|
|
|
duDebugDrawNavMeshPortals(&dd, *nm);
|
2025-07-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
if (!m_geo)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
int col = duRGBA(255, 0, 255, 255);
|
|
|
|
|
dd.begin(DU_DRAW_LINES);
|
2025-07-23 04:55:05 +00:00
|
|
|
const F32 *verts = m_geo->getVerts();
|
|
|
|
|
const S32 *tris = m_geo->getTris();
|
|
|
|
|
for(U32 t = 0; t < m_geo->getTriCount(); t++)
|
2014-11-28 08:42:10 +00:00
|
|
|
{
|
|
|
|
|
dd.vertex(&verts[tris[t*3]*3], col);
|
|
|
|
|
dd.vertex(&verts[tris[t*3+1]*3], col);
|
|
|
|
|
dd.vertex(&verts[tris[t*3+1]*3], col);
|
|
|
|
|
dd.vertex(&verts[tris[t*3+2]*3], col);
|
|
|
|
|
dd.vertex(&verts[tris[t*3+2]*3], col);
|
|
|
|
|
dd.vertex(&verts[tris[t*3]*3], col);
|
|
|
|
|
}
|
|
|
|
|
dd.end();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 07:27:48 +00:00
|
|
|
void NavMesh::onEditorEnable()
|
|
|
|
|
{
|
|
|
|
|
mNetFlags.set(Ghostable);
|
|
|
|
|
if(isClientObject() && !mAlwaysRender)
|
|
|
|
|
addToScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::onEditorDisable()
|
|
|
|
|
{
|
|
|
|
|
if(!mAlwaysRender)
|
|
|
|
|
{
|
|
|
|
|
mNetFlags.clear(Ghostable);
|
|
|
|
|
if(isClientObject())
|
|
|
|
|
removeFromScene();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
U32 NavMesh::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
|
|
|
|
|
{
|
|
|
|
|
U32 retMask = Parent::packUpdate(conn, mask, stream);
|
|
|
|
|
|
|
|
|
|
mathWrite(*stream, getTransform());
|
|
|
|
|
mathWrite(*stream, getScale());
|
|
|
|
|
stream->writeFlag(mAlwaysRender);
|
2025-07-24 13:25:02 +00:00
|
|
|
stream->write((U32)m_drawMode);
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
return retMask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::unpackUpdate(NetConnection *conn, BitStream *stream)
|
|
|
|
|
{
|
|
|
|
|
Parent::unpackUpdate(conn, stream);
|
|
|
|
|
|
|
|
|
|
mathRead(*stream, &mObjToWorld);
|
|
|
|
|
mathRead(*stream, &mObjScale);
|
|
|
|
|
mAlwaysRender = stream->readFlag();
|
|
|
|
|
setTransform(mObjToWorld);
|
2025-07-24 13:25:02 +00:00
|
|
|
U32 draw;
|
|
|
|
|
stream->read(&draw);
|
|
|
|
|
m_drawMode = (DrawMode)draw;
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
renderToDrawer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const int NAVMESHSET_MAGIC = 'M'<<24 | 'S'<<16 | 'E'<<8 | 'T'; //'MSET';
|
|
|
|
|
static const int NAVMESHSET_VERSION = 1;
|
|
|
|
|
|
|
|
|
|
struct NavMeshSetHeader
|
|
|
|
|
{
|
|
|
|
|
int magic;
|
|
|
|
|
int version;
|
|
|
|
|
int numTiles;
|
|
|
|
|
dtNavMeshParams params;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct NavMeshTileHeader
|
|
|
|
|
{
|
|
|
|
|
dtTileRef tileRef;
|
|
|
|
|
int dataSize;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool NavMesh::load()
|
|
|
|
|
{
|
|
|
|
|
if(!dStrlen(mFileName))
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-01-11 09:51:21 +00:00
|
|
|
FileStream stream;
|
|
|
|
|
if(!stream.open(mFileName, Torque::FS::File::Read))
|
2014-11-28 09:24:11 +00:00
|
|
|
{
|
|
|
|
|
Con::errorf("Could not open file %s when loading navmesh %s.",
|
|
|
|
|
mFileName, getName() ? getName() : getIdString());
|
2013-07-04 07:27:48 +00:00
|
|
|
return false;
|
2014-11-28 09:24:11 +00:00
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
// Read header.
|
|
|
|
|
NavMeshSetHeader header;
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.read(sizeof(NavMeshSetHeader), (char*)&header);
|
2013-07-04 07:27:48 +00:00
|
|
|
if(header.magic != NAVMESHSET_MAGIC)
|
|
|
|
|
{
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.close();
|
2014-11-28 09:24:11 +00:00
|
|
|
Con::errorf("Navmesh magic incorrect when loading navmesh %s; possible corrupt navmesh file %s.",
|
|
|
|
|
getName() ? getName() : getIdString(), mFileName);
|
|
|
|
|
return false;
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
if(header.version != NAVMESHSET_VERSION)
|
|
|
|
|
{
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.close();
|
2014-11-28 09:24:11 +00:00
|
|
|
Con::errorf("Navmesh version incorrect when loading navmesh %s; possible corrupt navmesh file %s.",
|
|
|
|
|
getName() ? getName() : getIdString(), mFileName);
|
|
|
|
|
return false;
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(nm)
|
|
|
|
|
dtFreeNavMesh(nm);
|
|
|
|
|
nm = dtAllocNavMesh();
|
|
|
|
|
if(!nm)
|
|
|
|
|
{
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.close();
|
2014-11-28 09:24:11 +00:00
|
|
|
Con::errorf("Out of memory when loading navmesh %s.",
|
|
|
|
|
getName() ? getName() : getIdString());
|
2013-07-04 07:27:48 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dtStatus status = nm->init(&header.params);
|
|
|
|
|
if(dtStatusFailed(status))
|
|
|
|
|
{
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.close();
|
2014-11-28 09:24:11 +00:00
|
|
|
Con::errorf("Failed to initialise navmesh params when loading navmesh %s.",
|
|
|
|
|
getName() ? getName() : getIdString());
|
2013-07-04 07:27:48 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read tiles.
|
|
|
|
|
for(U32 i = 0; i < header.numTiles; ++i)
|
|
|
|
|
{
|
|
|
|
|
NavMeshTileHeader tileHeader;
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.read(sizeof(NavMeshTileHeader), (char*)&tileHeader);
|
2013-07-04 07:27:48 +00:00
|
|
|
if(!tileHeader.tileRef || !tileHeader.dataSize)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
|
|
|
|
|
if(!data) break;
|
|
|
|
|
memset(data, 0, tileHeader.dataSize);
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.read(tileHeader.dataSize, (char*)data);
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
nm->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
S32 s;
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.read(sizeof(S32), (char*)&s);
|
2014-11-28 08:42:10 +00:00
|
|
|
setLinkCount(s);
|
2016-08-01 22:22:19 +00:00
|
|
|
if (s > 0)
|
|
|
|
|
{
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.read(sizeof(F32) * s * 6, (char*)const_cast<F32*>(mLinkVerts.address()));
|
|
|
|
|
stream.read(sizeof(F32) * s, (char*)const_cast<F32*>(mLinkRads.address()));
|
|
|
|
|
stream.read(sizeof(U8) * s, (char*)const_cast<U8*>(mLinkDirs.address()));
|
|
|
|
|
stream.read(sizeof(U8) * s, (char*)const_cast<U8*>(mLinkAreas.address()));
|
|
|
|
|
stream.read(sizeof(U16) * s, (char*)const_cast<U16*>(mLinkFlags.address()));
|
|
|
|
|
stream.read(sizeof(F32) * s, (char*)const_cast<U32*>(mLinkIDs.address()));
|
2016-08-01 22:22:19 +00:00
|
|
|
}
|
2014-11-28 08:42:10 +00:00
|
|
|
mLinksUnsynced.fill(false);
|
|
|
|
|
mLinkSelectStates.fill(Unselected);
|
|
|
|
|
mDeleteLinks.fill(false);
|
|
|
|
|
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.close();
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
updateTiles();
|
|
|
|
|
|
|
|
|
|
if(isServerObject())
|
|
|
|
|
{
|
|
|
|
|
setMaskBits(LoadFlag);
|
2014-11-28 08:42:10 +00:00
|
|
|
if(getEventManager())
|
|
|
|
|
getEventManager()->postEvent("NavMeshUpdate", getIdString());
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, load, bool, (),,
|
|
|
|
|
"@brief Load this NavMesh from its file.")
|
|
|
|
|
{
|
|
|
|
|
return object->load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NavMesh::save()
|
|
|
|
|
{
|
2025-07-23 20:02:44 +00:00
|
|
|
if (!nm)
|
2013-07-04 07:27:48 +00:00
|
|
|
return false;
|
2025-07-23 20:02:44 +00:00
|
|
|
|
|
|
|
|
if (!dStrlen(mFileName) || !nm)
|
|
|
|
|
{
|
|
|
|
|
createNewFile();
|
|
|
|
|
}
|
2014-11-28 09:24:11 +00:00
|
|
|
|
2021-01-11 09:51:21 +00:00
|
|
|
FileStream stream;
|
|
|
|
|
if(!stream.open(mFileName, Torque::FS::File::Write))
|
2014-11-28 09:24:11 +00:00
|
|
|
{
|
|
|
|
|
Con::errorf("Could not open file %s when saving navmesh %s.",
|
|
|
|
|
mFileName, getName() ? getName() : getIdString());
|
2013-07-04 07:27:48 +00:00
|
|
|
return false;
|
2014-11-28 09:24:11 +00:00
|
|
|
}
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
// Store header.
|
|
|
|
|
NavMeshSetHeader header;
|
|
|
|
|
header.magic = NAVMESHSET_MAGIC;
|
|
|
|
|
header.version = NAVMESHSET_VERSION;
|
|
|
|
|
header.numTiles = 0;
|
|
|
|
|
for(U32 i = 0; i < nm->getMaxTiles(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const dtMeshTile* tile = ((const dtNavMesh*)nm)->getTile(i);
|
|
|
|
|
if (!tile || !tile->header || !tile->dataSize) continue;
|
|
|
|
|
header.numTiles++;
|
|
|
|
|
}
|
|
|
|
|
memcpy(&header.params, nm->getParams(), sizeof(dtNavMeshParams));
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.write(sizeof(NavMeshSetHeader), (const char*)&header);
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
// Store tiles.
|
|
|
|
|
for(U32 i = 0; i < nm->getMaxTiles(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const dtMeshTile* tile = ((const dtNavMesh*)nm)->getTile(i);
|
|
|
|
|
if(!tile || !tile->header || !tile->dataSize) continue;
|
|
|
|
|
|
|
|
|
|
NavMeshTileHeader tileHeader;
|
|
|
|
|
tileHeader.tileRef = nm->getTileRef(tile);
|
|
|
|
|
tileHeader.dataSize = tile->dataSize;
|
|
|
|
|
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.write(sizeof(tileHeader), (const char*)&tileHeader);
|
|
|
|
|
stream.write(tile->dataSize, (const char*)tile->data);
|
2013-07-04 07:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-28 08:42:10 +00:00
|
|
|
S32 s = mLinkIDs.size();
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.write(sizeof(S32), (const char*)&s);
|
2016-09-14 11:08:20 +00:00
|
|
|
if (s > 0)
|
|
|
|
|
{
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.write(sizeof(F32) * s * 6, (const char*)mLinkVerts.address());
|
|
|
|
|
stream.write(sizeof(F32) * s, (const char*)mLinkRads.address());
|
|
|
|
|
stream.write(sizeof(U8) * s, (const char*)mLinkDirs.address());
|
|
|
|
|
stream.write(sizeof(U8) * s, (const char*)mLinkAreas.address());
|
|
|
|
|
stream.write(sizeof(U16) * s, (const char*)mLinkFlags.address());
|
|
|
|
|
stream.write(sizeof(U32) * s, (const char*)mLinkIDs.address());
|
2016-09-14 11:08:20 +00:00
|
|
|
}
|
2014-11-28 08:42:10 +00:00
|
|
|
|
2021-01-11 09:51:21 +00:00
|
|
|
stream.close();
|
2013-07-04 07:27:48 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefineEngineMethod(NavMesh, save, void, (),,
|
|
|
|
|
"@brief Save this NavMesh to its file.")
|
|
|
|
|
{
|
|
|
|
|
object->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NavMesh::write(Stream &stream, U32 tabStop, U32 flags)
|
|
|
|
|
{
|
|
|
|
|
save();
|
|
|
|
|
Parent::write(stream, tabStop, flags);
|
|
|
|
|
}
|