29 using OpenSim.Region.Framework.Interfaces;
31 namespace OpenSim.
Region.Framework.Scenes
33 public static class TerrainUtil
35 public static double MetersToSphericalStrength(
double size)
38 return (size + 1) * 1.35;
41 public static double SphericalFactor(
double x,
double y,
double rx,
double ry,
double size)
43 return size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry));
46 public static double GetBilinearInterpolate(
double x,
double y,
ITerrainChannel map)
60 const int stepSize = 1;
61 double h00 = map[(int) x, (
int) y];
62 double h10 = map[(
int) x + stepSize, (
int) y];
63 double h01 = map[(
int) x, (
int) y + stepSize];
64 double h11 = map[(
int) x + stepSize, (
int) y + stepSize];
72 double a11 = h1 - h2 - h3 + h4;
73 double partialx = x - (
int) x;
74 double partialz = y - (
int) y;
75 double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz);
79 private static
double Noise(
double x,
double y)
81 int n = (int) x + (
int) (y * 749);
83 return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
86 private static double SmoothedNoise1(
double x,
double y)
88 double corners = (Noise(x - 1, y - 1) + Noise(x + 1, y - 1) + Noise(x - 1, y + 1) + Noise(x + 1, y + 1)) / 16;
89 double sides = (Noise(x - 1, y) + Noise(x + 1, y) + Noise(x, y - 1) + Noise(x, y + 1)) / 8;
90 double center = Noise(x, y) / 4;
91 return corners + sides + center;
94 private static double Interpolate(
double x,
double y,
double z)
96 return (x * (1.0 - z)) + (y * z);
99 public static double InterpolatedNoise(
double x,
double y)
101 int integer_X = (int) (x);
102 double fractional_X = x - integer_X;
104 int integer_Y = (int) y;
105 double fractional_Y = y - integer_Y;
107 double v1 = SmoothedNoise1(integer_X, integer_Y);
108 double v2 = SmoothedNoise1(integer_X + 1, integer_Y);
109 double v3 = SmoothedNoise1(integer_X, integer_Y + 1);
110 double v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1);
112 double i1 = Interpolate(v1, v2, fractional_X);
113 double i2 = Interpolate(v3, v4, fractional_X);
115 return Interpolate(i1, i2, fractional_Y);
118 public static double PerlinNoise2D(
double x,
double y,
int octaves,
double persistence)
122 for (
int i = 0; i < octaves; i++)
124 double frequency = Math.Pow(2, i);
125 double amplitude = Math.Pow(persistence, i);
127 total += InterpolatedNoise(x * frequency, y * frequency) * amplitude;