Unity 5 uv bug?

EDIT: Don’t blame me for the bad coding, it’s just for testing purposes :stuck_out_tongue:

So I’ve tried to solve this on my own but I’ve had no luck so far.
And since no one else seems to have this issue, I doubt it’s a bug.

What I’m simply doing is build a mesh from script and apply a texture to it. I’ve done that many times before.

The problem now is that I have always 6 triangles in my mesh that are completely black.
And they are always at world position 0,0,0.

Even if I move my mesh, the black triangles stay at 0,0,0.

The meshes origin is always at 0,0,0. I just change the the vertices position to move the mesh.

I’ve tried a custom and the standard shader. Same effect on both.


If someone wants to see the script. The relevant code should be in BuildMesh().

using UnityEngine;
using System.Collections.Generic;

public class TerrainLayer : MonoBehaviour {

	public static int TERRAIN_WIDTH = 16;
	public static int TERRAIN_HEIGHT = 9;

	public Vector3 position;

	private byte[] terrainData;
	private Mesh mesh;

	public bool rebuildMesh;

	private float textureUnit = 0.0625f;
	private float textureMargin = 0.0001f;
	private List<Vector3> vertices;
	private List<int> triangles;
	private List<Vector2> uvs;

	// public

	void Awake() {

		terrainData = new byte[TERRAIN_WIDTH * TERRAIN_HEIGHT];
		vertices = new List<Vector3>();
		triangles = new List<int>();
		uvs = new List<Vector2>();

	}

	void Start() {

		mesh = GetComponent<MeshFilter>().mesh;
	}

	void Update() {

		if(rebuildMesh) {

			BuildMesh();

			rebuildMesh = false;
		}

	}

	#region Getter and Setter

	public void setRebuildFlag() {
		rebuildMesh = true;
	}

	public void setTerrainData(int x, int y, byte id) {

	}

	public byte getTerrainData(int x, int y) {


		return 0;
	}

	#endregion

	// private

	private void BuildMesh() {

		vertices.Clear();
		triangles.Clear();
		uvs.Clear();

		int quadCount = 0;

		Vector2 texture = new Vector2(0, 30);

		for(int x = (int)position.x - (TERRAIN_WIDTH / 2); x < position.x + (TERRAIN_WIDTH / 2); x++) {
			for(int y = (int)position.y - (TERRAIN_HEIGHT / 2); y < position.y + (TERRAIN_HEIGHT / 2); y++) {

				vertices.Add(new Vector3(x, y + 1));
				vertices.Add(new Vector3(x + 1, y + 1));
				vertices.Add(new Vector3(x + 1, y));
				vertices.Add(new Vector3(x, y));

				triangles.Add(quadCount * 4);
				triangles.Add((quadCount * 4) + 1);
				triangles.Add((quadCount * 4) + 3);
				triangles.Add((quadCount * 4) + 1);
				triangles.Add((quadCount * 4) + 2);
				triangles.Add((quadCount * 4) + 3);

				uvs.Add(new Vector2(textureUnit * texture.x + textureMargin, textureUnit * texture.y + textureUnit - textureMargin));
				uvs.Add(new Vector2(textureUnit * texture.x + textureUnit - textureMargin, textureUnit * texture.y + textureUnit - textureMargin));
				uvs.Add(new Vector2(textureUnit * texture.x + textureUnit - textureMargin, textureUnit * texture.y + textureMargin));
				uvs.Add(new Vector2(textureUnit * texture.x + textureMargin, textureUnit * texture.y + textureMargin));

				quadCount++;
			}
		}

		mesh.vertices = vertices.ToArray();
		mesh.triangles = triangles.ToArray();
		mesh.uv = uvs.ToArray();
	}
}

I kind of feel stupid now. I forgot to recalculate the normals of the mesh.