2
0

PrepassLight.frag 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 normalInput = texture2D(sNormalBuffer, vScreenPos);
  30. #else
  31. #if defined(HWDEPTH) && defined(ORTHO)
  32. float depth = texture2DProj(sDepthBuffer, vScreenPos).r;
  33. #elif defined(HWDEPTH) && !defined(ORTHO)
  34. float depth = ReconstructDepth(texture2DProj(sDepthBuffer, vScreenPos).r);
  35. #else
  36. float depth = DecodeDepth(texture2DProj(sDepthBuffer, vScreenPos).rgb);
  37. #endif
  38. #ifdef ORTHO
  39. vec3 worldPos = mix(vNearRay, vFarRay, depth) / vScreenPos.w;
  40. #else
  41. vec3 worldPos = vFarRay * depth / vScreenPos.w;
  42. #endif
  43. vec4 normalInput = texture2DProj(sNormalBuffer, vScreenPos);
  44. #endif
  45. vec3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  46. vec4 projWorldPos = vec4(worldPos, 1.0);
  47. vec3 lightColor;
  48. vec3 lightDir;
  49. float diff;
  50. // Accumulate light at half intensity to allow 2x "overburn"
  51. #ifdef DIRLIGHT
  52. lightDir = cLightDirPS;
  53. diff = 0.5 * GetDiffuseDir(normal, lightDir);
  54. #else
  55. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  56. diff = 0.5 * GetDiffusePointOrSpot(normal, lightVec, lightDir);
  57. #endif
  58. #ifdef SHADOW
  59. #if defined(DIRLIGHT)
  60. vec4 shadowPos = GetDirShadowPosDeferred(cLightMatricesPS, projWorldPos, depth);
  61. diff *= min(GetShadow(shadowPos) + GetShadowFade(depth), 1.0);
  62. #elif defined(SPOTLIGHT)
  63. vec4 shadowPos = cLightMatricesPS[1] * projWorldPos;
  64. diff *= GetShadow(shadowPos);
  65. #else
  66. vec3 shadowPos = worldPos - cLightPosPS.xyz;
  67. diff *= GetCubeShadow(shadowPos);
  68. #endif
  69. #endif
  70. #if defined(SPOTLIGHT)
  71. vec4 spotPos = cLightMatricesPS[0] * projWorldPos;
  72. lightColor = spotPos.w > 0.0 ? texture2DProj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : vec3(0.0);
  73. #elif defined(CUBEMASK)
  74. mat3 lightVecRot = mat3(cLightMatricesPS[0][0].xyz, cLightMatricesPS[0][1].xyz, cLightMatricesPS[0][2].xyz);
  75. lightColor = textureCube(sLightCubeMap, lightVecRot * lightVec).rgb * cLightColor.rgb;
  76. #else
  77. lightColor = cLightColor.rgb;
  78. #endif
  79. #ifdef SPECULAR
  80. float spec = lightColor.g * GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  81. gl_FragColor = diff * vec4(lightColor, spec * cLightColor.a);
  82. #else
  83. gl_FragColor = diff * vec4(lightColor, 0.0);
  84. #endif
  85. }