ErodeSphere.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 const double rainHeight = 0.2;
  37. private const int rounds = 10;
  38. private const NeighbourSystem type = NeighbourSystem.Moore;
  39. private const double waterSaturation = 0.30;
  40. #region Supporting Functions
  41. private static int[] Neighbours(NeighbourSystem neighbourType, int index)
  42. {
  43. int[] coord = new int[2];
  44. index++;
  45. switch (neighbourType)
  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, bool[,] mask, double rx, double ry, double rz, 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. if (mask[x,y])
  147. {
  148. const double solConst = (1.0 / rounds);
  149. double sedDelta = water[x, y] * solConst;
  150. map[x, y] -= sedDelta;
  151. sediment[x, y] += sedDelta;
  152. }
  153. }
  154. }
  155. // Move water
  156. for (x = 0; x < water.Width; x++)
  157. {
  158. for (y = 0; y < water.Height; y++)
  159. {
  160. if (water[x, y] <= 0)
  161. continue;
  162. // Step 1. Calculate average of neighbours
  163. int neighbours = 0;
  164. double altitudeTotal = 0.0;
  165. double altitudeMe = map[x, y] + water[x, y];
  166. const int NEIGHBOUR_ME = 4;
  167. const int NEIGHBOUR_MAX = 9;
  168. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  169. {
  170. if (j != NEIGHBOUR_ME)
  171. {
  172. int[] coords = Neighbours(type, j);
  173. coords[0] += x;
  174. coords[1] += y;
  175. if (coords[0] > map.Width - 1)
  176. continue;
  177. if (coords[1] > map.Height - 1)
  178. continue;
  179. if (coords[0] < 0)
  180. continue;
  181. if (coords[1] < 0)
  182. continue;
  183. // Calculate total height of this neighbour
  184. double altitudeNeighbour = water[coords[0], coords[1]] + map[coords[0], coords[1]];
  185. // If it's greater than me...
  186. if (altitudeNeighbour - altitudeMe < 0)
  187. {
  188. // Add it to our calculations
  189. neighbours++;
  190. altitudeTotal += altitudeNeighbour;
  191. }
  192. }
  193. }
  194. if (neighbours == 0)
  195. continue;
  196. double altitudeAvg = altitudeTotal / neighbours;
  197. // Step 2. Allocate water to neighbours.
  198. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  199. {
  200. if (j != NEIGHBOUR_ME)
  201. {
  202. int[] coords = Neighbours(type, j);
  203. coords[0] += x;
  204. coords[1] += y;
  205. if (coords[0] > map.Width - 1)
  206. continue;
  207. if (coords[1] > map.Height - 1)
  208. continue;
  209. if (coords[0] < 0)
  210. continue;
  211. if (coords[1] < 0)
  212. continue;
  213. // Skip if we dont have water to begin with.
  214. if (water[x, y] < 0)
  215. continue;
  216. // Calculate our delta average
  217. double altitudeDelta = altitudeMe - altitudeAvg;
  218. if (altitudeDelta < 0)
  219. continue;
  220. // Calculate how much water we can move
  221. double waterMin = Math.Min(water[x, y], altitudeDelta);
  222. double waterDelta = waterMin * ((water[coords[0], coords[1]] + map[coords[0], coords[1]])
  223. / altitudeTotal);
  224. double sedimentDelta = sediment[x, y] * (waterDelta / water[x, y]);
  225. if (sedimentDelta > 0)
  226. {
  227. sediment[x, y] -= sedimentDelta;
  228. sediment[coords[0], coords[1]] += sedimentDelta;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. // Evaporate
  235. for (x = 0; x < water.Width; x++)
  236. {
  237. for (y = 0; y < water.Height; y++)
  238. {
  239. water[x, y] *= 1.0 - (rainHeight / rounds);
  240. double waterCapacity = waterSaturation * water[x, y];
  241. double sedimentDeposit = sediment[x, y] - waterCapacity;
  242. if (sedimentDeposit > 0)
  243. {
  244. if (mask[x,y])
  245. {
  246. sediment[x, y] -= sedimentDeposit;
  247. map[x, y] += sedimentDeposit;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. // Deposit any remainder (should be minimal)
  254. for (x = 0; x < water.Width; x++)
  255. for (y = 0; y < water.Height; y++)
  256. if (mask[x,y] && sediment[x, y] > 0)
  257. map[x, y] += sediment[x, y];
  258. }
  259. #endregion
  260. }
  261. }