- Home /
Is there a memory-efficient solution to dynamic index count in a mesh?
I am implementing a ROAM terrain algorithm which, by it's nature, generates a mesh with frequently changing vertex/index count.
With vertices, I can store an array of the largest used count, and ignore any unused vertices.
With indices, however, there's no way to set the count when applying them to the mesh... you have to have an exact-count array, which requires me to instantiate one every time I apply indices to the mesh.
I know in most DirectX wrappers, you can send an array with an additional count int that tells it how many to actually use. If this were the case, I could work like vertices, where I just have an array big enough for the maximum size. Is there something like this in Unity that I'm not seeing?
For example, this is what I currently have:
int[] Indices;
List<int> indices = new List<int>();
for (int i = 0; i < NextTriangleID; i++)
{
if (Triangles.ContainsKey(i))
{
Triangle t = Triangles[i];
if (t.CT0 < 0 && t.CT1 < 0)
{
indices.Add(t.V0);
indices.Add(t.V1);
indices.Add(t.V2);
}
}
}
Indices = indices.ToArray();
Mesh.SetIndices(Indices, MeshTopology.Triangles, 0);
And this is what I'd like to have:
int[] Indices;
int IndexCount = 0;
for (int i = 0; i < NextTriangleID; i++)
{
if (Triangles.ContainsKey(i))
{
Triangle t = Triangles[i];
if (t.CT0 < 0 && t.CT1 < 0)
{
Indices[IndexCount] = t.V0;
IndexCount++;
Indices[IndexCount] = t.V1;
IndexCount++;
Indices[IndexCount] = t.V2;
IndexCount++;
}
}
}
Mesh.SetIndices(Indices, IndexCount, MeshTopology.Triangles, 0);
I see now that $$anonymous$$esh.SetTriangles takes in a list... but I don't know if that will just do the same array unboxing in the background?
As an aside, is there a reason you chose ROA$$anonymous$$? It's not really the best approach for terrain when using modern GPUs - modern as in the past decade - since it's very CPU intensive. You're often better off just throwing all of the triangles at the GPU or using a method more suited to the GPU such as geomipmapping or geoclipmapping.
I'm using ROA$$anonymous$$ on a planetary scale. So, it needs basically infinite precision and procedural elevation generation. Think similar to $$anonymous$$erbal Space Program's planets. Good from close up and afar.
I'm pretty sure those techniques you mentioned require an absolute highest resolution to base off of, but I don't have a maximum there.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Converting a mesh to terrain 2 Answers
Spawned Object Gradient Mesh Colors? 2 Answers