Skip to content

Instantly share code, notes, and snippets.

@native-m
Last active July 19, 2018 09:36
Show Gist options
  • Select an option

  • Save native-m/027615e527df5820f3b89185b987b550 to your computer and use it in GitHub Desktop.

Select an option

Save native-m/027615e527df5820f3b89185b987b550 to your computer and use it in GitHub Desktop.
Water Displacement (ShaderToy)
// Water displacement (MIT License)
// Native-M
float rand(vec2 p)
{
return fract(sin(dot(p, vec2(93.9898, 60.1414))) * 43758.5453);
}
float noise(vec2 uv, float blockiness)
{
vec2 lv = fract(uv);
vec2 id = floor(uv);
float n1 = rand(id);
float n2 = rand(id+vec2(1,0));
float n3 = rand(id+vec2(0,1));
float n4 = rand(id+vec2(1,1));
vec2 u = smoothstep(0.0, 1.0 + blockiness, lv);
return mix(mix(n1, n2, u.x), mix(n3, n4, u.x), u.y);
}
float fbm(vec2 uv, int count, float blockiness, float complexity)
{
float val = 0.0;
float amp = 0.5;
while(count != 0)
{
val += amp * noise(uv, blockiness);
amp *= 0.5;
uv *= complexity;
count--;
}
return val;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
vec2 uv2; // UV for background image
uv2 = uv;
uv.x *= iResolution.x/iResolution.y; // fix aspect ratio
// Rescale
uv *= .5;
// We use fractal noise to generate the displacement
// FBM Domain wrapping (http://www.iquilezles.org/www/articles/warp/warp.htm)
// f(p) = fbm( p + fbm( p + fbm( p ) ) )
float n = fbm(uv + iTime + fbm(uv + fbm(uv + iTime * 0.01, 8, 0.0, 2.0), 8, 0.0, 2.0) * 100.0, 2, 0.0, 2.0);
// Apply the displacement
vec3 col;
col = texture(iChannel0, vec2(uv2.x + n * 0.12, uv2.y + n * 0.12)).rgb;
// Mix the noise a bit
col = mix(col, vec3(n), 0.3);
// Output to screen
fragColor = vec4(col,1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment