Meshlet gen

This commit is contained in:
Dynamitos
2024-04-07 22:55:17 +02:00
parent cde48f0d15
commit d6381a03c5
3 changed files with 63 additions and 24 deletions
+58 -14
View File
@@ -8,9 +8,12 @@ using namespace Seele;
struct Triangle
{
StaticArray<uint32, 3> indices;
Array<uint32> twoAdjacent;
Array<uint32> oneAdjacent;
int32 meshletId = -1;
};
int findIndex(Meshlet& current, uint32 index)
int findIndex(Meshlet &current, uint32 index)
{
for (uint32 i = 0; i < current.numVertices; ++i)
{
@@ -28,7 +31,7 @@ int findIndex(Meshlet& current, uint32 index)
return current.numVertices++;
}
void completeMeshlet(Array<Meshlet>& meshlets, Meshlet& current)
void completeMeshlet(Array<Meshlet> &meshlets, Meshlet &current)
{
meshlets.add(current);
current = {
@@ -38,7 +41,7 @@ void completeMeshlet(Array<Meshlet>& meshlets, Meshlet& current)
};
}
void addTriangle(Array<Meshlet>& meshlets, Meshlet& current, Triangle tri)
bool addTriangle(const Array<Vector>& positions, Array<Meshlet> &meshlets, Meshlet &current, Triangle tri)
{
int f1 = findIndex(current, tri.indices[0]);
int f2 = findIndex(current, tri.indices[1]);
@@ -46,22 +49,19 @@ void addTriangle(Array<Meshlet>& meshlets, Meshlet& current, Triangle tri)
if (f1 == -1 || f2 == -1 || f3 == -1)
{
completeMeshlet(meshlets, current);
f1 = findIndex(current, tri.indices[0]);
f2 = findIndex(current, tri.indices[1]);
f3 = findIndex(current, tri.indices[2]);
return false;
}
current.boundingBox.adjust(positions[tri.indices[0]]);
current.boundingBox.adjust(positions[tri.indices[1]]);
current.boundingBox.adjust(positions[tri.indices[2]]);
current.primitiveLayout[current.numPrimitives * 3 + 0] = uint8(f1);
current.primitiveLayout[current.numPrimitives * 3 + 1] = uint8(f2);
current.primitiveLayout[current.numPrimitives * 3 + 2] = uint8(f3);
current.numPrimitives++;
if (current.numPrimitives == Gfx::numPrimitivesPerMeshlet)
{
completeMeshlet(meshlets, current);
}
return true;
}
void Meshlet::build(const Array<Vector>& positions, const Array<uint32>& indices, Array<Meshlet>& meshlets)
void Meshlet::build(const Array<Vector> &positions, const Array<uint32> &indices, Array<Meshlet> &meshlets)
{
Meshlet current = {
.numVertices = 0,
@@ -70,14 +70,58 @@ void Meshlet::build(const Array<Vector>& positions, const Array<uint32>& indices
Array<Triangle> triangles(indices.size() / 3);
for (size_t i = 0; i < triangles.size(); ++i)
{
triangles.add(Triangle{
triangles[i] = Triangle{
.indices = {
indices[i * 3 + 0],
indices[i * 3 + 1],
indices[i * 3 + 2],
},
});
};
}
for (size_t i = 0; i < triangles.size(); i++)
{
for (size_t j = 0; j < triangles.size(); j++)
{
if (i == j)
continue;
uint32 adjacency = 0;
if (triangles[i].indices[0] == triangles[j].indices[0]
|| triangles[i].indices[0] == triangles[j].indices[1]
|| triangles[i].indices[0] == triangles[j].indices[2])
{
adjacency++;
}
if (triangles[i].indices[1] == triangles[j].indices[0]
|| triangles[i].indices[1] == triangles[j].indices[1]
|| triangles[i].indices[1] == triangles[j].indices[2])
{
adjacency++;
}
if (triangles[i].indices[1] == triangles[j].indices[0]
|| triangles[i].indices[1] == triangles[j].indices[1]
|| triangles[i].indices[1] == triangles[j].indices[2])
{
adjacency++;
}
if(adjacency == 2)
{
triangles[i].twoAdjacent.add(j);
triangles[j].twoAdjacent.add(i);
}
if(adjacency == 1)
{
triangles[i].oneAdjacent.add(j);
triangles[j].oneAdjacent.add(i);
}
}
}
addTriangle(positions, meshlets, current, triangles.back());
triangles.pop();
while(!triangles.empty())
{
AABB boundingBox;
}
if (current.numVertices > 0)
{