Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEngine.AI;
- public class FindOrphanedNavmeshes : Editor {
- static Vector3 _originPoint = Vector3.zero;
- protected const int STEP_SIZE = 3; //scan granularity
- static RaycastHit _raycastHit = new RaycastHit();
- static NavMeshHit _navMeshHit = new NavMeshHit();
- static LayerMask _mask;
- static Vector3 _navMeshPoint = Vector3.zero; //cached for speed
- static HashSet<Vector3> _badPointsList = new HashSet<Vector3>();
- static bool _scanning = false;
- static double _scanStartTime = 0.0f;
- static bool _showDebug = true;
- static Bounds _bounds = new Bounds();
- static List<Vector3> _nearbyCheckList = new List<Vector3>(1);
- const int _walkableMask = 1 << 0; //walkable mask
- static int _navmeshTraversable = 0;
- static NavMeshPath _navMeshPath = new NavMeshPath();
- //loop indices
- static int xCurr = 0;
- static int yCurr = 0;
- static int zCurr = 0;
- [MenuItem("Tools/Navmesh/StopFindingOrphans")]
- static void ForceStop() {
- _scanning = false;
- EditorApplication.update -= Scan;
- xCurr = 0;
- yCurr = 0;
- zCurr = 0;
- Debug.Log("Forced stopped finding orphaned navmeshes");
- }
- [MenuItem("Tools/Navmesh/DisplayRecentOrphanedNaveshes")]
- static void DisplayOrphans() {
- foreach (Vector3 point in _badPointsList){
- Debug.DrawLine(point, point + 10*Vector3.up, Color.red, 10.0f);
- }
- }
- [MenuItem("Tools/Navmesh/FindOrphanedNavmeshes")]
- public static void FindOrphanedNavmeshesFunc() {
- _mask = LayerMask.GetMask("Buildings", "Ground", "Barrier", "Default");
- _navmeshTraversable = 1 << NavMesh.GetAreaFromName("Walkable") |
- 1 << NavMesh.GetAreaFromName("Jump") |
- 1 << NavMesh.GetAreaFromName("Climbable");
- if (_scanning) { Debug.Log("Currently scanning! Please wait"); return; }
- if (Selection.objects == null || Selection.objects.Length == 0) { return; }
- Debug.Log("Scanning for Orphaned Navmeshes...");
- _scanning = true;
- _scanStartTime = EditorApplication.timeSinceStartup;
- _badPointsList.Clear();
- _bounds = new Bounds();
- FixEndPoints.FixNavMeshEndPoints(); //this helps in case some of them are messed up
- //get a bounds of every collider
- Collider[] colliders = null;
- colliders = Selection.activeGameObject.GetComponentsInChildren<Collider>();
- if (colliders == null || colliders.Length == 0) { return; }
- for (int i = 0; i < colliders.Length; i++){
- if (colliders[i].isTrigger) { continue; } //ignore triggers
- if (_bounds.size == Vector3.zero) { _bounds = colliders[i].bounds; continue; } //start with this one, don't start at 0
- _bounds.Encapsulate(colliders[i].bounds);
- }
- _bounds.Expand(STEP_SIZE + 2); //+ to account for rounding
- DrawBounds(_bounds);
- if (_bounds.extents == Vector3.zero) { Debug.Log("No colliders to scan! Please select some colliders"); ForceStop(); return; }
- //starting positions for scanning. y goes up to down
- xCurr = (int)_bounds.min.x;
- yCurr = (int)_bounds.max.y;
- zCurr = (int)_bounds.min.z;
- EditorApplication.update += Scan;
- }
- static void Scan() {
- double frameStart = EditorApplication.timeSinceStartup;
- //for every point in the bounds
- for (int x = xCurr; x <= (int)_bounds.max.x; x += 1) {
- for (int z = zCurr; z <= (int)_bounds.max.z; z += 1) {
- for (int y = yCurr; y >= (int)_bounds.min.y; y -= 1) { //start from the roof and go down
- //Debug.Log("updating! xyz = "+x+","+y+","+z+" And boundsmax = "+_bounds.max);
- CheckPoint(x, y, z); //this adds points to the octree and edgepositions
- //continue later, this frame is taking too long (<60fps)
- y = nextY; //which is set in CheckPoint
- if (EditorApplication.timeSinceStartup - frameStart > 0.0166) {
- xCurr = x;
- yCurr = y;
- zCurr = z;
- if (_showDebug) {
- SceneView.RepaintAll();
- }
- return;
- }
- }
- yCurr = (int)_bounds.max.y; //max since we go down not up
- }
- zCurr = (int)_bounds.min.z;
- }
- double scanTime = EditorApplication.timeSinceStartup - _scanStartTime;
- Debug.Log("Done scanning! Found "+_badPointsList.Count +" orphaned positions in "+scanTime+" seconds. If any, please fix!");
- //clean up
- _scanning = false;
- EditorApplication.update -= Scan;
- }
- static int nextY = 99999;
- static void CheckPoint(int x, int y, int z) {
- Vector3 raycastOrigin = new Vector3(x, y, z);
- nextY = (int) _bounds.min.y; //if we don't hit anything, we are done
- //Debug.Log("raycastorigin = "+raycastOrigin);
- //raycast down and find colliders on roofs
- //distance is to the bottom of the bounds
- if (!Physics.Raycast(raycastOrigin, Vector3.down, out _raycastHit, (y - _bounds.min.y) + 1.0f, _mask)) {
- //Debug.DrawLine(raycastOrigin, raycastOrigin + Vector3.down * ((y - _bounds.min.y) + 1.0f), Color.red, 3.0f);
- return; //done with this Y column
- }
- //Debug.DrawLine(hit.point, hit.point+ Vector3.up, Color.green, 3.0f); //the top point, up a bit
- //optmization here: if we hit something with the raycast, even if we didn't keep it, return that y delta
- nextY = (int)_raycastHit.point.y - 1;
- //now find the closest navmesh point to the physics point
- if (!NavMesh.SamplePosition(_raycastHit.point, out _navMeshHit, STEP_SIZE, _walkableMask)){
- //Debug.DrawLine(hit.point, hit.point + Vector3.up, Color.yellow, 3.0f);
- return;
- }
- _navMeshPoint = _navMeshHit.position;
- //if this is the first hit, then save it, and we'll try to navigate to this position
- //TODO: Find some way to make sure this position is not also orphaned
- if (_originPoint == Vector3.zero) {
- _originPoint = _navMeshPoint;
- Debug.DrawLine(_originPoint, _originPoint + 5*Vector3.up, Color.cyan, 3.0f); //the top point, up a bit
- Debug.Log("Origin point is now " + _originPoint);
- return; //can't connect to self so we're good
- }
- if (_originPoint == Vector3.zero) {
- Debug.Log("This shouldn't have happened! origin is now " + _originPoint);
- ForceStop();
- return;
- }
- //now check if this point can connect to the origin point
- if (NavMesh.CalculatePath(_originPoint, _navMeshPoint, _navmeshTraversable, _navMeshPath) &&
- _navMeshPath.status == NavMeshPathStatus.PathComplete
- ){
- //we have a path so we're good
- Debug.DrawLine(_navMeshPoint, _navMeshPoint + Vector3.up, Color.green, 3.0f); //the top point, up a bit
- return;
- }
- //now we have an orphaned one
- //add!
- _badPointsList.Add(_navMeshPoint);
- Debug.DrawLine(_navMeshPoint, _navMeshPoint + Vector3.up, Color.red, 3.0f); //the top point, up a bit
- }
- static void DrawBounds(Bounds bounds) {
- Vector3 v3Center = bounds.center;
- Vector3 v3Extents = bounds.extents;
- Vector3 v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
- Vector3 v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
- Vector3 v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
- Vector3 v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
- Vector3 v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
- Vector3 v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
- Vector3 v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
- Vector3 v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
- Debug.DrawLine(v3FrontTopLeft, v3FrontTopRight, Color.cyan, 3.0f);
- Debug.DrawLine(v3FrontTopLeft, v3BackTopLeft, Color.cyan, 3.0f);
- Debug.DrawLine(v3FrontTopLeft, v3FrontBottomLeft, Color.cyan, 3.0f);
- Debug.DrawLine(v3FrontTopRight, v3FrontBottomRight, Color.cyan, 3.0f);
- Debug.DrawLine(v3FrontTopRight, v3BackTopRight, Color.cyan, 3.0f);
- Debug.DrawLine(v3FrontBottomLeft, v3FrontBottomRight, Color.cyan, 3.0f);
- Debug.DrawLine(v3FrontBottomLeft, v3BackBottomLeft, Color.cyan, 3.0f);
- Debug.DrawLine(v3FrontBottomRight, v3BackBottomRight, Color.cyan, 3.0f);
- Debug.DrawLine(v3BackBottomLeft, v3BackBottomRight, Color.cyan, 3.0f);
- Debug.DrawLine(v3BackTopLeft, v3BackTopRight, Color.cyan, 3.0f);
- Debug.DrawLine(v3BackTopLeft, v3BackBottomLeft, Color.cyan, 3.0f);
- Debug.DrawLine(v3BackTopRight, v3BackBottomRight, Color.cyan, 3.0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment