Skip to content

Commit

Permalink
Faster and improved value hashes, fixed cell values not being seeded
Browse files Browse the repository at this point in the history
  • Loading branch information
Auburn committed Feb 3, 2017
1 parent e35385c commit 4d36f87
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
63 changes: 39 additions & 24 deletions FastNoise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,33 +239,30 @@ unsigned char FastNoise::Index4D_256(unsigned char offset, int x, int y, int z,

static float ValCoord2D(int seed, int x, int y)
{
int n = X_PRIME * x;
n += Y_PRIME * y;
n += seed;
n &= 0x7fffffff;
n = (n >> 13) ^ n;
return 9.311924889611565e-10f * (((n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff) - 1073891824);
int n = seed;
n ^= X_PRIME * x;
n ^= Y_PRIME * y;

return (n * n * n * 60493) / 2147483648.f;
}
static float ValCoord3D(int seed, int x, int y, int z)
{
int n = X_PRIME * x;
n += Y_PRIME * y;
n += Z_PRIME * z;
n += seed;
n &= 0x7fffffff;
n = (n >> 13) ^ n;
return 9.311924889611565e-10f * (((n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff) - 1073891824);
int n = seed;
n ^= X_PRIME * x;
n ^= Y_PRIME * y;
n ^= Z_PRIME * z;

return (n * n * n * 60493) / 2147483648.f;
}
static float ValCoord4D(int seed, int x, int y, int z, int w)
{
int n = X_PRIME * x;
n += Y_PRIME * y;
n += Z_PRIME * z;
n += W_PRIME * w;
n += seed;
n &= 0x7fffffff;
n = (n >> 13) ^ n;
return 9.311924889611565e-10f * (((n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff) - 1073891824);
int n = seed;
n ^= X_PRIME * x;
n ^= Y_PRIME * y;
n ^= Z_PRIME * z;
n ^= W_PRIME * w;

return (n * n * n * 60493) / 2147483648.f;
}

float FastNoise::ValCoord2DFast(unsigned char offset, int x, int y)
Expand Down Expand Up @@ -1193,6 +1190,24 @@ float FastNoise::SingleSimplexFractalRigidMulti(float x, float y)
return sum;
}

float FastNoise::SingleSimplexFractalBlend(float x, float y)
{
float sum = SingleSimplex(m_perm[0], x, y);
float amp = 1.0f;
unsigned int i = 0;

while (++i < m_octaves)
{
x *= m_lacunarity;
y *= m_lacunarity;

amp *= m_gain;
sum *= SingleSimplex(m_perm[i], x, y) * amp + 1.f;
}

return sum * m_fractalBounding;
}

float FastNoise::GetSimplex(float x, float y)
{
return SingleSimplex(0, x * m_frequency, y * m_frequency);
Expand Down Expand Up @@ -1263,7 +1278,7 @@ float FastNoise::GetSimplex(float x, float y, float z, float w)
return SingleSimplex(0, x * m_frequency, y * m_frequency, z * m_frequency, w * m_frequency);
}

static const int SIMPLEX_4D[] =
static const unsigned char SIMPLEX_4D[] =
{
0,1,2,3,0,1,3,2,0,0,0,0,0,2,3,1,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,0,
0,2,1,3,0,0,0,0,0,3,1,2,0,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,1,3,2,0,
Expand Down Expand Up @@ -1483,7 +1498,7 @@ float FastNoise::SingleCellular(float x, float y, float z)
switch (m_cellularReturnType)
{
case CellValue:
return ValCoord3D(0, xc, yc, zc);
return ValCoord3D(m_seed, xc, yc, zc);

case NoiseLookup:
assert(m_cellularNoiseLookup);
Expand Down Expand Up @@ -1689,7 +1704,7 @@ float FastNoise::SingleCellular(float x, float y)
switch (m_cellularReturnType)
{
case CellValue:
return ValCoord2D(0, xc, yc);
return ValCoord2D(m_seed, xc, yc);

case NoiseLookup:
assert(m_cellularNoiseLookup);
Expand Down
1 change: 1 addition & 0 deletions FastNoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class FastNoise
float SingleSimplexFractalFBM(float x, float y);
float SingleSimplexFractalBillow(float x, float y);
float SingleSimplexFractalRigidMulti(float x, float y);
float SingleSimplexFractalBlend(float x, float y);
float SingleSimplex(unsigned char offset, float x, float y);

float SingleCellular(float x, float y);
Expand Down

0 comments on commit 4d36f87

Please sign in to comment.