grk464933/cw 8/shaders/shader_8_2h.frag
2024-02-02 11:19:35 +01:00

42 lines
1.3 KiB
GLSL

#version 330 core
in vec2 fragTexCoord;
out vec4 outColor;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D heightmap;
float waterThreshold = 0.3;
float grassThreshold = 0.6;
in vec3 viewDirTS;
in vec3 lightDirTS;
uniform vec3 lightPos;
uniform vec3 cameraPos;
vec3 blend(vec3 texture1, float height1, vec3 texture2, float height2, float blend_ratio)
{
float mix_threshold = 0.1;
float h1 = height1 + 1.0 - blend_ratio;
float h2 = height2 + blend_ratio;
float havg = (h1 + h2) / 2.0;
h1 = clamp((h1 - havg + 0.5 * mix_threshold) / (mix_threshold), 0.0, 1.0);
h2 = clamp((h2 - havg + 0.5 * mix_threshold) / (mix_threshold), 0.0, 1.0);
return texture1 * h1 + texture2 * h2;
}
void main()
{
float height = texture2D(heightmap, fragTexCoord).r;
vec3 textureColor = blend(texture2D(texture1, fragTexCoord).xyz, height, texture2D(texture2, fragTexCoord).xyz,0.5, 0.55);
textureColor = blend(texture2D(texture3, fragTexCoord).xyz, height, textureColor, 0.65, 0.55);
float diffuseIntensity = max(0.0, dot(normalize(vec3(0, 0, 1)), normalize(lightDirTS)));
float ambientIntensity = 0.3;
vec3 finalColor = textureColor.rgb * (diffuseIntensity + ambientIntensity);
outColor = vec4(finalColor, 1.0);
}