• 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
Question by ProgramYourFace · Oct 03, 2013 at 02:38 AM · optimizationloadingproceduralcullingloading during runtime

Optimizing Procedural terrain

I am making a voxel game in unity and all mi faces are culled correctly and it is still really laggy when i generate a new chunk i think this problem is its trying to generate a chunk instantly and it cant do that it needs to take as much time as it needs to do this kinda like a loading scralt texteen but i cant figure out how to do this.

screenshot (3).png (457.7 kB)
Comment

People who like this

0 Show 1
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 tw1st3d · Oct 03, 2013 at 02:43 AM 0
Share

Show us your code for generating the new block. Chances are you're doing it a very clunky / cloggy way that can be remedied.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by flaviusxvii · Oct 03, 2013 at 03:33 PM

CreateVisualMesh needs:

 yield return new WaitForSeconds(0.001f);

every so often. Maybe at line 64. This will let it generate some stuff, and then it'll yield to the render loop, and then it can come back and continue until it is done.

Comment
tw1st3d

People who like this

1 Show 2 · 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 tw1st3d · Oct 03, 2013 at 03:35 PM 0
Share

Good answer, it will eliminate the lag from a for(x ^ y ^ z) computation.

avatar image ProgramYourFace · Oct 03, 2013 at 08:10 PM 0
Share

were would you suggest i put this i try'd in different places and they did't work.i try'd at the end of the loops and before and after mesh rendering ,before collision and all these did't work. but thanks for all the help

avatar image

Answer by ProgramYourFace · Oct 03, 2013 at 03:26 PM

this is what i do for instantiating the chunks.

 for (float x = transform.position.x - viewSize; x < transform.position.x + viewSize; x+= Chunk.width)
         {
             for (float z = transform.position.z - viewSize; z < transform.position.z + viewSize; z+= Chunk.width)
             {
                 
                 Vector3 pos = new Vector3(x, 0, z);
                 pos.x = Mathf.Floor(pos.x / (float)Chunk.width) * Chunk.width;
                 pos.z = Mathf.Floor(pos.z / (float)Chunk.width) * Chunk.width;
                 
                 Chunk chunk = Chunk.FindChunk(pos);
                 if (chunk != null) continue;
                 
                  chunk = (Chunk)Instantiate(chunkFab, pos, Quaternion.identity);
                 
                 
                 
             }
         }

and this is what i use for creating a visual mesh

     public virtual IEnumerator CreateVisualMesh() {
         visualMesh = new Mesh();
         
         List<Vector3> verts = new List<Vector3>();
         List<Vector2> uvs = new List<Vector2>();
         List<int> tris = new List<int>();
         
         
         for (int x = 0; x < width; x++)
         {
             for (int y = 0; y < height; y++)
             {
                 for (int z = 0; z < width; z++)
                 {
                     if (map[x,y,z] == 0) continue;
                     
                     byte block = GetByte(x,y,z);
                     Vector3 start = new Vector3(x, y, z);
                     Vector3 offset1, offset2;
                     
                     if (IsTransparent(x, y - 1, z))
                     {
                         offset1 = Vector3.left;
                         offset2 = Vector3.back;
                         DrawFace(start + Vector3.right, offset1, offset2, block,verts,uvs,tris);
                     }
                     
                     if (IsTransparent(x, y + 1, z))
                     {
                         offset1 = Vector3.right;
                         offset2 = Vector3.back;
                         DrawFace(start + Vector3.up, offset1, offset2, block,verts,uvs,tris);
                     }
                     
                     if (IsTransparent(x - 1, y, z))
                     {
                         offset1 = Vector3.back;
                         offset2 = Vector3.down;
                         DrawFace(start + Vector3.up, offset1, offset2, block,verts,uvs,tris);
                     }
                     
                     if (IsTransparent(x + 1, y, z))
                     {
                         offset1 = Vector3.forward;
                         offset2 = Vector3.down;
                         DrawFace(start + Vector3.right + Vector3.up + Vector3.back, offset1, offset2, block,verts,uvs,tris);
                     }
                     
                     
                     if (IsTransparent(x, y, z - 1))
                     {
                         offset1 = Vector3.right;
                         offset2 = Vector3.down;
                         DrawFace(start + Vector3.back + Vector3.up, offset1, offset2, block,verts,uvs,tris);
                     }
                     
                     
                     if (IsTransparent(x, y, z + 1))
                     {
                         offset1 = Vector3.left;
                         offset2 = Vector3.down;
                         DrawFace(start + Vector3.up + Vector3.right, offset1, offset2, block,verts,uvs,tris);
                     }
                 }
             }
         }
                     
         visualMesh.vertices = verts.ToArray();
         visualMesh.uv = uvs.ToArray();
         visualMesh.triangles = tris.ToArray();
         visualMesh.RecalculateBounds();
         visualMesh.RecalculateNormals();
         
         meshFilter.mesh = visualMesh;
         meshCollider.sharedMesh = visualMesh;
         
         yield return 0;
         
     }
     
     public void DrawFace(Vector3 start, Vector3 offset1, Vector3 offset2, byte block,List<Vector3> verts, List<Vector2> uv, List<int> tris)
     {
         Terrain terrain = Terrain.activeTerrain;
         int index = verts.Count;
         
         BlockType blockType = Terrain.blocks.GetBlock(block);
         float zoom = 0.0625F;
         
         Vector2 uvBase = blockType.uv;
         
         if(blockType.sideTexture == true)
         {
             if ((offset2 == Vector3.down))
             {
                 uvBase = blockType.side;
             }
         }
         if(blockType.bottomTexture == true)
         {
             if (!(offset2 == Vector3.down) && !(offset1 == Vector3.right))
             {
                 uvBase = blockType.bottom;
             }
         }
         
         uv.Add(uvBase);
         uv.Add(uvBase + new Vector2(-zoom , 0F));
         uv.Add(uvBase + new Vector2(0F , zoom));
         uv.Add(uvBase + new Vector2(-zoom , zoom));
         
         verts.Add (start);
         verts.Add (start + offset1);
         verts.Add (start + offset2);
         verts.Add (start + offset1 + offset2);
         tris.Add (index + 0);
         tris.Add (index + 1);
         tris.Add (index + 2);
         tris.Add (index + 3);
         tris.Add (index + 2);
         tris.Add (index + 1);
         
     }
Comment

People who like this

0 Show 0 · 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

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

16 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

Related Questions

Best practices for developing optimized interiors for Unity? 2 Answers

Question about procedurally generated content efficiency 2 Answers

100000+ Instantiated Objects Optimization 0 Answers

What is the best way to load an instance of a gameObject when loading a saved game? 0 Answers

Problem moving static batch root when using StaticBatchingUtility.Combine 1 Answer


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