Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Halabya · Jun 16, 2015 at 12:48 PM · cubeopenglworldchunks

cube based enging

hey everyone this is my first time to post a question on the unity3d ask forum :D anyway i've been working on a cubical based engine and the chunks seems to take long to load when they are more than 6*6*6 but it takes about 4-5 seconds to load a chunk 12*12*12 size ! and no matter how i adjust my code to had smaller log() ! it still take the same amount of time ! i used arrays to create the vertices and then throw them with the triangles arrays to the an empty GO mesh ! if u need any farther info ask away and here is all the code used for the chunk and all related classes

face Class

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TestFace {
     Vector3 vertex1;
     Vector3 vertex2;
     Vector3 vertex3;
     Vector3 vertex4;
 
     public TestFace(Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4){
         vertex1 = v1;
         vertex2 = v2;
         vertex3 = v3;
         vertex4 = v4;
         //Debug.Log ("("+v1.ToString ()+":"+v2.ToString ()+":"+v3.ToString ()+":"+v4.ToString ()+")");
 
     }
 
     public Vector3[] toVec(){
         return new Vector3[]{vertex1, vertex2, vertex3, vertex4};
     }
 
     public string toStr(){
         return "("+vertex1.ToString ()+":"+vertex2.ToString ()+":"+vertex3.ToString ()+":"+vertex4.ToString ()+")";
     }
 
     public string flip(){
         List<Vector3> test = new List<Vector3> ();
         test.Add (vertex4);
         test.Add (vertex2);
         test.Add (vertex3);
         test.Add (vertex1);
         //return test;
         return "("+vertex4.ToString ()+":"+vertex2.ToString ()+":"+vertex3.ToString ()+":"+vertex1.ToString ()+")";
     }
 }


and the chunk class

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 
 public class TestChunk : MonoBehaviour {
 
     // Variables
     public int chunkWidth, chunkHeight;
     List<Vector3> givenVertices;
     List<int> givenTriangles;
     List<Vector2> givenUvs;
 
     List<TestFace> faces;
     bool[] boolFaces;
     List<Vector3[]> vecFaces;
     List<string> strFaces;
     List<int> optimisedTriangles;
 
     Mesh mesh;
 
     
     void Start (){
         givenVertices = new List<Vector3> ();
         givenTriangles = new List<int> ();
         givenUvs = new List<Vector2> ();
 
         faces = new List<TestFace> ();
         vecFaces = new List<Vector3[]> ();
         strFaces = new List<string> ();
         optimisedTriangles = new List<int> ();
         mesh = new Mesh ();
 
         for(int x=0; x<chunkWidth; x++){
             for(int z=0; z<chunkWidth; z++){
                 for(int y=0; y<chunkHeight; y++){
                     addBlock(x,y,z);
                 }
             }
         }
         optimise ();
         creatMesh ();
     }
 
     void Update() {
 
     }
 
     void addBlock(int x, int y, int z){
         faces.Add (new TestFace (new Vector3 (x,y+1,z), new Vector3 (x,y+1,z+1), new Vector3 (x+1,y+1,z), new Vector3 (x+1,y+1,z+1))); // Creating the Top Face of the Block
         faces.Add (new TestFace (new Vector3 (x+1,y,z+1), new Vector3 (x,y,z+1), new Vector3 (x+1,y,z), new Vector3 (x,y,z)));           // Creating the Bottom Face of the Block
         faces.Add (new TestFace (new Vector3 (x,y,z), new Vector3 (x,y,z+1), new Vector3 (x,y+1,z), new Vector3 (x,y+1,z+1))); // Creating the Front Face of the Block
         faces.Add (new TestFace (new Vector3 (x+1,y+1,z+1), new Vector3 (x+1,y,z+1), new Vector3 (x+1,y+1,z), new Vector3 (x+1,y,z))); // Creating the Back Face of the Block
         faces.Add (new TestFace (new Vector3 (x+1,y,z), new Vector3 (x,y,z), new Vector3 (x+1,y+1,z), new Vector3 (x,y+1,z))); // Creating the Right Face of the Block
         faces.Add (new TestFace (new Vector3 (x,y+1,z+1), new Vector3 (x,y,z+1), new Vector3 (x+1,y+1,z+1), new Vector3 (x+1,y,z+1))); // Creating the Left Face of the Block
     }
 
     void optimise(){
 
         // Calculating Lists
         calculate_vecFaces ();
         boolFaces = new bool[vecFaces.Count];
 
         // Fixing the bools And Calculating the givenTriangles & givenVertices
         for(int i=0; i<vecFaces.Count; i++){
             //calculating the givenVertices
             givenVertices.Add(vecFaces[i][0]);
             givenVertices.Add(vecFaces[i][1]);
             givenVertices.Add(vecFaces[i][2]);
             givenVertices.Add(vecFaces[i][3]);
             //calculating the givenUvs
             givenUvs.Add (new Vector2(0,0));
             givenUvs.Add (new Vector2(0,1));
             givenUvs.Add (new Vector2(1,0));
             givenUvs.Add (new Vector2(1,1));
             //calculating the givenTriangles
             givenTriangles.Add (i*4);
             givenTriangles.Add (i*4 +1);
             givenTriangles.Add (i*4 +2);
             givenTriangles.Add (i*4 +2);
             givenTriangles.Add (i*4 +1);
             givenTriangles.Add (i*4 +3);
             //Fixing the bools
 
             Debug.Log(faces[i].flip () + " : " + strFaces[0]);
             if(strFaces.Contains(faces[i].flip ())){
                 boolFaces[i] = false;
                 Debug.Log("accessed ..... ");
             }else{
                 boolFaces[i] = true;
             }
         }
 
         // Creating the optimised Triangles
         for(int i=0; i<boolFaces.Length; i++){
             //Debug.Log(boolFaces[0].ToString() + " : " + boolFaces[1].ToString() + " : " + boolFaces[2].ToString() + " : " + boolFaces[3].ToString() + " : " + boolFaces[4].ToString() + " : " + boolFaces[5].ToString());
             if(boolFaces[i]){
                 //Debug.Log(i*6 +1);
                 optimisedTriangles.Add(givenTriangles[i*6 + 0]);
                 optimisedTriangles.Add(givenTriangles[i*6 + 1]);
                 optimisedTriangles.Add(givenTriangles[i*6 + 2]);
                 optimisedTriangles.Add(givenTriangles[i*6 + 3]);
                 optimisedTriangles.Add(givenTriangles[i*6 + 4]);
                 optimisedTriangles.Add(givenTriangles[i*6 + 5]);
             }
         }
     }
 
     void calculate_vecFaces(){
         for(int i=0; i<faces.Count; i++){
             vecFaces.Add(faces[i].toVec());
             strFaces.Add(faces[i].toStr ());
         }
     }
 
     void creatMesh(){
         mesh.vertices = givenVertices.ToArray ();
         mesh.triangles = optimisedTriangles.ToArray ();
         mesh.uv = givenUvs.ToArray ();
         mesh.RecalculateBounds ();
         mesh.RecalculateNormals ();
         GetComponent<MeshFilter> ().mesh = mesh;
         Debug.Log (GetComponent<MeshFilter> ().mesh.vertices.Length);
         Debug.Log (GetComponent<MeshFilter> ().mesh.triangles.Length);
 
         /*
         Debug.Log ("given >> " + givenTriangles.Count);
         Debug.Log ("Optimised >> " + optimisedTriangles.Count);
         Debug.Log ("( " + optimisedTriangles[0] + " : " + optimisedTriangles[1] + " : " + optimisedTriangles[2] + " : " + optimisedTriangles[3] + " : " + optimisedTriangles[4] + " : " + optimisedTriangles[5] + " : " + 
                    optimisedTriangles[6] + " : " + optimisedTriangles[7] + " : " + optimisedTriangles[8] + " : " + optimisedTriangles[9] + " : " + optimisedTriangles[10] + " : " + optimisedTriangles[11] + " )");
         */
     }
 }
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
1
Best Answer

Answer by Dave-Carlile · Jun 16, 2015 at 12:51 PM

Running this through the Unity Profiler will show you where the code is running slowly, and that is where you want to concentrate your optimization. Use Deep Profile to allow drilling into functions.

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 Halabya · Jun 17, 2015 at 07:50 AM 1
Share

O$$anonymous$$G dude i Love youuuuuuu :D thank u sooooo much for your help and amazing feature :D i have managed to find the problem and even refine the code much better thank you so much mate (Y)

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Generating Meshes Is Slow 1 Answer

spinning cube 90 degrees along different axis 0 Answers

Cannot make a world edge 3 Answers

Generating Chunks 1 Answer

About 2D open worlds (top down) chunking 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