Skip to content

Instantly share code, notes, and snippets.

@prankush-tech
Created September 5, 2024 23:28
Show Gist options
  • Save prankush-tech/789943bed47347e47e3a2aad8a77a0e9 to your computer and use it in GitHub Desktop.
Save prankush-tech/789943bed47347e47e3a2aad8a77a0e9 to your computer and use it in GitHub Desktop.
In GLSL, **point light** emits light from a specific position in all directions with intensity decreasing over distance. **Directional light** has no position, shining uniformly from a direction. **Ambient light** provides constant, non-directional lighting to simulate global illumination.
vec3 pointLight(vec3 lightColor, float lightIntensity, vec3 normal, vec3 lightPosition, vec3 viewDirection, float specularPower, vec3 position, float lightDecay)
{
vec3 lightDelta = lightPosition - position;
float lightDistance = length(lightDelta);
vec3 lightDirection = normalize(lightDelta);
vec3 lightReflection = reflect(- lightDirection, normal);
// Shading
float shading = dot(normal, lightDirection);
shading = max(0.0, shading);
// Specular
float specular = - dot(lightReflection, viewDirection);
specular = max(0.0, specular);
specular = pow(specular, specularPower);
// Decay
float decay = 1.0 - lightDistance * lightDecay;
decay = max(0.0, decay);
return lightColor * lightIntensity * decay * (shading + specular);
}
vec3 directionalLight(vec3 lightColor, float lightIntensity, vec3 normal, vec3 lightPosition, vec3 viewDirection, float specularPower)
{
vec3 lightDirection = normalize(lightPosition);
vec3 lightReflection = reflect(- lightDirection, normal);
// Shading
float shading = dot(normal, lightDirection);
shading = max(0.0, shading);
// Specular
float specular = - dot(lightReflection, viewDirection);
specular = max(0.0, specular);
specular = pow(specular, specularPower);
return lightColor * lightIntensity * (shading + specular);
}
vec3 ambientLight(vec3 lightColor, float lightIntensity)
{
return lightColor * lightIntensity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment