加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 改进的噪声版本保持返回0

发布时间:2020-12-15 17:18:19 所属栏目:百科 来源:网络整理
导读:我正试图在我的XNA游戏中实现改进的噪音,但我的改进的噪音功能保持返回0.0f.它与Ken Perlin( http://mrl.nyu.edu/~perlin/noise/)的代码完全相同,只是移植到C#. 我已经尝试重写类,甚至直接从站点复制和粘贴(然后移植到C#,当然),但它不会输出任何值,而是0. 这
我正试图在我的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 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,z)),grad(permutations[AB],y - 1,grad(permutations[BB],z))),grad(permutations[AA + 1],z - 1),grad(permutations[BA + 1],z - 1)),grad(permutations[AB + 1],grad(permutations[BB + 1],z - 1)))); 
    } 

    public float noise2d(float x,float y) 
    { 
        return noise3d(x,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,z));

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

解决方法

如果所有参数都是整数,它似乎输出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);

将永远制作x,y&如果它们是整数,则z = 0;淡入淡出(0.0f)也始终为零.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读