VoxelFile.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Generic;
  20. using DotRecast.Core;
  21. using DotRecast.Recast;
  22. namespace DotRecast.Detour.Dynamic.Io
  23. {
  24. public class VoxelFile
  25. {
  26. public static readonly RcByteOrder PREFERRED_BYTE_ORDER = RcByteOrder.BIG_ENDIAN;
  27. public const int MAGIC = 'V' << 24 | 'O' << 16 | 'X' << 8 | 'L';
  28. public const int VERSION_EXPORTER_MASK = 0xF000;
  29. public const int VERSION_COMPRESSION_MASK = 0x0F00;
  30. public const int VERSION_EXPORTER_RECAST4J = 0x1000;
  31. public const int VERSION_COMPRESSION_LZ4 = 0x0100;
  32. public int version;
  33. public int partition = RcPartitionType.WATERSHED.Value;
  34. public bool filterLowHangingObstacles = true;
  35. public bool filterLedgeSpans = true;
  36. public bool filterWalkableLowHeightSpans = true;
  37. public float walkableRadius;
  38. public float walkableHeight;
  39. public float walkableClimb;
  40. public float walkableSlopeAngle;
  41. public float cellSize;
  42. public float maxSimplificationError;
  43. public float maxEdgeLen;
  44. public float minRegionArea;
  45. public float regionMergeArea;
  46. public int vertsPerPoly;
  47. public bool buildMeshDetail;
  48. public float detailSampleDistance;
  49. public float detailSampleMaxError;
  50. public bool useTiles;
  51. public int tileSizeX;
  52. public int tileSizeZ;
  53. public RcVec3f rotation = new RcVec3f();
  54. public float[] bounds = new float[6];
  55. public readonly List<VoxelTile> tiles = new List<VoxelTile>();
  56. public void AddTile(VoxelTile tile)
  57. {
  58. tiles.Add(tile);
  59. }
  60. public RcConfig GetConfig(VoxelTile tile, RcAreaModification walkbableAreaMod, bool buildMeshDetail)
  61. {
  62. return new RcConfig(useTiles, tileSizeX, tileSizeZ,
  63. tile.borderSize,
  64. RcPartitionType.OfValue(partition),
  65. cellSize, tile.cellHeight,
  66. walkableSlopeAngle, walkableHeight, walkableRadius, walkableClimb,
  67. minRegionArea, regionMergeArea,
  68. maxEdgeLen, maxSimplificationError,
  69. vertsPerPoly,
  70. detailSampleDistance, detailSampleMaxError,
  71. filterLowHangingObstacles, filterLedgeSpans, filterWalkableLowHeightSpans,
  72. walkbableAreaMod, buildMeshDetail);
  73. }
  74. public static VoxelFile From(RcConfig config, List<RecastBuilderResult> results)
  75. {
  76. VoxelFile f = new VoxelFile();
  77. f.version = 1;
  78. f.partition = config.Partition;
  79. f.filterLowHangingObstacles = config.FilterLowHangingObstacles;
  80. f.filterLedgeSpans = config.FilterLedgeSpans;
  81. f.filterWalkableLowHeightSpans = config.FilterWalkableLowHeightSpans;
  82. f.walkableRadius = config.WalkableRadiusWorld;
  83. f.walkableHeight = config.WalkableHeightWorld;
  84. f.walkableClimb = config.WalkableClimbWorld;
  85. f.walkableSlopeAngle = config.WalkableSlopeAngle;
  86. f.cellSize = config.Cs;
  87. f.maxSimplificationError = config.MaxSimplificationError;
  88. f.maxEdgeLen = config.MaxEdgeLenWorld;
  89. f.minRegionArea = config.MinRegionAreaWorld;
  90. f.regionMergeArea = config.MergeRegionAreaWorld;
  91. f.vertsPerPoly = config.MaxVertsPerPoly;
  92. f.buildMeshDetail = config.BuildMeshDetail;
  93. f.detailSampleDistance = config.DetailSampleDist;
  94. f.detailSampleMaxError = config.DetailSampleMaxError;
  95. f.useTiles = config.UseTiles;
  96. f.tileSizeX = config.TileSizeX;
  97. f.tileSizeZ = config.TileSizeZ;
  98. f.bounds = new float[]
  99. {
  100. float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity,
  101. float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity
  102. };
  103. foreach (RecastBuilderResult r in results)
  104. {
  105. f.tiles.Add(new VoxelTile(r.tileX, r.tileZ, r.GetSolidHeightfield()));
  106. f.bounds[0] = Math.Min(f.bounds[0], r.GetSolidHeightfield().bmin.x);
  107. f.bounds[1] = Math.Min(f.bounds[1], r.GetSolidHeightfield().bmin.y);
  108. f.bounds[2] = Math.Min(f.bounds[2], r.GetSolidHeightfield().bmin.z);
  109. f.bounds[3] = Math.Max(f.bounds[3], r.GetSolidHeightfield().bmax.x);
  110. f.bounds[4] = Math.Max(f.bounds[4], r.GetSolidHeightfield().bmax.y);
  111. f.bounds[5] = Math.Max(f.bounds[5], r.GetSolidHeightfield().bmax.z);
  112. }
  113. return f;
  114. }
  115. public static VoxelFile From(DynamicNavMesh mesh)
  116. {
  117. VoxelFile f = new VoxelFile();
  118. f.version = 1;
  119. DynamicNavMeshConfig config = mesh.config;
  120. f.partition = config.partition;
  121. f.filterLowHangingObstacles = config.filterLowHangingObstacles;
  122. f.filterLedgeSpans = config.filterLedgeSpans;
  123. f.filterWalkableLowHeightSpans = config.filterWalkableLowHeightSpans;
  124. f.walkableRadius = config.walkableRadius;
  125. f.walkableHeight = config.walkableHeight;
  126. f.walkableClimb = config.walkableClimb;
  127. f.walkableSlopeAngle = config.walkableSlopeAngle;
  128. f.cellSize = config.cellSize;
  129. f.maxSimplificationError = config.maxSimplificationError;
  130. f.maxEdgeLen = config.maxEdgeLen;
  131. f.minRegionArea = config.minRegionArea;
  132. f.regionMergeArea = config.regionMergeArea;
  133. f.vertsPerPoly = config.vertsPerPoly;
  134. f.buildMeshDetail = config.buildDetailMesh;
  135. f.detailSampleDistance = config.detailSampleDistance;
  136. f.detailSampleMaxError = config.detailSampleMaxError;
  137. f.useTiles = config.useTiles;
  138. f.tileSizeX = config.tileSizeX;
  139. f.tileSizeZ = config.tileSizeZ;
  140. f.bounds = new float[]
  141. {
  142. float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity,
  143. float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity
  144. };
  145. foreach (VoxelTile vt in mesh.VoxelTiles())
  146. {
  147. RcHeightfield heightfield = vt.Heightfield();
  148. f.tiles.Add(new VoxelTile(vt.tileX, vt.tileZ, heightfield));
  149. f.bounds[0] = Math.Min(f.bounds[0], vt.boundsMin.x);
  150. f.bounds[1] = Math.Min(f.bounds[1], vt.boundsMin.y);
  151. f.bounds[2] = Math.Min(f.bounds[2], vt.boundsMin.z);
  152. f.bounds[3] = Math.Max(f.bounds[3], vt.boundsMax.x);
  153. f.bounds[4] = Math.Max(f.bounds[4], vt.boundsMax.y);
  154. f.bounds[5] = Math.Max(f.bounds[5], vt.boundsMax.z);
  155. }
  156. return f;
  157. }
  158. }
  159. }