Created
September 21, 2021 05:28
-
-
Save wolf81/1a94cfc32a4ade98b2e661744548fd95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
in vec3 Normal; | |
in vec3 FragmentPos; | |
uniform vec3 viewPos; | |
uniform struct Fog { | |
vec2 range; | |
vec4 color; | |
} fog; | |
uniform struct PointLight { | |
vec3 position; | |
// attenuation | |
float constant; | |
float linear; | |
float quadratic; | |
vec3 ambient; | |
vec3 diffuse; | |
vec3 specular; | |
} light; | |
vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) | |
{ | |
vec3 norm = normalize(Normal); | |
vec3 lightDir = normalize(light.position - FragmentPos); | |
float diff = max(dot(norm, lightDir), 0.0); | |
// specular | |
vec3 reflectDir = reflect(-lightDir, norm); | |
vec3 viewDir = normalize(viewPos - FragmentPos); | |
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 1.0) * lovrMetalness; | |
// attenuation | |
float distance = length(light.position - FragmentPos); | |
float attenuation = 1.0 / (light.constant + light.linear * distance + | |
light.quadratic * (distance * distance)); | |
// combine results | |
vec3 ambient = light.ambient * vec3(texture(image, uv)); | |
vec3 diffuse = light.diffuse * diff * vec3(texture(image, uv)); | |
vec3 specular = light.specular * spec * vec3(texture(image, uv)); | |
ambient *= attenuation; | |
diffuse *= attenuation; | |
specular *= attenuation; | |
vec4 baseColor = vec4((ambient + diffuse + specular), 1.0); | |
// fog | |
float near = fog.range.x; | |
float far = fog.range.y; | |
float fogAmount = clamp((distance - near) / (far - near), 0.0, 0.97); | |
return mix(baseColor, fog.color, fogAmount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment