RcPolyMeshRaycast.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. namespace DotRecast.Recast
  21. {
  22. public static class RcPolyMeshRaycast
  23. {
  24. public static bool Raycast(IList<RecastBuilderResult> results, RcVec3f src, RcVec3f dst, out float hitTime)
  25. {
  26. hitTime = 0.0f;
  27. foreach (RecastBuilderResult result in results)
  28. {
  29. if (result.GetMeshDetail() != null)
  30. {
  31. if (Raycast(result.GetMesh(), result.GetMeshDetail(), src, dst, out hitTime))
  32. {
  33. return true;
  34. }
  35. }
  36. }
  37. return false;
  38. }
  39. private static bool Raycast(RcPolyMesh poly, RcPolyMeshDetail meshDetail, RcVec3f sp, RcVec3f sq, out float hitTime)
  40. {
  41. hitTime = 0;
  42. if (meshDetail != null)
  43. {
  44. for (int i = 0; i < meshDetail.nmeshes; ++i)
  45. {
  46. int m = i * 4;
  47. int bverts = meshDetail.meshes[m];
  48. int btris = meshDetail.meshes[m + 2];
  49. int ntris = meshDetail.meshes[m + 3];
  50. int verts = bverts * 3;
  51. int tris = btris * 4;
  52. for (int j = 0; j < ntris; ++j)
  53. {
  54. RcVec3f[] vs = new RcVec3f[3];
  55. for (int k = 0; k < 3; ++k)
  56. {
  57. vs[k].x = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3];
  58. vs[k].y = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3 + 1];
  59. vs[k].z = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3 + 2];
  60. }
  61. if (Intersections.IntersectSegmentTriangle(sp, sq, vs[0], vs[1], vs[2], out hitTime))
  62. {
  63. return true;
  64. }
  65. }
  66. }
  67. }
  68. else
  69. {
  70. // TODO: check PolyMesh instead
  71. }
  72. return false;
  73. }
  74. }
  75. }