Difference between revisions of "NoiseWrapper"

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.)
m (also removed superfluous contributors section, and added umlaut to description, and fixed minimal supported version.)
 
Line 63: Line 63:
 
end
 
end
 
</source>
 
</source>
 
== Contributors ==
 
* [[User:Substitute541|Substitute541]]
 
  
 
[[Category:Snippets]]
 
[[Category:Snippets]]
 
{{#set:Author=User:Substitute541}}
 
{{#set:Author=User:Substitute541}}
{{#set:LOVE Version=any}}
+
{{#set:LOVE Version=>0.9.0}}
{{#set:Description=A noise wrapper to LOVE's noise functions}}
+
{{#set:Description=A noise wrapper to LÖVE's noise functions}}

Latest revision as of 18:07, 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