Read Static Mesh Data into Procedural Mesh

Hey Guys,
So I’m currently working on reading in the render data from a static mesh and using it to create a procedural mesh so I can deform it at runtime, but running into issues with getting the mesh to render properly. Here’s the current function I have for reading in the render data using it:

void AProceduralLandscape::GenerateLandscape()
{
	TArray<FVector> vertices;
	TArray<int32> triangles;
	TArray<FVector> normals;
	TArray<FVector2D> UV0;
	TArray<FColor> vertexColors;
	TArray<FProcMeshTangent> tangents;

	if (LandscapeReference)
	{
		FPositionVertexBuffer* vertexPositionBuffer = &LandscapeReference->RenderData->LODResources[0].PositionVertexBuffer;
		FRawStaticIndexBuffer* indexBuffer = &LandscapeReference->RenderData->LODResources[0].IndexBuffer;
		FStaticMeshVertexBuffer* vertexBuffer = &LandscapeReference->RenderData->LODResources[0].VertexBuffer;

		if (vertexPositionBuffer)
		{

			const int32 vertexCount = vertexPositionBuffer->GetNumVertices();
			for (int32 i = 0; i < vertexCount; i++)
			{
				vertices.Add(vertexPositionBuffer->VertexPosition(i));
				triangles.Add(indexBuffer->GetArrayView()[i]);
				normals.Add(FVector(1, 0, 0));
				UV0.Add(vertexBuffer->GetVertexUV(i,0));
				vertexColors.Add(FColor(100, 100, 100, 100));
				tangents.Add(FProcMeshTangent(vertexBuffer->VertexTangentX(i).Vector.X, vertexBuffer->VertexTangentX(i).Vector.Y, vertexBuffer->VertexTangentX(i).Vector.Z));

			}
		}
		LandscapeMesh->CreateMeshSection(1, vertices, triangles, normals, UV0, vertexColors, tangents, false); 
		UE_LOG(LogTemp, Warning, TEXT("CREATED LANDSCAPE"));

	}
}

Currently it’ll render only the first 1/3 of the mesh. My hunch is the hard-coded normals I’m using for generation, but I can’t really find any normal info inside FStaticMeshLODResources. Any ideas? Thanks

You’re not getting all of the indices. With a triangle list that actually uses the indices correctly it will have quite a few more indices than vertices. Best thing to do is a separate loop that looks at indexBuffer.GetNumIndices() and copies all of them to the triangles array you have.

For an example, it’s possible to have a cube with only 8 vertices but still need 36 indices to makeup all 12 triangles.

To copy the indices, something like this should get them all.

for (int32 = 0; i < indexBuffer.GetNumIndices(); i++)
{
     triangles.Add(indexBuffer->GetArrayView()[i]);
}

That was exactly the issue, thanks! I should probably find a book on graphics since I never realized the reason behind indices.