Problems using OnRenderObject() and Graphics.DrawMeshNow()

I’m working on a 2D voxel game where the voxel geometry is split into 8x8 “chunks”. Originally, I just had one GameObject (with a MeshFilter/MeshRenderer) for each chunk and let Unity handle everything itself.

This worked just fine, but it wasn’t scaling well when I started playing with larger grids, so I’m implementing my own system where a single object/script generates all the chunk meshes and manually draws a small number of them based on the area visible to the camera. However, with this new system, my meshes are not appearing anywhere in the scene! They worked with the old method, so I know the issue isn’t with the mesh/material itself, and thanks to Debug.Log() I know that this code is all being executed as expected, so the problem must lie with how I’m using the voxel material and the “Graphics.DrawMeshNow()” method.

The rendering code is as follows:

void OnRenderObject()
	//For reference:
	//"Chunks" is the 2D array of chunks. Each chunk object stores its own mesh.
	//"VoxelBlockMat" is a simple material for rendering the chunks.
	//"Chunk.Size" is the number of voxels per chunk (8).

	//Figure out which chunks are in view.
	Camera cam = Camera.current;
	Vector2 orthoHalf = new Vector2(cam.orthographicSize * cam.pixelWidth / cam.pixelHeight,
	Vector3 camPos = cam.transform.position;
	Vector2 viewMin = new Vector2(camPos.x - orthoHalf.x, camPos.y - orthoHalf.y),
		    viewMax = viewMin + (orthoHalf * 2.0f);
	Vector2i viewMinI = new Vector2i((int)viewMin.x, (int)viewMin.y),
		     viewMaxI = new Vector2i((int)viewMax.x + 1, (int)viewMax.y + 1);

	Vector2i chunkMinI = viewMinI / Chunk.Size,
		     chunkMaxI = viewMaxI / Chunk.Size;

	//Render those chunks.
	for (int y = chunkMinI.y; y <= chunkMaxI.y && y < Chunks.GetLength(1); ++y)
		if (y > 0)
			for (int x = chunkMinI.x; x <= chunkMaxI.x && x < Chunks.GetLength(0); ++x)
				if (x > 0)
					//The mesh stores the world position of the voxels, so no world transform is needed.
					Graphics.DrawMeshNow(Chunks[x, y].VoxelMesh,, Quaternion.identity);

If i draw the default cube mesh with the default shader i can see a black cube at the origin. Keep in mind that Graphics.DrawMeshNow doesn’t supports lighting. It’s a low level rendering method.

Also keep in mind that currently you draw all chunks at the same position. If you haven’t placed the vertices at world space coordiates you will get strange results.

Do you use a custom shader or any of the built in?

Fixed the problem myself! Apparently my mesh wasn’t set up correctly, which is confusing because it worked just fine with the old method…but anyway, the problem wasn’t related to this rendering code.