I was experiencing with my Perlin noise function and I tried to animate a 2D noise by changing the z value of a small amount every frame. However, as z increases, the result becomes stranger and stranger, looking like this:
Why is this happening? How could I solve this?
Here's the Lua code that could be wrong (as asked):
function perlin.fade(t)
return t * t * t * (t * (6 * t - 15) + 10)
end
function perlin.lerp(t, a, b)
return a + t * (b - a)
end
function perlin.grad(hash, x, y, z)
local h = hash % 16
local u
local v
if (h < 8) then u = x else u = y end
if (h < 4) then v = y elseif (h == 12 or h == 14) then v = x else v = z end
local r
if ((h % 2) == 0) then r = u else r = -u end
if ((h % 4) == 0) then r = r + v else r = r -v end
return r
end
function perlin:noise(x, y, z)
y, z = y or 0, z or 0
local X = math.floor(x) % 256
local Y = math.floor(y) % 256
local Z = math.floor(z) % 256
x = x - math.floor(x)
y = y - math.floor(y)
z = z - math.floor(z)
local u = perlin.fade(x)
local v = perlin.fade(y)
local w = perlin.fade(z)
local A = self[X + 1] + Y
local AA = self[A + 1] + Z
local AB = self[A + 2] + Z
local B = self[X + 2] + Y
local BA = self[B + 1] + Z
local BB = self[B + 2] + Z
return perlin.lerp(w, perlin.lerp(v, perlin.lerp(u, perlin.grad(self[AA + 1], x , y , z ),
perlin.grad(self[BA + 1], x - 1, y , z )),
perlin.lerp(u, perlin.grad(self[AB + 1], x , y - 1, z ),
perlin.grad(self[BB + 1], x - 1, y - 1, z ))),
perlin.lerp(v, perlin.lerp(u, perlin.grad(self[AB + 2], x , y , z - 1),
perlin.grad(self[BA + 2], x - 1, y , z - 1)),
perlin.lerp(u, perlin.grad(self[AB + 2], x , y - 1, z - 1),
perlin.grad(self[BB + 2], x - 1, y - 1, z - 1))))
end