Skip to content

Commit 94792c7

Browse files
committed
switch permutation generator to XORSHIFT
1 parent ea38cfc commit 94792c7

File tree

7 files changed

+21
-22
lines changed

7 files changed

+21
-22
lines changed

simplex.png

31 KB
Loading

sources/noise/noise.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,23 @@ extension HashedNoise
6767
func table(seed:Int) -> ([Int], [Int])
6868
{
6969
let range:Int = Self.n_hashes
70-
var seed = seed
71-
var perm1024:[Int] = [Int](repeating: 0, count: 1024),
72-
hashes:[Int] = [Int](repeating: 0, count: 1024)
73-
var source:[Int] = Array(0 ..< 1024)
74-
for i in stride(from: 1023, to: 0, by: -1)
70+
var perm1024:[Int] = [Int](0 ..< 1024),
71+
state128:(UInt32, UInt32, UInt32, UInt32) = (1, 0, 0, UInt32(extendingOrTruncating: seed))
72+
for i in 0 ..< 1024 - 1
7573
{
76-
seed = seed &* 6364136223846793005 &+ 1442695040888963407
77-
var r:Int = (seed + 31) % (i + 1)
78-
if r < 0
79-
{
80-
r += i + 1
81-
}
82-
perm1024[i] = source[r]
83-
hashes[i] = perm1024[i] % range
84-
source[r] = source[i]
74+
var t:UInt32 = state128.3
75+
t ^= t &<< 11
76+
t ^= t &>> 8
77+
state128.3 = state128.2
78+
state128.2 = state128.1
79+
state128.1 = state128.0
80+
t ^= state128.0
81+
t ^= state128.0 &>> 19
82+
state128.0 = t
83+
perm1024.swapAt(i, Int(t) & 1023)
8584
}
8685

87-
return (perm1024, hashes)
86+
return (perm1024, perm1024.map{ $0 % range })
8887
}
8988
}
9089

sources/noise/opensimplex.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ struct Simplex2D:Hashed2DGradientNoise
1515
hashes:[Int]
1616

1717
private
18-
let amplitude:Double,
18+
let amplitude:Double, // this is not necissaryly the same amplitude passed into the initializer
1919
frequency:Double
2020

2121
public
2222
init(amplitude:Double, frequency:Double, seed:Int = 0)
2323
{
24-
self.amplitude = 7 * amplitude
24+
self.amplitude = 0.096 * amplitude
2525
self.frequency = frequency
2626
(self.perm1024, self.hashes) = Simplex2D.table(seed: seed)
2727
}

sources/noise/supersimplex.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct SuperSimplex2D:Hashed2DGradientNoise
112112
public
113113
init(amplitude:Double, frequency:Double, seed:Int = 0)
114114
{
115-
self.amplitude = 1275 * amplitude
115+
self.amplitude = 18 * amplitude
116116
self.frequency = frequency
117117
(self.perm1024, self.hashes) = SuperSimplex2D.table(seed: seed)
118118
}
@@ -366,7 +366,7 @@ struct SuperSimplex3D:Hashed3DGradientNoise
366366
public
367367
init(amplitude:Double, frequency:Double, seed:Int = 0)
368368
{
369-
self.amplitude = 700 * amplitude
369+
self.amplitude = 9 * amplitude
370370
self.frequency = frequency
371371
(self.perm1024, self.hashes) = SuperSimplex3D.table(seed: seed)
372372
}

super_simplex.png

54.8 KB
Loading

super_simplex3D.png

66.5 KB
Loading

tests/LinuxMain.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ for point:PoissonSampler.Point in poisson.generate(radius: 10, width: viewer_siz
3737
print(clock() - t0)
3838
try png_encode(path: "poisson.png", raw_data: pixbuf, properties: png_properties)
3939

40-
let S:fBm<Simplex2D> = fBm<Simplex2D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
40+
let S:fBm<Simplex2D> = fBm<Simplex2D>(amplitude: 0.5*127.5, frequency: 0.001, octaves: 10)
4141
t0 = clock()
4242
pixbuf = S.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
4343
print(clock() - t0)
4444
try png_encode(path: "simplex.png", raw_data: pixbuf, properties: png_properties)
4545

4646

47-
let SS:fBm<SuperSimplex2D> = fBm<SuperSimplex2D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
47+
let SS:fBm<SuperSimplex2D> = fBm<SuperSimplex2D>(amplitude: 0.5*127.5, frequency: 0.001, octaves: 10)
4848
t0 = clock()
4949
pixbuf = SS.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
5050
print(clock() - t0)
5151
try png_encode(path: "super_simplex.png", raw_data: pixbuf, properties: png_properties)
5252

53-
let SS3D:fBm<SuperSimplex3D> = fBm<SuperSimplex3D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
53+
let SS3D:fBm<SuperSimplex3D> = fBm<SuperSimplex3D>(amplitude: 0.5*127.5, frequency: 0.001, octaves: 10)
5454
t0 = clock()
5555
pixbuf = SS3D.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
5656
print(clock() - t0)

0 commit comments

Comments
 (0)