@@ -6,38 +6,26 @@ enum Const
6
6
STRETCH_2D : Double = 0.5 * ( 3 . squareRoot ( ) - 1 )
7
7
}
8
8
9
- // UNDOCUMENTED
10
- public
11
- struct ClassicNoise3D : HashedNoise
9
+ fileprivate
10
+ protocol _ClassicNoise3D
12
11
{
13
- let permutation_table : PermutationTable ,
14
- amplitude : Double ,
15
- frequency : Double
16
-
17
- init ( amplitude: Double , frequency: Double , permutation_table: PermutationTable )
18
- {
19
- self . amplitude = amplitude
20
- self . frequency = frequency
21
- self . permutation_table = permutation_table
22
- }
23
-
24
- public
25
- init ( amplitude: Double , frequency: Double , seed: Int = 0 )
26
- {
27
- self . amplitude = 0.982 * amplitude
28
- self . frequency = frequency
29
- self . permutation_table = PermutationTable ( seed: seed)
30
- }
31
-
32
- private
12
+ var frequency : Double { get }
13
+ var amplitude : Double { get }
33
14
func gradient( from point: Math . IntV3 , at offset: Math . DoubleV3 ) -> Double
15
+ }
16
+
17
+ extension _ClassicNoise3D
18
+ {
19
+ @inline ( __always)
20
+ fileprivate
21
+ func _gradient( hash: Int , offset: Math . DoubleV3 ) -> Double
34
22
{
35
23
// use vectors to the edge of a cube
36
- let hash : Int = self . permutation_table . hash ( point ) & 15 ,
37
- u : Double = hash < 8 ? offset. x : offset. y,
38
- vt : Double = hash == 12 || hash == 14 ? offset. x : offset. z,
39
- v : Double = hash < 4 ? offset. y : vt
40
- return ( hash & 1 != 0 ? - u : u) + ( hash & 2 != 0 ? - v : v)
24
+ let h : Int = hash & 15 ,
25
+ u : Double = h < 8 ? offset. x : offset. y,
26
+ vt : Double = h == 12 || h == 14 ? offset. x : offset. z,
27
+ v : Double = h < 4 ? offset. y : vt
28
+ return ( h & 1 != 0 ? - u : u) + ( h & 2 != 0 ? - v : v)
41
29
}
42
30
43
31
public
@@ -83,6 +71,79 @@ struct ClassicNoise3D:HashedNoise
83
71
}
84
72
}
85
73
74
+ // UNDOCUMENTED
75
+ public
76
+ struct ClassicNoise3D : HashedNoise , _ClassicNoise3D
77
+ {
78
+ let permutation_table : PermutationTable ,
79
+ amplitude : Double ,
80
+ frequency : Double
81
+
82
+ init ( amplitude: Double , frequency: Double , permutation_table: PermutationTable )
83
+ {
84
+ self . amplitude = amplitude
85
+ self . frequency = frequency
86
+ self . permutation_table = permutation_table
87
+ }
88
+
89
+ public
90
+ init ( amplitude: Double , frequency: Double , seed: Int = 0 )
91
+ {
92
+ self . amplitude = 0.982 * amplitude
93
+ self . frequency = frequency
94
+ self . permutation_table = PermutationTable ( seed: seed)
95
+ }
96
+
97
+ fileprivate
98
+ func gradient( from point: Math . IntV3 , at offset: Math . DoubleV3 ) -> Double
99
+ {
100
+ return self . _gradient ( hash: self . permutation_table. hash ( point) , offset: offset)
101
+ }
102
+ }
103
+
104
+ // UNDOCUMENTED
105
+ public
106
+ struct ClassicTilingNoise3D : HashedTilingNoise , _ClassicNoise3D
107
+ {
108
+ let permutation_table : PermutationTable ,
109
+ amplitude : Double ,
110
+ frequency : Double ,
111
+ wavelengths : Math . IntV3
112
+
113
+ init ( amplitude: Double , frequency: Double , permutation_table: PermutationTable , wavelengths: Math . IntV3 )
114
+ {
115
+ self . amplitude = amplitude
116
+ self . frequency = frequency
117
+ self . permutation_table = permutation_table
118
+ self . wavelengths = wavelengths
119
+ }
120
+
121
+ public
122
+ init ( amplitude: Double , frequency: Double , wavelengths: Int , seed: Int = 0 )
123
+ {
124
+ self . init ( amplitude: amplitude, frequency: frequency,
125
+ wavelengths_x: wavelengths,
126
+ wavelengths_y: wavelengths,
127
+ wavelengths_z: wavelengths,
128
+ seed: seed)
129
+ }
130
+
131
+ public
132
+ init ( amplitude: Double , frequency: Double , wavelengths_x: Int , wavelengths_y: Int , wavelengths_z: Int , seed: Int = 0 )
133
+ {
134
+ self . amplitude = 0.982 * amplitude
135
+ self . frequency = frequency
136
+ self . permutation_table = PermutationTable ( seed: seed)
137
+ self . wavelengths = ( wavelengths_x, wavelengths_y, wavelengths_z)
138
+ }
139
+
140
+ fileprivate
141
+ func gradient( from point: Math . IntV3 , at offset: Math . DoubleV3 ) -> Double
142
+ {
143
+ return self . _gradient ( hash: self . permutation_table. hash ( Math . mod ( point, self . wavelengths) ) , offset: offset)
144
+ }
145
+ }
146
+
86
147
@available ( * , deprecated, message: " simplex noise nearly identical to and is an inferior implementation of super simplex noise " )
87
148
public
88
149
struct SimplexNoise2D : HashedNoise
0 commit comments