- Home /
hydro erosion problems
hey so ime making some random terrain and ime trying to do hydro erosion on it but i just cant seem to get it working how i want. so would anyone be kind enough to look through this code and see if they know where the problem is. not sure if its my method or just the variable quantities
public float[,] Water; //amount of water
public Vector2[,] Velocity; //velocity of water
public float[,] Sed; //sediment contained within water
public int ErosionIts = 100; //iterations of the erosion algorythem
public float Rain = 5f; //amount of rain
public float ErosionRate = 0.001f; //rate at wich rock is tuned to sediment
public float EvapRate = 0.1f; //rate at wich water evapirates
public void Erode (){
for (int i = 0; i < ErosionIts; i++){
Water = new float[MapSize,MapSize];
Velocity = new Vector2[MapSize,MapSize];
Sed = new float[MapSize,MapSize];
for (int x = 0; x < MapSize; x++){
for (int y = 0; y < MapSize; y++){
Water [x,y] += Random.value*Rain;//add rain to each square
Velocity[x,y] = new Vector2(0,0);//reset value
//find the largest drop and asighn it as a velocity in the given directtion
float RelativeHeight = 0;
if (x < MapSize-1){
RelativeHeight = HeightMap[x,y]+Water[x,y]-HeightMap[x+1,y]-Water[x+1,y];
if (RelativeHeight > Velocity[x,y].magnitude){
if (RelativeHeight > 0){
Velocity[x,y] = new Vector2(RelativeHeight, 0);
}
}
}
if (x > 0){
RelativeHeight = HeightMap[x,y]+Water[x,y]-HeightMap[x-1,y]-Water[x-1,y];
if (RelativeHeight > Velocity[x,y].magnitude){
if (RelativeHeight > 0){
Velocity[x,y] = new Vector2(-RelativeHeight, 0);
}
}
}
if (y < MapSize-1){
RelativeHeight = HeightMap[x,y]+Water[x,y]-HeightMap[x,y+1]-Water[x,y+1];
if(RelativeHeight > Velocity[x,y].magnitude){
if (RelativeHeight > 0){
Velocity[x,y] = new Vector2(0, RelativeHeight);
}
}
}
if (y > 0){
RelativeHeight = HeightMap[x,y]+Water[x,y]-HeightMap[x,y-1]-Water[x,y-1];
if (RelativeHeight > Velocity[x,y].magnitude){
if (RelativeHeight > 0){
Velocity[x,y] = new Vector2(0, -RelativeHeight);
}
}
}
//erode or deposit soil based upon velocity and amount of watter
float PreviusSed = Sed[x,y];
Sed[x,y] = Velocity[x,y].magnitude * Water[x,y] * ErosionRate;
HeightMap[x,y] -= Sed[x,y] - PreviusSed;
//move the water and its contained sediment
float w1 = Water[x,y]; //initial water level of this tile
float w2; //initial water level of target tile
float w3; //final water level of this tile
float w4; //final water level of target tile
float s1 = Sed[x,y]; //initial sediment level of this tile
float s2; //initial sediment level of target tile
float s3; //final sediment level of this tile
float s4; //final sediment level of target tile
float h1 = HeightMap[x,y]; //height of this tile
float h2; //height of target tile
//calcuate and apply final levels
if ((x < MapSize-1) && (Velocity[x,y].x > 0)){
w2 = Water[x+1,y];
s2 = Sed[x+1,y];
h2 = HeightMap[x+1,y];
w3 = (w1+w2+h2-h1)/2;
w4 = w1+w2-w3;
s3 = ((s1*(w1-w3))/w1)+s1;
s4 = s1+s2-s3;
Water[x,y] = w3;
Sed[x,y] = s3;
Water[x+1,y] = w4;
Sed[x+1,y] = s4;
}
if ((x > 0) && (Velocity[x,y].x < 0)){
w2 = Water[x-1,y];
s2 = Sed[x-1,y];
h2 = HeightMap[x-1,y];
w3 = (w1+w2+h2-h1)/2;
w4 = w1+w2-w3;
s3 = ((s1*(w1-w3))/w1)+s1;
s4 = s1+s2-s3;
Water[x,y] = w3;
Sed[x,y] = s3;
Water[x-1,y] = w4;
Sed[x-1,y] = s4;
}
if ((y < MapSize-1) && (Velocity[x,y].y > 0)){
w2 = Water[x,y+1];
s2 = Sed[x,y+1];
h2 = HeightMap[x,y+1];
w3 = (w1+w2+h2-h1)/2;
w4 = w1+w2-w3;
s3 = ((s1*(w1-w3))/w1)+s1;
s4 = s1+s2-s3;
Water[x,y] = w3;
Sed[x,y] = s3;
Water[x,y+1] = w4;
Sed[x,y+1] = s4;
}
if ((y > 0) && (Velocity[x,y].y < 0)){
w2 = Water[x,y-1];
s2 = Sed[x,y-1];
h2 = HeightMap[x,y-1];
w3 = (w1+w2+h2-h1)/2;
w4 = w1+w2-w3;
s3 = ((s1*(w1-w3))/w1)+s1;
s4 = s1+s2-s3;
Water[x,y] = w3;
Sed[x,y] = s3;
Water[x,y-1] = w4;
Sed[x,y-1] = s4;
}
Water[x,y] *= EvapRate;// evaperate some water
}
}
}`
Thanks
but i just cant seem to get it working how i want , look through this code and see if they know where the problem is , not sure if its my method or just the variable quantities
Need more information, cannot guess the problem. What is the desired result, and what is the current result?
k sory it was like 4 o clock and i wanted to get some sleep ok so basically its designed to erode at a height map and smoth out rough terrain as well as form rivers and such things howether its actuly making the terrain alot more jagid so its doing the opiset of what its ment to i guess its probably the deposition and moment of sediment. left is with water erosion(meant to be smooth) right is without it
Answer by Dmitry Soldatenkov · Feb 26, 2016 at 08:15 PM
http://forum.unity3d.com/threads/terrain-water-erosion.388374/ This asset contains all the source code for erosion you want.