Difference between revisions of "FractalNoise"
(Created page with "Generates a fractal noise using LOVE's noise functions <source lang="lua"> -- returns a fractal noise value in the range [-1.0, 1.0] function genFractalNoise2(x, y, iter, factor...") |
m (Actually edit the category description so it reflects the changes.) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
<source lang="lua"> | <source lang="lua"> | ||
+ | -- x, y, z, w are the positions to sample | ||
+ | -- iter is how many iterations (octaves) of the noise (default: 1) | ||
+ | -- amp is the amplitude factor (default: 0.5) | ||
+ | -- freq is the frequency factor (default: 2) | ||
+ | |||
-- returns a fractal noise value in the range [-1.0, 1.0] | -- returns a fractal noise value in the range [-1.0, 1.0] | ||
− | function genFractalNoise2(x, y, iter, | + | function genFractalNoise1(x, iter, amp, freq) |
+ | val = love.math.noise(x)*2-1 | ||
+ | freq = freq or 2 | ||
+ | iter = iter or 1 | ||
+ | amp = amp or 0.5 | ||
+ | local n = 0 | ||
+ | while n < iter-1 do | ||
+ | val = val + (love.math.noise(x*freq)*2-1)*amp | ||
+ | freq = freq * freq | ||
+ | amp = amp * amp | ||
+ | n = n + 1 | ||
+ | end | ||
+ | return math.max(math.min(val, 1.0), -1.0) | ||
+ | end | ||
+ | |||
+ | -- returns a fractal noise value in the range [-1.0, 1.0] | ||
+ | function genFractalNoise2(x, y, iter, amp, freq) | ||
val = love.math.noise(x, y)*2-1 | val = love.math.noise(x, y)*2-1 | ||
− | + | freq = freq or 2 | |
iter = iter or 1 | iter = iter or 1 | ||
− | + | amp = amp or 0.5 | |
local n = 0 | local n = 0 | ||
while n < iter-1 do | while n < iter-1 do | ||
− | val = val + (love.math.noise(x* | + | val = val + (love.math.noise(x*freq, y*freq)*2-1)*amp |
+ | freq = freq * freq | ||
+ | amp = amp * amp | ||
n = n + 1 | n = n + 1 | ||
end | end | ||
Line 17: | Line 40: | ||
-- returns a fractal noise value in the range [-1.0, 1.0] | -- returns a fractal noise value in the range [-1.0, 1.0] | ||
− | function genFractalNoise3(x, y, z, iter, | + | function genFractalNoise3(x, y, z, iter, amp, freq) |
− | val = love.math.noise(x, y, z)*2-1 | + | local val = love.math.noise(x, y, z)*2-1 |
− | + | freq = freq or 2 | |
+ | iter = iter or 1 | ||
+ | amp = amp or 0.5 | ||
local n = 0 | local n = 0 | ||
while n < iter-1 do | while n < iter-1 do | ||
− | val = val + (love.math.noise(x*n | + | val = val + (love.math.noise(x*freq, y*freq, z*freq)*2-1)*amp |
+ | freq = freq * freq | ||
+ | amp = amp * amp | ||
+ | n = n + 1 | ||
+ | end | ||
+ | return math.max(math.min(val, 1.0), -1.0) | ||
+ | end | ||
+ | |||
+ | -- returns a fractal noise value in the range [-1.0, 1.0] | ||
+ | function genFractalNoise4(x, y, z, w, iter, amp, freq) | ||
+ | local val = love.math.noise(x, y, z, w)*2-1 | ||
+ | freq = freq or 2 | ||
+ | iter = iter or 1 | ||
+ | amp = amp or 0.5 | ||
+ | local n = 0 | ||
+ | while n < iter-1 do | ||
+ | val = val + (love.math.noise(x*freq, y*freq, z*freq, w*freq)*2-1)*amp | ||
+ | freq = freq * freq | ||
+ | amp = amp * amp | ||
n = n + 1 | n = n + 1 | ||
end | end | ||
Line 28: | Line 71: | ||
end | end | ||
</source> | </source> | ||
− | |||
− | |||
− | |||
[[Category:Snippets]] | [[Category:Snippets]] | ||
{{#set:Author=User:Substitute541}} | {{#set:Author=User:Substitute541}} | ||
− | {{#set:Description=Generates 2 or | + | {{#set:LOVE Version=>0.9.0}} |
+ | {{#set:Description=Generates 1, 2, 3 or 4 dimensional fractal noise}} |
Latest revision as of 17:43, 11 November 2016
Generates a fractal noise using LOVE's noise functions
-- x, y, z, w are the positions to sample
-- iter is how many iterations (octaves) of the noise (default: 1)
-- amp is the amplitude factor (default: 0.5)
-- freq is the frequency factor (default: 2)
-- returns a fractal noise value in the range [-1.0, 1.0]
function genFractalNoise1(x, iter, amp, freq)
val = love.math.noise(x)*2-1
freq = freq or 2
iter = iter or 1
amp = amp or 0.5
local n = 0
while n < iter-1 do
val = val + (love.math.noise(x*freq)*2-1)*amp
freq = freq * freq
amp = amp * amp
n = n + 1
end
return math.max(math.min(val, 1.0), -1.0)
end
-- returns a fractal noise value in the range [-1.0, 1.0]
function genFractalNoise2(x, y, iter, amp, freq)
val = love.math.noise(x, y)*2-1
freq = freq or 2
iter = iter or 1
amp = amp or 0.5
local n = 0
while n < iter-1 do
val = val + (love.math.noise(x*freq, y*freq)*2-1)*amp
freq = freq * freq
amp = amp * amp
n = n + 1
end
return math.max(math.min(val, 1.0), -1.0)
end
-- returns a fractal noise value in the range [-1.0, 1.0]
function genFractalNoise3(x, y, z, iter, amp, freq)
local val = love.math.noise(x, y, z)*2-1
freq = freq or 2
iter = iter or 1
amp = amp or 0.5
local n = 0
while n < iter-1 do
val = val + (love.math.noise(x*freq, y*freq, z*freq)*2-1)*amp
freq = freq * freq
amp = amp * amp
n = n + 1
end
return math.max(math.min(val, 1.0), -1.0)
end
-- returns a fractal noise value in the range [-1.0, 1.0]
function genFractalNoise4(x, y, z, w, iter, amp, freq)
local val = love.math.noise(x, y, z, w)*2-1
freq = freq or 2
iter = iter or 1
amp = amp or 0.5
local n = 0
while n < iter-1 do
val = val + (love.math.noise(x*freq, y*freq, z*freq, w*freq)*2-1)*amp
freq = freq * freq
amp = amp * amp
n = n + 1
end
return math.max(math.min(val, 1.0), -1.0)
end