lighting.fs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #version 300 es
  2. precision highp float;
  3. in vec2 fragTexCoord;
  4. uniform sampler2D gbufferColor;
  5. uniform sampler2D gbufferNormal;
  6. uniform sampler2D gbufferDepth;
  7. uniform sampler2D ssao;
  8. uniform vec3 camPos;
  9. uniform mat4 camInvViewProj;
  10. uniform vec3 lightDir;
  11. uniform vec3 sunColor;
  12. uniform float sunStrength;
  13. uniform vec3 skyColor;
  14. uniform float skyStrength;
  15. uniform float groundStrength;
  16. uniform float ambientStrength;
  17. uniform float exposure;
  18. uniform float camClipNear;
  19. uniform float camClipFar;
  20. out vec4 finalColor;
  21. #define PI 3.14159265358979323846264338327950288
  22. vec3 ToGamma(in vec3 col)
  23. {
  24. return vec3(pow(col.x, 2.2), pow(col.y, 2.2), pow(col.z, 2.2));
  25. }
  26. vec3 FromGamma(in vec3 col)
  27. {
  28. return vec3(pow(col.x, 1.0/2.2), pow(col.y, 1.0/2.2), pow(col.z, 1.0/2.2));
  29. }
  30. float LinearDepth(float depth, float near, float far)
  31. {
  32. return (2.0 * near) / (far + near - depth * (far - near));
  33. }
  34. float NonlinearDepth(float depth, float near, float far)
  35. {
  36. return (((2.0 * near) / depth) - far - near) / (near - far);
  37. }
  38. void main()
  39. {
  40. // Unpack GBuffer
  41. float depth = texture(gbufferDepth, fragTexCoord).r;
  42. if (depth == 1.0f) { discard; }
  43. vec4 colorAndSpec = texture(gbufferColor, fragTexCoord);
  44. vec4 normalAndGlossiness = texture(gbufferNormal, fragTexCoord);
  45. vec3 positionClip = vec3(fragTexCoord, NonlinearDepth(depth, camClipNear, camClipFar)) * 2.0f - 1.0f;
  46. vec4 fragPositionHomo = camInvViewProj * vec4(positionClip, 1);
  47. vec3 fragPosition = fragPositionHomo.xyz / fragPositionHomo.w;
  48. vec4 ssaoData = texture(ssao, fragTexCoord);
  49. vec3 fragNormal = normalAndGlossiness.xyz * 2.0f - 1.0f;
  50. vec3 albedo = colorAndSpec.xyz;
  51. float specularity = colorAndSpec.w;
  52. float glossiness = normalAndGlossiness.w * 100.0f;
  53. float sunShadow = ssaoData.g;
  54. float ambientShadow = ssaoData.r;
  55. // Compute lighting
  56. vec3 eyeDir = normalize(fragPosition - camPos);
  57. vec3 lightSunColor = FromGamma(sunColor);
  58. vec3 lightSunHalf = normalize(-lightDir - eyeDir);
  59. vec3 lightSkyColor = FromGamma(skyColor);
  60. vec3 skyDir = vec3(0.0, -1.0, 0.0);
  61. vec3 lightSkyHalf = normalize(-skyDir - eyeDir);
  62. float sunFactorDiff = max(dot(fragNormal, -lightDir), 0.0);
  63. float sunFactorSpec = specularity *
  64. ((glossiness+2.0) / (8.0 * PI)) *
  65. pow(max(dot(fragNormal, lightSunHalf), 0.0), glossiness);
  66. float skyFactorDiff = max(dot(fragNormal, -skyDir), 0.0);
  67. float skyFactorSpec = specularity *
  68. ((glossiness+2.0) / (8.0 * PI)) *
  69. pow(max(dot(fragNormal, lightSkyHalf), 0.0), glossiness);
  70. float groundFactorDiff = max(dot(fragNormal, skyDir), 0.0);
  71. // Combine
  72. vec3 ambient = ambientShadow * ambientStrength * lightSkyColor * albedo;
  73. vec3 diffuse = sunShadow * sunStrength * lightSunColor * albedo * sunFactorDiff +
  74. groundStrength * lightSkyColor * albedo * groundFactorDiff +
  75. skyStrength * lightSkyColor * albedo * skyFactorDiff;
  76. float specular = sunShadow * sunStrength * sunFactorSpec + skyStrength * skyFactorSpec;
  77. vec3 final = diffuse + ambient + specular;
  78. finalColor = vec4(ToGamma(exposure * final), 1.0f);
  79. //finalColor = vec4(ToGamma(vec3(ambientShadow, ambientShadow, ambientShadow)), 1.0f);
  80. //finalColor = vec4(ToGamma(vec3(sunShadow, sunShadow, sunShadow)), 1.0f);
  81. //finalColor = vec4(ToGamma(vec3(specular, specular, specular)), 1.0f);
  82. gl_FragDepth = NonlinearDepth(depth, camClipNear, camClipFar);
  83. }