diff options
Diffstat (limited to 'server/noise.lua')
-rw-r--r-- | server/noise.lua | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/server/noise.lua b/server/noise.lua index fe254ac..bf7ac2e 100644 --- a/server/noise.lua +++ b/server/noise.lua @@ -1,13 +1,29 @@ local Pos = require"common.coords".Pos local class = require"common.class" +local bit = require"bit" local tau = 2*math.pi math.randomseed(os.time()) -local function random_unit_vec() + +local function hash_list(t) + local h = #t + for _,x in ipairs(t) do + x = bit.bxor(bit.rshift(x,16),x) * 0x45d9f3b + x = bit.bxor(bit.rshift(x,16),x) * 0x45d9f3b + x = bit.bxor(bit.rshift(x,16),x) + h = bit.bxor(h,x + 0x9e3779b9 + bit.lshift(h,6) + bit.rshift(h,2)) + end + return h +end + +local function hash_to_unit_vec(t) + local h = hash_list(t) + math.randomseed(h) local theta = math.random()*tau return Pos:make(math.cos(theta),math.sin(theta)) end + local function lerp(a,b,t) return (1-t)*a + t*b end local function smoothstep(x) if x<0 then return 0 end @@ -17,15 +33,15 @@ end local function slerp(a,b,t) return lerp(a,b,smoothstep(t)) end local PerlinNoise = class() -function PerlinNoise.make(cls) +function PerlinNoise.make(cls,ind_seed) local grid = {} setmetatable(grid,{__index=function(t,k) t[k] = {} return t[k] end}) - return setmetatable({grid=grid},cls) + return setmetatable({grid=grid,ind_seed=ind_seed},cls) end function PerlinNoise.vertex(self,ix,iy) local v = self.grid[ix][iy] if v then return v end - local vv = random_unit_vec() + local vv = hash_to_unit_vec{ix,iy,self.ind_seed} self.grid[ix][iy] = vv return vv end |