2023-03-14 08:02:43 +03:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
|
|
|
|
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
|
2023-03-15 17:00:29 +03:00
|
|
|
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
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.Core;
|
|
|
|
using DotRecast.Recast;
|
|
|
|
using DotRecast.Recast.Geom;
|
2023-05-10 16:44:51 +03:00
|
|
|
using static DotRecast.Core.RcMath;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
namespace DotRecast.Detour.TileCache.Test;
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public class TestTileLayerBuilder : AbstractTileLayersBuilder
|
|
|
|
{
|
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 readonly RecastConfig rcConfig;
|
|
|
|
private const int m_tileSize = 48;
|
2023-05-05 03:06:43 +03:00
|
|
|
protected readonly IInputGeomProvider geom;
|
2023-03-14 08:02:43 +03:00
|
|
|
private readonly int tw;
|
|
|
|
private readonly int th;
|
|
|
|
|
2023-05-05 03:06:43 +03:00
|
|
|
public TestTileLayerBuilder(IInputGeomProvider geom)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
this.geom = geom;
|
2023-05-05 02:44:48 +03:00
|
|
|
rcConfig = new RecastConfig(true, m_tileSize, m_tileSize, RecastConfig.CalcBorder(m_agentRadius, m_cellSize),
|
2023-03-16 19:48:49 +03:00
|
|
|
PartitionType.WATERSHED, m_cellSize, m_cellHeight, m_agentMaxSlope, true, true, true, m_agentHeight,
|
|
|
|
m_agentRadius, m_agentMaxClimb, m_regionMinArea, m_regionMergeArea, m_edgeMaxLen, m_edgeMaxError, m_vertsPerPoly,
|
|
|
|
true, m_detailSampleDist, m_detailSampleMaxError, SampleAreaModifications.SAMPLE_AREAMOD_GROUND);
|
2023-05-05 02:44:48 +03:00
|
|
|
Vector3f bmin = geom.GetMeshBoundsMin();
|
|
|
|
Vector3f bmax = geom.GetMeshBoundsMax();
|
|
|
|
int[] twh = Recast.Recast.CalcTileCount(bmin, bmax, m_cellSize, m_tileSize, m_tileSize);
|
2023-03-14 08:02:43 +03:00
|
|
|
tw = twh[0];
|
|
|
|
th = twh[1];
|
|
|
|
}
|
|
|
|
|
2023-05-10 16:44:51 +03:00
|
|
|
public List<byte[]> Build(RcByteOrder order, bool cCompatibility, int threads)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
return Build(order, cCompatibility, threads, tw, th);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public int GetTw()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return tw;
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public int GetTh()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
return th;
|
|
|
|
}
|
|
|
|
|
2023-05-10 16:44:51 +03:00
|
|
|
protected override List<byte[]> Build(int tx, int ty, RcByteOrder order, bool cCompatibility)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
HeightfieldLayerSet lset = GetHeightfieldSet(tx, ty);
|
2023-03-14 08:02:43 +03:00
|
|
|
List<byte[]> result = new();
|
2023-03-16 19:48:49 +03:00
|
|
|
if (lset != null)
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
TileCacheBuilder builder = new TileCacheBuilder();
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < lset.layers.Length; ++i)
|
|
|
|
{
|
2023-05-07 12:10:20 +03:00
|
|
|
HeightfieldLayer layer = lset.layers[i];
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
// Store header
|
|
|
|
TileCacheLayerHeader header = new TileCacheLayerHeader();
|
|
|
|
header.magic = TileCacheLayerHeader.DT_TILECACHE_MAGIC;
|
|
|
|
header.version = TileCacheLayerHeader.DT_TILECACHE_VERSION;
|
|
|
|
|
|
|
|
// Tile layer location in the navmesh.
|
|
|
|
header.tx = tx;
|
|
|
|
header.ty = ty;
|
|
|
|
header.tlayer = i;
|
2023-04-12 17:53:28 +03:00
|
|
|
header.bmin = layer.bmin;
|
|
|
|
header.bmax = layer.bmax;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
// Tile info.
|
|
|
|
header.width = layer.width;
|
|
|
|
header.height = layer.height;
|
|
|
|
header.minx = layer.minx;
|
|
|
|
header.maxx = layer.maxx;
|
|
|
|
header.miny = layer.miny;
|
|
|
|
header.maxy = layer.maxy;
|
|
|
|
header.hmin = layer.hmin;
|
|
|
|
header.hmax = layer.hmax;
|
2023-05-05 02:44:48 +03:00
|
|
|
result.Add(builder.CompressTileCacheLayer(header, layer.heights, layer.areas, layer.cons, order, cCompatibility));
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
protected HeightfieldLayerSet GetHeightfieldSet(int tx, int ty)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
RecastBuilder rcBuilder = new RecastBuilder();
|
2023-05-05 02:44:48 +03:00
|
|
|
Vector3f bmin = geom.GetMeshBoundsMin();
|
|
|
|
Vector3f bmax = geom.GetMeshBoundsMax();
|
2023-03-14 08:02:43 +03:00
|
|
|
RecastBuilderConfig cfg = new RecastBuilderConfig(rcConfig, bmin, bmax, tx, ty);
|
2023-05-05 02:44:48 +03:00
|
|
|
HeightfieldLayerSet lset = rcBuilder.BuildLayers(geom, cfg);
|
2023-03-14 08:02:43 +03:00
|
|
|
return lset;
|
|
|
|
}
|
2023-04-29 07:29:40 +03:00
|
|
|
}
|