DynamicTile.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
  3. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. using System;
  19. using System.Collections.Concurrent;
  20. using System.Collections.Generic;
  21. using System.Collections.ObjectModel;
  22. using System.Linq;
  23. using DotRecast.Core;
  24. using DotRecast.Detour.Dynamic.Colliders;
  25. using DotRecast.Detour.Dynamic.Io;
  26. using DotRecast.Recast;
  27. namespace DotRecast.Detour.Dynamic
  28. {
  29. public class DynamicTile
  30. {
  31. public readonly VoxelTile voxelTile;
  32. public DynamicTileCheckpoint checkpoint;
  33. public RecastBuilderResult recastResult;
  34. private DtMeshData meshData;
  35. private readonly ConcurrentDictionary<long, ICollider> colliders = new ConcurrentDictionary<long, ICollider>();
  36. private bool dirty = true;
  37. private long id;
  38. public DynamicTile(VoxelTile voxelTile)
  39. {
  40. this.voxelTile = voxelTile;
  41. }
  42. public bool Build(RecastBuilder builder, DynamicNavMeshConfig config, RcTelemetry telemetry)
  43. {
  44. if (dirty)
  45. {
  46. RcHeightfield heightfield = BuildHeightfield(config, telemetry);
  47. RecastBuilderResult r = BuildRecast(builder, config, voxelTile, heightfield, telemetry);
  48. DtNavMeshCreateParams option = NavMeshCreateParams(voxelTile.tileX, voxelTile.tileZ, voxelTile.cellSize,
  49. voxelTile.cellHeight, config, r);
  50. meshData = DtNavMeshBuilder.CreateNavMeshData(option);
  51. return true;
  52. }
  53. return false;
  54. }
  55. private RcHeightfield BuildHeightfield(DynamicNavMeshConfig config, RcTelemetry telemetry)
  56. {
  57. ICollection<long> rasterizedColliders = checkpoint != null
  58. ? checkpoint.colliders as ICollection<long>
  59. : RcImmutableArray<long>.Empty;
  60. RcHeightfield heightfield = checkpoint != null
  61. ? checkpoint.heightfield
  62. : voxelTile.Heightfield();
  63. foreach (var (cid, c) in colliders)
  64. {
  65. if (!rasterizedColliders.Contains(cid))
  66. {
  67. heightfield.bmax.y = Math.Max(heightfield.bmax.y, c.Bounds()[4] + heightfield.ch * 2);
  68. c.Rasterize(heightfield, telemetry);
  69. }
  70. }
  71. if (config.enableCheckpoints)
  72. {
  73. checkpoint = new DynamicTileCheckpoint(heightfield, colliders.Keys.ToHashSet());
  74. }
  75. return heightfield;
  76. }
  77. private RecastBuilderResult BuildRecast(RecastBuilder builder, DynamicNavMeshConfig config, VoxelTile vt,
  78. RcHeightfield heightfield, RcTelemetry telemetry)
  79. {
  80. RcConfig rcConfig = new RcConfig(
  81. config.useTiles, config.tileSizeX, config.tileSizeZ,
  82. vt.borderSize,
  83. RcPartitionType.OfValue(config.partition),
  84. vt.cellSize, vt.cellHeight,
  85. config.walkableSlopeAngle, config.walkableHeight, config.walkableRadius, config.walkableClimb,
  86. config.minRegionArea, config.regionMergeArea,
  87. config.maxEdgeLen, config.maxSimplificationError,
  88. Math.Min(DynamicNavMesh.MAX_VERTS_PER_POLY, config.vertsPerPoly),
  89. config.detailSampleDistance, config.detailSampleMaxError,
  90. true, true, true, null, true);
  91. RecastBuilderResult r = builder.Build(vt.tileX, vt.tileZ, null, rcConfig, heightfield, telemetry);
  92. if (config.keepIntermediateResults)
  93. {
  94. recastResult = r;
  95. }
  96. return r;
  97. }
  98. public void AddCollider(long cid, ICollider collider)
  99. {
  100. colliders[cid] = collider;
  101. dirty = true;
  102. }
  103. public bool ContainsCollider(long cid)
  104. {
  105. return colliders.ContainsKey(cid);
  106. }
  107. public void RemoveCollider(long colliderId)
  108. {
  109. if (colliders.TryRemove(colliderId, out var collider))
  110. {
  111. dirty = true;
  112. checkpoint = null;
  113. }
  114. }
  115. private DtNavMeshCreateParams NavMeshCreateParams(int tilex, int tileZ, float cellSize, float cellHeight,
  116. DynamicNavMeshConfig config, RecastBuilderResult rcResult)
  117. {
  118. RcPolyMesh m_pmesh = rcResult.GetMesh();
  119. RcPolyMeshDetail m_dmesh = rcResult.GetMeshDetail();
  120. DtNavMeshCreateParams option = new DtNavMeshCreateParams();
  121. for (int i = 0; i < m_pmesh.npolys; ++i)
  122. {
  123. m_pmesh.flags[i] = 1;
  124. }
  125. option.tileX = tilex;
  126. option.tileZ = tileZ;
  127. option.verts = m_pmesh.verts;
  128. option.vertCount = m_pmesh.nverts;
  129. option.polys = m_pmesh.polys;
  130. option.polyAreas = m_pmesh.areas;
  131. option.polyFlags = m_pmesh.flags;
  132. option.polyCount = m_pmesh.npolys;
  133. option.nvp = m_pmesh.nvp;
  134. if (m_dmesh != null)
  135. {
  136. option.detailMeshes = m_dmesh.meshes;
  137. option.detailVerts = m_dmesh.verts;
  138. option.detailVertsCount = m_dmesh.nverts;
  139. option.detailTris = m_dmesh.tris;
  140. option.detailTriCount = m_dmesh.ntris;
  141. }
  142. option.walkableHeight = config.walkableHeight;
  143. option.walkableRadius = config.walkableRadius;
  144. option.walkableClimb = config.walkableClimb;
  145. option.bmin = m_pmesh.bmin;
  146. option.bmax = m_pmesh.bmax;
  147. option.cs = cellSize;
  148. option.ch = cellHeight;
  149. option.buildBvTree = true;
  150. option.offMeshConCount = 0;
  151. option.offMeshConVerts = new float[0];
  152. option.offMeshConRad = new float[0];
  153. option.offMeshConDir = new int[0];
  154. option.offMeshConAreas = new int[0];
  155. option.offMeshConFlags = new int[0];
  156. option.offMeshConUserID = new int[0];
  157. return option;
  158. }
  159. public void AddTo(DtNavMesh navMesh)
  160. {
  161. if (meshData != null)
  162. {
  163. id = navMesh.AddTile(meshData, 0, 0);
  164. }
  165. else
  166. {
  167. navMesh.RemoveTile(id);
  168. id = 0;
  169. }
  170. }
  171. }
  172. }