Space-Project/shaders/shader_parallax.frag

85 lines
3.0 KiB
GLSL

#version 330 core
out vec4 FragColor;
in VS_OUT {
vec3 FragPos;
vec2 TexCoords;
vec3 TangentLightPos;
vec3 TangentViewPos;
vec3 TangentFragPos;
} fs_in;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
uniform sampler2D depthTexture;
uniform float heightScale;
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
{
// 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), viewDir)));
// 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 = viewDir.xy / viewDir.z * heightScale;
vec2 deltaTexCoords = P / numLayers;
// get initial values
vec2 currentTexCoords = texCoords;
float currentdepthTextureValue = texture(depthTexture, currentTexCoords).r;
while(currentLayerDepth < currentdepthTextureValue)
{
// shift texture coordinates along direction of P
currentTexCoords -= deltaTexCoords;
// get depthTexture value at current texture coordinates
currentdepthTextureValue = texture(depthTexture, 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 = currentdepthTextureValue - currentLayerDepth;
float beforeDepth = texture(depthTexture, 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()
{
vec3 fragColor = vec3(0,0,0);
vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
vec2 texCoords = ParallaxMapping(fs_in.TexCoords, viewDir);
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
discard;
vec3 texture = texture2D(diffuseTexture, texCoords).rgb;
vec3 ambient = vec3(0.1, 0.1, 0.1) * texture;
// obtain normal from normal map
vec3 normal = texture2D(normalTexture, texCoords).rgb;
normal = normalize(normal * 2.0 - 1.0);
vec3 lightDir = normalize(fs_in.TangentLightPos[0] - fs_in.TangentFragPos);
float diff = max(0,dot(normal,normalize(lightDir)));
vec3 diffuse = diff * texture;
// specular
vec3 reflectDir = reflect(-lightDir, normal);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
vec3 specular = vec3(0.2) * spec;
FragColor = vec4(ambient + diffuse + specular, 1.0);
}