RcUtils.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 DotRecast.Core;
  21. namespace DotRecast.Recast
  22. {
  23. using static DotRecast.Recast.RcConstants;
  24. public static class RcUtils
  25. {
  26. public static void CalcBounds(float[] verts, int nv, float[] bmin, float[] bmax)
  27. {
  28. for (int i = 0; i < 3; i++)
  29. {
  30. bmin[i] = verts[i];
  31. bmax[i] = verts[i];
  32. }
  33. for (int i = 1; i < nv; ++i)
  34. {
  35. for (int j = 0; j < 3; j++)
  36. {
  37. bmin[j] = Math.Min(bmin[j], verts[i * 3 + j]);
  38. bmax[j] = Math.Max(bmax[j], verts[i * 3 + j]);
  39. }
  40. }
  41. // Calculate bounding box.
  42. }
  43. public static void CalcGridSize(RcVec3f bmin, RcVec3f bmax, float cs, out int sizeX, out int sizeZ)
  44. {
  45. sizeX = (int)((bmax.x - bmin.x) / cs + 0.5f);
  46. sizeZ = (int)((bmax.z - bmin.z) / cs + 0.5f);
  47. }
  48. public static void CalcTileCount(RcVec3f bmin, RcVec3f bmax, float cs, int tileSizeX, int tileSizeZ, out int tw, out int td)
  49. {
  50. CalcGridSize(bmin, bmax, cs, out var gw, out var gd);
  51. tw = (gw + tileSizeX - 1) / tileSizeX;
  52. td = (gd + tileSizeZ - 1) / tileSizeZ;
  53. }
  54. /// @par
  55. ///
  56. /// Modifies the area id of all triangles with a slope below the specified value.
  57. ///
  58. /// See the #rcConfig documentation for more information on the configuration parameters.
  59. ///
  60. /// @see rcHeightfield, rcClearUnwalkableTriangles, rcRasterizeTriangles
  61. public static int[] MarkWalkableTriangles(RcTelemetry ctx, float walkableSlopeAngle, float[] verts, int[] tris, int nt, RcAreaModification areaMod)
  62. {
  63. int[] areas = new int[nt];
  64. float walkableThr = (float)Math.Cos(walkableSlopeAngle / 180.0f * Math.PI);
  65. RcVec3f norm = new RcVec3f();
  66. for (int i = 0; i < nt; ++i)
  67. {
  68. int tri = i * 3;
  69. CalcTriNormal(verts, tris[tri], tris[tri + 1], tris[tri + 2], ref norm);
  70. // Check if the face is walkable.
  71. if (norm.y > walkableThr)
  72. areas[i] = areaMod.Apply(areas[i]);
  73. }
  74. return areas;
  75. }
  76. public static void CalcTriNormal(float[] verts, int v0, int v1, int v2, ref RcVec3f norm)
  77. {
  78. RcVec3f e0 = new RcVec3f();
  79. RcVec3f e1 = new RcVec3f();
  80. RcVec3f.Sub(ref e0, verts, v1 * 3, v0 * 3);
  81. RcVec3f.Sub(ref e1, verts, v2 * 3, v0 * 3);
  82. RcVec3f.Cross(ref norm, e0, e1);
  83. RcVec3f.Normalize(ref norm);
  84. }
  85. /// @par
  86. ///
  87. /// Only sets the area id's for the unwalkable triangles. Does not alter the
  88. /// area id's for walkable triangles.
  89. ///
  90. /// See the #rcConfig documentation for more information on the configuration parameters.
  91. ///
  92. /// @see rcHeightfield, rcClearUnwalkableTriangles, rcRasterizeTriangles
  93. public static void ClearUnwalkableTriangles(RcTelemetry ctx, float walkableSlopeAngle, float[] verts, int nv, int[] tris, int nt, int[] areas)
  94. {
  95. float walkableThr = (float)Math.Cos(walkableSlopeAngle / 180.0f * Math.PI);
  96. RcVec3f norm = new RcVec3f();
  97. for (int i = 0; i < nt; ++i)
  98. {
  99. int tri = i * 3;
  100. CalcTriNormal(verts, tris[tri], tris[tri + 1], tris[tri + 2], ref norm);
  101. // Check if the face is walkable.
  102. if (norm.y <= walkableThr)
  103. areas[i] = RC_NULL_AREA;
  104. }
  105. }
  106. }
  107. }