ForwardShadingCommonFrag.glsl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_SHADERS_FORWARD_SHADING_COMMON_FRAG_GLSL
  6. #define ANKI_SHADERS_FORWARD_SHADING_COMMON_FRAG_GLSL
  7. // Common code for all fragment shaders of BS
  8. #include "shaders/Common.glsl"
  9. #include "shaders/Functions.glsl"
  10. #include "shaders/Clusterer.glsl"
  11. // Global resources
  12. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D anki_msDepthRt;
  13. #define LIGHT_SET 0
  14. #define LIGHT_UBO_BINDING 0
  15. #define LIGHT_SS_BINDING 0
  16. #define LIGHT_TEX_BINDING 1
  17. #define LIGHT_LIGHTS
  18. #define LIGHT_COMMON_UNIS
  19. #include "shaders/ClusterLightCommon.glsl"
  20. #define anki_u_time u_time
  21. #define RENDERER_SIZE (u_rendererSize * 0.5)
  22. layout(location = 0) out vec4 out_color;
  23. void writeGBuffer(in vec4 color)
  24. {
  25. out_color = vec4(color.rgb, 1.0 - color.a);
  26. }
  27. vec4 readAnimatedTextureRgba(sampler2DArray tex, float period, vec2 uv, float time)
  28. {
  29. float layerCount = float(textureSize(tex, 0).z);
  30. float layer = mod(time * layerCount / period, layerCount);
  31. return texture(tex, vec3(uv, layer));
  32. }
  33. vec3 computeLightColor(vec3 diffCol, vec3 worldPos)
  34. {
  35. vec3 outColor = vec3(0.0);
  36. // Find the cluster and then the light counts
  37. uint clusterIdx = computeClusterIndex(
  38. u_clustererMagic, gl_FragCoord.xy / RENDERER_SIZE, worldPos, u_clusterCountX, u_clusterCountY);
  39. uint idxOffset = u_clusters[clusterIdx];
  40. // Skip decals
  41. uint count = u_lightIndices[idxOffset];
  42. idxOffset += count + 1;
  43. // Point lights
  44. count = u_lightIndices[idxOffset++];
  45. uint idxOffsetEnd = idxOffset + count;
  46. ANKI_LOOP while(idxOffset < idxOffsetEnd)
  47. {
  48. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  49. vec3 diffC = diffuseLambert(diffCol) * light.diffuseColorTileSize.rgb;
  50. vec3 frag2Light = light.posRadius.xyz - worldPos;
  51. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  52. #if LOD > 1
  53. const float shadow = 1.0;
  54. #else
  55. float shadow = 1.0;
  56. if(light.diffuseColorTileSize.w >= 0.0)
  57. {
  58. shadow = computeShadowFactorOmni(
  59. frag2Light, light.radiusPad1.x, light.atlasTiles, light.diffuseColorTileSize.w, u_shadowTex);
  60. }
  61. #endif
  62. outColor += diffC * (att * shadow);
  63. }
  64. // Spot lights
  65. count = u_lightIndices[idxOffset++];
  66. idxOffsetEnd = idxOffset + count;
  67. ANKI_LOOP while(idxOffset < idxOffsetEnd)
  68. {
  69. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  70. vec3 diffC = diffuseLambert(diffCol) * light.diffuseColorShadowmapId.rgb;
  71. vec3 frag2Light = light.posRadius.xyz - worldPos;
  72. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  73. vec3 l = normalize(frag2Light);
  74. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDirRadius.xyz);
  75. #if LOD > 1
  76. const float shadow = 1.0;
  77. #else
  78. float shadow = 1.0;
  79. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  80. if(shadowmapLayerIdx >= 0.0)
  81. {
  82. shadow = computeShadowFactorSpot(light.texProjectionMat, worldPos, light.lightDirRadius.w, u_shadowTex);
  83. }
  84. #endif
  85. outColor += diffC * (att * spot * shadow);
  86. }
  87. return outColor;
  88. }
  89. void particleAlpha(vec4 color, vec4 scaleColor, vec4 biasColor)
  90. {
  91. writeGBuffer(color * scaleColor + biasColor);
  92. }
  93. void fog(vec3 color, float fogAlphaScale, float fogDistanceOfMaxThikness, float zVSpace)
  94. {
  95. const vec2 screenSize = 1.0 / RENDERER_SIZE;
  96. vec2 texCoords = gl_FragCoord.xy * screenSize;
  97. float depth = texture(anki_msDepthRt, texCoords).r;
  98. float zFeatherFactor;
  99. vec4 fragPosVspace4 = u_invProjMat * vec4(vec3(UV_TO_NDC(texCoords), depth), 1.0);
  100. float sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
  101. float diff = max(0.0, zVSpace - sceneZVspace);
  102. zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
  103. writeGBuffer(vec4(color, zFeatherFactor * fogAlphaScale));
  104. }
  105. #endif