]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - shaders/3d/fragment.glsl
Add fog and face culling
[dragonblocks_alpha.git] / shaders / 3d / fragment.glsl
1 in vec3 fragmentPosition;
2 in vec3 fragmentNormal;
3 in float fragmentTextureIndex;
4 in vec2 fragmentTextureCoords;
5 in vec3 fragmentColor;
6
7 out vec4 outColor;
8
9 uniform float daylight;
10 uniform vec3 lightDir;
11 uniform vec3 cameraPos;
12 uniform sampler2D textures[MAX_TEXTURE_UNITS];
13
14 vec3 hsv2rgb(vec3 c)
15 {
16     vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
17     vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
18     return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
19 }
20
21 void main()
22 {
23         vec3 lightColor = vec3(1.0);
24         vec3 fogColor = vec3(0x87, 0xCE, 0xEB) / vec3(0xFF);
25
26         float ambientStrength = mix(0.3, 0.7, daylight);
27
28         vec3 ambient = ambientStrength * lightColor;
29         vec3 diffuse = 0.3 * daylight * clamp(dot(normalize(fragmentNormal), normalize(lightDir)), 0.0, 1.0) * lightColor;
30
31         vec3 light = ambient + diffuse;
32
33         outColor = texture(textures[int(fragmentTextureIndex + 0.5)], fragmentTextureCoords) * vec4(hsv2rgb(vec3(fragmentColor)), 1.0) * vec4(light, 1.0);
34         outColor.rgb = mix(outColor.rgb, ambientStrength * vec3(fogColor), clamp(length(fragmentPosition - cameraPos) / 255.0, 0.0, 1.0));
35 }