Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void BuildTrianglesDC(in ChunkInfo chunkInfo, out UnsafeList<Triangle> triangles)
- {
- int totalCells = (int)pow(worldInfo.size, 3);
- int totalCorners = (int)pow(worldInfo.size + 1, 3);
- triangles = this.triangles;
- triangles.Clear();
- // Add cell vertices
- var points = new UnsafeList<float4>(0, Allocator.Temp);
- var normals = new UnsafeList<float4>(0, Allocator.Temp);
- var edges = new UnsafeList<int>(0, Allocator.Temp);
- for (int cell = 0; cell < totalCells; cell++)
- {
- edges.Clear();
- points.Clear();
- normals.Clear();
- // Check all 12 edges for intersections
- float3 cellP = IndexPosition.CellPosition(cell, worldInfo.size);
- for (int edge = 0; edge < 12; edge++)
- {
- // Find the two edges voxels that make this edge
- float3 ca = cellP + Tables.CubeCorners[Tables.cornerA[edge]];
- float3 cb = cellP + Tables.CubeCorners[Tables.cornerB[edge]];
- int ia = IndexPosition.CornerIndex(ca, worldInfo.size);
- int ib = IndexPosition.CornerIndex(cb, worldInfo.size);
- Voxel va = chunkInfo.volume[ia];
- Voxel vb = chunkInfo.volume[ib];
- if (sign(va.Density) != sign(vb.Density))
- {
- float s = -va.Density / (vb.Density - va.Density);
- s = clamp(s, 0, 1);
- // QEF data
- float3 intersectionPoint = lerp(ca, cb, s);
- float3 intersectionNormal = normalize(lerp(va.Normal, vb.Normal, s));
- points.Add(float4(intersectionPoint, 0));
- normals.Add(float4(intersectionNormal, 0));
- // Commented code: only add edges that are unique to this cell
- // commented out to reduce complexity, it add all edges that are crossed
- // if (edge == 2 || edge == 3 || edge == 11)
- edges.Add(edge);
- // float cellBound = worldInfo.size / 2f - 0.5f;
- // if (cellP.x == cellBound && (edge == 1 || edge == 10))
- // edges.Add(edge);
- // if (cellP.y == cellBound && (edge == 6 || edge == 7))
- // edges.Add(edge);
- // if (cellP.z == cellBound && (edge == 0 || edge == 8))
- // edges.Add(edge);
- // if(all(cellP.xy == cellBound) && edge == 5)
- // edges.Add(edge);
- // if (all(cellP.xz == cellBound) && edge == 9)
- // edges.Add(edge);
- // if (all(cellP.yz == cellBound) && edge == 4)
- // edges.Add(edge);
- }
- }
- cellEdges[cell] = edges;
- unsafe
- {
- float3 v = QEF.Solve(points.Ptr, normals.Ptr, points.Length).xyz;
- if (any(isnan(v)) || any(isinf(v)))
- {
- cellVertices[cell] = VertexInfo.Empty;
- }
- else
- {
- cellVertices[cell] = new(v, up(), 0);
- DCDebugger.I.vertices.Add(chunkInfo.position + v);
- foreach (int edge in edges)
- {
- DCDebugger.I.edge1.Add(chunkInfo.position + cellP + Tables.CubeCorners[Tables.cornerA[edge]]);
- DCDebugger.I.edge2.Add(chunkInfo.position + cellP + Tables.CubeCorners[Tables.cornerB[edge]]);
- }
- }
- }
- }
- points.Dispose();
- normals.Dispose();
- edges.Dispose();
- // Connect vertices
- for (int cell = 0; cell < totalCells; cell++)
- {
- // Check if the cell vertex is valid
- VertexInfo v0 = cellVertices[cell];
- if(VertexInfo.Invalid(v0))
- continue;
- // For each active edge in this cell, connect the vertices of the cells that share the edge
- float3 cellP = IndexPosition.CellPosition(cell, worldInfo.size);
- foreach (int edge in cellEdges[cell])
- {
- // neighbors that share this edge
- // positions
- float3 n1 = cellP + Tables.EdgeSharingNeighborOffset[edge * 3 + 0];
- float3 n2 = cellP + Tables.EdgeSharingNeighborOffset[edge * 3 + 1];
- float3 n3 = cellP + Tables.EdgeSharingNeighborOffset[edge * 3 + 2];
- // indices
- int i1 = IndexPosition.CellIndex(n1, worldInfo.size);
- int i2 = IndexPosition.CellIndex(n2, worldInfo.size);
- int i3 = IndexPosition.CellIndex(n3, worldInfo.size);
- // vertices
- VertexInfo v1 = i1 > -1 && i1 < totalCells ? cellVertices[i1] : VertexInfo.Empty;
- VertexInfo v2 = i2 > -1 && i2 < totalCells ? cellVertices[i2] : VertexInfo.Empty;
- VertexInfo v3 = i3 > -1 && i3 < totalCells ? cellVertices[i3] : VertexInfo.Empty;
- // quad
- if (!VertexInfo.Invalid(v1) && !VertexInfo.Invalid(v2))
- triangles.Add(new(v0, v1, v2, 0));
- if (!VertexInfo.Invalid(v2) && !VertexInfo.Invalid(v3))
- triangles.Add(new(v2, v3, v0, 0));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement