diff options
Diffstat (limited to 'OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs')
-rw-r--r-- | OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs b/OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs index 23ab321..55265d9 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs | |||
@@ -47,5 +47,60 @@ namespace OpenSim.Region.Environment.Modules.Terrain | |||
47 | double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); | 47 | double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); |
48 | return hi; | 48 | return hi; |
49 | } | 49 | } |
50 | |||
51 | private static double Noise(double x, double y) | ||
52 | { | ||
53 | int n = (int)x + (int)(y * 749); | ||
54 | n = (n << 13) ^ n; | ||
55 | return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0); | ||
56 | } | ||
57 | |||
58 | private static double SmoothedNoise1(double x, double y) | ||
59 | { | ||
60 | double corners = (Noise(x - 1, y - 1) + Noise(x + 1, y - 1) + Noise(x - 1, y + 1) + Noise(x + 1, y + 1)) / 16; | ||
61 | double sides = (Noise(x - 1, y) + Noise(x + 1, y) + Noise(x, y - 1) + Noise(x, y + 1)) / 8; | ||
62 | double center = Noise(x, y) / 4; | ||
63 | return corners + sides + center; | ||
64 | } | ||
65 | |||
66 | private static double Interpolate(double x, double y, double z) | ||
67 | { | ||
68 | return (x * (1.0 - z)) + (y * z); | ||
69 | } | ||
70 | |||
71 | private static double InterpolatedNoise(double x, double y) | ||
72 | { | ||
73 | int integer_X = (int)(x); | ||
74 | double fractional_X = x - integer_X; | ||
75 | |||
76 | int integer_Y = (int)y; | ||
77 | double fractional_Y = y - integer_Y; | ||
78 | |||
79 | double v1 = SmoothedNoise1(integer_X, integer_Y); | ||
80 | double v2 = SmoothedNoise1(integer_X + 1, integer_Y); | ||
81 | double v3 = SmoothedNoise1(integer_X, integer_Y + 1); | ||
82 | double v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1); | ||
83 | |||
84 | double i1 = Interpolate(v1, v2, fractional_X); | ||
85 | double i2 = Interpolate(v3, v4, fractional_X); | ||
86 | |||
87 | return Interpolate(i1, i2, fractional_Y); | ||
88 | } | ||
89 | |||
90 | public static double PerlinNoise2D(double x, double y, int octaves, double persistence) | ||
91 | { | ||
92 | double frequency = 0.0; | ||
93 | double amplitude = 0.0; | ||
94 | double total = 0.0; | ||
95 | |||
96 | for (int i = 0; i < octaves; i++) | ||
97 | { | ||
98 | frequency = System.Math.Pow(2, i); | ||
99 | amplitude = System.Math.Pow(persistence, i); | ||
100 | |||
101 | total += InterpolatedNoise(x * frequency, y * frequency) * amplitude; | ||
102 | } | ||
103 | return total; | ||
104 | } | ||
50 | } | 105 | } |
51 | } | 106 | } |