Skip to content

Commit 290d7a1

Browse files
author
Jim Pavan
committed
v1.1
1 parent e225af6 commit 290d7a1

File tree

10 files changed

+133
-90
lines changed

10 files changed

+133
-90
lines changed

shaders/composite.fsh

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ in vec2 texcoord;
1010
/* RENDERTARGETS: 0 */
1111
layout(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

182240
void 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
}

shaders/gbuffers_skybasic.fsh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ uniform int renderStage;
88

99
in vec4 glcolor;
1010

11-
/* RENDERTARGETS: 0 */
11+
/* RENDERTARGETS: 0,7 */
1212
layout(location = 0) out vec4 color;
13+
layout(location = 1) out vec4 light;
1314

1415
void main() {
1516
if (renderStage == MC_RENDER_STAGE_STARS) {
@@ -19,5 +20,5 @@ void main() {
1920
color = vec4(calcSkyColor(normalize(pos)), 1.0);
2021
}
2122

22-
color = applyFog(color, 1.0, false);
23+
light = vec4(1.0);
2324
}

shaders/gbuffers_terrain.fsh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ in vec4 position;
1717
in vec4 glcolor;
1818
in flat int blockID;
1919

20-
/* RENDERTARGETS: 0,10 */
20+
/* RENDERTARGETS: 0,10,7 */
2121
layout(location = 0) out vec4 color;
2222
layout(location = 1) out vec4 terrainMask;
23+
layout(location = 2) out vec4 terrainLightMapForWater;
2324

2425
void main() {
2526
color = texture(gtexture, texcoord) * glcolor;
@@ -36,6 +37,10 @@ void main() {
3637
// Apply fog
3738
color = applyFog(color, 1.0, false);
3839

40+
// Terrain lighting (for darker water in deeper areas)
41+
terrainLightMapForWater = clamp(pow(texture(lightmap, lmcoord), vec4(1.0)), 0.0, 1.0);
42+
terrainLightMapForWater *= getVanillaLighting(n);
43+
3944
// Terrain mask
4045
terrainMask = vec4(position.xyz, 1.0);
4146
}

shaders/gbuffers_water.fsh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void main() {
4040
color = applyFog(outColor * glcolor, 1.0, false);
4141
} else if (blockID > 10000) {
4242
// Glass
43+
y = 1.0;
4344
isItGlass = 1.0;
4445
color = applyFog(outColor * glcolor, 1.0, false);
4546
} else {
@@ -49,17 +50,17 @@ void main() {
4950
outColor.a = 1.0;
5051
waterPosition = position;
5152
waterNormal = vec4(encodeNormal(normal), 1.0);
52-
waterColoredTexture = applyFog(outColor * getWaterColor(), 1.0, false);
53+
waterColoredTexture = applyFog(outColor, 1.0, false);
5354
waterColoredTexture = tintWater(waterColoredTexture, position);
55+
color = vec4(outColor.rgb, waterBlendFactor) * glcolor;
5456
}
5557

5658
// Water tiling
57-
float tiling = 16;
59+
float tiling = 10.0;
5860
vec3 worldPos = getWorldPositionFromModelPosition(position);
59-
tiling += min(cameraPosition.y - worldPos.y, 48);
60-
float x = mod(position.x, tiling) / tiling;
61-
float z = mod(position.z, tiling) / tiling;
62-
61+
float x = mod(worldPos.x, tiling) / tiling;
62+
float z = mod(worldPos.z, tiling) / tiling;
63+
6364
// Output masks
6465
iceMask = vec4(isItIce, 0.0, 0.0, isItIce);
6566
waterMask = vec4(length(position), stillWaterAmount, isItWater, isItWater);

shaders/gbuffers_water.gsh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ void outVertex(int i, float amount) {
3939

4040
void main() {
4141
// Used for interpolating between still water and flowing water (for reflection seamless transition)
42-
float stillWaterV0 = (isStillWater(gshPosition[0], gshNormal[0])) ? 1.0 : 0.0;
43-
float stillWaterV1 = (isStillWater(gshPosition[1], gshNormal[1])) ? 1.0 : 0.0;
44-
float stillWaterV2 = (isStillWater(gshPosition[2], gshNormal[2])) ? 1.0 : 0.0;
45-
46-
// Pass-thru
47-
outVertex(0, stillWaterV0);
48-
outVertex(1, stillWaterV1);
49-
outVertex(2, stillWaterV2);
42+
for (int i = 0; i < 3; i++) {
43+
float stillWaterV = (isStillWater(gshPosition[i], gshNormal[i])) ? 1.0 : 0.0;
44+
outVertex(i, stillWaterV);
45+
}
5046

5147
EndPrimitive();
5248
}

shaders/lang/en_US.lang

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
option.INFO=Water Shader Alpha
2-
value.INFO.0=v1.0
2+
value.INFO.0=v1.1
33

44
option.STYLE=Water surface style
5-
value.STYLE.1=Necrowizzard
6-
value.STYLE.2=Superbomb17
5+
value.STYLE.1=§bNecrowizzard
6+
value.STYLE.2=§bSuperbomb17
77

8-
option.PLANES=Available simultaneous water levels (1: Low, 4 High)
9-
value.PLANES.1=1
10-
value.PLANES.2=2
11-
value.PLANES.3=3
12-
value.PLANES.4=4
8+
option.PLANES=Available simultaneous water levels
9+
value.PLANES.1=§a1 - Low
10+
value.PLANES.2=§e2 - Medium
11+
value.PLANES.3=§c3 - High
12+
value.PLANES.4=§4 4 - Extreme
13+
option.PLANES.comment=Number of water patches at different heights handled for reflections.
1314

14-
option.SKYTEXTURED=Sun & moon reflection (Experimental)
15-
value.SKYTEXTURED.1=ON
16-
value.SKYTEXTURED.2=OFF
15+
option.SKYTEXTURED=Sun & moon reflection (§cExperimental)
16+
value.SKYTEXTURED.1=§aON
17+
value.SKYTEXTURED.2=§cOFF

0 commit comments

Comments
 (0)