using UnityEngine;
public class TerrainTileGenerator : MonoBehaviour
{
public int tile_x = 2;
public int tile_y = 2;
private const int totalSize = 256; // Assumo che il terreno abbia una dimensione totale di 256x256
private int tileSize;
private float[,] heightMap;
void Start()
{
tileSize = totalSize / Mathf.Max(tile_x, tile_y);
heightMap = GenerateTerrain(tile_x, tile_y);
}
private float[,] GenerateTerrain(int tilesX, int tilesY)
{
float[,] heightData = new float[totalSize, totalSize];
for (int tileIndexX = 0; tileIndexX < tilesX; ++tileIndexX)
{
for (int tileIndexY = 0; tileIndexY < tilesY; ++tileIndexY)
{
float[,] tileHeightMap = GenerateTileHeightMap();
for (int x = 0; x < tileSize; ++x)
{
for (int y = 0; y < tileSize; ++y)
{
heightData[tileIndexX * tileSize + x, tileIndexY * tileSize + y] =
tileHeightMap[x, y];
}
}
}
}
return heightData;
}
private float[,] GenerateTileHeightMap()
{
float[,] tileHeightMap = new float[tileSize, tileSize];
float noiseScale = 0.1f; // Parametro per controllare la "rugosità" del terreno
for (int x = 0; x < tileSize; ++x)
{
for (int y = 0; y < tileSize; ++y)
{
float xCoord = (float)x / tileSize * noiseScale;
float yCoord = (float)y / tileSize * noiseScale;
tileHeightMap[x, y] = Mathf.PerlinNoise(xCoord, yCoord);
}
}
return tileHeightMap;
}
public float[,] GetHeightMap()
{
return heightMap;
}
}
Questo script deve essere aggiunto a un GameObject in una scena Unity. L'output (heightMap) viene generato quando la scena viene avviata (Start()), e può essere recuperato chiamando il metodo GetHeightMap() dell'istanza di TerrainTileGenerator. Ho assunto che il terreno abbia dimensioni di 256x256, ma puoi cambiare il valore di totalSize per adattarlo alle tue esigenze. Buon divertimento!