Guest User

Untitled

a guest
May 30th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.AI;
  6.  
  7. public class FindOrphanedNavmeshes : Editor {
  8.  
  9.  
  10.     static Vector3 _originPoint = Vector3.zero;
  11.  
  12.     protected const int STEP_SIZE = 3;  //scan granularity
  13.     static RaycastHit _raycastHit = new RaycastHit();
  14.     static NavMeshHit _navMeshHit = new NavMeshHit();
  15.     static LayerMask _mask;
  16.     static Vector3 _navMeshPoint = Vector3.zero; //cached for speed
  17.     static HashSet<Vector3> _badPointsList = new HashSet<Vector3>();
  18.  
  19.     static bool _scanning = false;
  20.     static double _scanStartTime = 0.0f;
  21.     static bool _showDebug = true;
  22.  
  23.     static Bounds _bounds = new Bounds();
  24.     static List<Vector3> _nearbyCheckList = new List<Vector3>(1);
  25.     const int _walkableMask = 1 << 0;  //walkable mask
  26.     static int _navmeshTraversable = 0;
  27.     static NavMeshPath _navMeshPath = new NavMeshPath();
  28.  
  29.     //loop indices
  30.     static int xCurr = 0;
  31.     static int yCurr = 0;
  32.     static int zCurr = 0;
  33.  
  34.  
  35.     [MenuItem("Tools/Navmesh/StopFindingOrphans")]
  36.     static void ForceStop() {
  37.         _scanning = false;
  38.         EditorApplication.update -= Scan;
  39.         xCurr = 0;
  40.         yCurr = 0;
  41.         zCurr = 0;
  42.         Debug.Log("Forced stopped finding orphaned navmeshes");
  43.     }
  44.  
  45.     [MenuItem("Tools/Navmesh/DisplayRecentOrphanedNaveshes")]
  46.     static void DisplayOrphans() {
  47.         foreach (Vector3 point in _badPointsList){
  48.             Debug.DrawLine(point, point + 10*Vector3.up, Color.red, 10.0f);
  49.         }
  50.     }
  51.  
  52.  
  53.     [MenuItem("Tools/Navmesh/FindOrphanedNavmeshes")]
  54.     public static void FindOrphanedNavmeshesFunc() {
  55.         _mask = LayerMask.GetMask("Buildings", "Ground", "Barrier", "Default");
  56.         _navmeshTraversable = 1 << NavMesh.GetAreaFromName("Walkable") |
  57.             1 << NavMesh.GetAreaFromName("Jump") |
  58.             1 << NavMesh.GetAreaFromName("Climbable");
  59.  
  60.         if (_scanning) { Debug.Log("Currently scanning! Please wait"); return; }
  61.         if (Selection.objects == null || Selection.objects.Length == 0) { return; }
  62.         Debug.Log("Scanning for Orphaned Navmeshes...");
  63.         _scanning = true;
  64.         _scanStartTime = EditorApplication.timeSinceStartup;
  65.  
  66.         _badPointsList.Clear();
  67.         _bounds = new Bounds();
  68.         FixEndPoints.FixNavMeshEndPoints(); //this helps in case some of them are messed up
  69.  
  70.         //get a bounds of every collider
  71.         Collider[] colliders = null;
  72.         colliders = Selection.activeGameObject.GetComponentsInChildren<Collider>();
  73.         if (colliders == null || colliders.Length == 0) { return; }
  74.         for (int i = 0; i < colliders.Length; i++){
  75.             if (colliders[i].isTrigger) { continue;  } //ignore triggers
  76.             if (_bounds.size == Vector3.zero) { _bounds = colliders[i].bounds; continue;  } //start with this one, don't start at 0
  77.             _bounds.Encapsulate(colliders[i].bounds);
  78.         }
  79.         _bounds.Expand(STEP_SIZE + 2); //+ to account for rounding
  80.         DrawBounds(_bounds);
  81.  
  82.         if (_bounds.extents == Vector3.zero) { Debug.Log("No colliders to scan! Please select some colliders"); ForceStop(); return; }
  83.  
  84.         //starting positions for scanning. y goes up to down
  85.         xCurr = (int)_bounds.min.x;
  86.         yCurr = (int)_bounds.max.y;
  87.         zCurr = (int)_bounds.min.z;
  88.  
  89.         EditorApplication.update += Scan;
  90.     }
  91.  
  92.     static void Scan() {
  93.  
  94.         double frameStart = EditorApplication.timeSinceStartup;
  95.         //for every point in the bounds
  96.         for (int x = xCurr; x <= (int)_bounds.max.x; x += 1) {
  97.             for (int z = zCurr; z <= (int)_bounds.max.z; z += 1) {
  98.                 for (int y = yCurr; y >= (int)_bounds.min.y; y -= 1) { //start from the roof and go down
  99.                     //Debug.Log("updating! xyz = "+x+","+y+","+z+" And boundsmax = "+_bounds.max);
  100.                     CheckPoint(x, y, z); //this adds points to the octree and edgepositions
  101.                                          //continue later, this frame is taking too long (<60fps)
  102.                     y = nextY;  //which is set in CheckPoint   
  103.                     if (EditorApplication.timeSinceStartup - frameStart > 0.0166) {
  104.                         xCurr = x;
  105.                         yCurr = y;
  106.                         zCurr = z;
  107.                         if (_showDebug) {
  108.                             SceneView.RepaintAll();
  109.                         }
  110.                         return;
  111.                     }
  112.                 }
  113.                 yCurr = (int)_bounds.max.y; //max since we go down not up
  114.             }
  115.             zCurr = (int)_bounds.min.z;
  116.         }
  117.  
  118.         double scanTime = EditorApplication.timeSinceStartup - _scanStartTime;
  119.         Debug.Log("Done scanning! Found "+_badPointsList.Count +" orphaned positions in "+scanTime+" seconds. If any, please fix!");
  120.         //clean up
  121.         _scanning = false;
  122.         EditorApplication.update -= Scan;
  123.     }
  124.  
  125.     static int nextY = 99999;
  126.     static void CheckPoint(int x, int y, int z) {
  127.         Vector3 raycastOrigin = new Vector3(x, y, z);
  128.         nextY = (int) _bounds.min.y; //if we don't hit anything, we are done
  129.         //Debug.Log("raycastorigin = "+raycastOrigin);
  130.         //raycast down and find colliders on roofs
  131.         //distance is to the bottom of the bounds
  132.         if (!Physics.Raycast(raycastOrigin, Vector3.down, out _raycastHit, (y - _bounds.min.y) + 1.0f, _mask)) {
  133.             //Debug.DrawLine(raycastOrigin, raycastOrigin + Vector3.down * ((y - _bounds.min.y) + 1.0f), Color.red, 3.0f);
  134.             return;  //done with this Y column
  135.         }
  136.         //Debug.DrawLine(hit.point, hit.point+ Vector3.up, Color.green, 3.0f); //the top point, up a bit    
  137.  
  138.         //optmization here: if we hit something with the raycast, even if we didn't keep it, return that y delta
  139.         nextY = (int)_raycastHit.point.y - 1;
  140.  
  141.         //now find the closest navmesh point to the physics point
  142.         if (!NavMesh.SamplePosition(_raycastHit.point, out _navMeshHit, STEP_SIZE, _walkableMask)){  
  143.             //Debug.DrawLine(hit.point, hit.point + Vector3.up, Color.yellow, 3.0f);    
  144.             return;
  145.         }
  146.         _navMeshPoint = _navMeshHit.position;
  147.  
  148.         //if this is the first hit, then save it, and we'll try to navigate to this position
  149.         //TODO: Find some way to make sure this position is not also orphaned
  150.         if (_originPoint == Vector3.zero) {
  151.             _originPoint = _navMeshPoint;
  152.             Debug.DrawLine(_originPoint, _originPoint + 5*Vector3.up, Color.cyan, 3.0f); //the top point, up a bit  
  153.             Debug.Log("Origin point is now " + _originPoint);
  154.             return; //can't connect to self so we're good
  155.         }
  156.         if (_originPoint == Vector3.zero) {
  157.             Debug.Log("This shouldn't have happened! origin is now " + _originPoint);
  158.             ForceStop();
  159.             return;
  160.         }
  161.  
  162.         //now check if this point can connect to the origin point
  163.         if (NavMesh.CalculatePath(_originPoint, _navMeshPoint, _navmeshTraversable, _navMeshPath) &&
  164.             _navMeshPath.status == NavMeshPathStatus.PathComplete  
  165.         ){
  166.             //we have a path so we're good
  167.             Debug.DrawLine(_navMeshPoint, _navMeshPoint + Vector3.up, Color.green, 3.0f); //the top point, up a bit    
  168.             return;
  169.         }
  170.  
  171.         //now we have an orphaned one
  172.  
  173.         //add!
  174.         _badPointsList.Add(_navMeshPoint);
  175.         Debug.DrawLine(_navMeshPoint, _navMeshPoint + Vector3.up, Color.red, 3.0f); //the top point, up a bit  
  176.     }
  177.  
  178.     static void DrawBounds(Bounds bounds) {
  179.         Vector3 v3Center = bounds.center;
  180.         Vector3 v3Extents = bounds.extents;
  181.  
  182.         Vector3 v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
  183.         Vector3 v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
  184.         Vector3 v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
  185.         Vector3 v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
  186.         Vector3 v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
  187.         Vector3 v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
  188.         Vector3 v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
  189.         Vector3 v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner
  190.  
  191.         Debug.DrawLine(v3FrontTopLeft, v3FrontTopRight, Color.cyan, 3.0f);
  192.         Debug.DrawLine(v3FrontTopLeft, v3BackTopLeft, Color.cyan, 3.0f);
  193.         Debug.DrawLine(v3FrontTopLeft, v3FrontBottomLeft, Color.cyan, 3.0f);
  194.         Debug.DrawLine(v3FrontTopRight, v3FrontBottomRight, Color.cyan, 3.0f);
  195.         Debug.DrawLine(v3FrontTopRight, v3BackTopRight, Color.cyan, 3.0f);
  196.         Debug.DrawLine(v3FrontBottomLeft, v3FrontBottomRight, Color.cyan, 3.0f);
  197.         Debug.DrawLine(v3FrontBottomLeft, v3BackBottomLeft, Color.cyan, 3.0f);
  198.         Debug.DrawLine(v3FrontBottomRight, v3BackBottomRight, Color.cyan, 3.0f);
  199.         Debug.DrawLine(v3BackBottomLeft, v3BackBottomRight, Color.cyan, 3.0f);
  200.         Debug.DrawLine(v3BackTopLeft, v3BackTopRight, Color.cyan, 3.0f);
  201.         Debug.DrawLine(v3BackTopLeft, v3BackBottomLeft, Color.cyan, 3.0f);
  202.         Debug.DrawLine(v3BackTopRight, v3BackBottomRight, Color.cyan, 3.0f);
  203.     }
  204.  
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment