ObjExporter.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. recast4j copyright (c) 2015-2019 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. namespace DotRecast.Detour.Extras
  19. {
  20. public class ObjExporter
  21. {
  22. public void Export(DtNavMesh mesh)
  23. {
  24. string filename = Path.Combine(Directory.GetCurrentDirectory(), "Demo", "astar.obj");
  25. using var fs = new FileStream(filename, FileMode.CreateNew);
  26. using var fw = new StreamWriter(fs);
  27. for (int i = 0; i < mesh.GetTileCount(); i++)
  28. {
  29. DtMeshTile tile = mesh.GetTile(i);
  30. if (tile != null)
  31. {
  32. for (int v = 0; v < tile.data.header.vertCount; v++)
  33. {
  34. fw.Write("v " + tile.data.verts[v * 3] + " " + tile.data.verts[v * 3 + 1] + " "
  35. + tile.data.verts[v * 3 + 2] + "\n");
  36. }
  37. }
  38. }
  39. int vertexOffset = 1;
  40. for (int i = 0; i < mesh.GetTileCount(); i++)
  41. {
  42. DtMeshTile tile = mesh.GetTile(i);
  43. if (tile != null)
  44. {
  45. for (int p = 0; p < tile.data.header.polyCount; p++)
  46. {
  47. fw.Write("f ");
  48. DtPoly poly = tile.data.polys[p];
  49. for (int v = 0; v < poly.vertCount; v++)
  50. {
  51. fw.Write(poly.verts[v] + vertexOffset + " ");
  52. }
  53. fw.Write("\n");
  54. }
  55. vertexOffset += tile.data.header.vertCount;
  56. }
  57. }
  58. }
  59. /*
  60. *
  61. MeshSetReader reader = new MeshSetReader();
  62. ObjExporter exporter = new ObjExporter();
  63. exporter.Export(mesh);
  64. reader.Read(new FileInputStream("/home/piotr/Downloads/graph/all_tiles_navmesh.bin"), 3);
  65. */
  66. }
  67. }