-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimplexGaborNoiseIso.java
470 lines (407 loc) · 18 KB
/
SimplexGaborNoiseIso.java
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* K.jpg's Simplex-Gabor Noise, isotropic version.
* Gabor-reminiscent noise, based on OpenSimplex 2, smooth variant ("SuperSimplex")
*
* Notes:
* - Only supports basic (and approximated) sinusoidal kernels.
* - Not normalized in its current version.
* - For a slight speed improvement, you may be able to remove
* `+ permOffset2[pzi]` and `+ permOffset3[pzi]`, which just
* vary the phase offset of the kernels to increase variety.
*/
public class SimplexGaborNoiseIso {
private static final int PSIZE = 2048;
private static final int PMASK = 2047;
private short[] perm;
private Grad2[] permGrad2;
private Grad3[] permGrad3;
private double[] permOffset2;
private double[] permOffset3;
public SimplexGaborNoiseIso(long seed) {
perm = new short[PSIZE];
permGrad2 = new Grad2[PSIZE];
permGrad3 = new Grad3[PSIZE];
permOffset2 = new double[PSIZE];
permOffset3 = new double[PSIZE];
short[] source = new short[PSIZE];
for (short i = 0; i < PSIZE; i++)
source[i] = i;
for (int i = PSIZE - 1; i >= 0; i--) {
seed = seed * 6364136223846793005L + 1442695040888963407L;
int r = (int)((seed + 31) % (i + 1));
if (r < 0)
r += (i + 1);
perm[i] = source[r];
permGrad2[i] = GRADIENTS_2D[perm[i]];
permGrad3[i] = GRADIENTS_3D[perm[i]];
permOffset2[i] = OFFSETS_2D[perm[i]];
permOffset3[i] = OFFSETS_3D[perm[i]];
source[r] = source[i];
}
}
/*
* Noise Evaluators
*/
/**
* 2D SuperSimplex noise, standard lattice orientation.
*/
public double noise2(double x, double y, double waveFreq) {
// Get points for A2* lattice
double s = 0.366025403784439 * (x + y);
double xs = x + s, ys = y + s;
return noise2_Base(xs, ys, waveFreq);
}
/**
* 2D SuperSimplex noise, with Y pointing down the main diagonal.
* Might be better for a 2D sandbox style game, where Y is vertical.
* Probably slightly less optimal for heightmaps or continent maps.
*/
public double noise2_XBeforeY(double x, double y, double waveFreq) {
// Skew transform and rotation baked into one.
double xx = x * 0.7071067811865476;
double yy = y * 1.224744871380249;
return noise2_Base(yy + xx, yy - xx, waveFreq);
}
/**
* 2D SuperSimplex noise base.
* Lookup table implementation inspired by DigitalShadow.
*/
private double noise2_Base(double xs, double ys, double waveFreq) {
double value = 0;
// Get base points and offsets
int xsb = fastFloor(xs), ysb = fastFloor(ys);
double xsi = xs - xsb, ysi = ys - ysb;
// Index to point list
int a = (int)(xsi + ysi);
int index =
(a << 2) |
(int)(xsi - ysi / 2 + 1 - a / 2.0) << 3 |
(int)(ysi - xsi / 2 + 1 - a / 2.0) << 4;
double ssi = (xsi + ysi) * -0.211324865405187;
double xi = xsi + ssi, yi = ysi + ssi;
// Point contributions
for (int i = 0; i < 4; i++) {
LatticePoint2D c = LOOKUP_2D[index + i];
double dx = xi + c.dx, dy = yi + c.dy;
double attn = 2.0 / 3.0 - dx * dx - dy * dy;
if (attn <= 0) continue;
int pxm = (xsb + c.xsv) & PMASK, pym = (ysb + c.ysv) & PMASK;
int pyi = perm[pxm] ^ pym;
Grad2 grad = permGrad2[pyi];
double extrapolation = fastPseudoSine((grad.dx * dx + grad.dy * dy) * waveFreq + permOffset2[pyi]);
attn = attn * attn * attn;
value += attn * extrapolation;
}
return value;
}
/**
* 3D Re-oriented 8-point BCC noise, classic orientation
* Proper substitute for what 3D SuperSimplex would be,
* in light of Forbidden Formulae.
* Use noise3_XYBeforeZ or noise3_XZBeforeY instead, wherever appropriate.
*/
public double noise3_Classic(double x, double y, double z, double waveFreq) {
// Re-orient the cubic lattices via rotation, to produce the expected look on cardinal planar slices.
// If texturing objects that don't tend to have cardinal plane faces, you could even remove this.
// Orthonormal rotation. Not a skew transform.
double r = (2.0 / 3.0) * (x + y + z);
double xr = r - x, yr = r - y, zr = r - z;
// Evaluate both lattices to form a BCC lattice.
return noise3_BCC(xr, yr, zr, waveFreq);
}
/**
* 3D Re-oriented 8-point BCC noise, with better visual isotropy in (X, Y).
* Recommended for 3D terrain and time-varied animations.
* The Z coordinate should always be the "different" coordinate in your use case.
* If Y is vertical in world coordinates, call noise3_XYBeforeZ(x, z, Y) or use noise3_XZBeforeY.
* If Z is vertical in world coordinates, call noise3_XYBeforeZ(x, y, Z).
* For a time varied animation, call noise3_XYBeforeZ(x, y, T).
*/
public double noise3_XYBeforeZ(double x, double y, double z, double waveFreq) {
// Re-orient the cubic lattices without skewing, to make X and Y triangular like 2D.
// Orthonormal rotation. Not a skew transform.
double xy = x + y;
double s2 = xy * -0.211324865405187;
double zz = z * 0.577350269189626;
double xr = x + s2 - zz, yr = y + s2 - zz;
double zr = xy * 0.577350269189626 + zz;
// Evaluate both lattices to form a BCC lattice.
return noise3_BCC(xr, yr, zr, waveFreq);
}
/**
* 3D Re-oriented 8-point BCC noise, with better visual isotropy in (X, Z).
* Recommended for 3D terrain and time-varied animations.
* The Y coordinate should always be the "different" coordinate in your use case.
* If Y is vertical in world coordinates, call noise3_XZBeforeY(x, Y, z).
* If Z is vertical in world coordinates, call noise3_XZBeforeY(x, Z, y) or use noise3_XYBeforeZ.
* For a time varied animation, call noise3_XZBeforeY(x, T, y) or use noise3_XYBeforeZ.
*/
public double noise3_XZBeforeY(double x, double y, double z, double waveFreq) {
// Re-orient the cubic lattices without skewing, to make X and Z triangular like 2D.
// Orthonormal rotation. Not a skew transform.
double xz = x + z;
double s2 = xz * -0.211324865405187;
double yy = y * 0.577350269189626;
double xr = x + s2 - yy; double zr = z + s2 - yy;
double yr = xz * 0.577350269189626 + yy;
// Evaluate both lattices to form a BCC lattice.
return noise3_BCC(xr, yr, zr, waveFreq);
}
/**
* Generate overlapping cubic lattices for 3D Re-oriented BCC noise.
* Lookup table implementation inspired by DigitalShadow.
* It was actually faster to narrow down the points in the loop itself,
* than to build up the index with enough info to isolate 8 points.
*/
private double noise3_BCC(double xr, double yr, double zr, double waveFreq) {
// Get base and offsets inside cube of first lattice.
int xrb = fastFloor(xr), yrb = fastFloor(yr), zrb = fastFloor(zr);
double xri = xr - xrb, yri = yr - yrb, zri = zr - zrb;
// Identify which octant of the cube we're in. This determines which cell
// in the other cubic lattice we're in, and also narrows down one point on each.
int xht = (int)(xri + 0.5), yht = (int)(yri + 0.5), zht = (int)(zri + 0.5);
int index = (xht << 0) | (yht << 1) | (zht << 2);
// Point contributions
double value = 0;
LatticePoint3D c = LOOKUP_3D[index];
while (c != null) {
double dxr = xri + c.dxr, dyr = yri + c.dyr, dzr = zri + c.dzr;
double attn = 0.75 - dxr * dxr - dyr * dyr - dzr * dzr;
if (attn < 0) {
c = c.nextOnFailure;
} else {
int pxm = (xrb + c.xrv) & PMASK, pym = (yrb + c.yrv) & PMASK, pzm = (zrb + c.zrv) & PMASK;
int pzi = perm[perm[pxm] ^ pym] ^ pzm;
Grad3 grad = permGrad3[pzi];
double extrapolation = fastPseudoSine((grad.dx * dxr + grad.dy * dyr + grad.dz * dzr) * waveFreq + permOffset3[pzi]);
attn = attn * attn * attn;
value += attn * extrapolation;
c = c.nextOnSuccess;
}
}
return value;
}
/*
* Utility
*/
private static int fastFloor(double x) {
int xi = (int)x;
return x < xi ? xi - 1 : xi;
}
private static double fastPseudoSine(double x) {
int xb = fastFloor(x);
double t = x - xb;
double part = t * (1 + t * t * (-2 + t));
return (xb & 1) == 0 ? part : -part;
}
/*
* Definitions
*/
private static final LatticePoint2D[] LOOKUP_2D;
private static final LatticePoint3D[] LOOKUP_3D;
static {
LOOKUP_2D = new LatticePoint2D[8 * 4];
LOOKUP_3D = new LatticePoint3D[8];
for (int i = 0; i < 8; i++) {
int i1, j1, i2, j2;
if ((i & 1) == 0) {
if ((i & 2) == 0) { i1 = -1; j1 = 0; } else { i1 = 1; j1 = 0; }
if ((i & 4) == 0) { i2 = 0; j2 = -1; } else { i2 = 0; j2 = 1; }
} else {
if ((i & 2) != 0) { i1 = 2; j1 = 1; } else { i1 = 0; j1 = 1; }
if ((i & 4) != 0) { i2 = 1; j2 = 2; } else { i2 = 1; j2 = 0; }
}
LOOKUP_2D[i * 4 + 0] = new LatticePoint2D(0, 0);
LOOKUP_2D[i * 4 + 1] = new LatticePoint2D(1, 1);
LOOKUP_2D[i * 4 + 2] = new LatticePoint2D(i1, j1);
LOOKUP_2D[i * 4 + 3] = new LatticePoint2D(i2, j2);
}
for (int i = 0; i < 8; i++) {
int i1, j1, k1, i2, j2, k2;
i1 = (i >> 0) & 1; j1 = (i >> 1) & 1; k1 = (i >> 2) & 1;
i2 = i1 ^ 1; j2 = j1 ^ 1; k2 = k1 ^ 1;
// The two points within this octant, one from each of the two cubic half-lattices.
LatticePoint3D c0 = new LatticePoint3D(i1, j1, k1, 0);
LatticePoint3D c1 = new LatticePoint3D(i1 + i2, j1 + j2, k1 + k2, 1);
// (1, 0, 0) vs (0, 1, 1) away from octant.
LatticePoint3D c2 = new LatticePoint3D(i1 ^ 1, j1, k1, 0);
LatticePoint3D c3 = new LatticePoint3D(i1, j1 ^ 1, k1 ^ 1, 0);
// (1, 0, 0) vs (0, 1, 1) away from octant, on second half-lattice.
LatticePoint3D c4 = new LatticePoint3D(i1 + (i2 ^ 1), j1 + j2, k1 + k2, 1);
LatticePoint3D c5 = new LatticePoint3D(i1 + i2, j1 + (j2 ^ 1), k1 + (k2 ^ 1), 1);
// (0, 1, 0) vs (1, 0, 1) away from octant.
LatticePoint3D c6 = new LatticePoint3D(i1, j1 ^ 1, k1, 0);
LatticePoint3D c7 = new LatticePoint3D(i1 ^ 1, j1, k1 ^ 1, 0);
// (0, 1, 0) vs (1, 0, 1) away from octant, on second half-lattice.
LatticePoint3D c8 = new LatticePoint3D(i1 + i2, j1 + (j2 ^ 1), k1 + k2, 1);
LatticePoint3D c9 = new LatticePoint3D(i1 + (i2 ^ 1), j1 + j2, k1 + (k2 ^ 1), 1);
// (0, 0, 1) vs (1, 1, 0) away from octant.
LatticePoint3D cA = new LatticePoint3D(i1, j1, k1 ^ 1, 0);
LatticePoint3D cB = new LatticePoint3D(i1 ^ 1, j1 ^ 1, k1, 0);
// (0, 0, 1) vs (1, 1, 0) away from octant, on second half-lattice.
LatticePoint3D cC = new LatticePoint3D(i1 + i2, j1 + j2, k1 + (k2 ^ 1), 1);
LatticePoint3D cD = new LatticePoint3D(i1 + (i2 ^ 1), j1 + (j2 ^ 1), k1 + k2, 1);
// First two points are guaranteed.
c0.nextOnFailure = c0.nextOnSuccess = c1;
c1.nextOnFailure = c1.nextOnSuccess = c2;
// If c2 is in range, then we know c3 and c4 are not.
c2.nextOnFailure = c3; c2.nextOnSuccess = c5;
c3.nextOnFailure = c4; c3.nextOnSuccess = c4;
// If c4 is in range, then we know c5 is not.
c4.nextOnFailure = c5; c4.nextOnSuccess = c6;
c5.nextOnFailure = c5.nextOnSuccess = c6;
// If c6 is in range, then we know c7 and c8 are not.
c6.nextOnFailure = c7; c6.nextOnSuccess = c9;
c7.nextOnFailure = c8; c7.nextOnSuccess = c8;
// If c8 is in range, then we know c9 is not.
c8.nextOnFailure = c9; c8.nextOnSuccess = cA;
c9.nextOnFailure = c9.nextOnSuccess = cA;
// If cA is in range, then we know cB and cC are not.
cA.nextOnFailure = cB; cA.nextOnSuccess = cD;
cB.nextOnFailure = cC; cB.nextOnSuccess = cC;
// If cC is in range, then we know cD is not.
cC.nextOnFailure = cD; cC.nextOnSuccess = null;
cD.nextOnFailure = cD.nextOnSuccess = null;
LOOKUP_3D[i] = c0;
}
}
private static class LatticePoint2D {
int xsv, ysv;
double dx, dy;
public LatticePoint2D(int xsv, int ysv) {
this.xsv = xsv; this.ysv = ysv;
double ssv = (xsv + ysv) * -0.211324865405187;
this.dx = -xsv - ssv;
this.dy = -ysv - ssv;
}
}
private static class LatticePoint3D {
public double dxr, dyr, dzr;
public int xrv, yrv, zrv;
LatticePoint3D nextOnFailure, nextOnSuccess;
public LatticePoint3D(int xrv, int yrv, int zrv, int lattice) {
this.dxr = -xrv + lattice * 0.5; this.dyr = -yrv + lattice * 0.5; this.dzr = -zrv + lattice * 0.5;
this.xrv = xrv + lattice * 1024; this.yrv = yrv + lattice * 1024; this.zrv = zrv + lattice * 1024;
}
}
/*
* Gradients
*/
public static class Grad2 {
double dx, dy;
public Grad2(double dx, double dy) {
this.dx = dx; this.dy = dy;
}
}
public static class Grad3 {
double dx, dy, dz;
public Grad3(double dx, double dy, double dz) {
this.dx = dx; this.dy = dy; this.dz = dz;
}
}
private static final Grad2[] GRADIENTS_2D;
private static final Grad3[] GRADIENTS_3D;
private static final double[] OFFSETS_2D;
private static final double[] OFFSETS_3D;
static {
GRADIENTS_2D = new Grad2[PSIZE];
OFFSETS_2D = new double[PSIZE];
Grad2[] grad2 = {
new Grad2( 0.130526192220052, 0.99144486137381),
new Grad2( 0.38268343236509, 0.923879532511287),
new Grad2( 0.608761429008721, 0.793353340291235),
new Grad2( 0.793353340291235, 0.608761429008721),
new Grad2( 0.923879532511287, 0.38268343236509),
new Grad2( 0.99144486137381, 0.130526192220051),
new Grad2( 0.99144486137381, -0.130526192220051),
new Grad2( 0.923879532511287, -0.38268343236509),
new Grad2( 0.793353340291235, -0.60876142900872),
new Grad2( 0.608761429008721, -0.793353340291235),
new Grad2( 0.38268343236509, -0.923879532511287),
new Grad2( 0.130526192220052, -0.99144486137381),
new Grad2(-0.130526192220052, -0.99144486137381),
new Grad2(-0.38268343236509, -0.923879532511287),
new Grad2(-0.608761429008721, -0.793353340291235),
new Grad2(-0.793353340291235, -0.608761429008721),
new Grad2(-0.923879532511287, -0.38268343236509),
new Grad2(-0.99144486137381, -0.130526192220052),
new Grad2(-0.99144486137381, 0.130526192220051),
new Grad2(-0.923879532511287, 0.38268343236509),
new Grad2(-0.793353340291235, 0.608761429008721),
new Grad2(-0.608761429008721, 0.793353340291235),
new Grad2(-0.38268343236509, 0.923879532511287),
new Grad2(-0.130526192220052, 0.99144486137381)
};
Grad2[] grad2XBeforeY = new Grad2[grad2.length];
/*for (int i = 0; i < grad2.length; i++) {
double norm = Math.sqrt(grad2[i].dx * grad2[i].dx + grad2[i].dy * grad2[i].dy);
grad2[i].dx /= norm; grad2[i].dy /= norm;
}*/
double offsetNormalize2 = 1.0 / Math.round(PSIZE * 1.0 / grad2.length);
for (int i = 0; i < PSIZE; i++) {
GRADIENTS_2D[i] = grad2[i % grad2.length];
OFFSETS_2D[i] = (((i / grad2.length) * offsetNormalize2) % 1) * 2;
}
GRADIENTS_3D = new Grad3[PSIZE];
OFFSETS_3D = new double[PSIZE];
Grad3[] grad3 = {
new Grad3(-2.22474487139, -2.22474487139, -1.0),
new Grad3(-2.22474487139, -2.22474487139, 1.0),
new Grad3(-3.0862664687972017, -1.1721513422464978, 0.0),
new Grad3(-1.1721513422464978, -3.0862664687972017, 0.0),
new Grad3(-2.22474487139, -1.0, -2.22474487139),
new Grad3(-2.22474487139, 1.0, -2.22474487139),
new Grad3(-1.1721513422464978, 0.0, -3.0862664687972017),
new Grad3(-3.0862664687972017, 0.0, -1.1721513422464978),
new Grad3(-2.22474487139, -1.0, 2.22474487139),
new Grad3(-2.22474487139, 1.0, 2.22474487139),
new Grad3(-3.0862664687972017, 0.0, 1.1721513422464978),
new Grad3(-1.1721513422464978, 0.0, 3.0862664687972017),
new Grad3(-2.22474487139, 2.22474487139, -1.0),
new Grad3(-2.22474487139, 2.22474487139, 1.0),
new Grad3(-1.1721513422464978, 3.0862664687972017, 0.0),
new Grad3(-3.0862664687972017, 1.1721513422464978, 0.0),
new Grad3(-1.0, -2.22474487139, -2.22474487139),
new Grad3( 1.0, -2.22474487139, -2.22474487139),
new Grad3( 0.0, -3.0862664687972017, -1.1721513422464978),
new Grad3( 0.0, -1.1721513422464978, -3.0862664687972017),
new Grad3(-1.0, -2.22474487139, 2.22474487139),
new Grad3( 1.0, -2.22474487139, 2.22474487139),
new Grad3( 0.0, -1.1721513422464978, 3.0862664687972017),
new Grad3( 0.0, -3.0862664687972017, 1.1721513422464978),
new Grad3(-1.0, 2.22474487139, -2.22474487139),
new Grad3( 1.0, 2.22474487139, -2.22474487139),
new Grad3( 0.0, 1.1721513422464978, -3.0862664687972017),
new Grad3( 0.0, 3.0862664687972017, -1.1721513422464978),
new Grad3(-1.0, 2.22474487139, 2.22474487139),
new Grad3( 1.0, 2.22474487139, 2.22474487139),
new Grad3( 0.0, 3.0862664687972017, 1.1721513422464978),
new Grad3( 0.0, 1.1721513422464978, 3.0862664687972017),
new Grad3( 2.22474487139, -2.22474487139, -1.0),
new Grad3( 2.22474487139, -2.22474487139, 1.0),
new Grad3( 1.1721513422464978, -3.0862664687972017, 0.0),
new Grad3( 3.0862664687972017, -1.1721513422464978, 0.0),
new Grad3( 2.22474487139, -1.0, -2.22474487139),
new Grad3( 2.22474487139, 1.0, -2.22474487139),
new Grad3( 3.0862664687972017, 0.0, -1.1721513422464978),
new Grad3( 1.1721513422464978, 0.0, -3.0862664687972017),
new Grad3( 2.22474487139, -1.0, 2.22474487139),
new Grad3( 2.22474487139, 1.0, 2.22474487139),
new Grad3( 1.1721513422464978, 0.0, 3.0862664687972017),
new Grad3( 3.0862664687972017, 0.0, 1.1721513422464978),
new Grad3( 2.22474487139, 2.22474487139, -1.0),
new Grad3( 2.22474487139, 2.22474487139, 1.0),
new Grad3( 3.0862664687972017, 1.1721513422464978, 0.0),
new Grad3( 1.1721513422464978, 3.0862664687972017, 0.0)
};
for (int i = 0; i < grad3.length; i++) {
double norm = Math.sqrt(grad3[i].dx * grad3[i].dx + grad3[i].dy * grad3[i].dy + grad3[i].dz * grad3[i].dz);
grad3[i].dx /= norm; grad3[i].dy /= norm; grad3[i].dz /= norm;
}
double offsetNormalize3 = 1.0 / Math.round(PSIZE * 1.0 / grad3.length);
for (int i = 0; i < PSIZE; i++) {
GRADIENTS_3D[i] = grad3[i % grad3.length];
OFFSETS_3D[i] = (((i / grad3.length) * offsetNormalize3) % 1) * 2;
}
}
}