53 lines
1.7 KiB
GLSL
53 lines
1.7 KiB
GLSL
// MinecraftPT — WaterRefraction_FS
|
|
// Water refraction pass: sample the solid scene with wave-based offset.
|
|
|
|
#include "/Lib/Settings.glsl"
|
|
#include "/Lib/Utilities.glsl"
|
|
#include "/Lib/GbufferData.glsl"
|
|
#include "/Lib/IndividualFunctions/WaterWaves.glsl"
|
|
|
|
uniform sampler2D colortex12; // solid scene
|
|
uniform sampler2D colortex4; // translucent albedo
|
|
|
|
layout(location = 0) out vec4 colorOut;
|
|
|
|
void main(){
|
|
ivec2 texelCoord = ivec2(gl_FragCoord.xy);
|
|
|
|
float depth = texelFetch(depthtex0, texelCoord, 0).r;
|
|
if (depth >= 1.0){
|
|
colorOut = texelFetch(colortex12, texelCoord, 0);
|
|
return;
|
|
}
|
|
|
|
vec4 gbuffer5 = texelFetch(colortex5, texelCoord, 0);
|
|
float matID = gbuffer5.a * 255.0;
|
|
|
|
if (matID != MATID_WATER){
|
|
colorOut = texelFetch(colortex12, texelCoord, 0);
|
|
return;
|
|
}
|
|
|
|
// Reconstruct world position
|
|
vec4 viewPos = gbufferProjectionInverse * vec4(vec2(texelCoord) / screenSize * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
|
|
viewPos.xyz /= viewPos.w;
|
|
vec3 worldPos = gbufferModelViewInverse[3].xyz + viewPos.xyz;
|
|
|
|
// Wave normal-based refraction offset
|
|
vec3 waveNormal = GetWaveNormal(worldPos, 0.5);
|
|
vec3 viewDir = normalize(-viewPos.xyz);
|
|
vec3 refracted = refract(viewDir, waveNormal, 0.75);
|
|
|
|
// Sample the solid scene with the refraction offset
|
|
vec3 refractedDir = mat3(gbufferModelView) * refracted;
|
|
vec2 refractedCoord = viewPos.xy / viewPos.z * refractedDir.xy / refractedDir.z * 0.5 + 0.5;
|
|
vec2 sampleCoord = refractedCoord * screenSize;
|
|
|
|
vec3 color = textureLod(colortex12, sampleCoord / screenSize, 0.0).rgb;
|
|
|
|
// Water depth color fade
|
|
vec3 waterColor = texelFetch(colortex4, texelCoord, 0).rgb;
|
|
color = mix(color, waterColor, 0.3);
|
|
|
|
colorOut = vec4(color, 1.0);
|
|
} |