From 2050c476a2d6414fdfeaf4f4ae9e4791e75629ba Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 19 Aug 2023 14:55:45 +0900 Subject: [PATCH] add demo tile cache comp factory --- .../DtTileCacheCompressorDemoFactory.cs | 21 +++++++ .../DtTileCacheLZ4DemoCompressor.cs | 49 ++++++++++++++++ .../Tools/ObstacleTool.cs | 6 +- .../Geom/DemoDtTileCacheMeshProcess.cs | 57 +++++++++++++++++++ .../Geom/DemoOffMeshConnection.cs | 1 + .../Tools/ObstacleToolImpl.cs | 51 ++++++++++++++++- 6 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 src/DotRecast.Recast.Demo/DtTileCacheCompressorDemoFactory.cs create mode 100644 src/DotRecast.Recast.Demo/DtTileCacheLZ4DemoCompressor.cs create mode 100644 src/DotRecast.Recast.Toolset/Geom/DemoDtTileCacheMeshProcess.cs diff --git a/src/DotRecast.Recast.Demo/DtTileCacheCompressorDemoFactory.cs b/src/DotRecast.Recast.Demo/DtTileCacheCompressorDemoFactory.cs new file mode 100644 index 0000000..361b90e --- /dev/null +++ b/src/DotRecast.Recast.Demo/DtTileCacheCompressorDemoFactory.cs @@ -0,0 +1,21 @@ +using DotRecast.Core; +using DotRecast.Detour.TileCache.Io.Compress; + +namespace DotRecast.Recast.Demo; + +public class DtTileCacheCompressorDemoFactory : IDtTileCacheCompressorFactory +{ + public static readonly DtTileCacheCompressorDemoFactory Shared = new(); + + private DtTileCacheCompressorDemoFactory() + { + } + + public IRcCompressor Get(bool cCompatibility) + { + if (cCompatibility) + return DtTileCacheFastLzCompressor.Shared; + + return DtTileCacheLZ4DemoCompressor.Shared; + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/DtTileCacheLZ4DemoCompressor.cs b/src/DotRecast.Recast.Demo/DtTileCacheLZ4DemoCompressor.cs new file mode 100644 index 0000000..6056687 --- /dev/null +++ b/src/DotRecast.Recast.Demo/DtTileCacheLZ4DemoCompressor.cs @@ -0,0 +1,49 @@ +/* +Copyright (c) 2009-2010 Mikko Mononen memon@inside.org +recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org +DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com + +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 DotRecast.Core; +using K4os.Compression.LZ4; + +namespace DotRecast.Recast.Demo +{ + public class DtTileCacheLZ4DemoCompressor : IRcCompressor + { + public static readonly DtTileCacheLZ4DemoCompressor Shared = new(); + + private DtTileCacheLZ4DemoCompressor() + { + } + + public byte[] Decompress(byte[] buf) + { + return LZ4Pickler.Unpickle(buf); + } + + public byte[] Decompress(byte[] buf, int offset, int len, int outputlen) + { + return LZ4Pickler.Unpickle(buf, offset, len); + } + + public byte[] Compress(byte[] buf) + { + return LZ4Pickler.Pickle(buf); + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Tools/ObstacleTool.cs b/src/DotRecast.Recast.Demo/Tools/ObstacleTool.cs index 43304fd..1293007 100644 --- a/src/DotRecast.Recast.Demo/Tools/ObstacleTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/ObstacleTool.cs @@ -16,7 +16,7 @@ public class ObstacleTool : IRcTool public ObstacleTool() { - _impl = new(); + _impl = new(DtTileCacheCompressorDemoFactory.Shared); } public ISampleTool GetTool() @@ -30,9 +30,9 @@ public class ObstacleTool : IRcTool public void Layout() { - if (ImGui.Button("Remove All")) + if (ImGui.Button("Remove All Temp Obstacles")) { - //m_sample->clearAllTempObstacles(); + _impl.ClearAllTempObstacles(); } ImGui.Separator(); diff --git a/src/DotRecast.Recast.Toolset/Geom/DemoDtTileCacheMeshProcess.cs b/src/DotRecast.Recast.Toolset/Geom/DemoDtTileCacheMeshProcess.cs new file mode 100644 index 0000000..03eca7c --- /dev/null +++ b/src/DotRecast.Recast.Toolset/Geom/DemoDtTileCacheMeshProcess.cs @@ -0,0 +1,57 @@ +using DotRecast.Detour; +using DotRecast.Detour.TileCache; +using DotRecast.Recast.Geom; + +namespace DotRecast.Recast.Toolset.Geom +{ + public class DemoDtTileCacheMeshProcess : IDtTileCacheMeshProcess + { + private IInputGeomProvider _geom; + + public DemoDtTileCacheMeshProcess() + { + } + + public void Init(IInputGeomProvider geom) + { + _geom = geom; + } + + public void Process(DtNavMeshCreateParams option) + { + // // Update poly flags from areas. + // for (int i = 0; i < option.polyCount; ++i) + // { + // if (option.polyAreas[i] == DtTileCacheBuilder.DT_TILECACHE_WALKABLE_AREA) + // option.polyAreas[i] = SAMPLE_POLYAREA_GROUND; + // + // if (option.polyAreas[i] == SAMPLE_POLYAREA_GROUND || + // option.polyAreas[i] == SAMPLE_POLYAREA_GRASS || + // option.polyAreas[i] == SAMPLE_POLYAREA_ROAD) + // { + // option.polyFlags[i] = SAMPLE_POLYFLAGS_WALK; + // } + // else if (option.polyAreas[i] == SAMPLE_POLYAREA_WATER) + // { + // option.polyFlags[i] = SAMPLE_POLYFLAGS_SWIM; + // } + // else if (option.polyAreas[i] == SAMPLE_POLYAREA_DOOR) + // { + // option.polyFlags[i] = SAMPLE_POLYFLAGS_WALK | SAMPLE_POLYFLAGS_DOOR; + // } + // } + // + // // Pass in off-mesh connections. + // if (null != _geom) + // { + // option.offMeshConVerts = _geom.GetOffMeshConnectionVerts(); + // option.offMeshConRad = _geom.GetOffMeshConnectionRads(); + // option.offMeshConDir = _geom.GetOffMeshConnectionDirs(); + // option.offMeshConAreas = _geom.GetOffMeshConnectionAreas(); + // option.offMeshConFlags = _geom.GetOffMeshConnectionFlags(); + // option.offMeshConUserID = _geom.GetOffMeshConnectionId(); + // option.offMeshConCount = _geom.GetOffMeshConnectionCount(); + // } + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Toolset/Geom/DemoOffMeshConnection.cs b/src/DotRecast.Recast.Toolset/Geom/DemoOffMeshConnection.cs index d0d90ab..bfe790e 100644 --- a/src/DotRecast.Recast.Toolset/Geom/DemoOffMeshConnection.cs +++ b/src/DotRecast.Recast.Toolset/Geom/DemoOffMeshConnection.cs @@ -19,6 +19,7 @@ freely, subject to the following restrictions: */ using DotRecast.Core; +using DotRecast.Detour; namespace DotRecast.Recast.Toolset.Geom { diff --git a/src/DotRecast.Recast.Toolset/Tools/ObstacleToolImpl.cs b/src/DotRecast.Recast.Toolset/Tools/ObstacleToolImpl.cs index 2ffd72f..a0d5456 100644 --- a/src/DotRecast.Recast.Toolset/Tools/ObstacleToolImpl.cs +++ b/src/DotRecast.Recast.Toolset/Tools/ObstacleToolImpl.cs @@ -1,12 +1,25 @@ using DotRecast.Core; +using DotRecast.Detour; using DotRecast.Detour.TileCache; +using DotRecast.Detour.TileCache.Io.Compress; +using DotRecast.Recast.Geom; +using DotRecast.Recast.Toolset.Geom; namespace DotRecast.Recast.Toolset.Tools { public class ObstacleToolImpl : ISampleTool { private Sample _sample; - + private readonly IDtTileCacheCompressorFactory _compFactory; + private readonly IDtTileCacheMeshProcess _mProc; + private DtTileCache _tc; + + public ObstacleToolImpl(IDtTileCacheCompressorFactory compFactory, IDtTileCacheMeshProcess meshProcessor = null) + { + _compFactory = compFactory; + _mProc = meshProcessor ?? new DemoDtTileCacheMeshProcess(); + } + public string GetName() { return "Create Temp Obstacles"; @@ -22,6 +35,10 @@ namespace DotRecast.Recast.Toolset.Tools return _sample; } + public void ClearAllTempObstacles() + { + } + public void RemoveTempObstacle(RcVec3f sp, RcVec3f sq) { // .. @@ -32,5 +49,37 @@ namespace DotRecast.Recast.Toolset.Tools //p[1] -= 0.5f; //m_tileCache->addObstacle(p, 1.0f, 2.0f, 0); } + + public DtTileCache GetTileCache(IInputGeomProvider geom, RcByteOrder order, bool cCompatibility) + { + // DtTileCacheParams option = new DtTileCacheParams(); + // RcUtils.CalcTileCount(geom.GetMeshBoundsMin(), geom.GetMeshBoundsMax(), m_cellSize, m_tileSize, m_tileSize, out var tw, out var th); + // option.ch = m_cellHeight; + // option.cs = m_cellSize; + // option.orig = geom.GetMeshBoundsMin(); + // option.height = m_tileSize; + // option.width = m_tileSize; + // option.walkableHeight = m_agentHeight; + // option.walkableRadius = m_agentRadius; + // option.walkableClimb = m_agentMaxClimb; + // option.maxSimplificationError = m_edgeMaxError; + // option.maxTiles = tw * th * EXPECTED_LAYERS_PER_TILE; + // option.maxObstacles = 128; + // + // DtNavMeshParams navMeshParams = new DtNavMeshParams(); + // navMeshParams.orig = geom.GetMeshBoundsMin(); + // navMeshParams.tileWidth = m_tileSize * m_cellSize; + // navMeshParams.tileHeight = m_tileSize * m_cellSize; + // navMeshParams.maxTiles = 256; + // navMeshParams.maxPolys = 16384; + // + // var navMesh = new DtNavMesh(navMeshParams, 6); + // var comp = _compFactory.Get(cCompatibility); + // var storageParams = new DtTileCacheStorageParams(order, cCompatibility); + // var process = new TestTileCacheMeshProcess(); + // DtTileCache tc = new DtTileCache(option, storageParams, navMesh, comp, process); + // return tc; + return null; + } } } \ No newline at end of file