DeferredLight.glsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 albedoInput = texture2D(sAlbedoBuffer, vScreenPos);
  49. vec4 normalInput = texture2D(sNormalBuffer, vScreenPos);
  50. #else
  51. #ifdef HWDEPTH
  52. float depth = ReconstructDepth(texture2DProj(sDepthBuffer, vScreenPos).r);
  53. #else
  54. float depth = DecodeDepth(texture2DProj(sDepthBuffer, vScreenPos).rgb);
  55. #endif
  56. #ifdef ORTHO
  57. vec3 worldPos = mix(vNearRay, vFarRay, depth) / vScreenPos.w;
  58. #else
  59. vec3 worldPos = vFarRay * depth / vScreenPos.w;
  60. #endif
  61. vec4 albedoInput = texture2DProj(sAlbedoBuffer, vScreenPos);
  62. vec4 normalInput = texture2DProj(sNormalBuffer, vScreenPos);
  63. #endif
  64. // Position acquired via near/far ray is relative to camera. Bring position to world space
  65. vec3 eyeVec = -worldPos;
  66. worldPos += cCameraPosPS;
  67. vec3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  68. vec4 projWorldPos = vec4(worldPos, 1.0);
  69. vec3 lightColor;
  70. vec3 lightDir;
  71. float diff = GetDiffuse(normal, worldPos, lightDir);
  72. #ifdef SHADOW
  73. diff *= GetShadowDeferred(projWorldPos, normal, depth);
  74. #endif
  75. #if defined(SPOTLIGHT)
  76. vec4 spotPos = projWorldPos * cLightMatricesPS[0];
  77. lightColor = spotPos.w > 0.0 ? texture2DProj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : vec3(0.0);
  78. #elif defined(CUBEMASK)
  79. mat3 lightVecRot = mat3(cLightMatricesPS[0][0].xyz, cLightMatricesPS[0][1].xyz, cLightMatricesPS[0][2].xyz);
  80. lightColor = textureCube(sLightCubeMap, (worldPos - cLightPosPS.xyz) * lightVecRot).rgb * cLightColor.rgb;
  81. #else
  82. lightColor = cLightColor.rgb;
  83. #endif
  84. #ifdef SPECULAR
  85. float spec = GetSpecular(normal, eyeVec, lightDir, normalInput.a * 255.0);
  86. gl_FragColor = diff * vec4(lightColor * (albedoInput.rgb + spec * cLightColor.a * albedoInput.aaa), 0.0);
  87. #else
  88. gl_FragColor = diff * vec4(lightColor * albedoInput.rgb, 0.0);
  89. #endif
  90. }