I’ve made a working function now that manually adds new geometry to an existing mesh, so that now all of my body pile planes are combined into one mesh that adds a new body pile plane (four vertices, two triangles) every time a battalion receives damage. But there is still a problem:
public void AddBodyPile(Vector3 pileLocation)
{
Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;
//Create new arrays that will eventually replace current arrays of the mesh
//Add new indices for additional plane
Vector3[] vertices = new Vector3[mesh.vertexCount + 4];
Vector2[] uvs = new Vector2[vertices.Length];
int[] triangles = new int[mesh.triangles.Length + 6];
//Populate new vertex and triangle arrays with existing data
for (int i = 0; i < mesh.vertexCount; i++)
vertices _= mesh.vertices*;*_
* for (int i = 0; i < mesh.triangles.Length; i++)*
triangles = mesh.triangles*;*
* //Determine the positions of new bodyPile plane vertices*
* float offset = .5f;*
* Vector3 upperLeft = new Vector3(pileLocation.x + offset, pileLocation.y,pileLocation.z - offset);*
* Vector3 upperRight = new Vector3(pileLocation.x + offset, pileLocation.y,pileLocation.z + offset);*
* Vector3 lowerRight = new Vector3(pileLocation.x - offset, pileLocation.y,pileLocation.z + offset);*
* Vector3 lowerLeft = new Vector3(pileLocation.x - offset, pileLocation.y,pileLocation.z - offset);*
* //And add them to the new vertex array*
* vertices[mesh.vertexCount] = upperLeft;*
* vertices[mesh.vertexCount + 1] = upperRight;*
* vertices[mesh.vertexCount + 2] = lowerRight;*
* vertices[mesh.vertexCount + 3] = lowerLeft;*
* //Populate new uv array*
* for (int i = 0; i < uvs.Length; i++)*
uvs = new Vector2(vertices_.x, vertices*.z);*_
* //Populate new triangles based on new vertices.*
* triangles[mesh.triangles.Length] = mesh.vertexCount;*
* triangles[mesh.triangles.Length + 1] = mesh.vertexCount + 1;*
* triangles[mesh.triangles.Length + 2] = mesh.vertexCount + 2;*
* triangles[mesh.triangles.Length + 3] = mesh.vertexCount + 2;*
* triangles[mesh.triangles.Length + 4] = mesh.vertexCount + 3;*
* triangles[mesh.triangles.Length + 5] = mesh.vertexCount;*
* mesh.Clear();*
* //Swap the old mesh with the new one (which now has one more body pile plane)*
* mesh.vertices = vertices;*
* mesh.uv = uvs;*
* mesh.triangles = triangles;*
* mesh.RecalculateNormals();*
* }*
While this gets the job done, and ultimately has very little effect on performance in the long run when no more bodies are being piled, the calculation to add each new body pile plane becomes extremely slow when the vertex count only reaches a couple hundred. I assume this is because I have it iterating through large arrays thrice every time the function is called.
Is there a more efficient way to do this? Can I add to built-in arrays without having to constantly re-populate it through iteration? Because ultimately all I want to do is (frequently) add four vertices to an existing mesh.
I would love to hear some input from somebody who’s worked with procedural grass generation on terrain, as the concept seems very similar.
Any help is appreciated!