bettola/assets/shaders/simple.frag

51 lines
1.5 KiB
GLSL

#version 330 core
out vec4 FragColor;
in vec3 FragPos;
in vec3 Normal;
in vec3 WorldPos;
in float Detail;
uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightDir; /* Normalised direction vector for the light source. */
uniform bool u_IsTerrain;
void main() {
vec3 finalColor;
vec3 norm = normalize(Normal);
/* Define colours for different terrain types. */
if(u_IsTerrain) {
vec3 grassColor1 = vec3(0.4, 0.6, 0.2);
vec3 grassColor2 = vec3(0.3, 0.5, 0.15);
vec3 rockColor = vec3(0.5, 0.5, 0.4);
vec3 steepColor = vec3(0.35, 0.3, 0.25);
/* Create patchy grass colur using the detail noise. */
float detailFactor = smoothstep(-0.2, 0.2, Detail);
vec3 finalGrassColor = mix(grassColor1, grassColor2, detailFactor);
/* Blend from grass to rock between height of 2.0 and 4.0. */
float heightFactor = smoothstep(2.0, 4.0, WorldPos.y);
finalColor = mix(finalGrassColor, rockColor, heightFactor);
/* Flat surface has a normal.y of 1.0. A steep slope is closer to 0. */
float slopeFactor = 1.0 - smoothstep(0.6, 0.8, norm.y);
finalColor = mix(finalColor, steepColor, slopeFactor);
} else {
finalColor = objectColor;
}
/* Standard lighting calculations. */
float ambientStrength = 0.4;
vec3 ambient = ambientStrength * lightColor;
float diff = max(dot(norm, normalize(-lightDir)), 0.0);
vec3 diffuse = diff * lightColor;
vec3 result = (ambient + diffuse) * finalColor;
FragColor = vec4(result, 1.0);
}