- Home /
Dynamic Cube Rendering Problem
There's a very specific problem I am having. I'm currently making a voxel-based game that uses a chunk system. For each chunk, cubes are drawn out depending on their set values. Let's assume all of the spaces in the 3d space are filled. Always in the chunk that occupies the 0,0,0 of the chunk matrix, there seems to be a rendering problem. The picture is below, showing the dark gray space on the chunk.
If you move the chunk up or down, this happens:
I'm assuming this has something to do with how I'm setting up the mesh and texture coordinates. The way it works is that a cube is built out for each 1 unit. The code is below:
nTriangles.Add(faceCount * 4 ); //1
nTriangles.Add(faceCount * 4 + 1 );
nTriangles.Add(faceCount * 4 + 2 );
nTriangles.Add(faceCount * 4 );
nTriangles.Add(faceCount * 4 + 2 );
nTriangles.Add(faceCount * 4 + 3 );
nUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y));
nUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y + tUnit));
nUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y + tUnit));
nUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y));
faceCount++;
This is done for each side of the cube that has an empty space. nTriangles is a list of int. nUV is a list of Vector2's. Once all sides have been checked, the follow code is done:
mesh.Clear ();
mesh.vertices = nVertices.ToArray();
mesh.uv = nUV.ToArray();
mesh.triangles = nTriangles.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
col.sharedMesh=null;
col.sharedMesh=mesh;
nVertices.Clear();
nUV.Clear();
nTriangles.Clear();
faceCount=0;
col is the meshcollider of the current chunk. mesh is the the MeshFilter.mesh of the current chunk.
With these facts in mind, why do you believe this is happening?