semi-transparent water rendering (TODO)
This commit is contained in:
@ -24,7 +24,7 @@ out vec4 outputColor;
|
||||
|
||||
const float gamma = 2.2;
|
||||
|
||||
const float ambient = 0.3, specularStrength = 0.5, specularShininess = 32;
|
||||
const float ambient = 0.3, specularStrength = 0.08, specularShininess = 8;
|
||||
const float fogDensity = .00003;
|
||||
|
||||
|
93
internal/asset/shader/world/water.frag
Normal file
93
internal/asset/shader/world/water.frag
Normal file
@ -0,0 +1,93 @@
|
||||
#version 330
|
||||
|
||||
uniform sampler2D tex;
|
||||
uniform sampler2D shadowmap;
|
||||
uniform vec3 viewPos;
|
||||
uniform vec3 sun;
|
||||
uniform vec4 fogColor;
|
||||
|
||||
uniform float alpha; // Alpha of the semi-transparant layer
|
||||
|
||||
// Fragment information
|
||||
in vec4 fragPos;
|
||||
in vec4 fragPosLightspace;
|
||||
in vec3 fragNormal;
|
||||
in vec2 fragTexCoord;
|
||||
vec4 fragColor;
|
||||
|
||||
out vec4 outputColor;
|
||||
|
||||
|
||||
const float gamma = 2.2;
|
||||
|
||||
const float ambient = 0.3, specularStrength = 1.6, specularShininess = 128;
|
||||
const float fogDensity = .00003;
|
||||
|
||||
|
||||
float finalpha;
|
||||
float light;
|
||||
vec4 texpixel, color;
|
||||
void lightSun();
|
||||
float lightSunShadow();
|
||||
void lightPoint(int i);
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
fragColor = vec4(pow(texture(tex, fragTexCoord).rgb, vec3(gamma)), 1.0f);
|
||||
|
||||
finalpha = alpha;
|
||||
light = ambient;
|
||||
|
||||
lightSun();
|
||||
|
||||
color += vec4(fragColor.rgb * light, 0.0f);
|
||||
color.a = fragColor.a;
|
||||
color.rgb = pow(color.rgb, vec3(1.0/gamma));
|
||||
|
||||
float z = gl_FragCoord.z / gl_FragCoord.w;
|
||||
float fog = clamp(exp(-fogDensity * z * z), 0.2, 1);
|
||||
|
||||
outputColor = mix(fogColor, color, fog) * finalpha;
|
||||
}
|
||||
|
||||
void lightSun() {
|
||||
/* Diffuse */
|
||||
vec3 lightDir = sun;
|
||||
float diffuse = max(dot(fragNormal, lightDir), 0.0f);
|
||||
|
||||
/* Specular */
|
||||
vec3 viewDir = normalize(viewPos - fragPos.xyz);
|
||||
vec3 reflectDir = reflect(-lightDir, fragNormal);
|
||||
float specular = specularStrength * pow(max(dot(viewDir, reflectDir), 0.0), specularShininess);
|
||||
if (specular > 1.0f) {
|
||||
finalpha = min(finalpha + specular - 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
float shadow = lightSunShadow();
|
||||
light += diffuse * shadow;
|
||||
color += vec4(vec3(specular), 0.0f) * shadow;
|
||||
finalpha = min(finalpha + 0.1f * shadow, 1.0f);
|
||||
}
|
||||
|
||||
float lightSunShadow() {
|
||||
/* Shadow */
|
||||
float bias = max(0.0013 * (1.0 - dot(fragNormal, sun)), 0.0001);
|
||||
vec3 projCoords = fragPosLightspace.xyz / fragPosLightspace.w;
|
||||
projCoords = projCoords*0.5 + 0.5;
|
||||
float closestDepth = texture(shadowmap, projCoords.xy).r;
|
||||
float currentDepth = projCoords.z;
|
||||
float shadow = 0;
|
||||
|
||||
if (currentDepth > 1.0f || currentDepth < 0.0f)
|
||||
return 1.0f;
|
||||
|
||||
//vec2 texelSize = clamp((currentDepth+bias-closestDepth)*100.0f, 0.05f, 1.5f) / textureSize(shadowmap, 0);
|
||||
vec2 texelSize = 0.4 / textureSize(shadowmap, 0);
|
||||
for (int x=-4; x<=4; ++x)
|
||||
for (int y=-4; y<=4; ++y) {
|
||||
float pcfDepth = texture(shadowmap, projCoords.xy + vec2(x,y)*texelSize).r;
|
||||
shadow += currentDepth-bias < pcfDepth ? 1.0f : 0.0f;
|
||||
}
|
||||
return min(shadow/81.0f, 1.0f);
|
||||
}
|
26
internal/asset/shader/world/water.vert
Normal file
26
internal/asset/shader/world/water.vert
Normal file
@ -0,0 +1,26 @@
|
||||
#version 330
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform mat4 view;
|
||||
uniform mat4 model;
|
||||
uniform mat4 lightspace;
|
||||
|
||||
layout (location = 0) in vec3 vert;
|
||||
layout (location = 1) in vec3 normal;
|
||||
layout (location = 2) in vec2 texCoord;
|
||||
layout (location = 4) in float light;
|
||||
|
||||
out vec4 fragPos;
|
||||
out vec4 fragPosLightspace;
|
||||
out vec3 fragNormal;
|
||||
out vec2 fragTexCoord;
|
||||
|
||||
|
||||
void main() {
|
||||
fragTexCoord = texCoord;
|
||||
fragPos = model * vec4(vert, 1);
|
||||
fragPosLightspace = lightspace * fragPos;
|
||||
fragNormal = normal;
|
||||
gl_Position = projection * view * fragPos;
|
||||
}
|
||||
|
Reference in New Issue
Block a user