Guest User

Untitled

a guest
Jun 5th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 15.73 KB | None | 0 0
  1. commit 2a7d9014a466f26cbcfd0a53fb66c8be2eb0bb76
  2. Author: Aron Granberg <aron.granberg@gmail.com>
  3. Date:   Thu Apr 2 13:55:35 2020 +0200
  4.  
  5.     Added a filter parameter to graph linecast methods. This can be used to mark additional nodes as not being traversable by the linecast.
  6.  
  7. diff --git a/Assets/AstarPathfindingProject/Core/Path.cs b/Assets/AstarPathfindingProject/Core/Path.cs
  8. index 217f2c4a..7b9faecc 100644
  9. --- a/Assets/AstarPathfindingProject/Core/Path.cs
  10. +++ b/Assets/AstarPathfindingProject/Core/Path.cs
  11. @@ -357,6 +357,7 @@ namespace Pathfinding {
  12.             if (traversalProvider != null)
  13.                 return traversalProvider.CanTraverse(this, node);
  14.  
  15. +           // Manually inlined code from DefaultITraversalProvider
  16.             unchecked { return node.Walkable && (enabledTags >> (int)node.Tag & 0x1) != 0; }
  17.         }
  18.  
  19. diff --git a/Assets/AstarPathfindingProject/Core/astarclasses.cs b/Assets/AstarPathfindingProject/Core/astarclasses.cs
  20. index c8d6e1ed..5e1f0d04 100644
  21. --- a/Assets/AstarPathfindingProject/Core/astarclasses.cs
  22. +++ b/Assets/AstarPathfindingProject/Core/astarclasses.cs
  23. @@ -692,6 +692,7 @@ namespace Pathfinding {
  24.         bool Linecast(Vector3 start, Vector3 end, GraphNode hint);
  25.         bool Linecast(Vector3 start, Vector3 end, GraphNode hint, out GraphHitInfo hit);
  26.         bool Linecast(Vector3 start, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace);
  27. +       bool Linecast(Vector3 start, Vector3 end, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter);
  28.     }
  29.  
  30.     /** Integer Rectangle.
  31. diff --git a/Assets/AstarPathfindingProject/Generators/GridGenerator.cs b/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
  32. index a211ae3d..d5f4032b 100644
  33. --- a/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
  34. +++ b/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
  35. @@ -3104,15 +3104,32 @@ namespace Pathfinding {
  36.          *
  37.          * \snippet MiscSnippets.cs GridGraph.Linecast2
  38.          *
  39. -        * \version In 3.6.8 this method was rewritten to improve accuracy and performance.
  40. -        * Previously it used a sampling approach which could cut corners of obstacles slightly
  41. -        * and was pretty inefficient.
  42. -        *
  43.          * \astarpro
  44.          *
  45.          * \shadowimage{linecast.png}
  46.          */
  47.         public bool Linecast (Vector3 from, Vector3 to, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace) {
  48. +           return Linecast(from, to, out hit, trace, null);
  49. +       }
  50. +
  51. +       /** Returns if there is an obstacle between \a from and \a to on the graph.
  52. +        * \param [in] from Point to linecast from
  53. +        * \param [in] to Point to linecast to
  54. +        * \param [out] hit Contains info on what was hit, see GraphHitInfo
  55. +        * \param [in] hint This parameter is deprecated. It will be ignored.
  56. +        * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses
  57. +        * \param filter If not null then the delegate will be called for each node and if it returns false the node will be treated as unwalkable and a hit will be returned.
  58. +        *               Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
  59. +        *
  60. +        * This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions.
  61. +        *
  62. +        * \snippet MiscSnippets.cs GridGraph.Linecast2
  63. +        *
  64. +        * \astarpro
  65. +        *
  66. +        * \shadowimage{linecast.png}
  67. +        */
  68. +       public bool Linecast (Vector3 from, Vector3 to, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter) {
  69.             hit = new GraphHitInfo();
  70.  
  71.             hit.origin = from;
  72. @@ -3132,7 +3149,7 @@ namespace Pathfinding {
  73.             var startNode = GetNearest(transform.Transform(fromInGraphSpace), NNConstraint.None).node as GridNodeBase;
  74.             var endNode = GetNearest(transform.Transform(toInGraphSpace), NNConstraint.None).node as GridNodeBase;
  75.  
  76. -           if (!startNode.Walkable) {
  77. +           if (startNode != null && (!startNode.Walkable || (filter != null && !filter(startNode)))) {
  78.                 hit.node = startNode;
  79.                 // Hit point is the point where the segment intersects with the graph boundary
  80.                 // or just #from if it starts inside the graph
  81. @@ -3215,9 +3232,9 @@ namespace Pathfinding {
  82.                 // and pick the appropriate direction to move in
  83.                 int ndir = nerror < 0 ? directionToIncreaseError : directionToReduceError;
  84.  
  85. -               // Check we can move in that direction
  86. +               // Check if we can move in that direction
  87.                 var other = current.GetNeighbourAlongDirection(ndir);
  88. -               if (other != null) {
  89. +               if (other != null && (filter == null || filter(other))) {
  90.                     current = other;
  91.                 } else {
  92.                     // Hit obstacle
  93. @@ -3293,6 +3310,11 @@ namespace Pathfinding {
  94.         }
  95.  
  96.         /** Returns if there is an obstacle between the two nodes on the graph.
  97. +        * \param fromNode Node to start from.
  98. +        * \param toNode Node to try to reach using a straight line.
  99. +        * \param filter If not null then the delegate will be called for each node and if it returns false the node will be treated as unwalkable and a hit will be returned.
  100. +        *               Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
  101. +        *
  102.          * This method is very similar to the other Linecast methods however it is much faster
  103.          * due to being able to use only integer math and not having to look up which node is closest to a particular input point.
  104.          *
  105. @@ -3300,7 +3322,7 @@ namespace Pathfinding {
  106.          *
  107.          * \astarpro
  108.          */
  109. -       public bool Linecast (GridNodeBase fromNode, GridNodeBase toNode) {
  110. +       public bool Linecast (GridNodeBase fromNode, GridNodeBase toNode, System.Func<GraphNode, bool> filter = null) {
  111.             var dir = new Int2(toNode.XCoordinateInGrid - fromNode.XCoordinateInGrid, toNode.ZCoordinateInGrid - fromNode.ZCoordinateInGrid);
  112.  
  113.             // How much further we move away from (or towards) the line when walking along the primary direction (e.g up and right or down and left).
  114. @@ -3338,6 +3360,10 @@ namespace Pathfinding {
  115.             int directionToIncreaseError = (quadrant + 2) & 0x3;
  116.             int directionDiagonal = (dir.x != 0 && dir.y != 0) ? 4 + ((quadrant + 1) & 0x3) : -1;
  117.             Int2 offset = new Int2(0, 0);
  118. +
  119. +           // Use the filter
  120. +           if (filter != null && fromNode != null && !filter(fromNode)) fromNode = null;
  121. +
  122.             while (fromNode != null && fromNode.NodeInGridIndex != toNode.NodeInGridIndex) {
  123.                 // This is proportional to the distance between the line and the node
  124.                 var error = CrossMagnitude(dir, offset) * 2;
  125. @@ -3351,6 +3377,10 @@ namespace Pathfinding {
  126.                 if (nerror == 0 && directionDiagonal != -1) ndir = directionDiagonal;
  127.  
  128.                 fromNode = fromNode.GetNeighbourAlongDirection(ndir);
  129. +
  130. +               // Use the filter
  131. +               if (filter != null && fromNode != null && !filter(fromNode)) fromNode = null;
  132. +
  133.                 offset += new Int2(neighbourXOffsets[ndir], neighbourZOffsets[ndir]);
  134.             }
  135.             return fromNode != toNode;
  136. diff --git a/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs b/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs
  137. index 664b9802..1ad880d2 100644
  138. --- a/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs
  139. +++ b/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs
  140. @@ -1013,10 +1013,11 @@ namespace Pathfinding {
  141.         }
  142.  
  143.         /** Returns if there is an obstacle between \a origin and \a end on the graph.
  144. -        * \param [in] origin Point to linecast from
  145. -        * \param [in] end Point to linecast to
  146. -        * \param [out] hit Contains info on what was hit, see GraphHitInfo
  147. -        * \param [in] hint You need to pass the node closest to the start point
  148. +        * \param [in] origin Point to linecast from.
  149. +        * \param [in] end Point to linecast to.
  150. +        * \param [out] hit Contains info on what was hit, see GraphHitInfo.
  151. +        * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
  152. +        *             If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
  153.          *
  154.          * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
  155.          * \astarpro
  156. @@ -1028,9 +1029,10 @@ namespace Pathfinding {
  157.         }
  158.  
  159.         /** Returns if there is an obstacle between \a origin and \a end on the graph.
  160. -        * \param [in] origin Point to linecast from
  161. -        * \param [in] end Point to linecast to
  162. -        * \param [in] hint You need to pass the node closest to the start point
  163. +        * \param [in] origin Point to linecast from.
  164. +        * \param [in] end Point to linecast to.
  165. +        * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
  166. +        *             If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
  167.          *
  168.          * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
  169.          * \astarpro
  170. @@ -1044,11 +1046,12 @@ namespace Pathfinding {
  171.         }
  172.  
  173.         /** Returns if there is an obstacle between \a origin and \a end on the graph.
  174. -        * \param [in] origin Point to linecast from
  175. -        * \param [in] end Point to linecast to
  176. -        * \param [out] hit Contains info on what was hit, see GraphHitInfo
  177. -        * \param [in] hint You need to pass the node closest to the start point
  178. -        * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses
  179. +        * \param [in] origin Point to linecast from.
  180. +        * \param [in] end Point to linecast to.
  181. +        * \param [out] hit Contains info on what was hit, see GraphHitInfo.
  182. +        * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
  183. +        *             If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
  184. +        * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses.
  185.          *
  186.          * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
  187.          * \astarpro
  188. @@ -1060,11 +1063,29 @@ namespace Pathfinding {
  189.         }
  190.  
  191.         /** Returns if there is an obstacle between \a origin and \a end on the graph.
  192. -        * \param [in] graph The graph to perform the search on
  193. -        * \param [in] origin Point to start from
  194. -        * \param [in] end Point to linecast to
  195. -        * \param [out] hit Contains info on what was hit, see GraphHitInfo
  196. -        * \param [in] hint You need to pass the node closest to the start point, if null, a search for the closest node will be done
  197. +        * \param [in] origin Point to linecast from.
  198. +        * \param [in] end Point to linecast to.
  199. +        * \param [out] hit Contains info on what was hit, see GraphHitInfo.
  200. +        * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses.
  201. +        * \param filter If not null then the delegate will be called for each node and if it returns false the node will be treated as unwalkable and a hit will be returned.
  202. +        *               Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
  203. +        *
  204. +        * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
  205. +        * \astarpro
  206. +        *
  207. +        * \shadowimage{linecast.png}
  208. +        */
  209. +       public bool Linecast (Vector3 origin, Vector3 end, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter) {
  210. +           return Linecast(this, origin, end, null, out hit, trace, filter);
  211. +       }
  212. +
  213. +       /** Returns if there is an obstacle between \a origin and \a end on the graph.
  214. +        * \param [in] graph The graph to perform the search on.
  215. +        * \param [in] origin Point to start from.
  216. +        * \param [in] end Point to linecast to.
  217. +        * \param [out] hit Contains info on what was hit, see GraphHitInfo.
  218. +        * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
  219. +        *             If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
  220.          *
  221.          * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
  222.          * \astarpro
  223. @@ -1122,6 +1143,8 @@ namespace Pathfinding {
  224.          * \param [out] hit Contains info on what was hit, see GraphHitInfo
  225.          * \param [in] hint If you already know the node which contains the \a origin point, you may pass it here for slighly improved performance. If null, a search for the closest node will be done.
  226.          * \param trace If a list is passed, then it will be filled with all nodes along the line up until it hits an obstacle or reaches the end.
  227. +        * \param filter If not null then the delegate will be called for each node and if it returns false the node will be treated as unwalkable and a hit will be returned.
  228. +        *               Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
  229.          *
  230.          * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersections.
  231.          *
  232. @@ -1132,7 +1155,7 @@ namespace Pathfinding {
  233.          *
  234.          * \shadowimage{linecast.png}
  235.          */
  236. -       public static bool Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace) {
  237. +       public static bool Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter = null) {
  238.             hit = new GraphHitInfo();
  239.  
  240.             if (float.IsNaN(origin.x + origin.y + origin.z)) throw new System.ArgumentException("origin is NaN");
  241. @@ -1154,7 +1177,7 @@ namespace Pathfinding {
  242.             var i3originInGraphSpace = node.ClosestPointOnNodeXZInGraphSpace(origin);
  243.             hit.origin = graph.transform.Transform((Vector3)i3originInGraphSpace);
  244.  
  245. -           if (!node.Walkable) {
  246. +           if (!node.Walkable || (filter != null && !filter(node))) {
  247.                 hit.node = node;
  248.                 hit.point = hit.origin;
  249.                 hit.tangentOrigin = hit.origin;
  250. @@ -1215,7 +1238,7 @@ namespace Pathfinding {
  251.                             // This might be the next node that we enter
  252.  
  253.                             var neighbour = nodeConnections[i].node as TriangleMeshNode;
  254. -                           if (neighbour == null || !neighbour.Walkable) continue;
  255. +                           if (neighbour == null || !neighbour.Walkable || (filter != null && !filter(neighbour))) continue;
  256.  
  257.                             var neighbourConnections = neighbour.connections;
  258.                             int shapeEdgeB = -1;
  259. @@ -1228,7 +1251,7 @@ namespace Pathfinding {
  260.  
  261.                             if (shapeEdgeB == -1) {
  262.                                 // Connection was mono-directional!
  263. -                               // This shouldn't normally not happen on navmeshes happen on navmeshes (when the shapeEdge matches at least) unless a user has done something strange to the navmesh.
  264. +                               // This shouldn't normally happen on navmeshes (when the shapeEdge matches at least) unless a user has done something strange to the navmesh.
  265.                                 continue;
  266.                             }
  267.  
  268. diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
  269. index 5c4b5be9..277ef2b5 100644
  270. --- a/Assets/AstarPathfindingProject/changelog.cs
  271. +++ b/Assets/AstarPathfindingProject/changelog.cs
  272. @@ -1,6 +1,10 @@
  273.  /** \page changelog Changelog
  274.  \order{-10}
  275.  
  276. +- 4.3.20
  277. +   - Added a filter parameter to graph linecast methods. This can be used to mark additional nodes as not being traversable by the linecast.
  278. +       See \link Pathfinding.GridGraph.Linecast(Vector3,Vector3,GraphHitInfo,List<GraphNode>,System.Func<GraphNode,bool>) GridGraph.Linecast\endlink.
  279. +
  280.  - 4.3.19
  281.     - Fixed the Optimization tab not working when installing the package using the unity package manager.
Add Comment
Please, Sign In to add comment