Perlin noise gdscript error

288 views Asked by At

I'm trying to program a perlin noise function for gdscript (Godot Engine script) but it does not seem to be working, I do not know where I'm going wrong, whether it's in the permutation table creation or the perlin noise function implementation. I need some help to understand what's going wrong.

Where I studied to make the code:

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

When I use the perlin_noise () function in another function responsible for generating an array for me, when I paint it on the screen to better visualize, I get something much like a white noise, the problem is not in the function that generates the world, the same with a module that has a perlin noise function already implemented, and it worked, but mine does not.

1

There are 1 answers

0
PerduGames On

I got here, this page helped me a lot, now I understand what happens:

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

I also built a class in GDScript to generate some noises, it follows the git link:

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