hill-noise/main.lua
2017-03-31 01:23:09 -07:00

125 lines
3.5 KiB
Lua

lg = love.graphics
W,H = lg.getDimensions()
local Noise = require "noise"
local DRAW_RES = love.window.toPixels(4)
local SCALE = 100
local font = lg.newFont(love.window.toPixels(24))
local decay = function(x) return .1^x end
local function rng()
local r = love.math.newRandomGenerator(1)
return function() return r:random() end
end
local n1 = Noise.make1d(5,rng(),decay)
local n2 = Noise.make2d(7,rng(),decay)
local n3 = Noise.make3d(11,rng(),decay)
local s1 = Noise.make1dShader(7,rng(),decay)
local s2 = Noise.make2dShader(13,rng(),decay)
local s3 = Noise.make3dShader(17,rng(),decay)
local cw,ch = W/2,H/3
local canv = lg.newCanvas(cw,ch)
function love.load()
xOffset = 0
yOffset = 0
t0 = love.timer.getTime()
love.mouse.setCursor(love.mouse.getSystemCursor("hand"))
end
function love.mousepressed()
love.mouse.setCursor(love.mouse.getSystemCursor("sizeall"))
end
function love.mousereleased()
love.mouse.setCursor(love.mouse.getSystemCursor("hand"))
end
function love.draw()
lg.setColor(255,255,255)
lg.setLineWidth(love.window.toPixels(2))
lg.setLineJoin("none")
local p1 = {}
for x=0,W-H/3,DRAW_RES do
table.insert(p1,x)
table.insert(p1,n1(x/SCALE+xOffset)*H/3)
end
lg.line(p1)
local p2 = {}
local t = love.timer.getTime()-t0
for x=0,W-H/3,DRAW_RES do
table.insert(p2,x)
table.insert(p2,(1+n2(x/SCALE+xOffset,t))*H/3)
end
lg.line(p2)
for y=0,H/6*1.2,2*DRAW_RES do
local p3 = {}
for x=0,W-H/3,DRAW_RES do
table.insert(p3,x)
table.insert(p3,2/3*H+H/6*n3(x/SCALE+xOffset,y/SCALE+yOffset,t)+y)
end
lg.line(p3)
end
for x=-H/6,W-H/3,DRAW_RES*2 do
local p3 = {}
local slant = .5
for y=0,H/6*1.2,2*DRAW_RES do
local x2 = x + slant*y
if x2 < -2*DRAW_RES then goto continue end
table.insert(p3,x2)
table.insert(p3,2/3*H+H/6*n3(x2/SCALE+xOffset,y/SCALE+yOffset,t)+y)
if x2 > W/2 then break end
::continue::
end
if #p3 > 2 then
lg.line(p3)
end
end
lg.setShader(s1)
s1:send("range_min", xOffset)
s1:send("range_max", xOffset+cw/SCALE)
lg.draw(canv,W-cw,0)
lg.setShader(s2)
s2:send("range_min", {xOffset,yOffset})
s2:send("range_max", {xOffset+cw/SCALE,yOffset+ch/SCALE})
lg.draw(canv,W-cw,ch)
lg.setShader(s3)
s3:send("range_min", {xOffset,yOffset})
s3:send("range_max", {xOffset+cw/SCALE,yOffset+ch/SCALE})
s3:send('z', t)
lg.draw(canv,W-cw,2*ch)
lg.setShader(nil)
lg.setColor(255,200,0)
lg.setFont(font)
lg.printf("1D_Noise(x)", 5,5, W)
lg.printf("2D_Noise(x,time)", 5,5+H/3, W)
lg.printf("3D_Noise(x,y,time)", 5,5+2*H/3, W)
lg.printf("1D_Noise_Shader(x)", 0,5, W-5, 'right')
lg.printf("2D_Noise_Shader(x,y)", 0,5+H/3, W-5, 'right')
lg.printf("3D_Noise_Shader(x,y,time)", 0,5+2*H/3, W-5, 'right')
end
function love.update(dt)
local key = love.keyboard.isDown
local dx = 400/SCALE*dt*((key('right') and 1 or 0) + (key('left') and -1 or 0))
local dy = 400/SCALE*dt*((key('down') and 1 or 0) + (key('up') and -1 or 0))
xOffset = xOffset + dx
yOffset = yOffset + dy
end
function love.keypressed(key)
if key == 'escape' then love.event.quit() end
if key == 'r' then love.load() end
end
function love.mousemoved(x,y,dx,dy)
if love.mouse.isDown(1) then
xOffset = xOffset - dx/SCALE
yOffset = yOffset - dy/SCALE
end
end