DtTileCacheLayerBuilder.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. using System.Collections.Concurrent;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Threading.Tasks;
  24. using DotRecast.Core;
  25. using DotRecast.Detour.TileCache.Io.Compress;
  26. using DotRecast.Recast;
  27. using DotRecast.Recast.Geom;
  28. namespace DotRecast.Detour.TileCache
  29. {
  30. public class DtTileCacheLayerBuilder
  31. {
  32. private readonly IDtTileCacheCompressorFactory _compFactory;
  33. public DtTileCacheLayerBuilder(IDtTileCacheCompressorFactory compFactory)
  34. {
  35. _compFactory = compFactory;
  36. }
  37. public List<DtTileCacheLayerBuildResult> Build(IInputGeomProvider geom, RcConfig cfg, DtTileCacheStorageParams storageParams, int threads, int tw, int th)
  38. {
  39. if (threads == 1)
  40. {
  41. return BuildSingleThread(geom, cfg, storageParams, tw, th);
  42. }
  43. return BuildMultiThread(geom, cfg, storageParams, tw, th, threads);
  44. }
  45. private List<DtTileCacheLayerBuildResult> BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, DtTileCacheStorageParams storageParams, int tw, int th)
  46. {
  47. var results = new List<DtTileCacheLayerBuildResult>();
  48. for (int y = 0; y < th; ++y)
  49. {
  50. for (int x = 0; x < tw; ++x)
  51. {
  52. var result = BuildTileCacheLayer(geom, cfg, x, y, storageParams);
  53. results.Add(result);
  54. }
  55. }
  56. return results;
  57. }
  58. private List<DtTileCacheLayerBuildResult> BuildMultiThread(IInputGeomProvider geom, RcConfig cfg, DtTileCacheStorageParams storageParams, int tw, int th, int threads)
  59. {
  60. var results = new List<Task<DtTileCacheLayerBuildResult>>();
  61. for (int y = 0; y < th; ++y)
  62. {
  63. for (int x = 0; x < tw; ++x)
  64. {
  65. int tx = x;
  66. int ty = y;
  67. var task = Task.Run(() => BuildTileCacheLayer(geom, cfg, tx, ty, storageParams));
  68. results.Add(task);
  69. }
  70. }
  71. return results
  72. .Select(x => x.Result)
  73. .ToList();
  74. }
  75. protected virtual RcHeightfieldLayerSet BuildHeightfieldLayerSet(IInputGeomProvider geom, RcConfig cfg, int tx, int ty)
  76. {
  77. RecastBuilder rcBuilder = new RecastBuilder();
  78. RcVec3f bmin = geom.GetMeshBoundsMin();
  79. RcVec3f bmax = geom.GetMeshBoundsMax();
  80. RecastBuilderConfig builderCfg = new RecastBuilderConfig(cfg, bmin, bmax, tx, ty);
  81. RcHeightfieldLayerSet lset = rcBuilder.BuildLayers(geom, builderCfg);
  82. return lset;
  83. }
  84. protected virtual DtTileCacheLayerBuildResult BuildTileCacheLayer(IInputGeomProvider geom, RcConfig cfg, int tx, int ty, DtTileCacheStorageParams storageParams)
  85. {
  86. RcHeightfieldLayerSet lset = BuildHeightfieldLayerSet(geom, cfg, tx, ty);
  87. List<byte[]> result = new List<byte[]>();
  88. if (lset != null)
  89. {
  90. DtTileCacheBuilder builder = new DtTileCacheBuilder();
  91. for (int i = 0; i < lset.layers.Length; ++i)
  92. {
  93. RcHeightfieldLayer layer = lset.layers[i];
  94. // Store header
  95. DtTileCacheLayerHeader header = new DtTileCacheLayerHeader();
  96. header.magic = DtTileCacheLayerHeader.DT_TILECACHE_MAGIC;
  97. header.version = DtTileCacheLayerHeader.DT_TILECACHE_VERSION;
  98. // Tile layer location in the navmesh.
  99. header.tx = tx;
  100. header.ty = ty;
  101. header.tlayer = i;
  102. header.bmin = layer.bmin;
  103. header.bmax = layer.bmax;
  104. // Tile info.
  105. header.width = layer.width;
  106. header.height = layer.height;
  107. header.minx = layer.minx;
  108. header.maxx = layer.maxx;
  109. header.miny = layer.miny;
  110. header.maxy = layer.maxy;
  111. header.hmin = layer.hmin;
  112. header.hmax = layer.hmax;
  113. var comp = _compFactory.Create(storageParams.Compatibility ? 0 : 1);
  114. var bytes = builder.CompressTileCacheLayer(header, layer.heights, layer.areas, layer.cons, storageParams.Order, storageParams.Compatibility, comp);
  115. result.Add(bytes);
  116. }
  117. }
  118. return new DtTileCacheLayerBuildResult(tx, ty, result);
  119. }
  120. }
  121. }