Background:
I needed to make a mesh grid that I could change the height (y-axis) of individual quads at any coordinate I specified. I have the mesh created and am able to do exactly what I need to.
Problem:
I need to move many quads simultaneously. When I modify en mass, eg 100, (which is little compared to the many I intend on moving at once) my performance drops heavily.
My grid is made up of multiple meshes, so I find the relevant gameobject using the x and z coordinate and then edit the vertices in that gameobjects mesh
My code snippets:
private void GroupChange()
{
foreach (TileGroupClass group in groupList)
{
//if the last tiles height in the group does not equal the new target height, change the height.
if (group.tileList[group.tileList.Count - 1].y != group.newTargetHeight)
{
foreach (Vector3 tile in group.tileList)
{
ChangeHeight(tile, group);
}
}
}
}
void ChangeHeight(Vector3 tile, TileGroupClass group)
{
int x = Mathf.RoundToInt(tile.x);
int z = Mathf.RoundToInt(tile.z);
float height = group.newTargetHeight;
float heightChangeSpeed = group.speed;
//convert xz into chunkx and chunkz
int chunkX;
if (x >= chunkSize) chunkX = Mathf.RoundToInt(x / chunkSize);
else chunkX = 0;
int chunkZ = z % chunkSize;
if (z >= chunkSize) chunkZ = Mathf.RoundToInt(z / chunkSize);
else chunkZ = 0;
//get the mesh
GameObject curChunk = chunkArray[(chunkZ *chunkNum) +chunkX];
Mesh mesh = curChunk.GetComponent<MeshFilter>().mesh;
//set temporary arrays to modify
Vector3[] vertices = new Vector3[chunkSize * chunkSize * 4];
int[] triangles = new int[chunkSize * chunkSize * 6];
vertices = mesh.vertices;
triangles = mesh.triangles;
//set which quad vertices and triangles to modify
int changeNum = ((z % chunkSize) * chunkSize) + (x % chunkSize);
int v = changeNum * 4;
int t = changeNum * 6;
if(chunkX + chunkZ > 0)Debug.Log(changeNum);
float vertexOffset = cellSize * 0.5f;
Vector3 cellOffset = new Vector3((x - (chunkX * chunkSize)) * cellSize, 0, (z - (chunkZ * chunkSize)) * cellSize);
//save the quads current vertices position
Vector3 vertVect1 = new Vector3(vertices[v].x, vertices[v].y, vertices[v].z);
Vector3 vertVect2 = new Vector3(vertices[v + 1].x, vertices[v + 1].y, vertices[v + 1].z);
Vector3 vertVect3 = new Vector3(vertices[v + 2].x, vertices[v + 2].y, vertices[v + 2].z);
Vector3 vertVect4 = new Vector3(vertices[v + 3].x, vertices[v + 3].y, vertices[v + 3].z);
//move the quad to new position
vertices[v] = Vector3.MoveTowards(vertVect1, new Vector3(-vertexOffset, height, -vertexOffset) + cellOffset + gridOffset, heightChangeSpeed * Time.deltaTime);
vertices[v + 1] = Vector3.MoveTowards(vertVect2, new Vector3(-vertexOffset, height, vertexOffset) + cellOffset + gridOffset, heightChangeSpeed * Time.deltaTime);
vertices[v + 2] = Vector3.MoveTowards(vertVect3, new Vector3(vertexOffset, height, -vertexOffset) + cellOffset + gridOffset, heightChangeSpeed * Time.deltaTime);
vertices[v + 3] = Vector3.MoveTowards(vertVect4, new Vector3(vertexOffset, height, vertexOffset) + cellOffset + gridOffset, heightChangeSpeed * Time.deltaTime);
//set triangles
triangles[t] = v;
triangles[t + 1] = triangles[t + 4] = v + 1;
triangles[t + 2] = triangles[t + 3] = v + 2;
triangles[t + 5] = v + 3;
//set the values into the current mesh
mesh.vertices = vertices;
mesh.triangles = triangles;
//mesh.RecalculateNormals();
Vector3 temp = group.tileList[group.tileList.IndexOf(tile)];
//group.tileList[group.tileList.IndexOf(tile)].y = vertices[v + 3].y;
temp.y = vertices[v + 3].y;
group.tileList[group.tileList.IndexOf(tile)] = temp;
Debug.Log("tile height" + temp.x + " : " + temp.y + " : " + temp.z + " = " + group.tileList.Find(p => p == tile).y);
//group.curHeight = vertices[v + 3].y;
}
I understand this performance is because I’m updating many things using the update function, and wondered if there is a better way to implement what I’m trying to achieve. I have a great deal still to learn about programming.
Does anyone have any ideas?
Thank you for replying mate! I'm going to have a look through the code and make some serious modifications!
– IsItHaydenI didn't include all the code, but I have a question in regards to the bad housekeeping: As I am calling other meshes, not calling this gameobjects mesh, wouldn't I need to use something along those lines?
– IsItHayden