Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. public static void UpdateArea (GraphUpdateObject o, INavmesh graph) {
  2.             Bounds bounds = o.bounds;
  3.  
  4.             // Bounding rectangle with floating point coordinates
  5.             Rect r = Rect.MinMaxRect (bounds.min.x,bounds.min.z,bounds.max.x,bounds.max.z);
  6.  
  7.             // Bounding rectangle with int coordinates
  8.             var r2 = new IntRect(
  9.                 Mathf.FloorToInt(bounds.min.x*Int3.Precision),
  10.                 Mathf.FloorToInt(bounds.min.z*Int3.Precision),
  11.                 Mathf.FloorToInt(bounds.max.x*Int3.Precision),
  12.                 Mathf.FloorToInt(bounds.max.z*Int3.Precision)
  13.             );
  14.  
  15.             // Corners of the bounding rectangle
  16.             var a = new Int3(r2.xmin,0,r2.ymin);
  17.             var b = new Int3(r2.xmin,0,r2.ymax);
  18.             var c = new Int3(r2.xmax,0,r2.ymin);
  19.             var d = new Int3(r2.xmax,0,r2.ymax);
  20.  
  21.             var ymin = ((Int3)bounds.min).y;
  22.             var ymax = ((Int3)bounds.min).y;
  23.  
  24.             // Loop through all nodes
  25.             graph.GetNodes (_node => {
  26.                 var node = _node as TriangleMeshNode;
  27.  
  28.                 bool inside = false;
  29.  
  30.                 int allLeft = 0;
  31.                 int allRight = 0;
  32.                 int allTop = 0;
  33.                 int allBottom = 0;
  34.  
  35.                 // Check bounding box rect in XZ plane
  36.                 for (int v=0;v<3;v++) {
  37.                     Int3 p = node.GetVertex(v);
  38.                     var vert = (Vector3)p;
  39.  
  40.                     if (r2.Contains (p.x,p.z)) {
  41.                         inside = true;
  42.                         break;
  43.                     }
  44.  
  45.                     if (vert.x < r.xMin) allLeft++;
  46.                     if (vert.x > r.xMax) allRight++;
  47.                     if (vert.z < r.yMin) allTop++;
  48.                     if (vert.z > r.yMax) allBottom++;
  49.                 }
  50.  
  51.                 if (!inside) {
  52.                     if (allLeft == 3 || allRight == 3 || allTop == 3 || allBottom == 3) {
  53.                         return true;
  54.                     }
  55.                 }
  56.  
  57.                 // Check if the polygon edges intersect the bounding rect
  58.                 for (int v = 0; v < 3; v++) {
  59.                     int v2 = v > 1 ? 0 : v+1;
  60.  
  61.                     Int3 vert1 = node.GetVertex(v);
  62.                     Int3 vert2 = node.GetVertex(v2);
  63.  
  64.                     if (Polygon.Intersects (a,b,vert1,vert2)) { inside = true; break; }
  65.                     if (Polygon.Intersects (a,c,vert1,vert2)) { inside = true; break; }
  66.                     if (Polygon.Intersects (c,d,vert1,vert2)) { inside = true; break; }
  67.                     if (Polygon.Intersects (d,b,vert1,vert2)) { inside = true; break; }
  68.                 }
  69.  
  70.                 // Check if the node contains any corner of the bounding rect
  71.                 if (inside || node.ContainsPoint (a) || node.ContainsPoint (b) || node.ContainsPoint (c) || node.ContainsPoint (d)) {
  72.                     inside = true;
  73.                 }
  74.  
  75.                 if (!inside) {
  76.                     return true;
  77.                 }
  78.  
  79.                 int allAbove = 0;
  80.                 int allBelow = 0;
  81.  
  82.                 // Check y coordinate
  83.                 for (int v = 0; v < 3; v++) {
  84.                     Int3 p = node.GetVertex(v);
  85.                     var vert = (Vector3)p;
  86.                     if (vert.y < ymin) allBelow++;
  87.                     if (vert.y > ymax) allAbove++;
  88.                 }
  89.  
  90.                 // Polygon is either completely above the bounding box or completely below it
  91.                 if (allBelow == 3 || allAbove == 3) return true;
  92.  
  93.                 // Triangle is inside the bounding box!
  94.                 // Update it!
  95.                 o.WillUpdateNode(node);
  96.                 o.Apply (node);
  97.                 return true;
  98.             });
  99.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement