Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Tutorial] Procedural meshes and voxel terrain C#

Discussion in 'Community Learning & Teaching' started by 12pt, Sep 2, 2013.

  1. gapMindful

    gapMindful

    Joined:
    Jun 26, 2013
    Posts:
    17
    I just read through the first part of this tutorial and just wanted to say thanks. Its been a great read so far. The set up is super easy to follow and each part so far has been broken down really well.
     
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    I made 7 Days to Die like physics on this tutorial.

    To make them work you must add this function to chunk script:
    Code (CSharp):
    1. bool checkNeighbors (int x,int y, int z, int last, int steps) {
    2.         if (Block (x, y-1, z) != 0) {
    3.             return true;
    4.         }
    5.         if (steps > 10) {
    6.             return false;
    7.         }
    8.         steps++;
    9.         if(Block(x+1,y,z)!=0 && last !=1){
    10.             if (checkNeighbors(x+1,y,z, 0, steps)) {
    11.                 return true;
    12.             }  
    13.         }
    14.        
    15.         if(Block(x-1,y,z)!=0&&last!=0){
    16.             if (checkNeighbors(x-1,y,z, 1, steps)) {
    17.                 return true;
    18.             }
    19.         }
    20.        
    21.         if(Block(x,y,z+1)!=0&&last!=3){
    22.             if (checkNeighbors(x,y,z+1, 2, steps)) {
    23.                 return true;
    24.             }
    25.         }
    26.        
    27.         if(Block(x,y,z-1)!=0&&last!=2){
    28.             if (checkNeighbors(x,y,z-1,3,steps)) {
    29.                 return true;
    30.             }
    31.         }
    32.         return false;
    33.     }
    And in generate mesh function you must replace:
    Code (CSharp):
    1. if(world.Block(x,y-1,z)==0){
    2.        //Block below is air
    3.        CubeBot(x,y,z,world.Block(x,y,z));
    4.        
    5.       }
    with:
    Code (CSharp):
    1. if(Block(x,y-1,z)==0){
    2.                             //Block below is air
    3.                             CubeBot(x,y,z,Block(x,y,z));
    4.                             if (Block(x,y,z)!=3 || !checkNeighbors(x,y,z, 5, 0)) {
    5.                                 world.data[x+chunkX,y+chunkY-1,z+chunkZ]=Block(x,y,z);
    6.                                 world.data[x+chunkX,y+chunkY,z+chunkZ]=0;
    7.                                 update=true;
    8.                             }
    9.                         }
    I hope it helps someone.
    If you have any ideas for code optymalization post it here.
     
    00Fant likes this.
  3. Raja_Nukala

    Raja_Nukala

    Joined:
    Apr 18, 2017
    Posts:
    1


    I want to save and load these terrain(mesh) data. Is it possible?
     
  4. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Coooooool!
     
  5. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Can somebody help me? I can't open the pages!
     
    iamthecoolguy11 likes this.
  6. iamthecoolguy11

    iamthecoolguy11

    Joined:
    Jan 11, 2013
    Posts:
    8
  7. iamthecoolguy11

    iamthecoolguy11

    Joined:
    Jan 11, 2013
    Posts:
    8
    FreakForFreedom likes this.