lighting.frag 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. vec3 computeLighting(vec3 normalVector, vec3 lightDirection, vec3 lightColor, float attenuation)
  2. {
  3. float diffuse = max(dot(normalVector, lightDirection), 0.0);
  4. vec3 diffuseColor = lightColor * _baseColor.rgb * diffuse * attenuation;
  5. #if defined(SPECULAR)
  6. // Phong shading
  7. //vec3 vertexToEye = normalize(v_cameraDirection);
  8. //vec3 specularAngle = normalize(normalVector * diffuse * 2.0 - lightDirection);
  9. //vec3 specularColor = vec3(pow(clamp(dot(specularAngle, vertexToEye), 0.0, 1.0), u_specularExponent));
  10. // Blinn-Phong shading
  11. vec3 vertexToEye = normalize(v_cameraDirection);
  12. vec3 halfVector = normalize(lightDirection + vertexToEye);
  13. float specularAngle = clamp(dot(normalVector, halfVector), 0.0, 1.0);
  14. vec3 specularColor = vec3(pow(specularAngle, u_specularExponent)) * attenuation;
  15. return diffuseColor + specularColor;
  16. #else
  17. return diffuseColor;
  18. #endif
  19. }
  20. vec3 getLitPixel()
  21. {
  22. #if defined(BUMPED)
  23. vec3 normalVector = normalize(texture2D(u_normalmapTexture, v_texCoord).rgb * 2.0 - 1.0);
  24. #else
  25. vec3 normalVector = normalize(v_normalVector);
  26. #endif
  27. vec3 ambientColor = _baseColor.rgb * u_ambientColor;
  28. vec3 combinedColor = ambientColor;
  29. // Directional light contribution
  30. #if (DIRECTIONAL_LIGHT_COUNT > 0)
  31. for (int i = 0; i < DIRECTIONAL_LIGHT_COUNT; ++i)
  32. {
  33. #if defined(BUMPED)
  34. vec3 lightDirection = normalize(v_directionalLightDirection[i] * 2.0);
  35. #else
  36. vec3 lightDirection = normalize(u_directionalLightDirection[i] * 2.0);
  37. #endif
  38. combinedColor += computeLighting(normalVector, -lightDirection, u_directionalLightColor[i], 1.0);
  39. }
  40. #endif
  41. // Point light contribution
  42. #if (POINT_LIGHT_COUNT > 0)
  43. for (int i = 0; i < POINT_LIGHT_COUNT; ++i)
  44. {
  45. vec3 ldir = v_vertexToPointLightDirection[i] * u_pointLightRangeInverse[i];
  46. float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
  47. combinedColor += computeLighting(normalVector, normalize(v_vertexToPointLightDirection[i]), u_pointLightColor[i], attenuation);
  48. }
  49. #endif
  50. // Spot light contribution
  51. #if (SPOT_LIGHT_COUNT > 0)
  52. for (int i = 0; i < SPOT_LIGHT_COUNT; ++i)
  53. {
  54. // Compute range attenuation
  55. vec3 ldir = v_vertexToSpotLightDirection[i] * u_spotLightRangeInverse[i];
  56. float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
  57. vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection[i]);
  58. #if defined(BUMPED)
  59. vec3 spotLightDirection = normalize(v_spotLightDirection[i] * 2.0);
  60. #else
  61. vec3 spotLightDirection = normalize(u_spotLightDirection[i] * 2.0);
  62. #endif
  63. // "-lightDirection" is used because light direction points in opposite direction to spot direction.
  64. float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);
  65. // Apply spot attenuation
  66. attenuation *= smoothstep(u_spotLightOuterAngleCos[i], u_spotLightInnerAngleCos[i], spotCurrentAngleCos);
  67. combinedColor += computeLighting(normalVector, vertexToSpotLightDirection, u_spotLightColor[i], attenuation);
  68. }
  69. #endif
  70. return combinedColor;
  71. }