我有一个 Perlin 噪声发生器,我试图用它来创建滚动地形。
我有一个中心地形,围绕它的同一侧的八个地形(这些都工作正常)和第二个地形环,它的三倍大。对于这些,我需要我的噪声发生器输出“密集”三倍的噪声,这样我就可以将更大的地形放在更小的地形上,并且除了三倍大的图块(和地图大小)之外,它们会匹配.
我不想只生成更大的地图;这很快就会变得令人望而却步。
我不知道该怎么办; 显而易见的解决方案都以某种方式失败了。
/// <summary>
/// Generates a new perlin map.
/// </summary>
/// <param name="xStart">The left coordinate (using 0, 0 as top-left and +, + as down and to the right).</param>
/// <param name="yStart">The top coordinate (using 0, 0 as top-left and +, + as down and to the right).</param>
/// <param name="width">The width of the map.</param>
/// <param name="length">The length of the map.</param>
/// <param name="persistance">If set, values lower than 1 make the map less noisey; values greater than 1 make the map more noisy.</param>
/// <param name="fromShift">Low values here provide for a broader "base".</param>
/// <param name="toShift">High values here provide for more speckled "highlights".</param>
/// <param name="interpolate">If set to false, the algorithm will not smooth values.</param>
public double[,] Generate(
int xStart, int yStart,
int width, int length,
double? persistance,
uint fromShift, uint toShift,
bool interpolate = true,
)
{
_noiseMap = new double[width, length];
_workingMap = new double[width + 6, length + 6];
_smoothedNoise = new double[width + 6, length + 6];
int ifromShift = -(int)(toShift),
itoShift = -(int)(fromShift);
int idiv = 1 + (itoShift - ifromShift);
double ddiv = 0;
double amplitude = 0.0;
if (persistance.HasValue)
for (int i = ifromShift; i <= itoShift; ++i)
ddiv += Math.Pow(persistance.Value, i);
for (int i = ifromShift; i <= itoShift; ++i)
{
_frequency = Math.Pow(2, i);
if (persistance.HasValue) amplitude = Math.Pow(persistance.Value, i);
int useWidth = (int)(width * _frequency) + 1,
useLength = (int)(length * _frequency) + 1;
int useXStart = (int)(xStart * _frequency),
useYStart = (int)(yStart * _frequency);
double frequencyXStart = xStart * _frequency - useXStart,
frequencyYStart = yStart * _frequency - useYStart;
for (int y = 0; y < useLength + 5; ++y)
for (int x = 0; x < useWidth + 5; ++x)
{
int genX = ((int)(useXStart) + (int)((x) + 0.5));
int genY = ((int)(useYStart) + (int)((y) + 0.5));
_workingMap[x, y] = GenerateNoise(genX, genY);
}
if (interpolate)
{
for (int y = 1; y < length + 4; ++y)
for (int x = 1; x < width + 4; ++x)
{
_smoothedNoise[x, y] = SmoothedNoise(x, y);
}
if (persistance.HasValue)
for (int y = 0; y < length; ++y)
for (int x = 0; x < width; ++x)
{
_noiseMap[x, y] += InterpolatedNoise((x * _frequency) + 2 + frequencyXStart, (y * _frequency) + 2 + frequencyYStart) * amplitude;
// _noiseMap[x, y] += _workingMap[x, y] * amplitude;
}
else
for (int y = 0; y < length; ++y)
for (int x = 0; x < width; ++x)
{
_noiseMap[x, y] += InterpolatedNoise((x * _frequency) + 2 + frequencyXStart, (y * _frequency) + 2 + frequencyYStart) / idiv;
// _noiseMap[x, y] += _workingMap[x, y] / idiv;
}
}
else
if (persistance.HasValue)
for (int y = 0; y < length; ++y)
for (int x = 0; x < width; ++x)
{
_noiseMap[x, y] +=
_workingMap[(int)((x * _frequency) + 2 + frequencyXStart), (int)((y * _frequency) + 2 + frequencyYStart)] * amplitude;
}
else
for (int y = 0; y < length; ++y)
for (int x = 0; x < width; ++x)
{
_noiseMap[x, y] +=
_workingMap[(int)((x * _frequency) + 2 + frequencyXStart), (int)((y * _frequency) + 2 + frequencyYStart)] / idiv;
}
}
if (persistance.HasValue)
for (int y = 0; y < length; ++y)
for (int x = 0; x < width; ++x)
{
_noiseMap[x, y] = _noiseMap[x, y] / ddiv;
}
return _noiseMap;
}
谢谢。