diff --git a/internal/asset/shader/world/water.frag b/internal/asset/shader/world/water.frag index 52856d6..b2f666c 100644 --- a/internal/asset/shader/world/water.frag +++ b/internal/asset/shader/world/water.frag @@ -1,15 +1,19 @@ #version 330 +uniform sampler2D gPos; uniform sampler2D tex; uniform sampler2D shadowmap; uniform vec3 viewPos; uniform vec3 sun; uniform vec4 fogColor; +uniform vec2 screenSize; uniform float alpha; // Alpha of the semi-transparant layer // Fragment information in vec4 fragPos; +in vec4 fragPosView; +in vec4 fragPosScreen; in vec4 fragPosLightspace; in vec3 fragNormal; in vec2 fragTexCoord; @@ -24,7 +28,7 @@ const float ambient = 0.3, specularStrength = 1.6, specularShininess = 128; const float fogDensity = .00003; -float finalpha; +float sunalpha = 0; float light; vec3 color = vec3(0); void lightSun(); @@ -36,7 +40,6 @@ void main() { fragColor = vec4(texture(tex, fragTexCoord).rgb, 1.0f); - finalpha = alpha; light = ambient; lightSun(); @@ -46,7 +49,10 @@ void main() { //float z = gl_FragCoord.z / gl_FragCoord.w; //float fog = clamp(exp(-fogDensity * z * z), 0.2, 1); - outputColor = vec4(color, 1) * finalpha; + float waterDepth = (fragPosView.z-texture(gPos, gl_FragCoord.xy/screenSize).a); + + float waterFactor = pow(alpha, waterDepth); + outputColor = vec4(color*waterFactor, min(1-waterFactor + sunalpha, 1)); } void lightSun() { @@ -63,10 +69,9 @@ void lightSun() { light += diffuse * shadow; color += vec3(specular) * shadow; - if (specular*shadow > 1.0f) { - finalpha = min(finalpha + specular - 1.0f, 1.0f); - } - finalpha = min(finalpha + 0.1f * shadow, 1.0f); + if (specular*shadow > 1.0f) + sunalpha += specular*shadow - 1.0f; + sunalpha += 0.1f*shadow; } float lightSunShadow() { diff --git a/internal/asset/shader/world/water.vert b/internal/asset/shader/world/water.vert index f6f917b..987e4ab 100644 --- a/internal/asset/shader/world/water.vert +++ b/internal/asset/shader/world/water.vert @@ -11,6 +11,7 @@ layout (location = 2) in vec2 texCoord; layout (location = 4) in float light; out vec4 fragPos; +out vec4 fragPosView; out vec4 fragPosLightspace; out vec3 fragNormal; out vec2 fragTexCoord; @@ -19,8 +20,9 @@ out vec2 fragTexCoord; void main() { fragTexCoord = texCoord; fragPos = model * vec4(vert, 1); + fragPosView = view * fragPos; fragPosLightspace = lightspace * fragPos; fragNormal = normal; - gl_Position = projection * view * fragPos; + gl_Position = projection * fragPosView; } diff --git a/internal/render/render_world.go b/internal/render/render_world.go index f85dcdf..3100cfc 100644 --- a/internal/render/render_world.go +++ b/internal/render/render_world.go @@ -222,6 +222,7 @@ func (r *WorldRenderer) Init(w *world.World) (err error) { r.lighting.shader.SetUniformTextureHandle("gColor", r.gbuffer.color) r.ssao.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos) r.ssao.shader.SetUniformTextureHandle("gNorm", r.gbuffer.norm) + r.water.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos) // generate SSAO friends gl.GenFramebuffers(1, &r.ssao.fbo) @@ -430,7 +431,7 @@ func (r *WorldRenderer) Render(world *world.World, view *View) { gl.DepthFunc(gl.LESS) gl.Enable(gl.CULL_FACE) gl.Enable(gl.BLEND) - gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) + gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BlendEquation(gl.FUNC_ADD) r.water.shader.UseProgram() r.water.shader.BindTextures() @@ -442,6 +443,7 @@ func (r *WorldRenderer) Render(world *world.World, view *View) { r.water.shader.SetUniformVec4f("fogColor", io.FogColor) r.water.shader.SetUniformVec3f("sun", normalSun) r.water.shader.SetUniformFloat("alpha", alpha) + r.water.shader.SetUniformVec2f("screenSize", r.lastDisplaySize.ToFloat32()) world.RenderWater()