DeferredLight.frag 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "Uniforms.frag"
  2. #include "Samplers.frag"
  3. #include "Lighting.frag"
  4. #ifdef DIRLIGHT
  5. varying vec2 vScreenPos;
  6. #else
  7. varying vec4 vScreenPos;
  8. #endif
  9. varying vec3 vFarRay;
  10. #ifdef ORTHO
  11. varying vec3 vNearRay;
  12. #endif
  13. void main()
  14. {
  15. // If rendering a directional light quad, optimize out the w divide
  16. #ifdef DIRLIGHT
  17. #if defined(HWDEPTH) && defined(ORTHO)
  18. float depth = texture2D(sDepthBuffer, vScreenPos).r;
  19. #elif defined(HWDEPTH) && !defined(ORTHO)
  20. float depth = ReconstructDepth(texture2D(sDepthBuffer, vScreenPos).r);
  21. #else
  22. float depth = DecodeDepth(texture2D(sDepthBuffer, vScreenPos).rgb);
  23. #endif
  24. #ifdef ORTHO
  25. vec3 worldPos = mix(vNearRay, vFarRay, depth);
  26. #else
  27. vec3 worldPos = vFarRay * depth;
  28. #endif
  29. vec4 albedoInput = texture2D(sAlbedoBuffer, vScreenPos);
  30. vec4 normalInput = texture2D(sNormalBuffer, vScreenPos);
  31. #else
  32. #if defined(HWDEPTH) && defined(ORTHO)
  33. float depth = texture2DProj(sDepthBuffer, vScreenPos).r;
  34. #elif defined(HWDEPTH) && !defined(ORTHO)
  35. float depth = ReconstructDepth(texture2DProj(sDepthBuffer, vScreenPos).r);
  36. #else
  37. float depth = DecodeDepth(texture2DProj(sDepthBuffer, vScreenPos).rgb);
  38. #endif
  39. #ifdef ORTHO
  40. vec3 worldPos = mix(vNearRay, vFarRay, depth) / vScreenPos.w;
  41. #else
  42. vec3 worldPos = vFarRay * depth / vScreenPos.w;
  43. #endif
  44. vec4 albedoInput = texture2DProj(sAlbedoBuffer, vScreenPos);
  45. vec4 normalInput = texture2DProj(sNormalBuffer, vScreenPos);
  46. #endif
  47. vec3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  48. vec4 projWorldPos = vec4(worldPos, 1.0);
  49. vec3 lightColor;
  50. vec3 lightDir;
  51. float diff;
  52. #ifdef DIRLIGHT
  53. lightDir = cLightDirPS;
  54. diff = GetDiffuseDir(normal, lightDir);
  55. #else
  56. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  57. diff = GetDiffusePointOrSpot(normal, lightVec, lightDir);
  58. #endif
  59. #ifdef SHADOW
  60. #if defined(DIRLIGHT)
  61. vec4 shadowPos = GetDirShadowPosDeferred(cLightMatricesPS, projWorldPos, depth);
  62. diff *= min(GetShadow(shadowPos) + GetShadowFade(depth), 1.0);
  63. #elif defined(SPOTLIGHT)
  64. vec4 shadowPos = cLightMatricesPS[1] * projWorldPos;
  65. diff *= GetShadow(shadowPos);
  66. #else
  67. vec3 shadowPos = worldPos - cLightPosPS.xyz;
  68. diff *= GetCubeShadow(shadowPos);
  69. #endif
  70. #endif
  71. #if defined(SPOTLIGHT)
  72. vec4 spotPos = cLightMatricesPS[0] * projWorldPos;
  73. lightColor = spotPos.w > 0.0 ? texture2DProj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : vec3(0.0);
  74. #elif defined(CUBEMASK)
  75. mat3 lightVecRot = mat3(cLightMatricesPS[0][0].xyz, cLightMatricesPS[0][1].xyz, cLightMatricesPS[0][2].xyz);
  76. lightColor = textureCube(sLightCubeMap, lightVecRot * lightVec).rgb * cLightColor.rgb;
  77. #else
  78. lightColor = cLightColor.rgb;
  79. #endif
  80. #ifdef SPECULAR
  81. float spec = GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  82. gl_FragColor = diff * vec4(lightColor * (albedoInput.rgb + spec * cLightColor.a * albedoInput.aaa), 0.0);
  83. #else
  84. gl_FragColor = diff * vec4(lightColor * albedoInput.rgb, 0.0);
  85. #endif
  86. }