mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 10:31:50 +00:00
add math.noise
This commit is contained in:
parent
030b065bc5
commit
6910a7e029
1 changed files with 32 additions and 0 deletions
|
|
@ -39,3 +39,35 @@ end
|
|||
function math.sign(x)
|
||||
return (x > 0) and 1 or (x < 0 and -1 or 0)
|
||||
end
|
||||
|
||||
local noise_period = 3021
|
||||
local noise_rands = {}
|
||||
|
||||
for i=1,noise_period do
|
||||
table.insert(noise_rands, math.random() * 2.0 - 1.0)
|
||||
end
|
||||
|
||||
local function smoothstep(t)
|
||||
return t * t * (3 - 2 * t)
|
||||
end
|
||||
|
||||
local function sample_noise(x)
|
||||
x = x % noise_period
|
||||
local left_cell = math.floor(x)
|
||||
local right_cell = (left_cell + 1) % noise_period
|
||||
local t = smoothstep(x - left_cell)
|
||||
return noise_rands[left_cell + 1] * (1.0 - t)
|
||||
+ noise_rands[right_cell + 1] * t
|
||||
end
|
||||
|
||||
function math.noise(x, octaves)
|
||||
octaves = octaves or 1
|
||||
local value = 0.0
|
||||
local mul = 1.0
|
||||
for i=1,octaves do
|
||||
value = value * (1.0 - mul) + sample_noise(x + 37 * i) * mul
|
||||
x = x * 2.0
|
||||
mul = mul * 0.5
|
||||
end
|
||||
return value
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue