1
- fileprivate
2
- enum Const
3
- {
4
- static
5
- let SQUISH_2D : Double = 0.5 * ( 1 / 3 . squareRoot ( ) - 1 ) ,
6
- STRETCH_2D : Double = 0.5 * ( 3 . squareRoot ( ) - 1 )
7
- }
8
-
9
1
fileprivate
10
2
protocol _ClassicNoise3D
11
3
{
@@ -175,6 +167,10 @@ struct TilingClassicNoise3D:_ClassicNoise3D, HashedTilingNoise
175
167
public
176
168
struct SimplexNoise2D : HashedNoise
177
169
{
170
+ private static
171
+ let SQUISH_2D : Double = 0.5 * ( 1 / 3 . squareRoot ( ) - 1 ) ,
172
+ STRETCH_2D : Double = 0.5 * ( 3 . squareRoot ( ) - 1 )
173
+
178
174
private static
179
175
let gradient_table32 : [ Math . DoubleV2 ] =
180
176
[
@@ -234,7 +230,7 @@ struct SimplexNoise2D:HashedNoise
234
230
let sample : Math . DoubleV2 = ( x * self . frequency, y * self . frequency)
235
231
// transform our coordinate system so that the *simplex* (x, y) forms a
236
232
// rectangular grid (u, v)
237
- let squish_offset : Double = ( sample. x + sample. y) * Const . SQUISH_2D,
233
+ let squish_offset : Double = ( sample. x + sample. y) * SimplexNoise2D . SQUISH_2D,
238
234
sample_uv : Math . DoubleV2 = ( sample. x + squish_offset, sample. y + squish_offset)
239
235
240
236
// get integral (u, v) coordinates of the rhombus and get position inside
@@ -264,7 +260,7 @@ struct SimplexNoise2D:HashedNoise
264
260
// do the same in the original (x, y) coordinate space
265
261
266
262
// stretch back to get (x, y) coordinates of rhombus origin
267
- let stretch_offset : Double = Double ( bin. a + bin. b) * Const . STRETCH_2D,
263
+ let stretch_offset : Double = Double ( bin. a + bin. b) * SimplexNoise2D . STRETCH_2D,
268
264
origin : Math . DoubleV2 = ( Double ( bin. a) + stretch_offset, Double ( bin. b) + stretch_offset)
269
265
270
266
// get relative position inside the rhombus relative to (xb, xb)
@@ -279,27 +275,27 @@ struct SimplexNoise2D:HashedNoise
279
275
}
280
276
281
277
// contribution from (1, 0)
282
- _inspect ( point_offset: ( 1 , 0 ) , sample_offset: ( 1 + Const . STRETCH_2D, Const . STRETCH_2D) )
278
+ _inspect ( point_offset: ( 1 , 0 ) , sample_offset: ( 1 + SimplexNoise2D . STRETCH_2D, SimplexNoise2D . STRETCH_2D) )
283
279
284
280
// contribution from (0, 1)
285
- _inspect ( point_offset: ( 0 , 1 ) , sample_offset: ( Const . STRETCH_2D, 1 + Const . STRETCH_2D) )
281
+ _inspect ( point_offset: ( 0 , 1 ) , sample_offset: ( SimplexNoise2D . STRETCH_2D, 1 + SimplexNoise2D . STRETCH_2D) )
286
282
287
283
// decide which triangle we are in
288
284
let uv_sum : Double = sample_uv_rel. x + sample_uv_rel. y
289
285
if ( uv_sum > 1 ) // we are to the bottom-right of the diagonal line (du = 1 - dv)
290
286
{
291
- _inspect ( point_offset: ( 1 , 1 ) , sample_offset: ( 1 + 2 * Const . STRETCH_2D, 1 + 2 * Const . STRETCH_2D) )
287
+ _inspect ( point_offset: ( 1 , 1 ) , sample_offset: ( 1 + 2 * SimplexNoise2D . STRETCH_2D, 1 + 2 * SimplexNoise2D . STRETCH_2D) )
292
288
293
289
let center_dist : Double = 2 - uv_sum
294
290
if center_dist < sample_uv_rel. x || center_dist < sample_uv_rel. y
295
291
{
296
292
if sample_uv_rel. x > sample_uv_rel. y
297
293
{
298
- _inspect ( point_offset: ( 2 , 0 ) , sample_offset: ( 2 + 2 * Const . STRETCH_2D, 2 * Const . STRETCH_2D) )
294
+ _inspect ( point_offset: ( 2 , 0 ) , sample_offset: ( 2 + 2 * SimplexNoise2D . STRETCH_2D, 2 * SimplexNoise2D . STRETCH_2D) )
299
295
}
300
296
else
301
297
{
302
- _inspect ( point_offset: ( 0 , 2 ) , sample_offset: ( 2 * Const . STRETCH_2D, 2 + 2 * Const . STRETCH_2D) )
298
+ _inspect ( point_offset: ( 0 , 2 ) , sample_offset: ( 2 * SimplexNoise2D . STRETCH_2D, 2 + 2 * SimplexNoise2D . STRETCH_2D) )
303
299
}
304
300
}
305
301
else
@@ -325,7 +321,7 @@ struct SimplexNoise2D:HashedNoise
325
321
}
326
322
else
327
323
{
328
- _inspect ( point_offset: ( 1 , 1 ) , sample_offset: ( 1 + 2 * Const . STRETCH_2D, 1 + 2 * Const . STRETCH_2D) )
324
+ _inspect ( point_offset: ( 1 , 1 ) , sample_offset: ( 1 + 2 * SimplexNoise2D . STRETCH_2D, 1 + 2 * SimplexNoise2D . STRETCH_2D) )
329
325
}
330
326
}
331
327
@@ -351,6 +347,10 @@ typealias SuperSimplexNoise2D = GradientNoise2D
351
347
public
352
348
struct GradientNoise2D : HashedNoise
353
349
{
350
+ private static
351
+ let SQUISH_2D : Double = 0.5 * ( 1 / 3 . squareRoot ( ) - 1 ) ,
352
+ STRETCH_2D : Double = 0.5 * ( 3 . squareRoot ( ) - 1 )
353
+
354
354
private static
355
355
let points : [ ( Math . IntV2 , Math . DoubleV2 ) ] =
356
356
{
@@ -360,7 +360,7 @@ struct GradientNoise2D:HashedNoise
360
360
@inline ( __always)
361
361
func _lattice_point( at point: Math . IntV2 ) -> ( Math . IntV2 , Math . DoubleV2 )
362
362
{
363
- let stretch_offset : Double = Double ( point. a + point. b) * Const . SQUISH_2D
363
+ let stretch_offset : Double = Double ( point. a + point. b) * GradientNoise2D . SQUISH_2D
364
364
return ( point, ( Double ( point. a) + stretch_offset, Double ( point. b) + stretch_offset) )
365
365
}
366
366
@@ -437,7 +437,7 @@ struct GradientNoise2D:HashedNoise
437
437
{
438
438
let sample : Math . DoubleV2 = ( x * self . frequency, y * self . frequency)
439
439
// transform our (x, y) coordinate to (u, v) space
440
- let stretch_offset : Double = ( sample. x + sample. y) * Const . STRETCH_2D,
440
+ let stretch_offset : Double = ( sample. x + sample. y) * GradientNoise2D . STRETCH_2D,
441
441
sample_uv : Math . DoubleV2 = ( sample. x + stretch_offset, sample. y + stretch_offset)
442
442
443
443
// (0, 0) ----- (1, 0)
@@ -546,7 +546,7 @@ struct GradientNoise2D:HashedNoise
546
546
*/
547
547
548
548
// get the relative offset from (0, 0)
549
- let squish_offset : Double = ( sample_uv_rel. x + sample_uv_rel. y) * Const . SQUISH_2D,
549
+ let squish_offset : Double = ( sample_uv_rel. x + sample_uv_rel. y) * GradientNoise2D . SQUISH_2D,
550
550
sample_rel : Math . DoubleV2 = ( sample_uv_rel. x + squish_offset, sample_uv_rel. y + squish_offset)
551
551
552
552
var Σ : Double = 0
0 commit comments