I’m trying to change a plane mesh, created dynamically, vertex positions based on a heightmap.

Here’s my shader code:

```
v2f vert(appdata_base v)
{
v2f OUT;
v.vertex.y += tex2Dlod(_HeightMap, float4(TRANSFORM_TEX(v.texcoord, _HeightMap) * _Scale, 0.0, 0.0)) * _TerrainHeight;
OUT.pos = mul(UNITY_MATRIX_MVP, v.vertex);
OUT.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return OUT;
}
float4 frag(v2f IN) : COLOR
{
discard;
fixed4 texcol = tex2D (_MainTex, IN.uv);
return texcol * _Color;
}
```

_TerrainHeight is the actual height of the terrain (128 in this case) and _Scale should be the multiplier to compensate for the mesh division.

Currently I got it to work when using one big mesh that covers the terrain. It looks like this:

Now I’m trying to use more meshes to cover the same terrain. The meshes are smaller but contains more verts (better resolution). Now when I divide the big mesh in 4 I got this result:

_Scale = 4 in this case

Here my first mesh (right lower one) have the correct vertex displacement, but the other meshes are awfully wrong. If I divided it by 16 I got a very interesting result:

_Scale = 2 in this case

Here my first mesh (right lower one) have again the correct vertex displacement, and the ones next to it have a vertex displacement of the next position. Here’s the same meshes amount just with the next mesh moved to the left:

_Scale = 2 in this case

It seems like its skipping one mesh. Now the must interesting part is that if I divide the mesh by 64 I got a correct mesh deformation for all the 64 meshes if _Scale = 1

Can someone help me out on why my code is not working?

The problem wasn’t my shader, it was my mesh creation function. I forgot to scale the uv with the new scaled vert position. Here’s the function

```
/// <summary>
/// Makes a plane mesh
/// </summary>
/// <param name="size">Size of the grid mesh that wants to be created</param>
/// <param name="totalSize">Size of the grid</param>
/// <param name="posX">Local x position offest</param>
/// <param name="posY">Local z position offest</param>
/// <returns>Generated mesh</returns>
Mesh MakeMesh(int size, int totalSize, int posX, int posZ)
{
//Normals array
Vector3[] normals = new Vector3[size * size];
//Vertices array
Vector3[] verts = new Vector3[size * size];
//Texcoords array
Vector2[] texcoords = new Vector2[size * size];
//Triangle indices
int[] triangles = new int[size * size * 6];
//Scale of the current grid
float scale = ((float)gridSize / (size - 1));
//Counter for the triangles indices
int num = 0;
//Loop that iterates in x axis for the given grid size
for (int x = 0; x < size; x++)
{
//Loop that iterates in z axis for the given grid size
for (int z = 0; z < size; z++)
{
//New x coord that is affected by the scale
float X = x * scale;
//New z coord that is affected by the scale
float Z = z * scale;
//Normal direction, Vector3.up
Vector3 norm = new Vector3(0.0f, 1.0f, 0.0f);
//Vert position
Vector3 pos = new Vector3(X, 0.0f, Z);
//Vert uv
Vector2 uv = new Vector3((posX + X) / (totalSize - 1.0f), (posZ + Z) / (totalSize - 1.0f));
//Asign the normal to the array
normals[x + z * size] = norm;
//Asign the vert position to the array
verts[x + z * size] = pos;
//Asign the uv to the array
texcoords[x + z * size] = uv;
//Be sure that we are not in the last iteration of either of the loops
if (x < size - 1 && z < size - 1)
{
//Asign the corresponding indices to our triangles array
triangles[num++] = x + z * size;
triangles[num++] = x + (z + 1) * size;
triangles[num++] = (x + 1) + z * size;
triangles[num++] = x + (z + 1) * size;
triangles[num++] = (x + 1) + (z + 1) * size;
triangles[num++] = (x + 1) + z * size;
}
}
}
//Mesh to be returned
Mesh mesh = new Mesh();
//Asign normals array to the mesh
mesh.normals = normals;
//Asign verts position to the mesh
mesh.vertices = verts;
//Asign uv array to the mesh
mesh.uv = texcoords;
//Asign triangles array to the mesh
mesh.triangles = triangles;
//Return the created mesh
return mesh;
}
```

I eliminated the _Scale float in the shader code because its not needed.