added tile tool

This commit is contained in:
ikpil 2023-07-27 15:39:21 +09:00
parent afc515c498
commit 60850b0cbf
4 changed files with 78 additions and 23 deletions

View File

@ -1,14 +1,17 @@
using DotRecast.Core; using System;
using DotRecast.Core;
using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.DemoTool; using DotRecast.Recast.DemoTool;
using DotRecast.Recast.DemoTool.Tools; using DotRecast.Recast.DemoTool.Tools;
using ImGuiNET; using ImGuiNET;
using Serilog;
using static DotRecast.Recast.Demo.Draw.DebugDraw; using static DotRecast.Recast.Demo.Draw.DebugDraw;
namespace DotRecast.Recast.Demo.Tools; namespace DotRecast.Recast.Demo.Tools;
public class TileTool : IRcTool public class TileTool : IRcTool
{ {
private static readonly ILogger Logger = Log.ForContext<TileTool>();
private readonly TileToolImpl _impl; private readonly TileToolImpl _impl;
private bool _hitPosSet; private bool _hitPosSet;
@ -58,7 +61,8 @@ public class TileTool : IRcTool
} }
else else
{ {
_impl.BuildTile(_hitPos); _impl.BuildTile(_hitPos, out var tileBuildTicks, out var tileTriCount, out var tileMemUsage);
Logger.Information($"{tileBuildTicks / (float)TimeSpan.TicksPerMillisecond}ms / {tileTriCount}Tris / {tileMemUsage}kB ");
} }
} }
@ -80,7 +84,7 @@ public class TileTool : IRcTool
var settings = sample.GetSettings(); var settings = sample.GetSettings();
var s = settings.agentRadius; var s = settings.agentRadius;
float ts = settings.tileSize * settings.cellSize; float ts = settings.tileSize * settings.cellSize;
int tx = (int)((_hitPos.x - bmin[0]) / ts); int tx = (int)((_hitPos.x - bmin[0]) / ts);
int ty = (int)((_hitPos.z - bmin[2]) / ts); int ty = (int)((_hitPos.z - bmin[2]) / ts);
@ -101,6 +105,8 @@ public class TileTool : IRcTool
lastBuiltTileBmin.x, lastBuiltTileBmin.y, lastBuiltTileBmin.z, lastBuiltTileBmin.x, lastBuiltTileBmin.y, lastBuiltTileBmin.z,
lastBuiltTileBmax.x, lastBuiltTileBmax.y, lastBuiltTileBmax.z, lastBuiltTileBmax.x, lastBuiltTileBmax.y, lastBuiltTileBmax.z,
DuRGBA(255, 255, 255, 64), 1.0f); DuRGBA(255, 255, 255, 64), 1.0f);
// 표기
} }
} }

View File

@ -89,8 +89,8 @@ namespace DotRecast.Recast.DemoTool.Builder
return navMesh; return navMesh;
} }
private List<DtMeshData> BuildMeshData(DemoInputGeomProvider geom, float cellSize, float cellHeight, float agentHeight, public List<DtMeshData> BuildMeshData(DemoInputGeomProvider geom, float cellSize, float cellHeight, float agentHeight,
float agentRadius, float agentMaxClimb, List<RecastBuilderResult> results) float agentRadius, float agentMaxClimb, IList<RecastBuilderResult> results)
{ {
// Add tiles to nav mesh // Add tiles to nav mesh
List<DtMeshData> meshData = new List<DtMeshData>(); List<DtMeshData> meshData = new List<DtMeshData>();

View File

@ -1,4 +1,7 @@
using DotRecast.Core; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using DotRecast.Core;
using DotRecast.Detour.TileCache; using DotRecast.Detour.TileCache;
using DotRecast.Recast.DemoTool.Builder; using DotRecast.Recast.DemoTool.Builder;
@ -7,12 +10,12 @@ namespace DotRecast.Recast.DemoTool.Tools
public class TileToolImpl : ISampleTool public class TileToolImpl : ISampleTool
{ {
private Sample _sample; private Sample _sample;
public string GetName() public string GetName()
{ {
return "Create Tiles"; return "Create Tiles";
} }
public void SetSample(Sample sample) public void SetSample(Sample sample)
{ {
_sample = sample; _sample = sample;
@ -23,40 +26,84 @@ namespace DotRecast.Recast.DemoTool.Tools
return _sample; return _sample;
} }
public void BuildTile(RcVec3f pos) public bool BuildTile(RcVec3f pos, out long tileBuildTicks, out int tileTriCount, out int tileMemUsage)
{ {
var settings = _sample.GetSettings(); var settings = _sample.GetSettings();
var geom = _sample.GetInputGeom(); var geom = _sample.GetInputGeom();
var navMesh = _sample.GetNavMesh(); var navMesh = _sample.GetNavMesh();
tileBuildTicks = 0;
tileTriCount = 0;
tileMemUsage = 0;
if (null == settings || null == geom || navMesh == null) if (null == settings || null == geom || navMesh == null)
return; return false;
float ts = settings.tileSize * settings.cellSize; float ts = settings.tileSize * settings.cellSize;
var bmin = geom.GetMeshBoundsMin(); RcVec3f bmin = geom.GetMeshBoundsMin();
RcVec3f bmax = geom.GetMeshBoundsMax();
int tx = (int)((pos.x - bmin[0]) / ts); int tx = (int)((pos.x - bmin[0]) / ts);
int ty = (int)((pos.z - bmin[2]) / ts); int ty = (int)((pos.z - bmin[2]) / ts);
// var tnmb = new TileNavMeshBuilder(); RcConfig cfg = new RcConfig(
// tnmb.BuildRecastResult(geom, settings.partitioning, ) true,
settings.tileSize,
settings.tileSize,
RcConfig.CalcBorder(settings.agentRadius, settings.cellSize),
settings.partitioning,
settings.cellSize,
settings.cellHeight,
settings.agentMaxSlope,
settings.filterLowHangingObstacles,
settings.filterLedgeSpans,
settings.filterWalkableLowHeightSpans,
settings.agentHeight,
settings.agentRadius,
settings.agentMaxClimb,
settings.minRegionSize * settings.minRegionSize * settings.cellSize * settings.cellSize,
settings.mergedRegionSize * settings.mergedRegionSize * settings.cellSize * settings.cellSize,
settings.edgeMaxLen,
settings.edgeMaxError,
settings.vertsPerPoly,
true,
settings.detailSampleDist,
settings.detailSampleMaxError,
SampleAreaModifications.SAMPLE_AREAMOD_WALKABLE
);
//var tileRef = navMesh.GetTileRefAt(tx, ty, 0); var rb = new RecastBuilder();
// navMesh.RemoveTile(tileRef); var result = rb.BuildTile(geom, cfg, bmin, bmax, tx, ty, new RcAtomicInteger(0), 1);
var tb = new TileNavMeshBuilder();
var meshData = tb.BuildMeshData(geom,
settings.cellSize, settings.cellHeight, settings.agentHeight, settings.agentRadius, settings.agentMaxClimb,
ImmutableArray.Create(result)
).First();
navMesh.UpdateTile(meshData, 0);
var telemetry = result.GetTelemetry();
tileBuildTicks = telemetry.ToList().Sum(x => x.Ticks);
tileTriCount = 0; // ...
tileMemUsage = 0; // ...
return true;
} }
public void RemoveTile(RcVec3f pos) public bool RemoveTile(RcVec3f pos)
{ {
var settings = _sample.GetSettings(); var settings = _sample.GetSettings();
var geom = _sample.GetInputGeom(); var geom = _sample.GetInputGeom();
var navMesh = _sample.GetNavMesh(); var navMesh = _sample.GetNavMesh();
if (null == settings || null == geom || navMesh == null) if (null == settings || null == geom || navMesh == null)
return; return false;
float ts = settings.tileSize * settings.cellSize; float ts = settings.tileSize * settings.cellSize;
var bmin = geom.GetMeshBoundsMin(); var bmin = geom.GetMeshBoundsMin();
int tx = (int)((pos.x - bmin[0]) / ts); int tx = (int)((pos.x - bmin[0]) / ts);
@ -64,6 +111,8 @@ namespace DotRecast.Recast.DemoTool.Tools
var tileRef = navMesh.GetTileRefAt(tx, ty, 0); var tileRef = navMesh.GetTileRefAt(tx, ty, 0);
navMesh.RemoveTile(tileRef); navMesh.RemoveTile(tileRef);
return true;
} }
} }
} }

View File

@ -143,7 +143,7 @@ namespace DotRecast.Recast
return Task.WhenAll(tasks.ToArray()); return Task.WhenAll(tasks.ToArray());
} }
private RecastBuilderResult BuildTile(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tx, public RecastBuilderResult BuildTile(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tx,
int ty, RcAtomicInteger counter, int total) int ty, RcAtomicInteger counter, int total)
{ {
RecastBuilderResult result = Build(geom, new RecastBuilderConfig(cfg, bmin, bmax, tx, ty)); RecastBuilderResult result = Build(geom, new RecastBuilderConfig(cfg, bmin, bmax, tx, ty));