aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/Terrain/PaintBrushes/ErodeSphere.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/World/Terrain/PaintBrushes/ErodeSphere.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/World/Terrain/PaintBrushes/ErodeSphere.cs312
1 files changed, 312 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/Terrain/PaintBrushes/ErodeSphere.cs b/OpenSim/Region/Environment/Modules/World/Terrain/PaintBrushes/ErodeSphere.cs
new file mode 100644
index 0000000..29448aa
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/World/Terrain/PaintBrushes/ErodeSphere.cs
@@ -0,0 +1,312 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using OpenSim.Region.Environment.Interfaces;
30
31namespace OpenSim.Region.Environment.Modules.World.Terrain.PaintBrushes
32{
33 /// <summary>
34 /// Hydraulic Erosion Brush
35 /// </summary>
36 public class ErodeSphere : ITerrainPaintableEffect
37 {
38 private double rainHeight = 0.2;
39 private int rounds = 10;
40 private NeighbourSystem type = NeighbourSystem.Moore; // Parameter
41 private double waterSaturation = 0.30; // Can carry 1% of water in height
42
43 #region Supporting Functions
44
45 private int[] Neighbours(NeighbourSystem type, int index)
46 {
47 int[] coord = new int[2];
48
49 index++;
50
51 switch (type)
52 {
53 case NeighbourSystem.Moore:
54 switch (index)
55 {
56 case 1:
57 coord[0] = -1;
58 coord[1] = -1;
59 break;
60
61 case 2:
62 coord[0] = -0;
63 coord[1] = -1;
64 break;
65
66 case 3:
67 coord[0] = +1;
68 coord[1] = -1;
69 break;
70
71 case 4:
72 coord[0] = -1;
73 coord[1] = -0;
74 break;
75
76 case 5:
77 coord[0] = -0;
78 coord[1] = -0;
79 break;
80
81 case 6:
82 coord[0] = +1;
83 coord[1] = -0;
84 break;
85
86 case 7:
87 coord[0] = -1;
88 coord[1] = +1;
89 break;
90
91 case 8:
92 coord[0] = -0;
93 coord[1] = +1;
94 break;
95
96 case 9:
97 coord[0] = +1;
98 coord[1] = +1;
99 break;
100
101 default:
102 break;
103 }
104 break;
105
106 case NeighbourSystem.VonNeumann:
107 switch (index)
108 {
109 case 1:
110 coord[0] = 0;
111 coord[1] = -1;
112 break;
113
114 case 2:
115 coord[0] = -1;
116 coord[1] = 0;
117 break;
118
119 case 3:
120 coord[0] = +1;
121 coord[1] = 0;
122 break;
123
124 case 4:
125 coord[0] = 0;
126 coord[1] = +1;
127 break;
128
129 case 5:
130 coord[0] = -0;
131 coord[1] = -0;
132 break;
133
134 default:
135 break;
136 }
137 break;
138 }
139
140 return coord;
141 }
142
143 private enum NeighbourSystem
144 {
145 Moore,
146 VonNeumann
147 } ;
148
149 #endregion
150
151 #region ITerrainPaintableEffect Members
152
153 public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration)
154 {
155 strength = TerrainUtil.MetersToSphericalStrength(strength);
156
157 int x, y;
158 // Using one 'rain' round for this, so skipping a useless loop
159 // Will need to adapt back in for the Flood brush
160
161 ITerrainChannel water = new TerrainChannel(map.Width, map.Height);
162 ITerrainChannel sediment = new TerrainChannel(map.Width, map.Height);
163
164 // Fill with rain
165 for (x = 0; x < water.Width; x++)
166 for (y = 0; y < water.Height; y++)
167 water[x, y] = Math.Max(0.0, TerrainUtil.SphericalFactor(x, y, rx, ry, strength) * rainHeight * duration);
168
169 for (int i = 0; i < rounds; i++)
170 {
171 // Erode underlying terrain
172 for (x = 0; x < water.Width; x++)
173 {
174 for (y = 0; y < water.Height; y++)
175 {
176 double solConst = (1.0 / rounds);
177 double sedDelta = water[x, y] * solConst;
178 map[x, y] -= sedDelta;
179 sediment[x, y] += sedDelta;
180 }
181 }
182
183 // Move water
184 for (x = 0; x < water.Width; x++)
185 {
186 for (y = 0; y < water.Height; y++)
187 {
188 if (water[x, y] <= 0)
189 continue;
190
191 // Step 1. Calculate average of neighbours
192
193 int neighbours = 0;
194 double altitudeTotal = 0.0;
195 double altitudeMe = map[x, y] + water[x, y];
196
197 int NEIGHBOUR_ME = 4;
198
199 int NEIGHBOUR_MAX = type == NeighbourSystem.Moore ? 9 : 5;
200
201 for (int j = 0; j < NEIGHBOUR_MAX; j++)
202 {
203 if (j != NEIGHBOUR_ME)
204 {
205 int[] coords = Neighbours(type, j);
206
207 coords[0] += x;
208 coords[1] += y;
209
210 if (coords[0] > map.Width - 1)
211 continue;
212 if (coords[1] > map.Height - 1)
213 continue;
214 if (coords[0] < 0)
215 continue;
216 if (coords[1] < 0)
217 continue;
218
219 // Calculate total height of this neighbour
220 double altitudeNeighbour = water[coords[0], coords[1]] + map[coords[0], coords[1]];
221
222 // If it's greater than me...
223 if (altitudeNeighbour - altitudeMe < 0)
224 {
225 // Add it to our calculations
226 neighbours++;
227 altitudeTotal += altitudeNeighbour;
228 }
229 }
230 }
231
232 if (neighbours == 0)
233 continue;
234
235 double altitudeAvg = altitudeTotal / neighbours;
236
237 // Step 2. Allocate water to neighbours.
238 for (int j = 0; j < NEIGHBOUR_MAX; j++)
239 {
240 if (j != NEIGHBOUR_ME)
241 {
242 int[] coords = Neighbours(type, j);
243
244 coords[0] += x;
245 coords[1] += y;
246
247 if (coords[0] > map.Width - 1)
248 continue;
249 if (coords[1] > map.Height - 1)
250 continue;
251 if (coords[0] < 0)
252 continue;
253 if (coords[1] < 0)
254 continue;
255
256 // Skip if we dont have water to begin with.
257 if (water[x, y] < 0)
258 continue;
259
260 // Calculate our delta average
261 double altitudeDelta = altitudeMe - altitudeAvg;
262
263 if (altitudeDelta < 0)
264 continue;
265
266 // Calculate how much water we can move
267 double waterMin = Math.Min(water[x, y], altitudeDelta);
268 double waterDelta = waterMin * ((water[coords[0], coords[1]] + map[coords[0], coords[1]])
269 / altitudeTotal);
270
271 double sedimentDelta = sediment[x, y] * (waterDelta / water[x, y]);
272
273 if (sedimentDelta > 0)
274 {
275 sediment[x, y] -= sedimentDelta;
276 sediment[coords[0], coords[1]] += sedimentDelta;
277 }
278 }
279 }
280 }
281 }
282
283 // Evaporate
284
285 for (x = 0; x < water.Width; x++)
286 {
287 for (y = 0; y < water.Height; y++)
288 {
289 water[x, y] *= 1.0 - (rainHeight / rounds);
290
291 double waterCapacity = waterSaturation * water[x, y];
292
293 double sedimentDeposit = sediment[x, y] - waterCapacity;
294 if (sedimentDeposit > 0)
295 {
296 sediment[x, y] -= sedimentDeposit;
297 map[x, y] += sedimentDeposit;
298 }
299 }
300 }
301 }
302
303 // Deposit any remainder (should be minimal)
304 for (x = 0; x < water.Width; x++)
305 for (y = 0; y < water.Height; y++)
306 if (sediment[x, y] > 0)
307 map[x, y] += sediment[x, y];
308 }
309
310 #endregion
311 }
312} \ No newline at end of file