Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
5

Why does my Perlin Noise have a puzzle-like effect?

Asked by 10 years ago

I'm currently working on implementing Perlin Noise in a ROBLOX-independant fashion (but it will be used on ROBLOX) and I'm having an issue with the noise. As you can see, if I set the grid size to the noise size, it turns out okay looking. However, if I set the grid size to 32, it looks like this. All the individual pieces look okay, but they don't work together... does anyone have any idea on what could be causing this?

Here's my code.

local function rand(seed,lower,upper)
    math.randomseed(seed)
    math.random();math.random();math.random();math.random();math.random()
    return math.random()
end

local function magnitude(t)
    return math.sqrt(t[1]^2+t[2]^2)
end

local function unit(t)
    return {t[1]/magnitude(t),t[2]/magnitude(t)}
end

local function randVec(vec,seed)
    return unit{rand(seed+vec[1]+vec[2]^2/vec[1]),rand(seed+vec[1]+vec[2]^2/vec[1]+1)}
end

local function dot(t1,t2)
    return t1[1]*t2[1]+t1[2]*t2[2]
end

local function ease(x)
    return 6*x^5-15*x^4+10*x^3
end

local function lerp(x,y,a)
    return x+(y-x)*a
end

return function(gridsize,seed)
    return function(x,y)
        local pointX,pointY=x/gridsize,y/gridsize
        local nearPoints={
            {math.floor(pointX),math.floor(pointY)},
            {math.floor(pointX),math.ceil(pointY)},
            {math.ceil(pointX),math.ceil(pointY)},
            {math.ceil(pointX),math.floor(pointY)}
        }
        local influences={}
        for i,v in pairs(nearPoints) do
            local subX,subY=0,0
            if v[1]>pointX then
                subX=1
            end if v[2]>pointY then
                subY=1
            end
            influences[i]=dot(randVec(v,seed),{pointX%1-subX,pointY%1-subY})
        end
        return lerp(
            lerp(influences[1],influences[2],ease(pointX%1)),
            lerp(influences[3],influences[4],ease(pointX%1)),
            ease(pointY%1)
        )
    end
end
2
neither image is available. ZeroBits 142 — 9y

Answer this question