Last active
August 18, 2016 19:24
-
-
Save EntranceJew/c4cd75371b3e8c446380eb01d890f5dc to your computer and use it in GitHub Desktop.
gen_smooth_noise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local lerp = function(a, b, amount) | |
return a + (b - a) * (amount < 0 and 0 or (amount > 1 and 1 or amount)) | |
end | |
function gen_smooth_noise(width, height, baseNoise, octave) | |
local bit = require("bit") | |
local idata = love.image.newImageData( width, height ) | |
-- Sample period = 2 ^ octave; | |
local samplePeriod = bit.lshift(1, octave) | |
local sampleFrequency = 1 / samplePeriod; | |
local y, sample_y0, sample_y1, vert_blend | |
local x, sample_x0, sample_x1, horz_blend | |
local top, bot, val | |
for y = 0, height-1 do | |
-- Calculate vertical sample index | |
sample_y0 = (y / samplePeriod)*samplePeriod | |
sample_y1 = math.fmod((sample_y0 + samplePeriod), height) | |
vert_blend = (y - sample_y0) * sampleFrequency | |
for x = 0, width-1 do | |
-- Calculate horizontal sample index | |
sample_x0 = (x / samplePeriod)*samplePeriod -- Automaticaly floors | |
sample_x1 = math.fmod((sample_x0 + samplePeriod), width) -- Wraps | |
horz_blend = (x - sample_x0) * sampleFrequency | |
top = lerp(baseNoise[sample_x0 + (width*sample_y0)], baseNoise[sample_x1 + (width*sample_y0)], horz_blend) | |
bot = lerp(baseNoise[sample_x0 + (width*sample_y1)], baseNoise[sample_x1 + (width*sample_y1)], horz_blend) | |
-- Round the value to a flat integer between 0 and 255 | |
val = lerp(top, bot, vert_blend)*255 | |
val = val >= 0 and math.floor(val + .5) or math.ceil(val - .5) | |
idata:setPixel(x, y, val, val, val, 255) | |
end | |
end | |
return idata | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local gen = require("gen") | |
local coolpic | |
function love.load(args) | |
local width, height = 64, 64 | |
local noise = {} | |
local x, y | |
for y = 0, height-1 do | |
for x = 0, width-1 do | |
noise[x + width * y] = love.math.random() | |
end | |
end | |
coolpic = love.graphics.newImage(gen_smooth_noise(width, height, noise, 3)) | |
end | |
function love.draw() | |
love.graphics.draw(coolpic) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment