DotRecastNetSim/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs

130 lines
5.3 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
recast4j Copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System.Collections.Generic;
using DotRecast.Recast;
using DotRecast.Recast.Geom;
2023-05-26 15:34:29 +03:00
2023-03-14 08:02:43 +03:00
namespace DotRecast.Detour.Test;
2023-03-16 19:48:49 +03:00
public class TestTiledNavMeshBuilder
{
private readonly DtNavMesh navMesh;
2023-03-14 08:02:43 +03:00
private const float m_cellSize = 0.3f;
private const float m_cellHeight = 0.2f;
private const float m_agentHeight = 2.0f;
private const float m_agentRadius = 0.6f;
private const float m_agentMaxClimb = 0.9f;
private const float m_agentMaxSlope = 45.0f;
private const int m_regionMinSize = 8;
private const int m_regionMergeSize = 20;
private const float m_regionMinArea = m_regionMinSize * m_regionMinSize * m_cellSize * m_cellSize;
private const float m_regionMergeArea = m_regionMergeSize * m_regionMergeSize * m_cellSize * m_cellSize;
private const float m_edgeMaxLen = 12.0f;
private const float m_edgeMaxError = 1.3f;
private const int m_vertsPerPoly = 6;
private const float m_detailSampleDist = 6.0f;
private const float m_detailSampleMaxError = 1.0f;
private const int m_tileSize = 32;
public TestTiledNavMeshBuilder() :
this(SimpleInputGeomProvider.LoadFile("dungeon.obj"),
2023-07-28 14:43:50 +03:00
RcPartition.WATERSHED, m_cellSize, m_cellHeight, m_agentHeight, m_agentRadius, m_agentMaxClimb, m_agentMaxSlope,
2023-03-16 19:48:49 +03:00
m_regionMinSize, m_regionMergeSize, m_edgeMaxLen, m_edgeMaxError, m_vertsPerPoly, m_detailSampleDist,
m_detailSampleMaxError, m_tileSize)
2023-03-14 08:02:43 +03:00
{
}
2023-07-28 15:01:22 +03:00
public TestTiledNavMeshBuilder(IInputGeomProvider geom, RcPartition partitionType, float cellSize, float cellHeight,
float agentHeight, float agentRadius, float agentMaxClimb, float agentMaxSlope, int regionMinSize,
int regionMergeSize, float edgeMaxLen, float edgeMaxError, int vertsPerPoly, float detailSampleDist,
float detailSampleMaxError, int tileSize)
2023-03-16 19:48:49 +03:00
{
2023-03-14 08:02:43 +03:00
// Create empty nav mesh
DtNavMeshParams navMeshParams = new DtNavMeshParams();
2023-07-28 15:01:22 +03:00
navMeshParams.orig = geom.GetMeshBoundsMin();
navMeshParams.tileWidth = tileSize * cellSize;
navMeshParams.tileHeight = tileSize * cellSize;
2023-03-14 08:02:43 +03:00
navMeshParams.maxTiles = 128;
navMeshParams.maxPolys = 32768;
2024-05-12 16:05:19 +03:00
navMesh = new DtNavMesh();
navMesh.Init(navMeshParams, 6);
2023-03-14 08:02:43 +03:00
// Build all tiles
2023-07-28 15:01:22 +03:00
RcConfig cfg = new RcConfig(true, tileSize, tileSize, RcConfig.CalcBorder(agentRadius, cellSize),
2023-09-02 07:31:30 +03:00
partitionType,
cellSize, cellHeight,
agentMaxSlope, agentHeight, agentRadius, agentMaxClimb,
m_regionMinArea, m_regionMergeArea,
edgeMaxLen, edgeMaxError,
vertsPerPoly,
detailSampleDist, detailSampleMaxError,
true, true, true,
SampleAreaModifications.SAMPLE_AREAMOD_GROUND, true);
2023-09-22 17:42:21 +03:00
RcBuilder rcBuilder = new RcBuilder();
List<RcBuilderResult> rcResult = rcBuilder.BuildTiles(geom, cfg, false, true);
2023-03-14 08:02:43 +03:00
// Add tiles to nav mesh
2023-09-22 17:42:21 +03:00
foreach (RcBuilderResult result in rcResult)
2023-03-16 19:48:49 +03:00
{
RcPolyMesh pmesh = result.Mesh;
2023-03-16 19:48:49 +03:00
if (pmesh.npolys == 0)
{
2023-03-14 08:02:43 +03:00
continue;
}
2023-03-16 19:48:49 +03:00
for (int i = 0; i < pmesh.npolys; ++i)
{
2023-03-14 08:02:43 +03:00
pmesh.flags[i] = 1;
}
2023-03-16 19:48:49 +03:00
DtNavMeshCreateParams option = new DtNavMeshCreateParams();
2023-03-14 08:02:43 +03:00
option.verts = pmesh.verts;
option.vertCount = pmesh.nverts;
option.polys = pmesh.polys;
option.polyAreas = pmesh.areas;
option.polyFlags = pmesh.flags;
option.polyCount = pmesh.npolys;
option.nvp = pmesh.nvp;
RcPolyMeshDetail dmesh = result.MeshDetail;
2023-03-14 08:02:43 +03:00
option.detailMeshes = dmesh.meshes;
option.detailVerts = dmesh.verts;
option.detailVertsCount = dmesh.nverts;
option.detailTris = dmesh.tris;
option.detailTriCount = dmesh.ntris;
2023-07-28 15:01:22 +03:00
option.walkableHeight = agentHeight;
option.walkableRadius = agentRadius;
option.walkableClimb = agentMaxClimb;
2023-03-14 08:02:43 +03:00
option.bmin = pmesh.bmin;
option.bmax = pmesh.bmax;
2023-07-28 15:01:22 +03:00
option.cs = cellSize;
option.ch = cellHeight;
option.tileX = result.TileX;
option.tileZ = result.TileZ;
2023-03-14 08:02:43 +03:00
option.buildBvTree = true;
2024-05-12 16:05:19 +03:00
navMesh.AddTile(DtNavMeshBuilder.CreateNavMeshData(option), 0, 0, out _);
2023-03-14 08:02:43 +03:00
}
}
public DtNavMesh GetNavMesh()
2023-03-16 19:48:49 +03:00
{
2023-03-14 08:02:43 +03:00
return navMesh;
}
2023-03-16 19:48:49 +03:00
}