Guest User

Untitled

a guest
Nov 2nd, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #version 410 core
  2. out vec4 FragColor;
  3.  
  4. in VS_OUT {
  5. vec3 FragPos;
  6. vec3 Normal;
  7. vec2 TexCoords;
  8. } fs_in;
  9.  
  10. //uniform sampler2D diffuseTexture; //*********************************************** this breaks it
  11. uniform samplerCube cubeMapTexture;
  12. uniform sampler2DArray shadowMap;
  13.  
  14. uniform vec3 lightDir;
  15. uniform vec3 viewPos;
  16. uniform float farPlane;
  17.  
  18. uniform mat4 view;
  19.  
  20. layout (std140) uniform LightSpaceMatrices
  21. {
  22. mat4 lightSpaceMatrices[16];
  23. };
  24. uniform float cascadePlaneDistances[16];
  25. uniform int cascadeCount; // number of frusta - 1
  26.  
  27. float ShadowCalculation(vec3 fragPosWorldSpace)
  28. {
  29. // select cascade layer
  30. vec4 fragPosViewSpace = view * vec4(fragPosWorldSpace, 1.0);
  31. float depthValue = abs(fragPosViewSpace.z);
  32.  
  33. int layer = -1;
  34. for (int i = 0; i < cascadeCount; ++i)
  35. {
  36. if (depthValue < cascadePlaneDistances[i])
  37. {
  38. layer = i;
  39. break;
  40. }
  41. }
  42. if (layer == -1)
  43. {
  44. layer = cascadeCount;
  45. }
  46.  
  47. vec4 fragPosLightSpace = lightSpaceMatrices[layer] * vec4(fragPosWorldSpace, 1.0);
  48. // perform perspective divide
  49. vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
  50. // transform to [0,1] range
  51. projCoords = projCoords * 0.5 + 0.5;
  52.  
  53. // get depth of current fragment from light's perspective
  54. float currentDepth = projCoords.z;
  55.  
  56. // keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
  57. if (currentDepth > 1.0)
  58. {
  59. return 0.0;
  60. }
  61. // calculate bias (based on depth map resolution and slope)
  62. vec3 normal = normalize(fs_in.Normal);
  63. float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
  64. const float biasModifier = 0.5f;
  65. if (layer == cascadeCount)
  66. {
  67. bias *= 1 / (farPlane * biasModifier);
  68. }
  69. else
  70. {
  71. bias *= 1 / (cascadePlaneDistances[layer] * biasModifier);
  72. }
  73.  
  74. // PCF
  75. float shadow = 0.0;
  76. vec2 texelSize = 1.0 / vec2(textureSize(shadowMap, 0));
  77. for(int x = -1; x <= 1; ++x)
  78. {
  79. for(int y = -1; y <= 1; ++y)
  80. {
  81. float pcfDepth = texture(shadowMap, vec3(projCoords.xy + vec2(x, y) * texelSize, layer)).r;
  82. shadow += (currentDepth - bias) > pcfDepth ? 1.0 : 0.0;
  83. }
  84. }
  85. shadow /= 9.0;
  86.  
  87. return shadow;
  88. }
  89.  
  90. void main()
  91. {
  92.  
  93. // vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb;
  94. vec3 color = texture(cubeMapTexture, fs_in.FragPos).rgb;
  95. vec3 normal = normalize(fs_in.Normal);
  96. vec3 lightColor = vec3(1.0);
  97. // ambient
  98. vec3 ambient = 0.3 * color;
  99. // diffuse
  100. float diff = max(dot(lightDir, normal), 0.0);
  101. vec3 diffuse = diff * lightColor;
  102. // specular
  103. vec3 viewDir = normalize(viewPos - fs_in.FragPos);
  104. vec3 reflectDir = reflect(-lightDir, normal);
  105. float spec = 0.0;
  106. vec3 halfwayDir = normalize(lightDir + viewDir);
  107. spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0);
  108. vec3 specular = spec * lightColor;
  109. // calculate shadow
  110. float shadow = ShadowCalculation(fs_in.FragPos);
  111. vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color;
  112.  
  113. FragColor = vec4(lighting, 1.0);
  114. // FragColor = vec4(lighting, 1.0) * texture(cubeMapTexture, fs_in.TexCoords);
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment