Minetest-WorldEditAdditions/worldeditadditions/lib/sculpt/brushes/__gaussian.lua

26 lines
970 B
Lua
Raw Normal View History

2021-12-27 19:36:57 +00:00
local wea = worldeditadditions
2022-09-19 18:42:22 +00:00
local wea_c = worldeditadditions_core
2022-09-19 00:16:22 +00:00
local Vector3 = wea_c.Vector3
--- Returns a smooth gaussian brush.
2021-12-27 19:36:57 +00:00
-- @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)
2022-09-19 00:16:22 +00:00
size = math.min(size.x, size.y)
if size % 2 == 0 then size = size - 1 end -- Gaussian runs on odd numbers
if size < 1 then
2022-09-19 00:16:22 +00:00
return false, "Error: Invalid brush size (brushes must be at least 1 node in size)."
end
2022-09-19 00:16:22 +00:00
2021-12-28 18:32:10 +00:00
local success, gaussian = wea.conv.kernel_gaussian(size, sigma)
2021-12-27 19:36:57 +00:00
-- Normalise values to fill the range 0 - 1
-- By default, wea.conv.kernel_gaussian values add up to 1 in total
2022-09-19 00:16:22 +00:00
local max = wea_c.max(gaussian)
2021-12-27 19:36:57 +00:00
for i=0,size*size-1 do
gaussian[i] = gaussian[i] / max
end
return success, gaussian, Vector3.new(size, size, 0)
end