123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.Profiling;
- namespace Pathfinding.RVO {
- using Pathfinding.Util;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [AddComponentMenu("Pathfinding/Local Avoidance/RVO Navmesh")]
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_r_v_o_1_1_r_v_o_navmesh.php")]
- public class RVONavmesh : GraphModifier {
-
-
-
-
-
-
- public float wallHeight = 5;
-
- readonly List<ObstacleVertex> obstacles = new List<ObstacleVertex>();
-
- Simulator lastSim;
- public override void OnPostCacheLoad () {
- OnLatePostScan();
- }
- public override void OnGraphsPostUpdate () {
- OnLatePostScan();
- }
- public override void OnLatePostScan () {
- if (!Application.isPlaying) return;
- Profiler.BeginSample("Update RVO Obstacles From Graphs");
- RemoveObstacles();
- NavGraph[] graphs = AstarPath.active.graphs;
- RVOSimulator rvosim = RVOSimulator.active;
- if (rvosim == null) throw new System.NullReferenceException("No RVOSimulator could be found in the scene. Please add one to any GameObject");
-
- lastSim = rvosim.GetSimulator();
- for (int i = 0; i < graphs.Length; i++) {
- RecastGraph recast = graphs[i] as RecastGraph;
- INavmesh navmesh = graphs[i] as INavmesh;
- GridGraph grid = graphs[i] as GridGraph;
- if (recast != null) {
- foreach (var tile in recast.GetTiles()) {
- AddGraphObstacles(lastSim, tile);
- }
- } else if (navmesh != null) {
- AddGraphObstacles(lastSim, navmesh);
- } else if (grid != null) {
- AddGraphObstacles(lastSim, grid);
- }
- }
- Profiler.EndSample();
- }
- protected override void OnDisable () {
- base.OnDisable();
- RemoveObstacles();
- }
-
- public void RemoveObstacles () {
- if (lastSim != null) {
- for (int i = 0; i < obstacles.Count; i++) lastSim.RemoveObstacle(obstacles[i]);
- lastSim = null;
- }
- obstacles.Clear();
- }
-
- void AddGraphObstacles (Pathfinding.RVO.Simulator sim, GridGraph grid) {
- bool reverse = Vector3.Dot(grid.transform.TransformVector(Vector3.up), sim.movementPlane == MovementPlane.XY ? Vector3.back : Vector3.up) > 0;
- GraphUtilities.GetContours(grid, vertices => {
-
-
-
- if (reverse) System.Array.Reverse(vertices);
- obstacles.Add(sim.AddObstacle(vertices, wallHeight, true));
- }, wallHeight*0.4f);
- }
-
- void AddGraphObstacles (Pathfinding.RVO.Simulator simulator, INavmesh navmesh) {
- GraphUtilities.GetContours(navmesh, (vertices, cycle) => {
- var verticesV3 = new Vector3[vertices.Count];
- for (int i = 0; i < verticesV3.Length; i++) verticesV3[i] = (Vector3)vertices[i];
-
- ListPool<Int3>.Release(vertices);
- obstacles.Add(simulator.AddObstacle(verticesV3, wallHeight, cycle));
- });
- }
- }
- }
|