Help! I am trying to create a procedurally generated terrain, and a procedurally generated texture and apply the texture to the terrain. I already have the terrain generation down, along with the texture generation. The issue I am having is whenever I apply the texture to the terrain, only the first pixel of the texture is showing up, but it is showing up on the entire terrain. I figured it has something to do with the UV map i’ve created for the terrain’s mesh. I’ve verified that it is only the first pixel of the texture, by writing the first pixel of the texture to magenta, and the whole terrain turns magenta. So I’m guessing that there is something wrong with the UVs and that the UV map is making a bunch of the same first pixel over and over again. I don’t know though.
I didn’t include the code dealing with the triangles for the created mesh, because the mesh works just fine. I’m able to have proper collisions and shading with the generated terrain. It just won’t have the WHOLE texture applied to it. The terrain image is generated based on the generateHeight() function, and also works just fine. I can view the generated picture in the inspector and it matches exactly what the terrain height looks like.
Any assistance would be greatly appreciated! I’ve been searching through google for days now. This is the first time I’ve asked a question here, so my apologies if I haven’t included everything you need, please let me know!
My terrain is x units long by z units deep. The texture is x by z pixels, so that one pixel should correspond to each vertex of the terrain. I understand that the UV map should go from 0-1 over the entire terrain, so that would be 0-1 divided by the number of vertices in the x and z, or in other words U/xUnits by V/zUnits.
Some snippets of code:
Vector2[] uvsArray; //Array to hold the UV coordinates
Vector3[] verticesArray; //Array to hold the terrain vertices
public int verticesX = 10; //Total width of the terrain
public int verticesZ = 10; //Total depth of the terrain
void CreateUVsArray()
{
verticesArray = new Vector3[(verticesX +1) * (verticesZ+1)];
uvsArray = new Vector2[(verticesX +1) * (verticesZ+1)];
//Start with setting the uvsArray size to the same size as the vertices array for the terrain
for (int i = 0, z = 0; z < verticesZ; z++)
{
for(int x = 0; x < verticesX; x++)
{
// Loop through and generate the verticesArray values, works just fine
verticesArray *= new Vector3(x, generateHeight(), z);*
// Create a UV Array with the points of the terrain mesh divided by the total vertices for each x and z
uvsArray = new Vector2(x / verticesX, z / verticesZ);
i++;
}
}
}
I update the created mesh in the following
void UpdateMesh()
{
terrainMesh.Clear();
terrainMesh.vertices = verticesArray;
terrainMesh.triangles = trianglesArray;
terrainMesh.uv = uvsArray;
terrainMesh.RecalculateNormals();
terrainTexture.Apply();
terrainTexture.wrapMode = TextureWrapMode.Clamp;
}