ErodeSphere.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. using OpenSim.Region.Environment.Interfaces;
  28. using System;
  29. namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes
  30. {
  31. /// <summary>
  32. /// Hydraulic Erosion Brush
  33. /// </summary>
  34. public class ErodeSphere : ITerrainPaintableEffect
  35. {
  36. NeighbourSystem type = NeighbourSystem.Moore; // Parameter
  37. double rainHeight = 0.2;
  38. int rounds = 10;
  39. double waterSaturation = 0.30; // Can carry 1% of water in height
  40. #region Supporting Functions
  41. private enum NeighbourSystem
  42. {
  43. Moore,
  44. VonNeumann
  45. } ;
  46. private int[] Neighbours(NeighbourSystem type, int index)
  47. {
  48. int[] coord = new int[2];
  49. index++;
  50. switch (type)
  51. {
  52. case NeighbourSystem.Moore:
  53. switch (index)
  54. {
  55. case 1:
  56. coord[0] = -1;
  57. coord[1] = -1;
  58. break;
  59. case 2:
  60. coord[0] = -0;
  61. coord[1] = -1;
  62. break;
  63. case 3:
  64. coord[0] = +1;
  65. coord[1] = -1;
  66. break;
  67. case 4:
  68. coord[0] = -1;
  69. coord[1] = -0;
  70. break;
  71. case 5:
  72. coord[0] = -0;
  73. coord[1] = -0;
  74. break;
  75. case 6:
  76. coord[0] = +1;
  77. coord[1] = -0;
  78. break;
  79. case 7:
  80. coord[0] = -1;
  81. coord[1] = +1;
  82. break;
  83. case 8:
  84. coord[0] = -0;
  85. coord[1] = +1;
  86. break;
  87. case 9:
  88. coord[0] = +1;
  89. coord[1] = +1;
  90. break;
  91. default:
  92. break;
  93. }
  94. break;
  95. case NeighbourSystem.VonNeumann:
  96. switch (index)
  97. {
  98. case 1:
  99. coord[0] = 0;
  100. coord[1] = -1;
  101. break;
  102. case 2:
  103. coord[0] = -1;
  104. coord[1] = 0;
  105. break;
  106. case 3:
  107. coord[0] = +1;
  108. coord[1] = 0;
  109. break;
  110. case 4:
  111. coord[0] = 0;
  112. coord[1] = +1;
  113. break;
  114. case 5:
  115. coord[0] = -0;
  116. coord[1] = -0;
  117. break;
  118. default:
  119. break;
  120. }
  121. break;
  122. }
  123. return coord;
  124. }
  125. #endregion
  126. #region ITerrainPaintableEffect Members
  127. public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration)
  128. {
  129. strength = TerrainUtil.MetersToSphericalStrength(strength);
  130. int x, y;
  131. // Using one 'rain' round for this, so skipping a useless loop
  132. // Will need to adapt back in for the Flood brush
  133. ITerrainChannel water = new TerrainChannel(map.Width, map.Height);
  134. ITerrainChannel sediment = new TerrainChannel(map.Width, map.Height);
  135. // Fill with rain
  136. for (x = 0; x < water.Width; x++)
  137. for (y = 0; y < water.Height; y++)
  138. water[x, y] = Math.Max(0.0, TerrainUtil.SphericalFactor(x, y, rx, ry, strength) * rainHeight * duration);
  139. for (int i = 0; i < rounds; i++)
  140. {
  141. // Erode underlying terrain
  142. for (x = 0; x < water.Width; x++)
  143. {
  144. for (y = 0; y < water.Height; y++)
  145. {
  146. double solConst = (1.0 / rounds);
  147. double sedDelta = water[x, y] * solConst;
  148. map[x, y] -= sedDelta;
  149. sediment[x, y] += sedDelta;
  150. }
  151. }
  152. // Move water
  153. for (x = 0; x < water.Width; x++)
  154. {
  155. for (y = 0; y < water.Height; y++)
  156. {
  157. if (water[x, y] <= 0)
  158. continue;
  159. // Step 1. Calculate average of neighbours
  160. int neighbours = 0;
  161. double altitudeTotal = 0.0;
  162. double altitudeMe = map[x, y] + water[x, y];
  163. int NEIGHBOUR_ME = 4;
  164. int NEIGHBOUR_MAX = type == NeighbourSystem.Moore ? 9 : 5;
  165. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  166. {
  167. if (j != NEIGHBOUR_ME)
  168. {
  169. int[] coords = Neighbours(type, j);
  170. coords[0] += x;
  171. coords[1] += y;
  172. if (coords[0] > map.Width - 1)
  173. continue;
  174. if (coords[1] > map.Height - 1)
  175. continue;
  176. if (coords[0] < 0)
  177. continue;
  178. if (coords[1] < 0)
  179. continue;
  180. // Calculate total height of this neighbour
  181. double altitudeNeighbour = water[coords[0], coords[1]] + map[coords[0], coords[1]];
  182. // If it's greater than me...
  183. if (altitudeNeighbour - altitudeMe < 0)
  184. {
  185. // Add it to our calculations
  186. neighbours++;
  187. altitudeTotal += altitudeNeighbour;
  188. }
  189. }
  190. }
  191. if (neighbours == 0)
  192. continue;
  193. double altitudeAvg = altitudeTotal / neighbours;
  194. // Step 2. Allocate water to neighbours.
  195. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  196. {
  197. if (j != NEIGHBOUR_ME)
  198. {
  199. int[] coords = Neighbours(type, j);
  200. coords[0] += x;
  201. coords[1] += y;
  202. if (coords[0] > map.Width - 1)
  203. continue;
  204. if (coords[1] > map.Height - 1)
  205. continue;
  206. if (coords[0] < 0)
  207. continue;
  208. if (coords[1] < 0)
  209. continue;
  210. // Skip if we dont have water to begin with.
  211. if (water[x, y] < 0)
  212. continue;
  213. // Calculate our delta average
  214. double altitudeDelta = altitudeMe - altitudeAvg;
  215. if (altitudeDelta < 0)
  216. continue;
  217. // Calculate how much water we can move
  218. double waterMin = Math.Min(water[x, y], altitudeDelta);
  219. double waterDelta = waterMin * ((water[coords[0], coords[1]] + map[coords[0], coords[1]])
  220. / altitudeTotal);
  221. double sedimentDelta = sediment[x, y] * (waterDelta / water[x, y]);
  222. if (sedimentDelta > 0)
  223. {
  224. sediment[x, y] -= sedimentDelta;
  225. sediment[coords[0], coords[1]] += sedimentDelta;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. // Evaporate
  232. for (x = 0; x < water.Width; x++)
  233. {
  234. for (y = 0; y < water.Height; y++)
  235. {
  236. water[x, y] *= 1.0 - (rainHeight / rounds);
  237. double waterCapacity = waterSaturation * water[x, y];
  238. double sedimentDeposit = sediment[x, y] - waterCapacity;
  239. if (sedimentDeposit > 0)
  240. {
  241. sediment[x, y] -= sedimentDeposit;
  242. map[x, y] += sedimentDeposit;
  243. }
  244. }
  245. }
  246. }
  247. // Deposit any remainder (should be minimal)
  248. for (x = 0; x < water.Width; x++)
  249. for (y = 0; y < water.Height; y++)
  250. if (sediment[x, y] > 0)
  251. map[x, y] += sediment[x, y];
  252. }
  253. #endregion
  254. }
  255. }