1

我有一个 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;
    }

谢谢。

4

2 回答 2

0

Perlin 噪声比您的样本密集得多,因此一种算法的形状对于所有网格都是密集的。如果你想要额外的细节,你必须添加一个八度音阶或第二次噪音,在这种情况下,网格不会正确覆盖。所以这是一个选择形状并以不同网格密度对其进行采样的问题。

于 2014-03-05T09:40:51.793 回答
0

只是为了清楚(检查我对您的问题的理解)。

您生成了一个边为 X 的地图 A。该地图被另外九个地图 B1.​​..B9 包围,每个地图的边为 X。现在您需要地图 C1...C2,它将包围地图 B1.​​.. B9 从外面看。这些地图中的每一个都有一个大小为 3X 的边,但只有 X^2 个数据点(每个数据点的大小为 3x3)。

正确的?

您是否尝试过使用最近邻算法对数据进行插值?当任何代码从坐标 X 请求数据时,只需将坐标四舍五入为三的倍数并使用它。我认为这应该有效。

另一种选择是使用中点位移算法,除非您将瓷砖分成三个部分,而不是在每个递归步骤中只分成两个部分。这将允许您提前停止一轮,这将为您提供您正在寻找的结果:高度图,其数据点仅在 3x3 网格的交点上生成。无需触及其余点。

于 2011-03-16T15:29:40.117 回答