generated from karl/cpp-template
98 lines
3.3 KiB
GLSL
98 lines
3.3 KiB
GLSL
#version 430 core
|
|
|
|
out vec4 FragColor;
|
|
|
|
in VS_OUT {
|
|
vec3 FragPos;
|
|
vec2 TexCoords;
|
|
vec3 TangentLightPos;
|
|
vec3 TangentViewPos;
|
|
vec3 TangentFragPos;
|
|
} fs_in;
|
|
|
|
layout (binding = 0) uniform sampler2D albedoMap;
|
|
layout (binding = 1) uniform sampler2D normalMap;
|
|
layout (binding = 2) uniform sampler2D depthMap;
|
|
|
|
uniform float height_scale;
|
|
|
|
vec2 get_parallax_offset_uv(vec2 uv, vec3 view_direction)
|
|
{
|
|
// number of depth layers
|
|
const float minLayers = 8;
|
|
const float maxLayers = 32;
|
|
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), view_direction)));
|
|
// calculate the size of each layer
|
|
float layerDepth = 1.0 / numLayers;
|
|
// depth of current layer
|
|
float currentLayerDepth = 0.0;
|
|
// the amount to shift the texture coordinates per layer (from vector P)
|
|
vec2 P = view_direction.xy / view_direction.z * height_scale;
|
|
vec2 deltaTexCoords = P / numLayers;
|
|
|
|
// get initial values
|
|
vec2 currentTexCoords = uv;
|
|
float currentDepthMapValue = 1.0 - texture(depthMap, currentTexCoords).r;
|
|
|
|
while(currentLayerDepth < currentDepthMapValue)
|
|
{
|
|
// shift texture coordinates along direction of P
|
|
currentTexCoords -= deltaTexCoords;
|
|
// get depthmap value at current texture coordinates
|
|
currentDepthMapValue = 1.0 - texture(depthMap, currentTexCoords).r;
|
|
// get depth of next layer
|
|
currentLayerDepth += layerDepth;
|
|
}
|
|
|
|
// get texture coordinates before collision (reverse operations)
|
|
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
|
|
|
|
// get depth after and before collision for linear interpolation
|
|
float afterDepth = currentDepthMapValue - currentLayerDepth;
|
|
float beforeDepth = 1.0 - texture(depthMap, prevTexCoords).r - currentLayerDepth + layerDepth;
|
|
|
|
// interpolation of texture coordinates
|
|
float weight = afterDepth / (afterDepth - beforeDepth);
|
|
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
|
|
|
|
return finalTexCoords;
|
|
}
|
|
|
|
void main()
|
|
{
|
|
// Offset texture coordinates with Parallax Mapping
|
|
vec3 view_direction = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
|
vec2 uv = fs_in.TexCoords;
|
|
|
|
uv = get_parallax_offset_uv(fs_in.TexCoords, view_direction);
|
|
|
|
// Discard if the parallax offset moved us outside of the texture
|
|
if (uv.x > 1.0 || uv.y > 1.0 || uv.x < 0.0 || uv.y < 0.0)
|
|
discard;
|
|
|
|
// Get normal from normal map and scale it to -1..1
|
|
vec3 normal = texture(normalMap, uv).rgb;
|
|
normal = normalize(normal * 2.0 - 1.0);
|
|
|
|
// Get albedo color
|
|
vec3 color = texture(albedoMap, uv).rgb;
|
|
|
|
// Ambient lighting
|
|
vec3 ambient = 0.1 * color;
|
|
|
|
// Apply albedo with intensity based on the dot product between the light direction and the normal here
|
|
vec3 light_direction = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
|
float light_normal_dot = max(dot(light_direction, normal), 0.0);
|
|
vec3 albedo = light_normal_dot * color;
|
|
|
|
// Specular lighting
|
|
vec3 halfway_reflected_light_direction = normalize(light_direction + view_direction);
|
|
float spec = pow(max(dot(normal, halfway_reflected_light_direction), 0.0), 32.0);
|
|
|
|
vec3 specular = vec3(0.2) * spec;
|
|
|
|
// Apply
|
|
FragColor = vec4(ambient + albedo + specular, 1.0);
|
|
}
|
|
|