c# - 使用 perlin 噪声计算孤岛误差
问题描述
我正在开发一个小项目,它是一个 100 x 100 六边形的网格。在下面的脚本中,我用柏林噪声绘制了我的六边形,但我想要孤岛的格式并没有消失。我会留下我的代码和 2 个示例,因为我的地图会保留以及我希望它如何保留。
我的岛
根据我的需要
int getColor(float x, float z)
{
xTO = (int)x / terrainWidth - 30;
zTO = (int)z / terrainHeight - 30;
float v = Mathf.PerlinNoise((xTO + x + seed) * freq, (zTO + z) * freq);
// v += 0.001f;
float form = formWorld(x, z);
if (v < 0.25f)
{
//water
return 0;
}
else if (v < 0.5f)
{
//sand
return 1;
}
else if (v < 0.75f)
{
//grass
return 2;
}
else
{
//Trees / Forest
MakeNewTree(new Vector3(xx, 0, z * 7.5f));
return 2;
}
}
解决方案
如果你想让你的图像看起来更像第二个,最好的选择是添加一个圆形渐变来抵消你的 Perlin Noise。
最简单的方法是测量到中心的距离并将其与柏林噪声相结合。
这是一些未经测试的代码。
int getColor(float x, float z)
{
xTO = (int)x / terrainWidth - 30;
zTO = (int)z / terrainHeight - 30;
float v = Mathf.PerlinNoise((xTO + x + seed) * freq, (zTO + z) * freq);
// v += 0.001f;
v -= CircleOffset(x,z)/2; //Change the two to make the island bigger.
float form = formWorld(x, z);
if (v < 0.25f)
{
//water
return 0;
}
else if (v < 0.5f)
{
//sand
return 1;
}
else if (v < 0.75f)
{
//grass
return 2;
}
else
{
//Trees / Forest
MakeNewTree(new Vector3(xx, 0, z * 7.5f));
return 2;
}
}
float CircleOffset(float x, float y)
{
Vector2 center = new Vector2(terrainWidth/2,terrainHeight/2);
float distance = Mathf.Sqrt((center.x - x)*(center.x - x) + (center.y - y) * (center.y - y));
return distance/terrainWidth;
}
希望这可以帮助!
推荐阅读
- python - 过度拟合问题,验证成功率远低于训练。我能改变什么?
- python - 整个 Python 列表未写入 CSV 文件
- google-cloud-platform - 如何部署具有两个网络接口的 Windows 2016 服务器实例
- android - 如何通过获取 SQLite 数据自动填充编辑文本?
- conemu - conemu - 在新窗口中启动多个命令
- java - Java方法覆盖和继承
- c# - Google Play Custom App Publishing API 在 C# 中发布私有应用
- abap - 如何更改 SAP ABAP 中自定义表的主键值?
- apache-spark - Apache spark + confluent 平台集成和写入 hdfs 文件
- azure - 将 Azure Function App 日志输出获取到 Log Analytics