completed ObstacleSampleTool

This commit is contained in:
ikpil 2023-09-02 15:49:10 +09:00
parent d7f9b6e348
commit 2ed8e7a141
3 changed files with 70 additions and 51 deletions

View File

@ -32,7 +32,7 @@ namespace DotRecast.Detour.TileCache
{ {
public class DtTileCacheLayerBuilder public class DtTileCacheLayerBuilder
{ {
private IDtTileCacheCompressorFactory _compFactory; private readonly IDtTileCacheCompressorFactory _compFactory;
public DtTileCacheLayerBuilder(IDtTileCacheCompressorFactory compFactory) public DtTileCacheLayerBuilder(IDtTileCacheCompressorFactory compFactory)
{ {

View File

@ -45,7 +45,13 @@ public class ObstacleSampleTool : ISampleTool
var geom = _sample.GetInputGeom(); var geom = _sample.GetInputGeom();
var settings = _sample.GetSettings(); var settings = _sample.GetSettings();
_tool.Build(geom, settings, RcByteOrder.LITTLE_ENDIAN, true); var buildResult =_tool.Build(geom, settings, RcByteOrder.LITTLE_ENDIAN, true);
if (buildResult.Success)
{
_sample.Update(_sample.GetInputGeom(), buildResult.RecastBuilderResults, buildResult.NavMesh);
_sample.SetChanged(false);
}
} }
if (ImGui.Button("Remove All Temp Obstacles")) if (ImGui.Button("Remove All Temp Obstacles"))

View File

@ -1,8 +1,12 @@
using DotRecast.Core; using System;
using System.Collections.Generic;
using System.Linq;
using DotRecast.Core;
using DotRecast.Detour; using DotRecast.Detour;
using DotRecast.Detour.TileCache; using DotRecast.Detour.TileCache;
using DotRecast.Detour.TileCache.Io.Compress; using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Recast.Geom; using DotRecast.Recast.Geom;
using DotRecast.Recast.Toolset.Builder;
using DotRecast.Recast.Toolset.Geom; using DotRecast.Recast.Toolset.Geom;
namespace DotRecast.Recast.Toolset.Tools namespace DotRecast.Recast.Toolset.Tools
@ -24,14 +28,14 @@ namespace DotRecast.Recast.Toolset.Tools
return "Create Temp Obstacles"; return "Create Temp Obstacles";
} }
public bool Build(IInputGeomProvider geom, RcNavMeshBuildSettings setting, RcByteOrder order, bool cCompatibility) public NavMeshBuildResult Build(IInputGeomProvider geom, RcNavMeshBuildSettings setting, RcByteOrder order, bool cCompatibility)
{ {
DtStatus status; DtStatus status;
if (null == geom || null == geom.GetMesh()) if (null == geom || null == geom.GetMesh())
{ {
//m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: No vertices and triangles."); //m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: No vertices and triangles.");
return false; return new NavMeshBuildResult();
} }
_proc.Init(geom); _proc.Init(geom);
@ -45,56 +49,37 @@ namespace DotRecast.Recast.Toolset.Tools
int th = (gh + ts - 1) / ts; int th = (gh + ts - 1) / ts;
// Generation params. // Generation params.
// RcConfig cfg = new RcConfig(); var walkableRadius = (int)Math.Ceiling(setting.agentRadius / setting.cellSize); // Reserve enough padding.
// cfg.cs = m_cellSize; RcConfig cfg = new RcConfig(
// cfg.ch = m_cellHeight; true, setting.tileSize, setting.tileSize,
// cfg.walkableSlopeAngle = m_agentMaxSlope; walkableRadius + 3,
// cfg.walkableHeight = (int)ceilf(m_agentHeight / cfg.ch); RcPartitionType.OfValue(setting.partitioning),
// cfg.walkableClimb = (int)floorf(m_agentMaxClimb / cfg.ch); setting.cellSize, setting.cellHeight,
// cfg.walkableRadius = (int)ceilf(m_agentRadius / cfg.cs); setting.agentMaxSlope, setting.agentHeight, setting.agentRadius, setting.agentMaxClimb,
// cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize); (int)RcMath.Sqr(setting.minRegionSize), (int)RcMath.Sqr(setting.mergedRegionSize), // Note: area = size*size
// cfg.maxSimplificationError = m_edgeMaxError; (int)(setting.edgeMaxLen / setting.cellSize), setting.edgeMaxError,
// cfg.minRegionArea = (int)rcSqr(m_regionMinSize); // Note: area = size*size setting.vertsPerPoly,
// cfg.mergeRegionArea = (int)rcSqr(m_regionMergeSize); // Note: area = size*size setting.detailSampleDist, setting.detailSampleMaxError,
// cfg.maxVertsPerPoly = (int)m_vertsPerPoly; true, true, true,
// cfg.tileSize = (int)m_tileSize; SampleAreaModifications.SAMPLE_AREAMOD_WALKABLE, true);
// cfg.borderSize = cfg.walkableRadius + 3; // Reserve enough padding.
// cfg.width = cfg.tileSize + cfg.borderSize*2; var builder = new DtTileCacheLayerBuilder(DtTileCacheCompressorFactory.Shared);
// cfg.height = cfg.tileSize + cfg.borderSize*2; var storageParams = new DtTileCacheStorageParams(order, cCompatibility);
// cfg.detailSampleDist = m_detailSampleDist < 0.9f ? 0 : m_cellSize * m_detailSampleDist; var results = builder.Build(geom, cfg, storageParams, 8, tw, th);
// cfg.detailSampleMaxError = m_cellHeight * m_detailSampleMaxError; var layers = results
// rcVcopy(cfg.bmin, bmin); .SelectMany(x => x.layers)
// rcVcopy(cfg.bmax, bmax); .ToList();
_tc = CreateTileCache(geom, setting, tw, th, order, cCompatibility); _tc = CreateTileCache(geom, setting, tw, th, order, cCompatibility);
for (int y = 0; y < th; ++y) for (int i = 0; i < layers.Count; ++i)
{ {
for (int x = 0; x < tw; ++x) var layer = layers[i];
{ var refs = _tc.AddTile(layer, 0);
// TileCacheData tiles[MAX_LAYERS]; _tc.BuildNavMeshTile(refs);
// memset(tiles, 0, sizeof(tiles));
// int ntiles = rasterizeTileLayers(x, y, cfg, tiles, MAX_LAYERS);
//
// for (int i = 0; i < ntiles; ++i)
// {
// TileCacheData* tile = &tiles[i];
// status = m_tileCache->addTile(tile->data, tile->dataSize, DT_COMPRESSEDTILE_FREE_DATA, 0);
// if (dtStatusFailed(status))
// {
// dtFree(tile->data);
// tile->data = 0;
// continue;
// }
//
// m_cacheLayerCount++;
// m_cacheCompressedSize += tile->dataSize;
// m_cacheRawSize += calcLayerBufferSize(tcparams.width, tcparams.height);
// }
}
} }
return true; return new NavMeshBuildResult(RcImmutableArray<RecastBuilderResult>.Empty, _tc.GetNavMesh());
} }
public void ClearAllTempObstacles() public void ClearAllTempObstacles()
@ -117,8 +102,8 @@ namespace DotRecast.Recast.Toolset.Tools
if (null == _tc) if (null == _tc)
return; return;
//DtObstacleRef refs = hitTestObstacle(m_tileCache, sp, sq); long refs = HitTestObstacle(sp, sq);
//_tc.RemoveObstacle(refs); _tc.RemoveObstacle(refs);
} }
public long AddTempObstacle(RcVec3f p) public long AddTempObstacle(RcVec3f p)
@ -163,5 +148,33 @@ namespace DotRecast.Recast.Toolset.Tools
DtTileCache tc = new DtTileCache(option, storageParams, navMesh, comp, _proc); DtTileCache tc = new DtTileCache(option, storageParams, navMesh, comp, _proc);
return tc; return tc;
} }
public long HitTestObstacle(RcVec3f sp, RcVec3f sq)
{
float tmin = float.MaxValue;
DtTileCacheObstacle obmin = null;
for (int i = 0; i < _tc.GetObstacleCount(); ++i)
{
DtTileCacheObstacle ob = _tc.GetObstacle(i);
if (ob.state == DtObstacleState.DT_OBSTACLE_EMPTY)
continue;
RcVec3f bmin = RcVec3f.Zero;
RcVec3f bmax = RcVec3f.Zero;
_tc.GetObstacleBounds(ob, ref bmin, ref bmax);
if (Intersections.IsectSegAABB(sp, sq, bmin, bmax, out var t0, out var t1))
{
if (t0 < tmin)
{
tmin = t0;
obmin = ob;
}
}
}
return _tc.GetObstacleRef(obmin);
}
} }
} }