SimpleInputGeomProvider.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using DotRecast.Core;
  22. namespace DotRecast.Recast.Geom
  23. {
  24. public class SimpleInputGeomProvider : IInputGeomProvider
  25. {
  26. public readonly float[] vertices;
  27. public readonly int[] faces;
  28. public readonly float[] normals;
  29. private RcVec3f bmin;
  30. private RcVec3f bmax;
  31. private readonly List<RcConvexVolume> volumes = new List<RcConvexVolume>();
  32. private readonly RcTriMesh _mesh;
  33. public SimpleInputGeomProvider(List<float> vertexPositions, List<int> meshFaces)
  34. : this(MapVertices(vertexPositions), MapFaces(meshFaces))
  35. {
  36. }
  37. private static int[] MapFaces(List<int> meshFaces)
  38. {
  39. int[] faces = new int[meshFaces.Count];
  40. for (int i = 0; i < faces.Length; i++)
  41. {
  42. faces[i] = meshFaces[i];
  43. }
  44. return faces;
  45. }
  46. private static float[] MapVertices(List<float> vertexPositions)
  47. {
  48. float[] vertices = new float[vertexPositions.Count];
  49. for (int i = 0; i < vertices.Length; i++)
  50. {
  51. vertices[i] = vertexPositions[i];
  52. }
  53. return vertices;
  54. }
  55. public SimpleInputGeomProvider(float[] vertices, int[] faces)
  56. {
  57. this.vertices = vertices;
  58. this.faces = faces;
  59. normals = new float[faces.Length];
  60. CalculateNormals();
  61. bmin = RcVec3f.Zero;
  62. bmax = RcVec3f.Zero;
  63. RcVec3f.Copy(ref bmin, vertices, 0);
  64. RcVec3f.Copy(ref bmax, vertices, 0);
  65. for (int i = 1; i < vertices.Length / 3; i++)
  66. {
  67. bmin.Min(vertices, i * 3);
  68. bmax.Max(vertices, i * 3);
  69. }
  70. _mesh = new RcTriMesh(vertices, faces);
  71. }
  72. public RcTriMesh GetMesh()
  73. {
  74. return _mesh;
  75. }
  76. public RcVec3f GetMeshBoundsMin()
  77. {
  78. return bmin;
  79. }
  80. public RcVec3f GetMeshBoundsMax()
  81. {
  82. return bmax;
  83. }
  84. public IList<RcConvexVolume> ConvexVolumes()
  85. {
  86. return volumes;
  87. }
  88. public void AddConvexVolume(float[] verts, float minh, float maxh, RcAreaModification areaMod)
  89. {
  90. RcConvexVolume vol = new RcConvexVolume();
  91. vol.hmin = minh;
  92. vol.hmax = maxh;
  93. vol.verts = verts;
  94. vol.areaMod = areaMod;
  95. }
  96. public void AddConvexVolume(RcConvexVolume convexVolume)
  97. {
  98. volumes.Add(convexVolume);
  99. }
  100. public IEnumerable<RcTriMesh> Meshes()
  101. {
  102. return RcImmutableArray.Create(_mesh);
  103. }
  104. public List<RcOffMeshConnection> GetOffMeshConnections()
  105. {
  106. throw new NotImplementedException();
  107. }
  108. public void AddOffMeshConnection(RcVec3f start, RcVec3f end, float radius, bool bidir, int area, int flags)
  109. {
  110. throw new NotImplementedException();
  111. }
  112. public void RemoveOffMeshConnections(Predicate<RcOffMeshConnection> filter)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. public void CalculateNormals()
  117. {
  118. for (int i = 0; i < faces.Length; i += 3)
  119. {
  120. int v0 = faces[i] * 3;
  121. int v1 = faces[i + 1] * 3;
  122. int v2 = faces[i + 2] * 3;
  123. var e0 = new RcVec3f();
  124. var e1 = new RcVec3f();
  125. e0.x = vertices[v1 + 0] - vertices[v0 + 0];
  126. e0.y = vertices[v1 + 1] - vertices[v0 + 1];
  127. e0.z = vertices[v1 + 2] - vertices[v0 + 2];
  128. e1.x = vertices[v2 + 0] - vertices[v0 + 0];
  129. e1.y = vertices[v2 + 1] - vertices[v0 + 1];
  130. e1.z = vertices[v2 + 2] - vertices[v0 + 2];
  131. normals[i] = e0.y * e1.z - e0.z * e1.y;
  132. normals[i + 1] = e0.z * e1.x - e0.x * e1.z;
  133. normals[i + 2] = e0.x * e1.y - e0.y * e1.x;
  134. float d = (float)Math.Sqrt(normals[i] * normals[i] + normals[i + 1] * normals[i + 1] + normals[i + 2] * normals[i + 2]);
  135. if (d > 0)
  136. {
  137. d = 1.0f / d;
  138. normals[i] *= d;
  139. normals[i + 1] *= d;
  140. normals[i + 2] *= d;
  141. }
  142. }
  143. }
  144. }
  145. }