Lighting.vert 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. vec3 GetAmbient(float zonePos)
  2. {
  3. return cAmbientStartColor + zonePos * cAmbientEndColor;
  4. }
  5. #ifdef NUMVERTEXLIGHTS
  6. float GetVertexLight(int index, vec3 worldPos, vec3 normal)
  7. {
  8. vec3 lightDir = cVertexLights[index * 3 + 1].xyz;
  9. vec3 lightPos = cVertexLights[index * 3 + 2].xyz;
  10. float invRange = cVertexLights[index * 3].w;
  11. float cutoff = cVertexLights[index * 3 + 1].w;
  12. float invCutoff = cVertexLights[index * 3 + 2].w;
  13. // Directional light
  14. #ifndef BILLBOARD
  15. if (invRange == 0.0)
  16. {
  17. float NdotL = max(dot(normal, lightDir), 0.0);
  18. return NdotL;
  19. }
  20. // Point/spot light
  21. else
  22. {
  23. vec3 lightVec = (lightPos - worldPos) * invRange;
  24. float lightDist = length(lightVec);
  25. vec3 localDir = lightVec / lightDist;
  26. float NdotL = max(dot(normal, localDir), 0.0);
  27. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  28. float spotEffect = dot(localDir, lightDir);
  29. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  30. return NdotL * atten * spotAtten;
  31. }
  32. #else
  33. if (invRange == 0.0)
  34. return 1.0;
  35. // Point/spot light
  36. else
  37. {
  38. vec3 lightVec = (lightPos - worldPos) * invRange;
  39. float lightDist = length(lightVec);
  40. vec3 localDir = lightVec / lightDist;
  41. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  42. float spotEffect = dot(localDir, lightDir);
  43. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  44. return atten * spotAtten;
  45. }
  46. #endif
  47. }
  48. #endif