Search Unity

Help request for voxel terrain smoothing - pro boolean logicians only!

Discussion in 'Scripting' started by Olgo, Mar 30, 2014.

  1. Olgo

    Olgo

    Joined:
    Jul 26, 2012
    Posts:
    9
    Hey guys, I need your help. I posted this in an earlier thread of mine but got no responses, I figure this problem warrants a thread of its own. I can't figure out the boolean logic required to describe whether a voxel should be sloped or not. I've got the logic down to determine whether the corner of a voxel should be brought in a little bit to achieve a smoother look, but I also want to drop it down to achieve a sloped look.

    The algorithm builds each of the top 4 corners of a voxel separately, you can query whether the voxels around it are air or not. If they are air (empty) you can then decide whether the current corner should be adjusted for smoothness or not. Have a look at some sample code, the portion for the x and z axes works perfectly... can't seem to figure out Y without leaving gaps.

    Is it even possible with the information / algorithm at hand?

    Code (csharp):
    1.     void AddTopNorthWestVertex(int x, int y, int z){
    2.  
    3.         //Checking Top North West Corner
    4.         yAdjust = 0;
    5.         xAdjust = 0;
    6.         zAdjust = 0;
    7.  
    8.         if( WestCubeIsAir(x,y,z)  WestTopCubeIsAir(x,y,z)  NorthTopCubeIsAir(x,y,z)  NorthWestIsAir(x,y,z)  NorthWestTopIsAir(x,y,z)  TopCubeIsAir(x,y,z) ){
    9.             xAdjust = xSmooth;
    10.         }
    11.  
    12.         if( NorthCubeIsAir(x,y,z)  WestTopCubeIsAir(x,y,z)  NorthTopCubeIsAir(x,y,z)  NorthWestIsAir(x,y,z)  NorthWestTopIsAir(x,y,z)  TopCubeIsAir(x,y,z) ){
    13.             zAdjust = zSmooth;
    14.         }
    15.  
    16.         //this block will keep 2 diagonals from connecting weird
    17.         if( !NorthWestIsAir(x,y,z)  NorthCubeIsAir(x,y,z)  WestCubeIsAir(x,y,z) ){
    18.             xAdjust = 0;
    19.             zAdjust = 0;
    20.         }
    21.  
    22.         // add logic for y adjust smoothing
    23.         if( !NorthTopCubeIsAir(x,y,z)   //.....  HELP!
    24.  
    25.         newVertices.Add(new Vector3 (x + xAdjust,  y - yAdjust,  z + 1 - zAdjust));
    26.    }