Skip to content

Instantly share code, notes, and snippets.

@mlfarrell
Created June 10, 2023 00:12
Show Gist options
  • Save mlfarrell/c9b8f523e5222a84ec5881996314c464 to your computer and use it in GitHub Desktop.
Save mlfarrell/c9b8f523e5222a84ec5881996314c464 to your computer and use it in GitHub Desktop.
//
// Full lighting shader template
// Verto Studio 3D
// Created by Mike Farrell
//
// Please read the shader section of the user-guide
// for more information on shader programming
// within Verto Studio
precision mediump float;
in mediump vec3 va_normal;
in mediump vec4 va_eyepos;
out vec4 fragColor;
//Material colors
struct Material
{
lowp vec4 ambient, diffuse, specular, emissive;
lowp float alpha;
mediump float shininess;
};
uniform Material material;
//Lights
struct Light
{
mediump vec4 position;
lowp vec4 ambient, diffuse, specular;
mediump vec3 spotDirection;
float spotExponent, spotCosCutoff;
float constantAttenuation, linearAttenuation,
quadraticAttenuation;
bool enabled;
};
uniform Light lights[8]; //up to 8 lights
uniform vec4 lightModelProductSceneColor;
int getIndexOfFirstLight()
{
for(int i = 0; i < 8; i++)
{
if(lights[i].enabled)
return i;
}
return -1;
}
void main()
{
vec4 diffuseColor = vec4(0.0),
ambientColor = vec4(0.0),
specularColor = vec4(0.0);
vec3 lightColor = vec3(0.0);
//perform point lighting
for(int i = 0; i < 8; i++)
{
if(lights[i].enabled == false)
continue;
mediump vec3 ecPosition3, VP;
mediump float nDotVP, nDotHV, pf;
mediump vec3 normal, halfVector;
mediump vec3 eye;
eye = vec3(0.0, 0.0, 1.0);
normal = normalize(va_normal);
//get the vector from the viewer to the light
ecPosition3 = (vec3(va_eyepos)) / va_eyepos.w;
VP = normalize(vec3(lights[0].position)-ecPosition3);
halfVector = normalize(VP + eye);
//calculate the cosine of the angle between the
//normal vector and the light
nDotVP = dot(normal, VP);
//use light and material colors + nDotVP cosine
//fall-off to compute final light color
diffuseColor += lights[i].diffuse * material.diffuse *
nDotVP;
ambientColor += lights[i].ambient * material.ambient;
//specular component
nDotHV = max(0.0, dot(normal, halfVector));
pf = pow(nDotHV, material.shininess);
specularColor += lights[i].specular
* material.specular*pf;
}
//final light sum
lightColor = (lightModelProductSceneColor +
diffuseColor + ambientColor +
specularColor).rgb;
lightColor = clamp(lightColor, 0.0, 1.0);
fragColor = vec4(lightColor, material.alpha);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment