- Home /
Control what to draw manually?
I'm coming from 2D frameworks such as LibGDX and MonoGame and I currently want to create a 3D world out of blocks in Unity. My camera will be orthographic at a fixed angle to create a isometric "feel" and thus I can easily calculate what's inside the view and what not.
Let's say I just want a top down tilemap. I would keep a 2D array with data representing the tiles and based on camera location, viewport size and tile size I know exactly what to draw using the 2D array. It would look something like this:
//Assuming camera.position gives the center of the camera:
int startX = (camera.position.x - viewport.width / 2) / Tile.Width;
int startY = (camera.position.y - viewport.height / 2) / Tile.Height;
int endX = (camera.position.x + viewport.width / 2) / Tile.Width;
int endY = (camera.position.y + viewport.height / 2) / Tile.Height;
//Now I simply iterate from start to end and draw the tiles:
for (int y = startY; y < endY; y++)
{
for (int x = startX; x < endX; x++)
{
tileMap[x][y].draw();
}
}
I actually want to do the same with meshes but I understand that instantiating and destroying meshes is expensive so this might not be a desirable solution. Yet I do know what to draw so there must be a option for me to disregard everything outside of the viewport and get manual control of what's being drawn instead of letting the engine decide the culling.
My final project needs to have something like 128x128x128 "spaces" that hold meshes. I know these are a lot of blocks but only 10 height/z layers will be visible at maximum and manually culling everything outside view should leave me with just a couple hundred blocks at any one time.
Answer by DavidWatts · Aug 27, 2016 at 03:54 PM
You can use the low level graphics library https://docs.unity3d.com/ScriptReference/GL.html you want to start with something like
void OnPostRender () {
//set a material pass with SetPass
GL.PushMatrix();
GL.Begin( GL.TRIANGLES );
//use GL.TexCoord then GL.vertex to create triangles
GL.End();
GL.PopMatrix();
}
or if you want to use meshes you can use Graphics.Draw$$anonymous$$esh. this can be called in update and doesn't have to be in OnPostRender
Your answer
![](https://koobas.hobune.stream/wayback/20220612091656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tilemap.SetTile sync to clients on server? NetworkServer.Spawn? 0 Answers
Instantiating a sprite in a tilemap's cell 0 Answers
Dynamically instantiated game objects not showing in main camera (with background camera) 3 Answers
Tile Collision Detection Not Always Working 3 Answers
What is the fastest way at runtime? instantiate? moving in/out camera? activate/deactivate? 1 Answer