Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

99 строки
3.4 KiB

  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. #if defined(SPECULARMAP)
  16. vec4 specularTexColor = texture2D(u_specularTexture, v_texCoord);
  17. specularColor.rgb *= specularTexColor.rgb;
  18. #endif
  19. return diffuseColor + specularColor;
  20. #else
  21. return diffuseColor;
  22. #endif
  23. }
  24. vec3 getLitPixel()
  25. {
  26. #if defined(BUMPED)
  27. vec3 normalVector = normalize(texture2D(u_normalmapTexture, v_texCoord).rgb * 2.0 - 1.0);
  28. #else
  29. vec3 normalVector = normalize(v_normalVector);
  30. #endif
  31. vec3 ambientColor = _baseColor.rgb * u_ambientColor;
  32. vec3 combinedColor = ambientColor;
  33. // Directional light contribution
  34. #if (DIRECTIONAL_LIGHT_COUNT > 0)
  35. for (int i = 0; i < DIRECTIONAL_LIGHT_COUNT; ++i)
  36. {
  37. #if defined(BUMPED)
  38. vec3 lightDirection = normalize(v_directionalLightDirection[i] * 2.0);
  39. #else
  40. vec3 lightDirection = normalize(u_directionalLightDirection[i] * 2.0);
  41. #endif
  42. combinedColor += computeLighting(normalVector, -lightDirection, u_directionalLightColor[i], 1.0);
  43. }
  44. #endif
  45. // Point light contribution
  46. #if (POINT_LIGHT_COUNT > 0)
  47. for (int i = 0; i < POINT_LIGHT_COUNT; ++i)
  48. {
  49. vec3 ldir = v_vertexToPointLightDirection[i] * u_pointLightRangeInverse[i];
  50. float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
  51. combinedColor += computeLighting(normalVector, normalize(v_vertexToPointLightDirection[i]), u_pointLightColor[i], attenuation);
  52. }
  53. #endif
  54. // Spot light contribution
  55. #if (SPOT_LIGHT_COUNT > 0)
  56. for (int i = 0; i < SPOT_LIGHT_COUNT; ++i)
  57. {
  58. // Compute range attenuation
  59. vec3 ldir = v_vertexToSpotLightDirection[i] * u_spotLightRangeInverse[i];
  60. float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
  61. vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection[i]);
  62. #if defined(BUMPED)
  63. vec3 spotLightDirection = normalize(v_spotLightDirection[i] * 2.0);
  64. #else
  65. vec3 spotLightDirection = normalize(u_spotLightDirection[i] * 2.0);
  66. #endif
  67. // "-lightDirection" is used because light direction points in opposite direction to spot direction.
  68. float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);
  69. // Apply spot attenuation
  70. attenuation *= smoothstep(u_spotLightOuterAngleCos[i], u_spotLightInnerAngleCos[i], spotCurrentAngleCos);
  71. combinedColor += computeLighting(normalVector, vertexToSpotLightDirection, u_spotLightColor[i], attenuation);
  72. }
  73. #endif
  74. return combinedColor;
  75. }