Skip to content

Commit

Permalink
few very minor optimisations to libnoise
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jan 9, 2020
1 parent 76a98a3 commit a865a60
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 61 deletions.
8 changes: 2 additions & 6 deletions MCGalaxy/Generator/LibNoise/Billow.cs
Expand Up @@ -28,7 +28,6 @@ public sealed class Billow : GradientNoiseBasis, IModule
{
public double Frequency;
public double Persistence;
public NoiseQuality NoiseQuality;
public int Seed;
public int OctaveCount;
public double Lacunarity;
Expand All @@ -39,24 +38,21 @@ public Billow()
Lacunarity = 2.0;
OctaveCount = 6;
Persistence = 0.5;
NoiseQuality = NoiseQuality.Standard;
}

public double GetValue(double x, double y, double z)
{
double value = 0.0;
double signal = 0.0;
double curPersistence = 1.0;
long seed;

x *= Frequency;
y *= Frequency;
z *= Frequency;

for (int currentOctave = 0; currentOctave < OctaveCount; currentOctave++)
for (int octave = 0; octave < OctaveCount; octave++)
{
seed = (Seed + currentOctave) & 0xffffffff;
signal = GradientCoherentNoise(x, y, z, (int)seed, NoiseQuality);
signal = GradientCoherentNoise(x, y, z, Seed + octave);
signal = 2.0 * Math.Abs(signal) - 1.0;
value += signal * curPersistence;

Expand Down
39 changes: 6 additions & 33 deletions MCGalaxy/Generator/LibNoise/GradientNoiseBasis.cs
Expand Up @@ -292,8 +292,7 @@ public class GradientNoiseBasis
0.0337884, -0.979891, -0.196654, 0.0
};

public static double GradientCoherentNoise(double x, double y, double z, int seed,
NoiseQuality noiseQuality)
public static double GradientCoherentNoise(double x, double y, double z, int seed)
{
int x0 = (x > 0.0 ? (int)x : (int)x - 1);
int x1 = x0 + 1;
Expand All @@ -302,25 +301,10 @@ public class GradientNoiseBasis
int z0 = (z > 0.0 ? (int)z : (int)z - 1);
int z1 = z0 + 1;

double xs = 0, ys = 0, zs = 0;
switch (noiseQuality)
{
case NoiseQuality.Low:
xs = (x - x0);
ys = (y - y0);
zs = (z - z0);
break;
case NoiseQuality.Standard:
xs = SCurve3(x - x0);
ys = SCurve3(y - y0);
zs = SCurve3(z - z0);
break;
case NoiseQuality.High:
xs = SCurve5(x - x0);
ys = SCurve5(y - y0);
zs = SCurve5(z - z0);
break;
}
// case NoiseQuality.Standard:
double xs = SCurve3(x - x0);
double ys = SCurve3(y - y0);
double zs = SCurve3(z - z0);

double n0, n1, ix0, ix1, iy0, iy1;
n0 = GradientNoise(x, y, z, x0, y0, z0, seed);
Expand Down Expand Up @@ -382,17 +366,6 @@ static double LinearInterpolate(double n0, double n1, double a)
static double SCurve3(double a)
{
return (a * a * (3.0 - 2.0 * a));
}

/// <summary>
/// Returns the given value mapped onto a quintic S-curve.
/// </summary>
static double SCurve5(double a)
{
double a3 = a * a * a;
double a4 = a3 * a;
double a5 = a4 * a;
return (6.0 * a5) - (15.0 * a4) + (10.0 * a3);
}
}
}
}
7 changes: 0 additions & 7 deletions MCGalaxy/Generator/LibNoise/IModule.cs
Expand Up @@ -24,13 +24,6 @@

namespace LibNoise
{
public enum NoiseQuality
{
Low,
Standard,
High
}

public interface IModule
{
double GetValue(double x, double y, double z);
Expand Down
14 changes: 5 additions & 9 deletions MCGalaxy/Generator/LibNoise/Perlin.cs
Expand Up @@ -28,7 +28,6 @@ public sealed class Perlin : GradientNoiseBasis, IModule
{
public double Frequency;
public double Persistence;
public NoiseQuality NoiseQuality;
public int Seed;
public int OctaveCount;
public double Lacunarity;
Expand All @@ -39,24 +38,21 @@ public Perlin()
Lacunarity = 2.0;
OctaveCount = 6;
Persistence = 0.5;
NoiseQuality = NoiseQuality.Standard;
}

public double GetValue(double x, double y, double z)
{
double value = 0.0;
double signal = 0.0;
double curPersistence = 1.0;
long seed;

x *=Frequency;
y *=Frequency;
z *=Frequency;
x *= Frequency;
y *= Frequency;
z *= Frequency;

for(int currentOctave = 0; currentOctave < OctaveCount; currentOctave++)
for(int octave = 0; octave < OctaveCount; octave++)
{
seed = (Seed + currentOctave) & 0xffffffff;
signal = GradientCoherentNoise(x, y, z, (int)seed, NoiseQuality);
signal = GradientCoherentNoise(x, y, z, Seed + octave);
//signal = cachedNoise3(x, y, z);

value += signal * curPersistence;
Expand Down
9 changes: 3 additions & 6 deletions MCGalaxy/Generator/LibNoise/RidgedMultifractal.cs
Expand Up @@ -27,7 +27,6 @@ namespace LibNoise
public sealed class RidgedMultifractal : GradientNoiseBasis, IModule
{
public double Frequency;
public NoiseQuality NoiseQuality;
public int Seed;
public int OctaveCount;

Expand All @@ -40,7 +39,6 @@ public RidgedMultifractal()
Frequency = 1.0;
Lacunarity = 2.0;
OctaveCount = 6;
NoiseQuality = NoiseQuality.Standard;
Seed = 0;
}

Expand All @@ -59,10 +57,9 @@ public double GetValue(double x, double y, double z)
double offset = 1.0;
double gain = 2.0;

for (int currentOctave = 0; currentOctave < OctaveCount; currentOctave++)
for (int octave = 0; octave < OctaveCount; octave++)
{
long seed = (Seed + currentOctave) & 0x7fffffff;
signal = GradientCoherentNoise(x, y, z, (int)seed, NoiseQuality);
signal = GradientCoherentNoise(x, y, z, (Seed + octave) & 0x7fffffff);

// Make the ridges.
signal = Math.Abs(signal);
Expand All @@ -88,7 +85,7 @@ public double GetValue(double x, double y, double z)
}

// Add the signal to the output value.
value += (signal * SpectralWeights[currentOctave]);
value += (signal * SpectralWeights[octave]);

// Go to the next octave.
x *= Lacunarity;
Expand Down

0 comments on commit a865a60

Please sign in to comment.