Codesnippets > Math

Perlin Noise implementation

(1/4) > >>

CptnRoughnight:
Hi,

here is my Perlin Noise implementation. Feel free to use in your projects!

--- Code: (glbasic) ---//****************************************
//** Perlin Noise Implementatio
//** Date: 14.12.2013
//****************************************

TYPE TPerlin
d%
f#
result#
fracX#
fracY#
total#
octaves
pnum%
per_Prim_1%[]
per_Prim_2%[]

FUNCTION InitNoise:num%
self.d% = 0
self.f# = 0
self.result = 0
self.fracX = 0
self.fracY = 0
self.total = 0
self.octaves = 0
self.pnum% = num%

DIMDATA self.per_Prim_1%[],15731,15733,15737,15739,15749,15761,15767,15773,15787,15791,15797,15803,15809,15817,15823
DIMDATA self.per_Prim_2%[],789221,789227,789251,789311,789323,789331,789343,789367,789377,789389,789391,789407,789419,789443,789473
ENDFUNCTION

FUNCTION Noise#:n%,octave
self.d = bXOR(ASL(n,13),n)

RETURN (1.0 - (bAND((self.d * (self.d * self.d * self.per_Prim_1%[self.pnum] + self.per_Prim_2%[self.pnum]) + 1376312627),2147483647)) / 1073741824.0)
ENDFUNCTION

FUNCTION Cos_Interpolate#:a#,b#,x#
self.f# = COS(PI * x#)

self.f# = (1-self.f#) * 0.5;
RETURN a# *(1-self.f#)+b# *self.f#;
ENDFUNCTION

FUNCTION Noise2D#:x%,y%
RETURN self.Noise(x * 46349 + y * 46351, 1)
ENDFUNCTION

FUNCTION Smooth_Noise2D#:x%,y%
self.result = (self.Noise2D(x-1,y-1) + self.Noise2D(x+1,y-1) + self.Noise2D(x-1,y+1) + self.Noise2D(x+1,y+1)) / 16
self.result = self.result +( (self.Noise2D(x-1,y) + self.Noise2D(x+1,y) + self.Noise2D(x,y-1) + self.Noise2D(x,y+1)) / 8)
self.result = self.result +( self.Noise2D(x,y) / 4 )
RETURN self.result;

ENDFUNCTION

FUNCTION InterpolatedNoise2D#:x%,y%
self.fracX# = x - INTEGER(x)
self.fracY# = y - INTEGER(y)

RETURN self.Cos_Interpolate(self.Cos_Interpolate(self.Smooth_Noise2D(INTEGER(x#), INTEGER(y#)), self.Smooth_Noise2D(INTEGER(x#) + 1, INTEGER(y#)), self.fracX#),self.Cos_Interpolate(self.Smooth_Noise2D(INTEGER(x#), INTEGER(y#)), self.Smooth_Noise2D(INTEGER(x#) + 1, INTEGER(y#)), self.fracX#), self.fracY#)

ENDFUNCTION

FUNCTION GetPerlinNoise2D#:x#,y#,pers#,freq#,amp#,oct
self.total# = 0

FOR self.octaves = 0 TO oct
freq# = freq*2
amp# = amp*pers
self.total# = self.total# + (self.InterpolatedNoise2D(x#*freq#,y#*freq#)*amp#)
NEXT
RETURN self.total
ENDFUNCTION
ENDTYPE
--- End code ---

edit:

and the test program :

--- Code: (glbasic) ---// --------------------------------- //
// Project: Perlin
// Start: Tuesday, September 10, 2013
// IDE Version: 11.171

GLOBAL map[]
GLOBAL x,y
GLOBAL p AS TPerlin

DIM map[10001]

CONSTANT PI = 3.141593
p.InitNoise(4)

FOR x= 0 TO 99
FOR y = 0 TO 99
map[x+y*100] = (p.GetPerlinNoise2D(x,y,0.8,0.1,1,5)+1)/255 * 255
DEBUG "map: "+map[x+y*100]+"\n"
NEXT
NEXT

FOR x= 0 TO 99
FOR y = 0 TO 99
DRAWRECT x*4,y*4,4,4,RGB(map[x+y*100]*200,0,0)

NEXT
NEXT

SHOWSCREEN

MOUSEWAIT

--- End code ---

Edit : Strange PI Bug :D

fuzzy70:
Thanks for posting this Dr. Fist, I will have a good play with this later  :booze:

Lee

Moru:
Great, always wanted to play with perlin noise! Is there any seed somewhere to get different results?

CptnRoughnight:
Ahhh my fault!

for seeding, there are two arrays in the Perlin Type, with

--- Code: (glbasic) ---perlin.InitNoise( seed )

--- End code ---

you can access them!

Schranz0r:
thats cool!

Now the 3D and we good to go! :D