DotRecastNetSim/test/DotRecast.Detour.TileCache..../TestTileLayerBuilder.cs

129 lines
5.1 KiB
C#
Raw Normal View History

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-03-25 09:43:20 +03:00
using static DotRecast.Core.RecastMath;
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;
protected readonly InputGeomProvider geom;
private readonly int tw;
private readonly int th;
2023-03-16 19:48:49 +03:00
public TestTileLayerBuilder(InputGeomProvider geom)
{
2023-03-14 08:02:43 +03:00
this.geom = geom;
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-03-30 19:10:04 +03:00
Vector3f bmin = geom.getMeshBoundsMin();
Vector3f bmax = geom.getMeshBoundsMax();
2023-03-14 08:02:43 +03:00
int[] twh = Recast.Recast.calcTileCount(bmin, bmax, m_cellSize, m_tileSize, m_tileSize);
tw = twh[0];
th = twh[1];
}
2023-03-16 19:48:49 +03:00
public List<byte[]> build(ByteOrder order, bool cCompatibility, int threads)
{
2023-03-14 08:02:43 +03:00
return build(order, cCompatibility, threads, tw, th);
}
2023-03-16 19:48:49 +03:00
public int getTw()
{
2023-03-14 08:02:43 +03:00
return tw;
}
2023-03-16 19:48:49 +03:00
public int getTh()
{
2023-03-14 08:02:43 +03:00
return th;
}
2023-03-16 19:48:49 +03:00
protected override List<byte[]> build(int tx, int ty, ByteOrder order, bool cCompatibility)
{
2023-03-14 08:02:43 +03:00
HeightfieldLayerSet lset = getHeightfieldSet(tx, ty);
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-03-14 08:02:43 +03:00
HeightfieldLayerSet.HeightfieldLayer layer = lset.layers[i];
// 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;
result.Add(builder.compressTileCacheLayer(header, layer.heights, layer.areas, layer.cons, order, cCompatibility));
}
}
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
return result;
}
2023-03-16 19:48:49 +03:00
protected HeightfieldLayerSet getHeightfieldSet(int tx, int ty)
{
2023-03-14 08:02:43 +03:00
RecastBuilder rcBuilder = new RecastBuilder();
2023-03-30 19:10:04 +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);
HeightfieldLayerSet lset = rcBuilder.buildLayers(geom, cfg);
return lset;
}
2023-03-16 19:48:49 +03:00
}