【问题标题】:Version of Improved Noise keeps returning 0改进噪声的版本一直返回 0
【发布时间】:2011-09-29 06:24:02
【问题描述】:

我正在尝试在我的 XNA 游戏中实现改进的噪波,但我的改进噪波函数一直返回 0.0f。它与 Ken Perlin (http://mrl.nyu.edu/~perlin/noise/) 的代码完全相同,只是移植到 C#。

我尝试过重写该类,甚至直接从站点复制和粘贴(当然,然后移植到 C#),但它除了 0 之外不会输出任何值。

这是我正在使用的代码:

public class PerlinNoise 
    { 
        private int[] permutations = new int[512]; 

    private Random random; 

    public PerlinNoise() 
        : this(Environment.TickCount) 
    { } 

    public PerlinNoise(int seed) 
    { 
        random = new Random(seed); 

        for (int i = 0; i < 256; i++) 
        { 
            permutations[i] = i; 
        } 

        for (int i = 0; i < 256; i++) 
        { 
            int k = random.Next(256 - i) + i; 

            int l = permutations[i]; 

            permutations[i] = permutations[k]; 
            permutations[k] = l; 
            permutations[i + 256] = permutations[i]; 
        } 
    } 

    private int fastfloor(float x) 
    { 
        return x > 0 ? (int)x : (int)x - 1; 
    } 

    private float fade(float t) 
    { 
        return t * t * t * (t * (t * 6 - 15) + 10); 
    } 

    private float lerp(float t, float a, float b) 
    { 
        return a + t * (b - a); 
    } 

    public float grad(int hash, float x, float y, float z) 
    { 
        int h = hash & 15; 

        float u = h < 8 ? x : y, 
            v = h < 4 ? y : h == 12 || h == 14 ? x : z; 

        return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); 
    } 

    public float noise3d(float x, float y, float z) 
    { 
        int X = fastfloor(x) & 0xff, 
            Y = fastfloor(y) & 0xff, 
            Z = fastfloor(z) & 0xff; 

        x -= fastfloor(x); 
        y -= fastfloor(y); 
        z -= fastfloor(z); 

        float u = fade(x); 
        float v = fade(y); 
        float w = fade(z); 

        int A = permutations[X] + Y, AA = permutations[A] + Z, AB = permutations[A + 1] + Z, 
            B = permutations[X + 1] + Y, BA = permutations[B] + Z, BB = permutations[B + 1] + Z; 

        return lerp(w, lerp(v, lerp(u, grad(permutations[AA], x, y, z), 
                                 grad(permutations[BA], x - 1, y, z)), 
                         lerp(u, grad(permutations[AB], x, y - 1, z), 
                                 grad(permutations[BB], x - 1, y - 1, z))), 

                         lerp(v, lerp(u, grad(permutations[AA + 1], x, y, z - 1), 
                                 grad(permutations[BA + 1], x - 1, y, z - 1)), 
                         lerp(u, grad(permutations[AB + 1], x, y - 1, z - 1), 
                                 grad(permutations[BB + 1], x - 1, y - 1, z - 1)))); 
    } 

    public float noise2d(float x, float y) 
    { 
        return noise3d(x, y, 0f); 
    } 
} `

为了测试它,我只是做了:

string[] args = Console.ReadLine().Split(' '); 

PerlinNoise noise = new PerlinNoise(); 

int x = args[0]; 
int y = args[1]; 
int z = args[2]; 

Console.WriteLine(noise.noise3d(x, y, z));

正如我上面所说,它总是输出 0。

【问题讨论】:

    标签: c# random xna noise perlin-noise


    【解决方案1】:

    如果所有参数都是整数,它似乎输出0.0f。将您的测试代码更改为

    var input = Console.ReadLine()
                        .Split(' ')
                        .Select(s => float.Parse(s,
                            System.Globalization.CultureInfo.InvariantCulture))
                        .ToArray();
    

    并尝试输入,例如,4234.2123 3123.12312 423.2434

    我不太确定这是否是所需的行为,但是

            x -= Math.Floor(x);                                // FIND RELATIVE X,Y,Z
            y -= Math.Floor(y);                                // OF POINT IN CUBE.
            z -= Math.Floor(z);
    

    如果xyz 是整数,则它们始终为0; fade(0.0f) 也始终为零。

    【讨论】:

    • 不幸的是,它仍然会返回 0,除非值是小数或非常极端。因为我只会将 Perlin Noise 用于积分坐标,所以这对我来说并不完全有效。
    【解决方案2】:

    在您的输入情况下将您的输入乘以 (1 / MAX_VALUE),只需乘以 1 / 256 左右,并且永远不要给它更大的值。在您的游戏中使用时,将输入乘以 (1 / MAXIMUM_CHOORD_VALUE)

    【讨论】:

      猜你喜欢
      • 2015-04-06
      • 2020-07-28
      • 2014-08-18
      • 2016-08-17
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      相关资源
      最近更新 更多