cepmoc

Untitled

Feb 28th, 2026
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. --!native
  2. local pearlin = {}
  3. local vector = require(game:GetService("ServerStorage").vector)
  4.  
  5. function pearlin.fade(t)
  6.     return ((6 * t - 15) * t + 10) * t * t * t
  7. end
  8.  
  9. function pearlin.lerp(t, a1, a2)
  10.     return a1 + t * (a2 - a1)
  11. end
  12.  
  13. function pearlin.getConstVector(v)
  14.     v = v or 0
  15.     local h = bit32.band(v, 3)
  16.     if h == 0 then
  17.         return vector.new(1.0, 1.0)
  18.     elseif h == 1 then
  19.         return vector.new(-1.0, 1.0)
  20.     elseif h == 2 then
  21.         return vector.new(-1.0, -1.0)
  22.     else
  23.         return vector.new(1.0, -1.0)
  24.     end
  25. end
  26.  
  27. function pearlin.createNDarray(n, tbl)
  28.     tbl = tbl or {}
  29.     if n == 0 then
  30.         return tbl
  31.     end
  32.     setmetatable(tbl, {
  33.         __index = function(t, k)
  34.             local new = pearlin.createNDarray(n - 1)
  35.             t[k] = new
  36.             return new
  37.         end,
  38.     })
  39.     return tbl
  40. end
  41.  
  42. function pearlin.createANDarray(n, tbl, func)
  43.     tbl = tbl or {}
  44.     if n == 0 then
  45.         return setmetatable(tbl, { __newindex = func })
  46.     end
  47.     setmetatable(tbl, {
  48.         __index = function(t, k)
  49.             local new = pearlin.createANDarray(n - 1, nil, func)
  50.             t[k] = new
  51.             return new
  52.         end,
  53.     })
  54.     return tbl
  55. end
  56.  
  57. function pearlin.shuffle(tbl)
  58.     local temp
  59.     for e = (#tbl - 1), 1, -1 do
  60.         local index = math.floor((math.random() * (e - 1)) + 0.5)
  61.         temp = tbl[e]
  62.         tbl[e] = tbl[index]
  63.         tbl[index] = temp
  64.     end
  65. end
  66.  
  67. function pearlin.makeperm()
  68.     local P = {}
  69.     for i = 0, 8192 do
  70.         table.insert(P, i)
  71.     end
  72.     pearlin.shuffle(P)
  73.     for i = 0, 8192 do
  74.         table.insert(P, i)
  75.     end
  76.     return P
  77. end
  78. local P = pearlin.makeperm()
  79. function pearlin.noise2d(x, y)
  80.     local X = math.floor(x)
  81.     local Y = math.floor(y)
  82.     local xf = x - math.floor(x)
  83.     local yf = y - math.floor(y)
  84.     local topRight = vector.new(xf - 1.0, yf - 1.0)
  85.     local topLeft = vector.new(xf, yf - 1.0)
  86.     local bottomRight = vector.new(xf - 1.0, yf)
  87.     local bottomLeft = vector.new(xf, yf)
  88.     local valueTopRight = P[P[X + 1] + Y + 1]
  89.     local valueTopLeft = P[P[X] + Y + 1]
  90.     local valueBottomRight = P[P[X + 1] + Y]
  91.     local valueBottomLeft = P[P[X] + Y]
  92.     local dotTopRight = topRight:dot(pearlin.getConstVector(valueTopRight))
  93.     local dotTopLeft = topLeft:dot(pearlin.getConstVector(valueTopLeft))
  94.     local dotBottomRight = bottomRight:dot(pearlin.getConstVector(valueBottomRight))
  95.     local dotBottomLeft = bottomLeft:dot(pearlin.getConstVector(valueBottomLeft))
  96.     local u = pearlin.fade(xf)
  97.     local v = pearlin.fade(yf)
  98.     return pearlin.lerp(u, pearlin.lerp(v, dotBottomLeft, dotTopLeft), pearlin.lerp(v, dotBottomRight, dotTopRight))
  99. end
  100.  
  101. local function hash(str)
  102.     local h = 5381
  103.     for c in str:gmatch(".") do
  104.         h = math.fmod((bit32.lshift(h, 5) + h) + string.byte(c), 2147483648)
  105.     end
  106.     return h
  107. end
  108.  
  109. function pearlin.calcseed(inputstr)
  110.     if tonumber(inputstr) then
  111.         return inputstr
  112.     else
  113.         return hash(inputstr)
  114.     end
  115. end
  116.  
  117. function pearlin.noise(x, y, amp, seed, offsetx, offsety)
  118.     math.randomseed(seed or os.time())
  119.     offsetx = offsetx or 0
  120.     offsety = offsety or 0 -- whats the math.randomseed() for
  121.     local tbl = pearlin.createNDarray(2)
  122.     local defamp = amp or 0.01
  123.     for a = 1 + offsety, y + offsety do
  124.         for b = 1 + offsetx, x + offsetx do
  125.             local n = ((pearlin.noise2d(a * defamp)) + 1) * 0.5
  126.             n = n + 1.0
  127.             n = n * 0.5
  128.             tbl[a - offsety][b - offsetx] = n
  129.         end
  130.     end
  131.     return tbl
  132. end
  133.  
  134. return pearlin
Advertisement
Add Comment
Please, Sign In to add comment