Skip to content

Commit 21ac91e

Browse files
authored
added underwater.glsl
based on https://www.shadertoy.com/view/lljGDt
1 parent 531df80 commit 21ac91e

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

underwater.glsl

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// adapted by Alex Sherwin for Ghostty from https://www.shadertoy.com/view/lljGDt
2+
3+
#define BLACK_BLEND_THRESHOLD .4
4+
5+
float hash21(vec2 p) {
6+
p = fract(p * vec2(233.34, 851.73));
7+
p += dot(p, p + 23.45);
8+
return fract(p.x * p.y);
9+
}
10+
11+
float rayStrength(vec2 raySource, vec2 rayRefDirection, vec2 coord, float seedA, float seedB, float speed)
12+
{
13+
vec2 sourceToCoord = coord - raySource;
14+
float cosAngle = dot(normalize(sourceToCoord), rayRefDirection);
15+
16+
// Add subtle dithering based on screen coordinates
17+
float dither = hash21(coord) * 0.015 - 0.0075;
18+
19+
float ray = clamp(
20+
(0.45 + 0.15 * sin(cosAngle * seedA + iTime * speed)) +
21+
(0.3 + 0.2 * cos(-cosAngle * seedB + iTime * speed)) + dither,
22+
0.0, 1.0);
23+
24+
// Smoothstep the distance falloff
25+
float distFade = smoothstep(0.0, iResolution.x, iResolution.x - length(sourceToCoord));
26+
return ray * mix(0.5, 1.0, distFade);
27+
}
28+
29+
void mainImage( out vec4 fragColor, in vec2 fragCoord )
30+
{
31+
vec2 uv = fragCoord.xy / iResolution.xy;
32+
33+
uv.y = 1.0 - uv.y;
34+
vec2 coord = vec2(fragCoord.x, iResolution.y - fragCoord.y);
35+
36+
// Set the parameters of the sun rays
37+
vec2 rayPos1 = vec2(iResolution.x * 0.7, iResolution.y * 1.1);
38+
vec2 rayRefDir1 = normalize(vec2(1.0, 0.116));
39+
float raySeedA1 = 36.2214;
40+
float raySeedB1 = 21.11349;
41+
float raySpeed1 = 1.1;
42+
43+
vec2 rayPos2 = vec2(iResolution.x * 0.8, iResolution.y * 1.2);
44+
vec2 rayRefDir2 = normalize(vec2(1.0, -0.241));
45+
const float raySeedA2 = 22.39910;
46+
const float raySeedB2 = 18.0234;
47+
const float raySpeed2 = 0.9;
48+
49+
// Calculate the colour of the sun rays on the current fragment
50+
vec4 rays1 =
51+
vec4(1.0, 1.0, 1.0, 0.0) *
52+
rayStrength(rayPos1, rayRefDir1, coord, raySeedA1, raySeedB1, raySpeed1);
53+
54+
vec4 rays2 =
55+
vec4(1.0, 1.0, 1.0, 0.0) *
56+
rayStrength(rayPos2, rayRefDir2, coord, raySeedA2, raySeedB2, raySpeed2);
57+
58+
vec4 col = rays1 * 0.5 + rays2 * 0.4;
59+
60+
// Attenuate brightness towards the bottom, simulating light-loss due to depth.
61+
// Give the whole thing a blue-green tinge as well.
62+
float brightness = 1.0 - (coord.y / iResolution.y);
63+
col.r *= 0.05 + (brightness * 0.8);
64+
col.g *= 0.15 + (brightness * 0.6);
65+
col.b *= 0.3 + (brightness * 0.5);
66+
67+
vec2 termUV = fragCoord.xy / iResolution.xy;
68+
vec4 terminalColor = texture(iChannel0, termUV);
69+
70+
float alpha = step(length(terminalColor.rgb), BLACK_BLEND_THRESHOLD);
71+
vec3 blendedColor = mix(terminalColor.rgb * 1.0, col.rgb * 0.3, alpha);
72+
73+
fragColor = vec4(blendedColor, terminalColor.a);
74+
}

0 commit comments

Comments
 (0)