Search Unity

Can't overpass 65 000 vertices limit

Discussion in 'Scripting' started by Olivier felix, Jun 16, 2015.

  1. Olivier felix

    Olivier felix

    Joined:
    Mar 27, 2015
    Posts:
    5
    Hi,
    It's been a week since i'm stuck on a single problem. I'm trying to create a map with an cubic aspect but with more optimization if I may. I created a script who allows me to create a 71*71 map but I want something around the 250*250. I tried to split the script in several ones (one for the height of the general map, one for the interface and the last one for creating the chunks), I tried to create a function and finally I tried to make a repetition of my creation but I can't make it work...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(MeshFilter))]
    5. [RequireComponent(typeof(MeshRenderer))]
    6. [RequireComponent(typeof(MeshCollider))]
    7. public class Code : MonoBehaviour {
    8.     /*public int size_x = 100;
    9.     public int size_z = 50;*/
    10.    
    11.     [HideInInspector]
    12.     public int ChunkSize = 71;
    13.    
    14.    
    15.     [Header("Ground properties")]
    16.        
    17.     //Overpass the 65 000 vertices limit
    18.     public int ChunkX = 1;
    19.     public int ChunkZ = 1;
    20.    
    21.     public float tileSize = 1.0f;
    22.     [Range(0,70)]
    23.     public int heightMax = 5;
    24.     [Range(0,30)]
    25.     public int flat = 10;
    26.    
    27.     public int[,] Matrix;
    28.    
    29.     // Use this for initialization
    30.     void Start () {
    31.         BuildMesh();  
    32.     }
    33.    
    34.     void BuildMesh(){
    35.         // Fixing data
    36.         int size_x = ChunkSize;
    37.         int size_z = ChunkSize;
    38.         int numTiles     = size_x * size_z;
    39.         int numTris     = numTiles * 2 * 2;
    40.         int numverts     = numTiles * 4;
    41.         int numberOfChunks = ChunkX * ChunkZ;
    42.         int[,] triangles    = new int [numTris * 3*2, numberOfChunks];
    43.        
    44.         // Generate the mesh Data
    45.         Vector3[,] vertices = new Vector3[numverts * 3, numberOfChunks];
    46.         Vector3[,] normals  = new Vector3[numverts*3, numberOfChunks];
    47.         Vector2[,] uv       = new Vector2[numverts*3, numberOfChunks];
    48.        
    49.        
    50.         // Generation on the Y axes
    51.         Matrix = new int[size_x * ChunkX ,size_z * ChunkZ];
    52.         int scale = 1;
    53.         int power = 1;
    54.         Vector2 v2SampleStart = new Vector2(0.0f, 0.0f);
    55.         v2SampleStart = new Vector2(Random.Range (0.0f, 10000.0f), Random.Range (0.0f, 10000.0f));
    56.         for (int x_y = 0; x_y < size_x * ChunkX ; x_y++) {
    57.             for (int z_y = 0; z_y < size_z * ChunkZ ; z_y++) {
    58.                 float xCoord = v2SampleStart.x + x_y * scale;
    59.                 float yCoord = v2SampleStart.y + z_y * scale;
    60.                 Matrix[x_y, z_y] = (int) (heightMax * (Mathf.PerlinNoise (xCoord / flat, yCoord / flat)) * power);
    61.             }
    62.         }
    63.  
    64.         for(int zChunk = 0; zChunk < ChunkZ; zChunk++)
    65.         {
    66.             for(int xChunk = 0; xChunk < ChunkX; xChunk++)
    67.             {
    68.                 // Generation of the planes
    69.                 int x, z = 0;
    70.                 for(z = 0 ; z < size_z ; z++){
    71.                     for(x = 0 ; x < size_x ; x++){
    72.                         int height = Matrix[x,z];
    73.                         //Vertices
    74.                         vertices[(x + z * size_x) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector3((x + 0 + ChunkX * ChunkSize) * tileSize, height, (z + 0 + ChunkZ * ChunkSize) * tileSize);
    75.                         vertices[(x + z * size_x) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector3((x + 1 + ChunkX * ChunkSize) * tileSize, height, (z + 0 + ChunkZ * ChunkSize) * tileSize);
    76.                         vertices[(x + z * size_x) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector3((x + 0 + ChunkX * ChunkSize) * tileSize, height, (z + 1 + ChunkZ * ChunkSize) * tileSize);
    77.                         vertices[(x + z * size_x) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector3((x + 1 + ChunkX * ChunkSize) * tileSize, height, (z + 1 + ChunkZ * ChunkSize) * tileSize);
    78.                         //Normals
    79.                         normals [(x + z * size_x) * 4 + 0,xChunk + zChunk*ChunkX] = Vector3.up;
    80.                         normals [(x + z * size_x) * 4 + 1,xChunk + zChunk*ChunkX] = Vector3.up;
    81.                         normals [(x + z * size_x) * 4 + 2,xChunk + zChunk*ChunkX] = Vector3.up;
    82.                         normals [(x + z * size_x) * 4 + 3,xChunk + zChunk*ChunkX] = Vector3.up;
    83.                         //Uvs
    84.                         uv [(x + z * size_x) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector2(x + 0, z + 0);
    85.                         uv [(x + z * size_x) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector2(x + 1, z + 0);
    86.                         uv [(x + z * size_x) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector2(x + 0, z + 1);
    87.                         uv [(x + z * size_x) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector2(x + 1, z + 1);
    88.                         //Triangles
    89.                         //First
    90.                         triangles[6 * (z * size_x + x) + 0,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 2;
    91.                         triangles[6 * (z * size_x + x) + 1,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 1;
    92.                         triangles[6 * (z * size_x + x) + 2,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 0;
    93.                         //Second
    94.                         triangles[6 * (z * size_x + x) + 3,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 1;
    95.                         triangles[6 * (z * size_x + x) + 4,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 2;
    96.                         triangles[6 * (z * size_x + x) + 5,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 3;
    97.                     }
    98.                 }
    99.                 // Generation in the X axe
    100.                 int essais = 0;
    101.                 for(int cycle = 0; cycle < size_x * size_z - 1; cycle++){
    102.                     essais = size_x * size_z + cycle;
    103.                     if(((cycle+1) % size_x) != 0){
    104.                         vertices[(essais) * 4 + 0,xChunk + zChunk*ChunkX] = vertices[1 + cycle * 4,xChunk + zChunk*ChunkX];
    105.                         vertices[(essais) * 4 + 1,xChunk + zChunk*ChunkX] = vertices[3 + cycle * 4,xChunk + zChunk*ChunkX];
    106.                         vertices[(essais) * 4 + 2,xChunk + zChunk*ChunkX] = vertices[4 + cycle * 4,xChunk + zChunk*ChunkX];
    107.                         vertices[(essais) * 4 + 3,xChunk + zChunk*ChunkX] = vertices[6 + cycle * 4,xChunk + zChunk*ChunkX];
    108.                        
    109.                         normals [(essais) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    110.                         normals [(essais) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    111.                         normals [(essais) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    112.                         normals [(essais) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    113.                        
    114.                        
    115.                         triangles[(essais) * 6 + 0,xChunk + zChunk*ChunkX] = (essais) * 4 + 0;
    116.                         triangles[(essais) * 6 + 1,xChunk + zChunk*ChunkX] = (essais) * 4 + 1;
    117.                         triangles[(essais) * 6 + 2,xChunk + zChunk*ChunkX] = (essais) * 4 + 2;
    118.                        
    119.                         triangles[(essais) * 6 + 3,xChunk + zChunk*ChunkX] = (essais) * 4 + 3;
    120.                         triangles[(essais) * 6 + 4,xChunk + zChunk*ChunkX] = (essais) * 4 + 2;
    121.                         triangles[(essais) * 6 + 5,xChunk + zChunk*ChunkX] = (essais) * 4 + 1;
    122.                     }
    123.                 }
    124.                
    125.                 // Generation in the Z axe
    126.                 essais = essais + 1;
    127.                 for (int cycle = 0; cycle < size_x * (size_z - 1); cycle++) {
    128.                     if(cycle != 0){
    129.                         vertices [(essais + cycle) * 4 + 0,xChunk + zChunk*ChunkX] = vertices [2 + cycle * 4,xChunk + zChunk*ChunkX];
    130.                         vertices [(essais + cycle) * 4 + 1,xChunk + zChunk*ChunkX] = vertices [3 + cycle * 4,xChunk + zChunk*ChunkX];
    131.                         vertices [(essais + cycle) * 4 + 2,xChunk + zChunk*ChunkX] = vertices [0 + size_x * 4 + cycle * 4,xChunk + zChunk*ChunkX];
    132.                         vertices [(essais + cycle) * 4 + 3,xChunk + zChunk*ChunkX] = vertices [1 + size_x * 4 + cycle * 4,xChunk + zChunk*ChunkX];
    133.                        
    134.                         normals [(essais + cycle) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    135.                         normals [(essais + cycle) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    136.                         normals [(essais + cycle) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    137.                         normals [(essais + cycle) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    138.                        
    139.                        
    140.                         triangles [(essais + cycle) * 6 + 0,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 2;
    141.                         triangles [(essais + cycle) * 6 + 1,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 1;
    142.                         triangles [(essais + cycle) * 6 + 2,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 0;
    143.                        
    144.                         triangles [(essais + cycle) * 6 + 3,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 1;
    145.                         triangles [(essais + cycle) * 6 + 4,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 2;
    146.                         triangles [(essais + cycle) * 6 + 5,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 3;
    147.                     }
    148.                 }
    149.                 // Create a new mesh and populate with data
    150.                 int[] ChunkTriangles = new int[numTris * 3*2];
    151.                 Vector3[] ChunkVertices = new Vector3[numverts * 3];
    152.                 Vector3[] ChunkNormals = new Vector3[numverts*3];
    153.                 Vector2[] ChunkUVs = new Vector2[numverts*3];
    154.  
    155.                 for(x = 0 ;x < numTris * 3*2 ; x++)
    156.                 {
    157.                     ChunkTriangles[x] = triangles[x,xChunk + zChunk*ChunkX];
    158.                 }
    159.                 for(x = 0 ;x < numTris * 3 ; x++)
    160.                 {
    161.                     ChunkVertices[x] = vertices[x,xChunk + zChunk*ChunkX];
    162.                     ChunkNormals[x] = normals[x,xChunk + zChunk*ChunkX];
    163.                     ChunkUVs[x] = uv[x,xChunk + zChunk*ChunkX];
    164.                 }
    165.                 Mesh mesh = new Mesh ();
    166.                 mesh.vertices     = ChunkVertices;
    167.                 mesh.triangles     = ChunkTriangles;
    168.                 mesh.normals     = ChunkNormals;
    169.                 mesh.uv         = ChunkUVs;
    170.  
    171.                 // Assign our mesh to our Flter/Renderer/Collider
    172.                 MeshFilter      mesh_filter   =     GetComponent<MeshFilter>();
    173.                 MeshCollider mesh_collider =     GetComponent<MeshCollider>();
    174.                 MeshRenderer mesh_renderer =     GetComponent<MeshRenderer>();
    175.  
    176.                 mesh_filter.mesh = mesh;
    177.             }
    178.         }
    179.     }
    180. }
    If anyone could come up with an idea it would be appreciated. If you have any idea on how it would be more optimized I would like it too !

    PS: Sorry if I made spelling mistake I'm french
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I made some changes to your script. Look for 'kwd' to see the changes.

    The two problems you had were:

    - all meshes were offset the same amount, and going int he same place in space
    - all meshes were being added to one game object. This makes each prior mesh go away, or just "dangle" in memory, not rendered or anything.

    I changed this:

    - all meshes are still generated in place (see my notes at the bottom of the code)
    - each mesh is on a separate game object
    - I recalculated bounds and normals automatically (not necessary really, just preferred)

    Again, look for 'kwd' for the things I changed.

    Kurt

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. // kwd - removed component requirements
    6.  
    7. public class Code : MonoBehaviour {
    8.     /*public int size_x = 100;
    9.     public int size_z = 50;*/
    10.  
    11.     [HideInInspector]
    12.     public int ChunkSize = 71;
    13.  
    14.  
    15.     [Header("Ground properties")]
    16.  
    17.     //Overpass the 65 000 vertices limit
    18.     public int ChunkX = 1;
    19.     public int ChunkZ = 1;
    20.  
    21.     public float tileSize = 1.0f;
    22.     [Range(0,70)]
    23.     public int heightMax = 5;
    24.     [Range(0,30)]
    25.     public int flat = 10;
    26.  
    27.     public int[,] Matrix;
    28.  
    29.     // Use this for initialization
    30.     void Start () {
    31.         BuildMesh();
    32.     }
    33.  
    34.     void BuildMesh(){
    35.         // Fixing data
    36.         int size_x = ChunkSize;
    37.         int size_z = ChunkSize;
    38.         int numTiles     = size_x * size_z;
    39.         int numTris     = numTiles * 2 * 2;
    40.         int numverts     = numTiles * 4;
    41.         int numberOfChunks = ChunkX * ChunkZ;
    42.         int[,] triangles    = new int [numTris * 3*2, numberOfChunks];
    43.      
    44.         // Generate the mesh Data
    45.         Vector3[,] vertices = new Vector3[numverts * 3, numberOfChunks];
    46.         Vector3[,] normals  = new Vector3[numverts*3, numberOfChunks];
    47.         Vector2[,] uv       = new Vector2[numverts*3, numberOfChunks];
    48.      
    49.      
    50.         // Generation on the Y axes
    51.         Matrix = new int[size_x * ChunkX ,size_z * ChunkZ];
    52.         int scale = 1;
    53.         int power = 1;
    54.         Vector2 v2SampleStart = new Vector2(0.0f, 0.0f);
    55.         v2SampleStart = new Vector2(Random.Range (0.0f, 10000.0f), Random.Range (0.0f, 10000.0f));
    56.         for (int x_y = 0; x_y < size_x * ChunkX ; x_y++) {
    57.             for (int z_y = 0; z_y < size_z * ChunkZ ; z_y++) {
    58.                 float xCoord = v2SampleStart.x + x_y * scale;
    59.                 float yCoord = v2SampleStart.y + z_y * scale;
    60.                 Matrix[x_y, z_y] = (int) (heightMax * (Mathf.PerlinNoise (xCoord / flat, yCoord / flat)) * power);
    61.             }
    62.         }
    63.      
    64.         for(int zChunk = 0; zChunk < ChunkZ; zChunk++)
    65.         {
    66.             for(int xChunk = 0; xChunk < ChunkX; xChunk++)
    67.             {
    68.                 // Generation of the planes
    69.                 int x, z = 0;
    70.                 for(z = 0 ; z < size_z ; z++){
    71.                     for(x = 0 ; x < size_x ; x++){
    72.                         int height = Matrix[x,z];
    73.                         //Vertices
    74.                         vertices[(x + z * size_x) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector3((x + 0 + ChunkX * ChunkSize) * tileSize, height, (z + 0 + ChunkZ * ChunkSize) * tileSize);
    75.                         vertices[(x + z * size_x) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector3((x + 1 + ChunkX * ChunkSize) * tileSize, height, (z + 0 + ChunkZ * ChunkSize) * tileSize);
    76.                         vertices[(x + z * size_x) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector3((x + 0 + ChunkX * ChunkSize) * tileSize, height, (z + 1 + ChunkZ * ChunkSize) * tileSize);
    77.                         vertices[(x + z * size_x) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector3((x + 1 + ChunkX * ChunkSize) * tileSize, height, (z + 1 + ChunkZ * ChunkSize) * tileSize);
    78.                         //Normals
    79.                         normals [(x + z * size_x) * 4 + 0,xChunk + zChunk*ChunkX] = Vector3.up;
    80.                         normals [(x + z * size_x) * 4 + 1,xChunk + zChunk*ChunkX] = Vector3.up;
    81.                         normals [(x + z * size_x) * 4 + 2,xChunk + zChunk*ChunkX] = Vector3.up;
    82.                         normals [(x + z * size_x) * 4 + 3,xChunk + zChunk*ChunkX] = Vector3.up;
    83.                         //Uvs
    84.                         uv [(x + z * size_x) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector2(x + 0, z + 0);
    85.                         uv [(x + z * size_x) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector2(x + 1, z + 0);
    86.                         uv [(x + z * size_x) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector2(x + 0, z + 1);
    87.                         uv [(x + z * size_x) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector2(x + 1, z + 1);
    88.                         //Triangles
    89.                         //First
    90.                         triangles[6 * (z * size_x + x) + 0,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 2;
    91.                         triangles[6 * (z * size_x + x) + 1,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 1;
    92.                         triangles[6 * (z * size_x + x) + 2,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 0;
    93.                         //Second
    94.                         triangles[6 * (z * size_x + x) + 3,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 1;
    95.                         triangles[6 * (z * size_x + x) + 4,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 2;
    96.                         triangles[6 * (z * size_x + x) + 5,xChunk + zChunk*ChunkX] = (x + z * size_x) * 4 + 3;
    97.                     }
    98.                 }
    99.                 // Generation in the X axe
    100.                 int essais = 0;
    101.                 for(int cycle = 0; cycle < size_x * size_z - 1; cycle++){
    102.                     essais = size_x * size_z + cycle;
    103.                     if(((cycle+1) % size_x) != 0){
    104.                         vertices[(essais) * 4 + 0,xChunk + zChunk*ChunkX] = vertices[1 + cycle * 4,xChunk + zChunk*ChunkX];
    105.                         vertices[(essais) * 4 + 1,xChunk + zChunk*ChunkX] = vertices[3 + cycle * 4,xChunk + zChunk*ChunkX];
    106.                         vertices[(essais) * 4 + 2,xChunk + zChunk*ChunkX] = vertices[4 + cycle * 4,xChunk + zChunk*ChunkX];
    107.                         vertices[(essais) * 4 + 3,xChunk + zChunk*ChunkX] = vertices[6 + cycle * 4,xChunk + zChunk*ChunkX];
    108.                      
    109.                         normals [(essais) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    110.                         normals [(essais) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    111.                         normals [(essais) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    112.                         normals [(essais) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector3(1, 0, 0);
    113.                      
    114.                      
    115.                         triangles[(essais) * 6 + 0,xChunk + zChunk*ChunkX] = (essais) * 4 + 0;
    116.                         triangles[(essais) * 6 + 1,xChunk + zChunk*ChunkX] = (essais) * 4 + 1;
    117.                         triangles[(essais) * 6 + 2,xChunk + zChunk*ChunkX] = (essais) * 4 + 2;
    118.                      
    119.                         triangles[(essais) * 6 + 3,xChunk + zChunk*ChunkX] = (essais) * 4 + 3;
    120.                         triangles[(essais) * 6 + 4,xChunk + zChunk*ChunkX] = (essais) * 4 + 2;
    121.                         triangles[(essais) * 6 + 5,xChunk + zChunk*ChunkX] = (essais) * 4 + 1;
    122.                     }
    123.                 }
    124.              
    125.                 // Generation in the Z axe
    126.                 essais = essais + 1;
    127.                 for (int cycle = 0; cycle < size_x * (size_z - 1); cycle++) {
    128.                     if(cycle != 0){
    129.                         vertices [(essais + cycle) * 4 + 0,xChunk + zChunk*ChunkX] = vertices [2 + cycle * 4,xChunk + zChunk*ChunkX];
    130.                         vertices [(essais + cycle) * 4 + 1,xChunk + zChunk*ChunkX] = vertices [3 + cycle * 4,xChunk + zChunk*ChunkX];
    131.                         vertices [(essais + cycle) * 4 + 2,xChunk + zChunk*ChunkX] = vertices [0 + size_x * 4 + cycle * 4,xChunk + zChunk*ChunkX];
    132.                         vertices [(essais + cycle) * 4 + 3,xChunk + zChunk*ChunkX] = vertices [1 + size_x * 4 + cycle * 4,xChunk + zChunk*ChunkX];
    133.                      
    134.                         normals [(essais + cycle) * 4 + 0,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    135.                         normals [(essais + cycle) * 4 + 1,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    136.                         normals [(essais + cycle) * 4 + 2,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    137.                         normals [(essais + cycle) * 4 + 3,xChunk + zChunk*ChunkX] = new Vector3 (0, 0, 1);
    138.                      
    139.                      
    140.                         triangles [(essais + cycle) * 6 + 0,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 2;
    141.                         triangles [(essais + cycle) * 6 + 1,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 1;
    142.                         triangles [(essais + cycle) * 6 + 2,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 0;
    143.                      
    144.                         triangles [(essais + cycle) * 6 + 3,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 1;
    145.                         triangles [(essais + cycle) * 6 + 4,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 2;
    146.                         triangles [(essais + cycle) * 6 + 5,xChunk + zChunk*ChunkX] = (essais + cycle) * 4 + 3;
    147.                     }
    148.                 }
    149.                 // Create a new mesh and populate with data
    150.                 int[] ChunkTriangles = new int[numTris * 3*2];
    151.                 Vector3[] ChunkVertices = new Vector3[numverts * 3];
    152.                 Vector3[] ChunkNormals = new Vector3[numverts*3];
    153.                 Vector2[] ChunkUVs = new Vector2[numverts*3];
    154.              
    155.                 for(x = 0 ;x < numTris * 3*2 ; x++)
    156.                 {
    157.                     ChunkTriangles[x] = triangles[x,xChunk + zChunk*ChunkX];
    158.                 }
    159.                 for(x = 0 ;x < numTris * 3 ; x++)
    160.                 {
    161.                     ChunkVertices[x] = vertices[x,xChunk + zChunk*ChunkX];
    162.                     ChunkNormals[x] = normals[x,xChunk + zChunk*ChunkX];
    163.                     ChunkUVs[x] = uv[x,xChunk + zChunk*ChunkX];
    164.                 }
    165.                 Mesh mesh = new Mesh ();
    166.                 mesh.vertices     = ChunkVertices;
    167.                 mesh.triangles     = ChunkTriangles;
    168.                 mesh.normals     = ChunkNormals;
    169.                 mesh.uv         = ChunkUVs;
    170.  
    171.                 // kwd - added a new game object to hold each
    172.                 // new piece of mesh and renderer
    173.              
    174.                 GameObject piece = new GameObject( "piece");
    175.  
    176.                 // kwd - move that game object around to line up
    177.  
    178.                 piece.transform.position = new Vector3( xChunk, 0, zChunk) * ChunkSize;
    179.      
    180.                 // Assign our mesh to our Flter/Renderer/Collider
    181.                 MeshFilter      mesh_filter   =    piece.AddComponent<MeshFilter>();
    182.                 MeshCollider mesh_collider =     piece.AddComponent<MeshCollider>();
    183.                 MeshRenderer mesh_renderer =     piece.AddComponent<MeshRenderer>();
    184.              
    185.                 mesh_filter.mesh = mesh;
    186.  
    187.                 // kwd - I prefer doing this automatically initially...
    188.                 mesh.RecalculateBounds();
    189.                 mesh.RecalculateNormals ();
    190.  
    191.                 // kwd - in the vertex generation above, I would recommend
    192.                 // making all pieces of ground around (0,0,0) in the mesh space,
    193.                 // then the center of the game object associated with it will
    194.                 // be close to where the geometry is.
    195.             }
    196.         }
    197.     }
    198. }
     
    ThermalFusion and BenZed like this.
  3. Olivier felix

    Olivier felix

    Joined:
    Mar 27, 2015
    Posts:
    5
    Thank you very much ! (for your precision and how quick you answered)
    For all the meshes going in the same place in space, I observed that when I was changing the number of chunks, the only chunk appears in a spot defined by coordinates (ChunkX, ChunkZ). So I was admitting that the creation was working but was just replacing with new data each cycle.
    Thank you again !

    Félix Olivier
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You are very welcome. That last recommendation though: if you make all the geometry "centered" around (0,0,0) on your game object, I think you will enjoy working in the editor much more, because then you can select a piece of ground in the editor, and press F to instantly Focus on the ground and see it clearly framed.

    Also, when I work with procedural geometry, I like to put a checkered pattern texture (via material) onto things as early as possible, such that you can see what is going on in the geometry.

    Also... I have found if you write your mesh-builder as a CoRoutine and move some things around a bit, you can see "on the fly" as the geometry is created and figure out where your problems are better. I have solved many of my bugs by turning a routine into a coroutine and then single-framing through to see visually where things are going.

    That is all, I hope you have fun with your terrain!

    Also, I have attached the test package I put together when looking at your code...
     

    Attached Files:

    Last edited: Jun 17, 2015
  5. Olivier felix

    Olivier felix

    Joined:
    Mar 27, 2015
    Posts:
    5
    I'll try to change my program to have it centered.
    I had a texture applied but not as intuitive as yours once again thank you for that !
    I just downloaded your attachment but one thing is bugging me, why does the overall shape seem to have the same dimensions independently of the settings? I'll be searching on my own but I would like to know if you have any advice concerning that!

    Felix Olivier.