2

我正在尝试为 gdscript(Godot 引擎脚本)编写一个 perlin 噪声函数,但它似乎不起作用,我不知道我哪里出错了,无论是在排列表创建还是 perlin 噪声函数实现. 我需要一些帮助来了解出了什么问题。

我学习编写代码的地方:

http://www.dreamincode.net/forums/topic/66480-perlin-noise/ https://github.com/gustavoteixeira/tcc/tree/master/heightmap_generators

var _seed = 0
const _PI = 3.141592653589793
const MAX_COLUMN = 16
const MAX_LINE = 16
var rd_multiarray = []

var gradient = []
var permutation = []

func _ready():
    #Function that uses the perlin noise () function to generate a two-
    #dimensional array



#---------PSEUDO-RANDOM NUMBER GENERATOR---------------------------------
func simple_noise2d(var x, var y):
    var n=int(x)+int(y)*57
    n=(n<<13)^n
    var nn=(n*(n*n*60493+19990303)+1376312589)&0x7fffffff
    return 1.0-(float(nn/1073741824.0))

#---------INTERPOLATION--------------------------------------------------
func interpolate(var a,var b,var x):
    var ft=x * 3.1415927
    var f=(1.0-cos(ft))* 0.5
    return a*(1.0-f)+b*f

#---------NOISE----------------------------------------------------- 
func generate_permutation():
    for i in range(256):
        permutation.append(randf()*256)

func generateGradients():
    for i in range(4):
        gradient.append(0)
    var point1 = Vector2()
    point1.x = 1 
    point1.y = 0 
    gradient[0] = point1
    var point2 = Vector2()
    point2.x = 0 
    point2.y = 1
    gradient[1] = point2
    var point3 = Vector2()
    point3.x = -1
    point3.y =  0
    gradient[2] = point3
    var point4 = Vector2()
    point4.x = 0
    point4.y = -1
    gradient[3] = point4

func dotproduct(var v1, var x2, var y2):
    return float((v1.x * x2) + (v1.y * y2))

func fade(var t):
    return float(t*t*t*(t*(t*6-15)+10))

func perlin_noise(var x, var y):
    var x_int = int(floor(x))
    var y_int = int(floor(y))
    var grid_x = float(x - x_int)
    var grid_y = float(y - y_int)
    var x_fade = fade(grid_x)
    var y_fade = fade(grid_y)

    var gradient1 = gradient[ int(permutation[int(x_int + permutation[int(y_int % 256)]) % 256]) % 4]
    var gradient2 = gradient[ int(permutation[int((x_int+1) + permutation[int(y_int % 256)]) % 256]) % 4]
    var gradient3 = gradient[ int(permutation[int(x_int + permutation[int((y_int+1) % 256)]) % 256]) % 4]
    var gradient4 = gradient[ int(permutation[int((x_int+1) + permutation[int((y_int+1) % 256)]) % 256]) % 4]

    var dp1 = float(dotproduct(gradient1, grid_x, grid_y))
    var dp2 = float(dotproduct(gradient2, grid_x - 1, grid_y))
    var dp3 = float(dotproduct(gradient3, grid_x, grid_y - 1))
    var dp4 = float(dotproduct(gradient4, grid_x - 1, grid_y - 1))

    var x1lerp = float(cos_interpolate(dp1, dp2, x_fade))
    var x2lerp = float(cos_interpolate(dp3, dp4, x_fade))
    var final_value = float(cos_interpolate(x1lerp, x2lerp, y_fade))
    return final_value

当我在另一个负责为我生成数组的函数中使用 perlin_noise () 函数时,当我将它绘制在屏幕上以更好地可视化时,我得到的东西很像白噪声,问题不在于生成世界的函数,与已经实现了 perlin 噪声功能的模块相同,并且它有效,但我的没有。

4

1 回答 1

3

我到了这里,这个页面对我帮助很大,现在我明白会发生什么:

http://www.angelcode.com/dev/perlin/perlin.html

我还在 GDScript 中构建了一个类来生成一些噪音,它遵循 git 链接:

https://github.com/PerduGames/SoftNoise-GDScript-

func generateGradients():
    for i in range(256):
        p.append(i)
    for i in range(256):
        var j = randi() % 256
        var nSwap = p[i]
        p[i]  = p[j]
        p[j]  = nSwap

    for i in range(256):
        gx.append(float(randf())/(32767/2) - 1.0)
        gy.append(float(randf())/(32767/2) - 1.0)


func perlin_noise2d(var x, var y):
    var qx0 = int(floor(x))
    var qx1 = qx0 + 1
    var qy0 = int(floor(y))
    var qy1 = qy0 + 1

    var q00 = int(p[(qy0 + p[qx0 % 256]) % 256])
    var q01 = int(p[(qy0 + p[qx1 % 256]) % 256])
    var q10 = int(p[(qy1 + p[qx0 % 256]) % 256])
    var q11 = int(p[(qy1 + p[qx1 % 256]) % 256])

    var tx0 = x - floor(x)
    var tx1 = tx0 - 1
    var ty0 = y - floor(y)
    var ty1 = ty0 - 1

    var v00 = gx[q00]*tx0 + gy[q00]*ty0
    var v01 = gx[q01]*tx1 + gy[q01]*ty0
    var v10 = gx[q10]*tx0 + gy[q10]*ty1
    var v11 = gx[q11]*tx1 + gy[q11]*ty1

    var wx = (3 - 2*tx0)*tx0*tx0
    var v0 = v00 - wx*(v00 - v01)
    var v1 = v10 - wx*(v10 - v11)

    var wy = (3 - 2*ty0)*ty0*ty0
    var v = v0 - wy*(v0 - v1)
    return v
于 2017-09-06T02:41:38.077 回答