PBRDeferred.glsl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Constants.glsl"
  7. #include "PBR.glsl"
  8. #line 40007
  9. #ifdef DIRLIGHT
  10. varying vec2 vScreenPos;
  11. #else
  12. varying vec4 vScreenPos;
  13. #endif
  14. varying vec3 vFarRay;
  15. #ifdef ORTHO
  16. varying vec3 vNearRay;
  17. #endif
  18. void VS()
  19. {
  20. mat4 modelMatrix = iModelMatrix;
  21. vec3 worldPos = GetWorldPos(modelMatrix);
  22. gl_Position = GetClipPos(worldPos);
  23. #ifdef DIRLIGHT
  24. vScreenPos = GetScreenPosPreDiv(gl_Position);
  25. vFarRay = GetFarRay(gl_Position);
  26. #ifdef ORTHO
  27. vNearRay = GetNearRay(gl_Position);
  28. #endif
  29. #else
  30. vScreenPos = GetScreenPos(gl_Position);
  31. vFarRay = GetFarRay(gl_Position) * gl_Position.w;
  32. #ifdef ORTHO
  33. vNearRay = GetNearRay(gl_Position) * gl_Position.w;
  34. #endif
  35. #endif
  36. }
  37. void PS()
  38. {
  39. // If rendering a directional light quad, optimize out the w divide
  40. #ifdef DIRLIGHT
  41. vec4 depthInput = texture2D(sDepthBuffer, vScreenPos);
  42. #ifdef HWDEPTH
  43. float depth = ReconstructDepth(depthInput.r);
  44. #else
  45. float depth = DecodeDepth(depthInput.rgb);
  46. #endif
  47. #ifdef ORTHO
  48. vec3 worldPos = mix(vNearRay, vFarRay, depth);
  49. #else
  50. vec3 worldPos = vFarRay * depth;
  51. #endif
  52. vec4 albedoInput = texture2D(sAlbedoBuffer, vScreenPos);
  53. vec4 normalInput = texture2D(sNormalBuffer, vScreenPos);
  54. vec4 specularInput = texture2D(sSpecMap, vScreenPos);
  55. #else
  56. vec4 depthInput = texture2DProj(sDepthBuffer, vScreenPos);
  57. #ifdef HWDEPTH
  58. float depth = ReconstructDepth(depthInput.r);
  59. #else
  60. float depth = DecodeDepth(depthInput.rgb);
  61. #endif
  62. #ifdef ORTHO
  63. vec3 worldPos = mix(vNearRay, vFarRay, depth) / vScreenPos.w;
  64. #else
  65. vec3 worldPos = vFarRay * depth / vScreenPos.w;
  66. #endif
  67. vec4 albedoInput = texture2DProj(sAlbedoBuffer, vScreenPos);
  68. vec4 normalInput = texture2DProj(sNormalBuffer, vScreenPos);
  69. vec4 specularInput = texture2DProj(sSpecMap, vScreenPos);
  70. #endif
  71. // Position acquired via near/far ray is relative to camera. Bring position to world space
  72. vec3 eyeVec = -worldPos;
  73. worldPos += cCameraPosPS;
  74. vec3 normal = normalInput.rgb;
  75. float roughness = length(normal);
  76. normal = normalize(normal);
  77. vec3 specColor = specularInput.rgb;
  78. vec4 projWorldPos = vec4(worldPos, 1.0);
  79. vec3 lightDir;
  80. float atten = 1;
  81. #if defined(DIRLIGHT)
  82. atten = GetAtten(normal, worldPos, lightDir);
  83. #elif defined(SPOTLIGHT)
  84. atten = GetAttenSpot(normal, worldPos, lightDir);
  85. #else
  86. atten = GetAttenPoint(normal, worldPos, lightDir);
  87. #endif
  88. float shadow = 1;
  89. #ifdef SHADOW
  90. shadow *= GetShadowDeferred(projWorldPos, normal, depth);
  91. #endif
  92. #if defined(SPOTLIGHT)
  93. vec4 spotPos = projWorldPos * cLightMatricesPS[0];
  94. vec3 lightColor = spotPos.w > 0.0 ? texture2DProj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : vec3(0.0);
  95. #elif defined(CUBEMASK)
  96. mat3 lightVecRot = mat3(cLightMatricesPS[0][0].xyz, cLightMatricesPS[0][1].xyz, cLightMatricesPS[0][2].xyz);
  97. vec3 lightColor = textureCube(sLightCubeMap, (worldPos - cLightPosPS.xyz) * lightVecRot).rgb * cLightColor.rgb;
  98. #else
  99. vec3 lightColor = cLightColor.rgb;
  100. #endif
  101. vec3 toCamera = normalize(eyeVec);
  102. vec3 lightVec = normalize(lightDir);
  103. float ndl = clamp(abs(dot(normal, lightVec)), M_EPSILON, 1.0);
  104. vec3 BRDF = GetBRDF(worldPos, lightDir, lightVec, toCamera, normal, roughness, albedoInput.rgb, specColor);
  105. gl_FragColor.a = 1.0;
  106. gl_FragColor.rgb = BRDF * lightColor * (atten * shadow) / M_PI;
  107. }