generated from karl/cpp-template
Shader cleanup
This commit is contained in:
parent
ffd3807d0d
commit
a58aea923a
@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
#version 430 core
|
#version 430 core
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in VS_OUT {
|
in VS_OUT {
|
||||||
@ -11,28 +10,28 @@ in VS_OUT {
|
|||||||
vec3 TangentFragPos;
|
vec3 TangentFragPos;
|
||||||
} fs_in;
|
} fs_in;
|
||||||
|
|
||||||
layout (binding = 0) uniform sampler2D diffuseMap;
|
layout (binding = 0) uniform sampler2D albedoMap;
|
||||||
layout (binding = 1) uniform sampler2D normalMap;
|
layout (binding = 1) uniform sampler2D normalMap;
|
||||||
layout (binding = 2) uniform sampler2D depthMap;
|
layout (binding = 2) uniform sampler2D depthMap;
|
||||||
|
|
||||||
uniform float height_scale;
|
uniform float height_scale;
|
||||||
|
|
||||||
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
vec2 get_parallax_offset_uv(vec2 uv, vec3 view_direction)
|
||||||
{
|
{
|
||||||
// number of depth layers
|
// number of depth layers
|
||||||
const float minLayers = 8;
|
const float minLayers = 8;
|
||||||
const float maxLayers = 32;
|
const float maxLayers = 32;
|
||||||
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
|
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), view_direction)));
|
||||||
// calculate the size of each layer
|
// calculate the size of each layer
|
||||||
float layerDepth = 1.0 / numLayers;
|
float layerDepth = 1.0 / numLayers;
|
||||||
// depth of current layer
|
// depth of current layer
|
||||||
float currentLayerDepth = 0.0;
|
float currentLayerDepth = 0.0;
|
||||||
// the amount to shift the texture coordinates per layer (from vector P)
|
// the amount to shift the texture coordinates per layer (from vector P)
|
||||||
vec2 P = viewDir.xy / viewDir.z * height_scale;
|
vec2 P = view_direction.xy / view_direction.z * height_scale;
|
||||||
vec2 deltaTexCoords = P / numLayers;
|
vec2 deltaTexCoords = P / numLayers;
|
||||||
|
|
||||||
// get initial values
|
// get initial values
|
||||||
vec2 currentTexCoords = texCoords;
|
vec2 currentTexCoords = uv;
|
||||||
float currentDepthMapValue = 1.0 - texture(depthMap, currentTexCoords).r;
|
float currentDepthMapValue = 1.0 - texture(depthMap, currentTexCoords).r;
|
||||||
|
|
||||||
while(currentLayerDepth < currentDepthMapValue)
|
while(currentLayerDepth < currentDepthMapValue)
|
||||||
@ -61,32 +60,38 @@ vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
|||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// offset texture coordinates with Parallax Mapping
|
// Offset texture coordinates with Parallax Mapping
|
||||||
vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
vec3 view_direction = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
||||||
vec2 texCoords = fs_in.TexCoords;
|
vec2 uv = fs_in.TexCoords;
|
||||||
|
|
||||||
texCoords = ParallaxMapping(fs_in.TexCoords, viewDir);
|
uv = get_parallax_offset_uv(fs_in.TexCoords, view_direction);
|
||||||
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
|
|
||||||
|
// 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;
|
discard;
|
||||||
|
|
||||||
// obtain normal from normal map
|
// Get normal from normal map and scale it to -1..1
|
||||||
vec3 normal = texture(normalMap, texCoords).rgb;
|
vec3 normal = texture(normalMap, uv).rgb;
|
||||||
normal = normalize(normal * 2.0 - 1.0);
|
normal = normalize(normal * 2.0 - 1.0);
|
||||||
|
|
||||||
// get diffuse color
|
// Get albedo color
|
||||||
vec3 color = texture(diffuseMap, texCoords).rgb;
|
vec3 color = texture(albedoMap, uv).rgb;
|
||||||
// ambient
|
|
||||||
|
// Ambient lighting
|
||||||
vec3 ambient = 0.1 * color;
|
vec3 ambient = 0.1 * color;
|
||||||
// diffuse
|
|
||||||
vec3 lightDir = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
// Apply albedo with intensity based on the dot product between the light direction and the normal here
|
||||||
float diff = max(dot(lightDir, normal), 0.0);
|
vec3 light_direction = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
||||||
vec3 diffuse = diff * color;
|
float light_normal_dot = max(dot(light_direction, normal), 0.0);
|
||||||
// specular
|
vec3 albedo = light_normal_dot * color;
|
||||||
vec3 reflectDir = reflect(-lightDir, normal);
|
|
||||||
vec3 halfwayDir = normalize(lightDir + viewDir);
|
// Specular lighting
|
||||||
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
|
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;
|
vec3 specular = vec3(0.2) * spec;
|
||||||
FragColor = vec4(ambient + diffuse + specular, 1.0);
|
|
||||||
|
// Apply
|
||||||
|
FragColor = vec4(ambient + albedo + specular, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#version 430
|
#version 430
|
||||||
|
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec3 aNormal;
|
layout (location = 1) in vec3 aNormal;
|
||||||
layout (location = 2) in vec2 aTexCoords;
|
layout (location = 2) in vec2 aTexCoords;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user