better water
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / water.h
index 67b07454cbb3eb7a22117bc0d51a49feba8c56de..995cdb8012adf7b69c119c254c098afa02f04c74 100644 (file)
@@ -30,7 +30,7 @@ static struct vg_shader _shader_water = {
 "   gl_Position = uPv * vec4(world_pos,1.0);\n"
 "\n"
 "   vec2 depth_coords = (world_pos.xz-uDepthBounds.xy)*uDepthBounds.zw;\n"
-"   aUv = vec4(world_pos.xz*0.01,depth_coords);\n"
+"   aUv = vec4(world_pos.xz*0.005,depth_coords);\n"
 "   aCo = world_pos;\n"
 "\n"
 "   aDepth = gl_Position.z;\n"
@@ -40,6 +40,31 @@ static struct vg_shader _shader_water = {
 {
 .orig_file = "../shaders/water.fs",
 .static_src = 
+"vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
+"      vec4 beneath, vec4 above )\n"
+"{\n"
+"   vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );\n"
+"   vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );\n"
+"   vec3 surface_tint = mix(colour_shore, colour_ocean, depthvalue);\n"
+"\n"
+"   float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
+"\n"
+"   vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
+"   vec3 specdir = reflect( -lightdir, vnorm );\n"
+"   float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
+"   \n"
+"   // Depth \n"
+"   float depthblend = pow( beneath.a,0.8 );\n"
+"\n"
+"   // Composite\n"
+"   vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
+"   //vsurface += spec;\n"
+"\n"
+"   return vec4( vsurface,depthblend );\n"
+"}\n"
+"\n"
+"#line      2        0 \n"
+"\n"
 "out vec4 FragColor;\n"
 "\n"
 "uniform sampler2D uTexMain;\n"
@@ -59,45 +84,38 @@ static struct vg_shader _shader_water = {
 "\n"
 "void main()\n"
 "{\n"
-"   // Reflected and warped texture\n"
+"   // Create texture coords\n"
 "   vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
-"\n"
-"   vec4 dudva = texture( uTexDudv, aUv.xy - vec2(uTime*0.004f,uTime*0.003f) );\n"
-"   vec4 dudvb = texture( uTexDudv, aUv.xy*0.7 - vec2(uTime*0.01,uTime*0.0054) );\n"
-"   vec2 distortamt = (dudva.rg-0.5) * (dudvb.ba-0.5) * 0.6;\n"
-"\n"
-"   vec4 reflected = texture( uTexMain, ssuv+distortamt );\n"
 "   \n"
 "   // Surface colour composite\n"
 "   float depthvalue = texture( uTexDepth, aUv.zw ).r;\n"
 "\n"
-"   vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );\n"
-"   vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );\n"
-"   vec3 surface_tint = mix(colour_shore, colour_ocean, pow(depthvalue,1.8))*1.5;\n"
+"   vec4 dudva = texture(uTexDudv, aUv.xy + vec2(uTime*0.008,uTime*0.006))-0.5;\n"
+"   vec4 dudvb = texture(uTexDudv, aUv.xy*7.0-vec2(uTime*0.003,uTime*0.03))-0.5;\n"
+"\n"
+"   vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
+"   surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
 "   \n"
 "   // Foam\n"
-"   float fband = fract( aCo.z*0.1+uTime*0.1-depthvalue*10.0 );\n"
-"   fband = step( fband+dudvb.g*0.8, 0.5 ) * max((1.0-depthvalue*4.0),0.0);\n"
+"   float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
+"   fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
 "\n"
 "   // Lighting\n"
-"   vec3 surfnorm = vec3(distortamt.x,1.0,distortamt.y);\n"
-"   \n"
 "   vec3 halfview = -normalize( aCo-uCamera );\n"
-"   float ffresnel = pow(1.0-dot( surfnorm, halfview ),5.0);\n"
 "\n"
-"   vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
-"   vec3 specdir = reflect( -lightdir, surfnorm );\n"
-"   float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
-"   \n"
-"   // Depth \n"
-"   vec4 backsample = texture( uTexBack, ssuv+distortamt*0.1 );\n"
-"   float depthblend = pow(backsample.a,0.8);\n"
+"   // Sample textures\n"
+"   vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
+"   vec4 beneath = texture( uTexBack, ssuv );\n"
+"\n"
+"   // Fog\n"
+"   //vec4 horizon = vec4( 0.5, 0.6, 0.9, 1.0 );\n"
+"   vec4 horizon = vec4( 0.7,0.8,0.88, 1.0 );\n"
+"   float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
 "\n"
 "   // Composite\n"
-"   vec3 vsurface = mix(surface_tint*backsample.rgb, reflected.rgb, ffresnel );\n"
-"   vsurface += spec;\n"
-"   \n"
-"   FragColor = mix( vec4(vsurface,depthblend), vec4(1.0,1.0,1.0,0.8), fband );\n"
+"   vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
+"   vsurface.a -= fdist;\n"
+"   FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
 "}\n"
 ""},
 };