DtMeshDataReader.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. Recast4J Copyright (c) 2015 Piotr Piastucki piotr@jtilia.org
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. using System.IO;
  18. using DotRecast.Core;
  19. namespace DotRecast.Detour.Io
  20. {
  21. public class DtMeshDataReader
  22. {
  23. public const int DT_POLY_DETAIL_SIZE = 10;
  24. public DtMeshData Read(BinaryReader stream, int maxVertPerPoly)
  25. {
  26. RcByteBuffer buf = IOUtils.ToByteBuffer(stream);
  27. return Read(buf, maxVertPerPoly, false);
  28. }
  29. public DtMeshData Read(RcByteBuffer buf, int maxVertPerPoly)
  30. {
  31. return Read(buf, maxVertPerPoly, false);
  32. }
  33. public DtMeshData Read32Bit(BinaryReader stream, int maxVertPerPoly)
  34. {
  35. RcByteBuffer buf = IOUtils.ToByteBuffer(stream);
  36. return Read(buf, maxVertPerPoly, true);
  37. }
  38. public DtMeshData Read32Bit(RcByteBuffer buf, int maxVertPerPoly)
  39. {
  40. return Read(buf, maxVertPerPoly, true);
  41. }
  42. public DtMeshData Read(RcByteBuffer buf, int maxVertPerPoly, bool is32Bit)
  43. {
  44. DtMeshData data = new DtMeshData();
  45. DtMeshHeader header = new DtMeshHeader();
  46. data.header = header;
  47. header.magic = buf.GetInt();
  48. if (header.magic != DtMeshHeader.DT_NAVMESH_MAGIC)
  49. {
  50. header.magic = IOUtils.SwapEndianness(header.magic);
  51. if (header.magic != DtMeshHeader.DT_NAVMESH_MAGIC)
  52. {
  53. throw new IOException("Invalid magic");
  54. }
  55. buf.Order(buf.Order() == RcByteOrder.BIG_ENDIAN ? RcByteOrder.LITTLE_ENDIAN : RcByteOrder.BIG_ENDIAN);
  56. }
  57. header.version = buf.GetInt();
  58. if (header.version != DtMeshHeader.DT_NAVMESH_VERSION)
  59. {
  60. if (header.version < DtMeshHeader.DT_NAVMESH_VERSION_RECAST4J_FIRST
  61. || header.version > DtMeshHeader.DT_NAVMESH_VERSION_RECAST4J_LAST)
  62. {
  63. throw new IOException("Invalid version " + header.version);
  64. }
  65. }
  66. bool cCompatibility = header.version == DtMeshHeader.DT_NAVMESH_VERSION;
  67. header.x = buf.GetInt();
  68. header.y = buf.GetInt();
  69. header.layer = buf.GetInt();
  70. header.userId = buf.GetInt();
  71. header.polyCount = buf.GetInt();
  72. header.vertCount = buf.GetInt();
  73. header.maxLinkCount = buf.GetInt();
  74. header.detailMeshCount = buf.GetInt();
  75. header.detailVertCount = buf.GetInt();
  76. header.detailTriCount = buf.GetInt();
  77. header.bvNodeCount = buf.GetInt();
  78. header.offMeshConCount = buf.GetInt();
  79. header.offMeshBase = buf.GetInt();
  80. header.walkableHeight = buf.GetFloat();
  81. header.walkableRadius = buf.GetFloat();
  82. header.walkableClimb = buf.GetFloat();
  83. header.bmin.x = buf.GetFloat();
  84. header.bmin.y = buf.GetFloat();
  85. header.bmin.z = buf.GetFloat();
  86. header.bmax.x = buf.GetFloat();
  87. header.bmax.y = buf.GetFloat();
  88. header.bmax.z = buf.GetFloat();
  89. header.bvQuantFactor = buf.GetFloat();
  90. data.verts = ReadVerts(buf, header.vertCount);
  91. data.polys = ReadPolys(buf, header, maxVertPerPoly);
  92. if (cCompatibility)
  93. {
  94. buf.Position(buf.Position() + header.maxLinkCount * GetSizeofLink(is32Bit));
  95. }
  96. data.detailMeshes = ReadPolyDetails(buf, header, cCompatibility);
  97. data.detailVerts = ReadVerts(buf, header.detailVertCount);
  98. data.detailTris = ReadDTris(buf, header);
  99. data.bvTree = ReadBVTree(buf, header);
  100. data.offMeshCons = ReadOffMeshCons(buf, header);
  101. return data;
  102. }
  103. public const int LINK_SIZEOF = 16;
  104. public const int LINK_SIZEOF32BIT = 12;
  105. public static int GetSizeofLink(bool is32Bit)
  106. {
  107. return is32Bit ? LINK_SIZEOF32BIT : LINK_SIZEOF;
  108. }
  109. private float[] ReadVerts(RcByteBuffer buf, int count)
  110. {
  111. float[] verts = new float[count * 3];
  112. for (int i = 0; i < verts.Length; i++)
  113. {
  114. verts[i] = buf.GetFloat();
  115. }
  116. return verts;
  117. }
  118. private DtPoly[] ReadPolys(RcByteBuffer buf, DtMeshHeader header, int maxVertPerPoly)
  119. {
  120. DtPoly[] polys = new DtPoly[header.polyCount];
  121. for (int i = 0; i < polys.Length; i++)
  122. {
  123. polys[i] = new DtPoly(i, maxVertPerPoly);
  124. if (header.version < DtMeshHeader.DT_NAVMESH_VERSION_RECAST4J_NO_POLY_FIRSTLINK)
  125. {
  126. buf.GetInt(); // polys[i].firstLink
  127. }
  128. for (int j = 0; j < polys[i].verts.Length; j++)
  129. {
  130. polys[i].verts[j] = buf.GetShort() & 0xFFFF;
  131. }
  132. for (int j = 0; j < polys[i].neis.Length; j++)
  133. {
  134. polys[i].neis[j] = buf.GetShort() & 0xFFFF;
  135. }
  136. polys[i].flags = buf.GetShort() & 0xFFFF;
  137. polys[i].vertCount = buf.Get() & 0xFF;
  138. polys[i].areaAndtype = buf.Get() & 0xFF;
  139. }
  140. return polys;
  141. }
  142. private DtPolyDetail[] ReadPolyDetails(RcByteBuffer buf, DtMeshHeader header, bool cCompatibility)
  143. {
  144. DtPolyDetail[] polys = new DtPolyDetail[header.detailMeshCount];
  145. for (int i = 0; i < polys.Length; i++)
  146. {
  147. polys[i] = new DtPolyDetail();
  148. polys[i].vertBase = buf.GetInt();
  149. polys[i].triBase = buf.GetInt();
  150. polys[i].vertCount = buf.Get() & 0xFF;
  151. polys[i].triCount = buf.Get() & 0xFF;
  152. if (cCompatibility)
  153. {
  154. buf.GetShort(); // C struct padding
  155. }
  156. }
  157. return polys;
  158. }
  159. private int[] ReadDTris(RcByteBuffer buf, DtMeshHeader header)
  160. {
  161. int[] tris = new int[4 * header.detailTriCount];
  162. for (int i = 0; i < tris.Length; i++)
  163. {
  164. tris[i] = buf.Get() & 0xFF;
  165. }
  166. return tris;
  167. }
  168. private DtBVNode[] ReadBVTree(RcByteBuffer buf, DtMeshHeader header)
  169. {
  170. DtBVNode[] nodes = new DtBVNode[header.bvNodeCount];
  171. for (int i = 0; i < nodes.Length; i++)
  172. {
  173. nodes[i] = new DtBVNode();
  174. if (header.version < DtMeshHeader.DT_NAVMESH_VERSION_RECAST4J_32BIT_BVTREE)
  175. {
  176. for (int j = 0; j < 3; j++)
  177. {
  178. nodes[i].bmin[j] = buf.GetShort() & 0xFFFF;
  179. }
  180. for (int j = 0; j < 3; j++)
  181. {
  182. nodes[i].bmax[j] = buf.GetShort() & 0xFFFF;
  183. }
  184. }
  185. else
  186. {
  187. for (int j = 0; j < 3; j++)
  188. {
  189. nodes[i].bmin[j] = buf.GetInt();
  190. }
  191. for (int j = 0; j < 3; j++)
  192. {
  193. nodes[i].bmax[j] = buf.GetInt();
  194. }
  195. }
  196. nodes[i].i = buf.GetInt();
  197. }
  198. return nodes;
  199. }
  200. private DtOffMeshConnection[] ReadOffMeshCons(RcByteBuffer buf, DtMeshHeader header)
  201. {
  202. DtOffMeshConnection[] cons = new DtOffMeshConnection[header.offMeshConCount];
  203. for (int i = 0; i < cons.Length; i++)
  204. {
  205. cons[i] = new DtOffMeshConnection();
  206. for (int j = 0; j < 6; j++)
  207. {
  208. cons[i].pos[j] = buf.GetFloat();
  209. }
  210. cons[i].rad = buf.GetFloat();
  211. cons[i].poly = buf.GetShort() & 0xFFFF;
  212. cons[i].flags = buf.Get() & 0xFF;
  213. cons[i].side = buf.Get() & 0xFF;
  214. cons[i].userId = buf.GetInt();
  215. }
  216. return cons;
  217. }
  218. }
  219. }