lightsFragmentFunctions.fx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Light Computing
  2. struct lightingInfo
  3. {
  4. vec3 diffuse;
  5. #ifdef SPECULARTERM
  6. vec3 specular;
  7. #endif
  8. #ifdef NDOTL
  9. float ndl;
  10. #endif
  11. };
  12. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
  13. lightingInfo result;
  14. vec3 lightVectorW;
  15. float attenuation = 1.0;
  16. if (lightData.w == 0.)
  17. {
  18. vec3 direction = lightData.xyz - vPositionW;
  19. attenuation = max(0., 1.0 - length(direction) / range);
  20. lightVectorW = normalize(direction);
  21. }
  22. else
  23. {
  24. lightVectorW = normalize(-lightData.xyz);
  25. }
  26. // diffuse
  27. float ndl = max(0., dot(vNormal, lightVectorW));
  28. #ifdef NDOTL
  29. result.ndl = ndl;
  30. #endif
  31. result.diffuse = ndl * diffuseColor * attenuation;
  32. #ifdef SPECULARTERM
  33. // Specular
  34. vec3 angleW = normalize(viewDirectionW + lightVectorW);
  35. float specComp = max(0., dot(vNormal, angleW));
  36. specComp = pow(specComp, max(1., glossiness));
  37. result.specular = specComp * specularColor * attenuation;
  38. #endif
  39. return result;
  40. }
  41. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
  42. lightingInfo result;
  43. vec3 direction = lightData.xyz - vPositionW;
  44. vec3 lightVectorW = normalize(direction);
  45. float attenuation = max(0., 1.0 - length(direction) / range);
  46. // diffuse
  47. float cosAngle = max(0., dot(lightDirection.xyz, -lightVectorW));
  48. if (cosAngle >= lightDirection.w)
  49. {
  50. cosAngle = max(0., pow(cosAngle, lightData.w));
  51. attenuation *= cosAngle;
  52. // Diffuse
  53. float ndl = max(0., dot(vNormal, lightVectorW));
  54. #ifdef NDOTL
  55. result.ndl = ndl;
  56. #endif
  57. result.diffuse = ndl * diffuseColor * attenuation;
  58. #ifdef SPECULARTERM
  59. // Specular
  60. vec3 angleW = normalize(viewDirectionW + lightVectorW);
  61. float specComp = max(0., dot(vNormal, angleW));
  62. specComp = pow(specComp, max(1., glossiness));
  63. result.specular = specComp * specularColor * attenuation;
  64. #endif
  65. return result;
  66. }
  67. result.diffuse = vec3(0.);
  68. #ifdef SPECULARTERM
  69. result.specular = vec3(0.);
  70. #endif
  71. #ifdef NDOTL
  72. result.ndl = 0.;
  73. #endif
  74. return result;
  75. }
  76. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float glossiness) {
  77. lightingInfo result;
  78. // Diffuse
  79. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  80. #ifdef NDOTL
  81. result.ndl = ndl;
  82. #endif
  83. result.diffuse = mix(groundColor, diffuseColor, ndl);
  84. #ifdef SPECULARTERM
  85. // Specular
  86. vec3 angleW = normalize(viewDirectionW + lightData.xyz);
  87. float specComp = max(0., dot(vNormal, angleW));
  88. specComp = pow(specComp, max(1., glossiness));
  89. result.specular = specComp * specularColor;
  90. #endif
  91. return result;
  92. }
  93. #define inline
  94. vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler, mat4 textureProjectionMatrix){
  95. vec4 strq = textureProjectionMatrix * vec4(vPositionW, 1.0);
  96. strq /= strq.w;
  97. return strq.xyz;
  98. }