@@ -10,11 +10,21 @@ in vec2 texcoord;
1010/* RENDERTARGETS: 0 */
1111layout (location = 0 ) out vec4 color;
1212
13- vec4 getRefractedColor() {
13+ vec4 computeWaterLighting(vec2 uv) {
14+ vec4 colorOut = texture2D (colortex0, uv);
15+ float amountLighting = clamp (1.0 - getSunAmount(), 0.0 , 1.0 );
16+
17+ if (isEyeInWater == 0 ) {
18+ colorOut *= mix (getWaterLighting(uv), vec4 (1.0 ), amountLighting);
19+ }
20+
21+ return colorOut;
22+ }
23+
24+ vec4 getRefractedColor(vec4 stencil) {
1425 // Code from: Water Shader Mod by Necrowizzard
1526 float in_water;
1627 vec4 colorOut;
17- vec4 stencil = getStencil(texcoord);
1828
1929 if (stencil.r > 0.05 )
2030 in_water = 1.0 ;
@@ -26,7 +36,7 @@ vec4 getRefractedColor() {
2636 float z_scale = 1.0 ;
2737
2838 float used_timer = frameTimeCounter;
29- float time_scale = 0.275 ;
39+ float time_scale = 0.250 ;
3040 float size_scale = 1.6 * 6.3 ;
3141
3242 if (stencil.r <= 0.15 ) {
@@ -36,39 +46,39 @@ vec4 getRefractedColor() {
3646 size_scale *= stencil.r;
3747 }
3848
39- // timer needs to be 'in period'
49+ // timer needs to be 'in period'
4050 if (stencil.r >= 0.5 ) {
41- x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.5 * size_scale * 3.14159 * stencil.g) + (size_scale * 3.14159 * stencil.g)) / 200 .0 )); // scales btw 0.995 and 1.005
51+ x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.5 * size_scale * 3.14159 * stencil.g) + (size_scale * 3.14159 * stencil.g)) / 150 .0 )); // scales btw 0.995 and 1.005
4252 }
4353 z_scale = (1.0 + (sin (sin (time_scale * 3.14159 * used_timer) + 1.5 * sin (0.8 * size_scale * 3.14159 * stencil.b)) / 200.0 ));
4454
4555 vec2 disturbed = vec2 (x_scale * texcoord.x, z_scale * texcoord.y);
4656
4757 time_scale = 0.45 ;
4858
49- // 'refraction'(for all under-water)
59+ // 'refraction' (for all under-water)
5060 if (in_water > 0.05 ) {
51- float look_up_range = 0.001 ;
61+ float look_up_range = 0.008 ;
5262 float limit = 0.001 ;
53- // costs performance! (masking to avoid outside water look-ups, alternative another scene clipping)
63+ // costs performance! (masking to avoid outside water look-ups, alternative another scene clipping)
5464 if (getStencil(vec2 (disturbed.r + look_up_range, disturbed.g + look_up_range)).r > limit &&
5565 getStencil(vec2 (disturbed.r - look_up_range, disturbed.g - look_up_range)).r > limit &&
5666 getStencil(vec2 (disturbed.r, disturbed.g)).r > limit) {
57- colorOut = texture2D (colortex0, disturbed.rg); // drunken effect without stencil if
67+ // drunken effect without stencil if
68+ colorOut = computeWaterLighting(disturbed);
5869 } else {
59- colorOut = texture2D (colortex0, texcoord.rg );
70+ colorOut = computeWaterLighting( texcoord);
6071 }
6172 } else {
62- colorOut = texture2D (colortex0, texcoord.rg );
73+ colorOut = computeWaterLighting( texcoord);
6374 }
6475
6576 return vec4 (vec3 (colorOut), 1.0 );
6677}
6778
68- vec4 getReflectedColor() {
79+ vec4 getReflectedColor(vec4 stencil ) {
6980 // Code from: Water Shader Mod by Necrowizzard
7081 float in_water;
71- vec4 stencil = getStencil(texcoord);
7282
7383 if (stencil.r > 0.05 )
7484 in_water = 1.0 ;
@@ -92,23 +102,25 @@ vec4 getReflectedColor() {
92102
93103 // timer needs to be 'in period'
94104 if (stencil.r >= 0.5 ) {
95- x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.5 * size_scale * 3.14159 * stencil.g) + (size_scale * 3.14159 * stencil.g)) / 200 .0 ));
105+ x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.5 * size_scale * 3.14159 * stencil.g) + (size_scale * 3.14159 * stencil.g)) / 150 .0 ));
96106 }
97- z_scale = (1.0 + (sin (sin (time_scale * 3.14159 * used_timer) + 1.5 * sin (0.8 * size_scale * 3.14159 * stencil.b)) / 200 .0 ));
107+ z_scale = (1.0 + (sin (sin (time_scale * 3.14159 * used_timer) + 1.5 * sin (0.8 * size_scale * 3.14159 * stencil.b)) / 150 .0 ));
98108
99109 vec2 disturbed = vec2 (x_scale * texcoord.x, z_scale * texcoord.y);
110+ disturbed = clamp (disturbed, 0.001 , 0.99 );
100111 vec4 reflection = getWaterReflectionColor(disturbed);
101112
102113 time_scale = 0.45 ;
103114 size_scale = 2.4 * 6.3 * stencil.r;
104115
105116 // timer needs to be 'in period'
106117 if (stencil.r >= 0.5 ) {
107- x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.25 * size_scale * 3.14159 * stencil.g) + size_scale * 3.14159 * stencil.g) / 250 .0 ));
118+ x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.25 * size_scale * 3.14159 * stencil.g) + size_scale * 3.14159 * stencil.g) / 150 .0 ));
108119 }
109- z_scale = (1.0 + (sin (sin (time_scale * 3.14159 * used_timer) + 1.5 * sin (size_scale * 3.14159 * stencil.b)) / 250 .0 ));
120+ z_scale = (1.0 + (sin (sin (time_scale * 3.14159 * used_timer) + 1.5 * sin (size_scale * 3.14159 * stencil.b)) / 150 .0 ));
110121
111122 vec2 disturbed_2 = vec2 (x_scale * texcoord.x, z_scale * texcoord.y);
123+ disturbed_2 = clamp (disturbed_2, 0.001 , 0.99 );
112124 vec4 reflection_2 = getWaterReflectionColor(disturbed_2);
113125 reflection = (reflection + reflection_2) / 2.0 ;
114126
@@ -120,10 +132,9 @@ vec4 getReflectedColor() {
120132 return reflection;
121133}
122134
123- void getSurfaceEffect(inout vec4 reflectionColor, inout vec4 waterRawColor) {
135+ void getSurfaceEffect(in vec4 stencil, inout vec4 reflectionColor, inout vec4 waterRawColor, in float fadeAmount ) {
124136 // Code from: Superbomb17
125137 float in_water;
126- vec4 stencil = getStencil(texcoord);
127138
128139 if (stencil.r > 0.05 )
129140 in_water = 1.0 ;
@@ -135,7 +146,7 @@ void getSurfaceEffect(inout vec4 reflectionColor, inout vec4 waterRawColor) {
135146 float z_scale = 1.0 ;
136147
137148 float used_timer = frameTimeCounter;
138- float time_scale = 0.275 ;
149+ float time_scale = 0.250 ;
139150 float size_scale = 1.6 * 6.3 ;
140151
141152 if (stencil.r <= 0.15 ) {
@@ -147,7 +158,7 @@ void getSurfaceEffect(inout vec4 reflectionColor, inout vec4 waterRawColor) {
147158
148159 // timer needs to be 'in period'
149160 if (stencil.r >= 0.5 ) {
150- x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.5 * size_scale * 3.14159 * stencil.g) + (size_scale * 3.14159 * stencil.g)) / 200 .0 ));
161+ x_scale = (1.0 + (sin (2.0 * time_scale * 3.14159 * used_timer - sin (0.5 * size_scale * 3.14159 * stencil.g) + (size_scale * 3.14159 * stencil.g)) / 150 .0 ));
151162 }
152163 z_scale = (1.0 + (sin (sin (time_scale * 3.14159 * used_timer) + 1.5 * sin (0.8 * size_scale * 3.14159 * stencil.b)) / 200.0 ));
153164
@@ -163,6 +174,8 @@ void getSurfaceEffect(inout vec4 reflectionColor, inout vec4 waterRawColor) {
163174 if (reflectionColor.r < 0.01 )
164175 col = vec3 (0.0 );
165176
177+ col = col * pow (fadeAmount, 0.2 );
178+
166179 float str = (col.x + col.y + col.z) / 3.0 ;
167180 str = str * str - 0.325 ;
168181
@@ -175,17 +188,63 @@ void getSurfaceEffect(inout vec4 reflectionColor, inout vec4 waterRawColor) {
175188 waterRawColor = mix (color, waterRawColor, 0.60 );
176189}
177190
178- float getReflectionPower() {
179- return min ((getWaterDepth(texcoord) + 0.005 ) * 20.0 , 0.65 );
191+ vec4 computeFinalWaterColor(vec4 stencil, vec4 color, float fadeAmount, bool glass, float glassAlpha, vec4 waterRawColor) {
192+ // Water reflection fetch
193+ color = (! glass) ? getRefractedColor(stencil) : color;
194+ vec4 reflectionColor = (isEyeInWater == 1 ) ? color : getReflectedColor(stencil) * fadeAmount;
195+
196+ float in_water;
197+
198+ if (stencil.r > 0.05 )
199+ in_water = 1.0 ;
200+ else
201+ in_water = 0.0 ;
202+
203+ // combine reflection and scene at water surfaces
204+ float reflection_strength = 0.30 * (stencil.r- 0.1 );
205+ float disable_refl = stencil.r- 0.1 ;
206+
207+ if (disable_refl <= 0.0 ) disable_refl = 0.0 ; // no reflection
208+
209+ // times inverted color.r for a stronger reflection in darker water parts!
210+ vec3 reflection_color = vec3 (1.0 , 1.0 , 1.0 );
211+ reflection_color = reflection_strength * disable_refl * reflectionColor.rgb;
212+
213+ // more color in darker water in relation to the reflection
214+ // color darkened
215+ float difference = (reflection_color.r+ reflection_color.g+ reflection_color.b)/ 3.0 - (color.r + color.g + color.b)/ 5.5 ;
216+ if (difference < 0.0 ) difference = 0.0 ;
217+ vec3 regular_color = color.rgb * (1.0 - in_water* reflection_strength) + (in_water * (difference * getWaterColor().rgb));
218+
219+ color = vec4 (regular_color, 1.0 );
220+ reflectionColor = vec4 (reflection_color, 1.0 );
221+
222+ // Apply reflection to water
223+ reflectionColor = mix (color, reflectionColor, fadeAmount);
224+
225+ // Superbomb17 effect on water surface
226+ #if STYLE == 2
227+ if (! glass && isEyeInWater == 0 ) {
228+ getSurfaceEffect(stencil, reflectionColor, waterRawColor, fadeAmount);
229+ }
230+ #endif
231+
232+ // Blending reflection factor (how much of reflection color in final fragment)
233+ // Make sure transluscent geometry (glass) is correctly blended
234+ float reflectionBlendAmount = (glass) ? 0.85 - glassAlpha : 0.40 ;
235+ color = mix (color, reflectionColor, reflectionBlendAmount);
236+
237+ return color;
180238}
181239
182240void main() {
241+ // Fetch data
183242 color = texture(colortex0, texcoord);
184243
185- // Fetch data
186244 vec4 waterMask = getWaterMask(texcoord);
187245 vec4 glassMask = getGlassMask(texcoord);
188246 vec4 iceMask = getIceMask(texcoord);
247+ vec4 stencil = getStencil(texcoord);
189248
190249 vec3 waterNormal = getWaterNormal(texcoord);
191250 vec4 waterRawColor = getWaterRawColor(texcoord);
@@ -205,29 +264,8 @@ void main() {
205264 }
206265
207266 if ((water || glass) && ! ice) {
208- // Water reflection fetch
209- color = getRefractedColor();
210- vec4 reflectionColor = (isEyeInWater == 1 ) ? color : getReflectedColor();
211-
212- // Apply reflection to water
213- reflectionColor = mix (color, reflectionColor, fadeAmount);
214-
215- // Amount of reflection allowed: lass reflection in shallow water
216- float reflectionStrength = getReflectionPower();
217-
218- // Superbomb17 effect on water surface
219- #if STYLE == 2
220- if (! glass && isEyeInWater == 0 )
221- getSurfaceEffect(reflectionColor, waterRawColor);
222- #endif
223-
224- // Blend flowing water to still water
225- waterRawColor = mix (waterRawColor, color, 0.70 - reflectionStrength);
226-
227- // Blending reflection factor (how much of reflection color in final fragment)
228- // Make sure transluscent geometry (glass) is correctly blended
229- float reflectionBlendAmount = (glass) ? 0.75 - glassAlpha : reflectionStrength;
230- color = mix (color, reflectionColor, reflectionBlendAmount);
267+ // Compute reflections, refractions, and wawing water into color
268+ color = computeFinalWaterColor(stencil, color, fadeAmount, glass, glassAlpha, waterRawColor);
231269 }
232270
233271 if (water && isEyeInWater == 0 ) {
@@ -237,8 +275,6 @@ void main() {
237275 color = blendWaterInScene(color, waterRawColor, factor, fadeAmount);
238276 }
239277
240- if (isEyeInWater == 1 && dot (vec3 (1.0 ), color.rgb) < 0.01 ) {
241- // Fix weird bug underwater when looking at horizon (kinda patchy by works so...)
242- color = mix (underwaterFogColorNight, getUnderwaterDaytimeWaterColor(), getSunAmount());
243- }
278+ // Debug
279+ // color.rgb = texture(colortex11, texcoord).rgb;
244280}
0 commit comments