From d657ce1abe3bd3aac4cd6d6ef82fc49fc5f4a903 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Mon, 27 Dec 2021 19:36:57 +0000 Subject: [PATCH] //sculptlist: fix bugs. It works! --- worldeditadditions/init.lua | 2 +- worldeditadditions/lib/sculpt/brushes/__smooth.lua | 11 ++++++++++- .../lib/sculpt/brushes/default_hard.lua | 2 +- .../lib/sculpt/brushes/default_soft.lua | 2 +- worldeditadditions/lib/sculpt/init.lua | 2 ++ worldeditadditions/lib/sculpt/preview_brush.lua | 14 ++++++++------ worldeditadditions/utils/numbers.lua | 7 +++++-- .../commands/extra/sculptlist.lua | 9 ++++++--- 8 files changed, 34 insertions(+), 15 deletions(-) diff --git a/worldeditadditions/init.lua b/worldeditadditions/init.lua index 378f610..834883c 100644 --- a/worldeditadditions/init.lua +++ b/worldeditadditions/init.lua @@ -57,7 +57,7 @@ dofile(wea.modpath.."/lib/spiral_circle.lua") dofile(wea.modpath.."/lib/conv/conv.lua") dofile(wea.modpath.."/lib/erode/erode.lua") dofile(wea.modpath.."/lib/noise/init.lua") -dofile(wea.modpath.."/lib/sculpt/init.lua") +wea.sculpt = dofile(wea.modpath.."/lib/sculpt/init.lua") dofile(wea.modpath.."/lib/copy.lua") dofile(wea.modpath.."/lib/move.lua") diff --git a/worldeditadditions/lib/sculpt/brushes/__smooth.lua b/worldeditadditions/lib/sculpt/brushes/__smooth.lua index e8647a5..8654da6 100644 --- a/worldeditadditions/lib/sculpt/brushes/__smooth.lua +++ b/worldeditadditions/lib/sculpt/brushes/__smooth.lua @@ -1,6 +1,7 @@ +local wea = worldeditadditions --- Returns a smooth gaussian brush. --- @param size Vector3 The target size of the brush. Note that the actual size fo the brush will be different, as the gaussian function has some limitations. +-- @param size Vector3 The target size of the brush. Note that the actual size of the brush will be different, as the gaussian function has some limitations. -- @param sigma=2 number The 'smoothness' of the brush. Higher values are more smooth. return function(size, sigma) local size = math.min(size.x, size.y) @@ -9,5 +10,13 @@ return function(size, sigma) return false, "Error: Invalid brush size." end local success, gaussian = worldeditadditions.conv.kernel_gaussian(size, sigma) + + -- Normalise values to fill the range 0 - 1 + -- By default, wea.conv.kernel_gaussian values add up to 1 in total + local max = wea.max(gaussian) + for i=0,size*size-1 do + gaussian[i] = gaussian[i] / max + end + return success, gaussian, { x = size, y = size } end diff --git a/worldeditadditions/lib/sculpt/brushes/default_hard.lua b/worldeditadditions/lib/sculpt/brushes/default_hard.lua index c4fd325..33c1192 100644 --- a/worldeditadditions/lib/sculpt/brushes/default_hard.lua +++ b/worldeditadditions/lib/sculpt/brushes/default_hard.lua @@ -3,6 +3,6 @@ local wea = worldeditadditions local __smooth = dofile(wea.modpath.."/lib/sculpt/brushes/__smooth.lua") return function(size) - local success, brush, size_actual = __smooth(size, 1) + local success, brush, size_actual = __smooth(size, 1.25) return success, brush, size_actual end diff --git a/worldeditadditions/lib/sculpt/brushes/default_soft.lua b/worldeditadditions/lib/sculpt/brushes/default_soft.lua index 228e5ec..3982ee9 100644 --- a/worldeditadditions/lib/sculpt/brushes/default_soft.lua +++ b/worldeditadditions/lib/sculpt/brushes/default_soft.lua @@ -3,6 +3,6 @@ local wea = worldeditadditions local __smooth = dofile(wea.modpath.."/lib/sculpt/brushes/__smooth.lua") return function(size) - local success, brush, size_actual = __smooth(size, 5) + local success, brush, size_actual = __smooth(size, 3) return success, brush, size_actual end diff --git a/worldeditadditions/lib/sculpt/init.lua b/worldeditadditions/lib/sculpt/init.lua index 723ca3a..6dd75cd 100644 --- a/worldeditadditions/lib/sculpt/init.lua +++ b/worldeditadditions/lib/sculpt/init.lua @@ -13,6 +13,8 @@ local sculpt = { apply = dofile(wea.modpath.."/lib/sculpt/apply.lua") } +return sculpt + -- TODO: Automatically find & register all text file based brushes in the brushes directory -- TODO: Implement automatic scaling of static brushes to the correct size. We have ..scale already, but we probably need to implement a proper 2d canvas scaling algorithm. Some options to consider: linear < [bi]cubic < nohalo/lohalo diff --git a/worldeditadditions/lib/sculpt/preview_brush.lua b/worldeditadditions/lib/sculpt/preview_brush.lua index dd8f05d..c6da56f 100644 --- a/worldeditadditions/lib/sculpt/preview_brush.lua +++ b/worldeditadditions/lib/sculpt/preview_brush.lua @@ -22,15 +22,17 @@ local function preview_brush(brush_name, target_size) values["."] = 1 values[" "] = 0 + print(wea.inspect(brush)) + local result = {} - for z = target_size.z, 0, -1 do + for y = brush_size.y-1, 0, -1 do local row = {} - for x = target_size.x, 0, -1 do - local i = z*brush_size.x + x + for x = brush_size.x-1, 0, -1 do + local i = y*brush_size.x + x local pixel = " " local threshold_cur = -1 for value,threshold in pairs(values) do - if brush[i] > threshold and threshold_cur < threshold then + if brush[i] * 10 > threshold and threshold_cur < threshold then pixel = value threshold_cur = threshold end @@ -39,8 +41,8 @@ local function preview_brush(brush_name, target_size) end table.insert(result, table.concat(row)) end - - return true, table.concat(result) + print("RESULT", wea.inspect(result)) + return true, table.concat(result, "\n") end return preview_brush diff --git a/worldeditadditions/utils/numbers.lua b/worldeditadditions/utils/numbers.lua index 0a6f159..f8fb37c 100644 --- a/worldeditadditions/utils/numbers.lua +++ b/worldeditadditions/utils/numbers.lua @@ -33,7 +33,7 @@ end function worldeditadditions.min(list) if #list == 0 then return nil end local min = nil - for i,value in ipairs(list) do + for i,value in pairs(list) do if min == nil or min > value then min = value end @@ -46,8 +46,11 @@ end -- @returns number The maximum value in the given list. function worldeditadditions.max(list) if #list == 0 then return nil end + -- We use pairs() instead of ipairs() here, because then we can support + -- zero-indexed 1D heightmaps too - and we don't care that the order is + -- undefined with pairs(). local max = nil - for i,value in ipairs(list) do + for i,value in pairs(list) do if max == nil or max < value then max = value end diff --git a/worldeditadditions_commands/commands/extra/sculptlist.lua b/worldeditadditions_commands/commands/extra/sculptlist.lua index 9317e99..8506db4 100644 --- a/worldeditadditions_commands/commands/extra/sculptlist.lua +++ b/worldeditadditions_commands/commands/extra/sculptlist.lua @@ -20,13 +20,16 @@ minetest.register_chatcommand("/sculptlist", { if params_text == "preview" then for brush_name, brush_def in pairs(wea.sculpt.brushes) do - local preview = wea.sculpt.preview_brush(brush_name) + local success, preview = wea.sculpt.preview_brush(brush_name) local brush_size = "dynamic" - if type(brush_size) ~= "function" then + if type(brush_def) ~= "function" then brush_size = brush_def.size end + print("//sculptlist: preview for "..brush_name..":") + print(preview) + table.insert(msg, brush_name.." ["..brush_size.."]:\n") table.insert(msg, preview.."\n\n") end @@ -34,7 +37,7 @@ minetest.register_chatcommand("/sculptlist", { local display = { { "Name", "Native Size" } } for brush_name, brush_def in pairs(wea.sculpt.brushes) do local brush_size = "dynamic" - if type(brush_size) ~= "function" then + if type(brush_def) ~= "function" then brush_size = brush_def.size end