I’m trying to generate a mesh from a heightmap, this works fine “despite the heightmap being different from the biome map, also working on it…”.
The problem seams to be along the width “x axis” where it misses like a quarter of the mesh and starts overlapping also upside down.
The function is called on start only and honestly i can’t figure this one out, i’ve been comparing with other posts and for some reason have been having the same undesired result.
Assuming the width and height is 300 by 300 here’s the code and some pictures:



public int width = 300;
public int height = 300;
public Texture2D rawPerlinNoiseTex;
void Start()
{
// initialize a new texture if a noise generation code is used
//rawPerlinNoiseTex = new Texture2D(width, height);
GenerateMesh();
}
void GenerateMesh()
{
GameObject meshObject = GameObject.Find("Mesh");
MeshFilter meshFilter = null;
MeshRenderer meshRenderer = null;
Mesh mesh = null;
if (meshObject == null) {
meshObject = new GameObject("Mesh");
meshFilter = meshObject.AddComponent<MeshFilter>();
meshRenderer = meshObject.AddComponent<MeshRenderer>();
mesh = new Mesh();
meshFilter.mesh = mesh;
} else {
meshFilter = meshObject.GetComponent<MeshFilter>();
meshRenderer = meshObject.GetComponent<MeshRenderer>();
mesh = meshFilter.mesh;
mesh.Clear();
}
Vector3[] verts = new Vector3[width * height];
Vector2[] uvs = new Vector2[width * height];
int[] tris = new int[width * height * 6];
// vertices and uvs
int vertIndex = 0;
for (int x = 0; x < width - 1; x++) {
for (int y = 0; y < height - 1; y++)
{
// vertices
verts[vertIndex] = new Vector3(x, rawPerlinNoiseTex.GetPixelBilinear((float)x / width, (float)y / height).grayscale * 100, y);
// uvs
uvs[vertIndex] = new Vector2(x / (float)width, y / (float)height);
vertIndex++;
}
}
// triangles
int triIndex = 0;
for (int x = 0; x < width - 1; x++) {
for (int y = 0; y < height - 1; y++)
{
// current vertex
int index = (x * height) + y;
int indexDown = ((x + 1) * height) + y;
// first triangle of the quad
tris[triIndex] = index;
tris[triIndex + 1] = index + 1;
tris[triIndex + 2] = indexDown;
// second triangle of the quad
tris[triIndex + 3] = indexDown;
tris[triIndex + 4] = index + 1;
tris[triIndex + 5] = indexDown + 1;
triIndex += 6;
}
}
mesh.vertices = verts;
mesh.triangles = tris;
mesh.uv = uvs;
mesh.RecalculateNormals();
meshRenderer.material.mainTexture = texture;
}