public static Channel erode5(Channel channel, Channel rain, float erosion_water, float erosion_flow, float evaporation, float water_threshold, float solulibility, int ipr, int iterations) { Channel w = new Channel(channel.width, channel.height); // water map Channel dw = new Channel(channel.width, channel.height); // delta water map Channel s = new Channel(channel.width, channel.height); // sediment map Channel ds = new Channel(channel.width, channel.height); // delta sediment map Console.Write("Hydraulic erosion 5: "); for (int i = 0; i < iterations; i++) { Console.Write("."); // save frames /* if (channel.width > 128 && i%10 == 0) { if (i < 10) { channel.toLayer().saveAsPNG("erosion00" + i); } else if (i < 100) { channel.toLayer().saveAsPNG("erosion0" + i); } else { channel.toLayer().saveAsPNG("erosion" + i); } } */ // water is added according to rain map if (i%ipr == 0) { w.channelAdd(rain); } // the presence of water dissolves material channel.channelSubtract(w.copy().multiply(erosion_water)); s.channelAdd(w.copy().multiply(erosion_water)); // water and sediment are transported float h, h1, h2, h3, h4, d1, d2, d3, d4, total_height, total_height_diff, total_height_diff_inv, avr_height, water_amount; int cells; for (int y = 0; y < channel.height; y++) { for (int x = 0; x < channel.width; x++) { // water transport // calculate total heights and height differences h = channel.getPixel(x, y) + w.getPixel(x, y) + s.getPixel(x, y); h1 = channel.getPixelWrap(x , y + 1) + w.getPixelWrap(x , y + 1) + s.getPixelWrap(x , y + 1); h2 = channel.getPixelWrap(x - 1, y ) + w.getPixelWrap(x - 1, y ) + s.getPixelWrap(x - 1, y ); h3 = channel.getPixelWrap(x + 1, y ) + w.getPixelWrap(x + 1, y ) + s.getPixelWrap(x + 1, y ); h4 = channel.getPixelWrap(x , y - 1) + w.getPixelWrap(x , y - 1) + s.getPixelWrap(x , y - 1); d1 = h - h1; d2 = h - h2; d3 = h - h3; d4 = h - h4; // calculate amount of water to transport total_height = 0f; total_height_diff = 0f; cells = 1; if (d1 > 0) { total_height_diff+= d1; total_height+= h1; cells++; } if (d2 > 0) { total_height_diff+= d2; total_height+= h2; cells++; } if (d3 > 0) { total_height_diff+= d3; total_height+= h3; cells++; } if (d4 > 0) { total_height_diff+= d4; total_height+= h4; cells++; } if (cells == 1) { continue; } avr_height = total_height/cells; water_amount = Math.Min(w.getPixel(x, y), h - avr_height); dw.putPixel(x, y, dw.getPixel(x, y) - water_amount); total_height_diff_inv = water_amount/total_height_diff; // transport water if (d1 > 0) { dw.putPixelWrap(x, y + 1, dw.getPixelWrap(x, y + 1) + d1*total_height_diff_inv); } if (d2 > 0) { dw.putPixelWrap(x - 1, y, dw.getPixelWrap(x - 1, y) + d2*total_height_diff_inv); } if (d3 > 0) { dw.putPixelWrap(x + 1, y, dw.getPixelWrap(x + 1, y) + d3*total_height_diff_inv); } if (d4 > 0) { dw.putPixelWrap(x, y - 1, dw.getPixelWrap(x, y - 1) + d4*total_height_diff_inv); } // sediment transport /* h = s.getPixel(x, y); h1 = s.getPixelWrap(x , y + 1); h2 = s.getPixelWrap(x - 1, y ); h3 = s.getPixelWrap(x + 1, y ); h4 = s.getPixelWrap(x , y - 1); d1 = h - h1; d2 = h - h2; d3 = h - h3; d4 = h - h4; // calculate amount of sediment to transport total_height = 0f; total_height_diff = 0f; cells = 1; if (d1 > 0) { total_height_diff+= d1; total_height+= h1; cells++; } if (d2 > 0) { total_height_diff+= d2; total_height+= h2; cells++; } if (d3 > 0) { total_height_diff+= d3; total_height+= h3; cells++; } if (d4 > 0) { total_height_diff+= d4; total_height+= h4; cells++; } if (cells == 1) { continue; } avr_height = total_height/cells; sediment_amount = Math.Min(s.getPixel(x, y), h - avr_height); ds.putPixel(x, y, ds.getPixel(x, y) - sediment_amount); total_height_diff_inv = sediment_amount/total_height_diff; // transport sediment if (d1 > 0) { ds.putPixelWrap(x, y + 1, ds.getPixelWrap(x, y + 1) + d1*total_height_diff_inv); } if (d2 > 0) { ds.putPixelWrap(x - 1, y, ds.getPixelWrap(x - 1, y) + d2*total_height_diff_inv); } if (d3 > 0) { ds.putPixelWrap(x + 1, y, ds.getPixelWrap(x + 1, y) + d3*total_height_diff_inv); } if (d4 > 0) { ds.putPixelWrap(x, y - 1, ds.getPixelWrap(x, y - 1) + d4*total_height_diff_inv); } */ } } // more sediment is dissolved according to amount of water flow /* channel.channelSubtract(dw.copy().fill(0f, Float.MIN_VALUE, 0f).multiply(erosion_flow)); s.channelAdd(dw.copy().fill(0f, Float.MIN_VALUE, 0f).multiply(erosion_flow)); */ // apply water and sediment delta maps w.channelAdd(dw); //w.fill(0f, Float.MIN_VALUE, water_threshold); // remove water below threshold amount s.channelAdd(ds); dw.fill(0f); ds.fill(0f); // water evaporates w.multiply(evaporation); // sediment is deposited for (int y = 0; y < channel.height; y++) { for (int x = 0; x < channel.width; x++) { float deposition = s.getPixel(x, y) - w.getPixel(x, y)*solulibility; if (deposition > 0) { s.putPixel(x, y, s.getPixel(x, y) - deposition); channel.putPixel(x, y, channel.getPixel(x, y) + deposition); } } } } Console.WriteLine("DONE"); return channel; }