Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --!native
- local pearlin = {}
- local vector = require(game:GetService("ServerStorage").vector)
- function pearlin.fade(t)
- return ((6 * t - 15) * t + 10) * t * t * t
- end
- function pearlin.lerp(t, a1, a2)
- return a1 + t * (a2 - a1)
- end
- function pearlin.getConstVector(v)
- v = v or 0
- local h = bit32.band(v, 3)
- if h == 0 then
- return vector.new(1.0, 1.0)
- elseif h == 1 then
- return vector.new(-1.0, 1.0)
- elseif h == 2 then
- return vector.new(-1.0, -1.0)
- else
- return vector.new(1.0, -1.0)
- end
- end
- function pearlin.createNDarray(n, tbl)
- tbl = tbl or {}
- if n == 0 then
- return tbl
- end
- setmetatable(tbl, {
- __index = function(t, k)
- local new = pearlin.createNDarray(n - 1)
- t[k] = new
- return new
- end,
- })
- return tbl
- end
- function pearlin.createANDarray(n, tbl, func)
- tbl = tbl or {}
- if n == 0 then
- return setmetatable(tbl, { __newindex = func })
- end
- setmetatable(tbl, {
- __index = function(t, k)
- local new = pearlin.createANDarray(n - 1, nil, func)
- t[k] = new
- return new
- end,
- })
- return tbl
- end
- function pearlin.shuffle(tbl)
- local temp
- for e = (#tbl - 1), 1, -1 do
- local index = math.floor((math.random() * (e - 1)) + 0.5)
- temp = tbl[e]
- tbl[e] = tbl[index]
- tbl[index] = temp
- end
- end
- function pearlin.makeperm()
- local P = {}
- for i = 0, 8192 do
- table.insert(P, i)
- end
- pearlin.shuffle(P)
- for i = 0, 8192 do
- table.insert(P, i)
- end
- return P
- end
- local P = pearlin.makeperm()
- function pearlin.noise2d(x, y)
- local X = math.floor(x)
- local Y = math.floor(y)
- local xf = x - math.floor(x)
- local yf = y - math.floor(y)
- local topRight = vector.new(xf - 1.0, yf - 1.0)
- local topLeft = vector.new(xf, yf - 1.0)
- local bottomRight = vector.new(xf - 1.0, yf)
- local bottomLeft = vector.new(xf, yf)
- local valueTopRight = P[P[X + 1] + Y + 1]
- local valueTopLeft = P[P[X] + Y + 1]
- local valueBottomRight = P[P[X + 1] + Y]
- local valueBottomLeft = P[P[X] + Y]
- local dotTopRight = topRight:dot(pearlin.getConstVector(valueTopRight))
- local dotTopLeft = topLeft:dot(pearlin.getConstVector(valueTopLeft))
- local dotBottomRight = bottomRight:dot(pearlin.getConstVector(valueBottomRight))
- local dotBottomLeft = bottomLeft:dot(pearlin.getConstVector(valueBottomLeft))
- local u = pearlin.fade(xf)
- local v = pearlin.fade(yf)
- return pearlin.lerp(u, pearlin.lerp(v, dotBottomLeft, dotTopLeft), pearlin.lerp(v, dotBottomRight, dotTopRight))
- end
- local function hash(str)
- local h = 5381
- for c in str:gmatch(".") do
- h = math.fmod((bit32.lshift(h, 5) + h) + string.byte(c), 2147483648)
- end
- return h
- end
- function pearlin.calcseed(inputstr)
- if tonumber(inputstr) then
- return inputstr
- else
- return hash(inputstr)
- end
- end
- function pearlin.noise(x, y, amp, seed, offsetx, offsety)
- math.randomseed(seed or os.time())
- offsetx = offsetx or 0
- offsety = offsety or 0 -- whats the math.randomseed() for
- local tbl = pearlin.createNDarray(2)
- local defamp = amp or 0.01
- for a = 1 + offsety, y + offsety do
- for b = 1 + offsetx, x + offsetx do
- local n = ((pearlin.noise2d(a * defamp)) + 1) * 0.5
- n = n + 1.0
- n = n * 0.5
- tbl[a - offsety][b - offsetx] = n
- end
- end
- return tbl
- end
- return pearlin
Advertisement
Add Comment
Please, Sign In to add comment