PrepassLight.glsl 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #ifdef DIRLIGHT
  7. varying vec2 vScreenPos;
  8. #else
  9. varying vec4 vScreenPos;
  10. #endif
  11. varying vec3 vFarRay;
  12. #ifdef ORTHO
  13. varying vec3 vNearRay;
  14. #endif
  15. void VS()
  16. {
  17. mat4 modelMatrix = iModelMatrix;
  18. vec3 worldPos = GetWorldPos(modelMatrix);
  19. gl_Position = GetClipPos(worldPos);
  20. #ifdef DIRLIGHT
  21. vScreenPos = GetScreenPosPreDiv(gl_Position);
  22. vFarRay = GetFarRay(gl_Position);
  23. #ifdef ORTHO
  24. vNearRay = GetNearRay(gl_Position);
  25. #endif
  26. #else
  27. vScreenPos = GetScreenPos(gl_Position);
  28. vFarRay = GetFarRay(gl_Position) * gl_Position.w;
  29. #ifdef ORTHO
  30. vNearRay = GetNearRay(gl_Position) * gl_Position.w;
  31. #endif
  32. #endif
  33. }
  34. void PS()
  35. {
  36. // If rendering a directional light quad, optimize out the w divide
  37. #ifdef DIRLIGHT
  38. #ifdef HWDEPTH
  39. float depth = ReconstructDepth(texture2D(sDepthBuffer, vScreenPos).r);
  40. #else
  41. float depth = DecodeDepth(texture2D(sDepthBuffer, vScreenPos).rgb);
  42. #endif
  43. #ifdef ORTHO
  44. vec3 worldPos = mix(vNearRay, vFarRay, depth);
  45. #else
  46. vec3 worldPos = vFarRay * depth;
  47. #endif
  48. vec4 normalInput = texture2D(sNormalBuffer, vScreenPos);
  49. #else
  50. #ifdef HWDEPTH
  51. float depth = ReconstructDepth(texture2DProj(sDepthBuffer, vScreenPos).r);
  52. #else
  53. float depth = DecodeDepth(texture2DProj(sDepthBuffer, vScreenPos).rgb);
  54. #endif
  55. #ifdef ORTHO
  56. vec3 worldPos = mix(vNearRay, vFarRay, depth) / vScreenPos.w;
  57. #else
  58. vec3 worldPos = vFarRay * depth / vScreenPos.w;
  59. #endif
  60. vec4 normalInput = texture2DProj(sNormalBuffer, vScreenPos);
  61. #endif
  62. // Position acquired via near/far ray is relative to camera. Bring position to world space
  63. vec3 eyeVec = -worldPos;
  64. worldPos += cCameraPosPS;
  65. vec3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  66. vec4 projWorldPos = vec4(worldPos, 1.0);
  67. vec3 lightColor;
  68. vec3 lightDir;
  69. // Accumulate light at half intensity to allow 2x "overburn"
  70. float diff = 0.5 * GetDiffuse(normal, worldPos, lightDir);
  71. #ifdef SHADOW
  72. diff *= GetShadowDeferred(projWorldPos, normal, depth);
  73. #endif
  74. #if defined(SPOTLIGHT)
  75. vec4 spotPos = projWorldPos * cLightMatricesPS[0];
  76. lightColor = spotPos.w > 0.0 ? texture2DProj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : vec3(0.0);
  77. #elif defined(CUBEMASK)
  78. mat3 lightVecRot = mat3(cLightMatricesPS[0][0].xyz, cLightMatricesPS[0][1].xyz, cLightMatricesPS[0][2].xyz);
  79. lightColor = textureCube(sLightCubeMap, (worldPos - cLightPosPS.xyz) * lightVecRot).rgb * cLightColor.rgb;
  80. #else
  81. lightColor = cLightColor.rgb;
  82. #endif
  83. #ifdef SPECULAR
  84. float spec = lightColor.g * GetSpecular(normal, eyeVec, lightDir, normalInput.a * 255.0);
  85. gl_FragColor = diff * vec4(lightColor, spec * cLightColor.a);
  86. #else
  87. gl_FragColor = diff * vec4(lightColor, 0.0);
  88. #endif
  89. }