- Home /
Problem is not reproducible or outdated
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,
cam.orthographicSize);
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.
VoxelBlockMat.SetPass(0);
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, Vector3.zero, Quaternion.identity);
}
}
}
}
}
Answer by heyx3 · Jul 29, 2015 at 05:00 AM
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.
Answer by Bunny83 · Jul 29, 2015 at 03:27 AM
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?
Uhm, you use an orthographic camera? o.O
Have you checked the sceneview when in playmode?
As I mentioned before, all vertices are defined in world-space. This is a custom material for my voxels. This is a 2D game, so I don't use Unity's lighting system; I just need the $$anonymous$$VP matrix for the material (I assume that's still set automatically when I use Draw$$anonymous$$eshNow()?). The material takes in points and builds them into quads in the geometry shader, then just samples from a tilemap texture for the color.
As i said i tried it and i can see the cube in the sceneview as well as in the gameview. I even see it in the preview area. Try exposing one of your meshes in a public variable at runtime and double click it in the inspector. You should see the mesh preview at the bottom. If you can't see anything (after you rotate a bit) your mesh is messed up.
Without more details this is a dead-end since it works for me "out of the box". If it doesn't work for you there's something about your project we don't know.
Follow this Question
Related Questions
Fixing mesh normals 1 Answer
Does anyone know how to change the Mesh in the SkinnedMeshRenderer? 1 Answer
Distribute terrain in zones 3 Answers
Mesh Position at double gameobject's position 1 Answer
Placing 3Dtext with Perspective Camera 0 Answers