C#XNA Perlin噪声,数组越界

本文关键字:数组 越界 噪声 Perlin C#XNA | 更新日期: 2023-09-27 18:24:40

因此,在研究了这个:关于Perlin噪波的维基百科之后,我试图创建一个生成Perlin噪波纹理(输出为Color[,],稍后将转换为纹理)的类。然而,一旦我让它跑起来,在所有的跑中,我都会得到一个界外异常。这是课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Noise
{
class PerlinNoise : Noise
{
    private float[,,] gradient;
    private float[,] noise;
    private bool generated;
    private int width;
    private int height;
    /// <summary>
    /// Class constructor.
    /// </summary>
    /// <param name="x">Width of the perlin noise</param>
    /// <param name="y">Height of the perlin noise</param>
    public PerlinNoise(int width, int height)
    {
        gradient = new float[width,height,2];
        Random r = new Random();
        for (int i = 0; i < width; i++)
        {
            for (int o = 0; o < height; o++)
            {
                for (int p = 0; p < 2; p++)
                {
                    gradient[i, o, p] = ((float)r.NextDouble() * 2f) -1;
                }
            }
        }
        this.width = width;
        this.height = height;
        noise = new float[width, height];
        generated = false;

    }

    float Lerp(float a0, float a1, float w)
    {
        return (1.0f - w) * a0 + w * a1;
    }
    float DotGridGradient(int ix, int iy, float x, float y)
    {
        float dx = x - (float)ix;
        float dy = y - (float)iy;

        return (dx * gradient[iy, ix, 0]) + (dy * gradient[iy, ix, 1]); //It blows up here, usually with either iy or ix = -1
    }
    public float GenerateValue(float x, float y)
    {
        int x0 = x > 0.0f ? (int)x : (int)x - 1;
        int x1 = x0 + 1;
        int y0 = y > 0.0f ? (int)y : (int)y - 1;
        int y1 = y0 + 1;
        float sx = x - (float)x0;
        float sy = y - (float)y0;
        float n0, n1, ix0, ix1, value;
        n0 = DotGridGradient(x0, y0, x, y);
        n1 = DotGridGradient(x1, y0, x, y);
        ix0 = Lerp(n0, n1, sx);
        n0 = DotGridGradient(x0, y1, x, y);
        n1 = DotGridGradient(x1, y1, x, y);
        ix1 = Lerp(n0, n1, sx);
        value = Lerp(ix0, ix1, sy);
        return value;
    }
    public Color GenerateColor(int x, int y)
    {
        if (!generated) GenerateNoise();
        Color c = new Color();
        c.R = c.G = c.B = (byte)(256 * noise[x,y]);
        return c;
    }
    public Color[,] GenerateTexture()
    {
        if (!generated) GenerateNoise();
        Color[,] color = new Color[width,height];
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                color[x, y] = GenerateColor(x, y);
            }

        }
        return color;
    }
    public void GenerateNoise()
    {
        for (int x =0; x < width; x++)
            for (int y = 0; y < height; y++)
                noise[x, y] = GenerateValue(x, y);

        generated = true;
    }
}
}

那么,我可以修改什么来解决这个问题呢?

提前谢谢。

C#XNA Perlin噪声,数组越界

n1处,您可以传递x1x。如果CCD_ 4为零,则CCD_。然后在方法DotGridGradient中,将dx设置为x0-x,在本例中为-1.0。y也是如此。这就是错误的原因。如果在for循环中从1开始生成它,那么一切都很好。我对黑佩林一无所知,所以我不能再帮你了。