DeferredLight.glsl 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. float depth = DecodeDepth(texture2D(sDepthBuffer, vScreenPos).rgb);
  39. #ifdef ORTHO
  40. vec3 worldPos = mix(vNearRay, vFarRay, depth);
  41. #else
  42. vec3 worldPos = vFarRay * depth;
  43. #endif
  44. vec4 albedoInput = texture2D(sAlbedoBuffer, vScreenPos);
  45. vec4 normalInput = texture2D(sNormalBuffer, vScreenPos);
  46. #else
  47. float depth = DecodeDepth(texture2DProj(sDepthBuffer, vScreenPos).rgb);
  48. #ifdef ORTHO
  49. vec3 worldPos = mix(vNearRay, vFarRay, depth) / vScreenPos.w;
  50. #else
  51. vec3 worldPos = vFarRay * depth / vScreenPos.w;
  52. #endif
  53. vec4 albedoInput = texture2DProj(sAlbedoBuffer, vScreenPos);
  54. vec4 normalInput = texture2DProj(sNormalBuffer, vScreenPos);
  55. #endif
  56. vec3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  57. vec4 projWorldPos = vec4(worldPos, 1.0);
  58. vec3 lightColor;
  59. vec3 lightDir;
  60. float diff;
  61. #ifdef DIRLIGHT
  62. diff = GetDiffuse(normal, cLightDirPS, lightDir);
  63. #else
  64. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  65. diff = GetDiffuse(normal, lightVec, lightDir);
  66. #endif
  67. #ifdef SHADOW
  68. diff *= GetShadowDeferred(projWorldPos, depth);
  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 = GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  81. gl_FragColor = diff * vec4(lightColor * (albedoInput.rgb + spec * cLightColor.a * albedoInput.aaa), 0.0);
  82. #else
  83. gl_FragColor = diff * vec4(lightColor * albedoInput.rgb, 0.0);
  84. #endif
  85. }