Home | History | Annotate | Download | only in ShaderLib
      1 #ifndef NUM_LIGHTS
      2     #define NUM_LIGHTS 4
      3 #endif
      4 
      5 uniform mat4 g_ViewMatrix;
      6 uniform vec4 g_LightPosition[NUM_LIGHTS];
      7 uniform vec4 g_g_LightColor[NUM_LIGHTS];
      8 uniform float m_Shininess;
      9 
     10 float Lighting_Diffuse(vec3 norm, vec3 lightdir){
     11     return max(0.0, dot(norm, lightdir));
     12 }
     13 
     14 float Lighting_Specular(vec3 norm, vec3 viewdir, vec3 lightdir, float shiny){
     15     vec3 refdir = reflect(-lightdir, norm);
     16     return pow(max(dot(refdir, viewdir), 0.0), shiny);
     17 }
     18 
     19 void Lighting_Direction(vec3 worldPos, vec4 color, vec4 position, out vec4 lightDir){
     20     float posLight = step(0.5, color.w);
     21     vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight);
     22     float dist = length(tempVec);
     23 
     24     lightDir.w = clamp(1.0 - position.w * dist * posLight, 0.0, 1.0);
     25     lightDir.xyz = tempVec / dist;
     26 }
     27 
     28 void Lighting_ComputePS(vec3 tanNormal, mat3 tbnMat,
     29                      int lightCount, out vec3 outDiffuse, out vec3 outSpecular){
     30    // find tangent view dir & vert pos
     31    vec3 tanViewDir = viewDir * tbnMat;
     32 
     33    for (int i = 0; i < lightCount; i++){
     34        // find light dir in tangent space, works for point & directional lights
     35        vec4 wvLightPos = (g_ViewMatrix * vec4(g_LightPosition[i].xyz, g_LightColor[i].w));
     36        wvLightPos.w = g_LightPosition[i].w;
     37 
     38        vec4 tanLightDir;
     39        Lighting_Direction(wvPosition, g_LightColor[i], wvLightPos, tanLightDir);
     40        tanLightDir.xyz = tanLightDir.xyz * tbnMat;
     41 
     42        vec3 lightScale = g_LightColor[i].rgb * tanLightDir.w;
     43        float specular = Lighting_Specular(tanNormal, tanViewDir, tanLightDir.xyz, m_Shininess);
     44        float diffuse = Lighting_Diffuse(tanNormal, tanLightDir.xyz);
     45        outSpecular += specular * lightScale * step(0.01, diffuse) * g_LightColor[i].rgb;
     46        outDiffuse += diffuse * lightScale * g_LightColor[i].rgb;
     47    }
     48 }
     49