OpenSimplex2F constructor
OpenSimplex2F(
- int seed
Creates a seeded OpenSimplex2F that can be used to evaluate noise.
Implementation
OpenSimplex2F(int seed) {
if (!_staticInitialized) {
_staticInit();
_staticInitialized = true;
}
final source = Int16List(_kSize);
for (var i = 0; i < _kSize; i++) {
source[i] = i;
}
// KdotJPG's implementation uses Java's long here. Long in Java is a
// 64-bit two's complement integer. int in Dart is also a 64-bit two's
// complement integer, however, *only on native* (see https://dart.cn/guides/language/numbers).
// However, we want to support web, i.e. JavaScript, as well with this
// package and therefore we have to use the fixnum Int64 type. See
// https://github.com/dart-lang/sdk/issues/46852#issuecomment-894888740.
var seed64 = Int64(seed);
for (int i = _kSize - 1; i >= 0; i--) {
// KdotJPG's implementation uses long literals here. We can use int
// literals of this size as well in Dart, however, these are too big for
// JavaScript and therefore we have to use int.parse instead.
seed64 = seed64 * Int64.parseInt('6364136223846793005') +
Int64.parseInt('1442695040888963407');
// We know r cannot be bigger than 2047, so we can convert it back to an
// int.
var r = ((seed64 + 31) % (i + 1)).toInt();
if (r < 0) r += i + 1;
_perm[i] = source[r];
_permGrad2[i] = _gradients2d[_perm[i]];
_permGrad3[i] = _gradients3d[_perm[i]];
_permGrad4[i] = _gradients4d[_perm[i]];
source[r] = source[i];
}
}