Difference between revisions of "Noise"

From GiderosMobile
Line 27: Line 27:
 
require "FastNoise"
 
require "FastNoise"
 
   
 
   
local noise = FNoise.Noise2D
+
local myNoise = Noise.new() -- create FastNoise instance
 
   
 
   
FNoise.SetSeed(456)
+
myNoise:setSeed(456) -- set generation seed
FNoise.SetFrequency(0.04)
+
myNoise:setFrequency(0.04) -- how coarse the noise output is
FNoise.SetInterp(FNoise.HERMITE)
+
myNoise:setInterp(FNoise.HERMITE) -- set noise interpolation type
FNoise.SetNoiseType(FNoise.SIMPLEX)
+
myNoise:setNoiseType(FNoise.SIMPLEX) -- set noise type
 
   
 
   
for y = 1, 32 do  
+
for y = 1, 4 do  
for x = 1, 32 do  
+
for x = 1, 4 do  
local v = noise(x, y)
+
local v = myNoise:noise(x, y) -- generate 2D noise
 +
-- other possible function:
 +
-- myNoise:noise(x) -- 1D noise
 +
-- myNoise:noise(x, y, z) -- 3D noise
 +
-- myNoise:noise2D(x, y) -- true 2D noise
 +
-- myNoise:noise3D(x, y, z) -- true 3D noise
 +
-- difference between noise2D(x, y) and noise(x, y) is that "noise" function uses 3D noise with z = 0, where noise2D uses only 2 values. In other words: noise(x) == noise(x,0,0); noise(x, y) == noise(x,y,0); noise(x,y) == noise2D(x,y); noise(x,y,z) == noise3D(x,y,z)
 
print(v)
 
print(v)
 
end
 
end

Revision as of 08:03, 14 December 2019


Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2019.12

Description

FastNoise is an open source noise generation library with a large collection of different noise algorithms. [FastNoise home page]

Features

  • Value Noise 2D, 3D
  • Perlin Noise 2D, 3D
  • Simplex Noise 2D, 3D, 4D
  • Cubic Noise 2D, 3D
  • Gradient Perturb 2D, 3D
  • Multiple fractal options for all of the above
  • Cellular (Voronoi) Noise 2D, 3D
  • White Noise 2D, 3D, 4D
  • Texture generation
  • Array generation

Orignal documentation: https://github.com/Auburns/FastNoise/wiki

Examples

Example 1.

require "FastNoise"
 
local myNoise = Noise.new() -- create FastNoise instance
 
myNoise:setSeed(456) -- set generation seed
myNoise:setFrequency(0.04) -- how coarse the noise output is
myNoise:setInterp(FNoise.HERMITE) -- set noise interpolation type
myNoise:setNoiseType(FNoise.SIMPLEX) -- set noise type
 
for y = 1, 4 do 
	for x = 1, 4 do 
		local v = myNoise:noise(x, y) -- generate 2D noise
		-- other possible function:
		-- myNoise:noise(x) -- 1D noise
		-- myNoise:noise(x, y, z) -- 3D noise
		-- myNoise:noise2D(x, y) -- true 2D noise
		-- myNoise:noise3D(x, y, z) -- true 3D noise
		-- difference between noise2D(x, y) and noise(x, y) is that "noise" function uses 3D noise with z = 0, where noise2D uses only 2 values. In other words: noise(x) == noise(x,0,0); noise(x, y) == noise(x,y,0); noise(x,y) == noise2D(x,y); noise(x,y,z) == noise3D(x,y,z)
		print(v)
	end
end


Example 2.

-- genTex(width, height, filtering, options)
-- options (table): texture parameters. Includes standart options like wrap, transparentColor etc.
-- 	noise (table): noise parameters
-- 		xoff (number): offset noise on x axis (optional)
-- 		yoff (number): offset noise on y axis (optional)
-- 		zoff (number): offset noise on z axis (optional)		
-- 		min (number): noise minimum value (optional, default: -1)
-- 		max (number): noise maximum value (optional, default: 1)
-- 		colors (table): noise color map
-- 			h (number): color height (from 0 to 1)
-- 			color (table): color in r g b a format (by default - white color)
 
local colors = {
	{h = 0.3, color = {52,99,195,255}},
	{h = 0.4, color = {54,102,198,255}},
	{h = 0.45, color = {209,208,128,255}},
	{h = 0.55, color = {88,151,24,255}},
	{h = 0.6, color = {63,106,20,255}},
	{h = 0.7, color = {92,68,61,255}},
	{h = 0.9, color = {75,60,55,255}},
	{h = 1, color = {255,255,255,255}},
}
 
n:setFrequency(0.03)
n:setFractalOctaves(5)
n:setFractalLacunarity(2.5)
n:setInterp(Noise.HERMITE)
n:setFractalGain(0.6)
n:setNoiseType(Noise.SIMPLEX_FRACTAL)
 
local tex = genTex(128, 128, false, {wrap=Texture.REPEAT, noise = {xoff = 0, yoff = 0, zoff = 0, min = -1, max = 1, colors = colors}})
local btm = Bitmap.new(tex)
btm:setPosition(10, 10)
btm:setScale(4)
stage:addChild(btm)
 
n:setSeed(6546)
n:setFrequency(0.02)
n:setFractalOctaves(3)
 
tex = genTex(128, 128, false, {wrap=Texture.REPEAT, noise = {xoff = 0, yoff = 0, zoff = 0, min = -1, max = 1, colors = colors}})
btm = Bitmap.new()
btm:setPosition(128*4 + 20, 10)
btm:setScale(4)
stage:addChild(btm)


Example 3. Save a picture on your phone or on your pc.

application:setBackgroundColor(0x323232)
 
function byteColor(r,g,b,a)
	r=r or 255
	g=g or 255
	b=b or 255
	a=a or 255
	return string.format("%s%s%s%s", string.char(r), string.char(g), string.char(b), string.char(a))
end
 
function map(v, minC, maxC, minD, maxD)
	local newV = (v - minC) / (maxC - minC) * (maxD - minD) + minD
	return newV 
end
 
require "FastNoise"
 
local n = Noise.new()
 
function gen(w, h, filtering, options)
	local data = ""
	local t = options.noise or {}
	t.min = t.min or -1
	t.max = t.max or 1
	t.xoff = t.xoff or 0
	t.zoff = t.zoff or 0
	t.zoff = t.zoff or 0
 
	local colors = t.colors or {}
	for y = 1, h do 
		for x = 1, w do 
			local c = byteColor()
 
			local v = n:noise(x + t.xoff, y + t.yoff, t.zoff)
			local maped = map(v, t.min, t.max, 0, 1)
 
			for _,tc in ipairs(colors) do 
				if (maped <= tc.h) then 
					c = byteColor(unpack(tc.color))
					break
				end
			end
 
			data = data .. c
		end
	end
	return Texture.new(data,w,h,filtering,options)
end
local colors = {
	{h = 0.3, color = {52,99,195,255}},
	{h = 0.4, color = {54,102,198,255}},
	{h = 0.45, color = {209,208,128,255}},
	{h = 0.55, color = {88,151,24,255}},
	{h = 0.6, color = {63,106,20,255}},
	{h = 0.7, color = {92,68,61,255}},
	{h = 0.9, color = {75,60,55,255}},
	{h = 1, color = {255,255,255,255}},
}
 
n:setFrequency(0.03)
n:setFractalOctaves(5)
n:setFractalLacunarity(2.5)
n:setInterp(Noise.HERMITE)
n:setFractalType(Noise.FBM)
n:setFractalGain(0.6)
n:setNoiseType(Noise.SIMPLEX_FRACTAL)
 
local btm = Bitmap.new(gen(128, 128, false, {wrap=Texture.REPEAT, noise = {xoff = 0, yoff = 0, zoff = 0, min = -1, max = 1, colors = colors}}))
btm:setPosition(10, 10)
btm:setScale(4)
stage:addChild(btm)
 
--n:setSeed(6546)
n:setFrequency(0.02)
n:setFractalOctaves(3)
 
btm = Bitmap.new(gen(128, 128, false, {wrap=Texture.REPEAT, noise = {xoff = 0, yoff = 0, zoff = 0, min = -1, max = 1, colors = colors}}))
btm:setPosition(128*4 + 20, 10)
btm:setScale(4)
stage:addChild(btm)

Notes

Methods

FastNoise:reset reset noise parameters to default

Events

Constants