Skip to content

Commit dbd1472

Browse files
author
Jim Pavan
committed
Fixed many bugs
1 parent 23d0cad commit dbd1472

13 files changed

+94
-79
lines changed

shaders/composite.fsh

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void getSurfaceEffect(in vec4 stencil, inout vec4 reflectionColor, inout vec4 wa
181181

182182
reflectionColor.rgb += clamp((str * col), 0.0, 3.0);
183183
}
184-
reflectionColor /= 6;
184+
reflectionColor /= 6.0;
185185
}
186186

187187
color = vec4(color.rgb, 1.0) + vec4(reflectionColor.rgb, 1.0);
@@ -201,8 +201,8 @@ vec4 computeFinalWaterColor(vec4 stencil, vec4 color, float fadeAmount, bool gla
201201
in_water = 0.0;
202202

203203
// 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;
204+
float reflection_strength = 0.30 * (stencil.r - 0.1);
205+
float disable_refl = stencil.r - 0.1;
206206

207207
if (disable_refl <= 0.0) disable_refl = 0.0; // no reflection
208208

@@ -212,9 +212,9 @@ vec4 computeFinalWaterColor(vec4 stencil, vec4 color, float fadeAmount, bool gla
212212

213213
// more color in darker water in relation to the reflection
214214
// color darkened
215-
float difference = (reflection_color.r+reflection_color.g+reflection_color.b)/3.0 - (color.r + color.g + color.b)/5.5;
215+
float difference = (reflection_color.r + reflection_color.g + reflection_color.b) / 3.0 - (color.r + color.g + color.b) / 5.5;
216216
if (difference < 0.0) difference = 0.0;
217-
vec3 regular_color = color.rgb * (1.0-in_water*reflection_strength) + (in_water * (difference * getWaterColor().rgb));
217+
vec3 regular_color = color.rgb * (1.0 - in_water * reflection_strength) + (in_water * (difference * getWaterColor().rgb));
218218

219219
color = vec4(regular_color, 1.0);
220220
reflectionColor = vec4(reflection_color, 1.0);
@@ -224,9 +224,8 @@ vec4 computeFinalWaterColor(vec4 stencil, vec4 color, float fadeAmount, bool gla
224224

225225
// Superbomb17 effect on water surface
226226
#if STYLE == 2
227-
if (!glass && isEyeInWater == 0) {
227+
if (!glass && isEyeInWater == 0)
228228
getSurfaceEffect(stencil, reflectionColor, waterRawColor, fadeAmount);
229-
}
230229
#endif
231230

232231
// Blending reflection factor (how much of reflection color in final fragment)
@@ -243,38 +242,44 @@ void main() {
243242

244243
vec4 waterMask = getWaterMask(texcoord);
245244
vec4 glassMask = getGlassMask(texcoord);
245+
vec4 cloudMask = getCloudMask(texcoord);
246246
vec4 iceMask = getIceMask(texcoord);
247247
vec4 stencil = getStencil(texcoord);
248248

249+
vec4 waterModelPos = getWaterModelPos(texcoord);
249250
vec3 waterNormal = getWaterNormal(texcoord);
250251
vec4 waterRawColor = getWaterRawColor(texcoord);
251252

252253
bool water = isWater(waterMask);
253254
bool glass = isGlass(glassMask);
254255
bool ice = isIce(iceMask);
256+
bool cloud = isCloud(cloudMask);
255257

256258
float glassAlpha = getGlassTransparency(glassMask);
257259
float fadeAmount = getTransitionAmount(waterMask);
258260
float waterLength = getWaterDistance(waterMask);
259261
float glassLength = getGlassDistance(glassMask);
262+
float cloudLength = getCloudLength(cloudMask);
260263

261-
if (isLookingAtStillWaterThroughTransluscent(glass, ice, water, waterLength, glassLength, waterNormal)) {
262-
// Mare sure you can see water reflection through glass and ice when placed in water on solid block
263-
fadeAmount = 1.0;
264-
}
264+
if (!cloud && cloudLength < waterLength) {
265+
// Make sure clouds hide water like the rest of terrain, if no clouds in front then do water stuff
265266

266-
if ((water || glass) && !ice) {
267-
// Compute reflections, refractions, and wawing water into color
268-
color = computeFinalWaterColor(stencil, color, fadeAmount, glass, glassAlpha, waterRawColor);
269-
}
267+
if (isLookingAtStillWaterThroughTransluscent(glass, ice, water, waterLength, glassLength, waterNormal)) {
268+
// Mare sure you can see water reflection through glass and ice when placed in water on solid block
269+
fadeAmount = 1.0;
270+
}
270271

271-
if (water && isEyeInWater == 0) {
272-
// Blend flowing water to scene when outside water
273-
float divisor = (glassLength < waterLength) ? 2.0 : 8.0;
274-
float factor = max(0.0, waterBlendFactor - glassAlpha / divisor);
275-
color = blendWaterInScene(color, waterRawColor, factor, fadeAmount);
276-
}
272+
if ((water || glass) && !ice) {
273+
// Compute reflections, refractions, and wawing water into color
274+
color = computeFinalWaterColor(stencil, color, fadeAmount, glass, glassAlpha, waterRawColor);
275+
}
277276

278-
// Debug
279-
// color.rgb = texture(colortex11, texcoord).rgb;
277+
if (water && isEyeInWater == 0) {
278+
// Blend flowing water to scene when outside water
279+
float divisor = (glassLength < waterLength) ? 2.0 : 8.0;
280+
float factor = max(0.0, waterBlendFactor - glassAlpha / divisor);
281+
color = blendWaterInScene(color, waterRawColor, factor, fadeAmount);
282+
color = applyFogOnWater(color, waterModelPos);
283+
}
284+
}
280285
}

shaders/final.fsh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#version 430 compatibility
22

33
#include "/lib/common.glsl"
4-
#include "/lib/water.glsl"
54

65
in vec2 texcoord;
76

@@ -11,7 +10,7 @@ layout(location = 0) out vec4 color;
1110
void main() {
1211
color = texture(colortex0, texcoord);
1312

14-
// Zero out all layer counters
13+
// Zero out all layer occurence counters
1514
for (int i = 0; i < 384; i++) {
1615
layersData.layers[i] = 0;
1716
}

shaders/gbuffers_clouds.fsh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ uniform float alphaTestRef = 0.1;
1111

1212
in vec2 texcoord;
1313
in vec4 glcolor;
14+
in vec4 position;
1415

15-
/* RENDERTARGETS: 0 */
16+
/* RENDERTARGETS: 0,12 */
1617
layout(location = 0) out vec4 color;
18+
layout(location = 1) out vec4 cloudsMask;
1719

1820
void main() {
1921
color = texture(gtexture, texcoord) * glcolor;
@@ -23,4 +25,7 @@ void main() {
2325

2426
// Apply fog
2527
color = applyFog(color, 0.075, true);
28+
29+
// Mask clouds
30+
cloudsMask = vec4(1.0, length(position), 0.0, 1.0);
2631
}

shaders/gbuffers_clouds.vsh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
out vec2 texcoord;
77
out vec4 glcolor;
8+
out vec4 position;
89

910
void main() {
1011
vec3 pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
@@ -13,4 +14,5 @@ void main() {
1314
gl_FogFragCoord = length(pos);
1415
texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
1516
glcolor = gl_Color;
17+
position = gl_Vertex;
1618
}

shaders/gbuffers_skytextured.fsh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ in vec4 glcolor;
1313

1414
/* RENDERTARGETS: 0,8 */
1515
layout(location = 0) out vec4 color;
16-
layout(location = 1) out vec4 sunColor;
16+
layout(location = 1) out vec4 sunAndMoonColor;
1717

1818
void removeHalo(vec4 texturedColor) {
1919
if (dot(texturedColor, vec4(1.0)) < 2.0) {
@@ -32,9 +32,9 @@ void main() {
3232
removeHalo(texturedColor);
3333

3434
if (renderStage == MC_RENDER_STAGE_SUN || renderStage == MC_RENDER_STAGE_MOON) {
35-
// Sun && moon fragments
35+
// Sun & moon fragments
3636
if (frameCounter == 1)
37-
sunColor = texturedColor;
37+
sunAndMoonColor = texturedColor;
3838
else
3939
discard;
4040
}

shaders/gbuffers_skytextured.gsh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ void passThru() {
6565

6666
void main() {
6767
#if SKYTEXTURED == 1
68-
outForOffline();
68+
if (frameCounter == 1)
69+
outForOffline();
6970
#endif
7071

7172
passThru();

shaders/gbuffers_terrain.fsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void main() {
2626
color = texture(gtexture, texcoord) * glcolor;
2727
color *= texture(lightmap, lmcoord);
2828
if (color.a < alphaTestRef && blockID != 5) {
29-
// Remove / fill holes in tree leaves
29+
// Fill holes in tree leaves to match reflections
3030
discard;
3131
}
3232

shaders/gbuffers_water.fsh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void main() {
3838
// Ice
3939
isItIce = 1.0;
4040
color = applyFog(outColor * glcolor, 1.0, false);
41+
color.a = min(color.a + 0.125, 1.0);
4142
} else if (blockID > 10000) {
4243
// Glass
4344
y = 1.0;
@@ -50,8 +51,7 @@ void main() {
5051
outColor.a = 1.0;
5152
waterPosition = position;
5253
waterNormal = vec4(encodeNormal(normal), 1.0);
53-
waterColoredTexture = applyFog(outColor, 1.0, false);
54-
waterColoredTexture = tintWater(waterColoredTexture, position);
54+
waterColoredTexture = tintWater(outColor * mix(glcolor, getWaterColor(), 0.50), position);
5555
color = vec4(outColor.rgb, waterBlendFactor) * glcolor;
5656
}
5757

shaders/lib/common.glsl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ uniform sampler2D colortex14;
2525
uniform sampler2D colortex15;
2626

2727
// Uniform variables
28+
uniform int fogMode;
2829
uniform vec3 fogColor;
2930
uniform vec3 skyColor;
30-
uniform int fogMode;
3131
uniform int isEyeInWater;
3232
uniform float eyeAltitude;
3333
uniform ivec2 eyeBrightness;
@@ -71,26 +71,26 @@ const int shadowMapResolution = 2048;
7171
const float shadowIntervalSize = 0.0f;
7272

7373
// Texture targets clear settings
74-
const bool colortex4Clear = false;
7574
const bool colortex8Clear = false;
7675
const vec4 shadowcolor0ClearColor = vec4(0.0, 0.0, 0.0, 5.0);
7776

7877
// Texture formats
7978
/*
80-
const int shadowcolor0Format = RGBA16F; // reflection color
81-
const int shadowcolor1Format = RGBA; // reflection mask (is water in reflection)
82-
const int colortex0Format = RGBA; // scene color
83-
const int colortex1Format = RGBA32F; // position of water blocks in model space
84-
const int colortex2Format = RGBA; // scene normal (encoded and in world space)
79+
const int shadowcolor0Format = RGBA16F; // Reflection color
80+
const int shadowcolor1Format = RGBA; // Reflection mask (is water in reflection)
81+
const int colortex0Format = RGBA; // Scene color
82+
const int colortex1Format = RGBA32F; // Position of water blocks in model space
83+
const int colortex2Format = RGBA; // Scene normal (encoded and in world space)
8584
const int colortex3Format = RGBA16F; // Water mask
86-
const int colortex4Format = RGBA16F; // ID of water blocks, water mask for next frame for shadow pass (unexpanded)
85+
const int colortex4Format = RGBA; // -
8786
const int colortex5Format = RGBA; // Ice mask
8887
const int colortex6Format = RGBA16F; // Glass mask
8988
const int colortex7Format = RGBA; // Lightmap color
90-
const int colortex8Format = RGBA; // Sun and moon texture
89+
const int colortex8Format = RGBA; // Sun and moon textures
9190
const int colortex9Format = RGBA; // Water color
9291
const int colortex10Format = RGBA; // Terrain mask
9392
const int colortex11Format = RGBA; // Water tiling
93+
const int colortex12Format = RGBA16F; // Cloud mask
9494
*/
9595

9696
// Constant variables for water shader
@@ -100,11 +100,11 @@ const vec4 necrowizzardUnderwaterFogColorDay = vec4(0.03, 0.05, 0.12, 0.99);
100100
const vec4 superbomb17UnderwaterFogColorDay = vec4(0.03, 0.05, 0.12, 0.99); // Superbomb17 fog color when underwater (daytime)
101101
const vec4 underwaterFogColorNight = vec4(0.02, 0.13, 0.24, 0.9); // Fog color when underwater (nighttime)
102102
const float waterSurfaceTransparency = 0.35; // Water surface/texture transparency
103-
const float waterClipPlane = 1.0; // Delete vertices too close from water surface (strange results with player reflection otherwise)
103+
const float waterClipPlane = 1.0; // Delete vertices too close from water surface
104104
const float eyeCameraOffset = 1.68; // Eye camera offset from player's feet
105105
const float waterBlockOffset = 1.005; // Water block clipping plane height (inside block)
106106
const int lowerWorldBound = -64; // Lowest possible water reflection plane height (Minecraft minimum block height)
107-
const float waterBlendFactor = 0.20; // How much to blend flowing water to scene
107+
const float waterBlendFactor = 0.30; // How much to blend flowing water to scene
108108

109109
// Fog constants
110110
const int GL_LINEAR = 9729;
@@ -119,11 +119,11 @@ uniform vec2 iresolution;
119119
#define PLANES 1 // Reflection planes allowed [1 2 3 4]
120120
#define SKYTEXTURED 2 // Sun & moon reflection [1 2]
121121

122-
// Shader Storage Buffer Object for water information in the visible scene (player view)
122+
// Shader Storage Buffer Object for water information in player view
123123
layout(std430, binding = 0) buffer SSBOWaterShader {
124124
int layers[384]; // -64 to 320 every possible water layer, store water fragment occurence for each height
125-
int waterHeights[PLANES]; // Registered heights for reflection calculation in next frame
126-
int lowestHeight; // Height counter
125+
int waterHeights[PLANES]; // Registered heights for reflection calculation
126+
int lowestHeight; // Lowest registered height (optimization in later stage)
127127
} layersData;
128128

129129
// Shader Storage Buffer Object for specifying "water height mask" on all water fragments

shaders/lib/shading.glsl

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ vec4 applyFog(vec4 color, float factor, bool applyAlpha) {
7272
return color;
7373
}
7474

75+
vec4 applyFogOnWater(vec4 color, vec4 waterModelPos) {
76+
// Fogify water
77+
vec3 worldPos = getWorldPositionFromModelPosition(waterModelPos);
78+
float l = length(worldPos - cameraPosition);
79+
float d = clamp((l - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
80+
color = mix(color, gl_Fog.color, d);
81+
82+
return color;
83+
}
84+
7585
vec4 getSkyColor(vec4 waterModelPos) {
7686
// Get sky color on water fragment relative to player view direction
7787
vec3 skyDir = vec3(0.0, 1.0, 0.0);
@@ -82,16 +92,6 @@ vec4 getSkyColor(vec4 waterModelPos) {
8292
return vec4(calcSkyColor(skyDir), 1.0);
8393
}
8494

85-
vec4 tintWater(vec4 waterCol, vec4 waterModelPosition) {
86-
// Tint water based on sky color and cave ambient color
87-
float amountMinWaterColor = mix(0.0, 0.70, getMidDayFastFrac01());
88-
float amountInDarkness = mix(amountMinWaterColor, 1.0, getAmountInDarkness());
89-
vec4 skyColor = getSkyColor(waterModelPosition);
90-
vec4 waterEnvironmentColor = mix(skyColor, waterCol, amountInDarkness);
91-
92-
return waterEnvironmentColor;
93-
}
94-
9595
vec4 addSkyTexturedToWater(vec4 waterCol, vec3 waterNormal, vec4 waterModelPosition) {
9696
vec4 outColor = waterCol;
9797

@@ -107,3 +107,13 @@ vec4 addSkyTexturedToWater(vec4 waterCol, vec3 waterNormal, vec4 waterModelPosit
107107

108108
return outColor;
109109
}
110+
111+
vec4 tintWater(vec4 waterCol, vec4 waterModelPosition) {
112+
// Tint water based on sky color and cave ambient color
113+
float amountMinWaterColor = mix(0.0, 0.70, getMidDayFastFrac01());
114+
float amountInDarkness = mix(amountMinWaterColor, 0.85, getAmountInDarkness());
115+
vec4 skyColor = getSkyColor(waterModelPosition);
116+
vec4 waterEnvironmentColor = mix(skyColor, waterCol, amountInDarkness);
117+
118+
return waterEnvironmentColor;
119+
}

0 commit comments

Comments
 (0)