Skip to content

Commit c8eeaea

Browse files
committed
tiling noise protocols
1 parent af861d7 commit c8eeaea

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

sources/noise/cell.swift

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ protocol _CellNoise2D
33
{
44
var frequency:Double { get }
55
var amplitude:Double { get }
6-
func distance2(from sample_point:Math.DoubleV2, generating_point:Math.IntV2) -> Double
6+
7+
func hash(point:Math.IntV2) -> Int
78
}
89

910
extension _CellNoise2D
1011
{
1112
@inline(__always)
12-
func _distance2(hash:Int, sample_point:Math.DoubleV2, generating_point:Math.IntV2) -> Double
13+
private
14+
func distance2(from sample_point:Math.DoubleV2, generating_point:Math.IntV2) -> Double
1315
{
16+
let hash:Int = self.hash(point: generating_point)
1417
// hash is within 0 ... 255, take it to 0 ... 0.5
1518

1619
// Notice that we have 256 possible hashes, and therefore 8 bits of entropy,
@@ -25,35 +28,6 @@ extension _CellNoise2D
2528
let dv:Math.DoubleV2 = Math.sub(Math.add(Math.cast_double(generating_point), dp), sample_point)
2629
return Math.dot(dv, dv)
2730
}
28-
}
29-
30-
public
31-
struct CellNoise2D:_CellNoise2D, HashedNoise
32-
{
33-
let permutation_table:PermutationTable,
34-
amplitude:Double,
35-
frequency:Double
36-
37-
init(amplitude:Double, frequency:Double, permutation_table:PermutationTable)
38-
{
39-
self.amplitude = amplitude
40-
self.frequency = frequency
41-
self.permutation_table = permutation_table
42-
}
43-
44-
public
45-
init(amplitude:Double, frequency:Double, seed:Int = 0)
46-
{
47-
self.amplitude = amplitude * 1/2
48-
self.frequency = frequency
49-
self.permutation_table = PermutationTable(seed: seed)
50-
}
51-
52-
fileprivate
53-
func distance2(from sample_point:Math.DoubleV2, generating_point:Math.IntV2) -> Double
54-
{
55-
return self._distance2(hash: self.permutation_table.hash(generating_point), sample_point: sample_point, generating_point: generating_point)
56-
}
5731

5832
// still here to make tests run, because of a linker bug in the Swift compiler
5933
public
@@ -211,6 +185,35 @@ struct CellNoise2D:_CellNoise2D, HashedNoise
211185
}
212186
}
213187

188+
public
189+
struct CellNoise2D:_CellNoise2D, HashedNoise
190+
{
191+
let permutation_table:PermutationTable,
192+
amplitude:Double,
193+
frequency:Double
194+
195+
init(amplitude:Double, frequency:Double, permutation_table:PermutationTable)
196+
{
197+
self.amplitude = amplitude
198+
self.frequency = frequency
199+
self.permutation_table = permutation_table
200+
}
201+
202+
public
203+
init(amplitude:Double, frequency:Double, seed:Int = 0)
204+
{
205+
self.amplitude = amplitude * 1/2
206+
self.frequency = frequency
207+
self.permutation_table = PermutationTable(seed: seed)
208+
}
209+
210+
fileprivate
211+
func hash(point:Math.IntV2) -> Int
212+
{
213+
return self.permutation_table.hash(point)
214+
}
215+
}
216+
214217
public
215218
struct CellNoise3D:HashedNoise
216219
{

sources/noise/gradient.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ protocol _ClassicNoise3D
1111
{
1212
var frequency:Double { get }
1313
var amplitude:Double { get }
14-
func gradient(from point:Math.IntV3, at offset:Math.DoubleV3) -> Double
14+
15+
func hash(point:Math.IntV3) -> Int
1516
}
1617

1718
extension _ClassicNoise3D
1819
{
1920
@inline(__always)
20-
func _gradient(hash:Int, offset:Math.DoubleV3) -> Double
21+
private
22+
func gradient(from generating_point:Math.IntV3, at offset:Math.DoubleV3) -> Double
2123
{
2224
// use vectors to the edge of a cube
23-
let h:Int = hash & 15,
25+
let h:Int = self.hash(point: generating_point) & 15,
2426
u:Double = h < 8 ? offset.x : offset.y,
2527
vt:Double = h == 12 || h == 14 ? offset.x : offset.z,
2628
v:Double = h < 4 ? offset.y : vt
@@ -94,9 +96,9 @@ struct ClassicNoise3D:_ClassicNoise3D, HashedNoise
9496
}
9597

9698
fileprivate
97-
func gradient(from point:Math.IntV3, at offset:Math.DoubleV3) -> Double
99+
func hash(point:Math.IntV3) -> Int
98100
{
99-
return self._gradient(hash: self.permutation_table.hash(point), offset: offset)
101+
return self.permutation_table.hash(point)
100102
}
101103
}
102104

@@ -137,9 +139,9 @@ struct ClassicTilingNoise3D:_ClassicNoise3D, HashedTilingNoise
137139
}
138140

139141
fileprivate
140-
func gradient(from point:Math.IntV3, at offset:Math.DoubleV3) -> Double
142+
func hash(point:Math.IntV3) -> Int
141143
{
142-
return self._gradient(hash: self.permutation_table.hash(Math.mod(point, self.wavelengths)), offset: offset)
144+
return self.permutation_table.hash(Math.mod(point, self.wavelengths))
143145
}
144146
}
145147

tests/noise/tests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func banner_classic3d(width:Int, height:Int, seed:Int)
7979
path: "tests/banner_classic3d.png")
8080
}
8181

82+
/*
8283
func banner_simplex2d(width:Int, height:Int, seed:Int)
8384
{
8485
color_noise_png(r_noise: SimplexNoise2D(amplitude: 0.5*255, frequency: 0.015, seed: seed),
@@ -89,6 +90,7 @@ func banner_simplex2d(width:Int, height:Int, seed:Int)
8990
value_offset: (0.65*255, 0.65*255, 0.65*255),
9091
path: "tests/banner_simplex2d.png")
9192
}
93+
*/
9294

9395
func banner_supersimplex2d(width:Int, height:Int, seed:Int)
9496
{
@@ -260,7 +262,7 @@ func banners(width:Int, ratio:Double)
260262
{
261263
let height:Int = Int(Double(width) / ratio)
262264
banner_classic3d (width: width, height: height, seed: 6)
263-
banner_simplex2d (width: width, height: height, seed: 6)
265+
//banner_simplex2d (width: width, height: height, seed: 6)
264266
banner_supersimplex2d(width: width, height: height, seed: 8)
265267
banner_supersimplex3d(width: width, height: height, seed: 2)
266268
banner_cell2d (width: width, height: height, seed: 0)

0 commit comments

Comments
 (0)