Difference between revisions of "NoiseWrapper"

(Created page with "Generates either a normal noise (normalized from -1 to 1) or a fractal noise depending on how many arguments you give it. <source lang="lua"> local function clamp(v, M, m) retu...")
 
m (Added missing property for the category page; also updated the comments to reflect simplex noise being patent-laden above 2 dimensions. 1D fractal noise is not possible with this snippet, since it would also have 4 parameters.)
Line 42: Line 42:
 
end
 
end
  
-- Giving less than or equal to 4 arguments will generate normal simplex noise
+
-- Giving less than or equal to 2 arguments will generate normal simplex noise
-- normalized from -1 to 1. Giving more than 4 arguments will generate fractal
+
-- normalized from -1 to 1. For 2 or 3 parameters, it will generate Perlin
-- noise. When generating fractal noise, the last 3 arguments will be the
+
-- noise. Giving more than 4 arguments will generate fractal noise.
 +
-- When generating fractal noise, the last 3 arguments will be the
 
-- amount of octaves, the amplitude scale factor, and the frequency scale
 
-- amount of octaves, the amplitude scale factor, and the frequency scale
 
-- factor, respectively.
 
-- factor, respectively.
Line 68: Line 69:
 
[[Category:Snippets]]
 
[[Category:Snippets]]
 
{{#set:Author=User:Substitute541}}
 
{{#set:Author=User:Substitute541}}
 +
{{#set:LOVE Version=any}}
 
{{#set:Description=A noise wrapper to LOVE's noise functions}}
 
{{#set:Description=A noise wrapper to LOVE's noise functions}}

Revision as of 18:06, 11 November 2016

Generates either a normal noise (normalized from -1 to 1) or a fractal noise depending on how many arguments you give it.

local function clamp(v, M, m)
	return (v > M and M or v) < m and m or v
end

local function fractalNoise2(x, y, n, a, f)
	local ca = 1
	local cf = 1
	local val = 0
	for _=1, n do
		val = val + (love.math.noise(x*cf, y*cf)*2-1)*ca
		ca = ca * a
		cf = cf * f
	end
	return clamp(val, 1, -1)
end

local function fractalNoise3(x, y, z, n, a, f)
	local ca = 1
	local cf = 1
	local val = 0
	for _=1, n do
		val = val + (love.math.noise(x*cf, y*cf, z*cf)*2-1)*ca
		ca = ca * a
		cf = cf * f
	end
	return clamp(val, 1, -1)
end

local function fractalNoise4(x, y, z, w, n, a, f)
	local ca = 1
	local cf = 1
	local val = 0
	for _=1, n do
		val = val + (love.math.noise(x*cf, y*cf, z*cf, w*cf)*2-1)*ca
		ca = ca * a
		cf = cf * f
	end
	return clamp(val, 1, -1)
end

-- Giving less than or equal to 2 arguments will generate normal simplex noise
-- normalized from -1 to 1. For 2 or 3 parameters, it will generate Perlin
-- noise. Giving more than 4 arguments will generate fractal noise.
-- When generating fractal noise, the last 3 arguments will be the
-- amount of octaves, the amplitude scale factor, and the frequency scale
-- factor, respectively.
--
-- noiseWrapper(x, [y, [z, [w, [n, a, f]]]])
function noiseWrapper(...)
	local l = #{...}
	if l <= 4 then
		return love.math.noise(...)*2-1
	elseif l == 5 then
		return fractalNoise2(...)
	elseif l == 6 then
		return fractalNoise3(...)
	elseif l == 7 then
		return fractalNoise4(...)
	else return nil end
end

Contributors