9c77d5cc0f804d5ca1c34e7382c288311d236c8a
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Provides the noise generators `PerlinNoise` and `InterpolatedNoise`
21 # Get the noise value at `x`, `y`
23 # The coordinates `x`, `y` can be floats of any size.
25 # Returns a value between or equal to `min` and `max`.
26 fun [](x
, y
: Float): Float is abstract
28 # Lowest possible value returned by `[]`
32 # Require: `min < max`
33 var min
= 0.0 is writable
35 # Highest possible value returned by `[]`
39 # Require: `min < max`
40 var max
= 1.0 is writable
42 # Distance between reference points of the noise
44 # Higher values will result in smoother noise and
45 # lower values will result in steeper curves.
48 var period
= 1.0 is writable
50 # Amplitude of the values returned by `[]`
51 fun amplitude
: Float do return max
- min
53 # Set the desired amplitude of the values returned by `[]`
55 # Will only modify `max`, `min` stays the same.
56 fun amplitude
=(value
: Float) do max
= min
+ value
58 # Frequency of this noise
59 fun frequency
: Float do return 1.0/period
61 # Set the frequency if this noise
62 fun frequency
=(value
: Float) do period
= 1.0/value
64 # Seed to the random number generator `gradient_vector`
66 # By default, `seed` has a random value created with `Int::rand`.
67 var seed
: Int = 19559.rand
is lazy
, writable
70 # 2D Perlin noise generator using layered `InterpolatedNoise`
72 # Get values at any coordinates with `[]`.
73 # The behavior of this generator can be customized using its attributes `min`,
74 # `max`, `period` and `seed`.
76 # This noise is more realistic and less smooth than the `InterpolatedNoise`.
78 # Due to implementation logic, the full amplitude cannot be reached.
79 # In practice, only `amplitude * (1.0 - 1.0 / n_levels)` is covered.
81 # This implementation uses a custom deterministic pseudo random number
82 # generator to set `InterpolatedNoise::seed` of the `layers`.
83 # It is seeded with the local `seed` and can be further customized by
84 # redefining `pseudo_random`.
85 # This process do not require any state, so this class only holds the
86 # attributes of the generator and does not keep any generated data.
91 # var map = new PerlinNoise
99 # for y in 30.times do
100 # for x in 70.times do
101 # # Get a value at x, y
102 # var val = map[x.to_f, y.to_f]
103 # printn val.to_i.to_hex
110 # assert max <= map.max
111 # assert min >= map.min
114 # ## Result at seed == 0
117 # 7778887888899999888777666666666666666667777877776666666677777777788888
118 # 7788877788899999998877776666666666666667777777766666666667777777778888
119 # 77877777888999aa999888777777776666666667777777666666666666667777778888
120 # 7777777888899aaaa99988877777776666666667777777666666666666667777778888
121 # 7777777888899aaaa99998877777776666666667777777666666666666666777778888
122 # 7777778888999aaaa99999887777777666666666777667666666666656667777788888
123 # 7777778888899aaaa99999888877776666666666666666666666666555666777888888
124 # 7777778888999aaaa99999888887777666666666666666666666666555666778888888
125 # 6677778888999aaaa99999888887777666666677766666666666666555667778888888
126 # 6677788889999aaa999998888877777766666677766667666666666656667788888888
127 # 777788889999999a999998888777777766666677777777766666666666667788888888
128 # 6677888899999999999988888777667776677777777777766666666666666788888888
129 # 6677888999999999999988888776667777777777777777766666665566666788888888
130 # 6777788999999999998888887766667777777777777777666666665566666788888889
131 # 6777788999999999998888877766677777777777777777667666655566666788988999
132 # 7777888999988999988888877766677777777777877777777665555666667788999999
133 # 6778888899888889988888877766666777777777777776666665555666667888999999
134 # 7788888899888888888888877666666777777777777766666665556666677888898999
135 # 7788888899888888888888776666666777777777777666666666566666677888899999
136 # 7788889998888888888877766666666777777777777666666666666666777888899999
137 # 7888999988888888888777666666666777777777776666666666666667777788899988
138 # 7888999988888887777776666666666777777777776666766666666677788888899988
139 # 7788999998888877777776666666666777777777776667776666667778888888899988
140 # 7788999988888777777776666666666777776677766777776677777788888889999988
141 # 6788999988888777777766666666666677676677667777766677777888999999999998
142 # 7788888888888777777776666666666666676677777776666777778889999999999998
143 # 6778888888888777777766666566666666667777666776667777888999999999999999
144 # 6778888888888777777766666566666666667776666776667778889999999999999999
145 # 6778888888777777777666666566666666667776666777677788899999999999888999
146 # 6777787777777777776666666666666666677777677777777888999999999998888899
151 # Desired number of `layers`
153 # This attribute must be assigned before any call to `layers` or `[]`.
155 # By default, it is the highest integer under the logarithm base 2
156 # of `amplitude`, or 4, whichever is the highest.
157 var n_layers
: Int = 4.max
(amplitude
.abs
.log_base
(2.0).to_i
) is lazy
, writable
159 # Layers of `InterpolatedNoise` composing `self`
160 var layers
: Array[InterpolatedNoise] is lazy
do
161 var layers
= new Array[InterpolatedNoise]
167 for l
in n_layers
.times
do
170 seed
= pseudo_random
(seed
)
172 var layer
= new InterpolatedNoise
175 layer
.period
= period
179 period
= period
/ 2.0
187 for layer
in layers
do
193 # Deterministic pseudo random number generator
195 # Used to get seeds for layers from the previous layers or `seed`.
196 protected fun pseudo_random
(value
: Int): Int
198 return value
+ 2935391 % 954847
202 # Simple interpolated noise
204 # Generates smoother noise than `PerlinNoise`.
206 # Each coordinates at a multiple of `period` defines a random vector and
207 # values in between are interpolated from these vectors.
209 # This implementation uses a custom deterministic pseudo random number
210 # generator seeded with `seed`.
211 # It can be further customized by redefining `gradient_vector`.
212 # This process do not require any state, so this class only holds the
213 # attributes of the generator and does not keep any generated data.
218 # var map = new InterpolatedNoise
226 # for y in 30.times do
227 # for x in 70.times do
228 # # Get a value at x, y
229 # var val = map[x.to_f, y.to_f]
230 # printn val.to_i.to_hex
237 # assert max <= map.max
238 # assert min >= map.min
241 # ## Result at seed == 0
244 # 8777766666666666777787777666666666667777888999aaaaaaaaa999888777766666
245 # 88877776666666667777777766666666666667777889999aaaaaaa9999887777666666
246 # 98887777776666667777777666666666666666777889999aaaaaaa9999887776666666
247 # 998888777777766777777776666666666666667778889999aaaaa99998887776666666
248 # a998888777777777777777666666666666666667778899999aaa999998877766666666
249 # aa99888877777777777777666666655566666667778899999999999998877766666665
250 # aa99988877777777777777666666555556666667778889999999999988877766666655
251 # aaa9988887777777777777666665555555666667778889999999999988877766666555
252 # baa9998887777777777776666665555555666666778889999999999988877666666555
253 # baa9998887777777777776666665555555666666778889999999999988877666666555
254 # baa9998887777777777776666655555555566666778889999999999988877666665555
255 # baa9998887777777777776666665555555666666778889999999999988877666666555
256 # baa9998887777777777776666665555555666666778889999999999988877666666555
257 # aaa9988887777777777777666665555555666667778889999999999988877766666555
258 # aa99988877777777777777666666555556666667778889999999999988877766666655
259 # aa99888877777777777777666666655566666667778899999999999998877766666665
260 # a998888777777777777777666666666666666667778899999aaa999998877766666666
261 # 998888777777766777777776666666666666667778889999aaaaa99998887776666666
262 # 98887777776666667777777666666666666666777889999aaaaaaa9999887776666666
263 # 88877776666666667777777766666666666667777889999aaaaaaa9999887777666666
264 # 8777766666666666777787777666666666667777888999aaaaaaaaa999888777766666
265 # 777766666666666667777777777666677777778889999aaaaaaaaaa999887777666666
266 # 7776666666666666667777777777777777778888999aaaaaaaaaaa9999887776666666
267 # 777666666666666666777777777777777888889999aaaaaaaaaaaa9998887776666666
268 # 7766666666666666666777777777778888889999aaaaaaaaaaaaa99998877766666666
269 # 776666666555666666677777777888888899999aaaaaaaaaaaaaa99998877766666665
270 # 7766666655555666666777777788888899999aaaaaaaaaaaaaaa999988877766666655
271 # 776666655555556666677777788888999999aaaaaaaaaaaaaaaa999988877766666555
272 # 7666666555555566666677778888899999aaaaaabbbbaaaaaaa9999888777666666555
273 # 766666655555556666667778888899999aaaaaabbbbaaaaaaa99999888777666666555
275 class InterpolatedNoise
283 # Get grid coordinates
284 var x0
= if x
> 0.0 then x
.to_i
else x
.to_i
- 1
286 var y0
= if y
> 0.0 then y
.to_i
else y
.to_i
- 1
294 var n0
= gradient_dot_product
(x0
, y0
, x
, y
)
295 var n1
= gradient_dot_product
(x1
, y0
, x
, y
)
296 var ix0
= sx
.lerp
(n0
, n1
)
297 n0
= gradient_dot_product
(x0
, y1
, x
, y
)
298 n1
= gradient_dot_product
(x1
, y1
, x
, y
)
299 var ix1
= sx
.lerp
(n0
, n1
)
300 var val
= sy
.lerp
(ix0
, ix1
)
302 # Return value in [min...max] from val in [-0.5...0.5]
304 return val
.lerp
(min
, max
)
307 # Get the gradient vector at `x`, `y` on the axis `w`
309 # Returns a value between 0.0 and 1.0.
311 # Require: `w == 0 or w == 1`
312 protected fun gradient_vector
(x
, y
, w
: Int): Float
314 assert w
== 0 or w
== 1
316 # Use our own deterministic pseudo random number generator
318 # These magic prime numbers were determined good enough by
319 # non-emperical experimentation. They may need to be changed/improved.
320 var i
= 17957*seed
+ 45127*x
+ 22613*y
+ 36919*w
323 return (i
%mod
).to_f
/mod
.to_f
326 private fun gradient_dot_product
(ix
, iy
: Int, x
, y
: Float): Float
331 return dx
*gradient_vector
(ix
, iy
, 0) + dy
*gradient_vector
(ix
, iy
, 1)