Advertisement
Cookie042

QuadEdgeTurning

Jan 22nd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1.  
  2.     public void BuildDMesh()
  3.     {
  4.         dmesh = new DMesh3(false,true);
  5.  
  6.         for (var i = 0; i < points.Length; i++)
  7.         {
  8.             dmesh.AppendVertex(points[i]);
  9.             dmesh.SetVertexColor(i, heightGradient.Evaluate(InverseLerp(-3,7,heights[i])));
  10.         }
  11.  
  12.         for (int y = 0; y < gridSize.y - 1; y++)
  13.         {
  14.             var rowStartIndex = y * gridSize.x;
  15.  
  16.             //Left Vertx Heights (ints)
  17.             var topLeft = heights[rowStartIndex + gridSize.x];
  18.             var bottomLeft = heights[rowStartIndex];
  19.             for (int x = 0; x < gridSize.x - 1; x++)
  20.             {
  21.                 var quadBottomLeftIndex = rowStartIndex + x;
  22.  
  23.                 //Right Vertex Heights (ints)
  24.                 var topRight = heights[quadBottomLeftIndex + gridSize.x + 1];
  25.                 var bottomRight = heights[quadBottomLeftIndex + 1];
  26.                
  27.                 //deltaHeights
  28.                 var dhtl = DeltaHeight(topLeft, bottomLeft, topRight);
  29.                 var dhtr = DeltaHeight(topRight, bottomRight, topLeft);
  30.                 var dhbl = DeltaHeight(bottomLeft, topLeft, bottomRight);
  31.                 var dhbr = DeltaHeight(bottomRight, bottomLeft, topRight);
  32.  
  33.                 //if (bl == tr && (bl != tl || bl != br))
  34.  
  35.                 if (Abs(dhtr - dhbl ) < Abs(dhtl - dhbr))
  36.                 {
  37.                     //bl, tl, tr
  38.                     //tr, br, bl
  39.                     dmesh.AppendTriangle(
  40.                         quadBottomLeftIndex,
  41.                         quadBottomLeftIndex + gridSize.x,
  42.                         quadBottomLeftIndex + gridSize.x + 1);
  43.                     dmesh.AppendTriangle(
  44.                         quadBottomLeftIndex + gridSize.x + 1,
  45.                         quadBottomLeftIndex + 1,
  46.                         quadBottomLeftIndex);
  47.                 }
  48.                 else
  49.                 {
  50.                     //br, bl, tl
  51.                     //tl, lt, br
  52.                     dmesh.AppendTriangle(
  53.                         quadBottomLeftIndex + 1,
  54.                         quadBottomLeftIndex,
  55.                         quadBottomLeftIndex + gridSize.x);
  56.                     dmesh.AppendTriangle(
  57.                         quadBottomLeftIndex + gridSize.x,
  58.                         quadBottomLeftIndex + gridSize.x + 1,
  59.                         quadBottomLeftIndex + 1);
  60.                 }
  61.  
  62.                 bottomLeft = bottomRight; topLeft = topRight;
  63.  
  64.             }
  65.         }
  66.     }
  67.  
  68.     //returns the total height of 3 verticles
  69.     public int DeltaHeight(int a, int b, int c)
  70.     {
  71.         int max = -100000;
  72.         if (a > max)
  73.             max = a;
  74.         if (b > max)
  75.             max = b;
  76.         if (c > max)
  77.             max = c;
  78.  
  79.         int min = 100000;
  80.         if (a < min)
  81.             min = a;
  82.         if (b < min)
  83.             min = b;
  84.         if (c < min)
  85.             min = c;
  86.  
  87.  
  88.         return Abs(max - min);
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement