Skip to content

Instantly share code, notes, and snippets.

@mathewmariani
Created February 10, 2025 22:53
Show Gist options
  • Save mathewmariani/3fea3d3480ad750229c89d7ea6dfcb3c to your computer and use it in GitHub Desktop.
Save mathewmariani/3fea3d3480ad750229c89d7ea6dfcb3c to your computer and use it in GitHub Desktop.
#version 300 es
precision mediump float;
out vec4 FragColor;
in vec3 to_camera;
// const vec3 reflect_color = vec3(0.3843, 0.6509, 0.6627);
// const vec3 refract_color = vec3(0.3843, 0.6509, 0.6627);
const vec4 reflect_color = vec4(1.0, 0.0, 0.0, 1.0);
const vec4 refract_color = vec4(0.0, 1.0, 0.0, 0.6);
void main()
{
// fresnel effect
float refractiveFactor = dot(normalize(to_camera), vec3(0.0, 1.0, 0.0));
vec4 water_color = mix(reflect_color, refract_color, refractiveFactor);
FragColor = vec4(water_color);
}
#version 300 es
precision mediump float;
precision mediump sampler2D;
out vec4 FragColor;
in vec2 texcoords;
// uniforms
uniform sampler2D texture;
uniform float lod_bias;
uniform float tiling;
uniform float time;
uniform float top_scale;
uniform float bottom_scale;
uniform float brightness_lower_cutoff;
uniform float brightness_upper_cutoff;
float calculateLODBias(vec2 uv)
{
// Compute derivatives of texture coordinates to estimate LOD
vec2 dx = dFdx(uv);
vec2 dy = dFdy(uv);
// Calculate the rate of change of texture coordinates
// Use the max derivative length
float lod = max(length(dx), length(dy));
// Apply log2 and add a negative bias to switch to lower detail mipmaps faster
lod = log2(lod) + lod_bias;
return lod;
}
void main()
{
float lod = calculateLODBias(texcoords);
vec2 uv = texcoords * tiling;
// multiple samplers to add some depth
vec4 smp1 = textureLod(texture, uv + vec2(1.0, 0.0) * (time * 0.35), lod);
vec4 smp2 = textureLod(texture, uv + vec2(0.0, 1.0) * (time * 0.35), lod);
vec4 color = smp1 + smp2;
float brightness = dot(color.rgb, vec3(0.299, 0.587, 0.114));
if (brightness <= brightness_lower_cutoff || brightness > brightness_upper_cutoff)
{
discard;
}
FragColor = color;
}
#version 300 es
precision mediump float;
precision mediump sampler2D;
out vec4 FragColor;
in vec2 texcoords;
// uniforms
uniform sampler2D water_texture;
uniform float tiling;
uniform float time;
uniform vec3 color;
uniform vec2 direction;
uniform float Ts;
uniform float Bs;
uniform float lod_bias;
float calculateLODBias(vec2 uv)
{
// Compute derivatives of texture coordinates to estimate LOD
vec2 dx = dFdx(uv);
vec2 dy = dFdy(uv);
// Calculate the rate of change of texture coordinates
// Use the max derivative length
float lod = max(length(dx), length(dy));
// Apply log2 and add a negative bias to switch to lower detail mipmaps faster
lod = log2(lod) + lod_bias;
return lod;
}
void main()
{
float lod = calculateLODBias(texcoords);
// give direction and motion
vec2 dir = normalize(direction);
vec2 uv = texcoords * tiling + vec2(time * dir);
// add a rippling effect by displacing the lookup
uv.y += 0.01 * (sin(uv.x * 3.5 + time * 0.35) + sin(uv.x * 4.8 + time * 1.05) + sin(uv.x * 7.3 + time * 0.45)) / 3.0;
uv.x += 0.12 * (sin(uv.y * 4.0 + time * 0.5) + sin(uv.y * 6.8 + time * 0.75) + sin(uv.y * 11.3 + time * 0.2)) / 3.0;
uv.y += 0.12 * (sin(uv.x * 4.2 + time * 0.64) + sin(uv.x * 6.3 + time * 1.65) + sin(uv.x * 8.2 + time * 0.45)) / 3.0;
// multiple samplers to add some depth
vec4 smp1 = textureLod(water_texture, uv * 1.0, lod);
vec4 smp2 = textureLod(water_texture, uv * 1.0 + vec2(0.2), lod);
// combine color and sampler values
FragColor = vec4(color + vec3(smp1.a * Ts - smp2.a * Bs), 1.0);
}
#version 300 es
precision mediump float;
// attributes
layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec2 in_texcoord;
// uniforms
uniform mat4 model;
uniform mat4 view_proj;
uniform float scale;
uniform float strength;
uniform float tiling;
uniform float time;
uniform vec3 color;
uniform vec2 direction;
out vec2 texcoords;
float calculateSurface(float x, float z) {
float y = 0.0;
y += (sin(x * 1.0 / scale + time * 1.0) + sin(x * 2.3 / scale + time * 1.5) + sin(x * 3.3 / scale + time * 0.4)) / 3.0;
y += (sin(z * 0.2 / scale + time * 1.8) + sin(z * 1.8 / scale + time * 1.8) + sin(z * 2.8 / scale + time * 0.8)) / 3.0;
return y;
}
void main()
{
texcoords = in_texcoord;
vec3 pos = in_position;
pos.y += calculateSurface(pos.x, pos.z) * strength;
pos.y -= calculateSurface(0.0, 0.0) * strength;
gl_Position = view_proj * model * vec4(pos, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment