diff options
author | jjesswan <jessica_wan@brown.edu> | 2024-04-22 21:56:26 -0400 |
---|---|---|
committer | jjesswan <jessica_wan@brown.edu> | 2024-04-22 21:56:26 -0400 |
commit | a556b45abf18f1bd509daaf63b66b7d55e9fd291 (patch) | |
tree | bc9b8a2d184c12aee236e7f9f276a34b84ca552d /engine-ocean/Resources/Shaders/water.frag | |
parent | cd7c76017a12bb548036571c1ff13e551369d06d (diff) |
add engine version
Diffstat (limited to 'engine-ocean/Resources/Shaders/water.frag')
-rw-r--r-- | engine-ocean/Resources/Shaders/water.frag | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/engine-ocean/Resources/Shaders/water.frag b/engine-ocean/Resources/Shaders/water.frag new file mode 100644 index 0000000..b86e3a9 --- /dev/null +++ b/engine-ocean/Resources/Shaders/water.frag @@ -0,0 +1,63 @@ +#version 330 core +// Uniforms for shape information +//in vec3 worldSpace_pos; +//in vec3 worldSpace_norm; + +in float visibility; +in vec4 clipSpace; +in vec2 tex_coord; +in vec3 toCameraVector; + + + +// Camera uniform +uniform vec3 worldSpace_camPos; +uniform sampler2D reflect_texture; +uniform sampler2D refract_texture; +uniform sampler2D du_dv_map; +uniform float moveFactor; // offset that changes over time + +out vec4 fragColor; + +const float distortionStrength = .02f; +const vec4 waterColor = vec4(.06f, .49f, .59f, 1.f); +uniform vec3 skyColor; + + +void main() { + + // screen space coords + vec2 ndc = (clipSpace.xy/clipSpace.w)/2.f + .5f; + + vec2 refractTexCoords = ndc; + vec2 reflectTexCoords = vec2(ndc.x, ndc.y); + + // get associated rg color in the range -1,1 from du_dv map + vec2 distortion1 = (texture(du_dv_map, vec2(tex_coord.x + moveFactor, tex_coord.y)).rg*2.f - 1.f) * distortionStrength; + vec2 distortion2 = (texture(du_dv_map, vec2(-tex_coord.x + moveFactor, tex_coord.y + moveFactor)).rg*2.f - 1.f) * distortionStrength; + vec2 totalDistortion = distortion1 + distortion2; + + refractTexCoords += totalDistortion; + reflectTexCoords += totalDistortion; + + refractTexCoords = clamp(refractTexCoords, 0.001f, .999f); + reflectTexCoords = clamp(reflectTexCoords, 0.001f, .999f); + + // fresnel effect + vec3 viewVector = normalize(toCameraVector); + float refractiveFactor = dot(viewVector, vec3(0.f, 1.f, 0.f)); + refractiveFactor = pow(refractiveFactor, 5.f); // changes how reflective water is + + + + + + vec4 reflect = texture(reflect_texture, reflectTexCoords); + vec4 refract = texture(refract_texture, refractTexCoords); + + + // mix actual color with fog color + fragColor = mix(reflect, refract, refractiveFactor); + fragColor = mix(fragColor, waterColor, .2f); + fragColor = mix(vec4(skyColor, 1.f), fragColor, visibility); +} |