VoxelTile.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 DotRecast.Core;
  19. using DotRecast.Recast;
  20. namespace DotRecast.Detour.Dynamic.Io
  21. {
  22. public class VoxelTile
  23. {
  24. private const int SERIALIZED_SPAN_COUNT_BYTES = 2;
  25. private const int SERIALIZED_SPAN_BYTES = 12;
  26. public readonly int tileX;
  27. public readonly int tileZ;
  28. public readonly int borderSize;
  29. public int width;
  30. public int depth;
  31. public readonly RcVec3f boundsMin;
  32. public RcVec3f boundsMax;
  33. public float cellSize;
  34. public float cellHeight;
  35. public readonly byte[] spanData;
  36. public VoxelTile(int tileX, int tileZ, int width, int depth, RcVec3f boundsMin, RcVec3f boundsMax, float cellSize,
  37. float cellHeight, int borderSize, RcByteBuffer buffer)
  38. {
  39. this.tileX = tileX;
  40. this.tileZ = tileZ;
  41. this.width = width;
  42. this.depth = depth;
  43. this.boundsMin = boundsMin;
  44. this.boundsMax = boundsMax;
  45. this.cellSize = cellSize;
  46. this.cellHeight = cellHeight;
  47. this.borderSize = borderSize;
  48. spanData = ToByteArray(buffer, width, depth, VoxelFile.PREFERRED_BYTE_ORDER);
  49. }
  50. public VoxelTile(int tileX, int tileZ, RcHeightfield heightfield)
  51. {
  52. this.tileX = tileX;
  53. this.tileZ = tileZ;
  54. width = heightfield.width;
  55. depth = heightfield.height;
  56. boundsMin = heightfield.bmin;
  57. boundsMax = heightfield.bmax;
  58. cellSize = heightfield.cs;
  59. cellHeight = heightfield.ch;
  60. borderSize = heightfield.borderSize;
  61. spanData = SerializeSpans(heightfield, VoxelFile.PREFERRED_BYTE_ORDER);
  62. }
  63. public RcHeightfield Heightfield()
  64. {
  65. return VoxelFile.PREFERRED_BYTE_ORDER == RcByteOrder.BIG_ENDIAN ? HeightfieldBE() : HeightfieldLE();
  66. }
  67. private RcHeightfield HeightfieldBE()
  68. {
  69. RcHeightfield hf = new RcHeightfield(width, depth, boundsMin, boundsMax, cellSize, cellHeight, borderSize);
  70. int position = 0;
  71. for (int z = 0, pz = 0; z < depth; z++, pz += width)
  72. {
  73. for (int x = 0; x < width; x++)
  74. {
  75. RcSpan prev = null;
  76. int spanCount = ByteUtils.GetShortBE(spanData, position);
  77. position += 2;
  78. for (int s = 0; s < spanCount; s++)
  79. {
  80. RcSpan span = new RcSpan();
  81. span.smin = ByteUtils.GetIntBE(spanData, position);
  82. position += 4;
  83. span.smax = ByteUtils.GetIntBE(spanData, position);
  84. position += 4;
  85. span.area = ByteUtils.GetIntBE(spanData, position);
  86. position += 4;
  87. if (prev == null)
  88. {
  89. hf.spans[pz + x] = span;
  90. }
  91. else
  92. {
  93. prev.next = span;
  94. }
  95. prev = span;
  96. }
  97. }
  98. }
  99. return hf;
  100. }
  101. private RcHeightfield HeightfieldLE()
  102. {
  103. RcHeightfield hf = new RcHeightfield(width, depth, boundsMin, boundsMax, cellSize, cellHeight, borderSize);
  104. int position = 0;
  105. for (int z = 0, pz = 0; z < depth; z++, pz += width)
  106. {
  107. for (int x = 0; x < width; x++)
  108. {
  109. RcSpan prev = null;
  110. int spanCount = ByteUtils.GetShortLE(spanData, position);
  111. position += 2;
  112. for (int s = 0; s < spanCount; s++)
  113. {
  114. RcSpan span = new RcSpan();
  115. span.smin = ByteUtils.GetIntLE(spanData, position);
  116. position += 4;
  117. span.smax = ByteUtils.GetIntLE(spanData, position);
  118. position += 4;
  119. span.area = ByteUtils.GetIntLE(spanData, position);
  120. position += 4;
  121. if (prev == null)
  122. {
  123. hf.spans[pz + x] = span;
  124. }
  125. else
  126. {
  127. prev.next = span;
  128. }
  129. prev = span;
  130. }
  131. }
  132. }
  133. return hf;
  134. }
  135. private byte[] SerializeSpans(RcHeightfield heightfield, RcByteOrder order)
  136. {
  137. int[] counts = new int[heightfield.width * heightfield.height];
  138. int totalCount = 0;
  139. for (int z = 0, pz = 0; z < heightfield.height; z++, pz += heightfield.width)
  140. {
  141. for (int x = 0; x < heightfield.width; x++)
  142. {
  143. RcSpan span = heightfield.spans[pz + x];
  144. while (span != null)
  145. {
  146. counts[pz + x]++;
  147. totalCount++;
  148. span = span.next;
  149. }
  150. }
  151. }
  152. byte[] data = new byte[totalCount * SERIALIZED_SPAN_BYTES + counts.Length * SERIALIZED_SPAN_COUNT_BYTES];
  153. int position = 0;
  154. for (int z = 0, pz = 0; z < heightfield.height; z++, pz += heightfield.width)
  155. {
  156. for (int x = 0; x < heightfield.width; x++)
  157. {
  158. position = ByteUtils.PutShort(counts[pz + x], data, position, order);
  159. RcSpan span = heightfield.spans[pz + x];
  160. while (span != null)
  161. {
  162. position = ByteUtils.PutInt(span.smin, data, position, order);
  163. position = ByteUtils.PutInt(span.smax, data, position, order);
  164. position = ByteUtils.PutInt(span.area, data, position, order);
  165. span = span.next;
  166. }
  167. }
  168. }
  169. return data;
  170. }
  171. private byte[] ToByteArray(RcByteBuffer buf, int width, int height, RcByteOrder order)
  172. {
  173. byte[] data;
  174. if (buf.Order() == order)
  175. {
  176. data = buf.ReadBytes(buf.Limit()).ToArray();
  177. }
  178. else
  179. {
  180. data = new byte[buf.Limit()];
  181. int l = width * height;
  182. int position = 0;
  183. for (int i = 0; i < l; i++)
  184. {
  185. int count = buf.GetShort();
  186. ByteUtils.PutShort(count, data, position, order);
  187. position += 2;
  188. for (int j = 0; j < count; j++)
  189. {
  190. ByteUtils.PutInt(buf.GetInt(), data, position, order);
  191. position += 4;
  192. ByteUtils.PutInt(buf.GetInt(), data, position, order);
  193. position += 4;
  194. ByteUtils.PutInt(buf.GetInt(), data, position, order);
  195. position += 4;
  196. }
  197. }
  198. }
  199. return data;
  200. }
  201. }
  202. }