(SOLUTION IS AT BOTTOM!)
I apologize if this has been answered somewhere else, but I have been searching for a few days now and can’t seem to find an answer to my problem. Here is my code and a picture of the results, and at the bottom I outline my issue I am trying to solve/wanting to achieve.
I am procedurally generating cubes with the code below:
private GameObject CreateBoxes(Vector3 startPos)
{
Mesh myMesh = new Mesh();
Vector3 topLeftFront = startPos;
Vector3 topRightFront = new Vector3(startPos.x+1, startPos.y, startPos.z);
Vector3 bottomLeftFront = new Vector3(startPos.x, startPos.y+1, startPos.z);
Vector3 bottomRightFront = new Vector3(startPos.x+1, startPos.y+1, startPos.z);
Vector3 topLeftBack = new Vector3(startPos.x, startPos.y, startPos.z+1);
Vector3 topRightBack = new Vector3(startPos.x+1, startPos.y, startPos.z+1);
Vector3 bottomLeftBack = new Vector3(startPos.x, startPos.y+1, startPos.z+1);
Vector3 bottomRightBack = new Vector3(startPos.x+1, startPos.y+1, startPos.z+1);
//Assigning vertices to the mesh
Vector3[] verts = new Vector3[8] {topLeftFront, topRightFront, topLeftBack, topRightBack,
bottomLeftFront, bottomRightFront, bottomLeftBack, bottomRightBack};
myMesh.vertices = verts;
//Assigning UV coordinates (Texture coordinates)
myMesh.uv = tempChunk.Uvs;
//Assigning triangles for faces from chunk tri array
myMesh.triangles = tempChunk.Tris;
//Calculating normals, adding Mesh to object, adding texture to mesh, and collider
myMesh.RecalculateNormals();
GameObject myBox = new GameObject("Block");
myBox.tag = "Block";
myBox.AddComponent("MeshFilter");
myBox.AddComponent("MeshRenderer");
myBox.GetComponent<MeshFilter>().mesh = myMesh;
myBox.AddComponent("MeshCollider");
myBox.renderer.sharedMaterial = new Material(Shader.Find("Diffuse"));
return myBox;
}
For the Triangles and UV’s, it is here:
private int[] tris = new int[36] {5, 1, 0, 0, 4, 5,
6, 4, 0, 0, 2, 6,
7, 5, 4, 4, 6, 7,
5, 7, 3, 3, 1, 5,
3, 7, 6, 6, 2, 3,
3, 2, 0, 0, 1, 3};
private Vector2[] uvs = new Vector2[8]
{new Vector2( 0, 0 ),
new Vector2( 1, 0 ),
new Vector2( 1, 1 ),
new Vector2( 0, 1 ),
new Vector2( 0, 0 ),
new Vector2( 1, 0 ),
new Vector2( 1, 1 ),
new Vector2( 0, 1 )};
Using a for loop with proper increments, I am resulting in this cube generation (Which is exactly what I want):
THE PROBLEM: I want to have the cubes instead of looking like seperate cubic objects, look like a smooth singular surface (even though they are still seperate.) I have tried sharing normals among some vertices, and sharing vertices alone, but nothing I do seems to work, they always have that cubic ‘outline’ on them with seams.
If anyone could shed some light on this for me, I’d greatly appreciate it. Thanks in advance, guys!
THE SOLUTION: Taking the knowledge from Loius, and Fattie’s great advice and question/answer link in the comments, I stopped trying to fiddle with an 8 vertex cube and instead went to my original 24 vertex per cube setup. The cubes side by side this way appeared seamless and lit correctly.
What I took from it is, don’t share vertices between faces on a shape. It seems like it gave me nothing but trouble. I hope this helps someone in the future who might have the same issue!