- Home /
[SOLVED]Procedural generated side by side cubes without 'seams'?
(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!
you need to be able to control the normals to achieve what you want.
The normals need to match. A cube with flat-shaded sides has 6 sides * 4 vertices per side = 24 vertices. All those vertices visible in your screenshot should have Vector3.up as their normal.
we shouldn't give away such mesh building secrets man :)
indeed Jihoon you must understand RecalculateNormals, which you would NOT use here
pls read the many questions about it on here.
http://answers.unity3d.com/questions/329116/do-we-need-to-call-recalculatenormals-if-normal-ve.html
Be sure to vote-up that answer with the thumb button. I need moar points
http://answers.unity3d.com/questions/336967/constructive-advice-needed-for-using-builtin-array.html
"As this is a simple cube with all shared vertices"
you CAN NOT SHARE VERTICES. generally NEVER SHARE VERTICES when you are weaving mesh.
this is a highly misunderstood topic.
NEVER EVER EVER SHARE VERTICES.
that's clear now right?
carefully CAREFULLY read this LONG article that shows even advanced people are often stupid on this issue
http://answers.unity3d.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html
read all of these
http://answers.unity3d.com/questions/423569/meshvertices-is-too-small.html
http://answers.unity3d.com/questions/417483/is-there-a-way-to-assign-meshverticesi-directly.html
there are a vast number of long excellent answers on this topic if you would just search here
what Loius meant is all the normals must point "upwards from that face"
got it ? so each face has six vertices and hence six normals.
got it ?
each of the six normals must point straight-upwards from that face.
this has actually been answered many many times here
Answer by Fattie · May 16, 2013 at 11:33 AM
Never share vertices when weaving mesh
articles on this
http://answers.unity3d.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html
http://answers.unity3d.com/questions/423569/meshvertices-is-too-small.html
http://answers.unity3d.com/questions/417483/is-there-a-way-to-assign-meshverticesi-directly.html
Answer by mezzostatic · Jan 01, 2017 at 09:20 AM
Not 100 percent sure of this because i solved it a while back, i'm pretty sure however:
If you send mesh objects with different positions to the graphics card, unity's precision is not high enough and the actual vertex positions in the graphics cards, the seams, will not be the same. hence you will see seams inside the graphics card.
If you put all your cubes on 0,0,0 and instead change their vertex positions to the positions you want, and send them all to the graphics card, they will not contain seams.
AFAIK i solved the issue after some time and puzzling, perhaps waster 50 hours to solve it, becaues i was told that my problem was inside the game engine not the graphics card.
So... keep all cubes on zero zero zero, edit their vertex positions, rewrite all the vertices to the new positions you want the cubes to be in, there should be no mesh, please confirm, ive done it with terrain and the seams completely vanished.
Its a weird error inside Unity that no one else than me knows about because i am a lame coder who never managed to code a game but i know so much about everything.
Your answer
Follow this Question
Related Questions
Seams between Primitive Cubes 1 Answer
Lighting Issues with custom generated mesh 0 Answers
How to change type of shading? 1 Answer
Determine whether or not model is "inside out" 2 Answers
Trying to create a hard edged procedural torus mesh 2 Answers