From 0dee65ca1d90fe7469b1b38076d8f28f2834d51d Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 13 Jul 2021 00:15:23 +0100 Subject: [PATCH] //noise2d: add red noise --- Chat-Command-Reference.md | 2 + worldeditadditions/lib/noise/engines/init.lua | 5 ++- worldeditadditions/lib/noise/engines/red.lua | 39 +++++++++++++++++++ worldeditadditions/lib/noise/make_2d.lua | 2 + 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 worldeditadditions/lib/noise/engines/red.lua diff --git a/Chat-Command-Reference.md b/Chat-Command-Reference.md index 33edc42..3693be5 100644 --- a/Chat-Command-Reference.md +++ b/Chat-Command-Reference.md @@ -518,6 +518,8 @@ Algorithm | Description ------------|-------------------------- `perlin` | Perlin noise. Functional, but currently contains artefacts I'm having difficulty tracking down. `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: diff --git a/worldeditadditions/lib/noise/engines/init.lua b/worldeditadditions/lib/noise/engines/init.lua index 921117e..8918c8b 100644 --- a/worldeditadditions/lib/noise/engines/init.lua +++ b/worldeditadditions/lib/noise/engines/init.lua @@ -2,9 +2,10 @@ local wea = worldeditadditions return { - available = { "perlin", "sin", "white" }, + available = { "perlin", "sin", "white", "red" }, Perlin = dofile(wea.modpath.."/lib/noise/engines/perlin.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) } diff --git a/worldeditadditions/lib/noise/engines/red.lua b/worldeditadditions/lib/noise/engines/red.lua new file mode 100644 index 0000000..3ff9e13 --- /dev/null +++ b/worldeditadditions/lib/noise/engines/red.lua @@ -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 diff --git a/worldeditadditions/lib/noise/make_2d.lua b/worldeditadditions/lib/noise/make_2d.lua index 9eba7f4..06043e4 100644 --- a/worldeditadditions/lib/noise/make_2d.lua +++ b/worldeditadditions/lib/noise/make_2d.lua @@ -21,6 +21,8 @@ function worldeditadditions.noise.make_2d(size, start_pos, params) generator = wea.noise.engines.Sin.new() elseif layer.algorithm == "white" then 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 return false, "Error: Unknown noise algorithm '"..tostring(layer.algorithm).."' in layer "..layer_i.." of "..#params.." (available algorithms: "..table.concat(wea.noise.engines.available, ", ")..")." end