ErodeSphere.cs 12 KB

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