BVTreeBuilder.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
  3. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. using DotRecast.Core;
  19. using static DotRecast.Core.RcMath;
  20. namespace DotRecast.Detour.Extras
  21. {
  22. public class BVTreeBuilder
  23. {
  24. public void Build(DtMeshData data)
  25. {
  26. data.bvTree = new DtBVNode[data.header.polyCount * 2];
  27. data.header.bvNodeCount = data.bvTree.Length == 0
  28. ? 0
  29. : CreateBVTree(data, data.bvTree, data.header.bvQuantFactor);
  30. }
  31. private static int CreateBVTree(DtMeshData data, DtBVNode[] nodes, float quantFactor)
  32. {
  33. BVItem[] items = new BVItem[data.header.polyCount];
  34. for (int i = 0; i < data.header.polyCount; i++)
  35. {
  36. BVItem it = new BVItem();
  37. items[i] = it;
  38. it.i = i;
  39. RcVec3f bmin = new RcVec3f();
  40. RcVec3f bmax = new RcVec3f();
  41. bmin.Set(data.verts, data.polys[i].verts[0] * 3);
  42. bmax.Set(data.verts, data.polys[i].verts[0] * 3);
  43. for (int j = 1; j < data.polys[i].vertCount; j++)
  44. {
  45. bmin.Min(data.verts, data.polys[i].verts[j] * 3);
  46. bmax.Max(data.verts, data.polys[i].verts[j] * 3);
  47. }
  48. it.bmin[0] = Clamp((int)((bmin.x - data.header.bmin.x) * quantFactor), 0, 0x7fffffff);
  49. it.bmin[1] = Clamp((int)((bmin.y - data.header.bmin.y) * quantFactor), 0, 0x7fffffff);
  50. it.bmin[2] = Clamp((int)((bmin.z - data.header.bmin.z) * quantFactor), 0, 0x7fffffff);
  51. it.bmax[0] = Clamp((int)((bmax.x - data.header.bmin.x) * quantFactor), 0, 0x7fffffff);
  52. it.bmax[1] = Clamp((int)((bmax.y - data.header.bmin.y) * quantFactor), 0, 0x7fffffff);
  53. it.bmax[2] = Clamp((int)((bmax.z - data.header.bmin.z) * quantFactor), 0, 0x7fffffff);
  54. }
  55. return DtNavMeshBuilder.Subdivide(items, data.header.polyCount, 0, data.header.polyCount, 0, nodes);
  56. }
  57. }
  58. }