use double precision numbers in love.math.noise

This commit is contained in:
megagrump
2021-11-22 14:57:05 +01:00
parent 9cc98df218
commit 136c3cd277
7 changed files with 146 additions and 157 deletions
+34 -36
View File
@@ -85,7 +85,7 @@ unsigned char SimplexNoise1234::perm[512] = {151,160,137,91,90,15,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
};
//---------------------------------------------------------------------
@@ -103,37 +103,35 @@ unsigned char SimplexNoise1234::perm[512] = {151,160,137,91,90,15,
* float SLnoise = (SimplexNoise1234::noise(x,y,z) + 1.0) * 0.5;
*/
float SimplexNoise1234::grad( int hash, float x ) {
double SimplexNoise1234::grad( int hash, double x ) {
int h = hash & 15;
float grad = 1.0f + (h & 7); // Gradient value 1.0, 2.0, ..., 8.0
double grad = 1.0 + (h & 7); // Gradient value 1.0, 2.0, ..., 8.0
if (h&8) grad = -grad; // Set a random sign for the gradient
return ( grad * x ); // Multiply the gradient with the distance
}
float SimplexNoise1234::grad( int hash, float x, float y ) {
double SimplexNoise1234::grad( int hash, double x, double y ) {
int h = hash & 7; // Convert low 3 bits of hash code
float u = h<4 ? x : y; // into 8 simple gradient directions,
float v = h<4 ? y : x; // and compute the dot product with (x,y).
return ((h&1)? -u : u) + ((h&2)? -2.0f*v : 2.0f*v);
double u = h<4 ? x : y; // into 8 simple gradient directions,
double v = h<4 ? y : x; // and compute the dot product with (x,y).
return ((h&1)? -u : u) + ((h&2)? -2.0*v : 2.0*v);
}
// 1D simplex noise
float SimplexNoise1234::noise(float x) {
float SimplexNoise1234::noise(double x) {
int i0 = FASTFLOOR(x);
int i1 = i0 + 1;
float x0 = x - i0;
float x1 = x0 - 1.0f;
double x0 = x - i0;
double x1 = x0 - 1.0;
float n0, n1;
double n0, n1;
float t0 = 1.0f - x0*x0;
// if(t0 < 0.0f) t0 = 0.0f;
double t0 = 1.0 - x0*x0;
t0 *= t0;
n0 = t0 * t0 * grad(perm[i0 & 0xff], x0);
float t1 = 1.0f - x1*x1;
// if(t1 < 0.0f) t1 = 0.0f;
double t1 = 1.0 - x1*x1;
t1 *= t1;
n1 = t1 * t1 * grad(perm[i1 & 0xff], x1);
// The maximum value of this noise is 8*(3/4)^4 = 2.53125
@@ -143,25 +141,25 @@ float SimplexNoise1234::noise(float x) {
}
// 2D simplex noise
float SimplexNoise1234::noise(float x, float y) {
float SimplexNoise1234::noise(double x, double y) {
#define F2 0.366025403 // F2 = 0.5*(sqrt(3.0)-1.0)
#define G2 0.211324865 // G2 = (3.0-Math.sqrt(3.0))/6.0
float n0, n1, n2; // Noise contributions from the three corners
double n0, n1, n2; // Noise contributions from the three corners
// Skew the input space to determine which simplex cell we're in
float s = (x+y)*F2; // Hairy factor for 2D
float xs = x + s;
float ys = y + s;
double s = (x+y)*F2; // Hairy factor for 2D
double xs = x + s;
double ys = y + s;
int i = FASTFLOOR(xs);
int j = FASTFLOOR(ys);
float t = (float)(i+j)*G2;
float X0 = i-t; // Unskew the cell origin back to (x,y) space
float Y0 = j-t;
float x0 = x-X0; // The x,y distances from the cell origin
float y0 = y-Y0;
double t = (i+j)*G2;
double X0 = i-t; // Unskew the cell origin back to (x,y) space
double Y0 = j-t;
double x0 = x-X0; // The x,y distances from the cell origin
double y0 = y-Y0;
// For the 2D case, the simplex shape is an equilateral triangle.
// Determine which simplex we are in.
@@ -173,32 +171,32 @@ float SimplexNoise1234::noise(float x, float y) {
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
// c = (3-sqrt(3))/6
float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
float y1 = y0 - j1 + G2;
float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
float y2 = y0 - 1.0f + 2.0f * G2;
double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
double y1 = y0 - j1 + G2;
double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
double y2 = y0 - 1.0 + 2.0 * G2;
// Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
int ii = i & 0xff;
int jj = j & 0xff;
// Calculate the contribution from the three corners
float t0 = 0.5f - x0*x0-y0*y0;
if(t0 < 0.0f) n0 = 0.0f;
double t0 = 0.5 - x0*x0-y0*y0;
if(t0 < 0.0) n0 = 0.0;
else {
t0 *= t0;
n0 = t0 * t0 * grad(perm[ii+perm[jj]], x0, y0);
n0 = t0 * t0 * grad(perm[ii+perm[jj]], x0, y0);
}
float t1 = 0.5f - x1*x1-y1*y1;
if(t1 < 0.0f) n1 = 0.0f;
double t1 = 0.5 - x1*x1-y1*y1;
if(t1 < 0.0) n1 = 0.0;
else {
t1 *= t1;
n1 = t1 * t1 * grad(perm[ii+i1+perm[jj+j1]], x1, y1);
}
float t2 = 0.5f - x2*x2-y2*y2;
if(t2 < 0.0f) n2 = 0.0f;
double t2 = 0.5 - x2*x2-y2*y2;
if(t2 < 0.0) n2 = 0.0;
else {
t2 *= t2;
n2 = t2 * t2 * grad(perm[ii+1+perm[jj+1]], x2, y2);