Files

84 lines
2.6 KiB
GLSL

// MinecraftPT — Translucent_FS
// Composites translucent geometry (water, glass, particles) on top of solid.
#include "/Lib/Settings.glsl"
#include "/Lib/Utilities.glsl"
#include "/Lib/BasicFunctions/LightingConstants.glsl"
#include "/Lib/GbufferData.glsl"
#include "/Lib/BasicFunctions/Sunlight_Shadow.glsl"
#include "/Lib/BasicFunctions/Blocklight.glsl"
#include "/Lib/PathTracing/Tracer/SampleIRC.glsl"
#include "/Lib/PathTracing/Tracer/ShadowTracing.glsl"
uniform sampler2D colortex12; // combined HDR solid scene
uniform sampler2D colortex4; // translucent albedo
uniform sampler2D colortex5; // translucent normal
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;
}
// Check if there is translucent data
vec4 gbuffer5 = texelFetch(colortex5, texelCoord, 0);
float matID = gbuffer5.a * 255.0;
if (matID < 0.5){
colorOut = texelFetch(colortex12, texelCoord, 0);
return;
}
bool isSmooth;
GbufferData gbuffer = GetGbufferDataTranslucent(texelCoord, isSmooth);
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;
vec3 solidColor = texelFetch(colortex12, texelCoord, 0).rgb;
// Water: blend with refraction
vec3 color = solidColor;
if (matID == MATID_WATER){
vec3 waterColor = gbuffer.albedo;
float alpha = gbuffer.albedoAlpha;
// Fresnel
vec3 viewDir = normalize(-viewPos.xyz);
float NdotV = max(dot(gbuffer.worldNormal, viewDir), 0.0);
float fresnel = 0.02 + 0.98 * pow(1.0 - NdotV, 5.0);
// Light contribution (sun + sky) on water
vec3 sunLight = GetSunlight(viewPos.xyz, worldPos, gbuffer.vertexNormal, gbuffer.worldNormal, gbuffer.lightmap.x);
vec3 waterLight = waterColor * (sunLight + Blocklight(gbuffer.lightmap) * 0.5);
// Water tint / depth color
vec3 deepColor = waterColor * 0.3;
color = mix(solidColor, waterLight + deepColor, fresnel * 0.8 + 0.2);
}else if (matID == MATID_STAINEDGLASS){
vec3 glassColor = gbuffer.albedo;
float alpha = gbuffer.albedoAlpha;
vec3 viewDir = normalize(-viewPos.xyz);
float NdotV = max(dot(gbuffer.worldNormal, viewDir), 0.0);
float fresnel = 0.04 + 0.96 * pow(1.0 - NdotV, 5.0);
color = mix(solidColor, solidColor * glassColor, fresnel);
color += glassColor * 0.1;
}else{
// Particles etc — additively blend
color = solidColor + gbuffer.albedo * gbuffer.albedoAlpha * 0.5;
}
colorOut = vec4(color, 1.0);
}