Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
uniform int numLights;
uniform vec3 viewDirection;
uniform vec3 specularColor;
uniform float shininess;
uniform sampler2D samplerPos;
uniform sampler2D samplerNorm;
uniform sampler2D samplerColor;
uniform sampler2D samplerUv;
uniform samplerBuffer lightPos;
uniform samplerBuffer lightColor;
uniform samplerBuffer lightFalloff;
out vec4 fragColor;
void main()
{
vec2 screenSpaceUV = vUV.st;
vec2 resolution = uTD2DInfos[0].res.zw;
// parse data from g-buffer
vec3 position = texture( sTD2DInputs[0], screenSpaceUV ).rgb;
vec3 normal = texture( sTD2DInputs[1], screenSpaceUV ).rgb;
vec4 color = texture( sTD2DInputs[2], screenSpaceUV );
vec2 uv = texture( sTD2DInputs[3], screenSpaceUV ).rg;
// set up placeholder for final color
vec3 finalColor = vec3(0.0);
// loop through all lights
for ( int light = 0; light < numLights; ++light ){
// parse lighitng data based on the current light index
vec3 currentLightPos = texelFetchBuffer( lightPos, light ).xyz;
vec3 currentLightColor = texelFetchBuffer( lightColor, light ).xyz;
vec3 currentLightFalloff = texelFetchBuffer( lightFalloff, light ).xyz;
// calculate the distance between the current fragment and the light source
float lightDist = length( currentLightPos - position );
// diffuse contrabution
vec3 toLight = normalize( currentLightPos - position );
vec3 diffuse = max( dot( normal, toLight ), 0.0 ) * color.rgb * currentLightColor;
// specular contrabution
vec3 toViewer = normalize( position - viewDirection );
vec3 h = normalize( toLight - toViewer );
float spec = pow( max( dot( normal, h ), 0.0 ), shininess );
vec3 specular = currentLightColor * spec * specularColor;
// attenuation
float attenuation = 1.0 / ( 1.0 + currentLightFalloff.y * lightDist + currentLightFalloff.z * lightDist * lightDist );
diffuse *= attenuation;
specular *= attenuation;
// accumulate lighting
finalColor += diffuse + specular;
}
// final color out
fragColor = TDOutputSwizzle( vec4( finalColor, color.a ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment