Hi,
I’m fairly new to creating meshes and quads. I’m trying to create a mesh in order to create a 2D tilemap where players can click to add and remove tiles. And it’s all based on a grid.
The idea is that the last tile added onto the tilemap should be drawn on top of the others (some tiles are overlapping each other).
With the unity tilemap, I wasn’t able to get the tiles to overlap each other properly. So I tried to create this mesh trying to follow CodeMonkey’s yoututbe tutorials.
But it seems like the quads are being rendered in order of the mesh index…with the highest index quad being drawn on top of the lower ones.
So I tried to add new tiles(quads) to the end of the mesh arrays(vertices, uvs and triangles) (while also removing the vertices, uvs and triangles and adjusting the index number corresponding to the old tile being replaced) and it’s working in terms of being able to draw on top of the other existing tiles(quads). But now, only the latest quad/tile I’m adding is being rendered on screen even though I can see the vertices, uv and triangle array all have the correct values in the correct order to draw the other quads on screen.
I have no idea why the other quads aren’t being rendered. All the mesh data that is being fed into the mesh seems to be correct. I’ve triple checked this using visual studio debugger.
I’m starting to think if this is a bug? Or something to do with the shader?
I’m starting to think i may not not be understanding something about meshes and shaders.
Any help will be appreciated. I’ve been scratching my head on this one for a week now.
This is basically the key parts of the code:
public void UpdateIndividualTile( Vector3 worldPosition)
{
grid.GetGridXY(worldPosition, out int x, out int y)
Vector3[ ] targetVertices;
Vector2[ ] targetUv
int[ ] targetTriangles;
int newIndex;
int existingQuadIndex = gridObject.quadIndex;
targetVertices = new Vector3[mesh.vertices.Length];
targetUv = new Vector2[mesh.uv.Length];
targetTriangles = new int[mesh.triangles.Length];
newIndex = targetVertices.Length / 4 - 1;
Vector3[ ] existingVertices = mesh.vertices;
Vector2[ ] existingUv = mesh.uv;
int[ ] existingTriangles = mesh.triangles;
Array.Copy(existingVertices, targetVertices, existingQuadIndex * 4);
Array.Copy(existingUv, targetUv, existingQuadIndex * 4);
Array.Copy(existingTriangles, targetTriangles, existingQuadIndex * 6);
Array.Copy(existingVertices, existingQuadIndex * 4 + 4, targetVertices, existingQuadIndex * 4, (existingVertices.Length - (existingQuadIndex * 4 + 4)));
Array.Copy(existingUv, existingQuadIndex * 4 + 4, targetUv, existingQuadIndex * 4, (existingUv.Length - (existingQuadIndex * 4 + 4)));
Array.Copy(existingTriangles, existingQuadIndex * 6 + 6, targetTriangles, existingQuadIndex * 6, (existingTriangles.Length - (existingQuadIndex * 6 + 6)));
for (int i = 0; i < grid.GetWidth(); i++)
{
for (int j = 0; j < grid.GetHeight(); j++)
{
if (grid.GetGridObject(i,j).quadIndex > existingQuadIndex)
{
grid.GetGridObject(i, j).quadIndex -= 1;
}
}
}
gridObject.quadIndex = newIndex;
int vIndex = newIndex* 4;
int vIndex0 = vIndex;
int vIndex1 = vIndex + 1;
int vIndex2 = vIndex + 2;
int vIndex3 = vIndex + 3;
baseSize *= (0.5f, 0.5f, 0);
vertices[vIndex0] = pos + new Vector3(-baseSize.x, baseSize.y);
vertices[vIndex1] = pos - baseSize;
vertices[vIndex2] = pos + new Vector3(baseSize.x, -baseSize.y);
vertices[vIndex3] = pos + baseSize;
int tIndex = index * 6;
triangles[tIndex+0] = vIndex0;
triangles[tIndex+1] = vIndex3;
triangles[tIndex+2] = vIndex1;
triangles[tIndex+3] = vIndex1;
triangles[tIndex+4] = vIndex3;
triangles[tIndex+5] = vIndex2;
mesh.Clear();
mesh.vertices = targetVertices;
mesh.uv = targetUv;
mesh.triangles = targetTriangles;
}