- Home /
My voxel terrain is being rendered stretched in some places, but not in others. Why is this and how can it be fixed?
I am working on a voxel terrain generation, and for some reason, some of the textures appear stretched randomly, and I don't know how to go about fixing it. This is the problem:

As you can see, some blocks (most blocks) appear textured correctly and fine, however some are completely stretched like this. I'm generating the vertices etc, and then having them rendered, so only the faces visible are rendered. The textures are being read from a UV texture. Here is the code I'm working with:
public virtual void BuildFace(byte brick, Vector3 corner, Vector3 up, Vector3 right, bool reversed, List<Vector3> verts, List<Vector2> uvs, List<int> tris)
{
int index = verts.Count;
verts.Add (corner);
verts.Add (corner + up);
verts.Add (corner + up + right);
verts.Add (corner + right);
Vector2 uvWidth = new Vector2(0.25f, 0.25f);
Vector2 uvCorner = new Vector2(0.00f, 0.75f);
uvCorner.x += (float)(brick - 1) / 4;
uvs.Add(uvCorner);
uvs.Add(new Vector2(uvCorner.x, uvCorner.y + uvWidth.y));
uvs.Add(new Vector2(uvCorner.x + uvWidth.x, uvCorner.y + uvWidth.y));
uvs.Add(new Vector2(uvCorner.x + uvWidth.x, uvCorner.y));
if (reversed)
{
tris.Add(index + 0);
tris.Add(index + 1);
tris.Add(index + 2);
tris.Add(index + 2);
tris.Add(index + 3);
tris.Add(index + 0);
}
else
{
tris.Add(index + 1);
tris.Add(index + 0);
tris.Add(index + 2);
tris.Add(index + 3);
tris.Add(index + 2);
tris.Add(index + 0);
}
}
The Mesh generator is as follows:
public virtual void CreateVisualMesh(){
visualMesh = new Mesh ();
List<Vector3> verts = new List<Vector3>();
List<Vector2> uvs = new List<Vector2> ();
List<int> tris = new List<int> ();
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
for(int z = 0; z < width; z++)
{
if(map[x, y, z] == 0) continue;
byte brick = map[x,y,x];
//Left wall
if(IsTransparent(x-1, y, z))
BuildFace (brick, new Vector3(x,y,z), Vector3.up, Vector3.forward, false, verts, uvs, tris);
//Right wall
if(IsTransparent(x+1, y, z))
BuildFace (brick, new Vector3(x+1,y,z), Vector3.up, Vector3.forward, true, verts, uvs, tris);
//Bottom wall
if(IsTransparent(x, y-1, z))
BuildFace (brick, new Vector3(x,y,z), Vector3.forward, Vector3.right, false, verts, uvs, tris);
//Top wall
if(IsTransparent(x, y+1, z))
BuildFace (brick, new Vector3(x,y+1,z), Vector3.forward, Vector3.right, true, verts, uvs, tris);
//Back wall
if(IsTransparent (x, y, z-1))
BuildFace (brick, new Vector3(x,y,z), Vector3.up, Vector3.right, true, verts, uvs, tris);
//Front wall
if(IsTransparent(x, y, z+1))
BuildFace (brick, new Vector3(x,y,z+1), Vector3.up, Vector3.right, false, verts, uvs, tris);
}
}
}
visualMesh.vertices = verts.ToArray ();
visualMesh.uv = uvs.ToArray ();
visualMesh.triangles = tris.ToArray ();
visualMesh.RecalculateBounds ();
visualMesh.RecalculateNormals ();
meshFilter.mesh = visualMesh;
meshCollider.sharedMesh = visualMesh;
}
I'm very confused and don't know how to fix this. If anyone could please offer some advice, I'd be very grateful! Thanks a lot in advance :)
Your answer
Follow this Question
Related Questions
Textures do not render in not so long distances,Textures do not render in not log distances 0 Answers
How do I texture this voxel cube? 1 Answer
Voxel texturing with single texture instead of texturesheet? 0 Answers
Mesh renderer with three submesh textures - only one renders properly, others are solid colors 1 Answer