Advertisement
Guest User

DC: missing edges

a guest
Jul 4th, 2023
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. public void BuildTrianglesDC(in ChunkInfo chunkInfo, out UnsafeList<Triangle> triangles)
  2.         {
  3.             int totalCells = (int)pow(worldInfo.size, 3);
  4.             int totalCorners = (int)pow(worldInfo.size + 1, 3);
  5.  
  6.             triangles = this.triangles;
  7.             triangles.Clear();
  8.  
  9.             // Add cell vertices
  10.             var points = new UnsafeList<float4>(0, Allocator.Temp);
  11.             var normals = new UnsafeList<float4>(0, Allocator.Temp);
  12.             var edges = new UnsafeList<int>(0, Allocator.Temp);
  13.             for (int cell = 0; cell < totalCells; cell++)
  14.             {
  15.                 edges.Clear();
  16.                 points.Clear();
  17.                 normals.Clear();
  18.  
  19.                 // Check all 12 edges for intersections
  20.                 float3 cellP = IndexPosition.CellPosition(cell, worldInfo.size);
  21.                 for (int edge = 0; edge < 12; edge++)
  22.                 {
  23.                     // Find the two edges voxels that make this edge
  24.                     float3 ca = cellP + Tables.CubeCorners[Tables.cornerA[edge]];
  25.                     float3 cb = cellP + Tables.CubeCorners[Tables.cornerB[edge]];
  26.                     int ia = IndexPosition.CornerIndex(ca, worldInfo.size);
  27.                     int ib = IndexPosition.CornerIndex(cb, worldInfo.size);
  28.                     Voxel va = chunkInfo.volume[ia];
  29.                     Voxel vb = chunkInfo.volume[ib];
  30.  
  31.                     if (sign(va.Density) != sign(vb.Density))
  32.                     {
  33.                         float s = -va.Density / (vb.Density - va.Density);
  34.                         s = clamp(s, 0, 1);
  35.                         // QEF data
  36.                         float3 intersectionPoint = lerp(ca, cb, s);
  37.                         float3 intersectionNormal = normalize(lerp(va.Normal, vb.Normal, s));
  38.                         points.Add(float4(intersectionPoint, 0));
  39.                         normals.Add(float4(intersectionNormal, 0));
  40.                        
  41.                         // Commented code: only add edges that are unique to this cell
  42.                         // commented out to reduce complexity, it add all edges that are crossed
  43.                         // if (edge == 2 || edge == 3 || edge == 11)
  44.                         edges.Add(edge);
  45.  
  46.                         // float cellBound = worldInfo.size / 2f - 0.5f;
  47.                        
  48.                         // if (cellP.x == cellBound && (edge == 1 || edge == 10))
  49.                         //     edges.Add(edge);
  50.                         // if (cellP.y == cellBound && (edge == 6 || edge == 7))
  51.                         //     edges.Add(edge);
  52.                         // if (cellP.z == cellBound && (edge == 0 || edge == 8))
  53.                         //     edges.Add(edge);
  54.  
  55.                         // if(all(cellP.xy == cellBound) && edge == 5)
  56.                         //     edges.Add(edge);
  57.                         // if (all(cellP.xz == cellBound) && edge == 9)
  58.                         //     edges.Add(edge);
  59.                         // if (all(cellP.yz == cellBound) && edge == 4)
  60.                         //     edges.Add(edge);
  61.  
  62.                     }
  63.  
  64.                 }
  65.                 cellEdges[cell] = edges;
  66.                
  67.                 unsafe
  68.                 {
  69.                     float3 v = QEF.Solve(points.Ptr, normals.Ptr, points.Length).xyz;
  70.                     if (any(isnan(v)) || any(isinf(v)))
  71.                     {
  72.                         cellVertices[cell] = VertexInfo.Empty;
  73.                     }
  74.                     else
  75.                     {
  76.                         cellVertices[cell] = new(v, up(), 0);
  77.  
  78.                         DCDebugger.I.vertices.Add(chunkInfo.position + v);
  79.                         foreach (int edge in edges)
  80.                         {
  81.                             DCDebugger.I.edge1.Add(chunkInfo.position + cellP + Tables.CubeCorners[Tables.cornerA[edge]]);
  82.                             DCDebugger.I.edge2.Add(chunkInfo.position + cellP + Tables.CubeCorners[Tables.cornerB[edge]]);
  83.                         }
  84.                     }
  85.  
  86.                 }
  87.             }
  88.             points.Dispose();
  89.             normals.Dispose();
  90.             edges.Dispose();
  91.  
  92.             // Connect vertices
  93.             for (int cell = 0; cell < totalCells; cell++)
  94.             {
  95.                 // Check if the cell vertex is valid
  96.                 VertexInfo v0 = cellVertices[cell];
  97.                 if(VertexInfo.Invalid(v0))
  98.                     continue;
  99.  
  100.                 // For each active edge in this cell, connect the vertices of the cells that share the edge
  101.                 float3 cellP = IndexPosition.CellPosition(cell, worldInfo.size);
  102.                 foreach (int edge in cellEdges[cell])
  103.                 {
  104.                     // neighbors that share this edge
  105.                     // positions
  106.                     float3 n1 = cellP + Tables.EdgeSharingNeighborOffset[edge * 3 + 0];
  107.                     float3 n2 = cellP + Tables.EdgeSharingNeighborOffset[edge * 3 + 1];
  108.                     float3 n3 = cellP + Tables.EdgeSharingNeighborOffset[edge * 3 + 2];
  109.                     // indices
  110.                     int i1 = IndexPosition.CellIndex(n1, worldInfo.size);
  111.                     int i2 = IndexPosition.CellIndex(n2, worldInfo.size);
  112.                     int i3 = IndexPosition.CellIndex(n3, worldInfo.size);
  113.                     // vertices
  114.                     VertexInfo v1 = i1 > -1 && i1 < totalCells ? cellVertices[i1] : VertexInfo.Empty;
  115.                     VertexInfo v2 = i2 > -1 && i2 < totalCells ? cellVertices[i2] : VertexInfo.Empty;
  116.                     VertexInfo v3 = i3 > -1 && i3 < totalCells ? cellVertices[i3] : VertexInfo.Empty;
  117.  
  118.                     // quad
  119.                     if (!VertexInfo.Invalid(v1) && !VertexInfo.Invalid(v2))
  120.                         triangles.Add(new(v0, v1, v2, 0));
  121.                     if (!VertexInfo.Invalid(v2) && !VertexInfo.Invalid(v3))
  122.                         triangles.Add(new(v2, v3, v0, 0));
  123.                 }
  124.             }
  125.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement