AerobicErosion.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. namespace libTerrain
  29. {
  30. partial class Channel
  31. {
  32. // Ideas for Aerobic erosion
  33. //
  34. // Unlike thermal (gravity) and hydraulic (water suspension)
  35. // aerobic erosion should displace mass by moving sediment
  36. // in "hops". The length of the hop being dictated by the
  37. // presence of sharp cliffs and wind speed.
  38. // The ability to pickup sediment is defined by the total
  39. // surface area, such that:
  40. // 0 0 0
  41. // 0 1 0
  42. // 0 0 0
  43. // Would be the best possible value for sediment to be
  44. // picked up (total difference = 8) and flatter land
  45. // will erode less quickly.
  46. // Suspended particles assist the erosion process by hitting
  47. // the surface and chiselling additional particles off faster
  48. // than alone.
  49. // Particles are deposited when one of two conditions is met
  50. // First:
  51. // When particles hit a wall - such that the
  52. // wind direction points at a difference >= the
  53. // deposition mininum talus.
  54. // Second:
  55. // When wind speed is lowered to below the minimum
  56. // required for transit. An idea for this is to
  57. // use the navier-stokes algorithms for simulating
  58. // pressure across the terrain.
  59. /// <summary>
  60. /// An experimental erosion algorithm developed by Adam. Moves sediment by factoring the surface area of each height point.
  61. /// </summary>
  62. /// <param name="windspeed">0..1 The speed of the wind</param>
  63. /// <param name="pickup_talus_minimum">The minimum angle at which rock is eroded 0..1 (recommended: <= 0.30)</param>
  64. /// <param name="drop_talus_minimum">The minimum angle at which rock is dropped 0..1 (recommended: >= 0.00)</param>
  65. /// <param name="carry">The percentage of rock which can be picked up to pickup 0..1</param>
  66. /// <param name="rounds">The number of erosion rounds (recommended: 25+)</param>
  67. /// <param name="lowest">Drop sediment at the lowest point?</param>
  68. public void AerobicErosion(double windspeed, double pickupTalusMinimum, double dropTalusMinimum, double carry,
  69. int rounds, bool lowest, bool usingFluidDynamics)
  70. {
  71. bool debugImages = false;
  72. Channel wind = new Channel(w, h);
  73. Channel sediment = new Channel(w, h);
  74. int x, y, i, j;
  75. Normalise();
  76. wind = Copy();
  77. wind.Noise();
  78. if (debugImages)
  79. wind.SaveImage("testimg/wind_start.png");
  80. if (usingFluidDynamics)
  81. {
  82. wind.navierStokes(20, 0.1, 0.0, 0.0);
  83. }
  84. else
  85. {
  86. wind.Pertubation(30);
  87. }
  88. if (debugImages)
  89. wind.SaveImage("testimg/wind_begin.png");
  90. for (i = 0; i < rounds; i++)
  91. {
  92. // Convert some rocks to sand
  93. for (x = 1; x < w - 1; x++)
  94. {
  95. for (y = 1; y < h - 1; y++)
  96. {
  97. double me = Get(x, y);
  98. double surfacearea = 0.3; // Everything will erode even if it's flat. Just slower.
  99. for (j = 0; j < 9; j++)
  100. {
  101. int[] coords = Neighbours(NeighbourSystem.Moore, j);
  102. double target = Get(x + coords[0], y + coords[1]);
  103. surfacearea += Math.Abs(target - me);
  104. }
  105. double amount = surfacearea*wind.map[x, y]*carry;
  106. if (amount < 0)
  107. amount = 0;
  108. if (surfacearea > pickupTalusMinimum)
  109. {
  110. Set(x, y, map[x, y] - amount);
  111. sediment.map[x, y] += amount;
  112. }
  113. }
  114. }
  115. if (usingFluidDynamics)
  116. {
  117. sediment.navierStokes(7, 0.1, 0.0, 0.1);
  118. Channel noiseChan = new Channel(w, h);
  119. noiseChan.Noise();
  120. wind.Blend(noiseChan, 0.01);
  121. wind.navierStokes(10, 0.1, 0.01, 0.01);
  122. sediment.Distort(wind, windspeed);
  123. }
  124. else
  125. {
  126. wind.Pertubation(15); // Can do better later
  127. wind.seed++;
  128. sediment.Pertubation(10); // Sediment is blown around a bit
  129. sediment.seed++;
  130. }
  131. if (debugImages)
  132. wind.SaveImage("testimg/wind_" + i.ToString() + ".png");
  133. // Convert some sand to rock
  134. for (x = 1; x < w - 1; x++)
  135. {
  136. for (y = 1; y < h - 1; y++)
  137. {
  138. double me = Get(x, y);
  139. double surfacearea = 0.01; // Flat land does not get deposition
  140. double min = double.MaxValue;
  141. int[] minside = new int[2];
  142. for (j = 0; j < 9; j++)
  143. {
  144. int[] coords = Neighbours(NeighbourSystem.Moore, j);
  145. double target = Get(x + coords[0], y + coords[1]);
  146. surfacearea += Math.Abs(target - me);
  147. if (target < min && lowest)
  148. {
  149. minside = (int[]) coords.Clone();
  150. min = target;
  151. }
  152. }
  153. double amount = surfacearea*(1.0 - wind.map[x, y])*carry;
  154. if (amount < 0)
  155. amount = 0;
  156. if (surfacearea > dropTalusMinimum)
  157. {
  158. Set(x + minside[0], y + minside[1], map[x + minside[0], y + minside[1]] + amount);
  159. sediment.map[x, y] -= amount;
  160. }
  161. }
  162. }
  163. if (debugImages)
  164. sediment.SaveImage("testimg/sediment_" + i.ToString() + ".png");
  165. wind.Normalise();
  166. wind *= windspeed;
  167. Normalise();
  168. }
  169. Channel myself = this;
  170. myself += sediment;
  171. myself.Normalise();
  172. if (debugImages)
  173. SaveImage("testimg/output.png");
  174. }
  175. }
  176. }