2016-06-18 19:24:37 +02:00
|
|
|
#version 450
|
|
|
|
|
|
2016-06-25 13:30:55 +02:00
|
|
|
layout (set = 0, binding = 1) uniform sampler2D samplerHeight;
|
|
|
|
|
layout (set = 0, binding = 2) uniform sampler2DArray samplerLayers;
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
layout (location = 0) in vec3 inNormal;
|
|
|
|
|
layout (location = 1) in vec2 inUV;
|
2016-06-25 13:30:55 +02:00
|
|
|
layout (location = 2) in vec3 inViewVec;
|
2016-06-18 19:24:37 +02:00
|
|
|
layout (location = 3) in vec3 inLightVec;
|
2016-06-25 20:40:22 +02:00
|
|
|
layout (location = 4) in vec3 inEyePos;
|
|
|
|
|
layout (location = 5) in vec3 inWorldPos;
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
layout (location = 0) out vec4 outFragColor;
|
|
|
|
|
|
2016-06-25 13:30:55 +02:00
|
|
|
vec3 sampleTerrainLayer()
|
2016-06-18 19:24:37 +02:00
|
|
|
{
|
|
|
|
|
// Define some layer ranges for sampling depending on terrain height
|
2016-06-19 13:35:09 +02:00
|
|
|
vec2 layers[6];
|
|
|
|
|
layers[0] = vec2(-10.0, 10.0);
|
2016-06-25 21:23:12 +02:00
|
|
|
layers[1] = vec2(5.0, 45.0);
|
|
|
|
|
layers[2] = vec2(45.0, 80.0);
|
|
|
|
|
layers[3] = vec2(75.0, 100.0);
|
|
|
|
|
layers[4] = vec2(95.0, 140.0);
|
2016-06-19 13:35:09 +02:00
|
|
|
layers[5] = vec2(140.0, 190.0);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
vec3 color = vec3(0.0);
|
|
|
|
|
|
|
|
|
|
// Get height from displacement map
|
2016-06-25 13:30:55 +02:00
|
|
|
float height = textureLod(samplerHeight, inUV, 0.0).r * 255.0;
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2016-06-19 13:35:09 +02:00
|
|
|
for (int i = 0; i < 6; i++)
|
2016-06-18 19:24:37 +02:00
|
|
|
{
|
|
|
|
|
float range = layers[i].y - layers[i].x;
|
|
|
|
|
float weight = (range - abs(height - layers[i].y)) / range;
|
2016-06-19 13:35:09 +02:00
|
|
|
weight = max(0.0, weight);
|
2016-06-25 13:30:55 +02:00
|
|
|
color += weight * texture(samplerLayers, vec3(inUV * 16.0, i)).rgb;
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-25 13:30:55 +02:00
|
|
|
return color;
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-25 20:40:22 +02:00
|
|
|
float fog(float density)
|
|
|
|
|
{
|
|
|
|
|
const float LOG2 = -1.442695;
|
|
|
|
|
float dist = gl_FragCoord.z / gl_FragCoord.w * 0.1;
|
|
|
|
|
float d = density * dist;
|
|
|
|
|
return 1.0 - clamp(exp2(d * d * LOG2), 0.0, 1.0);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
vec3 N = normalize(inNormal);
|
2016-06-25 13:30:55 +02:00
|
|
|
vec3 L = normalize(inLightVec);
|
|
|
|
|
vec3 ambient = vec3(0.5);
|
|
|
|
|
vec3 diffuse = max(dot(N, L), 0.0) * vec3(1.0);
|
2016-06-25 20:40:22 +02:00
|
|
|
|
|
|
|
|
vec4 color = vec4((ambient + diffuse) * sampleTerrainLayer(), 1.0);
|
|
|
|
|
|
|
|
|
|
const vec4 fogColor = vec4(0.47, 0.5, 0.67, 0.0);
|
|
|
|
|
outFragColor = mix(color, fogColor, fog(0.25));
|
2016-06-23 22:01:48 +02:00
|
|
|
}
|