Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- commit 2a7d9014a466f26cbcfd0a53fb66c8be2eb0bb76
- Author: Aron Granberg <aron.granberg@gmail.com>
- Date: Thu Apr 2 13:55:35 2020 +0200
- Added a filter parameter to graph linecast methods. This can be used to mark additional nodes as not being traversable by the linecast.
- diff --git a/Assets/AstarPathfindingProject/Core/Path.cs b/Assets/AstarPathfindingProject/Core/Path.cs
- index 217f2c4a..7b9faecc 100644
- --- a/Assets/AstarPathfindingProject/Core/Path.cs
- +++ b/Assets/AstarPathfindingProject/Core/Path.cs
- @@ -357,6 +357,7 @@ namespace Pathfinding {
- if (traversalProvider != null)
- return traversalProvider.CanTraverse(this, node);
- + // Manually inlined code from DefaultITraversalProvider
- unchecked { return node.Walkable && (enabledTags >> (int)node.Tag & 0x1) != 0; }
- }
- diff --git a/Assets/AstarPathfindingProject/Core/astarclasses.cs b/Assets/AstarPathfindingProject/Core/astarclasses.cs
- index c8d6e1ed..5e1f0d04 100644
- --- a/Assets/AstarPathfindingProject/Core/astarclasses.cs
- +++ b/Assets/AstarPathfindingProject/Core/astarclasses.cs
- @@ -692,6 +692,7 @@ namespace Pathfinding {
- bool Linecast(Vector3 start, Vector3 end, GraphNode hint);
- bool Linecast(Vector3 start, Vector3 end, GraphNode hint, out GraphHitInfo hit);
- bool Linecast(Vector3 start, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace);
- + bool Linecast(Vector3 start, Vector3 end, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter);
- }
- /** Integer Rectangle.
- diff --git a/Assets/AstarPathfindingProject/Generators/GridGenerator.cs b/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
- index a211ae3d..d5f4032b 100644
- --- a/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
- +++ b/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
- @@ -3104,15 +3104,32 @@ namespace Pathfinding {
- *
- * \snippet MiscSnippets.cs GridGraph.Linecast2
- *
- - * \version In 3.6.8 this method was rewritten to improve accuracy and performance.
- - * Previously it used a sampling approach which could cut corners of obstacles slightly
- - * and was pretty inefficient.
- - *
- * \astarpro
- *
- * \shadowimage{linecast.png}
- */
- public bool Linecast (Vector3 from, Vector3 to, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace) {
- + return Linecast(from, to, out hit, trace, null);
- + }
- +
- + /** Returns if there is an obstacle between \a from and \a to on the graph.
- + * \param [in] from Point to linecast from
- + * \param [in] to Point to linecast to
- + * \param [out] hit Contains info on what was hit, see GraphHitInfo
- + * \param [in] hint This parameter is deprecated. It will be ignored.
- + * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses
- + * \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.
- + * Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
- + *
- + * This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions.
- + *
- + * \snippet MiscSnippets.cs GridGraph.Linecast2
- + *
- + * \astarpro
- + *
- + * \shadowimage{linecast.png}
- + */
- + public bool Linecast (Vector3 from, Vector3 to, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter) {
- hit = new GraphHitInfo();
- hit.origin = from;
- @@ -3132,7 +3149,7 @@ namespace Pathfinding {
- var startNode = GetNearest(transform.Transform(fromInGraphSpace), NNConstraint.None).node as GridNodeBase;
- var endNode = GetNearest(transform.Transform(toInGraphSpace), NNConstraint.None).node as GridNodeBase;
- - if (!startNode.Walkable) {
- + if (startNode != null && (!startNode.Walkable || (filter != null && !filter(startNode)))) {
- hit.node = startNode;
- // Hit point is the point where the segment intersects with the graph boundary
- // or just #from if it starts inside the graph
- @@ -3215,9 +3232,9 @@ namespace Pathfinding {
- // and pick the appropriate direction to move in
- int ndir = nerror < 0 ? directionToIncreaseError : directionToReduceError;
- - // Check we can move in that direction
- + // Check if we can move in that direction
- var other = current.GetNeighbourAlongDirection(ndir);
- - if (other != null) {
- + if (other != null && (filter == null || filter(other))) {
- current = other;
- } else {
- // Hit obstacle
- @@ -3293,6 +3310,11 @@ namespace Pathfinding {
- }
- /** Returns if there is an obstacle between the two nodes on the graph.
- + * \param fromNode Node to start from.
- + * \param toNode Node to try to reach using a straight line.
- + * \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.
- + * Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
- + *
- * This method is very similar to the other Linecast methods however it is much faster
- * due to being able to use only integer math and not having to look up which node is closest to a particular input point.
- *
- @@ -3300,7 +3322,7 @@ namespace Pathfinding {
- *
- * \astarpro
- */
- - public bool Linecast (GridNodeBase fromNode, GridNodeBase toNode) {
- + public bool Linecast (GridNodeBase fromNode, GridNodeBase toNode, System.Func<GraphNode, bool> filter = null) {
- var dir = new Int2(toNode.XCoordinateInGrid - fromNode.XCoordinateInGrid, toNode.ZCoordinateInGrid - fromNode.ZCoordinateInGrid);
- // 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).
- @@ -3338,6 +3360,10 @@ namespace Pathfinding {
- int directionToIncreaseError = (quadrant + 2) & 0x3;
- int directionDiagonal = (dir.x != 0 && dir.y != 0) ? 4 + ((quadrant + 1) & 0x3) : -1;
- Int2 offset = new Int2(0, 0);
- +
- + // Use the filter
- + if (filter != null && fromNode != null && !filter(fromNode)) fromNode = null;
- +
- while (fromNode != null && fromNode.NodeInGridIndex != toNode.NodeInGridIndex) {
- // This is proportional to the distance between the line and the node
- var error = CrossMagnitude(dir, offset) * 2;
- @@ -3351,6 +3377,10 @@ namespace Pathfinding {
- if (nerror == 0 && directionDiagonal != -1) ndir = directionDiagonal;
- fromNode = fromNode.GetNeighbourAlongDirection(ndir);
- +
- + // Use the filter
- + if (filter != null && fromNode != null && !filter(fromNode)) fromNode = null;
- +
- offset += new Int2(neighbourXOffsets[ndir], neighbourZOffsets[ndir]);
- }
- return fromNode != toNode;
- diff --git a/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs b/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs
- index 664b9802..1ad880d2 100644
- --- a/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs
- +++ b/Assets/AstarPathfindingProject/Generators/NavmeshBase.cs
- @@ -1013,10 +1013,11 @@ namespace Pathfinding {
- }
- /** Returns if there is an obstacle between \a origin and \a end on the graph.
- - * \param [in] origin Point to linecast from
- - * \param [in] end Point to linecast to
- - * \param [out] hit Contains info on what was hit, see GraphHitInfo
- - * \param [in] hint You need to pass the node closest to the start point
- + * \param [in] origin Point to linecast from.
- + * \param [in] end Point to linecast to.
- + * \param [out] hit Contains info on what was hit, see GraphHitInfo.
- + * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
- + * If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
- *
- * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
- * \astarpro
- @@ -1028,9 +1029,10 @@ namespace Pathfinding {
- }
- /** Returns if there is an obstacle between \a origin and \a end on the graph.
- - * \param [in] origin Point to linecast from
- - * \param [in] end Point to linecast to
- - * \param [in] hint You need to pass the node closest to the start point
- + * \param [in] origin Point to linecast from.
- + * \param [in] end Point to linecast to.
- + * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
- + * If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
- *
- * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
- * \astarpro
- @@ -1044,11 +1046,12 @@ namespace Pathfinding {
- }
- /** Returns if there is an obstacle between \a origin and \a end on the graph.
- - * \param [in] origin Point to linecast from
- - * \param [in] end Point to linecast to
- - * \param [out] hit Contains info on what was hit, see GraphHitInfo
- - * \param [in] hint You need to pass the node closest to the start point
- - * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses
- + * \param [in] origin Point to linecast from.
- + * \param [in] end Point to linecast to.
- + * \param [out] hit Contains info on what was hit, see GraphHitInfo.
- + * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
- + * If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
- + * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses.
- *
- * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
- * \astarpro
- @@ -1060,11 +1063,29 @@ namespace Pathfinding {
- }
- /** Returns if there is an obstacle between \a origin and \a end on the graph.
- - * \param [in] graph The graph to perform the search on
- - * \param [in] origin Point to start from
- - * \param [in] end Point to linecast to
- - * \param [out] hit Contains info on what was hit, see GraphHitInfo
- - * \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
- + * \param [in] origin Point to linecast from.
- + * \param [in] end Point to linecast to.
- + * \param [out] hit Contains info on what was hit, see GraphHitInfo.
- + * \param trace If a list is passed, then it will be filled with all nodes the linecast traverses.
- + * \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.
- + * Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
- + *
- + * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
- + * \astarpro
- + *
- + * \shadowimage{linecast.png}
- + */
- + public bool Linecast (Vector3 origin, Vector3 end, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter) {
- + return Linecast(this, origin, end, null, out hit, trace, filter);
- + }
- +
- + /** Returns if there is an obstacle between \a origin and \a end on the graph.
- + * \param [in] graph The graph to perform the search on.
- + * \param [in] origin Point to start from.
- + * \param [in] end Point to linecast to.
- + * \param [out] hit Contains info on what was hit, see GraphHitInfo.
- + * \param [in] hint You may pass the node closest to the start point if you already know it for a minor performance boost.
- + * If null, a search for the closest node will be done. This parameter is mostly deprecated and should be avoided. Pass null instead.
- *
- * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersection.
- * \astarpro
- @@ -1122,6 +1143,8 @@ namespace Pathfinding {
- * \param [out] hit Contains info on what was hit, see GraphHitInfo
- * \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.
- * \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.
- + * \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.
- + * Note that unwalkable nodes are \a always treated as unwalkable regardless of what this filter returns.
- *
- * This is not the same as Physics.Linecast, this function traverses the \b graph and looks for collisions instead of checking for collider intersections.
- *
- @@ -1132,7 +1155,7 @@ namespace Pathfinding {
- *
- * \shadowimage{linecast.png}
- */
- - public static bool Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace) {
- + public static bool Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter = null) {
- hit = new GraphHitInfo();
- if (float.IsNaN(origin.x + origin.y + origin.z)) throw new System.ArgumentException("origin is NaN");
- @@ -1154,7 +1177,7 @@ namespace Pathfinding {
- var i3originInGraphSpace = node.ClosestPointOnNodeXZInGraphSpace(origin);
- hit.origin = graph.transform.Transform((Vector3)i3originInGraphSpace);
- - if (!node.Walkable) {
- + if (!node.Walkable || (filter != null && !filter(node))) {
- hit.node = node;
- hit.point = hit.origin;
- hit.tangentOrigin = hit.origin;
- @@ -1215,7 +1238,7 @@ namespace Pathfinding {
- // This might be the next node that we enter
- var neighbour = nodeConnections[i].node as TriangleMeshNode;
- - if (neighbour == null || !neighbour.Walkable) continue;
- + if (neighbour == null || !neighbour.Walkable || (filter != null && !filter(neighbour))) continue;
- var neighbourConnections = neighbour.connections;
- int shapeEdgeB = -1;
- @@ -1228,7 +1251,7 @@ namespace Pathfinding {
- if (shapeEdgeB == -1) {
- // Connection was mono-directional!
- - // 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.
- + // This shouldn't normally happen on navmeshes (when the shapeEdge matches at least) unless a user has done something strange to the navmesh.
- continue;
- }
- diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
- index 5c4b5be9..277ef2b5 100644
- --- a/Assets/AstarPathfindingProject/changelog.cs
- +++ b/Assets/AstarPathfindingProject/changelog.cs
- @@ -1,6 +1,10 @@
- /** \page changelog Changelog
- \order{-10}
- +- 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.
- +
- - 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