--# Main
supportedOrientations(LANDSCAPE_ANY)
function setup()
backingMode(RETAINED)
baseScale,octaveCount,persistance = 0.1,6,0.25
size = 256
xb,yb = 0,0
tileSize = WIDTH / size
nm = NoiseMap(10)
noises = {}
parameter.number("baseScale", 0.02, 0.8, 0.05, setScale)
parameter.integer("octaveCount",1,10,6,setScale)
parameter.number("persistance",0.01,1,0.25,setScale)
parameter.number("noiseSpeed", -10, 10, 0)
parameter.action("update",moveNoise)
parameter.watch([[xb.." "..yb]])
end
function moveNoise()
noises = nm:genPerlinNoise2D(xb,yb,size,size)
-- nm:expend(noises,1.5)
xb = xb + noiseSpeed
yb = yb + noiseSpeed
end
function draw()
background(0, 0, 0, 255)
noStroke()
noSmooth()
for x,column in pairs(noises) do
for y,value in pairs(column) do
fill(value*255)
rect((x-1)*tileSize, (y-1)*tileSize, tileSize, tileSize)
end
end
moveNoise()
end
function setScale()
nm:setScale(baseScale,octaveCount,persistance)
end
--# NoiseMap
NoiseMap = class()
function NoiseMap:init(seed,baseScale,octaveCount,persistance)
local s = seed or math.floor(math.random() * 1938527 - math.random() * 173777)
self.seed = math.abs(s + 123) % 200000
self.xBase = math.floor((self.seed * 37 + 13) / 127) + 1024
self.yBase = math.floor((self.seed * 14 + 31) / 67) + 512
self.zBase = math.floor((self.seed * 31 + 7) / 103) + 2048
print(self.seed,self.xBase,self.yBase,self.zBase)
self:setScale(baseScale,octaveCount,persistance)
end
function NoiseMap:setScale(baseScale,octaveCount,persistance)
if baseScale and baseScale > 1 then baseScale = 1 end
self.baseScale = baseScale or 0.2
self.octaveCount = octaveCount or 4
self.persistance = persistance or 0.25
end
function NoiseMap:genPerlinNoise2D(xBase,yBase,length,width)
local smoothedNoise = {}
for i = 1,octaveCount do
smoothedNoise[i] = self:gen2DNoise(xBase,yBase,length,width,i)
end
local perlinNoise = {}
for x = 1, length do
perlinNoise[x] = {}
for y = 1, width do
perlinNoise[x][y] = 0
end
end
amplitude = 1.0
totalAmplitude = 0.0
for octave = 1,octaveCount do
amplitude = amplitude * persistance
totalAmplitude = totalAmplitude + amplitude
for x = 1, length do
for y = 1, width do
perlinNoise[x][y] = perlinNoise[x][y] + smoothedNoise[octave][x][y] * amplitude
end
end
end
for x = 1, length do
for y = 1, width do
perlinNoise[x][y] = perlinNoise[x][y] / totalAmplitude
end
end
return perlinNoise
end
function NoiseMap:gen2DNoise(xBase,yBase,length,width,octave)
local noises = {}
local xa,ya = self.xBase + xBase,self.yBase + yBase
-- 平滑程度
local noiseScale = self.baseScale * math.pow(2,octave - 1)
for x = 1, length do
noises[x] = {}
for y = 1, width do
noises[x][y] = (noise((x + xa) * noiseScale, (y + ya) * noiseScale) + 1) / 2
end
end
return noises
end
-- 将二维数组转换到一维数组
function NoiseMap:convert2DMapToArray(map)
local array = {}
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
array[(x - 1) * width + y] = map[x][y]
end
end
return array
end
-- 噪声混合,alpha取值0~1,返回混合后的噪声
function NoiseMap:calcMix(map1,map2,alpha)
local noises = {}
local length,width = #map1,#map1[1]
for x = 1, length do
noises[x] = {}
for y = 1, width do
local a,b = map1[x][y],map2[x][y]
noises[x][y] = a + (b - a) * alpha
end
end
end
-- 噪声叠加,将两个噪声相加,返回相加后都的噪声
function NoiseMap:calcAdd(map1,map2)
local noises = {}
local length,width = #map1,#map1[1]
for x = 1, length do
noises[x] = {}
for y = 1, width do
noises[x][y] = map1[x][y] + map2[x][y]
end
end
return noises
end
-- 噪声缩放,s是缩放比例
function NoiseMap:scale(map,s)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
map[x][y] = map[x][y] * s
end
end
end
-- 分离噪声,极值化,用来生成山峰
function NoiseMap:peak(map,least)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
if map[x][y] < least then
map[x][y] = 0
end
end
end
end
-- 噪声自增加
function NoiseMap:add(map,a)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
map[x][y] = map[x][y] + a
end
end
end
-- 复制噪声
function NoiseMap:clone(map)
local noises = {}
local length,width = #map,#map[1]
for x = 1, length do
noises[x] = {}
for y = 1, width do
noises[x][y] = map[x][y]
end
end
return noises
end
-- 噪声扩张,s为扩张系数
function NoiseMap:expend(map,s)
local length,width = #map,#map[1]
local c = 0.5
for x = 1, length do
for y = 1, width do
map[x][y] = map[x][y] + (map[x][y] - c) * s
end
end
end
function NoiseMap:addWithFilter(map,a,filter)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
if map[x][y] > filter then
map[x][y] = map[x][y] + a
end
end
end
end
supportedOrientations(LANDSCAPE_ANY)
function setup()
backingMode(RETAINED)
baseScale,octaveCount,persistance = 0.1,6,0.25
size = 256
xb,yb = 0,0
tileSize = WIDTH / size
nm = NoiseMap(10)
noises = {}
parameter.number("baseScale", 0.02, 0.8, 0.05, setScale)
parameter.integer("octaveCount",1,10,6,setScale)
parameter.number("persistance",0.01,1,0.25,setScale)
parameter.number("noiseSpeed", -10, 10, 0)
parameter.action("update",moveNoise)
parameter.watch([[xb.." "..yb]])
end
function moveNoise()
noises = nm:genPerlinNoise2D(xb,yb,size,size)
-- nm:expend(noises,1.5)
xb = xb + noiseSpeed
yb = yb + noiseSpeed
end
function draw()
background(0, 0, 0, 255)
noStroke()
noSmooth()
for x,column in pairs(noises) do
for y,value in pairs(column) do
fill(value*255)
rect((x-1)*tileSize, (y-1)*tileSize, tileSize, tileSize)
end
end
moveNoise()
end
function setScale()
nm:setScale(baseScale,octaveCount,persistance)
end
--# NoiseMap
NoiseMap = class()
function NoiseMap:init(seed,baseScale,octaveCount,persistance)
local s = seed or math.floor(math.random() * 1938527 - math.random() * 173777)
self.seed = math.abs(s + 123) % 200000
self.xBase = math.floor((self.seed * 37 + 13) / 127) + 1024
self.yBase = math.floor((self.seed * 14 + 31) / 67) + 512
self.zBase = math.floor((self.seed * 31 + 7) / 103) + 2048
print(self.seed,self.xBase,self.yBase,self.zBase)
self:setScale(baseScale,octaveCount,persistance)
end
function NoiseMap:setScale(baseScale,octaveCount,persistance)
if baseScale and baseScale > 1 then baseScale = 1 end
self.baseScale = baseScale or 0.2
self.octaveCount = octaveCount or 4
self.persistance = persistance or 0.25
end
function NoiseMap:genPerlinNoise2D(xBase,yBase,length,width)
local smoothedNoise = {}
for i = 1,octaveCount do
smoothedNoise[i] = self:gen2DNoise(xBase,yBase,length,width,i)
end
local perlinNoise = {}
for x = 1, length do
perlinNoise[x] = {}
for y = 1, width do
perlinNoise[x][y] = 0
end
end
amplitude = 1.0
totalAmplitude = 0.0
for octave = 1,octaveCount do
amplitude = amplitude * persistance
totalAmplitude = totalAmplitude + amplitude
for x = 1, length do
for y = 1, width do
perlinNoise[x][y] = perlinNoise[x][y] + smoothedNoise[octave][x][y] * amplitude
end
end
end
for x = 1, length do
for y = 1, width do
perlinNoise[x][y] = perlinNoise[x][y] / totalAmplitude
end
end
return perlinNoise
end
function NoiseMap:gen2DNoise(xBase,yBase,length,width,octave)
local noises = {}
local xa,ya = self.xBase + xBase,self.yBase + yBase
-- 平滑程度
local noiseScale = self.baseScale * math.pow(2,octave - 1)
for x = 1, length do
noises[x] = {}
for y = 1, width do
noises[x][y] = (noise((x + xa) * noiseScale, (y + ya) * noiseScale) + 1) / 2
end
end
return noises
end
-- 将二维数组转换到一维数组
function NoiseMap:convert2DMapToArray(map)
local array = {}
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
array[(x - 1) * width + y] = map[x][y]
end
end
return array
end
-- 噪声混合,alpha取值0~1,返回混合后的噪声
function NoiseMap:calcMix(map1,map2,alpha)
local noises = {}
local length,width = #map1,#map1[1]
for x = 1, length do
noises[x] = {}
for y = 1, width do
local a,b = map1[x][y],map2[x][y]
noises[x][y] = a + (b - a) * alpha
end
end
end
-- 噪声叠加,将两个噪声相加,返回相加后都的噪声
function NoiseMap:calcAdd(map1,map2)
local noises = {}
local length,width = #map1,#map1[1]
for x = 1, length do
noises[x] = {}
for y = 1, width do
noises[x][y] = map1[x][y] + map2[x][y]
end
end
return noises
end
-- 噪声缩放,s是缩放比例
function NoiseMap:scale(map,s)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
map[x][y] = map[x][y] * s
end
end
end
-- 分离噪声,极值化,用来生成山峰
function NoiseMap:peak(map,least)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
if map[x][y] < least then
map[x][y] = 0
end
end
end
end
-- 噪声自增加
function NoiseMap:add(map,a)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
map[x][y] = map[x][y] + a
end
end
end
-- 复制噪声
function NoiseMap:clone(map)
local noises = {}
local length,width = #map,#map[1]
for x = 1, length do
noises[x] = {}
for y = 1, width do
noises[x][y] = map[x][y]
end
end
return noises
end
-- 噪声扩张,s为扩张系数
function NoiseMap:expend(map,s)
local length,width = #map,#map[1]
local c = 0.5
for x = 1, length do
for y = 1, width do
map[x][y] = map[x][y] + (map[x][y] - c) * s
end
end
end
function NoiseMap:addWithFilter(map,a,filter)
local length,width = #map,#map[1]
for x = 1, length do
for y = 1, width do
if map[x][y] > filter then
map[x][y] = map[x][y] + a
end
end
end
end