Waterflow
Visualize water in terrain
|
00001 00002 00003 #version 430 00004 00005 layout(local_size_x = 16, local_size_y = 16) in; 00006 00007 layout (std430,binding = 4) readonly buffer height0 00008 { 00009 float u0[]; 00010 00011 }; 00012 00013 layout (std430, binding = 5) writeonly buffer height1 00014 { 00015 float u1[]; 00016 }; 00017 00018 layout (std430,binding = 6) buffer velocity 00019 { 00020 float v[]; 00021 00022 }; 00023 00024 layout (std430,binding = 7) readonly buffer terrainHeight 00025 { 00026 float terrHeight[]; 00027 00028 }; 00029 00030 uniform ivec2 size; 00031 uniform float dt; 00032 00036 float getHeight(int i, int j, float ourTot, float ourTerr){ 00037 if( i < 0 || j < 0 || i > size.x -1 || j > size.y -1){ 00038 return 0.0f; 00039 } 00040 00041 i = clamp(i,0,size.x-1); 00042 j = clamp(j,0,size.y-1); 00043 float theirTot = u0[i+j*size.x]; 00044 float theirTerr = terrHeight[i+j*size.x]; 00045 float theirWater = theirTot - theirTerr; 00046 float ourWater = ourTot - ourTerr; 00047 float diff = theirTot - ourTot;//ourTot - theirTot; 00048 // 00049 return clamp(diff,-ourWater/4.0f,theirWater/4.0f); 00050 } 00051 00055 void main(){ 00056 //determine where to sample 00057 ivec2 storePos = ivec2(gl_GlobalInvocationID.xy); 00058 int i = storePos.x; 00059 int j = storePos.y; 00060 00061 //Change these to clamps. 00062 if(i < size.x && j < size.y) { 00063 int offset = (i + j*size.x); 00064 float c2 = 25000.0f; 00065 00066 float h2 = 4.0f; 00067 00068 float ourHeight = u0[offset]; 00069 float ourTerr = terrHeight[offset]; 00070 float u_east = getHeight((i+1), j, ourHeight, ourTerr); 00071 float u_west = getHeight((i-1), j, ourHeight,ourTerr); 00072 float u_south = getHeight(i, (j-1), ourHeight,ourTerr); 00073 float u_north = getHeight(i, (j+1), ourHeight,ourTerr); 00074 00075 float f = c2/h2*(u_west + u_east + u_south + u_north); 00076 00077 //f = clamp(f, -20.0f, 20.0f); 00078 00079 float vel =v[offset] + f*dt; 00080 float height = max((ourHeight + vel * dt),ourTerr); 00081 u1[offset] = height; 00082 00083 if(height - ourTerr <= 0.0f){ 00084 v[offset] = 0.0f;// *0.9999995f 00085 }else{ 00086 v[offset] = vel *0.96f; 00087 } 00088 } 00089 }