January 26, 2021, 06:20:14 AM

### Author Topic: [bb] Simple Smooth Noise by _PJ_ [ 1+ years ago ]  (Read 652 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Simple Smooth Noise by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Simple Smooth Noise
Author : _PJ_
Posted : 1+ years ago

Description : Generates smooth and tileable random "noise" maps which can be used for contours etc.
After frustratingly trying to get to grips with some Perlin Noise routines, I thought I'd have a go at just writing my own.
Maybe it's not as effective as the 'official' Perlin methods, but it seems to work okay, is very simple to follow and actually quite fast!

Code :
Code: BlitzBasic
1. ;Really basic example:
2.
3. Graphics 1600,900,32
4. Global NoiseSeed
5. Const NoiseCells%=64
6.
7. Dim NoiseMap#(0,0)
8. Dim TangentMap#(0,0)
9.
10. NoiseSeed=InitialiseNoise()
11. GenerateContourMap()
12.
13. w=NoiseCells-1
14. h=NoiseCells-1
15. For y= 0 To h
16.         For x=0 To w
17.                 c#=NoiseMap(x,y)
18.                 c=c*255
19.                 Color c,c,c
20.                 Rect x*4,y*4,4,4,True
21.         Next
22. Next
23.
24.
25.
26.
27. ;_________________________________________________________
28.
29.
30. ;Fast Smooth Noise by PJ Chowdhury 2015
31.
32. Function InitialiseNoise(Seed=0)
33.         If (Not(Seed))
34.                 Seed=MilliSecs()
35.         End If
36.         SeedRnd Seed
37.
38.         Return Seed
39. End Function
40.
41. Function GenerateContourMap()
42.         Local Y#
43.         Local X#
44.         Local Z#
45.
46.         Local W=NoiseCells-1
47.         Local H=NoiseCells-1
48.
49.         RandomiseNoiseMap
50.         SmootheMap
51. End Function
52.
53. Function SmootheMap()
54.         CalculateTangentMap
55.
56.         Local W=NoiseCells-1
57.         Local H=NoiseCells-1
58.
59.         Local X
60.         Local Y
61.         Local Z#
62.
63.         For Y = 0 To H
64.                 For X = 0 To W
65.
66.                         ;Contour Map Point
67.                         Z=(NoiseMap(X,Y)+(TangentMap(X,Y)))
68.
69.                         ;Modify point by tangential amount
70.                         NoiseMap(X,Y)=Z
71.
72.                 Next
73.         Next
74.
75.         ;We have finished with Tangent Map now, so DeAllocate memory space
76.         Dim TangentMap#(0,0)
77. End Function
78.
79. Function CalculateTangentMap()
80.         Local X
81.         Local Y
82.
83.         Local XX
84.         Local YY
85.
86.         Local XXX
87.         Local YYY
88.
89.         Local W=NoiseCells-1
90.         Local H=NoiseCells-1
91.
92.         Dim TangentMap#(W,H)
93.
94.         Local TestPoint#
95.         Local Current#
96.         Local Difference#
97.         Local Mean#
98.
99.         ;First Pass to Populate Base Tangents and obtain Maxima/Minima
100.         For Y=0 To H
101.                 For X= 0 To W
102.
103.                         Current=NoiseMap(X,Y)
104.                         Mean=0.0
105.
106.                         ;Determine height difference by contributions  from all surrounding points (including wraparound boundary for tiling)
107.                         For YY=Y-1 To Y+1
108.                                 For XX=X-1 To X+1
109.
110.                                         ;This allows for wraparound
111.                                         XXX=((XX+NoiseCells) Mod NoiseCells)
112.                                         YYY=((YY+NoiseCells) Mod NoiseCells)
113.
114.                                         TestPoint#=NoiseMap#(XXX,YYY)
115.                                         Difference=(TestPoint-Current)
116.                                         Mean=Mean+Difference
117.
118.                                 Next
119.                         Next
120.
121.                         ;Average weighting contributions for this cell
122.                         Mean#=Mean# * 0.125
123.
124.                         ;Store the mean tangent value
125.                         TangentMap#(X,Y) = Mean#
126.
127.                 Next
128.         Next
129.
130. End Function
131.
132. Function RandomiseNoiseMap()
133.         Local Y#
134.         Local X#
135.         Local Z#
136.
137.         Local W%=NoiseCells-1
138.         Local H%=NoiseCells-1
139.
140.         Dim NoiseMap#(W,H)
141.
142.         ;Randomise all points
143.         For Y=0 To H
144.                 For X=0 To W
145.                         ;Put value into Z# to ensure it's float
146.                         Z#=Rnd(0.0,1.0)
147.                         NoiseMap#(X,Y)=Z#
148.                 Next
149.         Next
150. End Function

Yue(Posted 1+ years ago)

Dan(Posted 1+ years ago)

Cut out the lines from ;Really basic example:to;Fast Smooth Noise by PJ Chowdhury 2015and paste them after the line Dim TangentMap#(0,0)(between dim TangentMap# and Function InitialiseNoise( )or Cutout this
Code: [Select]
`Global NoiseSeedConst NoiseCells%=64Dim NoiseMap#(0,0)Dim TangentMap#(0,0)`and paste it at the top of the program

_PJ_(Posted 1+ years ago)

Updated. Pease try this now. [/i]