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.
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-