ObjImporter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.IO;
  21. using DotRecast.Recast.Geom;
  22. namespace DotRecast.Recast
  23. {
  24. public static class ObjImporter
  25. {
  26. public static IInputGeomProvider Load(byte[] chunk)
  27. {
  28. var context = LoadContext(chunk);
  29. return new SimpleInputGeomProvider(context.vertexPositions, context.meshFaces);
  30. }
  31. public static ObjImporterContext LoadContext(byte[] chunk)
  32. {
  33. ObjImporterContext context = new ObjImporterContext();
  34. try
  35. {
  36. using StreamReader reader = new StreamReader(new MemoryStream(chunk));
  37. string line;
  38. while ((line = reader.ReadLine()) != null)
  39. {
  40. line = line.Trim();
  41. ReadLine(line, context);
  42. }
  43. }
  44. catch (Exception e)
  45. {
  46. throw new Exception(e.Message, e);
  47. }
  48. return context;
  49. }
  50. public static void ReadLine(string line, ObjImporterContext context)
  51. {
  52. if (line.StartsWith("v"))
  53. {
  54. ReadVertex(line, context);
  55. }
  56. else if (line.StartsWith("f"))
  57. {
  58. ReadFace(line, context);
  59. }
  60. }
  61. private static void ReadVertex(string line, ObjImporterContext context)
  62. {
  63. if (line.StartsWith("v "))
  64. {
  65. float[] vert = ReadVector3f(line);
  66. foreach (float vp in vert)
  67. {
  68. context.vertexPositions.Add(vp);
  69. }
  70. }
  71. }
  72. private static float[] ReadVector3f(string line)
  73. {
  74. string[] v = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  75. if (v.Length < 4)
  76. {
  77. throw new Exception("Invalid vector, expected 3 coordinates, found " + (v.Length - 1));
  78. }
  79. // fix - https://github.com/ikpil/DotRecast/issues/7
  80. return new float[]
  81. {
  82. float.Parse(v[1], CultureInfo.InvariantCulture),
  83. float.Parse(v[2], CultureInfo.InvariantCulture),
  84. float.Parse(v[3], CultureInfo.InvariantCulture)
  85. };
  86. }
  87. private static void ReadFace(string line, ObjImporterContext context)
  88. {
  89. string[] v = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  90. if (v.Length < 4)
  91. {
  92. throw new Exception("Invalid number of face vertices: 3 coordinates expected, found " + v.Length);
  93. }
  94. for (int j = 0; j < v.Length - 3; j++)
  95. {
  96. context.meshFaces.Add(ReadFaceVertex(v[1], context));
  97. for (int i = 0; i < 2; i++)
  98. {
  99. context.meshFaces.Add(ReadFaceVertex(v[2 + j + i], context));
  100. }
  101. }
  102. }
  103. private static int ReadFaceVertex(string face, ObjImporterContext context)
  104. {
  105. string[] v = face.Split("/");
  106. return GetIndex(int.Parse(v[0]), context.vertexPositions.Count);
  107. }
  108. private static int GetIndex(int posi, int size)
  109. {
  110. if (posi > 0)
  111. {
  112. posi--;
  113. }
  114. else if (posi < 0)
  115. {
  116. posi = size + posi;
  117. }
  118. else
  119. {
  120. throw new Exception("0 vertex index");
  121. }
  122. return posi;
  123. }
  124. }
  125. }