- Home /
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] + " )");
         */
     }
 }
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.
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
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                