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 System;
  28. using OpenSim.Region.Environment.Interfaces;
  29. namespace OpenSim.Region.Environment.Modules.World.Terrain.PaintBrushes
  30. {
  31. /// <summary>
  32. /// Hydraulic Erosion Brush
  33. /// </summary>
  34. public class ErodeSphere : ITerrainPaintableEffect
  35. {
  36. private double rainHeight = 0.2;
  37. private int rounds = 10;
  38. private NeighbourSystem type = NeighbourSystem.Moore; // Parameter
  39. private double waterSaturation = 0.30; // Can carry 1% of water in height
  40. #region Supporting Functions
  41. private int[] Neighbours(NeighbourSystem type, int index)
  42. {
  43. int[] coord = new int[2];
  44. index++;
  45. switch (type)
  46. {
  47. case NeighbourSystem.Moore:
  48. switch (index)
  49. {
  50. case 1:
  51. coord[0] = -1;
  52. coord[1] = -1;
  53. break;
  54. case 2:
  55. coord[0] = -0;
  56. coord[1] = -1;
  57. break;
  58. case 3:
  59. coord[0] = +1;
  60. coord[1] = -1;
  61. break;
  62. case 4:
  63. coord[0] = -1;
  64. coord[1] = -0;
  65. break;
  66. case 5:
  67. coord[0] = -0;
  68. coord[1] = -0;
  69. break;
  70. case 6:
  71. coord[0] = +1;
  72. coord[1] = -0;
  73. break;
  74. case 7:
  75. coord[0] = -1;
  76. coord[1] = +1;
  77. break;
  78. case 8:
  79. coord[0] = -0;
  80. coord[1] = +1;
  81. break;
  82. case 9:
  83. coord[0] = +1;
  84. coord[1] = +1;
  85. break;
  86. default:
  87. break;
  88. }
  89. break;
  90. case NeighbourSystem.VonNeumann:
  91. switch (index)
  92. {
  93. case 1:
  94. coord[0] = 0;
  95. coord[1] = -1;
  96. break;
  97. case 2:
  98. coord[0] = -1;
  99. coord[1] = 0;
  100. break;
  101. case 3:
  102. coord[0] = +1;
  103. coord[1] = 0;
  104. break;
  105. case 4:
  106. coord[0] = 0;
  107. coord[1] = +1;
  108. break;
  109. case 5:
  110. coord[0] = -0;
  111. coord[1] = -0;
  112. break;
  113. default:
  114. break;
  115. }
  116. break;
  117. }
  118. return coord;
  119. }
  120. private enum NeighbourSystem
  121. {
  122. Moore,
  123. VonNeumann
  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. }