1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use crate::{
math::{
interpolate::linear,
s_curve::quintic::Quintic,
vectors::{Vector2, Vector3, Vector4},
},
permutationtable::NoiseHasher,
};
use core::f64;
#[inline(always)]
pub fn perlin_1d<NH>(point: f64, hasher: &NH) -> f64
where
NH: NoiseHasher + ?Sized,
{
// Unscaled range of linearly interpolated perlin noise should be (-sqrt(N)/2, sqrt(N)/2).
// Need to invert this value and multiply the unscaled result by the value to get a scaled
// range of (-1, 1).
//
// 1/(sqrt(N)/2), N=1 -> 2/sqrt(1) -> 2
const SCALE_FACTOR: f64 = 2.0;
let corner = point as isize;
let distance = point - corner as f64;
macro_rules! call_gradient(
($x_offset:expr) => {
{
let offset = distance - $x_offset as f64;
match hasher.hash(&[corner + $x_offset]) & 0b1 {
0 => offset, // ( 1 )
1 => -offset, // (-1 )
_ => unreachable!(),
}
}
}
);
let g0 = call_gradient!(0);
let g1 = call_gradient!(1);
let curve = distance.map_quintic();
let result = linear(g0, g1, curve) * SCALE_FACTOR;
// At this point, we should be really damn close to the (-1, 1) range, but some float errors
// could have accumulated, so let's just clamp the results to (-1, 1) to cut off any
// outliers and return it.
result.clamp(-1.0, 1.0)
}
#[inline(always)]
pub fn perlin_2d<NH>(point: Vector2<f64>, hasher: &NH) -> f64
where
NH: NoiseHasher + ?Sized,
{
// Unscaled range of linearly interpolated perlin noise should be (-sqrt(N)/2, sqrt(N)/2).
// Need to invert this value and multiply the unscaled result by the value to get a scaled
// range of (-1, 1).
//
// 1/(sqrt(N)/2), N=2 -> 2/sqrt(2)
const SCALE_FACTOR: f64 = 2.0 / f64::consts::SQRT_2;
let corner = point.floor_to_isize();
let distance = point - corner.numcast().unwrap();
macro_rules! call_gradient(
($x:expr, $y:expr) => {
{
let offset = Vector2::new($x, $y);
let point = distance - offset.numcast().unwrap();
match hasher.hash(&(corner + offset).into_array()) & 0b11 {
0 => point.x + point.y, // ( 1, 1)
1 => -point.x + point.y, // (-1, 1)
2 => point.x - point.y, // ( 1, -1)
3 => -point.x - point.y, // (-1, -1)
_ => unreachable!(),
}
}
}
);
let g00 = call_gradient!(0, 0);
let g10 = call_gradient!(1, 0);
let g01 = call_gradient!(0, 1);
let g11 = call_gradient!(1, 1);
let curve = distance.map_quintic();
let result = linear(
linear(g00, g01, curve.y),
linear(g10, g11, curve.y),
curve.x,
) * SCALE_FACTOR;
// At this point, we should be really damn close to the (-1, 1) range, but some float errors
// could have accumulated, so let's just clamp the results to (-1, 1) to cut off any
// outliers and return it.
result.clamp(-1.0, 1.0)
}
#[inline(always)]
pub fn perlin_3d<NH>(point: Vector3<f64>, hasher: &NH) -> f64
where
NH: NoiseHasher + ?Sized,
{
// Unscaled range of linearly interpolated perlin noise should be (-sqrt(N)/2, sqrt(N)/2).
// Need to invert this value and multiply the unscaled result by the value to get a scaled
// range of (-1, 1).
//
// 1/(sqrt(N)/2), N=3 -> 2/sqrt(3)
// sqrt() is not a const function, so use a high-precision value instead.
// TODO: Replace fixed const values with const fn if sqrt() ever becomes a const function.
// 2/sqrt(3) = 1.1547005383792515290182975610039149112952035025402537520372046529
const SCALE_FACTOR: f64 = 1.154_700_538_379_251_5;
let corner = point.floor_to_isize();
let distance = point - corner.numcast().unwrap();
macro_rules! call_gradient(
($x:expr, $y:expr, $z:expr) => {
{
let offset = Vector3::new($x, $y, $z);
let point = distance - offset.numcast().unwrap();
match hasher.hash(&(corner + offset).into_array()) & 0b1111 {
0 | 12 => point.x + point.y, // ( 1, 1, 0)
1 | 13 => -point.x + point.y, // (-1, 1, 0)
2 => point.x - point.y, // ( 1, -1, 0)
3 => -point.x - point.y, // (-1, -1, 0)
4 => point.x + point.z, // ( 1, 0, 1)
5 => -point.x + point.z, // (-1, 0, 1)
6 => point.x - point.z, // ( 1, 0, -1)
7 => -point.x - point.z, // (-1, 0, -1)
8 => point.y + point.z, // ( 0, 1, 1)
9 | 14 => -point.y + point.z, // ( 0, -1, 1)
10 => point.y - point.z, // ( 0, 1, -1)
11 | 15 => -point.y - point.z, // ( 0, -1, -1)
_ => unreachable!(),
}
}
}
);
let g000 = call_gradient!(0, 0, 0);
let g100 = call_gradient!(1, 0, 0);
let g010 = call_gradient!(0, 1, 0);
let g110 = call_gradient!(1, 1, 0);
let g001 = call_gradient!(0, 0, 1);
let g101 = call_gradient!(1, 0, 1);
let g011 = call_gradient!(0, 1, 1);
let g111 = call_gradient!(1, 1, 1);
let curve = distance.map_quintic();
let result = linear(
linear(
linear(g000, g001, curve.z),
linear(g010, g011, curve.z),
curve.y,
),
linear(
linear(g100, g101, curve.z),
linear(g110, g111, curve.z),
curve.y,
),
curve.x,
) * SCALE_FACTOR;
// At this point, we should be really damn close to the (-1, 1) range, but some float errors
// could have accumulated, so let's just clamp the results to (-1, 1) to cut off any
// outliers and return it.
result.clamp(-1.0, 1.0)
}
#[inline(always)]
pub fn perlin_4d<NH>(point: Vector4<f64>, hasher: &NH) -> f64
where
NH: NoiseHasher + ?Sized,
{
// Unscaled range of linearly interpolated perlin noise should be (-sqrt(N)/2, sqrt(N)/2).
// Need to invert this value and multiply the unscaled result by the value to get a scaled
// range of (-1, 1).
const SCALE_FACTOR: f64 = 1.0; // 1/(sqrt(N)/2), N=4 -> 2/sqrt(4) -> 2/2 -> 1
let corner = point.floor_to_isize();
let distance = point - corner.numcast().unwrap();
macro_rules! call_gradient(
($x:expr, $y:expr, $z:expr, $w:expr) => {
{
let offset = Vector4::new($x, $y, $z, $w);
let point = distance - offset.numcast().unwrap();
match hasher.hash(&(corner + offset).into_array()) & 0b11111 {
0 | 28 => point.x + point.y + point.z, // ( 1, 1, 1, 0)
1 => -point.x + point.y + point.z, // (-1, 1, 1, 0)
2 => point.x - point.y + point.z, // ( 1, -1, 1, 0)
3 => point.x + point.y - point.z, // ( 1, 1, -1, 0)
4 => -point.x + point.y - point.z, // (-1, 1, -1, 0)
5 => point.x - point.y - point.z, // ( 1, -1, -1, 0)
6 => point.x - point.y - point.z, // (-1, -1, -1, 0)
7 | 29 => point.x + point.y + point.w, // ( 1, 1, 0, 1)
8 => -point.x + point.y + point.w, // (-1, 1, 0, 1)
9 => point.x - point.y + point.w, // ( 1, -1, 0, 1)
10 => point.x + point.y - point.w, // ( 1, 1, 0, -1)
11 => point.x + point.y - point.w, // (-1, 1, 0, -1)
12 => point.x + point.y - point.w, // ( 1, -1, 0, -1)
13 => -point.x - point.y - point.w, // (-1, -1, 0, -1)
14 | 30 => point.x + point.z + point.w, // ( 1, 0, 1, 1)
15 => -point.x + point.z + point.w, // (-1, 0, 1, 1)
16 => point.x - point.z + point.w, // ( 1, 0, -1, 1)
17 => point.x + point.z - point.w, // ( 1, 0, 1, -1)
18 => point.x + point.z - point.w, // (-1, 0, 1, -1)
19 => point.x + point.z - point.w, // ( 1, 0, -1, -1)
20 => -point.x - point.z - point.w, // (-1, 0, -1, -1)
21 | 31 => point.y + point.z + point.w, // ( 0, 1, 1, 1)
22 => -point.y + point.z + point.w, // ( 0, -1, 1, 1)
23 => point.y - point.z + point.w, // ( 0, 1, -1, 1)
24 => point.y - point.z - point.w, // ( 0, 1, 1, -1)
25 => -point.y - point.z - point.w, // ( 0, -1, 1, -1)
26 => point.y - point.z - point.w, // ( 0, 1, -1, -1)
27 => -point.y - point.z - point.w, // ( 0, -1, -1, -1)
_ => unreachable!(),
}
}
}
);
let g0000 = call_gradient!(0, 0, 0, 0);
let g1000 = call_gradient!(1, 0, 0, 0);
let g0100 = call_gradient!(0, 1, 0, 0);
let g1100 = call_gradient!(1, 1, 0, 0);
let g0010 = call_gradient!(0, 0, 1, 0);
let g1010 = call_gradient!(1, 0, 1, 0);
let g0110 = call_gradient!(0, 1, 1, 0);
let g1110 = call_gradient!(1, 1, 1, 0);
let g0001 = call_gradient!(0, 0, 0, 1);
let g1001 = call_gradient!(1, 0, 0, 1);
let g0101 = call_gradient!(0, 1, 0, 1);
let g1101 = call_gradient!(1, 1, 0, 1);
let g0011 = call_gradient!(0, 0, 1, 1);
let g1011 = call_gradient!(1, 0, 1, 1);
let g0111 = call_gradient!(0, 1, 1, 1);
let g1111 = call_gradient!(1, 1, 1, 1);
let curve = distance.map_quintic();
let result = linear(
linear(
linear(
linear(g0000, g0001, curve.w),
linear(g0010, g0011, curve.w),
curve.z,
),
linear(
linear(g0100, g0101, curve.w),
linear(g0110, g0111, curve.w),
curve.z,
),
curve.y,
),
linear(
linear(
linear(g1000, g1001, curve.w),
linear(g1010, g1011, curve.w),
curve.z,
),
linear(
linear(g1100, g1101, curve.w),
linear(g1110, g1111, curve.w),
curve.z,
),
curve.y,
),
curve.x,
) * SCALE_FACTOR;
// At this point, we should be really damn close to the (-1, 1) range, but some float errors
// could have accumulated, so let's just clamp the results to (-1, 1) to cut off any
// outliers and return it.
result.clamp(-1.0, 1.0)
}