From 17cc91ba1c6694c3819425663b57796d27650563 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 28 Dec 2021 17:45:20 +0000 Subject: [PATCH] //sculpt: implement circle brush --- worldeditadditions/lib/sculpt/apply.lua | 2 + .../lib/sculpt/brushes/circle.lua | 5 +- worldeditadditions/lib/sculpt/init.lua | 1 + .../lib/sculpt/make_preview.lua | 54 +++++++++++++++++++ .../lib/sculpt/preview_brush.lua | 43 ++------------- .../commands/sculpt.lua | 1 - 6 files changed, 62 insertions(+), 44 deletions(-) create mode 100644 worldeditadditions/lib/sculpt/make_preview.lua diff --git a/worldeditadditions/lib/sculpt/apply.lua b/worldeditadditions/lib/sculpt/apply.lua index fcb9c52..28307f7 100644 --- a/worldeditadditions/lib/sculpt/apply.lua +++ b/worldeditadditions/lib/sculpt/apply.lua @@ -12,6 +12,8 @@ local function apply(pos1, brush_name, height, brush_size) local success, brush, brush_size_actual = wea.sculpt.make_brush(brush_name, brush_size) if not success then return success, brush end + print(wea.sculpt.make_preview(brush, brush_size_actual)) + local brush_size_terrain = Vector3.new( brush_size_actual.x, 0, diff --git a/worldeditadditions/lib/sculpt/brushes/circle.lua b/worldeditadditions/lib/sculpt/brushes/circle.lua index 4ff6746..c2cdc0e 100644 --- a/worldeditadditions/lib/sculpt/brushes/circle.lua +++ b/worldeditadditions/lib/sculpt/brushes/circle.lua @@ -9,14 +9,11 @@ return function(size) local centre = (size / 2):floor() local minsize = math.floor(math.min(size.x, size.y) / 2) - print("centre", centre, "minsize", minsize) - for y = size.y - 1, 0, -1 do for x = size.x - 1, 0, -1 do local i = y*size.x + x - - if math.floor((centre - Vector3.new(x, y, 0)):abs():length()) < minsize - 2 then + if math.floor((centre - Vector3.new(x, y, 0)):length()) < minsize then brush[i] = 1 else brush[i] = 0 diff --git a/worldeditadditions/lib/sculpt/init.lua b/worldeditadditions/lib/sculpt/init.lua index ad9ffb6..b1ae6d4 100644 --- a/worldeditadditions/lib/sculpt/init.lua +++ b/worldeditadditions/lib/sculpt/init.lua @@ -9,6 +9,7 @@ local sculpt = { circle = dofile(wea.modpath.."/lib/sculpt/brushes/circle.lua") }, make_brush = dofile(wea.modpath.."/lib/sculpt/make_brush.lua"), + make_preview = dofile(wea.modpath.."/lib/sculpt/make_preview.lua"), preview_brush = dofile(wea.modpath.."/lib/sculpt/preview_brush.lua"), read_brush_static = dofile(wea.modpath.."/lib/sculpt/read_brush_static.lua"), apply_heightmap = dofile(wea.modpath.."/lib/sculpt/apply_heightmap.lua"), diff --git a/worldeditadditions/lib/sculpt/make_preview.lua b/worldeditadditions/lib/sculpt/make_preview.lua new file mode 100644 index 0000000..2b29d17 --- /dev/null +++ b/worldeditadditions/lib/sculpt/make_preview.lua @@ -0,0 +1,54 @@ +local wea = worldeditadditions +local Vector3 = wea.Vector3 + +local make_brush = dofile(wea.modpath.."/lib/sculpt/make_brush.lua") + +--- Generates a textual preview of a given brush. +-- @param brush table The brush in question to preview. +-- @param size Vector3 The size of the brush. +-- @returns string A preview of the brush as a string. +local function make_preview(brush, size, framed) + if framed == nil then framed = true end + + -- Values to map brush pixel values to. + -- Brush pixel values are first multiplied by 10 before comparing to these numbers + local values = {} + values["@"] = 9.5 + values["#"] = 8 + values["="] = 6 + values[":"] = 5 + values["-"] = 4 + values["."] = 1 + values[" "] = 0 + + local frame_vertical = "+"..string.rep("-", math.max(0, size.x)).."+" + + local result = {} + if framed then table.insert(result, frame_vertical) end + + for y = size.y-1, 0, -1 do + local row = {} + if framed then table.insert(row, "|") end + for x = size.x-1, 0, -1 do + local i = y*size.x + x + local pixel = " " + local threshold_cur = -1 + for value,threshold in pairs(values) do + if brush[i] * 10 > threshold and threshold_cur < threshold then + pixel = value + threshold_cur = threshold + end + end + table.insert(row, pixel) + end + if framed then table.insert(row, "|") end + table.insert(result, table.concat(row)) + end + + if framed then table.insert(result, frame_vertical) end + + + return table.concat(result, "\n") +end + +return make_preview diff --git a/worldeditadditions/lib/sculpt/preview_brush.lua b/worldeditadditions/lib/sculpt/preview_brush.lua index ede291c..13b07df 100644 --- a/worldeditadditions/lib/sculpt/preview_brush.lua +++ b/worldeditadditions/lib/sculpt/preview_brush.lua @@ -2,6 +2,7 @@ local wea = worldeditadditions local Vector3 = wea.Vector3 local make_brush = dofile(wea.modpath.."/lib/sculpt/make_brush.lua") +local make_preview = dofile(wea.modpath.."/lib/sculpt/make_preview.lua") --- Generates a textual preview of a given brush. -- @param brush_name string The name of the brush to create a preview for. @@ -10,47 +11,11 @@ local make_brush = dofile(wea.modpath.."/lib/sculpt/make_brush.lua") local function preview_brush(brush_name, target_size, framed) if framed == nil then framed = true end if not target_size then target_size = Vector3.new(10, 10, 0) end + local success, brush, brush_size = make_brush(brush_name, target_size) + if not success then return success, brush end - -- Values to map brush pixel values to. - -- Brush pixel values are first multiplied by 10 before comparing to these numbers - local values = {} - values["@"] = 9.5 - values["#"] = 8 - values["="] = 6 - values[":"] = 5 - values["-"] = 4 - values["."] = 1 - values[" "] = 0 - - local frame_vertical = "+"..string.rep("-", math.max(0, brush_size.x)).."+" - - local result = {} - if framed then table.insert(result, frame_vertical) end - - for y = brush_size.y-1, 0, -1 do - local row = {} - if framed then table.insert(row, "|") end - 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] * 10 > threshold and threshold_cur < threshold then - pixel = value - threshold_cur = threshold - end - end - table.insert(row, pixel) - end - if framed then table.insert(row, "|") end - table.insert(result, table.concat(row)) - end - - if framed then table.insert(result, frame_vertical) end - - - return true, table.concat(result, "\n") + return true, make_preview(brush, brush_size, framed) end return preview_brush diff --git a/worldeditadditions_commands/commands/sculpt.lua b/worldeditadditions_commands/commands/sculpt.lua index b0ffb98..a9e949e 100644 --- a/worldeditadditions_commands/commands/sculpt.lua +++ b/worldeditadditions_commands/commands/sculpt.lua @@ -56,7 +56,6 @@ worldedit.register_command("sculpt", { local brush_min = wea.min(brush) local brush_max = wea.max(brush) local range_nodes = (brush_max * height) - (brush_min * height) - print("//sculpt range_nodes", range_nodes) return size_actual.x * size_actual.y * range_nodes end,