map3drendering/Map3DRendering/Shaders/shader.frag
2024-02-28 17:07:45 +01:00

57 lines
No EOL
1.4 KiB
GLSL

#version 330
out vec4 outputColor;
uniform vec3 viewPos;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform sampler2D texture0;
in vec4 FarColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoord;
in vec4 TexType;
in float RealType;
void main()
{
float tileSize = 64.0 / 512.0;
float baseWeight = 0.2;
float dominantWeight = 0.4;
vec4 weights = vec4(baseWeight);
vec2 uvOffsets[4];
for (int i = 0; i < 4; i++) {
float type = TexType[i];
uvOffsets[i] = vec2(mod(type, 8.0), floor(type / 8.0)) * tileSize;
if (type == RealType) {
weights[i] = dominantWeight;
}
}
vec4 blendedColor[4];
for (int i = 0; i < 4; i++) {
vec2 uv = TexCoord * tileSize + uvOffsets[i];
blendedColor[i] = texture(texture0, uv);
}
float weightX = TexCoord.x;
float weightY = TexCoord.y;
vec4 mix1 = mix(blendedColor[0], blendedColor[2], weightX);
vec4 mix2 = mix(blendedColor[1], blendedColor[3], weightX);
vec4 finalColor = mix(mix1, mix2, weightY);
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec4 litColor = vec4(diffuse, 1.0) * finalColor;
float distance = length(viewPos - FragPos);
float interpolationFactor = clamp(distance / 1000.0, 0.0, 1.0);
outputColor = mix(litColor, FarColor, interpolationFactor);
}