Sin descripción

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. layout (binding = 1) uniform samplerCube samplerEnvMap;
  5. layout (location = 0) in vec3 inUVW;
  6. layout (location = 1) in vec3 inPos;
  7. layout (location = 2) in vec3 inNormal;
  8. layout (location = 3) in vec3 inViewVec;
  9. layout (location = 4) in vec3 inLightVec;
  10. layout (location = 5) in mat4 inInvModelView;
  11. layout (location = 0) out vec4 outColor0;
  12. layout (location = 1) out vec4 outColor1;
  13. layout (constant_id = 0) const int type = 0;
  14. #define PI 3.1415926
  15. #define TwoPI (2.0 * PI)
  16. layout (binding = 2) uniform UBO {
  17. float exposure;
  18. } ubo;
  19. void main()
  20. {
  21. vec4 color;
  22. vec3 wcNormal;
  23. switch (type) {
  24. case 0: // Skybox
  25. {
  26. vec3 normal = normalize(inUVW);
  27. color = texture(samplerEnvMap, normal);
  28. }
  29. break;
  30. case 1: // Reflect
  31. {
  32. vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec);
  33. vec3 normal = normalize(inNormal);
  34. vec3 wNormal = mat3(inInvModelView) * normal;
  35. float NdotL = max(dot(normal, inLightVec), 0.0);
  36. vec3 eyeDir = normalize(inViewVec);
  37. vec3 halfVec = normalize(inLightVec + eyeDir);
  38. float NdotH = max(dot(normal, halfVec), 0.0);
  39. float NdotV = max(dot(normal, eyeDir), 0.0);
  40. float VdotH = max(dot(eyeDir, halfVec), 0.0);
  41. // Geometric attenuation
  42. float NH2 = 2.0 * NdotH;
  43. float g1 = (NH2 * NdotV) / VdotH;
  44. float g2 = (NH2 * NdotL) / VdotH;
  45. float geoAtt = min(1.0, min(g1, g2));
  46. const float F0 = 0.6;
  47. const float k = 0.2;
  48. // Fresnel (schlick approximation)
  49. float fresnel = pow(1.0 - VdotH, 5.0);
  50. fresnel *= (1.0 - F0);
  51. fresnel += F0;
  52. float spec = (fresnel * geoAtt) / (NdotV * NdotL * 3.14);
  53. color = texture(samplerEnvMap, reflect(-wViewVec, wNormal));
  54. color = vec4(color.rgb * NdotL * (k + spec * (1.0 - k)), 1.0);
  55. }
  56. break;
  57. case 2: // Refract
  58. {
  59. vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec);
  60. vec3 wNormal = mat3(inInvModelView) * inNormal;
  61. color = texture(samplerEnvMap, refract(-wViewVec, wNormal, 1.0/1.6));
  62. }
  63. break;
  64. }
  65. // Color with manual exposure into attachment 0
  66. outColor0.rgb = vec3(1.0) - exp(-color.rgb * ubo.exposure);
  67. // Bright parts for bloom into attachment 1
  68. float l = dot(outColor0.rgb, vec3(0.2126, 0.7152, 0.0722));
  69. float threshold = 0.75;
  70. outColor1.rgb = (l > threshold) ? outColor0.rgb : vec3(0.0);
  71. outColor1.a = 1.0;
  72. }