Итак, насколько я понимаю ваш вопрос, вы хотите преобразовать высоту местности в me sh для определенной части местности.
Вы можете использовать Terrain.SampleHeight
чтобы получить высоту ландшафта в заданной точке (x, y)
Я думаю, вам нужно решить, насколько детальным / зернистым должен быть ваш результат sh для более или менее детализации ландшафта.
Затем сгенерируйте растр сетки вершин и получите высоты ландшафта. Примерно так
/// <summary>
/// Takes a section of a terrain between the diagonal startPoint and endPoint
/// and returns a mesh with given vertexCount and shape of terrain heights
/// <summary>
/// <param name="terrain">the given terrain to sample from</param>
/// <param name="startPoint">Start world position of the wanted section</param>
/// <param name="endPoint">End world position of the given section</param>
/// <param name="vertexCount">How many vertices shall be used for the mesh (=granularity)</param>
public static Mesh TerrainSectionToMesh(Terrain terrain, Vector3 startPoint, Vector3 endPoint, Vector2 vertexCount)
{
// First get the step sizes according to vertexCount
var sectionSize = endPoint - startPoint;
var stepX = sectionSize / vertexCount.x;
var stepZ = sectionSize / vertexcount.z;
// iterate the size in X and Z direction to get the target vertices
// This crates a vertex grid with given terrain heights on according positions
var vertices = new Vector3[vertexCount.x * vertexCount.y];
var uv = new Vector2(vertices.Length);
var tangents = new Vector4[vertices.Length];
var tangent = new Vector4(1f, 0f, 0f, -1f);
for(var i = 0, z = 0; z < vertexCount.y; z++)
{
for(var x = 0; x < vertexCount.x; x++, i++)
{
var position = startPoint + Vector3.forward * stepZ * z + Vector3.right * stepX * x;
position.y = terrain.SampleHeight(position);
vertices[i] = position;
uv[i] = new Vector2((float)x / vertexCount.x, (float)y / vertexCount.y);
tangents[i] = tangent;
}
}
// Procedural grid generation taken from https://catlikecoding.com/unity/tutorials/procedural-grid
// This generates the triangles for the given vertex grid
int[] triangles = new int[vertices.Length * 6];
for (int ti = 0, vi = 0, y = 0; y < vertexCount.y; y++, vi++)
{
for (int x = 0; x < vertexCount.x; x++, ti += 6, vi++)
{
triangles[ti] = vi;
triangles[ti + 3] = triangles[ti + 2] = vi + 1;
triangles[ti + 4] = triangles[ti + 1] = vi + vertexCount.x + 1;
triangles[ti + 5] = vi + vertexCount.x + 2;
}
}
// Finally create the mesh and fill in the data
var mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uvs = uvs;
mesh.tangents = tangents;
mesh.RecalculateNormals();
return mesh;
}
Процедурная генерация сетки взята из https://catlikecoding.com/unity/tutorials/procedural-grid
Обратите внимание, что я набираю это на смартфоне и не могу протестируйте прямо сейчас. Но я надеюсь, что идея прояснится