//noise2d: add red noise

This commit is contained in:
Starbeamrainbowlabs 2021-07-13 00:15:23 +01:00
parent 7db2207aea
commit 0dee65ca1d
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 46 additions and 2 deletions

View file

@ -518,6 +518,8 @@ Algorithm | Description
------------|-------------------------- ------------|--------------------------
`perlin` | Perlin noise. Functional, but currently contains artefacts I'm having difficulty tracking down. `perlin` | Perlin noise. Functional, but currently contains artefacts I'm having difficulty tracking down.
`sin` | A sine wave created with `math.sin()`. `sin` | A sine wave created with `math.sin()`.
`white` | Random white noise.
`red` | Red noise - has a lower frequency than white noise.
When specifying algorithm names, the `algorithm` parameter name is optional. For example, the following are both equivalent: When specifying algorithm names, the `algorithm` parameter name is optional. For example, the following are both equivalent:

View file

@ -2,9 +2,10 @@ local wea = worldeditadditions
return { return {
available = { "perlin", "sin", "white" }, available = { "perlin", "sin", "white", "red" },
Perlin = dofile(wea.modpath.."/lib/noise/engines/perlin.lua"), Perlin = dofile(wea.modpath.."/lib/noise/engines/perlin.lua"),
Sin = dofile(wea.modpath.."/lib/noise/engines/sin.lua"), Sin = dofile(wea.modpath.."/lib/noise/engines/sin.lua"),
White = dofile(wea.modpath.."/lib/noise/engines/white.lua") White = dofile(wea.modpath.."/lib/noise/engines/white.lua"),
Red = dofile(wea.modpath.."/lib/noise/engines/red.lua")
-- TODO: Follow https://www.redblobgames.com/articles/noise/introduction.html and implement different colours of noise (*especially* red and pink noise) -- TODO: Follow https://www.redblobgames.com/articles/noise/introduction.html and implement different colours of noise (*especially* red and pink noise)
} }

View file

@ -0,0 +1,39 @@
local wea = worldeditadditions
local White = dofile(wea.modpath.."/lib/noise/engines/white.lua")
local Red = {}
Red.__index = Red
function Red.new(seed)
local result = {
seed = seed or math.random(),
white = White.new(seed)
}
setmetatable(result, Red)
return result
end
function Red:noise( x, y, z )
local values = {
self.white:noise(x, y, z),
self.white:noise(x + 1, y, z),
self.white:noise(x, y + 1, z),
self.white:noise(x, y, z + 1),
self.white:noise(x - 1, y, z),
self.white:noise(x, y - 1, z),
self.white:noise(x, y, z - 1),
self.white:noise(x, y - 1, z - 1),
self.white:noise(x - 1, y, z - 1),
self.white:noise(x - 1, y - 1, z),
self.white:noise(x - 1, y - 1, z - 1),
self.white:noise(x, y + 1, z + 1),
self.white:noise(x + 1, y, z + 1),
self.white:noise(x + 1, y + 1, z),
self.white:noise(x + 1, y + 1, z + 1),
}
return wea.average(values)
end
return Red

View file

@ -21,6 +21,8 @@ function worldeditadditions.noise.make_2d(size, start_pos, params)
generator = wea.noise.engines.Sin.new() generator = wea.noise.engines.Sin.new()
elseif layer.algorithm == "white" then elseif layer.algorithm == "white" then
generator = wea.noise.engines.White.new() generator = wea.noise.engines.White.new()
elseif layer.algorithm == "red" then
generator = wea.noise.engines.Red.new()
else -- We don't have any other generators just yet else -- We don't have any other generators just yet
return false, "Error: Unknown noise algorithm '"..tostring(layer.algorithm).."' in layer "..layer_i.." of "..#params.." (available algorithms: "..table.concat(wea.noise.engines.available, ", ")..")." return false, "Error: Unknown noise algorithm '"..tostring(layer.algorithm).."' in layer "..layer_i.." of "..#params.." (available algorithms: "..table.concat(wea.noise.engines.available, ", ")..")."
end end