Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- commit 4adb2cde5744189550f87a475c09837938ed90d4
- Author: Aron Granberg <aron.granberg@gmail.com>
- Date: Thu Apr 2 13:57:16 2020 +0200
- The RaycastModifier now respects which tags are traversable and any ITraversalProvider set on the path.
- diff --git a/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs b/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs
- index 7e0c5157..23298d54 100644
- --- a/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs
- +++ b/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs
- @@ -108,12 +108,28 @@ namespace Pathfinding {
- static float[] DPCosts = new float[16];
- static int[] DPParents = new int[16];
- + Filter cachedFilter = new Filter();
- +
- + class Filter {
- + public Path path;
- + public readonly System.Func<GraphNode, bool> cachedDelegate;
- +
- + public Filter() {
- + cachedDelegate = this.CanTraverse;
- + }
- +
- + bool CanTraverse(GraphNode node) {
- + return path.CanTraverse(node);
- + }
- + }
- +
- public override void Apply (Path p) {
- if (!useRaycasting && !useGraphRaycasting) return;
- var points = p.vectorPath;
- + cachedFilter.path = p;
- - if (ValidateLine(null, null, p.vectorPath[0], p.vectorPath[p.vectorPath.Count-1])) {
- + if (ValidateLine(null, null, p.vectorPath[0], p.vectorPath[p.vectorPath.Count-1], cachedFilter.cachedDelegate)) {
- // A very common case is that there is a straight line to the target.
- var s = p.vectorPath[0];
- var e = p.vectorPath[p.vectorPath.Count-1];
- @@ -130,7 +146,7 @@ namespace Pathfinding {
- points.Reverse();
- }
- - points = quality >= Quality.High ? ApplyDP(p, points) : ApplyGreedy(p, points);
- + points = quality >= Quality.High ? ApplyDP(p, points, cachedFilter.cachedDelegate) : ApplyGreedy(p, points, cachedFilter.cachedDelegate);
- }
- if ((iterations % 2) == 0) points.Reverse();
- }
- @@ -138,7 +154,7 @@ namespace Pathfinding {
- p.vectorPath = points;
- }
- - List<Vector3> ApplyGreedy (Path p, List<Vector3> points) {
- + List<Vector3> ApplyGreedy (Path p, List<Vector3> points, System.Func<GraphNode, bool> filter) {
- bool canBeOriginalNodes = points.Count == p.path.Count;
- int startIndex = 0;
- @@ -157,7 +173,7 @@ namespace Pathfinding {
- }
- Vector3 end = points[endIndex];
- var endNode = canBeOriginalNodes && end == (Vector3)p.path[endIndex].position ? p.path[endIndex] : null;
- - if (!ValidateLine(startNode, endNode, start, end)) break;
- + if (!ValidateLine(startNode, endNode, start, end, filter)) break;
- mn = mx;
- mx *= 2;
- }
- @@ -168,7 +184,7 @@ namespace Pathfinding {
- Vector3 end = points[endIndex];
- var endNode = canBeOriginalNodes && end == (Vector3)p.path[endIndex].position ? p.path[endIndex] : null;
- - if (ValidateLine(startNode, endNode, start, end)) {
- + if (ValidateLine(startNode, endNode, start, end, filter)) {
- mn = mid;
- } else {
- mx = mid;
- @@ -182,7 +198,7 @@ namespace Pathfinding {
- return points;
- }
- - List<Vector3> ApplyDP (Path p, List<Vector3> points) {
- + List<Vector3> ApplyDP (Path p, List<Vector3> points, System.Func<GraphNode, bool> filter) {
- if (DPCosts.Length < points.Count) {
- DPCosts = new float[points.Count];
- DPParents = new int[points.Count];
- @@ -201,7 +217,7 @@ namespace Pathfinding {
- float d2 = d + (points[j] - start).magnitude + 0.0001f;
- if (DPParents[j] == -1 || d2 < DPCosts[j]) {
- var endIsOriginalNode = canBeOriginalNodes && points[j] == (Vector3)p.path[j].position;
- - if (j == i+1 || ValidateLine(startIsOriginalNode ? p.path[i] : null, endIsOriginalNode ? p.path[j] : null, start, points[j])) {
- + if (j == i+1 || ValidateLine(startIsOriginalNode ? p.path[i] : null, endIsOriginalNode ? p.path[j] : null, start, points[j], filter)) {
- DPCosts[j] = d2;
- DPParents[j] = i;
- } else {
- @@ -225,7 +241,7 @@ namespace Pathfinding {
- /** Check if a straight path between v1 and v2 is valid.
- * If both \a n1 and \a n2 are supplied it is assumed that the line goes from the center of \a n1 to the center of \a n2 and a more optimized graph linecast may be done.
- */
- - protected bool ValidateLine (GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2) {
- + protected bool ValidateLine (GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2, System.Func<GraphNode, bool> filter) {
- if (useRaycasting) {
- // Use raycasting to check if a straight path between v1 and v2 is valid
- if (use2DPhysics) {
- @@ -276,11 +292,11 @@ namespace Pathfinding {
- // This method is also more stable when raycasting along a diagonal when the line just touches an obstacle.
- // The normal linecast method may or may not detect that as a hit depending on floating point errors
- // however this method never detect it as an obstacle (and that is very good for this component as it improves the simplification).
- - return !gg.Linecast(n1 as GridNodeBase, n2 as GridNodeBase);
- + return !gg.Linecast(n1 as GridNodeBase, n2 as GridNodeBase, filter);
- } else
- #endif
- if (rayGraph != null) {
- - return !rayGraph.Linecast(v1, v2, n1);
- + return !rayGraph.Linecast(v1, v2, out GraphHitInfo _, null, filter);
- }
- }
- }
- diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
- index 277ef2b5..6e78c8e7 100644
- --- a/Assets/AstarPathfindingProject/changelog.cs
- +++ b/Assets/AstarPathfindingProject/changelog.cs
- @@ -4,6 +4,7 @@
- - 4.3.20
- - Added a filter parameter to graph linecast methods. This can be used to mark additional nodes as not being traversable by the linecast.
- See \link Pathfinding.GridGraph.Linecast(Vector3,Vector3,GraphHitInfo,List<GraphNode>,System.Func<GraphNode,bool>) GridGraph.Linecast\endlink.
- + - The \link Pathfinding.RaycastModifier RaycastModifier\endlink now respects which tags are traversable and any ITraversalProvider set on the path.
- - 4.3.19
- - Fixed the Optimization tab not working when installing the package using the unity package manager.
Add Comment
Please, Sign In to add comment