0
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ I think you have some problems on the calculation. Adding the code will help people a lot here. \$\endgroup\$ – Greffin28 Apr 24 '16 at 6:44
  • \$\begingroup\$ @Greffin28 Done. See any error I can' t find? \$\endgroup\$ – user6245072 Apr 24 '16 at 7:07
3
\$\begingroup\$

Here:

    perlin.lerp(v, perlin.lerp(u, perlin.grad(self[AB + 2], x    , y    , z - 1),

This should have AA + 2 instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.