我想生成一个代码,该代码生成一个地形,该地形的中间是平坦区域,周围是丘陵。问题是我找不到平滑解决这两个区域的解决方案。我可以在这里进行平滑穿越的区域,但是我不知道如何进行。实际上看起来像这样:Terrain中间区域和周围区域之间的间隙应该是平滑的交叉点。
public class TerrainGenerator : MonoBehaviour
{
public InputField platformWidthInput, platformLengthInput, terrainWidthInput, terrainLengthInput;
public Terrain terrain;
public float scale, height;
private int platformWidth, platformWidthHalf, platformLength, platformLengthHalf;
private int terrainWidth, terrainWidthHalf, terrainLength, terrainLengthHalf;
public void Generate()
{
platformWidth = int.Parse(platformWidthInput.text);
platformWidthHalf = platformWidth / 2;
platformLength = int.Parse(platformLengthInput.text);
platformLengthHalf = platformLength / 2;
terrainWidth = int.Parse(terrainWidthInput.text);
terrainWidthHalf = terrainWidth / 2;
terrainLength = int.Parse(terrainLengthInput.text);
terrainLengthHalf = terrainLength / 2;
var terrainInstance = InstantiateTerrain(terrain, new Vector3(-terrainWidthHalf, 0, -terrainLengthHalf), "Terrain");
TerrainData(terrainInstance.terrainData);
}
private static Terrain InstantiateTerrain(Terrain terrain, Vector3 position, string name)
{
var instance = Instantiate(terrain, position, Quaternion.identity);
instance.name = name;
return instance;
}
private void TerrainData(TerrainData terrainData)
{
var platformPercentWidth = (platformWidth / 100) * 10;
var platformPercentLength = (platformLength / 100) * 10;
terrainData.size = new Vector3(terrainWidth, height, terrainLength);
terrainData.heightmapResolution = terrainWidth + terrainLength + 1;
var resScaleX = (float) terrainWidth / terrainData.heightmapResolution;
var resScaleY = (float) terrainLength / terrainData.heightmapResolution;
var xRes = terrainData.heightmapWidth;
var yRes = terrainData.heightmapHeight;
var heights = terrainData.GetHeights(0, 0, xRes, yRes);
for (var x = 0; x < xRes; x++)
{
for (var y = 0; y < yRes; y++)
{
if (x >= (terrainWidthHalf - platformWidthHalf) / resScaleX && x <= (terrainWidthHalf + platformWidthHalf) / resScaleX && y >= (terrainLengthHalf - platformLengthHalf) / resScaleY && y <= (terrainLengthHalf + platformLengthHalf) / resScaleY)
{
heights[x, y] = 1;
}
else if (x >= (terrainWidthHalf - platformWidthHalf - platformPercentWidth) / resScaleX && x <= (terrainWidthHalf + platformWidthHalf + platformPercentWidth) / resScaleX && y >= (terrainLengthHalf - platformLengthHalf - platformPercentLength) / resScaleY && y <= (terrainLengthHalf + platformLengthHalf + platformPercentLength) / resScaleY)
{
heights[x, y] = 0.5f;
//Smooth Area
}
else
{
heights[x, y] = Mathf.PerlinNoise((float) x / xRes * scale, (float) y / yRes * scale);
}
}
}
terrainData.SetHeights(0, 0, heights);
}
}