Skip to content

Instantly share code, notes, and snippets.

@thatcosmonaut
Created August 15, 2023 05:50
Show Gist options
  • Save thatcosmonaut/d293cd5e379950db8881514178ce098f to your computer and use it in GitHub Desktop.
Save thatcosmonaut/d293cd5e379950db8881514178ce098f to your computer and use it in GitHub Desktop.
Phong Lighting Shader
#version 450
layout(location = 0) in vec3 InColor;
layout(location = 1) in vec3 InNormal;
layout(location = 2) in vec3 InFragPos;
layout(location = 0) out vec4 FragColor;
layout(binding = 0, set = 3) uniform UBO
{
vec3 LightPos;
vec3 LightColor;
vec3 ViewPos;
vec3 AmbientColor;
float AmbientStrength;
float SpecularStrength;
float SpecularShininess;
} ubo;
void main()
{
// ambient
vec3 ambientColor = ubo.AmbientStrength * ubo.LightColor;
// diffuse
vec3 norm = normalize(InNormal);
vec3 lightDir = normalize(ubo.LightPos - InFragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * ubo.LightColor;
// specular
vec3 norm = normalize(InNormal);
vec3 viewDir = normalize(ubo.ViewPos - InFragPos);
vec3 lightDir = normalize(ubo.LightPos - InFragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), ubo.SpecularShininess);
vec3 specular = ubo.SpecularStrength * spec * ubo.LightColor;
vec3 result = (ambient + diffuse + specular) * InColor;
FragColor = vec4(result, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment