• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MadMenyo · Aug 27, 2016 at 02:41 PM · instantiatetilemapculling

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidWatts · Aug 27, 2016 at 03:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

GameObject Instantiated Off Camera 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges