Skip to content

Commit f0d9346

Browse files
author
Jim Pavan
committed
Fixed bugs & cleaned code
1 parent 7bb3e49 commit f0d9346

File tree

10 files changed

+45
-71
lines changed

10 files changed

+45
-71
lines changed

shaders/composite.fsh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ vec4 getReflectedColor(vec4 stencil) {
107107
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));
108108

109109
vec2 disturbed = vec2(x_scale * texcoord.x, z_scale * texcoord.y);
110-
disturbed = clamp(disturbed, 0.001, 0.99);
110+
disturbed = clamp(disturbed, 0.001, 0.999);
111111
vec4 reflection = getWaterReflectionColor(disturbed);
112112

113113
time_scale = 0.45;
@@ -120,7 +120,7 @@ vec4 getReflectedColor(vec4 stencil) {
120120
z_scale = (1.0 + (sin(sin(time_scale * 3.14159 * used_timer) + 1.5 * sin(size_scale * 3.14159 * stencil.b)) / 150.0));
121121

122122
vec2 disturbed_2 = vec2(x_scale * texcoord.x, z_scale * texcoord.y);
123-
disturbed_2 = clamp(disturbed_2, 0.001, 0.99);
123+
disturbed_2 = clamp(disturbed_2, 0.001, 0.999);
124124
vec4 reflection_2 = getWaterReflectionColor(disturbed_2);
125125
reflection = (reflection + reflection_2) / 2.0;
126126

@@ -163,6 +163,7 @@ void getSurfaceEffect(in vec4 stencil, inout vec4 reflectionColor, inout vec4 wa
163163
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));
164164

165165
vec2 disturbed = vec2(x_scale * texcoord.x, z_scale * texcoord.y);
166+
disturbed = clamp(disturbed, 0.001, 0.999);
166167

167168
float surface_effects = 1.0;
168169
if (surface_effects > 0.0) {

shaders/composite_a.csh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
44

55
#include "/lib/common.glsl"
6-
#include "/lib/water.glsl"
76

87
void setValue(int index, int elem, inout int[PLANES] array) {
98
array[index] = elem;

shaders/gbuffers_clouds.fsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ in vec2 texcoord;
1313
in vec4 glcolor;
1414
in vec4 position;
1515

16-
/* RENDERTARGETS: 0,12 */
16+
/* RENDERTARGETS: 0,4 */
1717
layout(location = 0) out vec4 color;
1818
layout(location = 1) out vec4 cloudsMask;
1919

shaders/gbuffers_water.fsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ in vec2 lmcoord;
1313
in vec2 texcoord;
1414
in vec4 glcolor;
1515
in vec4 position;
16+
in vec3 worldPos;
1617
in vec3 normal;
1718
in float stillWaterAmount;
1819

@@ -57,7 +58,6 @@ void main() {
5758

5859
// Water tiling
5960
float tiling = 10.0;
60-
vec3 worldPos = getWorldPositionFromModelPosition(position);
6161
float x = mod(worldPos.x, tiling) / tiling;
6262
float z = mod(worldPos.z, tiling) / tiling;
6363

shaders/gbuffers_water.gsh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ in vec2 gshTexcoord[];
1212
in vec4 gshGlcolor[];
1313
in vec4 gshPosition[];
1414
in vec3 gshPosition2[];
15+
in vec3 gshWorldPos[];
1516
in vec3 gshNormal[];
1617

1718
out flat int blockID;
1819
out vec2 lmcoord;
1920
out vec2 texcoord;
2021
out vec4 glcolor;
2122
out vec4 position;
23+
out vec3 worldPos;
2224
out vec3 normal;
2325
out float stillWaterAmount;
2426

@@ -29,6 +31,7 @@ void outVertex(int i, float amount) {
2931
lmcoord = gshLmcoord[i];
3032
glcolor = gshGlcolor[i];
3133
position = gshPosition[i];
34+
worldPos = gshWorldPos[i];
3235
normal = gshNormal[i];
3336
blockID = gshBlockID[i];
3437
stillWaterAmount = amount;

shaders/gbuffers_water.vsh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ out vec2 gshTexcoord;
1010
out vec4 gshGlcolor;
1111
out vec4 gshPosition;
1212
out vec3 gshPosition2;
13+
out vec3 gshWorldPos;
1314
out vec3 gshNormal;
1415

1516
void main() {
@@ -23,4 +24,5 @@ void main() {
2324
gshPosition = gl_Vertex;
2425
gshNormal = gl_Normal;
2526
gshBlockID = int(mc_Entity.x);
27+
gshWorldPos = getWorldPositionFromModelPosition(gl_Vertex);
2628
}

shaders/lang/en_US.lang

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

44
option.STYLE=Water surface style
55
value.STYLE.1=§bNecrowizzard
66
value.STYLE.2=§bSuperbomb17
77

88
option.PLANES=Available simultaneous water levels
9-
value.PLANES.1=§a1 - Low
10-
value.PLANES.2=§e2 - Medium
11-
value.PLANES.3=§c3 - High
9+
value.PLANES.1=§a 1 - Low
10+
value.PLANES.2=§e 2 - Medium
11+
value.PLANES.3=§c 3 - High
1212
value.PLANES.4=§4 4 - Extreme
1313
option.PLANES.comment=Number of water patches at different heights handled for reflections.
1414

1515
option.SKYTEXTURED=Sun & moon reflection (§cExperimental)
16-
value.SKYTEXTURED.1=§aON
17-
value.SKYTEXTURED.2=§cOFF
16+
value.SKYTEXTURED.0=§cOFF
17+
value.SKYTEXTURED.1=§aON

shaders/lib/common.glsl

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,24 @@ const float shadowIntervalSize = 0.0f;
7272

7373
// Texture targets clear settings
7474
const bool colortex8Clear = false;
75-
const vec4 shadowcolor0ClearColor = vec4(0.0, 0.0, 0.0, 5.0);
75+
const vec4 shadowcolor0ClearColor = vec4(0.0, 0.0, 0.0, 1.0);
7676

7777
// Texture formats
7878
/*
79-
const int shadowcolor0Format = RGBA16F; // Reflection color
79+
const int shadowcolor0Format = RGBA; // Reflection color
8080
const int shadowcolor1Format = RGBA; // Reflection mask (is water in reflection)
8181
const int colortex0Format = RGBA; // Scene color
8282
const int colortex1Format = RGBA32F; // Position of water blocks in model space
83-
const int colortex2Format = RGBA; // Scene normal (encoded and in world space)
83+
const int colortex2Format = RGBA; // Scene normals (encoded and in world space)
8484
const int colortex3Format = RGBA16F; // Water mask
85-
const int colortex4Format = RGBA; // -
85+
const int colortex4Format = RGBA16F; // Cloud mask
8686
const int colortex5Format = RGBA; // Ice mask
8787
const int colortex6Format = RGBA16F; // Glass mask
8888
const int colortex7Format = RGBA; // Lightmap color
8989
const int colortex8Format = RGBA; // Sun and moon textures
9090
const int colortex9Format = RGBA; // Water color
9191
const int colortex10Format = RGBA; // Terrain mask
92-
const int colortex11Format = RGBA; // Water tiling
93-
const int colortex12Format = RGBA16F; // Cloud mask
92+
const int colortex11Format = RGBA; // Water tiling
9493
*/
9594

9695
// Constant variables for water shader
@@ -100,11 +99,11 @@ const vec4 necrowizzardUnderwaterFogColorDay = vec4(0.03, 0.05, 0.12, 0.99);
10099
const vec4 superbomb17UnderwaterFogColorDay = vec4(0.03, 0.05, 0.12, 0.99); // Superbomb17 fog color when underwater (daytime)
101100
const vec4 underwaterFogColorNight = vec4(0.02, 0.13, 0.24, 0.9); // Fog color when underwater (nighttime)
102101
const float waterSurfaceTransparency = 0.35; // Water surface/texture transparency
102+
const float waterBlendFactor = 0.30; // How much to blend flowing water to scene
103103
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.30; // How much to blend flowing water to scene
108107

109108
// Fog constants
110109
const int GL_LINEAR = 9729;
@@ -114,10 +113,10 @@ const int GL_EXP2 = 2049;
114113
// Custom uniforms
115114
uniform vec2 iresolution;
116115

117-
#define INFO 0 // [0]
116+
#define INFO 0 // Shader version [0]
118117
#define STYLE 1 // Water surface style [1 2]
119118
#define PLANES 1 // Reflection planes allowed [1 2 3 4]
120-
#define SKYTEXTURED 2 // Sun & moon reflection [1 2]
119+
#define SKYTEXTURED 0 // Sun & moon reflection [0 1]
121120

122121
// Shader Storage Buffer Object for water information in player view
123122
layout(std430, binding = 0) buffer SSBOWaterShader {
@@ -152,6 +151,7 @@ vec4 getUnderwaterDaytimeWaterColor() {
152151
#endif
153152
}
154153

154+
// ---------------- Encoding ----------------
155155
vec3 encodeNormal(vec3 normal) {
156156
return (normal + 1.0) / 2.0;
157157
}
@@ -160,6 +160,23 @@ vec3 decodeNormal(vec3 normal) {
160160
return (normal * 2.0) - 1.0;
161161
}
162162

163+
int YToArray(int Y) {
164+
return Y + 64;
165+
}
166+
167+
int ArrayToY(int index) {
168+
return index - 64;
169+
}
170+
171+
// ---------------- World ----------------
172+
vec3 getWorldPositionFromModelPosition(vec4 modelPos) {
173+
dvec3 viewPos = (gbufferModelView * modelPos).xyz;
174+
dvec3 eyePlayerPos = mat3(gbufferModelViewInverse) * viewPos;
175+
dvec3 worldPos = eyePlayerPos + cameraPosition + gbufferModelViewInverse[3].xyz;
176+
177+
return vec3(worldPos);
178+
}
179+
163180
vec2 reproject(vec2 uv, float depth) {
164181
/* Find uv coordinate of the pixel in previous frame */
165182
vec4 frag = gbufferProjectionInverse * vec4(vec3(uv, depth) * 2.0 - 1.0, 1.0);
@@ -173,13 +190,8 @@ vec2 reproject(vec2 uv, float depth) {
173190
return prevPos.xy / prevPos.w * 0.5 + 0.5;
174191
}
175192

193+
// ---------------- Textures ----------------
176194
bool isWithinTexture(vec2 uv) {
177195
// Is uv within texture bounds
178196
return uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0;
179197
}
180-
181-
float linearizeDepth(float depth) {
182-
// Implemetation from: LearnOpenGL
183-
float z = depth * 2.0 - 1.0; // back to NDC
184-
return ((2.0 * near * far) / (far + near - z * (far - near))) / far;
185-
}

shaders/lib/sky.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ vec4 skyTexturedReflection(vec3 ro, vec3 rd, vec4 color) {
232232
}
233233

234234
vec4 getCloudMask(vec2 uv) {
235-
return texture(colortex12, uv);
235+
return texture(colortex4, uv);
236236
}
237237

238238
bool isCloud(vec4 mask) {

shaders/lib/water.glsl

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
// ---------------- Encoding ----------------
2-
int YToArray(int Y) {
3-
return Y + 64;
4-
}
5-
6-
int ArrayToY(int index) {
7-
return index - 64;
8-
}
9-
102
int getIndex(ivec2 pixelCoord) {
113
// Convert 2D pixel coordinate to 1D array index
124
return int(pixelCoord.y * viewWidth + pixelCoord.x);
@@ -53,27 +45,7 @@ mat4 invertPitch(mat4 viewMatrix) {
5345
return invertedView;
5446
}
5547

56-
vec3 getWorldPositionFromModelPosition(vec4 modelPos) {
57-
dvec3 viewPos = (gbufferModelView * modelPos).xyz;
58-
dvec3 eyePlayerPos = mat3(gbufferModelViewInverse) * viewPos;
59-
dvec3 worldPos = eyePlayerPos + cameraPosition + gbufferModelViewInverse[3].xyz;
60-
61-
return vec3(worldPos);
62-
}
63-
6448
// ---------------- Water Utils ----------------
65-
float getDepth(vec2 uv) {
66-
return texture(depthtex0, uv).r;
67-
}
68-
69-
float getDepthNoTransluscent(vec2 uv) {
70-
return texture(depthtex1, uv).r;
71-
}
72-
73-
float getWaterDepth(vec2 uv) {
74-
return linearizeDepth(getDepthNoTransluscent(uv)) - linearizeDepth(getDepth(uv));
75-
}
76-
7749
vec4 getWaterMask(vec2 uv) {
7850
return texelFetch(colortex3, ivec2(uv * iresolution), 0);
7951
}
@@ -98,10 +70,6 @@ bool isWater(vec4 mask) {
9870
return mask.b > 0.99;
9971
}
10072

101-
bool isFlowing(vec2 uv) {
102-
return texture(colortex11, uv).b > 0.0;
103-
}
104-
10573
bool isIce(vec4 mask) {
10674
return mask.r > 0.99;
10775
}
@@ -149,15 +117,12 @@ float getIceDistance(vec4 mask) {
149117

150118
bool isStillWater(vec4 pos, vec3 normal) {
151119
/* Still water has its normal pointing straight up
152-
Still water is always located at the same position in world space
153-
This code actually captures a bit more than just still water
154-
(ie all water fragments close to still water in normal and height) */
120+
Still water is always located at the same position in world space. */
155121

156122
vec3 worldPos = getWorldPositionFromModelPosition(pos);
157123
float ratioPointingUp = dot(normal, vec3(0.0, 1.0, 0.0));
158124
float YPos = fract(worldPos.y);
159125

160-
// return ratioPointingUp >= 0.988 && YPos >= 0.885;
161126
return YPos >= 0.885 && YPos < 0.90 && ratioPointingUp >= 0.988;
162127
}
163128

@@ -171,14 +136,6 @@ int getWaterID(vec4 modelPos) {
171136
return (id < 0) ? id - 1 : id;
172137
}
173138

174-
float getFresnelFactor(vec3 viewDir, vec3 waterNormal) {
175-
// Fresnel: amount of reflection / refraction
176-
float fresnel = dot(-viewDir, waterNormal);
177-
fresnel = clamp(fresnel, 0.0, 1.0);
178-
179-
return fresnel;
180-
}
181-
182139
vec4 getWaterLighting(vec2 uv) {
183140
return texture(colortex7, uv);
184141
}

0 commit comments

Comments
 (0)