- Home /
How remove block mesh gap
I'm creating a minecraft based world, but i came across a problem.
When I generate the world, it becomes like the image with this little gap between blocks.
How i remove it?
I'm creating each quad individually and then combining it in a single chunk.
This is the code used to combine:
private void CombineQuads()
{
var meshFilters = Object.GetComponentsInChildren<MeshFilter>();
var combine = new CombineInstance[meshFilters.Length];
for (int i = 0; i < meshFilters.Length; i++)
{
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
}
var mf = Object.AddComponent<MeshFilter>();
mf.mesh = new Mesh();
mf.mesh.CombineMeshes(combine);
MeshRenderer renderer = Object.AddComponent<MeshRenderer>();
renderer.material = TextureAtlas;
foreach (Transform quad in Object.transform)
{
GameObject.Destroy(quad.gameObject);
}
}
And this what i use to create each single quad:
private void CreateQuad(CubeSide side)
{
var mesh = new Mesh
{
name = side.ToString() + "QuadMesh",
};
var p0 = new Vector3(-0.5f, -0.5f, 0.5f);
var p1 = new Vector3(0.5f, -0.5f, 0.5f);
var p2 = new Vector3(0.5f, -0.5f, -0.5f);
var p3 = new Vector3(-0.5f, -0.5f, -0.5f);
var p4 = new Vector3(-0.5f, 0.5f, 0.5f);
var p5 = new Vector3(0.5f, 0.5f, 0.5f);
var p6 = new Vector3(0.5f, 0.5f, -0.5f);
var p7 = new Vector3(-0.5f, 0.5f, -0.5f);
switch (side)
{
case CubeSide.Bottom:
mesh.vertices = new Vector3[] { p0, p1, p2, p3 };
mesh.normals = new Vector3[] {Vector3.down, Vector3.down,
Vector3.down, Vector3.down};
break;
case CubeSide.Top:
mesh.vertices = new Vector3[] { p7, p6, p5, p4 };
mesh.normals = new Vector3[] {Vector3.up, Vector3.up,
Vector3.up, Vector3.up};
break;
case CubeSide.Left:
mesh.vertices = new Vector3[] { p7, p4, p0, p3 };
mesh.normals = new Vector3[] {Vector3.left, Vector3.left,
Vector3.left, Vector3.left};
break;
case CubeSide.Right:
mesh.vertices = new Vector3[] { p5, p6, p2, p1 };
mesh.normals = new Vector3[] {Vector3.right, Vector3.right,
Vector3.right, Vector3.right};
break;
case CubeSide.Front:
mesh.vertices = new Vector3[] { p4, p5, p1, p0 };
mesh.normals = new Vector3[] {Vector3.forward, Vector3.forward,
Vector3.forward, Vector3.forward};
break;
case CubeSide.Back:
mesh.vertices = new Vector3[] { p6, p7, p3, p2 };
mesh.normals = new Vector3[] {Vector3.back, Vector3.back,
Vector3.back, Vector3.back};
break;
}
mesh.uv = Block.UVs.TryGetValue(side, CubeSide.Front).ToArray();
mesh.triangles = new int[] { 3, 1, 0, 3, 2, 1 };
mesh.RecalculateBounds();
var quad = new GameObject("Quad");
quad.transform.parent = Parent.Object.transform;
quad.transform.position = Position;
var meshFilter = quad.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
}
Answer by Bunny83 · Oct 06, 2020 at 11:15 PM
It's hard to tell from the single screenshot. The issue is most likely a rendering issue. Though since Unity now supports several render pipelines and countless different shaders we can't really suggest much at this point. It could be an issue with your mipmaps. Do you use an atlas texture? If so are your tiles of a power of two size? Also what filtering method does the texture use? The Minecraft texture atlas doesn't require any padding space between tiles because it uses point filter mode and ensures to sample the center of a texel rather than the edge / corner.
Your answer
Follow this Question
Related Questions
Add perlin noise to blocky terrain 0 Answers
Minecraft like building system 0 Answers
15625 blocks are making my game lag? Increase performance? 3 Answers