C#XNA Perlin Noise,Array超出范围

时间:2014-09-25 11:16:09

标签: c# xna indexoutofboundsexception perlin-noise

因此,在研究了这个:Wikipedia on Perlin Noise之后,我尝试做一个生成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;
    }

}
}

那么,为了解决这个问题,我可以修改哪些内容?

提前致谢。

1 个答案:

答案 0 :(得分:0)

n1,您通过x1x。如果x为零,则x0为-1。 然后,您在方法DotGridGradient中设置dxx0 - x,在这种情况下为-1 - 0。 同样适用于y。 那是错误导致的。 如果你在for循环中从1开始生成它一切都很好。我对Perlin Noises一无所知,所以我无法帮助你。