Hello Unity community,
So, I’ve asked on Unity Answers about this, but eventually I figured that those black lines are impossibly the work of inaccurate block placement.
The Problem so far:
To simplify it as much as possible, a grid of primitive cubes in Unity will produce rendering artifacts at random camera positions that look like shading errors between 2 cubes that should be snapped next to each other but aren’t.
Of course a lot of people on Unity Answers were very kind and offered a lot of possible solutions, the problem persists though and after some investigation yesterday I am almost certain that this is not an issue of shading.
Why? Well, mainly because the artifacts keep appearing when the cube meshes have casting and recieving shadows off, but there are more things that make me think that its my Unity messing with me, as other people on UA didn’t experience the same kind of problem.
Reproduction of the problem:
Basically I am spawning a grid of primitive cubes and clone them to another grid of clones of primitive cubes, just to make sure that the error is not caused by the cloning of prefabs, using this code.
for (int i = 0 ; i < 10 ; i++)
{
for (int j = 0 ; j < 10; j++)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localScale += new Vector3(0.2f,0.2f,0.2f);
cube.transform.position = new Vector3(i,j,-5f);
Instantiate (cube,new Vector3(i+11,j,(-5f)),Quaternion.identity);
}
}
Next, I’ve set up a camera that I can move by a very little using the arrow buttons and is directly facing my grid (meaning no rotation) .
I moved the camera around a little bit and of course those artifacts appeared again. Carefully I moved into a position where one of the artifacts simply stayed on screen. (For me that meant that its not a random artifact that appears for a single frame because rendering screwed up, but I’m new to Unity so I wouldn’t know)
Now, the common workaround that was recommended to fix the minute gap error was to make the cubes a bit larger so they overlap. But as I stated before this is probably not a matter of gaps between blocks, I exaggerated a bit and made the cubes 1.2 large (after trying very small values at first of course.)
Screenshot Attached as Overlap.png
(It’s too large to be shown properly here, please open the screenshot in a new tab.)
As you can see, even though both blocks overlap by a fair bit, the black line is getting rendered.
I tried around a bit and those are further results.
Marking BOTH blocks and pulling them right or left even by a tiny bit would make the line disappear at the place those blocks were located. Pulling them back at their original position would make the line reappear.
Screenshot Attached as Pull right.png
(It’s too large to be shown properly here, please open the screenshot in a new tab.)
Marking only the left block and pulling it further into the direction of the right block would cause the line to disappear at it’s position (It’s too large to be shown properly here, please open the screenshot in a new tab.)
Screenshot Attached as pull left into right.png
(It’s too large to be shown properly here, please open the screenshot in a new tab.)
But Marking the right block and pulling it into the left block to further overlap it would not give the same effect.
Screenshot Attached as pull right into left.png
(It’s too large to be shown properly here, please open the screenshot in a new tab.)
Making an actual gap of course would create a black line, being the shadow between the two. Funny thing, even if those two blocks don’t overlap with nearby blocks but are only snapped there is no artifact, unless the rendering decides to create one after camera movement.
Screenshot Attached as Actual gap.png
(It’s too large to be shown properly here)
Now this is as much information as comes to my mind right now, note that I tried to turn of the Cast and Recieve shadows in the Mesh Renderer for the cubes, and while all shadows disappeared, the black line did not.
I hope someone will know why Unity tries to make me go insane, or will enlighten me about how stupid my mistake is.
Have a nice day,
Alex