VoxelFileReader.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.IO;
  19. using DotRecast.Core;
  20. using DotRecast.Detour.Io;
  21. namespace DotRecast.Detour.Dynamic.Io
  22. {
  23. public class VoxelFileReader
  24. {
  25. private readonly IRcCompressor _compressor;
  26. public VoxelFileReader(IRcCompressor compressor)
  27. {
  28. _compressor = compressor;
  29. }
  30. public VoxelFile Read(BinaryReader stream)
  31. {
  32. RcByteBuffer buf = IOUtils.ToByteBuffer(stream);
  33. VoxelFile file = new VoxelFile();
  34. int magic = buf.GetInt();
  35. if (magic != VoxelFile.MAGIC)
  36. {
  37. magic = IOUtils.SwapEndianness(magic);
  38. if (magic != VoxelFile.MAGIC)
  39. {
  40. throw new IOException("Invalid magic");
  41. }
  42. buf.Order(buf.Order() == RcByteOrder.BIG_ENDIAN ? RcByteOrder.LITTLE_ENDIAN : RcByteOrder.BIG_ENDIAN);
  43. }
  44. file.version = buf.GetInt();
  45. bool isExportedFromAstar = (file.version & VoxelFile.VERSION_EXPORTER_MASK) == 0;
  46. bool compression = (file.version & VoxelFile.VERSION_COMPRESSION_MASK) == VoxelFile.VERSION_COMPRESSION_LZ4;
  47. file.walkableRadius = buf.GetFloat();
  48. file.walkableHeight = buf.GetFloat();
  49. file.walkableClimb = buf.GetFloat();
  50. file.walkableSlopeAngle = buf.GetFloat();
  51. file.cellSize = buf.GetFloat();
  52. file.maxSimplificationError = buf.GetFloat();
  53. file.maxEdgeLen = buf.GetFloat();
  54. file.minRegionArea = (int)buf.GetFloat();
  55. if (!isExportedFromAstar)
  56. {
  57. file.regionMergeArea = buf.GetFloat();
  58. file.vertsPerPoly = buf.GetInt();
  59. file.buildMeshDetail = buf.Get() != 0;
  60. file.detailSampleDistance = buf.GetFloat();
  61. file.detailSampleMaxError = buf.GetFloat();
  62. }
  63. else
  64. {
  65. file.regionMergeArea = 6 * file.minRegionArea;
  66. file.vertsPerPoly = 6;
  67. file.buildMeshDetail = true;
  68. file.detailSampleDistance = file.maxEdgeLen * 0.5f;
  69. file.detailSampleMaxError = file.maxSimplificationError * 0.8f;
  70. }
  71. file.useTiles = buf.Get() != 0;
  72. file.tileSizeX = buf.GetInt();
  73. file.tileSizeZ = buf.GetInt();
  74. file.rotation.x = buf.GetFloat();
  75. file.rotation.y = buf.GetFloat();
  76. file.rotation.z = buf.GetFloat();
  77. file.bounds[0] = buf.GetFloat();
  78. file.bounds[1] = buf.GetFloat();
  79. file.bounds[2] = buf.GetFloat();
  80. file.bounds[3] = buf.GetFloat();
  81. file.bounds[4] = buf.GetFloat();
  82. file.bounds[5] = buf.GetFloat();
  83. if (isExportedFromAstar)
  84. {
  85. // bounds are saved as center + size
  86. file.bounds[0] -= 0.5f * file.bounds[3];
  87. file.bounds[1] -= 0.5f * file.bounds[4];
  88. file.bounds[2] -= 0.5f * file.bounds[5];
  89. file.bounds[3] += file.bounds[0];
  90. file.bounds[4] += file.bounds[1];
  91. file.bounds[5] += file.bounds[2];
  92. }
  93. int tileCount = buf.GetInt();
  94. for (int tile = 0; tile < tileCount; tile++)
  95. {
  96. int tileX = buf.GetInt();
  97. int tileZ = buf.GetInt();
  98. int width = buf.GetInt();
  99. int depth = buf.GetInt();
  100. int borderSize = buf.GetInt();
  101. RcVec3f boundsMin = new RcVec3f();
  102. boundsMin.x = buf.GetFloat();
  103. boundsMin.y = buf.GetFloat();
  104. boundsMin.z = buf.GetFloat();
  105. RcVec3f boundsMax = new RcVec3f();
  106. boundsMax.x = buf.GetFloat();
  107. boundsMax.y = buf.GetFloat();
  108. boundsMax.z = buf.GetFloat();
  109. if (isExportedFromAstar)
  110. {
  111. // bounds are local
  112. boundsMin.x += file.bounds[0];
  113. boundsMin.y += file.bounds[1];
  114. boundsMin.z += file.bounds[2];
  115. boundsMax.x += file.bounds[0];
  116. boundsMax.y += file.bounds[1];
  117. boundsMax.z += file.bounds[2];
  118. }
  119. float cellSize = buf.GetFloat();
  120. float cellHeight = buf.GetFloat();
  121. int voxelSize = buf.GetInt();
  122. int position = buf.Position();
  123. byte[] bytes = buf.ReadBytes(voxelSize).ToArray();
  124. if (compression)
  125. {
  126. bytes = _compressor.Decompress(bytes);
  127. }
  128. RcByteBuffer data = new RcByteBuffer(bytes);
  129. data.Order(buf.Order());
  130. file.AddTile(new VoxelTile(tileX, tileZ, width, depth, boundsMin, boundsMax, cellSize, cellHeight, borderSize, data));
  131. buf.Position(position + voxelSize);
  132. }
  133. return file;
  134. }
  135. }
  136. }