Lighting.vert 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if (invRange == 0.0)
  15. {
  16. float NdotL = max(dot(normal, lightDir), 0.0);
  17. return NdotL;
  18. }
  19. // Point/spot light
  20. else
  21. {
  22. vec3 lightVec = (lightPos - worldPos) * invRange;
  23. float lightDist = length(lightVec);
  24. vec3 localDir = lightVec / lightDist;
  25. float NdotL = max(dot(normal, localDir), 0.0);
  26. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  27. float spotEffect = dot(localDir, lightDir);
  28. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  29. return NdotL * atten * spotAtten;
  30. }
  31. }
  32. float GetVertexLightVolumetric(int index, vec3 worldPos)
  33. {
  34. vec3 lightDir = cVertexLights[index * 3 + 1].xyz;
  35. vec3 lightPos = cVertexLights[index * 3 + 2].xyz;
  36. float invRange = cVertexLights[index * 3].w;
  37. float cutoff = cVertexLights[index * 3 + 1].w;
  38. float invCutoff = cVertexLights[index * 3 + 2].w;
  39. // Directional light
  40. if (invRange == 0.0)
  41. return 1.0;
  42. // Point/spot light
  43. else
  44. {
  45. vec3 lightVec = (lightPos - worldPos) * invRange;
  46. float lightDist = length(lightVec);
  47. vec3 localDir = lightVec / lightDist;
  48. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  49. float spotEffect = dot(localDir, lightDir);
  50. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  51. return atten * spotAtten;
  52. }
  53. }
  54. #endif
  55. #ifdef SHADOW
  56. #ifdef DIRLIGHT
  57. #ifndef GL_ES
  58. #define NUMCASCADES 4
  59. #else
  60. #define NUMCASCADES 2
  61. #endif
  62. #else
  63. #define NUMCASCADES 1
  64. #endif
  65. vec4 GetShadowPos(int index, vec4 projWorldPos)
  66. {
  67. #if defined(DIRLIGHT)
  68. return cLightMatrices[index] * projWorldPos;
  69. #elif defined(SPOTLIGHT)
  70. return cLightMatrices[1] * projWorldPos;
  71. #else
  72. return vec4(projWorldPos.xyz - cLightPos.xyz, 1.0);
  73. #endif
  74. }
  75. #endif