Search Unity

Weird Texturing Issue, Voxel Terrian Gen

Discussion in 'Scripting' started by Zero_Xue, Nov 30, 2017.

  1. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    Hi ive been working on a Voxel terrian generator for some time now and its nearing the end of it all, bit ive come across something weird and not entirely sure how to fix it as you can see by the image below am getting seams between each voxel, each voxel is 1x1x1 and placed right next to each other, I know it has something to do with my Atlas am using and ive played with it for hours but to no result, so am here to ask for help....

    -- Current Results


    --My Atlas Function
    Code (CSharp):
    1.  
    2.     public Texture2D CreateAtlas()
    3.     {
    4.         string[] _Images = System.IO.Directory.GetFiles("Textures/");
    5.         //Debug.Log("Loaded " + (_Images.Length - 1) + " Textures");
    6.  
    7.         int pixelwidth = 128;
    8.         int pixelheight = 128;
    9.         int AtlasWidth = Mathf.CeilToInt((Mathf.Sqrt(_Images.Length) + 2) * pixelwidth);
    10.         int AtlasHeight = Mathf.CeilToInt((Mathf.Sqrt(_Images.Length) + 2) * pixelheight);
    11.         //Debug.Log(AtlasWidth + "," + AtlasHeight);
    12.         Texture2D Atlas = new Texture2D(AtlasWidth, AtlasHeight);
    13.         int count = 0;
    14.         for (int x = 0; x < AtlasWidth / pixelwidth; x++)
    15.         {
    16.             for (int y = 0; y < AtlasHeight / pixelheight; y++)
    17.             {
    18.                 if (count >= _Images.Length - 1)
    19.                 {
    20.                     goto end;
    21.                 }
    22.                 Texture2D temp = new Texture2D(0, 0);
    23.                 temp.LoadImage(System.IO.File.ReadAllBytes(_Images[count]));
    24.                 Atlas.SetPixels(x * pixelwidth, y * pixelheight, pixelwidth, pixelheight, temp.GetPixels());
    25.  
    26.                 float startx = x * pixelwidth;
    27.                 float starty = y * pixelheight;
    28.                 float perpixelratiox = 1 / (float)Atlas.width;
    29.                 float perpixelratioy = 1 / (float)Atlas.height;
    30.                 startx *= perpixelratiox;
    31.                 starty *= perpixelratioy;
    32.                 float endx = startx + (perpixelratiox * pixelwidth);
    33.                 float endy = starty + (perpixelratioy * pixelheight);
    34.  
    35.                 UVMap.AddNewMap(_Images[count], new Vector2[]
    36.                 {
    37.                     new Vector2(startx,starty),
    38.                     new Vector2(startx,endy),
    39.                     new Vector2(endx,starty),
    40.                     new Vector2(endx,endy)
    41.                 });
    42.                 count++;
    43.             }
    44.         }
    45.  
    46.         end:;
    47.  
    48.         Texture2D SavedAtlas = new Texture2D(0, 0);
    49.         SavedAtlas.LoadImage(Atlas.EncodeToPNG());
    50.         SavedAtlas.wrapMode = TextureWrapMode.Clamp;
    51.         SavedAtlas.filterMode = FilterMode.Point;
    52.  
    53.         return SavedAtlas;
    54.     }
    55.  
    --The way i setup each Voxel Cube
    Code (CSharp):
    1.  
    2.     private MeshData GetMeshData(Vector3 Pos, int Dir, int CurrentVertCount,Vector2[] UVMap)
    3.     {
    4.         List<Vector3> vertices = new List<Vector3>();
    5.         if (Dir == 0) // Top
    6.         {
    7.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 1, Pos.z + 1));
    8.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 1, Pos.z + 1));
    9.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 1, Pos.z + 0));
    10.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 1, Pos.z + 0));
    11.         }
    12.  
    13.         if (Dir == 1) // Bottom
    14.         {
    15.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 0, Pos.z + 0));
    16.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 0, Pos.z + 0));
    17.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 0, Pos.z + 1));
    18.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 0, Pos.z + 1));
    19.         }
    20.  
    21.         if (Dir == 2) //Back
    22.         {
    23.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 0, Pos.z + 0));
    24.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 0, Pos.z + 0));
    25.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 1, Pos.z + 0));
    26.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 1, Pos.z + 0));
    27.         }
    28.  
    29.         if (Dir == 3) //Front
    30.         {
    31.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 0, Pos.z + 1));
    32.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 0, Pos.z + 1));
    33.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 1, Pos.z + 1));
    34.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 1, Pos.z + 1));
    35.         }
    36.  
    37.         if (Dir == 4) //Right
    38.         {
    39.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 0, Pos.z + 1));
    40.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 0, Pos.z + 0));
    41.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 1, Pos.z + 0));
    42.             vertices.Add(new Vector3(Pos.x + 1, Pos.y + 1, Pos.z + 1));
    43.         }
    44.  
    45.         if (Dir == 5) //Left
    46.         {
    47.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 0, Pos.z + 0));
    48.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 0, Pos.z + 1));
    49.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 1, Pos.z + 1));
    50.             vertices.Add(new Vector3(Pos.x + 0, Pos.y + 1, Pos.z + 0));
    51.         }
    52.  
    53.         List<int> Tris = new List<int>
    54.         {
    55.             CurrentVertCount + 0,
    56.             CurrentVertCount + 1,
    57.             CurrentVertCount + 3,
    58.             CurrentVertCount + 1,
    59.             CurrentVertCount + 2,
    60.             CurrentVertCount + 3
    61.         };
    62.  
    63.         List<Vector2> Uvs = new List<Vector2>
    64.         {
    65.             UVMap[2],UVMap[0],UVMap[1],UVMap[3]
    66.         };
    67.  
    68.         MeshData NewData = new MeshData();
    69.         NewData.Verts = vertices;
    70.         NewData.Tris = Tris;
    71.         NewData.UV = Uvs;
    72.         return NewData;
    73.     }
    74.  
    --and the atlas it create



    And yes i know its minecraft, but its all the textures i had on hand XD

    Ive tried turning off AA, changed anisotropic Textures all the stuff its said on Google... am just completly outta ideas now...
     
  2. jackmememe

    jackmememe

    Joined:
    Jan 21, 2016
    Posts:
    122
    I did encounter this problem before. As far i know, even without anisotropic filters, unity still get some pixels from the neighbors textures, so in your picture, i think it is taking a texture with alpha 0 that is next to the ground texture, the further the model is, the bigger this problem get.

    I found two solutions for this. the first one is to zoom the UV a little bit for each texture, this some times break the seamless textures.

    The other one is to extend each texture in the atlas, to have extra pixels in each corner.

    I do have a script that does both of those things:
    https://www.assetstore.unity3d.com/en/#!/content/94126

    I will send you a voucher for it, so you can see if any of those solutions work for you.