AbstractGroundSampler.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using DotRecast.Core;
  3. using DotRecast.Recast;
  4. using static DotRecast.Core.RcMath;
  5. namespace DotRecast.Detour.Extras.Jumplink
  6. {
  7. public abstract class AbstractGroundSampler : IGroundSampler
  8. {
  9. public delegate bool ComputeNavMeshHeight(RcVec3f pt, float cellSize, out float height);
  10. protected void SampleGround(JumpLinkBuilderConfig acfg, EdgeSampler es, ComputeNavMeshHeight heightFunc)
  11. {
  12. float cs = acfg.cellSize;
  13. float dist = (float)Math.Sqrt(RcVec3f.Dist2DSqr(es.start.p, es.start.q));
  14. int ngsamples = Math.Max(2, (int)Math.Ceiling(dist / cs));
  15. SampleGroundSegment(heightFunc, es.start, ngsamples);
  16. foreach (GroundSegment end in es.end)
  17. {
  18. SampleGroundSegment(heightFunc, end, ngsamples);
  19. }
  20. }
  21. public abstract void Sample(JumpLinkBuilderConfig acfg, RecastBuilderResult result, EdgeSampler es);
  22. protected void SampleGroundSegment(ComputeNavMeshHeight heightFunc, GroundSegment seg, int nsamples)
  23. {
  24. seg.gsamples = new GroundSample[nsamples];
  25. for (int i = 0; i < nsamples; ++i)
  26. {
  27. float u = i / (float)(nsamples - 1);
  28. GroundSample s = new GroundSample();
  29. seg.gsamples[i] = s;
  30. RcVec3f pt = RcVec3f.Lerp(seg.p, seg.q, u);
  31. bool success = heightFunc.Invoke(pt, seg.height, out var height);
  32. s.p.x = pt.x;
  33. s.p.y = height;
  34. s.p.z = pt.z;
  35. if (!success)
  36. {
  37. continue;
  38. }
  39. s.validHeight = true;
  40. }
  41. }
  42. }
  43. }