RecastVoxelization.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 System.Collections.Generic;
  19. using DotRecast.Core;
  20. using DotRecast.Recast.Geom;
  21. namespace DotRecast.Recast
  22. {
  23. public static class RecastVoxelization
  24. {
  25. public static RcHeightfield BuildSolidHeightfield(IInputGeomProvider geomProvider, RecastBuilderConfig builderCfg, RcTelemetry ctx)
  26. {
  27. RcConfig cfg = builderCfg.cfg;
  28. // Allocate voxel heightfield where we rasterize our input data to.
  29. RcHeightfield solid = new RcHeightfield(builderCfg.width, builderCfg.height, builderCfg.bmin, builderCfg.bmax, cfg.Cs, cfg.Ch, cfg.BorderSize);
  30. // Allocate array that can hold triangle area types.
  31. // If you have multiple meshes you need to process, allocate
  32. // and array which can hold the max number of triangles you need to
  33. // process.
  34. // Find triangles which are walkable based on their slope and rasterize
  35. // them.
  36. // If your input data is multiple meshes, you can transform them here,
  37. // calculate
  38. // the are type for each of the meshes and rasterize them.
  39. foreach (RcTriMesh geom in geomProvider.Meshes())
  40. {
  41. float[] verts = geom.GetVerts();
  42. if (cfg.UseTiles)
  43. {
  44. float[] tbmin = new float[2];
  45. float[] tbmax = new float[2];
  46. tbmin[0] = builderCfg.bmin.x;
  47. tbmin[1] = builderCfg.bmin.z;
  48. tbmax[0] = builderCfg.bmax.x;
  49. tbmax[1] = builderCfg.bmax.z;
  50. List<RcChunkyTriMeshNode> nodes = geom.GetChunksOverlappingRect(tbmin, tbmax);
  51. foreach (RcChunkyTriMeshNode node in nodes)
  52. {
  53. int[] tris = node.tris;
  54. int ntris = tris.Length / 3;
  55. int[] m_triareas = RcUtils.MarkWalkableTriangles(ctx, cfg.WalkableSlopeAngle, verts, tris, ntris, cfg.WalkableAreaMod);
  56. RecastRasterization.RasterizeTriangles(solid, verts, tris, m_triareas, ntris, cfg.WalkableClimb, ctx);
  57. }
  58. }
  59. else
  60. {
  61. int[] tris = geom.GetTris();
  62. int ntris = tris.Length / 3;
  63. int[] m_triareas = RcUtils.MarkWalkableTriangles(ctx, cfg.WalkableSlopeAngle, verts, tris, ntris, cfg.WalkableAreaMod);
  64. RecastRasterization.RasterizeTriangles(solid, verts, tris, m_triareas, ntris, cfg.WalkableClimb, ctx);
  65. }
  66. }
  67. return solid;
  68. }
  69. }
  70. }